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