Whamcloud - gitweb
LU-3342 mdt: HSM coordinator agent interface
[fs/lustre-release.git] / lustre / mdt / mdt_hsm.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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.
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
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
26  *                          alternatives
27  */
28 /*
29  * lustre/mdt/mdt_hsm.c
30  *
31  * Lustre Metadata Target (mdt) request handler
32  *
33  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
34  * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include "mdt_internal.h"
40
41 /* Max allocation to satisfy single HSM RPC. */
42 #define MDT_HSM_ALLOC_MAX (1 << 20)
43
44 #define MDT_HSM_ALLOC(ptr, size)                        \
45         do {                                            \
46                 if ((size) <= MDT_HSM_ALLOC_MAX)        \
47                         OBD_ALLOC_LARGE((ptr), (size)); \
48                 else                                    \
49                         (ptr) = NULL;                   \
50         } while (0)
51
52 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
53
54 /**
55  * Update on-disk HSM attributes.
56  */
57 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
58                      struct md_hsm *mh)
59 {
60         struct md_object        *next = mdt_object_child(obj);
61         struct lu_buf           *buf = &info->mti_buf;
62         struct hsm_attrs        *attrs;
63         int                      rc;
64         ENTRY;
65
66         attrs = (struct hsm_attrs *)info->mti_xattr_buf;
67         CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
68
69         /* pack HSM attributes */
70         lustre_hsm2buf(info->mti_xattr_buf, mh);
71
72         /* update SOM attributes */
73         buf->lb_buf = attrs;
74         buf->lb_len = sizeof(*attrs);
75         rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
76
77         RETURN(rc);
78 }
79
80 /**
81  * Extract information coming from a copytool and asks coordinator to update
82  * a request status depending on the update content.
83  *
84  * Copytools could use this to report failure in their process.
85  *
86  * This is HSM_PROGRESS RPC handler.
87  */
88 int mdt_hsm_progress(struct mdt_thread_info *info)
89 {
90         struct mdt_body                 *body;
91         struct hsm_progress_kernel      *hpk;
92         int                              rc;
93         ENTRY;
94
95         body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
96         if (body == NULL)
97                 RETURN(-EPROTO);
98
99         hpk = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_PROGRESS);
100         if (hpk == NULL)
101                 RETURN(-EPROTO);
102
103         hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
104
105         CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
106                PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
107
108         if (hpk->hpk_errval)
109                 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
110                        PFID(&hpk->hpk_fid), hpk->hpk_errval,
111                        hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
112
113         if (hpk->hpk_flags & HP_FLAG_COMPLETED)
114                 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
115                        PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
116
117         rc = mdt_hsm_coordinator_update(info, hpk);
118
119         RETURN(rc);
120 }
121
122 int mdt_hsm_ct_register(struct mdt_thread_info *info)
123 {
124         struct mdt_body         *body;
125         struct ptlrpc_request   *req = mdt_info_req(info);
126         __u32                   *archives;
127         int                      rc;
128         ENTRY;
129
130         body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
131         if (body == NULL)
132                 RETURN(-EPROTO);
133
134         archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
135         if (archives == NULL)
136                 RETURN(-EPROTO);
137
138         /* XXX: directly include this function here? */
139         rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
140                                          *archives);
141
142         RETURN(rc);
143 }
144
145 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
146 {
147         struct mdt_body         *body;
148         struct ptlrpc_request   *req = mdt_info_req(info);
149         int                      rc;
150         ENTRY;
151
152         body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
153         if (body == NULL)
154                 RETURN(-EPROTO);
155
156         /* XXX: directly include this function here? */
157         rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
158
159         RETURN(rc);
160 }
161
162 /**
163  * Retrieve the current HSM flags, archive id and undergoing HSM requests for
164  * the fid provided in RPC body.
165  *
166  * Current requests are read from coordinator states.
167  *
168  * This is MDS_HSM_STATE_GET RPC handler.
169  */
170 int mdt_hsm_state_get(struct mdt_thread_info *info)
171 {
172         struct mdt_object       *obj = info->mti_object;
173         struct md_attr          *ma  = &info->mti_attr;
174         struct hsm_user_state   *hus;
175         struct mdt_lock_handle  *lh;
176         int                      rc;
177         ENTRY;
178
179         lh = &info->mti_lh[MDT_LH_CHILD];
180         mdt_lock_reg_init(lh, LCK_PR);
181         rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
182                              MDT_LOCAL_LOCK);
183         if (rc)
184                 RETURN(rc);
185
186         /* Only valid if client is remote */
187         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
188         if (rc)
189                 GOTO(out_unlock, rc = err_serious(rc));
190
191         ma->ma_valid = 0;
192         ma->ma_need = MA_HSM;
193         rc = mdt_attr_get_complex(info, obj, ma);
194         if (rc)
195                 GOTO(out_ucred, rc);
196
197         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
198                 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
199                             req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
200
201         hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
202         if (hus == NULL)
203                 GOTO(out_ucred, rc = -EPROTO);
204
205         /* Current HSM flags */
206         hus->hus_states = ma->ma_hsm.mh_flags;
207         hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
208
209         EXIT;
210 out_ucred:
211         mdt_exit_ucred(info);
212 out_unlock:
213         mdt_object_unlock(info, obj, lh, 1);
214         return rc;
215 }
216
217 /**
218  * Change HSM state and archive number of a file.
219  *
220  * Archive number is changed iif the value is not 0.
221  * The new flagset that will be computed should result in a coherent state.
222  * This function checks that are flags are compatible.
223  *
224  * This is MDS_HSM_STATE_SET RPC handler.
225  */
226 int mdt_hsm_state_set(struct mdt_thread_info *info)
227 {
228         struct mdt_object       *obj = info->mti_object;
229         struct md_attr          *ma = &info->mti_attr;
230         struct hsm_state_set    *hss;
231         struct mdt_lock_handle  *lh;
232         int                      rc;
233         __u64                    flags;
234         ENTRY;
235
236         lh = &info->mti_lh[MDT_LH_CHILD];
237         mdt_lock_reg_init(lh, LCK_PW);
238         rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
239                              MDT_LOCAL_LOCK);
240         if (rc)
241                 RETURN(rc);
242
243         /* Only valid if client is remote */
244         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
245         if (rc)
246                 GOTO(out_obj, rc = err_serious(rc));
247
248         /* Read current HSM info */
249         ma->ma_valid = 0;
250         ma->ma_need = MA_HSM;
251         rc = mdt_attr_get_complex(info, obj, ma);
252         if (rc)
253                 GOTO(out_ucred, rc);
254
255         hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
256         if (hss == NULL)
257                 GOTO(out_ucred, rc = -EPROTO);
258
259         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
260                 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
261                             req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
262
263         /* Change HSM flags depending on provided masks */
264         if (hss->hss_valid & HSS_SETMASK)
265                 ma->ma_hsm.mh_flags |= hss->hss_setmask;
266         if (hss->hss_valid & HSS_CLEARMASK)
267                 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
268
269         /* Change archive_id if provided. */
270         if (hss->hss_valid & HSS_ARCHIVE_ID) {
271                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
272                         CDEBUG(D_HSM, "Could not set an archive number for "
273                                DFID "if HSM EXISTS flag is not set.\n",
274                                PFID(&info->mti_body->fid1));
275                         GOTO(out_ucred, rc);
276                 }
277                 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
278         }
279
280         /* Check for inconsistant HSM flagset.
281          * DIRTY without EXISTS: no dirty if no archive was created.
282          * DIRTY and RELEASED: a dirty file could not be released.
283          * RELEASED without ARCHIVED: do not release a non-archived file.
284          * LOST without ARCHIVED: cannot lost a non-archived file.
285          */
286         flags = ma->ma_hsm.mh_flags;
287         if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
288             ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
289             ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
290             ((flags & HS_LOST)     && !(flags & HS_ARCHIVED))) {
291                 CDEBUG(D_HSM, "Incompatible flag change on "DFID
292                               "flags="LPX64"\n",
293                        PFID(&info->mti_body->fid1), flags);
294                 GOTO(out_ucred, rc = -EINVAL);
295         }
296
297         /* Save the modified flags */
298         rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
299         if (rc)
300                 GOTO(out_ucred, rc);
301
302         EXIT;
303
304 out_ucred:
305         mdt_exit_ucred(info);
306 out_obj:
307         mdt_object_unlock(info, obj, lh, 1);
308         return rc;
309 }
310
311 /**
312  * Retrieve undergoing HSM requests for the fid provided in RPC body.
313  * Current requests are read from coordinator states.
314  *
315  * This is MDS_HSM_ACTION RPC handler.
316  */
317 int mdt_hsm_action(struct mdt_thread_info *info)
318 {
319         struct hsm_current_action       *hca;
320         struct hsm_action_list          *hal = NULL;
321         struct hsm_action_item          *hai;
322         int                              hal_size;
323         int                              rc;
324         ENTRY;
325
326         /* Only valid if client is remote */
327         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
328         if (rc)
329                 RETURN(rc = err_serious(rc));
330
331         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
332                 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
333                                  req_capsule_client_get(info->mti_pill,
334                                                         &RMF_CAPA1));
335
336         hca = req_capsule_server_get(info->mti_pill,
337                                      &RMF_MDS_HSM_CURRENT_ACTION);
338         if (hca == NULL)
339                 GOTO(out_ucred, rc = -EPROTO);
340
341         /* Coordinator information */
342         hal_size = sizeof(*hal) +
343                    cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
344                    cfs_size_round(sizeof(*hai));
345
346         MDT_HSM_ALLOC(hal, hal_size);
347         if (hal == NULL)
348                 GOTO(out_ucred, rc = -ENOMEM);
349
350         hal->hal_version = HAL_VERSION;
351         hal->hal_archive_id = 0;
352         hal->hal_flags = 0;
353         obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
354                         MTI_NAME_MAXLEN);
355         hal->hal_count = 1;
356         hai = hai_zero(hal);
357         hai->hai_action = HSMA_NONE;
358         hai->hai_cookie = 0;
359         hai->hai_gid = 0;
360         hai->hai_fid = info->mti_body->fid1;
361         hai->hai_len = sizeof(*hai);
362
363         rc = mdt_hsm_get_actions(info, hal);
364         if (rc)
365                 GOTO(out_free, rc);
366
367         /* cookie is used to give back request status */
368         if (hai->hai_cookie == 0)
369                 hca->hca_state = HPS_WAITING;
370         else
371                 hca->hca_state = HPS_RUNNING;
372
373         switch (hai->hai_action) {
374         case HSMA_NONE:
375                 hca->hca_action = HUA_NONE;
376                 break;
377         case HSMA_ARCHIVE:
378                 hca->hca_action = HUA_ARCHIVE;
379                 break;
380         case HSMA_RESTORE:
381                 hca->hca_action = HUA_RESTORE;
382                 break;
383         case HSMA_REMOVE:
384                 hca->hca_action = HUA_REMOVE;
385                 break;
386         case HSMA_CANCEL:
387                 hca->hca_action = HUA_CANCEL;
388                 break;
389         default:
390                 hca->hca_action = HUA_NONE;
391                 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
392                        mdt_obd_name(info->mti_mdt),
393                        hai->hai_action, PFID(&hai->hai_fid));
394                 break;
395         }
396
397         hca->hca_location = hai->hai_extent;
398
399         EXIT;
400 out_free:
401         MDT_HSM_FREE(hal, hal_size);
402 out_ucred:
403         mdt_exit_ucred(info);
404         return rc;
405 }
406
407 /**
408  * Process the HSM actions described in a struct hsm_user_request.
409  *
410  * The action described in hur will be send to coordinator to be saved and
411  * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
412  *
413  * This is MDS_HSM_REQUEST RPC handler.
414  */
415 int mdt_hsm_request(struct mdt_thread_info *info)
416 {
417         struct req_capsule              *pill = info->mti_pill;
418         struct mdt_body                 *body;
419         struct hsm_request              *hr;
420         struct hsm_user_item            *hui;
421         struct hsm_action_list          *hal;
422         struct hsm_action_item          *hai;
423         const void                      *data;
424         int                              hui_list_size;
425         int                              data_size;
426         enum hsm_copytool_action         action = HSMA_NONE;
427         __u64                            compound_id;
428         int                              hal_size, i, rc;
429         ENTRY;
430
431         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
432         hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
433         hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
434         data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
435
436         if (body == NULL || hr == NULL || hui == NULL || data == NULL)
437                 RETURN(-EPROTO);
438
439         /* Sanity check. Nothing to do with an empty list */
440         if (hr->hr_itemcount == 0)
441                 RETURN(0);
442
443         hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
444                                              RCL_CLIENT);
445         if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
446                 RETURN(-EPROTO);
447
448         data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
449         if (data_size != hr->hr_data_len)
450                 RETURN(-EPROTO);
451
452         /* Only valid if client is remote */
453         rc = mdt_init_ucred(info, body);
454         if (rc)
455                 RETURN(err_serious(rc));
456
457         switch (hr->hr_action) {
458         /* code to be removed in hsm1_merge and final patch */
459         case HUA_RELEASE:
460                 CERROR("Release action is not working in hsm1_coord\n");
461                 GOTO(out_ucred, rc = -EINVAL);
462                 break;
463         /* end of code to be removed */
464         case HUA_ARCHIVE:
465                 action = HSMA_ARCHIVE;
466                 break;
467         case HUA_RESTORE:
468                 action = HSMA_RESTORE;
469                 break;
470         case HUA_REMOVE:
471                 action = HSMA_REMOVE;
472                 break;
473         case HUA_CANCEL:
474                 action = HSMA_CANCEL;
475                 break;
476         default:
477                 CERROR("Unknown hsm action: %d\n", hr->hr_action);
478                 GOTO(out_ucred, rc = -EINVAL);
479         }
480
481         hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
482                    (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
483                    hr->hr_itemcount;
484
485         MDT_HSM_ALLOC(hal, hal_size);
486         if (hal == NULL)
487                 GOTO(out_ucred, rc = -ENOMEM);
488
489         hal->hal_version = HAL_VERSION;
490         hal->hal_archive_id = hr->hr_archive_id;
491         hal->hal_flags = hr->hr_flags;
492         obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
493                         MTI_NAME_MAXLEN);
494
495         hal->hal_count = hr->hr_itemcount;
496         hai = hai_zero(hal);
497         for (i = 0; i < hr->hr_itemcount; i++, hai = hai_next(hai)) {
498                 hai->hai_action = action;
499                 hai->hai_cookie = 0;
500                 hai->hai_gid = 0;
501                 hai->hai_fid = hui[i].hui_fid;
502                 hai->hai_extent = hui[i].hui_extent;
503                 memcpy(hai->hai_data, data, hr->hr_data_len);
504                 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
505         }
506
507         rc = mdt_hsm_add_actions(info, hal, &compound_id);
508         /* ENODATA error code is needed only for implicit requests */
509         if (rc == -ENODATA)
510                 rc = 0;
511
512         MDT_HSM_FREE(hal, hal_size);
513
514         GOTO(out_ucred, rc);
515
516 out_ucred:
517         mdt_exit_ucred(info);
518         return rc;
519 }