Whamcloud - gitweb
LU-14462 gss: fix support for namespace in lgss_keyring
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LOV
34
35 #include <linux/delay.h>
36 #include <libcfs/libcfs.h>
37
38 #include <obd_class.h>
39 #include "lov_internal.h"
40
41 static void lov_init_set(struct lov_request_set *set)
42 {
43         set->set_count = 0;
44         atomic_set(&set->set_completes, 0);
45         atomic_set(&set->set_success, 0);
46         INIT_LIST_HEAD(&set->set_list);
47 }
48
49 static void lov_finish_set(struct lov_request_set *set)
50 {
51         struct list_head *pos, *n;
52         struct lov_request *req;
53
54         ENTRY;
55
56         LASSERT(set != NULL);
57         list_for_each_safe(pos, n, &set->set_list) {
58                 req = list_entry(pos, struct lov_request, rq_link);
59                 list_del_init(&req->rq_link);
60
61                 if (req->rq_oi.oi_osfs)
62                         OBD_FREE_PTR(req->rq_oi.oi_osfs);
63
64                 OBD_FREE_PTR(req);
65         }
66
67         OBD_FREE_PTR(set);
68         EXIT;
69 }
70
71 static void
72 lov_update_set(struct lov_request_set *set, struct lov_request *req, int rc)
73 {
74         atomic_inc(&set->set_completes);
75         if (rc == 0)
76                 atomic_inc(&set->set_success);
77 }
78
79 static void
80 lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
81 {
82         list_add_tail(&req->rq_link, &set->set_list);
83         set->set_count++;
84         req->rq_rqset = set;
85 }
86
87 static int lov_check_set(struct lov_obd *lov, int idx)
88 {
89         int rc = 0;
90
91         mutex_lock(&lov->lov_lock);
92
93         if (!lov->lov_tgts[idx] || lov->lov_tgts[idx]->ltd_active ||
94             (lov->lov_tgts[idx]->ltd_exp &&
95              class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
96                 rc = 1;
97
98         mutex_unlock(&lov->lov_lock);
99         return rc;
100 }
101
102 /*
103  * Check if the OSC connection exists and is active.
104  * If the OSC has not yet had a chance to connect to the OST the first time,
105  * wait once for it to connect instead of returning an error.
106  */
107 static int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
108 {
109         struct lov_tgt_desc *tgt;
110         struct obd_import *imp = NULL;
111         int rc = 0;
112         int cnt;
113
114         mutex_lock(&lov->lov_lock);
115
116         tgt = lov->lov_tgts[ost_idx];
117
118         if (unlikely(!tgt))
119                 GOTO(out, rc = 0);
120
121         if (likely(tgt->ltd_active))
122                 GOTO(out, rc = 1);
123
124         if (tgt->ltd_exp)
125                 imp = class_exp2cliimp(tgt->ltd_exp);
126         if (imp && imp->imp_connect_tried)
127                 GOTO(out, rc = 0);
128         if (imp && imp->imp_state == LUSTRE_IMP_IDLE)
129                 GOTO(out, rc = 0);
130
131         mutex_unlock(&lov->lov_lock);
132
133         cnt = obd_timeout;
134         while (cnt > 0 &&
135                !lov_check_set(lov, ost_idx)) {
136                 ssleep(1);
137                 cnt -= 1;
138         }
139         if (tgt->ltd_active)
140                 return 1;
141
142         return 0;
143
144 out:
145         mutex_unlock(&lov->lov_lock);
146         return rc;
147 }
148
149 #define LOV_U64_MAX ((__u64)~0ULL)
150 #define LOV_SUM_MAX(tot, add)                                           \
151         do {                                                            \
152                 if ((tot) + (add) < (tot))                              \
153                         (tot) = LOV_U64_MAX;                            \
154                 else                                                    \
155                         (tot) += (add);                                 \
156         } while (0)
157
158 static int
159 lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs, int success)
160 {
161         ENTRY;
162
163         if (success) {
164                 __u32 expected_stripes = lov_get_stripe_count(&obd->u.lov,
165                                                               LOV_MAGIC, 0);
166                 if (osfs->os_files != LOV_U64_MAX)
167                         lov_do_div64(osfs->os_files, expected_stripes);
168                 if (osfs->os_ffree != LOV_U64_MAX)
169                         lov_do_div64(osfs->os_ffree, expected_stripes);
170
171                 spin_lock(&obd->obd_osfs_lock);
172                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
173                 obd->obd_osfs_age = ktime_get_seconds();
174                 spin_unlock(&obd->obd_osfs_lock);
175                 RETURN(0);
176         }
177
178         RETURN(-EIO);
179 }
180
181 int lov_fini_statfs_set(struct lov_request_set *set)
182 {
183         int rc = 0;
184         ENTRY;
185
186         if (!set)
187                 RETURN(0);
188
189         if (atomic_read(&set->set_completes)) {
190                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
191                                      atomic_read(&set->set_success));
192         }
193
194         lov_finish_set(set);
195
196         RETURN(rc);
197 }
198
199 static void
200 lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
201                   int success)
202 {
203         int shift = 0, quit = 0;
204         __u64 tmp;
205
206         if (success == 0) {
207                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
208         } else {
209                 if (osfs->os_bsize != lov_sfs->os_bsize) {
210                         /* assume all block sizes are always powers of 2 */
211                         /* get the bits difference */
212                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
213                         for (shift = 0; shift <= 64; ++shift) {
214                                 if (tmp & 1) {
215                                         if (quit)
216                                                 break;
217                                         quit = 1;
218                                         shift = 0;
219                                 }
220                                 tmp >>= 1;
221                         }
222                 }
223
224                 if (osfs->os_bsize < lov_sfs->os_bsize) {
225                         osfs->os_bsize = lov_sfs->os_bsize;
226
227                         osfs->os_bfree  >>= shift;
228                         osfs->os_bavail >>= shift;
229                         osfs->os_blocks >>= shift;
230                 } else if (shift != 0) {
231                         lov_sfs->os_bfree  >>= shift;
232                         lov_sfs->os_bavail >>= shift;
233                         lov_sfs->os_blocks >>= shift;
234                 }
235 #ifdef MIN_DF
236                 /*
237                  * Sandia requested that df (and so, statfs) only
238                  * returned minimal available space on
239                  * a single OST, so people would be able to
240                  * write this much data guaranteed.
241                  */
242                 if (osfs->os_bavail > lov_sfs->os_bavail) {
243                         /*
244                          * Presumably if new bavail is smaller,
245                          * new bfree is bigger as well
246                          */
247                         osfs->os_bfree = lov_sfs->os_bfree;
248                         osfs->os_bavail = lov_sfs->os_bavail;
249                 }
250 #else
251                 osfs->os_bfree += lov_sfs->os_bfree;
252                 osfs->os_bavail += lov_sfs->os_bavail;
253 #endif
254                 osfs->os_blocks += lov_sfs->os_blocks;
255                 /*
256                  * XXX not sure about this one - depends on policy.
257                  *   - could be minimum if we always stripe on all OBDs
258                  *     (but that would be wrong for any other policy,
259                  *     if one of the OBDs has no more objects left)
260                  *   - could be sum if we stripe whole objects
261                  *   - could be average, just to give a nice number
262                  *
263                  * To give a "reasonable" (if not wholly accurate)
264                  * number, we divide the total number of free objects
265                  * by expected stripe count (watch out for overflow).
266                  */
267                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
268                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
269         }
270 }
271
272 /*
273  * The callback for osc_statfs_async that finilizes a request info when a
274  * response is received.
275  */
276 static int cb_statfs_update(void *cookie, int rc)
277 {
278         struct obd_info *oinfo = cookie;
279         struct lov_request *lovreq;
280         struct lov_request_set *set;
281         struct obd_statfs *osfs, *lov_sfs;
282         struct lov_obd *lov;
283         struct lov_tgt_desc *tgt;
284         struct obd_device *lovobd, *tgtobd;
285         int success;
286
287         ENTRY;
288
289         lovreq = container_of(oinfo, struct lov_request, rq_oi);
290         set = lovreq->rq_rqset;
291         lovobd = set->set_obd;
292         lov = &lovobd->u.lov;
293         osfs = set->set_oi->oi_osfs;
294         lov_sfs = oinfo->oi_osfs;
295         success = atomic_read(&set->set_success);
296         /*
297          * XXX: the same is done in lov_update_common_set, however
298          * lovset->set_exp is not initialized.
299          */
300         lov_update_set(set, lovreq, rc);
301         if (rc)
302                 GOTO(out, rc);
303
304         lov_tgts_getref(lovobd);
305         tgt = lov->lov_tgts[lovreq->rq_idx];
306         if (!tgt || !tgt->ltd_active)
307                 GOTO(out_update, rc);
308
309         tgtobd = class_exp2obd(tgt->ltd_exp);
310         spin_lock(&tgtobd->obd_osfs_lock);
311         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
312         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
313                 tgtobd->obd_osfs_age = ktime_get_seconds();
314         spin_unlock(&tgtobd->obd_osfs_lock);
315
316 out_update:
317         lov_update_statfs(osfs, lov_sfs, success);
318         lov_tgts_putref(lovobd);
319 out:
320         RETURN(0);
321 }
322
323 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
324                         struct lov_request_set **reqset)
325 {
326         struct lov_request_set *set;
327         struct lov_obd *lov = &obd->u.lov;
328         int rc = 0, i;
329
330         ENTRY;
331
332         OBD_ALLOC(set, sizeof(*set));
333         if (!set)
334                 RETURN(-ENOMEM);
335         lov_init_set(set);
336
337         set->set_obd = obd;
338         set->set_oi = oinfo;
339
340         /* We only get block data from the OBD */
341         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
342                 struct lov_tgt_desc *ltd = lov->lov_tgts[i];
343                 struct lov_request *req;
344
345                 if (!ltd) {
346                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
347                         continue;
348                 }
349
350                 /*
351                  * skip targets that have been explicitely disabled by the
352                  * administrator
353                  */
354                 if (!ltd->ltd_exp) {
355                         CDEBUG(D_HA, "lov idx %d administratively disabled\n",
356                                i);
357                         continue;
358                 }
359
360                 if (oinfo->oi_flags & OBD_STATFS_NODELAY &&
361                     class_exp2cliimp(ltd->ltd_exp)->imp_state !=
362                     LUSTRE_IMP_IDLE && !ltd->ltd_active) {
363                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
364                         continue;
365                 }
366
367                 if (!ltd->ltd_active)
368                         lov_check_and_wait_active(lov, i);
369
370                 OBD_ALLOC(req, sizeof(*req));
371                 if (!req)
372                         GOTO(out_set, rc = -ENOMEM);
373
374                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
375                 if (!req->rq_oi.oi_osfs) {
376                         OBD_FREE(req, sizeof(*req));
377                         GOTO(out_set, rc = -ENOMEM);
378                 }
379
380                 req->rq_idx = i;
381                 req->rq_oi.oi_cb_up = cb_statfs_update;
382                 req->rq_oi.oi_flags = oinfo->oi_flags;
383
384                 lov_set_add_req(req, set);
385         }
386         if (!set->set_count)
387                 GOTO(out_set, rc = -EIO);
388         *reqset = set;
389         RETURN(rc);
390 out_set:
391         lov_fini_statfs_set(set);
392         RETURN(rc);
393 }