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