Whamcloud - gitweb
LU-6017 obd: remove destroy cookie handling
[fs/lustre-release.git] / lustre / lov / lov_request.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include <libcfs/libcfs.h>
40
41 #include <obd_class.h>
42 #include <lustre/lustre_idl.h>
43
44 #include "lov_internal.h"
45
46 static void lov_init_set(struct lov_request_set *set)
47 {
48         set->set_count = 0;
49         atomic_set(&set->set_completes, 0);
50         atomic_set(&set->set_success, 0);
51         atomic_set(&set->set_finish_checked, 0);
52         INIT_LIST_HEAD(&set->set_list);
53         atomic_set(&set->set_refcount, 1);
54         init_waitqueue_head(&set->set_waitq);
55 }
56
57 void lov_finish_set(struct lov_request_set *set)
58 {
59         struct list_head *pos, *n;
60         struct lov_request *req;
61         ENTRY;
62
63         LASSERT(set != NULL);
64         list_for_each_safe(pos, n, &set->set_list) {
65                 req = list_entry(pos, struct lov_request, rq_link);
66                 list_del_init(&req->rq_link);
67
68                 if (req->rq_oi.oi_oa != NULL)
69                         OBDO_FREE(req->rq_oi.oi_oa);
70
71                 if (req->rq_oi.oi_osfs != NULL)
72                         OBD_FREE_PTR(req->rq_oi.oi_osfs);
73
74                 OBD_FREE_PTR(req);
75         }
76
77         OBD_FREE_PTR(set);
78         EXIT;
79 }
80
81 int lov_set_finished(struct lov_request_set *set, int idempotent)
82 {
83         int completes = atomic_read(&set->set_completes);
84
85         CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
86
87         if (completes == set->set_count) {
88                 if (idempotent)
89                         return 1;
90                 if (atomic_inc_return(&set->set_finish_checked) == 1)
91                         return 1;
92         }
93         return 0;
94 }
95
96 void lov_update_set(struct lov_request_set *set,
97                     struct lov_request *req, int rc)
98 {
99         req->rq_complete = 1;
100         req->rq_rc = rc;
101
102         atomic_inc(&set->set_completes);
103         if (rc == 0)
104                 atomic_inc(&set->set_success);
105
106         wake_up(&set->set_waitq);
107 }
108
109 int lov_update_common_set(struct lov_request_set *set,
110                           struct lov_request *req, int rc)
111 {
112         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
113         ENTRY;
114
115         lov_update_set(set, req, rc);
116
117         /* grace error on inactive ost */
118         if (rc && !(lov->lov_tgts[req->rq_idx] &&
119                     lov->lov_tgts[req->rq_idx]->ltd_active))
120                 rc = 0;
121
122         /* FIXME in raid1 regime, should return 0 */
123         RETURN(rc);
124 }
125
126 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
127 {
128         list_add_tail(&req->rq_link, &set->set_list);
129         set->set_count++;
130         req->rq_rqset = set;
131 }
132
133 static int lov_check_set(struct lov_obd *lov, int idx)
134 {
135         int rc = 0;
136         mutex_lock(&lov->lov_lock);
137
138         if (lov->lov_tgts[idx] == NULL ||
139             lov->lov_tgts[idx]->ltd_active ||
140             (lov->lov_tgts[idx]->ltd_exp != NULL &&
141              class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
142                 rc = 1;
143
144         mutex_unlock(&lov->lov_lock);
145         return rc;
146 }
147
148 /* Check if the OSC connection exists and is active.
149  * If the OSC has not yet had a chance to connect to the OST the first time,
150  * wait once for it to connect instead of returning an error.
151  */
152 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
153 {
154         wait_queue_head_t waitq;
155         struct l_wait_info lwi;
156         struct lov_tgt_desc *tgt;
157         int rc = 0;
158
159         mutex_lock(&lov->lov_lock);
160
161         tgt = lov->lov_tgts[ost_idx];
162
163         if (unlikely(tgt == NULL))
164                 GOTO(out, rc = 0);
165
166         if (likely(tgt->ltd_active))
167                 GOTO(out, rc = 1);
168
169         if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
170                 GOTO(out, rc = 0);
171
172         mutex_unlock(&lov->lov_lock);
173
174         init_waitqueue_head(&waitq);
175         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
176                                    cfs_time_seconds(1), NULL, NULL);
177
178         rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
179         if (tgt->ltd_active)
180                 return 1;
181
182         return 0;
183
184 out:
185         mutex_unlock(&lov->lov_lock);
186         return rc;
187 }
188
189 static int common_attr_done(struct lov_request_set *set)
190 {
191         struct list_head *pos;
192         struct lov_request *req;
193         struct obdo *tmp_oa;
194         int rc = 0, attrset = 0;
195         ENTRY;
196
197         LASSERT(set->set_oi != NULL);
198
199         if (set->set_oi->oi_oa == NULL)
200                 RETURN(0);
201
202         if (!atomic_read(&set->set_success))
203                 RETURN(-EIO);
204
205         OBDO_ALLOC(tmp_oa);
206         if (tmp_oa == NULL)
207                 GOTO(out, rc = -ENOMEM);
208
209         list_for_each(pos, &set->set_list) {
210                 req = list_entry(pos, struct lov_request, rq_link);
211
212                 if (!req->rq_complete || req->rq_rc)
213                         continue;
214                 if (req->rq_oi.oi_oa->o_valid == 0)   /* inactive stripe */
215                         continue;
216                 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
217                                 req->rq_oi.oi_oa->o_valid,
218                                 set->set_oi->oi_md, req->rq_stripe, &attrset);
219         }
220         if (!attrset) {
221                 CERROR("No stripes had valid attrs\n");
222                 rc = -EIO;
223         }
224
225         tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
226         memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
227 out:
228         if (tmp_oa)
229                 OBDO_FREE(tmp_oa);
230         RETURN(rc);
231
232 }
233
234 int lov_fini_getattr_set(struct lov_request_set *set)
235 {
236         int rc = 0;
237         ENTRY;
238
239         if (set == NULL)
240                 RETURN(0);
241         LASSERT(set->set_exp);
242         if (atomic_read(&set->set_completes))
243                 rc = common_attr_done(set);
244
245         lov_put_reqset(set);
246
247         RETURN(rc);
248 }
249
250 /* The callback for osc_getattr_async that finilizes a request info when a
251  * response is received. */
252 static int cb_getattr_update(void *cookie, int rc)
253 {
254         struct obd_info *oinfo = cookie;
255         struct lov_request *lovreq;
256         lovreq = container_of(oinfo, struct lov_request, rq_oi);
257         return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
258 }
259
260 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
261                          struct lov_request_set **reqset)
262 {
263         struct lov_request_set *set;
264         struct lov_obd *lov = &exp->exp_obd->u.lov;
265         int rc = 0, i;
266         ENTRY;
267
268         OBD_ALLOC(set, sizeof(*set));
269         if (set == NULL)
270                 RETURN(-ENOMEM);
271         lov_init_set(set);
272
273         set->set_exp = exp;
274         set->set_oi = oinfo;
275
276         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
277                 struct lov_oinfo *loi;
278                 struct lov_request *req;
279
280                 loi = oinfo->oi_md->lsm_oinfo[i];
281                 if (lov_oinfo_is_dummy(loi))
282                         continue;
283
284                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
285                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
286                         continue;
287                 }
288
289                 OBD_ALLOC(req, sizeof(*req));
290                 if (req == NULL)
291                         GOTO(out_set, rc = -ENOMEM);
292
293                 req->rq_stripe = i;
294                 req->rq_idx = loi->loi_ost_idx;
295
296                 OBDO_ALLOC(req->rq_oi.oi_oa);
297                 if (req->rq_oi.oi_oa == NULL) {
298                         OBD_FREE(req, sizeof(*req));
299                         GOTO(out_set, rc = -ENOMEM);
300                 }
301                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
302                        sizeof(*req->rq_oi.oi_oa));
303                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
304                 req->rq_oi.oi_cb_up = cb_getattr_update;
305                 req->rq_oi.oi_capa = oinfo->oi_capa;
306
307                 lov_set_add_req(req, set);
308         }
309         if (!set->set_count)
310                 GOTO(out_set, rc = -EIO);
311         *reqset = set;
312         RETURN(rc);
313 out_set:
314         lov_fini_getattr_set(set);
315         RETURN(rc);
316 }
317
318 #define LOV_U64_MAX ((__u64)~0ULL)
319 #define LOV_SUM_MAX(tot, add)                                           \
320         do {                                                            \
321                 if ((tot) + (add) < (tot))                              \
322                         (tot) = LOV_U64_MAX;                            \
323                 else                                                    \
324                         (tot) += (add);                                 \
325         } while(0)
326
327 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
328 {
329         ENTRY;
330
331         if (success) {
332                 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
333                                                            LOV_MAGIC, 0);
334                 if (osfs->os_files != LOV_U64_MAX)
335                         lov_do_div64(osfs->os_files, expected_stripes);
336                 if (osfs->os_ffree != LOV_U64_MAX)
337                         lov_do_div64(osfs->os_ffree, expected_stripes);
338
339                 spin_lock(&obd->obd_osfs_lock);
340                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
341                 obd->obd_osfs_age = cfs_time_current_64();
342                 spin_unlock(&obd->obd_osfs_lock);
343                 RETURN(0);
344         }
345
346         RETURN(-EIO);
347 }
348
349 int lov_fini_statfs_set(struct lov_request_set *set)
350 {
351         int rc = 0;
352         ENTRY;
353
354         if (set == NULL)
355                 RETURN(0);
356
357         if (atomic_read(&set->set_completes)) {
358                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
359                                      atomic_read(&set->set_success));
360         }
361         lov_put_reqset(set);
362         RETURN(rc);
363 }
364
365 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
366                        int success)
367 {
368         int shift = 0, quit = 0;
369         __u64 tmp;
370
371         if (success == 0) {
372                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
373         } else {
374                 if (osfs->os_bsize != lov_sfs->os_bsize) {
375                         /* assume all block sizes are always powers of 2 */
376                         /* get the bits difference */
377                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
378                         for (shift = 0; shift <= 64; ++shift) {
379                                 if (tmp & 1) {
380                                         if (quit)
381                                                 break;
382                                         else
383                                                 quit = 1;
384                                         shift = 0;
385                                 }
386                                 tmp >>= 1;
387                         }
388                 }
389
390                 if (osfs->os_bsize < lov_sfs->os_bsize) {
391                         osfs->os_bsize = lov_sfs->os_bsize;
392
393                         osfs->os_bfree  >>= shift;
394                         osfs->os_bavail >>= shift;
395                         osfs->os_blocks >>= shift;
396                 } else if (shift != 0) {
397                         lov_sfs->os_bfree  >>= shift;
398                         lov_sfs->os_bavail >>= shift;
399                         lov_sfs->os_blocks >>= shift;
400                 }
401 #ifdef MIN_DF
402                 /* Sandia requested that df (and so, statfs) only
403                    returned minimal available space on
404                    a single OST, so people would be able to
405                    write this much data guaranteed. */
406                 if (osfs->os_bavail > lov_sfs->os_bavail) {
407                         /* Presumably if new bavail is smaller,
408                            new bfree is bigger as well */
409                         osfs->os_bfree = lov_sfs->os_bfree;
410                         osfs->os_bavail = lov_sfs->os_bavail;
411                 }
412 #else
413                 osfs->os_bfree += lov_sfs->os_bfree;
414                 osfs->os_bavail += lov_sfs->os_bavail;
415 #endif
416                 osfs->os_blocks += lov_sfs->os_blocks;
417                 /* XXX not sure about this one - depends on policy.
418                  *   - could be minimum if we always stripe on all OBDs
419                  *     (but that would be wrong for any other policy,
420                  *     if one of the OBDs has no more objects left)
421                  *   - could be sum if we stripe whole objects
422                  *   - could be average, just to give a nice number
423                  *
424                  * To give a "reasonable" (if not wholly accurate)
425                  * number, we divide the total number of free objects
426                  * by expected stripe count (watch out for overflow).
427                  */
428                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
429                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
430         }
431 }
432
433 /* The callback for osc_statfs_async that finilizes a request info when a
434  * response is received. */
435 static int cb_statfs_update(void *cookie, int rc)
436 {
437         struct obd_info *oinfo = cookie;
438         struct lov_request *lovreq;
439         struct lov_request_set *set;
440         struct obd_statfs *osfs, *lov_sfs;
441         struct lov_obd *lov;
442         struct lov_tgt_desc *tgt;
443         struct obd_device *lovobd, *tgtobd;
444         int success;
445         ENTRY;
446
447         lovreq = container_of(oinfo, struct lov_request, rq_oi);
448         set = lovreq->rq_rqset;
449         lovobd = set->set_obd;
450         lov = &lovobd->u.lov;
451         osfs = set->set_oi->oi_osfs;
452         lov_sfs = oinfo->oi_osfs;
453         success = atomic_read(&set->set_success);
454         /* XXX: the same is done in lov_update_common_set, however
455            lovset->set_exp is not initialized. */
456         lov_update_set(set, lovreq, rc);
457         if (rc)
458                 GOTO(out, rc);
459
460         obd_getref(lovobd);
461         tgt = lov->lov_tgts[lovreq->rq_idx];
462         if (!tgt || !tgt->ltd_active)
463                 GOTO(out_update, rc);
464
465         tgtobd = class_exp2obd(tgt->ltd_exp);
466         spin_lock(&tgtobd->obd_osfs_lock);
467         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
468         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
469                 tgtobd->obd_osfs_age = cfs_time_current_64();
470         spin_unlock(&tgtobd->obd_osfs_lock);
471
472 out_update:
473         lov_update_statfs(osfs, lov_sfs, success);
474         obd_putref(lovobd);
475
476 out:
477         if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
478             lov_set_finished(set, 0)) {
479                 lov_statfs_interpret(NULL, set, set->set_count !=
480                                      atomic_read(&set->set_success));
481         }
482
483         RETURN(0);
484 }
485
486 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
487                         struct lov_request_set **reqset)
488 {
489         struct lov_request_set *set;
490         struct lov_obd *lov = &obd->u.lov;
491         int rc = 0, i;
492         ENTRY;
493
494         OBD_ALLOC(set, sizeof(*set));
495         if (set == NULL)
496                 RETURN(-ENOMEM);
497         lov_init_set(set);
498
499         set->set_obd = obd;
500         set->set_oi = oinfo;
501
502         /* We only get block data from the OBD */
503         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
504                 struct lov_request *req;
505
506                 if (lov->lov_tgts[i] == NULL ||
507                     (oinfo->oi_flags & OBD_STATFS_NODELAY &&
508                      !lov->lov_tgts[i]->ltd_active)) {
509                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
510                         continue;
511                 }
512
513                 if (!lov->lov_tgts[i]->ltd_active)
514                         lov_check_and_wait_active(lov, i);
515
516                 /* skip targets that have been explicitely disabled by the
517                  * administrator */
518                 if (!lov->lov_tgts[i]->ltd_exp) {
519                         CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
520                         continue;
521                 }
522
523                 OBD_ALLOC(req, sizeof(*req));
524                 if (req == NULL)
525                         GOTO(out_set, rc = -ENOMEM);
526
527                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
528                 if (req->rq_oi.oi_osfs == NULL) {
529                         OBD_FREE(req, sizeof(*req));
530                         GOTO(out_set, rc = -ENOMEM);
531                 }
532
533                 req->rq_idx = i;
534                 req->rq_oi.oi_cb_up = cb_statfs_update;
535                 req->rq_oi.oi_flags = oinfo->oi_flags;
536
537                 lov_set_add_req(req, set);
538         }
539         if (!set->set_count)
540                 GOTO(out_set, rc = -EIO);
541         *reqset = set;
542         RETURN(rc);
543 out_set:
544         lov_fini_statfs_set(set);
545         RETURN(rc);
546 }