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