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