Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / lustre / mdt / mdt_hsm_cdt_client.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  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
24  *     alternatives
25  *
26  * Copyright (c) 2013, 2017, Intel Corporation.
27  */
28 /*
29  * lustre/mdt/mdt_hsm_cdt_client.c
30  *
31  * Lustre HSM Coordinator
32  *
33  * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include <obd_support.h>
40 #include <lustre_export.h>
41 #include <obd.h>
42 #include <lprocfs_status.h>
43 #include <lustre_log.h>
44 #include "mdt_internal.h"
45
46 /**
47  * data passed to llog_cat_process() callback
48  * to find compatible requests
49  */
50 struct hsm_compat_data_cb {
51         struct coordinator      *cdt;
52         struct hsm_action_list  *hal;
53 };
54
55 /**
56  * llog_cat_process() callback, used to find record
57  * compatibles with a new hsm_action_list
58  * \param env [IN] environment
59  * \param llh [IN] llog handle
60  * \param hdr [IN] llog record
61  * \param data [IN] cb data = hsm_compat_data_cb
62  * \retval 0 success
63  * \retval -ve failure
64  */
65 static int hsm_find_compatible_cb(const struct lu_env *env,
66                                   struct llog_handle *llh,
67                                   struct llog_rec_hdr *hdr, void *data)
68 {
69         struct llog_agent_req_rec       *larr;
70         struct hsm_compat_data_cb       *hcdcb;
71         struct hsm_action_item          *hai;
72         int                              i;
73         ENTRY;
74
75         larr = (struct llog_agent_req_rec *)hdr;
76         hcdcb = data;
77         /* a compatible request must be WAITING or STARTED
78          * and not a cancel */
79         if ((larr->arr_status != ARS_WAITING &&
80              larr->arr_status != ARS_STARTED) ||
81             larr->arr_hai.hai_action == HSMA_CANCEL)
82                 RETURN(0);
83
84         hai = hai_first(hcdcb->hal);
85         for (i = 0; i < hcdcb->hal->hal_count; i++, hai = hai_next(hai)) {
86                 /* if request is a CANCEL:
87                  * if cookie set in the request, there is no need to find a
88                  * compatible one, the cookie in the request is directly used.
89                  * if cookie is not set, we use the FID to find the request
90                  * to cancel (the "compatible" one)
91                  * if the caller sets the cookie, we assume he also sets the
92                  * arr_archive_id
93                  */
94                 if (hai->hai_action == HSMA_CANCEL && hai->hai_cookie != 0)
95                         continue;
96
97                 if (!lu_fid_eq(&hai->hai_fid, &larr->arr_hai.hai_fid))
98                         continue;
99
100                 /* HSMA_NONE is used to find running request for some FID */
101                 if (hai->hai_action == HSMA_NONE) {
102                         hcdcb->hal->hal_archive_id = larr->arr_archive_id;
103                         hcdcb->hal->hal_flags = larr->arr_flags;
104                         *hai = larr->arr_hai;
105                         continue;
106                 }
107                 /* in V1 we do not manage partial transfer
108                  * so extent is always whole file
109                  */
110                 hai->hai_cookie = larr->arr_hai.hai_cookie;
111                 /* we read the archive number from the request we cancel */
112                 if (hai->hai_action == HSMA_CANCEL &&
113                     hcdcb->hal->hal_archive_id == 0)
114                         hcdcb->hal->hal_archive_id = larr->arr_archive_id;
115         }
116         RETURN(0);
117 }
118
119 /**
120  * find compatible requests already recorded
121  * \param env [IN] environment
122  * \param mdt [IN] MDT device
123  * \param hal [IN/OUT] new request
124  *    cookie set to compatible found or to 0 if not found
125  *    for cancel request, see callback hsm_find_compatible_cb()
126  * \retval 0 success
127  * \retval -ve failure
128  */
129 static int hsm_find_compatible(const struct lu_env *env, struct mdt_device *mdt,
130                                struct hsm_action_list *hal)
131 {
132         struct hsm_action_item          *hai;
133         struct hsm_compat_data_cb        hcdcb;
134         int                              rc, i, ok_cnt;
135         ENTRY;
136
137         ok_cnt = 0;
138         hai = hai_first(hal);
139         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
140                 /* in a cancel request hai_cookie may be set by caller to
141                  * show the request to be canceled
142                  * if not we need to search by FID
143                  */
144                 if (hai->hai_action == HSMA_CANCEL && hai->hai_cookie != 0)
145                         ok_cnt++;
146                 else
147                         hai->hai_cookie = 0;
148         }
149
150         /* if all requests are cancel with cookie, no need to find compatible */
151         if (ok_cnt == hal->hal_count)
152                 RETURN(0);
153
154         hcdcb.cdt = &mdt->mdt_coordinator;
155         hcdcb.hal = hal;
156
157         rc = cdt_llog_process(env, mdt, hsm_find_compatible_cb, &hcdcb, 0, 0,
158                               READ);
159
160         RETURN(rc);
161 }
162
163 /**
164  * check if an action is really needed
165  * \param hai [IN] request description
166  * \param hal_an [IN] request archive number (not used)
167  * \param rq_flags [IN] request flags
168  * \param hsm [IN] file HSM metadata
169  * \retval boolean
170  */
171 static bool hsm_action_is_needed(struct hsm_action_item *hai, int hal_an,
172                                  __u64 rq_flags, struct md_hsm *hsm)
173 {
174         bool     is_needed = false;
175         int      hsm_flags;
176         ENTRY;
177
178         if (rq_flags & HSM_FORCE_ACTION)
179                 RETURN(true);
180
181         hsm_flags = hsm->mh_flags;
182         switch (hai->hai_action) {
183         case HSMA_ARCHIVE:
184                 if (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED))
185                         is_needed = true;
186                 break;
187         case HSMA_RESTORE:
188                 /* if file is dirty we must return an error, this function
189                  * cannot, so we ask for an action and
190                  * mdt_hsm_is_action_compat() will return an error
191                  */
192                 if (hsm_flags & (HS_RELEASED | HS_DIRTY))
193                         is_needed = true;
194                 break;
195         case HSMA_REMOVE:
196                 if (hsm_flags & (HS_ARCHIVED | HS_EXISTS))
197                         is_needed = true;
198                 break;
199         case HSMA_CANCEL:
200                 is_needed = true;
201                 break;
202         }
203         CDEBUG(D_HSM, "fid="DFID" action=%s rq_flags=%#llx"
204                       " extent=%#llx-%#llx hsm_flags=%X %s\n",
205                       PFID(&hai->hai_fid),
206                       hsm_copytool_action2name(hai->hai_action), rq_flags,
207                       hai->hai_extent.offset, hai->hai_extent.length,
208                       hsm->mh_flags,
209                       (is_needed ? "action needed" : "no action needed"));
210
211         RETURN(is_needed);
212 }
213
214 /**
215  * test sanity of an hal
216  * FID must be valid
217  * action must be known
218  * \param hal [IN]
219  * \retval boolean
220  */
221 static bool hal_is_sane(struct hsm_action_list *hal)
222 {
223         int                      i;
224         struct hsm_action_item  *hai;
225         ENTRY;
226
227         if (hal->hal_count == 0)
228                 RETURN(false);
229
230         hai = hai_first(hal);
231         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
232                 if (!fid_is_sane(&hai->hai_fid))
233                         RETURN(false);
234                 switch (hai->hai_action) {
235                 case HSMA_NONE:
236                 case HSMA_ARCHIVE:
237                 case HSMA_RESTORE:
238                 case HSMA_REMOVE:
239                 case HSMA_CANCEL:
240                         break;
241                 default:
242                         RETURN(false);
243                 }
244         }
245         RETURN(true);
246 }
247
248 static int
249 hsm_action_permission(struct mdt_thread_info *mti,
250                       struct mdt_object *obj,
251                       enum hsm_copytool_action hsma)
252 {
253         struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
254         struct lu_ucred *uc = mdt_ucred(mti);
255         struct md_attr *ma = &mti->mti_attr;
256         const __u64 *mask;
257         int rc;
258         ENTRY;
259
260         if (hsma != HSMA_RESTORE && mdt_rdonly(mti->mti_exp))
261                 RETURN(-EROFS);
262
263         if (md_capable(uc, CFS_CAP_SYS_ADMIN))
264                 RETURN(0);
265
266         ma->ma_need = MA_INODE;
267         rc = mdt_attr_get_complex(mti, obj, ma);
268         if (rc < 0)
269                 RETURN(rc);
270
271         if (uc->uc_fsuid == ma->ma_attr.la_uid)
272                 mask = &cdt->cdt_user_request_mask;
273         else if (lustre_in_group_p(uc, ma->ma_attr.la_gid))
274                 mask = &cdt->cdt_group_request_mask;
275         else
276                 mask = &cdt->cdt_other_request_mask;
277
278         if (!(0 <= hsma && hsma < 8 * sizeof(*mask)))
279                 RETURN(-EINVAL);
280
281         RETURN(*mask & (1UL << hsma) ? 0 : -EPERM);
282 }
283
284 /* Process a single HAL. hsm_find_compatible has already been called
285  * on it. */
286 static int mdt_hsm_register_hal(struct mdt_thread_info *mti,
287                                 struct mdt_device *mdt,
288                                 struct coordinator *cdt,
289                                 struct hsm_action_list *hal)
290 {
291         struct hsm_action_item  *hai;
292         struct mdt_object       *obj = NULL;
293         int                      rc, i;
294         struct md_hsm            mh;
295         bool                     is_restore = false;
296         __u64                    compound_id;
297
298         compound_id = atomic_inc_return(&cdt->cdt_compound_id);
299
300         hai = hai_first(hal);
301         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
302                 int archive_id;
303                 __u64 flags;
304
305                 /* default archive number is the one explicitly specified */
306                 archive_id = hal->hal_archive_id;
307                 flags = hal->hal_flags;
308
309                 /* by default, data FID is same as Lustre FID */
310                 /* the volatile data FID will be created by copy tool and
311                  * send from the agent through the progress call */
312                 hai->hai_dfid = hai->hai_fid;
313
314                 /* done here to manage first and redundant requests cases */
315                 if (hai->hai_action == HSMA_RESTORE)
316                         is_restore = true;
317
318                 /* test result of hsm_find_compatible()
319                  * if request redundant or cancel of nothing
320                  * do not record
321                  */
322                 /* redundant case */
323                 if (hai->hai_action != HSMA_CANCEL && hai->hai_cookie != 0)
324                         continue;
325                 /* cancel nothing case */
326                 if (hai->hai_action == HSMA_CANCEL && hai->hai_cookie == 0)
327                         continue;
328
329                 /* new request or cancel request
330                  * we search for HSM status flags to check for compatibility
331                  * if restore, we take the layout lock
332                  */
333
334                 /* Get HSM attributes and check permissions. */
335                 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &mh);
336                 if (IS_ERR(obj)) {
337                         /* In case of REMOVE and CANCEL a Lustre file
338                          * is not mandatory, but restrict this
339                          * exception to admins. */
340                         if (md_capable(mdt_ucred(mti), CFS_CAP_SYS_ADMIN) &&
341                             (hai->hai_action == HSMA_REMOVE ||
342                              hai->hai_action == HSMA_CANCEL))
343                                 goto record;
344                         else
345                                 GOTO(out, rc = PTR_ERR(obj));
346                 }
347
348                 rc = hsm_action_permission(mti, obj, hai->hai_action);
349                 mdt_object_put(mti->mti_env, obj);
350
351                 if (rc < 0)
352                         GOTO(out, rc);
353
354                 /* if action is cancel, also no need to check */
355                 if (hai->hai_action == HSMA_CANCEL)
356                         goto record;
357
358                 /* Check if an action is needed, compare request
359                  * and HSM flags status */
360                 if (!hsm_action_is_needed(hai, archive_id, flags, &mh))
361                         continue;
362
363                 /* Check if file request is compatible with HSM flags status
364                  * and stop at first incompatible
365                  */
366                 if (!mdt_hsm_is_action_compat(hai, archive_id, flags, &mh))
367                         GOTO(out, rc = -EPERM);
368
369                 /* for cancel archive number is taken from canceled request
370                  * for other request, we take from lma if not specified,
371                  * or we use the default if none found in lma
372                  * this works also for archive because the default value is 0
373                  * /!\ there is a side effect: in case of restore on multiple
374                  * files which are in different backend, the initial compound
375                  * request will be split in multiple requests because we cannot
376                  * warranty an agent can serve any combinaison of archive
377                  * backend
378                  */
379                 if (hai->hai_action != HSMA_CANCEL && archive_id == 0) {
380                         if (mh.mh_arch_id != 0)
381                                 archive_id = mh.mh_arch_id;
382                         else
383                                 archive_id = cdt->cdt_default_archive_id;
384                 }
385
386                 /* if restore, take an exclusive lock on layout */
387                 if (hai->hai_action == HSMA_RESTORE) {
388                         struct cdt_restore_handle *crh;
389
390                         /* in V1 only whole file is supported. */
391                         if (hai->hai_extent.offset != 0)
392                                 GOTO(out, rc = -EPROTO);
393
394                         OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
395                         if (crh == NULL)
396                                 GOTO(out, rc = -ENOMEM);
397
398                         crh->crh_fid = hai->hai_fid;
399                         /* in V1 only whole file is supported. However the
400                          * restore may be due to truncate. */
401                         crh->crh_extent.start = 0;
402                         crh->crh_extent.end = hai->hai_extent.length;
403
404                         mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
405                         obj = mdt_object_find_lock(mti, &crh->crh_fid,
406                                                    &crh->crh_lh,
407                                                    MDS_INODELOCK_LAYOUT);
408                         if (IS_ERR(obj)) {
409                                 rc = PTR_ERR(obj);
410                                 CERROR("%s: cannot take layout lock for "
411                                        DFID": rc = %d\n", mdt_obd_name(mdt),
412                                        PFID(&crh->crh_fid), rc);
413                                 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
414                                 GOTO(out, rc);
415                         }
416
417                         /* we choose to not keep a keep a reference
418                          * on the object during the restore time which can be
419                          * very long */
420                         mdt_object_put(mti->mti_env, obj);
421
422                         mutex_lock(&cdt->cdt_restore_lock);
423                         if (unlikely((cdt->cdt_state == CDT_STOPPED) ||
424                                      (cdt->cdt_state == CDT_STOPPING))) {
425                                 mutex_unlock(&cdt->cdt_restore_lock);
426                                 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
427                                 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
428                                 GOTO(out, rc = -EAGAIN);
429                         }
430                         list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
431                         mutex_unlock(&cdt->cdt_restore_lock);
432                 }
433 record:
434                 /*
435                  * Wait here to catch the 2nd RESTORE request to the same FID.
436                  * Normally layout lock protects against adding such request.
437                  * But when cdt is stopping it cancel all locks via
438                  * ldlm_resource_clean and protections may not work.
439                  * See LU-9266 and sanity-hsm_407 for details.
440                  */
441                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_HSM_CDT_DELAY, cfs_fail_val);
442                 /* record request */
443                 rc = mdt_agent_record_add(mti->mti_env, mdt, compound_id,
444                                           archive_id, flags, hai);
445                 if (rc)
446                         GOTO(out, rc);
447         }
448         if (is_restore &&
449             (cdt->cdt_policy & CDT_NONBLOCKING_RESTORE))
450                 rc = -ENODATA;
451         else
452                 rc = 0;
453
454         GOTO(out, rc);
455
456 out:
457         return rc;
458 }
459
460 /*
461  * Coordinator external API
462  */
463
464 /**
465  * register a list of requests
466  * \param mti [IN]
467  * \param hal [IN] list of requests
468  * \retval 0 success
469  * \retval -ve failure
470  * in case of restore, caller must hold layout lock
471  */
472 int mdt_hsm_add_actions(struct mdt_thread_info *mti,
473                         struct hsm_action_list *hal)
474 {
475         struct mdt_device       *mdt = mti->mti_mdt;
476         struct coordinator      *cdt = &mdt->mdt_coordinator;
477         int                      rc;
478         ENTRY;
479
480         /* no coordinator started, so we cannot serve requests */
481         if (cdt->cdt_state == CDT_STOPPED)
482                 RETURN(-EAGAIN);
483
484         if (!hal_is_sane(hal))
485                 RETURN(-EINVAL);
486
487         /* search for compatible request, if found hai_cookie is set
488          * to the request cookie
489          * it is also used to set the cookie for cancel request by FID
490          */
491         rc = hsm_find_compatible(mti->mti_env, mdt, hal);
492         if (rc)
493                 GOTO(out, rc);
494
495         rc = mdt_hsm_register_hal(mti, mdt, cdt, hal);
496
497         GOTO(out, rc);
498 out:
499         /* if work has been added, signal the coordinator */
500         if (rc == 0 || rc == -ENODATA)
501                 mdt_hsm_cdt_event(cdt);
502
503         return rc;
504 }
505
506 /**
507  * check if a restore is running on a FID
508  * this is redundant with mdt_hsm_coordinator_get_running()
509  * but as it can be called frequently when getting attr
510  * we make an optimized/simpler version only for a FID
511  * \param mti [IN]
512  * \param fid [IN] file FID
513  * \retval boolean
514  */
515 bool mdt_hsm_restore_is_running(struct mdt_thread_info *mti,
516                                 const struct lu_fid *fid)
517 {
518         struct mdt_device               *mdt = mti->mti_mdt;
519         struct coordinator              *cdt = &mdt->mdt_coordinator;
520         struct cdt_restore_handle       *crh;
521         bool                             rc = false;
522         ENTRY;
523
524         if (!fid_is_sane(fid))
525                 RETURN(rc);
526
527         mutex_lock(&cdt->cdt_restore_lock);
528         list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
529                 if (lu_fid_eq(&crh->crh_fid, fid)) {
530                         rc = true;
531                         break;
532                 }
533         }
534         mutex_unlock(&cdt->cdt_restore_lock);
535         RETURN(rc);
536 }
537
538 /**
539  * get registered action on a FID list
540  * \param mti [IN]
541  * \param hal [IN/OUT] requests
542  * \retval 0 success
543  * \retval -ve failure
544  */
545 int mdt_hsm_get_actions(struct mdt_thread_info *mti,
546                         struct hsm_action_list *hal)
547 {
548         struct mdt_device       *mdt = mti->mti_mdt;
549         struct coordinator      *cdt = &mdt->mdt_coordinator;
550         struct hsm_action_item  *hai;
551         int                      i, rc;
552         ENTRY;
553
554         hai = hai_first(hal);
555         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
556                 hai->hai_action = HSMA_NONE;
557                 if (!fid_is_sane(&hai->hai_fid))
558                         RETURN(-EINVAL);
559         }
560
561         /* 1st we search in recorded requests */
562         rc = hsm_find_compatible(mti->mti_env, mdt, hal);
563         /* if llog file is not created, no action is recorded */
564         if (rc == -ENOENT)
565                 RETURN(0);
566
567         if (rc)
568                 RETURN(rc);
569
570         /* 2nd we search if the request are running
571          * cookie is cleared to tell to caller, the request is
572          * waiting
573          * we could in place use the record status, but in the future
574          * we may want do give back dynamic informations on the
575          * running request
576          */
577         hai = hai_first(hal);
578         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
579                 struct cdt_agent_req *car;
580
581                 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
582                 if (car == NULL) {
583                         hai->hai_cookie = 0;
584                 } else {
585                         __u64 data_moved;
586
587                         mdt_cdt_get_work_done(car, &data_moved);
588                         /* this is just to give the volume of data moved
589                          * it means data_moved data have been moved from the
590                          * original request but we do not know which one
591                          */
592                         hai->hai_extent.length = data_moved;
593                         mdt_cdt_put_request(car);
594                 }
595         }
596
597         RETURN(0);
598 }