Whamcloud - gitweb
LU-3030 build: Update Master Copyrights pre 2.4 split
[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, 2013, 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 #ifdef __KERNEL__
40 #include <libcfs/libcfs.h>
41 #else
42 #include <liblustre.h>
43 #endif
44
45 #include <obd_class.h>
46 #include <obd_lov.h>
47 #include <lustre/lustre_idl.h>
48
49 #include "lov_internal.h"
50
51 static void lov_init_set(struct lov_request_set *set)
52 {
53         set->set_count = 0;
54         cfs_atomic_set(&set->set_completes, 0);
55         cfs_atomic_set(&set->set_success, 0);
56         cfs_atomic_set(&set->set_finish_checked, 0);
57         set->set_cookies = 0;
58         CFS_INIT_LIST_HEAD(&set->set_list);
59         cfs_atomic_set(&set->set_refcount, 1);
60         cfs_waitq_init(&set->set_waitq);
61         spin_lock_init(&set->set_lock);
62 }
63
64 void lov_finish_set(struct lov_request_set *set)
65 {
66         cfs_list_t *pos, *n;
67         ENTRY;
68
69         LASSERT(set);
70         cfs_list_for_each_safe(pos, n, &set->set_list) {
71                 struct lov_request *req = cfs_list_entry(pos,
72                                                          struct lov_request,
73                                                          rq_link);
74                 cfs_list_del_init(&req->rq_link);
75
76                 if (req->rq_oi.oi_oa)
77                         OBDO_FREE(req->rq_oi.oi_oa);
78                 if (req->rq_oi.oi_md)
79                         OBD_FREE_LARGE(req->rq_oi.oi_md, req->rq_buflen);
80                 if (req->rq_oi.oi_osfs)
81                         OBD_FREE(req->rq_oi.oi_osfs,
82                                  sizeof(*req->rq_oi.oi_osfs));
83                 OBD_FREE(req, sizeof(*req));
84         }
85
86         if (set->set_pga) {
87                 int len = set->set_oabufs * sizeof(*set->set_pga);
88                 OBD_FREE_LARGE(set->set_pga, len);
89         }
90         if (set->set_lockh)
91                 lov_llh_put(set->set_lockh);
92
93         OBD_FREE(set, sizeof(*set));
94         EXIT;
95 }
96
97 int lov_set_finished(struct lov_request_set *set, int idempotent)
98 {
99         int completes = cfs_atomic_read(&set->set_completes);
100
101         CDEBUG(D_INFO, "check set %d/%d\n", completes, set->set_count);
102
103         if (completes == set->set_count) {
104                 if (idempotent)
105                         return 1;
106                 if (cfs_atomic_inc_return(&set->set_finish_checked) == 1)
107                         return 1;
108         }
109         return 0;
110 }
111
112 void lov_update_set(struct lov_request_set *set,
113                     struct lov_request *req, int rc)
114 {
115         req->rq_complete = 1;
116         req->rq_rc = rc;
117
118         cfs_atomic_inc(&set->set_completes);
119         if (rc == 0)
120                 cfs_atomic_inc(&set->set_success);
121
122         cfs_waitq_signal(&set->set_waitq);
123 }
124
125 int lov_update_common_set(struct lov_request_set *set,
126                           struct lov_request *req, int rc)
127 {
128         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
129         ENTRY;
130
131         lov_update_set(set, req, rc);
132
133         /* grace error on inactive ost */
134         if (rc && !(lov->lov_tgts[req->rq_idx] &&
135                     lov->lov_tgts[req->rq_idx]->ltd_active))
136                 rc = 0;
137
138         /* FIXME in raid1 regime, should return 0 */
139         RETURN(rc);
140 }
141
142 void lov_set_add_req(struct lov_request *req, struct lov_request_set *set)
143 {
144         cfs_list_add_tail(&req->rq_link, &set->set_list);
145         set->set_count++;
146         req->rq_rqset = set;
147 }
148
149 static int lov_check_set(struct lov_obd *lov, int idx)
150 {
151         int rc = 0;
152         mutex_lock(&lov->lov_lock);
153
154         if (lov->lov_tgts[idx] == NULL ||
155             lov->lov_tgts[idx]->ltd_active ||
156             (lov->lov_tgts[idx]->ltd_exp != NULL &&
157              class_exp2cliimp(lov->lov_tgts[idx]->ltd_exp)->imp_connect_tried))
158                 rc = 1;
159
160         mutex_unlock(&lov->lov_lock);
161         return rc;
162 }
163
164 /* Check if the OSC connection exists and is active.
165  * If the OSC has not yet had a chance to connect to the OST the first time,
166  * wait once for it to connect instead of returning an error.
167  */
168 int lov_check_and_wait_active(struct lov_obd *lov, int ost_idx)
169 {
170         cfs_waitq_t waitq;
171         struct l_wait_info lwi;
172         struct lov_tgt_desc *tgt;
173         int rc = 0;
174
175         mutex_lock(&lov->lov_lock);
176
177         tgt = lov->lov_tgts[ost_idx];
178
179         if (unlikely(tgt == NULL))
180                 GOTO(out, rc = 0);
181
182         if (likely(tgt->ltd_active))
183                 GOTO(out, rc = 1);
184
185         if (tgt->ltd_exp && class_exp2cliimp(tgt->ltd_exp)->imp_connect_tried)
186                 GOTO(out, rc = 0);
187
188         mutex_unlock(&lov->lov_lock);
189
190         cfs_waitq_init(&waitq);
191         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(obd_timeout),
192                                    cfs_time_seconds(1), NULL, NULL);
193
194         rc = l_wait_event(waitq, lov_check_set(lov, ost_idx), &lwi);
195         if (tgt != NULL && tgt->ltd_active)
196                 return 1;
197
198         return 0;
199
200 out:
201         mutex_unlock(&lov->lov_lock);
202         return rc;
203 }
204
205 extern void osc_update_enqueue(struct lustre_handle *lov_lockhp,
206                                struct lov_oinfo *loi, int flags,
207                                struct ost_lvb *lvb, __u32 mode, int rc);
208
209 static int lov_update_enqueue_lov(struct obd_export *exp,
210                                   struct lustre_handle *lov_lockhp,
211                                   struct lov_oinfo *loi, int flags, int idx,
212                                   struct ost_id *oi, int rc)
213 {
214         struct lov_obd *lov = &exp->exp_obd->u.lov;
215
216         if (rc != ELDLM_OK &&
217             !(rc == ELDLM_LOCK_ABORTED && (flags & LDLM_FL_HAS_INTENT))) {
218                 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
219                 if (lov->lov_tgts[idx] && lov->lov_tgts[idx]->ltd_active) {
220                         /* -EUSERS used by OST to report file contention */
221                         if (rc != -EINTR && rc != -EUSERS)
222                                 CERROR("%s: enqueue objid "DOSTID" subobj"
223                                        DOSTID" on OST idx %d: rc %d\n",
224                                        exp->exp_obd->obd_name,
225                                        POSTID(oi), POSTID(&loi->loi_oi),
226                                        loi->loi_ost_idx, rc);
227                 } else
228                         rc = ELDLM_OK;
229         }
230         return rc;
231 }
232
233 int lov_update_enqueue_set(struct lov_request *req, __u32 mode, int rc)
234 {
235         struct lov_request_set *set = req->rq_rqset;
236         struct lustre_handle *lov_lockhp;
237         struct obd_info *oi = set->set_oi;
238         struct lov_oinfo *loi;
239         ENTRY;
240
241         LASSERT(oi != NULL);
242
243         lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
244         loi = oi->oi_md->lsm_oinfo[req->rq_stripe];
245
246         /* XXX LOV STACKING: OSC gets a copy, created in lov_prep_enqueue_set
247          * and that copy can be arbitrarily out of date.
248          *
249          * The LOV API is due for a serious rewriting anyways, and this
250          * can be addressed then. */
251
252         lov_stripe_lock(oi->oi_md);
253         osc_update_enqueue(lov_lockhp, loi, oi->oi_flags,
254                            &req->rq_oi.oi_md->lsm_oinfo[0]->loi_lvb, mode, rc);
255         if (rc == ELDLM_LOCK_ABORTED && (oi->oi_flags & LDLM_FL_HAS_INTENT))
256                 memset(lov_lockhp, 0, sizeof *lov_lockhp);
257         rc = lov_update_enqueue_lov(set->set_exp, lov_lockhp, loi, oi->oi_flags,
258                                     req->rq_idx, &oi->oi_md->lsm_oi, rc);
259         lov_stripe_unlock(oi->oi_md);
260         lov_update_set(set, req, rc);
261         RETURN(rc);
262 }
263
264 /* The callback for osc_enqueue that updates lov info for every OSC request. */
265 static int cb_update_enqueue(void *cookie, int rc)
266 {
267         struct obd_info *oinfo = cookie;
268         struct ldlm_enqueue_info *einfo;
269         struct lov_request *lovreq;
270
271         lovreq = container_of(oinfo, struct lov_request, rq_oi);
272         einfo = lovreq->rq_rqset->set_ei;
273         return lov_update_enqueue_set(lovreq, einfo->ei_mode, rc);
274 }
275
276 static int enqueue_done(struct lov_request_set *set, __u32 mode)
277 {
278         struct lov_request *req;
279         struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
280         int completes = cfs_atomic_read(&set->set_completes);
281         int rc = 0;
282         ENTRY;
283
284         /* enqueue/match success, just return */
285         if (completes && completes == cfs_atomic_read(&set->set_success))
286                 RETURN(0);
287
288         /* cancel enqueued/matched locks */
289         cfs_list_for_each_entry(req, &set->set_list, rq_link) {
290                 struct lustre_handle *lov_lockhp;
291
292                 if (!req->rq_complete || req->rq_rc)
293                         continue;
294
295                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
296                 LASSERT(lov_lockhp);
297                 if (!lustre_handle_is_used(lov_lockhp))
298                         continue;
299
300                 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
301                                 req->rq_oi.oi_md, mode, lov_lockhp);
302                 if (rc && lov->lov_tgts[req->rq_idx] &&
303                     lov->lov_tgts[req->rq_idx]->ltd_active)
304                         CERROR("%s: cancelling obdjid "DOSTID" on OST"
305                                "idx %d error: rc = %d\n",
306                                set->set_exp->exp_obd->obd_name,
307                                POSTID(&req->rq_oi.oi_md->lsm_oi),
308                                req->rq_idx, rc);
309         }
310         if (set->set_lockh)
311                 lov_llh_put(set->set_lockh);
312         RETURN(rc);
313 }
314
315 int lov_fini_enqueue_set(struct lov_request_set *set, __u32 mode, int rc,
316                          struct ptlrpc_request_set *rqset)
317 {
318         int ret = 0;
319         ENTRY;
320
321         if (set == NULL)
322                 RETURN(0);
323         LASSERT(set->set_exp);
324         /* Do enqueue_done only for sync requests and if any request
325          * succeeded. */
326         if (!rqset) {
327                 if (rc)
328                         cfs_atomic_set(&set->set_completes, 0);
329                 ret = enqueue_done(set, mode);
330         } else if (set->set_lockh)
331                 lov_llh_put(set->set_lockh);
332
333         lov_put_reqset(set);
334
335         RETURN(rc ? rc : ret);
336 }
337
338 static void lov_llh_addref(void *llhp)
339 {
340         struct lov_lock_handles *llh = llhp;
341
342         cfs_atomic_inc(&llh->llh_refcount);
343         CDEBUG(D_INFO, "GETting llh %p : new refcount %d\n", llh,
344                cfs_atomic_read(&llh->llh_refcount));
345 }
346
347 static struct portals_handle_ops lov_handle_ops = {
348         .hop_addref = lov_llh_addref,
349         .hop_free   = NULL,
350 };
351
352 static struct lov_lock_handles *lov_llh_new(struct lov_stripe_md *lsm)
353 {
354         struct lov_lock_handles *llh;
355
356         OBD_ALLOC(llh, sizeof *llh +
357                   sizeof(*llh->llh_handles) * lsm->lsm_stripe_count);
358         if (llh == NULL)
359                 return NULL;
360
361         cfs_atomic_set(&llh->llh_refcount, 2);
362         llh->llh_stripe_count = lsm->lsm_stripe_count;
363         CFS_INIT_LIST_HEAD(&llh->llh_handle.h_link);
364         class_handle_hash(&llh->llh_handle, &lov_handle_ops);
365
366         return llh;
367 }
368
369 int lov_prep_enqueue_set(struct obd_export *exp, struct obd_info *oinfo,
370                          struct ldlm_enqueue_info *einfo,
371                          struct lov_request_set **reqset)
372 {
373         struct lov_obd *lov = &exp->exp_obd->u.lov;
374         struct lov_request_set *set;
375         int i, rc = 0;
376         ENTRY;
377
378         OBD_ALLOC(set, sizeof(*set));
379         if (set == NULL)
380                 RETURN(-ENOMEM);
381         lov_init_set(set);
382
383         set->set_exp = exp;
384         set->set_oi = oinfo;
385         set->set_ei = einfo;
386         set->set_lockh = lov_llh_new(oinfo->oi_md);
387         if (set->set_lockh == NULL)
388                 GOTO(out_set, rc = -ENOMEM);
389         oinfo->oi_lockh->cookie = set->set_lockh->llh_handle.h_cookie;
390
391         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
392                 struct lov_oinfo *loi;
393                 struct lov_request *req;
394                 obd_off start, end;
395
396                 loi = oinfo->oi_md->lsm_oinfo[i];
397                 if (!lov_stripe_intersects(oinfo->oi_md, i,
398                                            oinfo->oi_policy.l_extent.start,
399                                            oinfo->oi_policy.l_extent.end,
400                                            &start, &end))
401                         continue;
402
403                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
404                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
405                         continue;
406                 }
407
408                 OBD_ALLOC(req, sizeof(*req));
409                 if (req == NULL)
410                         GOTO(out_set, rc = -ENOMEM);
411
412                 req->rq_buflen = sizeof(*req->rq_oi.oi_md) +
413                         sizeof(struct lov_oinfo *) +
414                         sizeof(struct lov_oinfo);
415                 OBD_ALLOC_LARGE(req->rq_oi.oi_md, req->rq_buflen);
416                 if (req->rq_oi.oi_md == NULL) {
417                         OBD_FREE(req, sizeof(*req));
418                         GOTO(out_set, rc = -ENOMEM);
419                 }
420                 req->rq_oi.oi_md->lsm_oinfo[0] =
421                         ((void *)req->rq_oi.oi_md) + sizeof(*req->rq_oi.oi_md) +
422                         sizeof(struct lov_oinfo *);
423
424                 /* Set lov request specific parameters. */
425                 req->rq_oi.oi_lockh = set->set_lockh->llh_handles + i;
426                 req->rq_oi.oi_cb_up = cb_update_enqueue;
427                 req->rq_oi.oi_flags = oinfo->oi_flags;
428
429                 LASSERT(req->rq_oi.oi_lockh);
430
431                 req->rq_oi.oi_policy.l_extent.gid =
432                         oinfo->oi_policy.l_extent.gid;
433                 req->rq_oi.oi_policy.l_extent.start = start;
434                 req->rq_oi.oi_policy.l_extent.end = end;
435
436                 req->rq_idx = loi->loi_ost_idx;
437                 req->rq_stripe = i;
438
439                 /* XXX LOV STACKING: submd should be from the subobj */
440                 req->rq_oi.oi_md->lsm_oi = loi->loi_oi;
441                 req->rq_oi.oi_md->lsm_stripe_count = 0;
442                 req->rq_oi.oi_md->lsm_oinfo[0]->loi_kms_valid =
443                         loi->loi_kms_valid;
444                 req->rq_oi.oi_md->lsm_oinfo[0]->loi_kms = loi->loi_kms;
445                 req->rq_oi.oi_md->lsm_oinfo[0]->loi_lvb = loi->loi_lvb;
446
447                 lov_set_add_req(req, set);
448         }
449         if (!set->set_count)
450                 GOTO(out_set, rc = -EIO);
451         *reqset = set;
452         RETURN(0);
453 out_set:
454         lov_fini_enqueue_set(set, einfo->ei_mode, rc, NULL);
455         RETURN(rc);
456 }
457
458 int lov_fini_match_set(struct lov_request_set *set, __u32 mode, int flags)
459 {
460         int rc = 0;
461         ENTRY;
462
463         if (set == NULL)
464                 RETURN(0);
465         LASSERT(set->set_exp);
466         rc = enqueue_done(set, mode);
467         if ((set->set_count == cfs_atomic_read(&set->set_success)) &&
468             (flags & LDLM_FL_TEST_LOCK))
469                 lov_llh_put(set->set_lockh);
470
471         lov_put_reqset(set);
472
473         RETURN(rc);
474 }
475
476 int lov_prep_match_set(struct obd_export *exp, struct obd_info *oinfo,
477                        struct lov_stripe_md *lsm, ldlm_policy_data_t *policy,
478                        __u32 mode, struct lustre_handle *lockh,
479                        struct lov_request_set **reqset)
480 {
481         struct lov_obd *lov = &exp->exp_obd->u.lov;
482         struct lov_request_set *set;
483         int i, rc = 0;
484         ENTRY;
485
486         OBD_ALLOC(set, sizeof(*set));
487         if (set == NULL)
488                 RETURN(-ENOMEM);
489         lov_init_set(set);
490
491         set->set_exp = exp;
492         set->set_oi = oinfo;
493         set->set_oi->oi_md = lsm;
494         set->set_lockh = lov_llh_new(lsm);
495         if (set->set_lockh == NULL)
496                 GOTO(out_set, rc = -ENOMEM);
497         lockh->cookie = set->set_lockh->llh_handle.h_cookie;
498
499         for (i = 0; i < lsm->lsm_stripe_count; i++){
500                 struct lov_oinfo *loi;
501                 struct lov_request *req;
502                 obd_off start, end;
503
504                 loi = lsm->lsm_oinfo[i];
505                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
506                                            policy->l_extent.end, &start, &end))
507                         continue;
508
509                 /* FIXME raid1 should grace this error */
510                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
511                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
512                         GOTO(out_set, rc = -EIO);
513                 }
514
515                 OBD_ALLOC(req, sizeof(*req));
516                 if (req == NULL)
517                         GOTO(out_set, rc = -ENOMEM);
518
519                 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
520                 OBD_ALLOC_LARGE(req->rq_oi.oi_md, req->rq_buflen);
521                 if (req->rq_oi.oi_md == NULL) {
522                         OBD_FREE(req, sizeof(*req));
523                         GOTO(out_set, rc = -ENOMEM);
524                 }
525
526                 req->rq_oi.oi_policy.l_extent.start = start;
527                 req->rq_oi.oi_policy.l_extent.end = end;
528                 req->rq_oi.oi_policy.l_extent.gid = policy->l_extent.gid;
529
530                 req->rq_idx = loi->loi_ost_idx;
531                 req->rq_stripe = i;
532
533                 /* XXX LOV STACKING: submd should be from the subobj */
534                 req->rq_oi.oi_md->lsm_oi = loi->loi_oi;
535                 req->rq_oi.oi_md->lsm_stripe_count = 0;
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_match_set(set, mode, 0);
545         RETURN(rc);
546 }
547
548 int lov_fini_cancel_set(struct lov_request_set *set)
549 {
550         int rc = 0;
551         ENTRY;
552
553         if (set == NULL)
554                 RETURN(0);
555
556         LASSERT(set->set_exp);
557         if (set->set_lockh)
558                 lov_llh_put(set->set_lockh);
559
560         lov_put_reqset(set);
561
562         RETURN(rc);
563 }
564
565 int lov_prep_cancel_set(struct obd_export *exp, struct obd_info *oinfo,
566                         struct lov_stripe_md *lsm, __u32 mode,
567                         struct lustre_handle *lockh,
568                         struct lov_request_set **reqset)
569 {
570         struct lov_request_set *set;
571         int i, rc = 0;
572         ENTRY;
573
574         OBD_ALLOC(set, sizeof(*set));
575         if (set == NULL)
576                 RETURN(-ENOMEM);
577         lov_init_set(set);
578
579         set->set_exp = exp;
580         set->set_oi = oinfo;
581         set->set_oi->oi_md = lsm;
582         set->set_lockh = lov_handle2llh(lockh);
583         if (set->set_lockh == NULL) {
584                 CERROR("LOV: invalid lov lock handle %p\n", lockh);
585                 GOTO(out_set, rc = -EINVAL);
586         }
587         lockh->cookie = set->set_lockh->llh_handle.h_cookie;
588
589         for (i = 0; i < lsm->lsm_stripe_count; i++){
590                 struct lov_request *req;
591                 struct lustre_handle *lov_lockhp;
592                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
593
594                 lov_lockhp = set->set_lockh->llh_handles + i;
595                 if (!lustre_handle_is_used(lov_lockhp)) {
596                         CDEBUG(D_INFO, "lov idx %d subobj "DOSTID" no lock\n",
597                                loi->loi_ost_idx, POSTID(&loi->loi_oi));
598                         continue;
599                 }
600
601                 OBD_ALLOC(req, sizeof(*req));
602                 if (req == NULL)
603                         GOTO(out_set, rc = -ENOMEM);
604
605                 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
606                 OBD_ALLOC_LARGE(req->rq_oi.oi_md, req->rq_buflen);
607                 if (req->rq_oi.oi_md == NULL) {
608                         OBD_FREE(req, sizeof(*req));
609                         GOTO(out_set, rc = -ENOMEM);
610                 }
611
612                 req->rq_idx = loi->loi_ost_idx;
613                 req->rq_stripe = i;
614
615                 /* XXX LOV STACKING: submd should be from the subobj */
616                 req->rq_oi.oi_md->lsm_oi = loi->loi_oi;
617                 req->rq_oi.oi_md->lsm_stripe_count = 0;
618
619                 lov_set_add_req(req, set);
620         }
621         if (!set->set_count)
622                 GOTO(out_set, rc = -EIO);
623         *reqset = set;
624         RETURN(rc);
625 out_set:
626         lov_fini_cancel_set(set);
627         RETURN(rc);
628 }
629 static int common_attr_done(struct lov_request_set *set)
630 {
631         cfs_list_t *pos;
632         struct lov_request *req;
633         struct obdo *tmp_oa;
634         int rc = 0, attrset = 0;
635         ENTRY;
636
637         LASSERT(set->set_oi != NULL);
638
639         if (set->set_oi->oi_oa == NULL)
640                 RETURN(0);
641
642         if (!cfs_atomic_read(&set->set_success))
643                 RETURN(-EIO);
644
645         OBDO_ALLOC(tmp_oa);
646         if (tmp_oa == NULL)
647                 GOTO(out, rc = -ENOMEM);
648
649         cfs_list_for_each (pos, &set->set_list) {
650                 req = cfs_list_entry(pos, struct lov_request, rq_link);
651
652                 if (!req->rq_complete || req->rq_rc)
653                         continue;
654                 if (req->rq_oi.oi_oa->o_valid == 0)   /* inactive stripe */
655                         continue;
656                 lov_merge_attrs(tmp_oa, req->rq_oi.oi_oa,
657                                 req->rq_oi.oi_oa->o_valid,
658                                 set->set_oi->oi_md, req->rq_stripe, &attrset);
659         }
660         if (!attrset) {
661                 CERROR("No stripes had valid attrs\n");
662                 rc = -EIO;
663         }
664         if ((set->set_oi->oi_oa->o_valid & OBD_MD_FLEPOCH) &&
665             (set->set_oi->oi_md->lsm_stripe_count != attrset)) {
666                 /* When we take attributes of some epoch, we require all the
667                  * ost to be active. */
668                 CERROR("Not all the stripes had valid attrs\n");
669                 GOTO(out, rc = -EIO);
670         }
671
672         tmp_oa->o_oi = set->set_oi->oi_oa->o_oi;
673         memcpy(set->set_oi->oi_oa, tmp_oa, sizeof(*set->set_oi->oi_oa));
674 out:
675         if (tmp_oa)
676                 OBDO_FREE(tmp_oa);
677         RETURN(rc);
678
679 }
680
681 static int brw_done(struct lov_request_set *set)
682 {
683         struct lov_stripe_md *lsm = set->set_oi->oi_md;
684         struct lov_oinfo     *loi = NULL;
685         cfs_list_t *pos;
686         struct lov_request *req;
687         ENTRY;
688
689         cfs_list_for_each (pos, &set->set_list) {
690                 req = cfs_list_entry(pos, struct lov_request, rq_link);
691
692                 if (!req->rq_complete || req->rq_rc)
693                         continue;
694
695                 loi = lsm->lsm_oinfo[req->rq_stripe];
696
697                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLBLOCKS)
698                         loi->loi_lvb.lvb_blocks = req->rq_oi.oi_oa->o_blocks;
699         }
700
701         RETURN(0);
702 }
703
704 int lov_fini_brw_set(struct lov_request_set *set)
705 {
706         int rc = 0;
707         ENTRY;
708
709         if (set == NULL)
710                 RETURN(0);
711         LASSERT(set->set_exp);
712         if (cfs_atomic_read(&set->set_completes)) {
713                 rc = brw_done(set);
714                 /* FIXME update qos data here */
715         }
716         lov_put_reqset(set);
717
718         RETURN(rc);
719 }
720
721 int lov_prep_brw_set(struct obd_export *exp, struct obd_info *oinfo,
722                      obd_count oa_bufs, struct brw_page *pga,
723                      struct obd_trans_info *oti,
724                      struct lov_request_set **reqset)
725 {
726         struct {
727                 obd_count       index;
728                 obd_count       count;
729                 obd_count       off;
730         } *info = NULL;
731         struct lov_request_set *set;
732         struct lov_obd *lov = &exp->exp_obd->u.lov;
733         int rc = 0, i, shift;
734         ENTRY;
735
736         OBD_ALLOC(set, sizeof(*set));
737         if (set == NULL)
738                 RETURN(-ENOMEM);
739         lov_init_set(set);
740
741         set->set_exp = exp;
742         set->set_oti = oti;
743         set->set_oi = oinfo;
744         set->set_oabufs = oa_bufs;
745         OBD_ALLOC_LARGE(set->set_pga, oa_bufs * sizeof(*set->set_pga));
746         if (!set->set_pga)
747                 GOTO(out, rc = -ENOMEM);
748
749         OBD_ALLOC_LARGE(info, sizeof(*info) * oinfo->oi_md->lsm_stripe_count);
750         if (!info)
751                 GOTO(out, rc = -ENOMEM);
752
753         /* calculate the page count for each stripe */
754         for (i = 0; i < oa_bufs; i++) {
755                 int stripe = lov_stripe_number(oinfo->oi_md, pga[i].off);
756                 info[stripe].count++;
757         }
758
759         /* alloc and initialize lov request */
760         shift = 0;
761         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++){
762                 struct lov_oinfo *loi = NULL;
763                 struct lov_request *req;
764
765                 if (info[i].count == 0)
766                         continue;
767
768                 loi = oinfo->oi_md->lsm_oinfo[i];
769                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
770                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
771                         GOTO(out, rc = -EIO);
772                 }
773
774                 OBD_ALLOC(req, sizeof(*req));
775                 if (req == NULL)
776                         GOTO(out, rc = -ENOMEM);
777
778                 OBDO_ALLOC(req->rq_oi.oi_oa);
779                 if (req->rq_oi.oi_oa == NULL) {
780                         OBD_FREE(req, sizeof(*req));
781                         GOTO(out, rc = -ENOMEM);
782                 }
783
784                 if (oinfo->oi_oa) {
785                         memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
786                                sizeof(*req->rq_oi.oi_oa));
787                 }
788                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
789                 req->rq_oi.oi_oa->o_stripe_idx = i;
790
791                 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
792                 OBD_ALLOC_LARGE(req->rq_oi.oi_md, req->rq_buflen);
793                 if (req->rq_oi.oi_md == NULL) {
794                         OBDO_FREE(req->rq_oi.oi_oa);
795                         OBD_FREE(req, sizeof(*req));
796                         GOTO(out, rc = -ENOMEM);
797                 }
798
799                 req->rq_idx = loi->loi_ost_idx;
800                 req->rq_stripe = i;
801
802                 /* XXX LOV STACKING */
803                 req->rq_oi.oi_md->lsm_oi = loi->loi_oi;
804                 req->rq_oabufs = info[i].count;
805                 req->rq_pgaidx = shift;
806                 shift += req->rq_oabufs;
807
808                 /* remember the index for sort brw_page array */
809                 info[i].index = req->rq_pgaidx;
810
811                 req->rq_oi.oi_capa = oinfo->oi_capa;
812
813                 lov_set_add_req(req, set);
814         }
815         if (!set->set_count)
816                 GOTO(out, rc = -EIO);
817
818         /* rotate & sort the brw_page array */
819         for (i = 0; i < oa_bufs; i++) {
820                 int stripe = lov_stripe_number(oinfo->oi_md, pga[i].off);
821
822                 shift = info[stripe].index + info[stripe].off;
823                 LASSERT(shift < oa_bufs);
824                 set->set_pga[shift] = pga[i];
825                 lov_stripe_offset(oinfo->oi_md, pga[i].off, stripe,
826                                   &set->set_pga[shift].off);
827                 info[stripe].off++;
828         }
829 out:
830         if (info)
831                 OBD_FREE_LARGE(info,
832                                sizeof(*info) * oinfo->oi_md->lsm_stripe_count);
833
834         if (rc == 0)
835                 *reqset = set;
836         else
837                 lov_fini_brw_set(set);
838
839         RETURN(rc);
840 }
841
842 int lov_fini_getattr_set(struct lov_request_set *set)
843 {
844         int rc = 0;
845         ENTRY;
846
847         if (set == NULL)
848                 RETURN(0);
849         LASSERT(set->set_exp);
850         if (cfs_atomic_read(&set->set_completes))
851                 rc = common_attr_done(set);
852
853         lov_put_reqset(set);
854
855         RETURN(rc);
856 }
857
858 /* The callback for osc_getattr_async that finilizes a request info when a
859  * response is received. */
860 static int cb_getattr_update(void *cookie, int rc)
861 {
862         struct obd_info *oinfo = cookie;
863         struct lov_request *lovreq;
864         lovreq = container_of(oinfo, struct lov_request, rq_oi);
865         return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
866 }
867
868 int lov_prep_getattr_set(struct obd_export *exp, struct obd_info *oinfo,
869                          struct lov_request_set **reqset)
870 {
871         struct lov_request_set *set;
872         struct lov_obd *lov = &exp->exp_obd->u.lov;
873         int rc = 0, i;
874         ENTRY;
875
876         OBD_ALLOC(set, sizeof(*set));
877         if (set == NULL)
878                 RETURN(-ENOMEM);
879         lov_init_set(set);
880
881         set->set_exp = exp;
882         set->set_oi = oinfo;
883
884         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
885                 struct lov_oinfo *loi;
886                 struct lov_request *req;
887
888                 loi = oinfo->oi_md->lsm_oinfo[i];
889                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
890                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
891                         if (oinfo->oi_oa->o_valid & OBD_MD_FLEPOCH)
892                                 /* SOM requires all the OSTs to be active. */
893                                 GOTO(out_set, rc = -EIO);
894                         continue;
895                 }
896
897                 OBD_ALLOC(req, sizeof(*req));
898                 if (req == NULL)
899                         GOTO(out_set, rc = -ENOMEM);
900
901                 req->rq_stripe = i;
902                 req->rq_idx = loi->loi_ost_idx;
903
904                 OBDO_ALLOC(req->rq_oi.oi_oa);
905                 if (req->rq_oi.oi_oa == NULL) {
906                         OBD_FREE(req, sizeof(*req));
907                         GOTO(out_set, rc = -ENOMEM);
908                 }
909                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
910                        sizeof(*req->rq_oi.oi_oa));
911                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
912                 req->rq_oi.oi_cb_up = cb_getattr_update;
913                 req->rq_oi.oi_capa = oinfo->oi_capa;
914
915                 lov_set_add_req(req, set);
916         }
917         if (!set->set_count)
918                 GOTO(out_set, rc = -EIO);
919         *reqset = set;
920         RETURN(rc);
921 out_set:
922         lov_fini_getattr_set(set);
923         RETURN(rc);
924 }
925
926 int lov_fini_destroy_set(struct lov_request_set *set)
927 {
928         ENTRY;
929
930         if (set == NULL)
931                 RETURN(0);
932         LASSERT(set->set_exp);
933         if (cfs_atomic_read(&set->set_completes)) {
934                 /* FIXME update qos data here */
935         }
936
937         lov_put_reqset(set);
938
939         RETURN(0);
940 }
941
942 int lov_prep_destroy_set(struct obd_export *exp, struct obd_info *oinfo,
943                          struct obdo *src_oa, struct lov_stripe_md *lsm,
944                          struct obd_trans_info *oti,
945                          struct lov_request_set **reqset)
946 {
947         struct lov_request_set *set;
948         struct lov_obd *lov = &exp->exp_obd->u.lov;
949         int rc = 0, i;
950         ENTRY;
951
952         OBD_ALLOC(set, sizeof(*set));
953         if (set == NULL)
954                 RETURN(-ENOMEM);
955         lov_init_set(set);
956
957         set->set_exp = exp;
958         set->set_oi = oinfo;
959         set->set_oi->oi_md = lsm;
960         set->set_oi->oi_oa = src_oa;
961         set->set_oti = oti;
962         if (oti != NULL && src_oa->o_valid & OBD_MD_FLCOOKIE)
963                 set->set_cookies = oti->oti_logcookies;
964
965         for (i = 0; i < lsm->lsm_stripe_count; i++) {
966                 struct lov_oinfo *loi;
967                 struct lov_request *req;
968
969                 loi = lsm->lsm_oinfo[i];
970                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
971                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
972                         continue;
973                 }
974
975                 OBD_ALLOC(req, sizeof(*req));
976                 if (req == NULL)
977                         GOTO(out_set, rc = -ENOMEM);
978
979                 req->rq_stripe = i;
980                 req->rq_idx = loi->loi_ost_idx;
981
982                 OBDO_ALLOC(req->rq_oi.oi_oa);
983                 if (req->rq_oi.oi_oa == NULL) {
984                         OBD_FREE(req, sizeof(*req));
985                         GOTO(out_set, rc = -ENOMEM);
986                 }
987                 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
988                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
989                 lov_set_add_req(req, set);
990         }
991         if (!set->set_count)
992                 GOTO(out_set, rc = -EIO);
993         *reqset = set;
994         RETURN(rc);
995 out_set:
996         lov_fini_destroy_set(set);
997         RETURN(rc);
998 }
999
1000 int lov_fini_setattr_set(struct lov_request_set *set)
1001 {
1002         int rc = 0;
1003         ENTRY;
1004
1005         if (set == NULL)
1006                 RETURN(0);
1007         LASSERT(set->set_exp);
1008         if (cfs_atomic_read(&set->set_completes)) {
1009                 rc = common_attr_done(set);
1010                 /* FIXME update qos data here */
1011         }
1012
1013         lov_put_reqset(set);
1014         RETURN(rc);
1015 }
1016
1017 int lov_update_setattr_set(struct lov_request_set *set,
1018                            struct lov_request *req, int rc)
1019 {
1020         struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
1021         struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
1022         ENTRY;
1023
1024         lov_update_set(set, req, rc);
1025
1026         /* grace error on inactive ost */
1027         if (rc && !(lov->lov_tgts[req->rq_idx] &&
1028                     lov->lov_tgts[req->rq_idx]->ltd_active))
1029                 rc = 0;
1030
1031         if (rc == 0) {
1032                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLCTIME)
1033                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_ctime =
1034                                 req->rq_oi.oi_oa->o_ctime;
1035                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLMTIME)
1036                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_mtime =
1037                                 req->rq_oi.oi_oa->o_mtime;
1038                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLATIME)
1039                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_atime =
1040                                 req->rq_oi.oi_oa->o_atime;
1041         }
1042
1043         RETURN(rc);
1044 }
1045
1046 /* The callback for osc_setattr_async that finilizes a request info when a
1047  * response is received. */
1048 static int cb_setattr_update(void *cookie, int rc)
1049 {
1050         struct obd_info *oinfo = cookie;
1051         struct lov_request *lovreq;
1052         lovreq = container_of(oinfo, struct lov_request, rq_oi);
1053         return lov_update_setattr_set(lovreq->rq_rqset, lovreq, rc);
1054 }
1055
1056 int lov_prep_setattr_set(struct obd_export *exp, struct obd_info *oinfo,
1057                          struct obd_trans_info *oti,
1058                          struct lov_request_set **reqset)
1059 {
1060         struct lov_request_set *set;
1061         struct lov_obd *lov = &exp->exp_obd->u.lov;
1062         int rc = 0, i;
1063         ENTRY;
1064
1065         OBD_ALLOC(set, sizeof(*set));
1066         if (set == NULL)
1067                 RETURN(-ENOMEM);
1068         lov_init_set(set);
1069
1070         set->set_exp = exp;
1071         set->set_oti = oti;
1072         set->set_oi = oinfo;
1073         if (oti != NULL && oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1074                 set->set_cookies = oti->oti_logcookies;
1075
1076         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
1077                 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
1078                 struct lov_request *req;
1079
1080                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
1081                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1082                         continue;
1083                 }
1084
1085                 OBD_ALLOC(req, sizeof(*req));
1086                 if (req == NULL)
1087                         GOTO(out_set, rc = -ENOMEM);
1088                 req->rq_stripe = i;
1089                 req->rq_idx = loi->loi_ost_idx;
1090
1091                 OBDO_ALLOC(req->rq_oi.oi_oa);
1092                 if (req->rq_oi.oi_oa == NULL) {
1093                         OBD_FREE(req, sizeof(*req));
1094                         GOTO(out_set, rc = -ENOMEM);
1095                 }
1096                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
1097                        sizeof(*req->rq_oi.oi_oa));
1098                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
1099                 req->rq_oi.oi_oa->o_stripe_idx = i;
1100                 req->rq_oi.oi_cb_up = cb_setattr_update;
1101                 req->rq_oi.oi_capa = oinfo->oi_capa;
1102
1103                 if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE) {
1104                         int off = lov_stripe_offset(oinfo->oi_md,
1105                                                     oinfo->oi_oa->o_size, i,
1106                                                     &req->rq_oi.oi_oa->o_size);
1107
1108                         if (off < 0 && req->rq_oi.oi_oa->o_size)
1109                                 req->rq_oi.oi_oa->o_size--;
1110
1111                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1112                                i, req->rq_oi.oi_oa->o_size,
1113                                oinfo->oi_oa->o_size);
1114                 }
1115                 lov_set_add_req(req, set);
1116         }
1117         if (!set->set_count)
1118                 GOTO(out_set, rc = -EIO);
1119         *reqset = set;
1120         RETURN(rc);
1121 out_set:
1122         lov_fini_setattr_set(set);
1123         RETURN(rc);
1124 }
1125
1126 int lov_fini_punch_set(struct lov_request_set *set)
1127 {
1128         int rc = 0;
1129         ENTRY;
1130
1131         if (set == NULL)
1132                 RETURN(0);
1133         LASSERT(set->set_exp);
1134         if (cfs_atomic_read(&set->set_completes)) {
1135                 rc = -EIO;
1136                 /* FIXME update qos data here */
1137                 if (cfs_atomic_read(&set->set_success))
1138                         rc = common_attr_done(set);
1139         }
1140
1141         lov_put_reqset(set);
1142
1143         RETURN(rc);
1144 }
1145
1146 int lov_update_punch_set(struct lov_request_set *set,
1147                          struct lov_request *req, int rc)
1148 {
1149         struct lov_obd *lov = &req->rq_rqset->set_exp->exp_obd->u.lov;
1150         struct lov_stripe_md *lsm = req->rq_rqset->set_oi->oi_md;
1151         ENTRY;
1152
1153         lov_update_set(set, req, rc);
1154
1155         /* grace error on inactive ost */
1156         if (rc && !lov->lov_tgts[req->rq_idx]->ltd_active)
1157                 rc = 0;
1158
1159         if (rc == 0) {
1160                 lov_stripe_lock(lsm);
1161                 if (req->rq_oi.oi_oa->o_valid & OBD_MD_FLBLOCKS) {
1162                         lsm->lsm_oinfo[req->rq_stripe]->loi_lvb.lvb_blocks =
1163                                 req->rq_oi.oi_oa->o_blocks;
1164                 }
1165
1166                 lov_stripe_unlock(lsm);
1167         }
1168
1169         RETURN(rc);
1170 }
1171
1172 /* The callback for osc_punch that finilizes a request info when a response
1173  * is received. */
1174 static int cb_update_punch(void *cookie, int rc)
1175 {
1176         struct obd_info *oinfo = cookie;
1177         struct lov_request *lovreq;
1178         lovreq = container_of(oinfo, struct lov_request, rq_oi);
1179         return lov_update_punch_set(lovreq->rq_rqset, lovreq, rc);
1180 }
1181
1182 int lov_prep_punch_set(struct obd_export *exp, struct obd_info *oinfo,
1183                        struct obd_trans_info *oti,
1184                        struct lov_request_set **reqset)
1185 {
1186         struct lov_request_set *set;
1187         struct lov_obd *lov = &exp->exp_obd->u.lov;
1188         int rc = 0, i;
1189         ENTRY;
1190
1191         OBD_ALLOC(set, sizeof(*set));
1192         if (set == NULL)
1193                 RETURN(-ENOMEM);
1194         lov_init_set(set);
1195
1196         set->set_oi = oinfo;
1197         set->set_exp = exp;
1198
1199         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
1200                 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
1201                 struct lov_request *req;
1202                 obd_off rs, re;
1203
1204                 if (!lov_stripe_intersects(oinfo->oi_md, i,
1205                                            oinfo->oi_policy.l_extent.start,
1206                                            oinfo->oi_policy.l_extent.end,
1207                                            &rs, &re))
1208                         continue;
1209
1210                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
1211                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1212                         GOTO(out_set, rc = -EIO);
1213                 }
1214
1215                 OBD_ALLOC(req, sizeof(*req));
1216                 if (req == NULL)
1217                         GOTO(out_set, rc = -ENOMEM);
1218                 req->rq_stripe = i;
1219                 req->rq_idx = loi->loi_ost_idx;
1220
1221                 OBDO_ALLOC(req->rq_oi.oi_oa);
1222                 if (req->rq_oi.oi_oa == NULL) {
1223                         OBD_FREE(req, sizeof(*req));
1224                         GOTO(out_set, rc = -ENOMEM);
1225                 }
1226                 memcpy(req->rq_oi.oi_oa, oinfo->oi_oa,
1227                        sizeof(*req->rq_oi.oi_oa));
1228                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
1229                 req->rq_oi.oi_oa->o_valid |= OBD_MD_FLGROUP;
1230
1231                 req->rq_oi.oi_oa->o_stripe_idx = i;
1232                 req->rq_oi.oi_cb_up = cb_update_punch;
1233
1234                 req->rq_oi.oi_policy.l_extent.start = rs;
1235                 req->rq_oi.oi_policy.l_extent.end = re;
1236                 req->rq_oi.oi_policy.l_extent.gid = -1;
1237
1238                 req->rq_oi.oi_capa = oinfo->oi_capa;
1239
1240                 lov_set_add_req(req, set);
1241         }
1242         if (!set->set_count)
1243                 GOTO(out_set, rc = -EIO);
1244         *reqset = set;
1245         RETURN(rc);
1246 out_set:
1247         lov_fini_punch_set(set);
1248         RETURN(rc);
1249 }
1250
1251 int lov_fini_sync_set(struct lov_request_set *set)
1252 {
1253         int rc = 0;
1254         ENTRY;
1255
1256         if (set == NULL)
1257                 RETURN(0);
1258         LASSERT(set->set_exp);
1259         if (cfs_atomic_read(&set->set_completes)) {
1260                 if (!cfs_atomic_read(&set->set_success))
1261                         rc = -EIO;
1262                 /* FIXME update qos data here */
1263         }
1264
1265         lov_put_reqset(set);
1266
1267         RETURN(rc);
1268 }
1269
1270 /* The callback for osc_sync that finilizes a request info when a
1271  * response is recieved. */
1272 static int cb_sync_update(void *cookie, int rc)
1273 {
1274         struct obd_info *oinfo = cookie;
1275         struct lov_request *lovreq;
1276
1277         lovreq = container_of(oinfo, struct lov_request, rq_oi);
1278         return lov_update_common_set(lovreq->rq_rqset, lovreq, rc);
1279 }
1280
1281 int lov_prep_sync_set(struct obd_export *exp, struct obd_info *oinfo,
1282                       obd_off start, obd_off end,
1283                       struct lov_request_set **reqset)
1284 {
1285         struct lov_request_set *set;
1286         struct lov_obd *lov = &exp->exp_obd->u.lov;
1287         int rc = 0, i;
1288         ENTRY;
1289
1290         OBD_ALLOC_PTR(set);
1291         if (set == NULL)
1292                 RETURN(-ENOMEM);
1293         lov_init_set(set);
1294
1295         set->set_exp = exp;
1296         set->set_oi = oinfo;
1297
1298         for (i = 0; i < oinfo->oi_md->lsm_stripe_count; i++) {
1299                 struct lov_oinfo *loi = oinfo->oi_md->lsm_oinfo[i];
1300                 struct lov_request *req;
1301                 obd_off rs, re;
1302
1303                 if (!lov_check_and_wait_active(lov, loi->loi_ost_idx)) {
1304                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1305                         continue;
1306                 }
1307
1308                 if (!lov_stripe_intersects(oinfo->oi_md, i, start, end, &rs,
1309                                            &re))
1310                         continue;
1311
1312                 OBD_ALLOC_PTR(req);
1313                 if (req == NULL)
1314                         GOTO(out_set, rc = -ENOMEM);
1315                 req->rq_stripe = i;
1316                 req->rq_idx = loi->loi_ost_idx;
1317
1318                 OBDO_ALLOC(req->rq_oi.oi_oa);
1319                 if (req->rq_oi.oi_oa == NULL) {
1320                         OBD_FREE(req, sizeof(*req));
1321                         GOTO(out_set, rc = -ENOMEM);
1322                 }
1323                 *req->rq_oi.oi_oa = *oinfo->oi_oa;
1324                 req->rq_oi.oi_oa->o_oi = loi->loi_oi;
1325                 req->rq_oi.oi_oa->o_stripe_idx = i;
1326
1327                 req->rq_oi.oi_policy.l_extent.start = rs;
1328                 req->rq_oi.oi_policy.l_extent.end = re;
1329                 req->rq_oi.oi_policy.l_extent.gid = -1;
1330                 req->rq_oi.oi_cb_up = cb_sync_update;
1331
1332                 lov_set_add_req(req, set);
1333         }
1334         if (!set->set_count)
1335                 GOTO(out_set, rc = -EIO);
1336         *reqset = set;
1337         RETURN(rc);
1338 out_set:
1339         lov_fini_sync_set(set);
1340         RETURN(rc);
1341 }
1342
1343 #define LOV_U64_MAX ((__u64)~0ULL)
1344 #define LOV_SUM_MAX(tot, add)                                           \
1345         do {                                                            \
1346                 if ((tot) + (add) < (tot))                              \
1347                         (tot) = LOV_U64_MAX;                            \
1348                 else                                                    \
1349                         (tot) += (add);                                 \
1350         } while(0)
1351
1352 int lov_fini_statfs(struct obd_device *obd, struct obd_statfs *osfs,int success)
1353 {
1354         ENTRY;
1355
1356         if (success) {
1357                 __u32 expected_stripes = lov_get_stripecnt(&obd->u.lov,
1358                                                            LOV_MAGIC, 0);
1359                 if (osfs->os_files != LOV_U64_MAX)
1360                         lov_do_div64(osfs->os_files, expected_stripes);
1361                 if (osfs->os_ffree != LOV_U64_MAX)
1362                         lov_do_div64(osfs->os_ffree, expected_stripes);
1363
1364                 spin_lock(&obd->obd_osfs_lock);
1365                 memcpy(&obd->obd_osfs, osfs, sizeof(*osfs));
1366                 obd->obd_osfs_age = cfs_time_current_64();
1367                 spin_unlock(&obd->obd_osfs_lock);
1368                 RETURN(0);
1369         }
1370
1371         RETURN(-EIO);
1372 }
1373
1374 int lov_fini_statfs_set(struct lov_request_set *set)
1375 {
1376         int rc = 0;
1377         ENTRY;
1378
1379         if (set == NULL)
1380                 RETURN(0);
1381
1382         if (cfs_atomic_read(&set->set_completes)) {
1383                 rc = lov_fini_statfs(set->set_obd, set->set_oi->oi_osfs,
1384                                      cfs_atomic_read(&set->set_success));
1385         }
1386         lov_put_reqset(set);
1387         RETURN(rc);
1388 }
1389
1390 void lov_update_statfs(struct obd_statfs *osfs, struct obd_statfs *lov_sfs,
1391                        int success)
1392 {
1393         int shift = 0, quit = 0;
1394         __u64 tmp;
1395
1396         if (success == 0) {
1397                 memcpy(osfs, lov_sfs, sizeof(*lov_sfs));
1398         } else {
1399                 if (osfs->os_bsize != lov_sfs->os_bsize) {
1400                         /* assume all block sizes are always powers of 2 */
1401                         /* get the bits difference */
1402                         tmp = osfs->os_bsize | lov_sfs->os_bsize;
1403                         for (shift = 0; shift <= 64; ++shift) {
1404                                 if (tmp & 1) {
1405                                         if (quit)
1406                                                 break;
1407                                         else
1408                                                 quit = 1;
1409                                         shift = 0;
1410                                 }
1411                                 tmp >>= 1;
1412                         }
1413                 }
1414
1415                 if (osfs->os_bsize < lov_sfs->os_bsize) {
1416                         osfs->os_bsize = lov_sfs->os_bsize;
1417
1418                         osfs->os_bfree  >>= shift;
1419                         osfs->os_bavail >>= shift;
1420                         osfs->os_blocks >>= shift;
1421                 } else if (shift != 0) {
1422                         lov_sfs->os_bfree  >>= shift;
1423                         lov_sfs->os_bavail >>= shift;
1424                         lov_sfs->os_blocks >>= shift;
1425                 }
1426 #ifdef MIN_DF
1427                 /* Sandia requested that df (and so, statfs) only
1428                    returned minimal available space on
1429                    a single OST, so people would be able to
1430                    write this much data guaranteed. */
1431                 if (osfs->os_bavail > lov_sfs->os_bavail) {
1432                         /* Presumably if new bavail is smaller,
1433                            new bfree is bigger as well */
1434                         osfs->os_bfree = lov_sfs->os_bfree;
1435                         osfs->os_bavail = lov_sfs->os_bavail;
1436                 }
1437 #else
1438                 osfs->os_bfree += lov_sfs->os_bfree;
1439                 osfs->os_bavail += lov_sfs->os_bavail;
1440 #endif
1441                 osfs->os_blocks += lov_sfs->os_blocks;
1442                 /* XXX not sure about this one - depends on policy.
1443                  *   - could be minimum if we always stripe on all OBDs
1444                  *     (but that would be wrong for any other policy,
1445                  *     if one of the OBDs has no more objects left)
1446                  *   - could be sum if we stripe whole objects
1447                  *   - could be average, just to give a nice number
1448                  *
1449                  * To give a "reasonable" (if not wholly accurate)
1450                  * number, we divide the total number of free objects
1451                  * by expected stripe count (watch out for overflow).
1452                  */
1453                 LOV_SUM_MAX(osfs->os_files, lov_sfs->os_files);
1454                 LOV_SUM_MAX(osfs->os_ffree, lov_sfs->os_ffree);
1455         }
1456 }
1457
1458 /* The callback for osc_statfs_async that finilizes a request info when a
1459  * response is received. */
1460 static int cb_statfs_update(void *cookie, int rc)
1461 {
1462         struct obd_info *oinfo = cookie;
1463         struct lov_request *lovreq;
1464         struct lov_request_set *set;
1465         struct obd_statfs *osfs, *lov_sfs;
1466         struct lov_obd *lov;
1467         struct lov_tgt_desc *tgt;
1468         struct obd_device *lovobd, *tgtobd;
1469         int success;
1470         ENTRY;
1471
1472         lovreq = container_of(oinfo, struct lov_request, rq_oi);
1473         set = lovreq->rq_rqset;
1474         lovobd = set->set_obd;
1475         lov = &lovobd->u.lov;
1476         osfs = set->set_oi->oi_osfs;
1477         lov_sfs = oinfo->oi_osfs;
1478         success = cfs_atomic_read(&set->set_success);
1479         /* XXX: the same is done in lov_update_common_set, however
1480            lovset->set_exp is not initialized. */
1481         lov_update_set(set, lovreq, rc);
1482         if (rc)
1483                 GOTO(out, rc);
1484
1485         obd_getref(lovobd);
1486         tgt = lov->lov_tgts[lovreq->rq_idx];
1487         if (!tgt || !tgt->ltd_active)
1488                 GOTO(out_update, rc);
1489
1490         tgtobd = class_exp2obd(tgt->ltd_exp);
1491         spin_lock(&tgtobd->obd_osfs_lock);
1492         memcpy(&tgtobd->obd_osfs, lov_sfs, sizeof(*lov_sfs));
1493         if ((oinfo->oi_flags & OBD_STATFS_FROM_CACHE) == 0)
1494                 tgtobd->obd_osfs_age = cfs_time_current_64();
1495         spin_unlock(&tgtobd->obd_osfs_lock);
1496
1497 out_update:
1498         lov_update_statfs(osfs, lov_sfs, success);
1499         obd_putref(lovobd);
1500
1501 out:
1502         if (set->set_oi->oi_flags & OBD_STATFS_PTLRPCD &&
1503             lov_set_finished(set, 0)) {
1504                 lov_statfs_interpret(NULL, set, set->set_count !=
1505                                      cfs_atomic_read(&set->set_success));
1506         }
1507
1508         RETURN(0);
1509 }
1510
1511 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
1512                         struct lov_request_set **reqset)
1513 {
1514         struct lov_request_set *set;
1515         struct lov_obd *lov = &obd->u.lov;
1516         int rc = 0, i;
1517         ENTRY;
1518
1519         OBD_ALLOC(set, sizeof(*set));
1520         if (set == NULL)
1521                 RETURN(-ENOMEM);
1522         lov_init_set(set);
1523
1524         set->set_obd = obd;
1525         set->set_oi = oinfo;
1526
1527         /* We only get block data from the OBD */
1528         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1529                 struct lov_request *req;
1530
1531                 if (lov->lov_tgts[i] == NULL ||
1532                     (!lov_check_and_wait_active(lov, i) &&
1533                      (oinfo->oi_flags & OBD_STATFS_NODELAY))) {
1534                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
1535                         continue;
1536                 }
1537
1538                 /* skip targets that have been explicitely disabled by the
1539                  * administrator */
1540                 if (!lov->lov_tgts[i]->ltd_exp) {
1541                         CDEBUG(D_HA, "lov idx %d administratively disabled\n", i);
1542                         continue;
1543                 }
1544
1545                 OBD_ALLOC(req, sizeof(*req));
1546                 if (req == NULL)
1547                         GOTO(out_set, rc = -ENOMEM);
1548
1549                 OBD_ALLOC(req->rq_oi.oi_osfs, sizeof(*req->rq_oi.oi_osfs));
1550                 if (req->rq_oi.oi_osfs == NULL) {
1551                         OBD_FREE(req, sizeof(*req));
1552                         GOTO(out_set, rc = -ENOMEM);
1553                 }
1554
1555                 req->rq_idx = i;
1556                 req->rq_oi.oi_cb_up = cb_statfs_update;
1557                 req->rq_oi.oi_flags = oinfo->oi_flags;
1558
1559                 lov_set_add_req(req, set);
1560         }
1561         if (!set->set_count)
1562                 GOTO(out_set, rc = -EIO);
1563         *reqset = set;
1564         RETURN(rc);
1565 out_set:
1566         lov_fini_statfs_set(set);
1567         RETURN(rc);
1568 }