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