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