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