Whamcloud - gitweb
LU-1286 obdclass: oops in lprocfs_remove_nolock
[fs/lustre-release.git] / lustre / lov / lov_obd.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lov/lov_obd.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Peter Braam <braam@clusterfs.com>
40  * Author: Mike Shaver <shaver@clusterfs.com>
41  * Author: Nathan Rutman <nathan@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LOV
45 #ifdef __KERNEL__
46 #include <libcfs/libcfs.h>
47 #else
48 #include <liblustre.h>
49 #endif
50
51 #include <obd_support.h>
52 #include <lustre_lib.h>
53 #include <lustre_net.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre_dlm.h>
56 #include <lustre_mds.h>
57 #include <lustre_debug.h>
58 #include <obd_class.h>
59 #include <obd_lov.h>
60 #include <obd_ost.h>
61 #include <lprocfs_status.h>
62 #include <lustre_param.h>
63 #include <cl_object.h>
64 #include <lustre/ll_fiemap.h>
65
66 #include "lov_internal.h"
67
68 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
69    Any function that expects lov_tgts to remain stationary must take a ref. */
70 static void lov_getref(struct obd_device *obd)
71 {
72         struct lov_obd *lov = &obd->u.lov;
73
74         /* nobody gets through here until lov_putref is done */
75         cfs_mutex_lock(&lov->lov_lock);
76         cfs_atomic_inc(&lov->lov_refcount);
77         cfs_mutex_unlock(&lov->lov_lock);
78         return;
79 }
80
81 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
82
83 static void lov_putref(struct obd_device *obd)
84 {
85         struct lov_obd *lov = &obd->u.lov;
86
87         cfs_mutex_lock(&lov->lov_lock);
88         /* ok to dec to 0 more than once -- ltd_exp's will be null */
89         if (cfs_atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
90                 CFS_LIST_HEAD(kill);
91                 int i;
92                 struct lov_tgt_desc *tgt, *n;
93                 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
94                        lov->lov_death_row);
95                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
96                         tgt = lov->lov_tgts[i];
97
98                         if (!tgt || !tgt->ltd_reap)
99                                 continue;
100                         cfs_list_add(&tgt->ltd_kill, &kill);
101                         /* XXX - right now there is a dependency on ld_tgt_count
102                          * being the maximum tgt index for computing the
103                          * mds_max_easize. So we can't shrink it. */
104                         lov_ost_pool_remove(&lov->lov_packed, i);
105                         lov->lov_tgts[i] = NULL;
106                         lov->lov_death_row--;
107                 }
108                 cfs_mutex_unlock(&lov->lov_lock);
109
110                 cfs_list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
111                         cfs_list_del(&tgt->ltd_kill);
112                         /* Disconnect */
113                         __lov_del_obd(obd, tgt);
114                 }
115         } else {
116                 cfs_mutex_unlock(&lov->lov_lock);
117         }
118 }
119
120 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
121                               enum obd_notify_event ev);
122 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
123                       enum obd_notify_event ev, void *data);
124
125
126 #define MAX_STRING_SIZE 128
127 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
128                     struct obd_connect_data *data)
129 {
130         struct lov_obd *lov = &obd->u.lov;
131         struct obd_uuid *tgt_uuid;
132         struct obd_device *tgt_obd;
133         static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
134         struct obd_import *imp;
135 #ifdef __KERNEL__
136         cfs_proc_dir_entry_t *lov_proc_dir;
137 #endif
138         int rc;
139         ENTRY;
140
141         if (!lov->lov_tgts[index])
142                 RETURN(-EINVAL);
143
144         tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
145         tgt_obd = lov->lov_tgts[index]->ltd_obd;
146
147         if (!tgt_obd->obd_set_up) {
148                 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
149                 RETURN(-EINVAL);
150         }
151
152         /* override the sp_me from lov */
153         tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
154
155         if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
156                 data->ocd_index = index;
157
158         /*
159          * Divine LOV knows that OBDs under it are OSCs.
160          */
161         imp = tgt_obd->u.cli.cl_import;
162
163         if (activate) {
164                 tgt_obd->obd_no_recov = 0;
165                 /* FIXME this is probably supposed to be
166                    ptlrpc_set_import_active.  Horrible naming. */
167                 ptlrpc_activate_import(imp);
168         }
169
170         rc = obd_register_observer(tgt_obd, obd);
171         if (rc) {
172                 CERROR("Target %s register_observer error %d\n",
173                        obd_uuid2str(tgt_uuid), rc);
174                 RETURN(rc);
175         }
176
177
178         if (imp->imp_invalid) {
179                 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
180                        "disabled\n", obd_uuid2str(tgt_uuid));
181                 RETURN(0);
182         }
183
184         rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
185                          &lov_osc_uuid, data, NULL);
186         if (rc || !lov->lov_tgts[index]->ltd_exp) {
187                 CERROR("Target %s connect error %d\n",
188                        obd_uuid2str(tgt_uuid), rc);
189                 RETURN(-ENODEV);
190         }
191
192         lov->lov_tgts[index]->ltd_reap = 0;
193
194         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
195                obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
196
197 #ifdef __KERNEL__
198         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
199         if (lov_proc_dir) {
200                 struct obd_device *osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
201                 cfs_proc_dir_entry_t *osc_symlink;
202
203                 LASSERT(osc_obd != NULL);
204                 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
205                 LASSERT(osc_obd->obd_type->typ_name != NULL);
206
207                 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
208                                                   lov_proc_dir,
209                                                   "../../../%s/%s",
210                                                   osc_obd->obd_type->typ_name,
211                                                   osc_obd->obd_name);
212                 if (osc_symlink == NULL) {
213                         CERROR("could not register LOV target "
214                                 "/proc/fs/lustre/%s/%s/target_obds/%s.",
215                                 obd->obd_type->typ_name, obd->obd_name,
216                                 osc_obd->obd_name);
217                         lprocfs_remove(&lov_proc_dir);
218                 }
219         }
220 #endif
221
222         rc = qos_add_tgt(obd, index);
223         if (rc)
224                 CERROR("qos_add_tgt failed %d\n", rc);
225
226         RETURN(0);
227 }
228
229 static int lov_connect(const struct lu_env *env,
230                        struct obd_export **exp, struct obd_device *obd,
231                        struct obd_uuid *cluuid, struct obd_connect_data *data,
232                        void *localdata)
233 {
234         struct lov_obd *lov = &obd->u.lov;
235         struct lov_tgt_desc *tgt;
236         struct lustre_handle conn;
237         int i, rc;
238         ENTRY;
239
240         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
241
242         rc = class_connect(&conn, obd, cluuid);
243         if (rc)
244                 RETURN(rc);
245
246         *exp = class_conn2export(&conn);
247
248         /* Why should there ever be more than 1 connect? */
249         lov->lov_connects++;
250         LASSERT(lov->lov_connects == 1);
251
252         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
253         if (data)
254                 lov->lov_ocd = *data;
255
256         obd_getref(obd);
257         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
258                 tgt = lov->lov_tgts[i];
259                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
260                         continue;
261                 /* Flags will be lowest common denominator */
262                 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
263                 if (rc) {
264                         CERROR("%s: lov connect tgt %d failed: %d\n",
265                                obd->obd_name, i, rc);
266                         continue;
267                 }
268                 /* connect to administrative disabled ost */
269                 if (!lov->lov_tgts[i]->ltd_exp)
270                         continue;
271
272                 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
273                                 OBD_NOTIFY_CONNECT, (void *)&i);
274                 if (rc) {
275                         CERROR("%s error sending notify %d\n",
276                                obd->obd_name, rc);
277                 }
278         }
279         obd_putref(obd);
280
281         RETURN(0);
282 }
283
284 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
285 {
286         cfs_proc_dir_entry_t *lov_proc_dir;
287         struct lov_obd *lov = &obd->u.lov;
288         struct obd_device *osc_obd;
289         int rc;
290         ENTRY;
291
292         osc_obd = class_exp2obd(tgt->ltd_exp);
293         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
294                obd->obd_name, osc_obd->obd_name);
295
296         if (tgt->ltd_active) {
297                 tgt->ltd_active = 0;
298                 lov->desc.ld_active_tgt_count--;
299                 tgt->ltd_exp->exp_obd->obd_inactive = 1;
300         }
301
302         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
303         if (lov_proc_dir) {
304                 cfs_proc_dir_entry_t *osc_symlink;
305
306                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
307                 if (osc_symlink) {
308                         lprocfs_remove(&osc_symlink);
309                 } else {
310                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing.",
311                                obd->obd_type->typ_name, obd->obd_name,
312                                osc_obd->obd_name);
313                 }
314         }
315
316         if (osc_obd) {
317                 /* Pass it on to our clients.
318                  * XXX This should be an argument to disconnect,
319                  * XXX not a back-door flag on the OBD.  Ah well.
320                  */
321                 osc_obd->obd_force = obd->obd_force;
322                 osc_obd->obd_fail = obd->obd_fail;
323                 osc_obd->obd_no_recov = obd->obd_no_recov;
324         }
325
326         obd_register_observer(osc_obd, NULL);
327
328         rc = obd_disconnect(tgt->ltd_exp);
329         if (rc) {
330                 CERROR("Target %s disconnect error %d\n",
331                        tgt->ltd_uuid.uuid, rc);
332                 rc = 0;
333         }
334
335         qos_del_tgt(obd, tgt);
336
337         tgt->ltd_exp = NULL;
338         RETURN(0);
339 }
340
341 static int lov_disconnect(struct obd_export *exp)
342 {
343         struct obd_device *obd = class_exp2obd(exp);
344         struct lov_obd *lov = &obd->u.lov;
345         int i, rc;
346         ENTRY;
347
348         if (!lov->lov_tgts)
349                 goto out;
350
351         /* Only disconnect the underlying layers on the final disconnect. */
352         lov->lov_connects--;
353         if (lov->lov_connects != 0) {
354                 /* why should there be more than 1 connect? */
355                 CERROR("disconnect #%d\n", lov->lov_connects);
356                 goto out;
357         }
358
359         /* Let's hold another reference so lov_del_obd doesn't spin through
360            putref every time */
361         obd_getref(obd);
362
363         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
364                 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
365                         /* Disconnection is the last we know about an obd */
366                         lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
367                 }
368         }
369         obd_putref(obd);
370
371 out:
372         rc = class_disconnect(exp); /* bz 9811 */
373         RETURN(rc);
374 }
375
376 /* Error codes:
377  *
378  *  -EINVAL  : UUID can't be found in the LOV's target list
379  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
380  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
381  *  any >= 0 : is log target index
382  */
383 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
384                               enum obd_notify_event ev)
385 {
386         struct lov_obd *lov = &obd->u.lov;
387         struct lov_tgt_desc *tgt;
388         int index, activate, active;
389         ENTRY;
390
391         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
392                lov, uuid->uuid, ev);
393
394         obd_getref(obd);
395         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
396                 tgt = lov->lov_tgts[index];
397                 if (!tgt || !tgt->ltd_exp)
398                         continue;
399
400                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
401                        index, obd_uuid2str(&tgt->ltd_uuid),
402                        tgt->ltd_exp->exp_handle.h_cookie);
403                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
404                         break;
405         }
406
407         if (index == lov->desc.ld_tgt_count)
408                 GOTO(out, index = -EINVAL);
409
410         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
411                 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
412
413                 if (lov->lov_tgts[index]->ltd_activate == activate) {
414                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
415                                uuid->uuid, activate ? "" : "de");
416                 } else {
417                         lov->lov_tgts[index]->ltd_activate = activate;
418                         CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
419                                activate ? "" : "de", obd_uuid2str(uuid));
420                 }
421
422         } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
423                 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
424
425                 if (lov->lov_tgts[index]->ltd_active == active) {
426                         CDEBUG(D_INFO, "OSC %s already %sactive!\n",
427                                uuid->uuid, active ? "" : "in");
428                         GOTO(out, index);
429                 } else {
430                         CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
431                                obd_uuid2str(uuid), active ? "" : "in");
432                 }
433
434                 lov->lov_tgts[index]->ltd_active = active;
435                 if (active) {
436                         lov->desc.ld_active_tgt_count++;
437                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
438                 } else {
439                         lov->desc.ld_active_tgt_count--;
440                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
441                 }
442                 /* remove any old qos penalty */
443                 lov->lov_tgts[index]->ltd_qos.ltq_penalty = 0;
444         } else {
445                 CERROR("Unknown event(%d) for uuid %s", ev, uuid->uuid);
446         }
447
448  out:
449         obd_putref(obd);
450         RETURN(index);
451 }
452
453 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
454                       enum obd_notify_event ev, void *data)
455 {
456         int rc = 0;
457         ENTRY;
458
459         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
460             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
461                 struct obd_uuid *uuid;
462
463                 LASSERT(watched);
464
465                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
466                         CERROR("unexpected notification of %s %s!\n",
467                                watched->obd_type->typ_name,
468                                watched->obd_name);
469                         RETURN(-EINVAL);
470                 }
471                 uuid = &watched->u.cli.cl_target_uuid;
472
473                 /* Set OSC as active before notifying the observer, so the
474                  * observer can use the OSC normally.
475                  */
476                 rc = lov_set_osc_active(obd, uuid, ev);
477                 if (rc < 0) {
478                         CERROR("event(%d) of %s failed: %d\n", ev,
479                                obd_uuid2str(uuid), rc);
480                         RETURN(rc);
481                 }
482                 /* active event should be pass lov target index as data */
483                 data = &rc;
484         }
485
486         /* Pass the notification up the chain. */
487         if (watched) {
488                 rc = obd_notify_observer(obd, watched, ev, data);
489         } else {
490                 /* NULL watched means all osc's in the lov (only for syncs) */
491                 /* sync event should be send lov idx as data */
492                 struct lov_obd *lov = &obd->u.lov;
493                 int i, is_sync;
494
495                 data = &i;
496                 is_sync = (ev == OBD_NOTIFY_SYNC) ||
497                           (ev == OBD_NOTIFY_SYNC_NONBLOCK);
498
499                 obd_getref(obd);
500                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
501                         if (!lov->lov_tgts[i])
502                                 continue;
503
504                         /* don't send sync event if target not
505                          * connected/activated */
506                         if (is_sync &&  !lov->lov_tgts[i]->ltd_active)
507                                 continue;
508
509                         rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
510                                                  ev, data);
511                         if (rc) {
512                                 CERROR("%s: notify %s of %s failed %d\n",
513                                        obd->obd_name,
514                                        obd->obd_observer->obd_name,
515                                        lov->lov_tgts[i]->ltd_obd->obd_name,
516                                        rc);
517                         }
518                 }
519                 obd_putref(obd);
520         }
521
522         RETURN(rc);
523 }
524
525 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
526                           __u32 index, int gen, int active)
527 {
528         struct lov_obd *lov = &obd->u.lov;
529         struct lov_tgt_desc *tgt;
530         struct obd_device *tgt_obd;
531         int rc;
532         ENTRY;
533
534         CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
535                uuidp->uuid, index, gen, active);
536
537         if (gen <= 0) {
538                 CERROR("request to add OBD %s with invalid generation: %d\n",
539                        uuidp->uuid, gen);
540                 RETURN(-EINVAL);
541         }
542
543         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
544                                         &obd->obd_uuid);
545         if (tgt_obd == NULL)
546                 RETURN(-EINVAL);
547
548         cfs_mutex_lock(&lov->lov_lock);
549
550         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
551                 tgt = lov->lov_tgts[index];
552                 CERROR("UUID %s already assigned at LOV target index %d\n",
553                        obd_uuid2str(&tgt->ltd_uuid), index);
554                 cfs_mutex_unlock(&lov->lov_lock);
555                 RETURN(-EEXIST);
556         }
557
558         if (index >= lov->lov_tgt_size) {
559                 /* We need to reallocate the lov target array. */
560                 struct lov_tgt_desc **newtgts, **old = NULL;
561                 __u32 newsize, oldsize = 0;
562
563                 newsize = max(lov->lov_tgt_size, (__u32)2);
564                 while (newsize < index + 1)
565                         newsize = newsize << 1;
566                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
567                 if (newtgts == NULL) {
568                         cfs_mutex_unlock(&lov->lov_lock);
569                         RETURN(-ENOMEM);
570                 }
571
572                 if (lov->lov_tgt_size) {
573                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
574                                lov->lov_tgt_size);
575                         old = lov->lov_tgts;
576                         oldsize = lov->lov_tgt_size;
577                 }
578
579                 lov->lov_tgts = newtgts;
580                 lov->lov_tgt_size = newsize;
581 #ifdef __KERNEL__
582                 smp_rmb();
583 #endif
584                 if (old)
585                         OBD_FREE(old, sizeof(*old) * oldsize);
586
587                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
588                        lov->lov_tgts, lov->lov_tgt_size);
589         }
590
591         OBD_ALLOC_PTR(tgt);
592         if (!tgt) {
593                 cfs_mutex_unlock(&lov->lov_lock);
594                 RETURN(-ENOMEM);
595         }
596
597         rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
598         if (rc) {
599                 cfs_mutex_unlock(&lov->lov_lock);
600                 OBD_FREE_PTR(tgt);
601                 RETURN(rc);
602         }
603
604         tgt->ltd_uuid = *uuidp;
605         tgt->ltd_obd = tgt_obd;
606         /* XXX - add a sanity check on the generation number. */
607         tgt->ltd_gen = gen;
608         tgt->ltd_index = index;
609         tgt->ltd_activate = active;
610         lov->lov_tgts[index] = tgt;
611         if (index >= lov->desc.ld_tgt_count)
612                 lov->desc.ld_tgt_count = index + 1;
613
614         cfs_mutex_unlock(&lov->lov_lock);
615
616         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
617                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
618
619         rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
620
621         if (lov->lov_connects == 0) {
622                 /* lov_connect hasn't been called yet. We'll do the
623                    lov_connect_obd on this target when that fn first runs,
624                    because we don't know the connect flags yet. */
625                 RETURN(0);
626         }
627
628         obd_getref(obd);
629
630         rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
631         if (rc)
632                 GOTO(out, rc);
633
634         /* connect to administrative disabled ost */
635         if (!tgt->ltd_exp)
636                 GOTO(out, rc = 0);
637
638         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
639                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
640                         (void *)&index);
641
642 out:
643         if (rc) {
644                 CERROR("add failed (%d), deleting %s\n", rc,
645                        obd_uuid2str(&tgt->ltd_uuid));
646                 lov_del_target(obd, index, 0, 0);
647         }
648         obd_putref(obd);
649         RETURN(rc);
650 }
651
652 /* Schedule a target for deletion */
653 int lov_del_target(struct obd_device *obd, __u32 index,
654                    struct obd_uuid *uuidp, int gen)
655 {
656         struct lov_obd *lov = &obd->u.lov;
657         int count = lov->desc.ld_tgt_count;
658         int rc = 0;
659         ENTRY;
660
661         if (index >= count) {
662                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
663                        index, count);
664                 RETURN(-EINVAL);
665         }
666
667         obd_getref(obd);
668
669         if (!lov->lov_tgts[index]) {
670                 CERROR("LOV target at index %d is not setup.\n", index);
671                 GOTO(out, rc = -EINVAL);
672         }
673
674         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
675                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
676                        lov_uuid2str(lov, index), index,
677                        obd_uuid2str(uuidp));
678                 GOTO(out, rc = -EINVAL);
679         }
680
681         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
682                lov_uuid2str(lov, index), index,
683                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
684                lov->lov_tgts[index]->ltd_active);
685
686         lov->lov_tgts[index]->ltd_reap = 1;
687         lov->lov_death_row++;
688         /* we really delete it from obd_putref */
689 out:
690         obd_putref(obd);
691
692         RETURN(rc);
693 }
694
695 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
696 {
697         struct obd_device *osc_obd;
698
699         LASSERT(tgt);
700         LASSERT(tgt->ltd_reap);
701
702         osc_obd = class_exp2obd(tgt->ltd_exp);
703
704         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
705                tgt->ltd_uuid.uuid,
706                osc_obd ? osc_obd->obd_name : "<no obd>");
707
708         if (tgt->ltd_exp)
709                 lov_disconnect_obd(obd, tgt);
710
711         OBD_FREE_PTR(tgt);
712
713         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
714            do it ourselves. And we can't do it from lov_cleanup,
715            because we just lost our only reference to it. */
716         if (osc_obd)
717                 class_manual_cleanup(osc_obd);
718 }
719
720 void lov_fix_desc_stripe_size(__u64 *val)
721 {
722         if (*val < PTLRPC_MAX_BRW_SIZE) {
723                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
724                               PTLRPC_MAX_BRW_SIZE);
725                 *val = PTLRPC_MAX_BRW_SIZE;
726         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
727                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
728                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
729                               "multiple of %u)\n",
730                               *val, LOV_MIN_STRIPE_SIZE);
731         }
732 }
733
734 void lov_fix_desc_stripe_count(__u32 *val)
735 {
736         if (*val == 0)
737                 *val = 1;
738 }
739
740 void lov_fix_desc_pattern(__u32 *val)
741 {
742         /* from lov_setstripe */
743         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
744                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
745                 *val = 0;
746         }
747 }
748
749 void lov_fix_desc_qos_maxage(__u32 *val)
750 {
751         /* fix qos_maxage */
752         if (*val == 0)
753                 *val = QOS_DEFAULT_MAXAGE;
754 }
755
756 void lov_fix_desc(struct lov_desc *desc)
757 {
758         lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
759         lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
760         lov_fix_desc_pattern(&desc->ld_pattern);
761         lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
762 }
763
764 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
765 {
766         struct lprocfs_static_vars lvars = { 0 };
767         struct lov_desc *desc;
768         struct lov_obd *lov = &obd->u.lov;
769         int rc;
770         ENTRY;
771
772         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
773                 CERROR("LOV setup requires a descriptor\n");
774                 RETURN(-EINVAL);
775         }
776
777         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
778
779         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
780                 CERROR("descriptor size wrong: %d > %d\n",
781                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
782                 RETURN(-EINVAL);
783         }
784
785         if (desc->ld_magic != LOV_DESC_MAGIC) {
786                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
787                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
788                                    obd->obd_name, desc);
789                             lustre_swab_lov_desc(desc);
790                 } else {
791                         CERROR("%s: Bad lov desc magic: %#x\n",
792                                obd->obd_name, desc->ld_magic);
793                         RETURN(-EINVAL);
794                 }
795         }
796
797         lov_fix_desc(desc);
798
799         desc->ld_active_tgt_count = 0;
800         lov->desc = *desc;
801         lov->lov_tgt_size = 0;
802
803         cfs_mutex_init(&lov->lov_lock);
804         cfs_atomic_set(&lov->lov_refcount, 0);
805         CFS_INIT_LIST_HEAD(&lov->lov_qos.lq_oss_list);
806         cfs_init_rwsem(&lov->lov_qos.lq_rw_sem);
807         lov->lov_sp_me = LUSTRE_SP_CLI;
808         lov->lov_qos.lq_dirty = 1;
809         lov->lov_qos.lq_rr.lqr_dirty = 1;
810         lov->lov_qos.lq_reset = 1;
811         /* Default priority is toward free space balance */
812         lov->lov_qos.lq_prio_free = 232;
813         /* Default threshold for rr (roughly 17%) */
814         lov->lov_qos.lq_threshold_rr = 43;
815         /* Init statfs fields */
816         OBD_ALLOC_PTR(lov->lov_qos.lq_statfs_data);
817         if (NULL == lov->lov_qos.lq_statfs_data)
818                 RETURN(-ENOMEM);
819         cfs_waitq_init(&lov->lov_qos.lq_statfs_waitq);
820
821         lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
822                                                    HASH_POOLS_MAX_BITS,
823                                                    HASH_POOLS_BKT_BITS, 0,
824                                                    CFS_HASH_MIN_THETA,
825                                                    CFS_HASH_MAX_THETA,
826                                                    &pool_hash_operations,
827                                                    CFS_HASH_DEFAULT);
828         CFS_INIT_LIST_HEAD(&lov->lov_pool_list);
829         lov->lov_pool_count = 0;
830         rc = lov_ost_pool_init(&lov->lov_packed, 0);
831         if (rc)
832                 GOTO(out_free_statfs, rc);
833         rc = lov_ost_pool_init(&lov->lov_qos.lq_rr.lqr_pool, 0);
834         if (rc)
835                 GOTO(out_free_lov_packed, rc);
836
837         lprocfs_lov_init_vars(&lvars);
838         lprocfs_obd_setup(obd, lvars.obd_vars);
839 #ifdef LPROCFS
840         {
841                 int rc;
842
843                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
844                                         0444, &lov_proc_target_fops, obd);
845                 if (rc)
846                         CWARN("Error adding the target_obd file\n");
847         }
848 #endif
849         lov->lov_pool_proc_entry = lprocfs_register("pools",
850                                                     obd->obd_proc_entry,
851                                                     NULL, NULL);
852
853         RETURN(0);
854
855 out_free_lov_packed:
856         lov_ost_pool_free(&lov->lov_packed);
857 out_free_statfs:
858         OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
859         return rc;
860 }
861
862 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
863 {
864         int rc = 0;
865         struct lov_obd *lov = &obd->u.lov;
866
867         ENTRY;
868
869         switch (stage) {
870         case OBD_CLEANUP_EARLY: {
871                 int i;
872                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
873                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
874                                 continue;
875                         obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
876                                        OBD_CLEANUP_EARLY);
877                 }
878                 break;
879         }
880         case OBD_CLEANUP_EXPORTS:
881                 rc = obd_llog_finish(obd, 0);
882                 if (rc != 0)
883                         CERROR("failed to cleanup llogging subsystems\n");
884                 break;
885         }
886         RETURN(rc);
887 }
888
889 static int lov_cleanup(struct obd_device *obd)
890 {
891         struct lov_obd *lov = &obd->u.lov;
892         cfs_list_t *pos, *tmp;
893         struct pool_desc *pool;
894         ENTRY;
895
896         cfs_list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
897                 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
898                 /* free pool structs */
899                 CDEBUG(D_INFO, "delete pool %p\n", pool);
900                 lov_pool_del(obd, pool->pool_name);
901         }
902         cfs_hash_putref(lov->lov_pools_hash_body);
903         lov_ost_pool_free(&(lov->lov_qos.lq_rr.lqr_pool));
904         lov_ost_pool_free(&lov->lov_packed);
905
906         lprocfs_obd_cleanup(obd);
907         if (lov->lov_tgts) {
908                 int i;
909                 obd_getref(obd);
910                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
911                         if (!lov->lov_tgts[i])
912                                 continue;
913
914                         /* Inactive targets may never have connected */
915                         if (lov->lov_tgts[i]->ltd_active ||
916                             cfs_atomic_read(&lov->lov_refcount))
917                             /* We should never get here - these
918                                should have been removed in the
919                              disconnect. */
920                                 CERROR("lov tgt %d not cleaned!"
921                                        " deathrow=%d, lovrc=%d\n",
922                                        i, lov->lov_death_row,
923                                        cfs_atomic_read(&lov->lov_refcount));
924                         lov_del_target(obd, i, 0, 0);
925                 }
926                 obd_putref(obd);
927                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
928                          lov->lov_tgt_size);
929                 lov->lov_tgt_size = 0;
930         }
931         OBD_FREE_PTR(lov->lov_qos.lq_statfs_data);
932         RETURN(0);
933 }
934
935 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
936                             __u32 *indexp, int *genp)
937 {
938         struct obd_uuid obd_uuid;
939         int cmd;
940         int rc = 0;
941         ENTRY;
942
943         switch(cmd = lcfg->lcfg_command) {
944         case LCFG_LOV_ADD_OBD:
945         case LCFG_LOV_ADD_INA:
946         case LCFG_LOV_DEL_OBD: {
947                 __u32 index;
948                 int gen;
949                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
950                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
951                         GOTO(out, rc = -EINVAL);
952
953                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
954
955                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", indexp) != 1)
956                         GOTO(out, rc = -EINVAL);
957                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
958                         GOTO(out, rc = -EINVAL);
959                 index = *indexp;
960                 gen = *genp;
961                 if (cmd == LCFG_LOV_ADD_OBD)
962                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
963                 else if (cmd == LCFG_LOV_ADD_INA)
964                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
965                 else
966                         rc = lov_del_target(obd, index, &obd_uuid, gen);
967                 GOTO(out, rc);
968         }
969         case LCFG_PARAM: {
970                 struct lprocfs_static_vars lvars = { 0 };
971                 struct lov_desc *desc = &(obd->u.lov.desc);
972
973                 if (!desc)
974                         GOTO(out, rc = -EINVAL);
975
976                 lprocfs_lov_init_vars(&lvars);
977
978                 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
979                                               lcfg, obd);
980                 if (rc > 0)
981                         rc = 0;
982                 GOTO(out, rc);
983         }
984         case LCFG_POOL_NEW:
985         case LCFG_POOL_ADD:
986         case LCFG_POOL_DEL:
987         case LCFG_POOL_REM:
988                 GOTO(out, rc);
989
990         default: {
991                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
992                 GOTO(out, rc = -EINVAL);
993
994         }
995         }
996 out:
997         RETURN(rc);
998 }
999
1000 #ifndef log2
1001 #define log2(n) cfs_ffz(~(n))
1002 #endif
1003
1004 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
1005                              struct lov_stripe_md **ea,
1006                              struct obd_trans_info *oti)
1007 {
1008         struct lov_obd *lov;
1009         struct obdo *tmp_oa;
1010         struct obd_uuid *ost_uuid = NULL;
1011         int rc = 0, i;
1012         ENTRY;
1013
1014         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1015                 src_oa->o_flags == OBD_FL_DELORPHAN);
1016
1017         lov = &export->exp_obd->u.lov;
1018
1019         OBDO_ALLOC(tmp_oa);
1020         if (tmp_oa == NULL)
1021                 RETURN(-ENOMEM);
1022
1023         if (oti->oti_ost_uuid) {
1024                 ost_uuid = oti->oti_ost_uuid;
1025                 CDEBUG(D_HA, "clearing orphans only for %s\n",
1026                        ost_uuid->uuid);
1027         }
1028
1029         obd_getref(export->exp_obd);
1030         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1031                 struct lov_stripe_md obj_md;
1032                 struct lov_stripe_md *obj_mdp = &obj_md;
1033                 struct lov_tgt_desc *tgt;
1034                 int err;
1035
1036                 tgt = lov->lov_tgts[i];
1037                 if (!tgt)
1038                         continue;
1039
1040                 /* if called for a specific target, we don't
1041                    care if it is not active. */
1042                 if (!lov->lov_tgts[i]->ltd_active && ost_uuid == NULL) {
1043                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
1044                         continue;
1045                 }
1046
1047                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
1048                         continue;
1049
1050                 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
1051                        obd_uuid2str(ost_uuid));
1052
1053                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1054
1055                 LASSERT(lov->lov_tgts[i]->ltd_exp);
1056                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1057                 err = obd_create(NULL, lov->lov_tgts[i]->ltd_exp,
1058                                  tmp_oa, &obj_mdp, oti);
1059                 if (err) {
1060                         /* This export will be disabled until it is recovered,
1061                            and then orphan recovery will be completed. */
1062                         CERROR("error in orphan recovery on OST idx %d/%d: "
1063                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
1064                         rc = err;
1065                 }
1066
1067                 if (ost_uuid)
1068                         break;
1069         }
1070         obd_putref(export->exp_obd);
1071
1072         OBDO_FREE(tmp_oa);
1073         RETURN(rc);
1074 }
1075
1076 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
1077                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
1078 {
1079         struct lov_stripe_md *obj_mdp, *lsm;
1080         struct lov_obd *lov = &exp->exp_obd->u.lov;
1081         unsigned ost_idx;
1082         int rc, i;
1083         ENTRY;
1084
1085         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1086                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
1087
1088         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
1089         if (obj_mdp == NULL)
1090                 RETURN(-ENOMEM);
1091
1092         ost_idx = src_oa->o_nlink;
1093         lsm = *ea;
1094         if (lsm == NULL)
1095                 GOTO(out, rc = -EINVAL);
1096         if (ost_idx >= lov->desc.ld_tgt_count ||
1097             !lov->lov_tgts[ost_idx])
1098                 GOTO(out, rc = -EINVAL);
1099
1100         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1101                 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
1102                         if (lsm->lsm_oinfo[i]->loi_id != src_oa->o_id)
1103                                 GOTO(out, rc = -EINVAL);
1104                         break;
1105                 }
1106         }
1107         if (i == lsm->lsm_stripe_count)
1108                 GOTO(out, rc = -EINVAL);
1109
1110         rc = obd_create(NULL, lov->lov_tgts[ost_idx]->ltd_exp,
1111                         src_oa, &obj_mdp, oti);
1112 out:
1113         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
1114         RETURN(rc);
1115 }
1116
1117 /* the LOV expects oa->o_id to be set to the LOV object id */
1118 static int lov_create(const struct lu_env *env, struct obd_export *exp,
1119                       struct obdo *src_oa, struct lov_stripe_md **ea,
1120                       struct obd_trans_info *oti)
1121 {
1122         struct lov_obd *lov;
1123         struct obd_info oinfo;
1124         struct lov_request_set *set = NULL;
1125         struct lov_request *req;
1126         struct l_wait_info  lwi = { 0 };
1127         int rc = 0;
1128         ENTRY;
1129
1130         LASSERT(ea != NULL);
1131         if (exp == NULL)
1132                 RETURN(-EINVAL);
1133
1134         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1135             src_oa->o_flags == OBD_FL_DELORPHAN) {
1136                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
1137                 RETURN(rc);
1138         }
1139
1140         lov = &exp->exp_obd->u.lov;
1141         if (!lov->desc.ld_active_tgt_count)
1142                 RETURN(-EIO);
1143
1144         obd_getref(exp->exp_obd);
1145         /* Recreate a specific object id at the given OST index */
1146         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1147             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1148                  rc = lov_recreate(exp, src_oa, ea, oti);
1149                  GOTO(out, rc);
1150         }
1151
1152         /* issue statfs rpcs if the osfs data is older than qos_maxage - 1s,
1153          * later in alloc_qos(), we will wait for those rpcs to complete if
1154          * the osfs age is older than 2 * qos_maxage */
1155         qos_statfs_update(exp->exp_obd,
1156                           cfs_time_shift_64(-lov->desc.ld_qos_maxage +
1157                                             OBD_STATFS_CACHE_SECONDS),
1158                           0);
1159
1160         rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1161         if (rc)
1162                 GOTO(out, rc);
1163
1164         cfs_list_for_each_entry(req, &set->set_list, rq_link) {
1165                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1166                 rc = obd_create_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1167                                       &req->rq_oi, &req->rq_oi.oi_md, oti);
1168         }
1169
1170         /* osc_create have timeout equ obd_timeout/2 so waiting don't be
1171          * longer then this */
1172         l_wait_event(set->set_waitq, lov_finished_set(set), &lwi);
1173
1174         /* we not have ptlrpc set for assign set->interpret and should
1175          * be call interpret function himself. calling from cb_create_update
1176          * not permited because lov_fini_create_set can sleep for long time,
1177          * but we must avoid sleeping in ptlrpcd interpret function. */
1178         rc = lov_fini_create_set(set, ea);
1179 out:
1180         obd_putref(exp->exp_obd);
1181         RETURN(rc);
1182 }
1183
1184 #define ASSERT_LSM_MAGIC(lsmp)                                                  \
1185 do {                                                                            \
1186         LASSERT((lsmp) != NULL);                                                \
1187         LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 ||                          \
1188                  (lsmp)->lsm_magic == LOV_MAGIC_V3),                            \
1189                  "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic);              \
1190 } while (0)
1191
1192 static int lov_destroy(const struct lu_env *env, struct obd_export *exp,
1193                        struct obdo *oa, struct lov_stripe_md *lsm,
1194                        struct obd_trans_info *oti, struct obd_export *md_exp,
1195                        void *capa)
1196 {
1197         struct lov_request_set *set;
1198         struct obd_info oinfo;
1199         struct lov_request *req;
1200         cfs_list_t *pos;
1201         struct lov_obd *lov;
1202         int rc = 0, err = 0;
1203         ENTRY;
1204
1205         ASSERT_LSM_MAGIC(lsm);
1206
1207         if (!exp || !exp->exp_obd)
1208                 RETURN(-ENODEV);
1209
1210         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1211                 LASSERT(oti);
1212                 LASSERT(oti->oti_logcookies);
1213         }
1214
1215         lov = &exp->exp_obd->u.lov;
1216         obd_getref(exp->exp_obd);
1217         rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1218         if (rc)
1219                 GOTO(out, rc);
1220
1221         cfs_list_for_each (pos, &set->set_list) {
1222                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1223
1224                 if (oa->o_valid & OBD_MD_FLCOOKIE)
1225                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1226
1227                 err = obd_destroy(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1228                                   req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1229                 err = lov_update_common_set(set, req, err);
1230                 if (err) {
1231                         CERROR("error: destroying objid "LPX64" subobj "
1232                                LPX64" on OST idx %d: rc = %d\n",
1233                                oa->o_id, req->rq_oi.oi_oa->o_id,
1234                                req->rq_idx, err);
1235                         if (!rc)
1236                                 rc = err;
1237                 }
1238         }
1239
1240         if (rc == 0) {
1241                 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1242                 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1243         }
1244         err = lov_fini_destroy_set(set);
1245 out:
1246         obd_putref(exp->exp_obd);
1247         RETURN(rc ? rc : err);
1248 }
1249
1250 static int lov_getattr(const struct lu_env *env, struct obd_export *exp,
1251                        struct obd_info *oinfo)
1252 {
1253         struct lov_request_set *set;
1254         struct lov_request *req;
1255         cfs_list_t *pos;
1256         struct lov_obd *lov;
1257         int err = 0, rc = 0;
1258         ENTRY;
1259
1260         LASSERT(oinfo);
1261         ASSERT_LSM_MAGIC(oinfo->oi_md);
1262
1263         if (!exp || !exp->exp_obd)
1264                 RETURN(-ENODEV);
1265
1266         lov = &exp->exp_obd->u.lov;
1267
1268         rc = lov_prep_getattr_set(exp, oinfo, &set);
1269         if (rc)
1270                 RETURN(rc);
1271
1272         cfs_list_for_each (pos, &set->set_list) {
1273                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1274
1275                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1276                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1277                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1278
1279                 rc = obd_getattr(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1280                                  &req->rq_oi);
1281                 err = lov_update_common_set(set, req, rc);
1282                 if (err) {
1283                         CERROR("error: getattr objid "LPX64" subobj "
1284                                LPX64" on OST idx %d: rc = %d\n",
1285                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1286                                req->rq_idx, err);
1287                         break;
1288                 }
1289         }
1290
1291         rc = lov_fini_getattr_set(set);
1292         if (err)
1293                 rc = err;
1294         RETURN(rc);
1295 }
1296
1297 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1298                                  void *data, int rc)
1299 {
1300         struct lov_request_set *lovset = (struct lov_request_set *)data;
1301         int err;
1302         ENTRY;
1303
1304         /* don't do attribute merge if this aysnc op failed */
1305         if (rc)
1306                 cfs_atomic_set(&lovset->set_completes, 0);
1307         err = lov_fini_getattr_set(lovset);
1308         RETURN(rc ? rc : err);
1309 }
1310
1311 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1312                               struct ptlrpc_request_set *rqset)
1313 {
1314         struct lov_request_set *lovset;
1315         struct lov_obd *lov;
1316         cfs_list_t *pos;
1317         struct lov_request *req;
1318         int rc = 0, err;
1319         ENTRY;
1320
1321         LASSERT(oinfo);
1322         ASSERT_LSM_MAGIC(oinfo->oi_md);
1323
1324         if (!exp || !exp->exp_obd)
1325                 RETURN(-ENODEV);
1326
1327         lov = &exp->exp_obd->u.lov;
1328
1329         rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1330         if (rc)
1331                 RETURN(rc);
1332
1333         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1334                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1335                oinfo->oi_md->lsm_stripe_size);
1336
1337         cfs_list_for_each (pos, &lovset->set_list) {
1338                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1339
1340                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1341                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1342                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1343                 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1344                                        &req->rq_oi, rqset);
1345                 if (rc) {
1346                         CERROR("error: getattr objid "LPX64" subobj "
1347                                LPX64" on OST idx %d: rc = %d\n",
1348                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1349                                req->rq_idx, rc);
1350                         GOTO(out, rc);
1351                 }
1352         }
1353
1354         if (!cfs_list_empty(&rqset->set_requests)) {
1355                 LASSERT(rc == 0);
1356                 LASSERT (rqset->set_interpret == NULL);
1357                 rqset->set_interpret = lov_getattr_interpret;
1358                 rqset->set_arg = (void *)lovset;
1359                 RETURN(rc);
1360         }
1361 out:
1362         if (rc)
1363                 cfs_atomic_set(&lovset->set_completes, 0);
1364         err = lov_fini_getattr_set(lovset);
1365         RETURN(rc ? rc : err);
1366 }
1367
1368 static int lov_setattr(const struct lu_env *env, struct obd_export *exp,
1369                        struct obd_info *oinfo, struct obd_trans_info *oti)
1370 {
1371         struct lov_request_set *set;
1372         struct lov_obd *lov;
1373         cfs_list_t *pos;
1374         struct lov_request *req;
1375         int err = 0, rc = 0;
1376         ENTRY;
1377
1378         LASSERT(oinfo);
1379         ASSERT_LSM_MAGIC(oinfo->oi_md);
1380
1381         if (!exp || !exp->exp_obd)
1382                 RETURN(-ENODEV);
1383
1384         /* for now, we only expect the following updates here */
1385         LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1386                                             OBD_MD_FLMODE | OBD_MD_FLATIME |
1387                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1388                                             OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1389                                             OBD_MD_FLGROUP | OBD_MD_FLUID |
1390                                             OBD_MD_FLGID | OBD_MD_FLFID |
1391                                             OBD_MD_FLGENER)));
1392         lov = &exp->exp_obd->u.lov;
1393         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1394         if (rc)
1395                 RETURN(rc);
1396
1397         cfs_list_for_each (pos, &set->set_list) {
1398                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1399
1400                 rc = obd_setattr(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1401                                  &req->rq_oi, NULL);
1402                 err = lov_update_setattr_set(set, req, rc);
1403                 if (err) {
1404                         CERROR("error: setattr objid "LPX64" subobj "
1405                                LPX64" on OST idx %d: rc = %d\n",
1406                                set->set_oi->oi_oa->o_id,
1407                                req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1408                         if (!rc)
1409                                 rc = err;
1410                 }
1411         }
1412         err = lov_fini_setattr_set(set);
1413         if (!rc)
1414                 rc = err;
1415         RETURN(rc);
1416 }
1417
1418 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1419                                  void *data, int rc)
1420 {
1421         struct lov_request_set *lovset = (struct lov_request_set *)data;
1422         int err;
1423         ENTRY;
1424
1425         if (rc)
1426                 cfs_atomic_set(&lovset->set_completes, 0);
1427         err = lov_fini_setattr_set(lovset);
1428         RETURN(rc ? rc : err);
1429 }
1430
1431 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1432    needed. Otherwise, a client is waiting for responses. */
1433 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1434                              struct obd_trans_info *oti,
1435                              struct ptlrpc_request_set *rqset)
1436 {
1437         struct lov_request_set *set;
1438         struct lov_request *req;
1439         cfs_list_t *pos;
1440         struct lov_obd *lov;
1441         int rc = 0;
1442         ENTRY;
1443
1444         LASSERT(oinfo);
1445         ASSERT_LSM_MAGIC(oinfo->oi_md);
1446         if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1447                 LASSERT(oti);
1448                 LASSERT(oti->oti_logcookies);
1449         }
1450
1451         if (!exp || !exp->exp_obd)
1452                 RETURN(-ENODEV);
1453
1454         lov = &exp->exp_obd->u.lov;
1455         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1456         if (rc)
1457                 RETURN(rc);
1458
1459         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1460                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1461                oinfo->oi_md->lsm_stripe_size);
1462
1463         cfs_list_for_each (pos, &set->set_list) {
1464                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1465
1466                 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1467                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1468
1469                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1470                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1471                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1472
1473                 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1474                                        &req->rq_oi, oti, rqset);
1475                 if (rc) {
1476                         CERROR("error: setattr objid "LPX64" subobj "
1477                                LPX64" on OST idx %d: rc = %d\n",
1478                                set->set_oi->oi_oa->o_id,
1479                                req->rq_oi.oi_oa->o_id,
1480                                req->rq_idx, rc);
1481                         break;
1482                 }
1483         }
1484
1485         /* If we are not waiting for responses on async requests, return. */
1486         if (rc || !rqset || cfs_list_empty(&rqset->set_requests)) {
1487                 int err;
1488                 if (rc)
1489                         cfs_atomic_set(&set->set_completes, 0);
1490                 err = lov_fini_setattr_set(set);
1491                 RETURN(rc ? rc : err);
1492         }
1493
1494         LASSERT(rqset->set_interpret == NULL);
1495         rqset->set_interpret = lov_setattr_interpret;
1496         rqset->set_arg = (void *)set;
1497
1498         RETURN(0);
1499 }
1500
1501 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1502                                void *data, int rc)
1503 {
1504         struct lov_request_set *lovset = (struct lov_request_set *)data;
1505         int err;
1506         ENTRY;
1507
1508         if (rc)
1509                 cfs_atomic_set(&lovset->set_completes, 0);
1510         err = lov_fini_punch_set(lovset);
1511         RETURN(rc ? rc : err);
1512 }
1513
1514 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1515  * we can send this 'punch' to just the authoritative node and the nodes
1516  * that the punch will affect. */
1517 static int lov_punch(const struct lu_env *env, struct obd_export *exp,
1518                      struct obd_info *oinfo, struct obd_trans_info *oti,
1519                      struct ptlrpc_request_set *rqset)
1520 {
1521         struct lov_request_set *set;
1522         struct lov_obd *lov;
1523         cfs_list_t *pos;
1524         struct lov_request *req;
1525         int rc = 0;
1526         ENTRY;
1527
1528         LASSERT(oinfo);
1529         ASSERT_LSM_MAGIC(oinfo->oi_md);
1530
1531         if (!exp || !exp->exp_obd)
1532                 RETURN(-ENODEV);
1533
1534         lov = &exp->exp_obd->u.lov;
1535         rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1536         if (rc)
1537                 RETURN(rc);
1538
1539         cfs_list_for_each (pos, &set->set_list) {
1540                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1541
1542                 rc = obd_punch(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1543                                &req->rq_oi, NULL, rqset);
1544                 if (rc) {
1545                         CERROR("error: punch objid "LPX64" subobj "LPX64
1546                                " on OST idx %d: rc = %d\n",
1547                                set->set_oi->oi_oa->o_id,
1548                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1549                         break;
1550                 }
1551         }
1552
1553         if (rc || cfs_list_empty(&rqset->set_requests)) {
1554                 int err;
1555                 err = lov_fini_punch_set(set);
1556                 RETURN(rc ? rc : err);
1557         }
1558
1559         LASSERT(rqset->set_interpret == NULL);
1560         rqset->set_interpret = lov_punch_interpret;
1561         rqset->set_arg = (void *)set;
1562
1563         RETURN(0);
1564 }
1565
1566 static int lov_sync_interpret(struct ptlrpc_request_set *rqset,
1567                               void *data, int rc)
1568 {
1569         struct lov_request_set *lovset = data;
1570         int err;
1571         ENTRY;
1572
1573         if (rc)
1574                 cfs_atomic_set(&lovset->set_completes, 0);
1575         err = lov_fini_sync_set(lovset);
1576         RETURN(rc ?: err);
1577 }
1578
1579 static int lov_sync(const struct lu_env *env, struct obd_export *exp,
1580                     struct obd_info *oinfo, obd_off start, obd_off end,
1581                     struct ptlrpc_request_set *rqset)
1582 {
1583         struct lov_request_set *set = NULL;
1584         struct lov_obd *lov;
1585         cfs_list_t *pos;
1586         struct lov_request *req;
1587         int rc = 0;
1588         ENTRY;
1589
1590         ASSERT_LSM_MAGIC(oinfo->oi_md);
1591         LASSERT(rqset != NULL);
1592
1593         if (!exp->exp_obd)
1594                 RETURN(-ENODEV);
1595
1596         lov = &exp->exp_obd->u.lov;
1597         rc = lov_prep_sync_set(exp, oinfo, start, end, &set);
1598         if (rc)
1599                 RETURN(rc);
1600
1601         CDEBUG(D_INFO, "fsync objid "LPX64" ["LPX64", "LPX64"]\n",
1602                set->set_oi->oi_oa->o_id, start, end);
1603
1604         cfs_list_for_each (pos, &set->set_list) {
1605                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1606
1607                 rc = obd_sync(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1608                               &req->rq_oi, req->rq_oi.oi_policy.l_extent.start,
1609                               req->rq_oi.oi_policy.l_extent.end, rqset);
1610                 if (rc) {
1611                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1612                                " on OST idx %d: rc = %d\n",
1613                                set->set_oi->oi_oa->o_id,
1614                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1615                         break;
1616                 }
1617         }
1618
1619         /* If we are not waiting for responses on async requests, return. */
1620         if (rc || cfs_list_empty(&rqset->set_requests)) {
1621                 int err = lov_fini_sync_set(set);
1622
1623                 RETURN(rc ?: err);
1624         }
1625
1626         LASSERT(rqset->set_interpret == NULL);
1627         rqset->set_interpret = lov_sync_interpret;
1628         rqset->set_arg = (void *)set;
1629
1630         RETURN(0);
1631 }
1632
1633 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1634                          obd_count oa_bufs, struct brw_page *pga)
1635 {
1636         struct obd_info oinfo = { { { 0 } } };
1637         int i, rc = 0;
1638
1639         oinfo.oi_oa = lov_oinfo->oi_oa;
1640
1641         /* The caller just wants to know if there's a chance that this
1642          * I/O can succeed */
1643         for (i = 0; i < oa_bufs; i++) {
1644                 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1645                 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
1646                 obd_off start, end;
1647
1648                 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1649                                            pga[i].off + pga[i].count - 1,
1650                                            &start, &end))
1651                         continue;
1652
1653                 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1654                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1655                         return -EIO;
1656                 }
1657
1658                 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1659                              1, &pga[i], NULL);
1660                 if (rc)
1661                         break;
1662         }
1663         return rc;
1664 }
1665
1666 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1667                    obd_count oa_bufs, struct brw_page *pga,
1668                    struct obd_trans_info *oti)
1669 {
1670         struct lov_request_set *set;
1671         struct lov_request *req;
1672         cfs_list_t *pos;
1673         struct lov_obd *lov = &exp->exp_obd->u.lov;
1674         int err, rc = 0;
1675         ENTRY;
1676
1677         ASSERT_LSM_MAGIC(oinfo->oi_md);
1678
1679         if (cmd == OBD_BRW_CHECK) {
1680                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1681                 RETURN(rc);
1682         }
1683
1684         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1685         if (rc)
1686                 RETURN(rc);
1687
1688         cfs_list_for_each (pos, &set->set_list) {
1689                 struct obd_export *sub_exp;
1690                 struct brw_page *sub_pga;
1691                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1692
1693                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1694                 sub_pga = set->set_pga + req->rq_pgaidx;
1695                 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1696                              sub_pga, oti);
1697                 if (rc)
1698                         break;
1699                 lov_update_common_set(set, req, rc);
1700         }
1701
1702         err = lov_fini_brw_set(set);
1703         if (!rc)
1704                 rc = err;
1705         RETURN(rc);
1706 }
1707
1708 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1709                                  void *data, int rc)
1710 {
1711         struct lov_request_set *lovset = (struct lov_request_set *)data;
1712         ENTRY;
1713         rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
1714         RETURN(rc);
1715 }
1716
1717 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1718                        struct ldlm_enqueue_info *einfo,
1719                        struct ptlrpc_request_set *rqset)
1720 {
1721         ldlm_mode_t mode = einfo->ei_mode;
1722         struct lov_request_set *set;
1723         struct lov_request *req;
1724         cfs_list_t *pos;
1725         struct lov_obd *lov;
1726         ldlm_error_t rc;
1727         ENTRY;
1728
1729         LASSERT(oinfo);
1730         ASSERT_LSM_MAGIC(oinfo->oi_md);
1731         LASSERT(mode == (mode & -mode));
1732
1733         /* we should never be asked to replay a lock this way. */
1734         LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
1735
1736         if (!exp || !exp->exp_obd)
1737                 RETURN(-ENODEV);
1738
1739         lov = &exp->exp_obd->u.lov;
1740         rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1741         if (rc)
1742                 RETURN(rc);
1743
1744         cfs_list_for_each (pos, &set->set_list) {
1745                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1746
1747                 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1748                                  &req->rq_oi, einfo, rqset);
1749                 if (rc != ELDLM_OK)
1750                         GOTO(out, rc);
1751         }
1752
1753         if (rqset && !cfs_list_empty(&rqset->set_requests)) {
1754                 LASSERT(rc == 0);
1755                 LASSERT(rqset->set_interpret == NULL);
1756                 rqset->set_interpret = lov_enqueue_interpret;
1757                 rqset->set_arg = (void *)set;
1758                 RETURN(rc);
1759         }
1760 out:
1761         rc = lov_fini_enqueue_set(set, mode, rc, rqset);
1762         RETURN(rc);
1763 }
1764
1765 static int lov_change_cbdata(struct obd_export *exp,
1766                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1767                              void *data)
1768 {
1769         struct lov_obd *lov;
1770         int rc = 0, i;
1771         ENTRY;
1772
1773         ASSERT_LSM_MAGIC(lsm);
1774
1775         if (!exp || !exp->exp_obd)
1776                 RETURN(-ENODEV);
1777
1778         lov = &exp->exp_obd->u.lov;
1779         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1780                 struct lov_stripe_md submd;
1781                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1782
1783                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1784                         CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1785                         continue;
1786                 }
1787
1788                 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1789                 submd.lsm_object_id = loi->loi_id;
1790                 submd.lsm_object_seq = loi->loi_seq;
1791                 submd.lsm_stripe_count = 0;
1792                 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1793                                        &submd, it, data);
1794         }
1795         RETURN(rc);
1796 }
1797
1798 /* find any ldlm lock of the inode in lov
1799  * return 0    not find
1800  *        1    find one
1801  *      < 0    error */
1802 static int lov_find_cbdata(struct obd_export *exp,
1803                            struct lov_stripe_md *lsm, ldlm_iterator_t it,
1804                            void *data)
1805 {
1806         struct lov_obd *lov;
1807         int rc = 0, i;
1808         ENTRY;
1809
1810         ASSERT_LSM_MAGIC(lsm);
1811
1812         if (!exp || !exp->exp_obd)
1813                 RETURN(-ENODEV);
1814
1815         lov = &exp->exp_obd->u.lov;
1816         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1817                 struct lov_stripe_md submd;
1818                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1819
1820                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1821                         CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1822                         continue;
1823                 }
1824
1825                 LASSERT_SEQ_IS_MDT(loi->loi_seq);
1826                 submd.lsm_object_id = loi->loi_id;
1827                 submd.lsm_object_seq = loi->loi_seq;
1828                 submd.lsm_stripe_count = 0;
1829                 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1830                                      &submd, it, data);
1831                 if (rc != 0)
1832                         RETURN(rc);
1833         }
1834         RETURN(rc);
1835 }
1836
1837 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1838                       __u32 mode, struct lustre_handle *lockh)
1839 {
1840         struct lov_request_set *set;
1841         struct obd_info oinfo;
1842         struct lov_request *req;
1843         cfs_list_t *pos;
1844         struct lov_obd *lov;
1845         struct lustre_handle *lov_lockhp;
1846         int err = 0, rc = 0;
1847         ENTRY;
1848
1849         ASSERT_LSM_MAGIC(lsm);
1850
1851         if (!exp || !exp->exp_obd)
1852                 RETURN(-ENODEV);
1853
1854         LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1855         LASSERT(lockh);
1856         lov = &exp->exp_obd->u.lov;
1857         rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1858         if (rc)
1859                 RETURN(rc);
1860
1861         cfs_list_for_each (pos, &set->set_list) {
1862                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1863                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1864
1865                 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
1866                                 req->rq_oi.oi_md, mode, lov_lockhp);
1867                 rc = lov_update_common_set(set, req, rc);
1868                 if (rc) {
1869                         CERROR("error: cancel objid "LPX64" subobj "
1870                                LPX64" on OST idx %d: rc = %d\n",
1871                                lsm->lsm_object_id,
1872                                req->rq_oi.oi_md->lsm_object_id,
1873                                req->rq_idx, rc);
1874                         err = rc;
1875                 }
1876
1877         }
1878         lov_fini_cancel_set(set);
1879         RETURN(err);
1880 }
1881
1882 static int lov_cancel_unused(struct obd_export *exp,
1883                              struct lov_stripe_md *lsm,
1884                              ldlm_cancel_flags_t flags, void *opaque)
1885 {
1886         struct lov_obd *lov;
1887         int rc = 0, i;
1888         ENTRY;
1889
1890         if (!exp || !exp->exp_obd)
1891                 RETURN(-ENODEV);
1892
1893         lov = &exp->exp_obd->u.lov;
1894         if (lsm == NULL) {
1895                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1896                         int err;
1897                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1898                                 continue;
1899
1900                         err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
1901                                                 flags, opaque);
1902                         if (!rc)
1903                                 rc = err;
1904                 }
1905                 RETURN(rc);
1906         }
1907
1908         ASSERT_LSM_MAGIC(lsm);
1909
1910         LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
1911         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1912                 struct lov_stripe_md submd;
1913                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1914                 int err;
1915
1916                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1917                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1918                         continue;
1919                 }
1920
1921                 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
1922                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1923
1924                 submd.lsm_object_id = loi->loi_id;
1925                 submd.lsm_object_seq = loi->loi_seq;
1926                 submd.lsm_stripe_count = 0;
1927                 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1928                                         &submd, flags, opaque);
1929                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1930                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
1931                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1932                                loi->loi_id, loi->loi_ost_idx, err);
1933                         if (!rc)
1934                                 rc = err;
1935                 }
1936         }
1937         RETURN(rc);
1938 }
1939
1940 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1941 {
1942         struct lov_request_set *lovset = (struct lov_request_set *)data;
1943         int err;
1944         ENTRY;
1945
1946         if (rc)
1947                 cfs_atomic_set(&lovset->set_completes, 0);
1948
1949         err = lov_fini_statfs_set(lovset);
1950         RETURN(rc ? rc : err);
1951 }
1952
1953 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1954                             __u64 max_age, struct ptlrpc_request_set *rqset)
1955 {
1956         struct obd_device      *obd = class_exp2obd(exp);
1957         struct lov_request_set *set;
1958         struct lov_request *req;
1959         cfs_list_t *pos;
1960         struct lov_obd *lov;
1961         int rc = 0;
1962         ENTRY;
1963
1964         LASSERT(oinfo != NULL);
1965         LASSERT(oinfo->oi_osfs != NULL);
1966
1967         lov = &obd->u.lov;
1968         rc = lov_prep_statfs_set(obd, oinfo, &set);
1969         if (rc)
1970                 RETURN(rc);
1971
1972         cfs_list_for_each (pos, &set->set_list) {
1973                 req = cfs_list_entry(pos, struct lov_request, rq_link);
1974                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1975                                       &req->rq_oi, max_age, rqset);
1976                 if (rc)
1977                         break;
1978         }
1979
1980         if (rc || cfs_list_empty(&rqset->set_requests)) {
1981                 int err;
1982                 if (rc)
1983                         cfs_atomic_set(&set->set_completes, 0);
1984                 err = lov_fini_statfs_set(set);
1985                 RETURN(rc ? rc : err);
1986         }
1987
1988         LASSERT(rqset->set_interpret == NULL);
1989         rqset->set_interpret = lov_statfs_interpret;
1990         rqset->set_arg = (void *)set;
1991         RETURN(0);
1992 }
1993
1994 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1995                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1996 {
1997         struct ptlrpc_request_set *set = NULL;
1998         struct obd_info oinfo = { { { 0 } } };
1999         int rc = 0;
2000         ENTRY;
2001
2002
2003         /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
2004          * statfs requests */
2005         set = ptlrpc_prep_set();
2006         if (set == NULL)
2007                 RETURN(-ENOMEM);
2008
2009         oinfo.oi_osfs = osfs;
2010         oinfo.oi_flags = flags;
2011         rc = lov_statfs_async(exp, &oinfo, max_age, set);
2012         if (rc == 0)
2013                 rc = ptlrpc_set_wait(set);
2014         ptlrpc_set_destroy(set);
2015
2016         RETURN(rc);
2017 }
2018
2019 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2020                          void *karg, void *uarg)
2021 {
2022         struct obd_device *obddev = class_exp2obd(exp);
2023         struct lov_obd *lov = &obddev->u.lov;
2024         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
2025         struct obd_uuid *uuidp;
2026         ENTRY;
2027
2028         switch (cmd) {
2029         case IOC_OBD_STATFS: {
2030                 struct obd_ioctl_data *data = karg;
2031                 struct obd_device *osc_obd;
2032                 struct obd_statfs stat_buf = {0};
2033                 __u32 index;
2034
2035                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2036                 if ((index >= count))
2037                         RETURN(-ENODEV);
2038
2039                 if (!lov->lov_tgts[index])
2040                         /* Try again with the next index */
2041                         RETURN(-EAGAIN);
2042                 if (!lov->lov_tgts[index]->ltd_active)
2043                         RETURN(-ENODATA);
2044
2045                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2046                 if (!osc_obd)
2047                         RETURN(-EINVAL);
2048
2049                 /* copy UUID */
2050                 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
2051                                      min((int) data->ioc_plen2,
2052                                          (int) sizeof(struct obd_uuid))))
2053                         RETURN(-EFAULT);
2054
2055                 /* got statfs data */
2056                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
2057                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2058                                 0);
2059                 if (rc)
2060                         RETURN(rc);
2061                 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
2062                                      min((int) data->ioc_plen1,
2063                                          (int) sizeof(stat_buf))))
2064                         RETURN(-EFAULT);
2065                 break;
2066         }
2067         case OBD_IOC_LOV_GET_CONFIG: {
2068                 struct obd_ioctl_data *data;
2069                 struct lov_desc *desc;
2070                 char *buf = NULL;
2071                 __u32 *genp;
2072
2073                 len = 0;
2074                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2075                         RETURN(-EINVAL);
2076
2077                 data = (struct obd_ioctl_data *)buf;
2078
2079                 if (sizeof(*desc) > data->ioc_inllen1) {
2080                         obd_ioctl_freedata(buf, len);
2081                         RETURN(-EINVAL);
2082                 }
2083
2084                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2085                         obd_ioctl_freedata(buf, len);
2086                         RETURN(-EINVAL);
2087                 }
2088
2089                 if (sizeof(__u32) * count > data->ioc_inllen3) {
2090                         obd_ioctl_freedata(buf, len);
2091                         RETURN(-EINVAL);
2092                 }
2093
2094                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2095                 memcpy(desc, &(lov->desc), sizeof(*desc));
2096
2097                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2098                 genp = (__u32 *)data->ioc_inlbuf3;
2099                 /* the uuid will be empty for deleted OSTs */
2100                 for (i = 0; i < count; i++, uuidp++, genp++) {
2101                         if (!lov->lov_tgts[i])
2102                                 continue;
2103                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
2104                         *genp = lov->lov_tgts[i]->ltd_gen;
2105                 }
2106
2107                 if (cfs_copy_to_user((void *)uarg, buf, len))
2108                         rc = -EFAULT;
2109                 obd_ioctl_freedata(buf, len);
2110                 break;
2111         }
2112         case LL_IOC_LOV_SETSTRIPE:
2113                 rc = lov_setstripe(exp, len, karg, uarg);
2114                 break;
2115         case LL_IOC_LOV_GETSTRIPE:
2116                 rc = lov_getstripe(exp, karg, uarg);
2117                 break;
2118         case LL_IOC_LOV_SETEA:
2119                 rc = lov_setea(exp, karg, uarg);
2120                 break;
2121         case OBD_IOC_QUOTACTL: {
2122                 struct if_quotactl *qctl = karg;
2123                 struct lov_tgt_desc *tgt = NULL;
2124                 struct obd_quotactl *oqctl;
2125
2126                 if (qctl->qc_valid == QC_OSTIDX) {
2127                         if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
2128                                 RETURN(-EINVAL);
2129
2130                         tgt = lov->lov_tgts[qctl->qc_idx];
2131                         if (!tgt || !tgt->ltd_exp)
2132                                 RETURN(-EINVAL);
2133                 } else if (qctl->qc_valid == QC_UUID) {
2134                         for (i = 0; i < count; i++) {
2135                                 tgt = lov->lov_tgts[i];
2136                                 if (!tgt ||
2137                                     !obd_uuid_equals(&tgt->ltd_uuid,
2138                                                      &qctl->obd_uuid))
2139                                         continue;
2140
2141                                 if (tgt->ltd_exp == NULL)
2142                                         RETURN(-EINVAL);
2143
2144                                 break;
2145                         }
2146                 } else {
2147                         RETURN(-EINVAL);
2148                 }
2149
2150                 if (i >= count)
2151                         RETURN(-EAGAIN);
2152
2153                 LASSERT(tgt && tgt->ltd_exp);
2154                 OBD_ALLOC_PTR(oqctl);
2155                 if (!oqctl)
2156                         RETURN(-ENOMEM);
2157
2158                 QCTL_COPY(oqctl, qctl);
2159                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2160                 if (rc == 0) {
2161                         QCTL_COPY(qctl, oqctl);
2162                         qctl->qc_valid = QC_OSTIDX;
2163                         qctl->obd_uuid = tgt->ltd_uuid;
2164                 }
2165                 OBD_FREE_PTR(oqctl);
2166                 break;
2167         }
2168         default: {
2169                 int set = 0;
2170
2171                 if (count == 0)
2172                         RETURN(-ENOTTY);
2173
2174                 for (i = 0; i < count; i++) {
2175                         int err;
2176                         struct obd_device *osc_obd;
2177
2178                         /* OST was disconnected */
2179                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2180                                 continue;
2181
2182                         /* ll_umount_begin() sets force flag but for lov, not
2183                          * osc. Let's pass it through */
2184                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
2185                         osc_obd->obd_force = obddev->obd_force;
2186                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2187                                             len, karg, uarg);
2188                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2189                                 RETURN(err);
2190                         } else if (err) {
2191                                 if (lov->lov_tgts[i]->ltd_active) {
2192                                         CDEBUG(err == -ENOTTY ?
2193                                                D_IOCTL : D_WARNING,
2194                                                "iocontrol OSC %s on OST "
2195                                                "idx %d cmd %x: err = %d\n",
2196                                                lov_uuid2str(lov, i),
2197                                                i, cmd, err);
2198                                         if (!rc)
2199                                                 rc = err;
2200                                 }
2201                         } else {
2202                                 set = 1;
2203                         }
2204                 }
2205                 if (!set && !rc)
2206                         rc = -EIO;
2207         }
2208         }
2209
2210         RETURN(rc);
2211 }
2212
2213 #define FIEMAP_BUFFER_SIZE 4096
2214
2215 /**
2216  * Non-zero fe_logical indicates that this is a continuation FIEMAP
2217  * call. The local end offset and the device are sent in the first
2218  * fm_extent. This function calculates the stripe number from the index.
2219  * This function returns a stripe_no on which mapping is to be restarted.
2220  *
2221  * This function returns fm_end_offset which is the in-OST offset at which
2222  * mapping should be restarted. If fm_end_offset=0 is returned then caller
2223  * will re-calculate proper offset in next stripe.
2224  * Note that the first extent is passed to lov_get_info via the value field.
2225  *
2226  * \param fiemap fiemap request header
2227  * \param lsm striping information for the file
2228  * \param fm_start logical start of mapping
2229  * \param fm_end logical end of mapping
2230  * \param start_stripe starting stripe will be returned in this
2231  */
2232 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
2233                                    struct lov_stripe_md *lsm, obd_size fm_start,
2234                                    obd_size fm_end, int *start_stripe)
2235 {
2236         obd_size local_end = fiemap->fm_extents[0].fe_logical;
2237         obd_off lun_start, lun_end;
2238         obd_size fm_end_offset;
2239         int stripe_no = -1, i;
2240
2241         if (fiemap->fm_extent_count == 0 ||
2242             fiemap->fm_extents[0].fe_logical == 0)
2243                 return 0;
2244
2245         /* Find out stripe_no from ost_index saved in the fe_device */
2246         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2247                 if (lsm->lsm_oinfo[i]->loi_ost_idx ==
2248                                         fiemap->fm_extents[0].fe_device) {
2249                         stripe_no = i;
2250                         break;
2251                 }
2252         }
2253
2254         /* If we have finished mapping on previous device, shift logical
2255          * offset to start of next device */
2256         if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
2257                                    &lun_start, &lun_end)) != 0 &&
2258                                    local_end < lun_end) {
2259                 fm_end_offset = local_end;
2260                 *start_stripe = stripe_no;
2261         } else {
2262                 /* This is a special value to indicate that caller should
2263                  * calculate offset in next stripe. */
2264                 fm_end_offset = 0;
2265                 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
2266         }
2267
2268         return fm_end_offset;
2269 }
2270
2271 /**
2272  * We calculate on which OST the mapping will end. If the length of mapping
2273  * is greater than (stripe_size * stripe_count) then the last_stripe will
2274  * will be one just before start_stripe. Else we check if the mapping
2275  * intersects each OST and find last_stripe.
2276  * This function returns the last_stripe and also sets the stripe_count
2277  * over which the mapping is spread
2278  *
2279  * \param lsm striping information for the file
2280  * \param fm_start logical start of mapping
2281  * \param fm_end logical end of mapping
2282  * \param start_stripe starting stripe of the mapping
2283  * \param stripe_count the number of stripes across which to map is returned
2284  *
2285  * \retval last_stripe return the last stripe of the mapping
2286  */
2287 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
2288                             obd_size fm_end, int start_stripe,
2289                             int *stripe_count)
2290 {
2291         int last_stripe;
2292         obd_off obd_start, obd_end;
2293         int i, j;
2294
2295         if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
2296                 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
2297                                                               start_stripe - 1);
2298                 *stripe_count = lsm->lsm_stripe_count;
2299         } else {
2300                 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
2301                      i = (i + 1) % lsm->lsm_stripe_count, j++) {
2302                         if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
2303                                                    &obd_start, &obd_end)) == 0)
2304                                 break;
2305                 }
2306                 *stripe_count = j;
2307                 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
2308         }
2309
2310         return last_stripe;
2311 }
2312
2313 /**
2314  * Set fe_device and copy extents from local buffer into main return buffer.
2315  *
2316  * \param fiemap fiemap request header
2317  * \param lcl_fm_ext array of local fiemap extents to be copied
2318  * \param ost_index OST index to be written into the fm_device field for each
2319                     extent
2320  * \param ext_count number of extents to be copied
2321  * \param current_extent where to start copying in main extent array
2322  */
2323 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
2324                                   struct ll_fiemap_extent *lcl_fm_ext,
2325                                   int ost_index, unsigned int ext_count,
2326                                   int current_extent)
2327 {
2328         char *to;
2329         int ext;
2330
2331         for (ext = 0; ext < ext_count; ext++) {
2332                 lcl_fm_ext[ext].fe_device = ost_index;
2333                 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
2334         }
2335
2336         /* Copy fm_extent's from fm_local to return buffer */
2337         to = (char *)fiemap + fiemap_count_to_size(current_extent);
2338         memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
2339 }
2340
2341 /**
2342  * Break down the FIEMAP request and send appropriate calls to individual OSTs.
2343  * This also handles the restarting of FIEMAP calls in case mapping overflows
2344  * the available number of extents in single call.
2345  */
2346 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
2347                       __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2348 {
2349         struct ll_fiemap_info_key *fm_key = key;
2350         struct ll_user_fiemap *fiemap = val;
2351         struct ll_user_fiemap *fm_local = NULL;
2352         struct ll_fiemap_extent *lcl_fm_ext;
2353         int count_local;
2354         unsigned int get_num_extents = 0;
2355         int ost_index = 0, actual_start_stripe, start_stripe;
2356         obd_size fm_start, fm_end, fm_length, fm_end_offset = 0;
2357         obd_size curr_loc;
2358         int current_extent = 0, rc = 0, i;
2359         int ost_eof = 0; /* EOF for object */
2360         int ost_done = 0; /* done with required mapping for this OST? */
2361         int last_stripe;
2362         int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
2363         unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
2364
2365         if (lsm == NULL)
2366                 GOTO(out, rc = 0);
2367
2368         if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
2369                 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
2370
2371         OBD_ALLOC_LARGE(fm_local, buffer_size);
2372         if (fm_local == NULL)
2373                 GOTO(out, rc = -ENOMEM);
2374         lcl_fm_ext = &fm_local->fm_extents[0];
2375
2376         count_local = fiemap_size_to_count(buffer_size);
2377
2378         memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
2379         fm_start = fiemap->fm_start;
2380         fm_length = fiemap->fm_length;
2381         /* Calculate start stripe, last stripe and length of mapping */
2382         actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
2383         fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
2384                                                 fm_start + fm_length - 1);
2385         /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
2386         if (fm_end > fm_key->oa.o_size)
2387                 fm_end = fm_key->oa.o_size;
2388
2389         last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
2390                                             actual_start_stripe, &stripe_count);
2391
2392         fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start, fm_end,
2393                                                   &start_stripe);
2394
2395         if (fiemap->fm_extent_count == 0) {
2396                 get_num_extents = 1;
2397                 count_local = 0;
2398         }
2399
2400         /* Check each stripe */
2401         for (cur_stripe = start_stripe, i = 0; i < stripe_count;
2402              i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
2403                 obd_size req_fm_len; /* Stores length of required mapping */
2404                 obd_size len_mapped_single_call;
2405                 obd_off lun_start, lun_end, obd_object_end;
2406                 unsigned int ext_count;
2407
2408                 cur_stripe_wrap = cur_stripe;
2409
2410                 /* Find out range of mapping on this stripe */
2411                 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
2412                                            &lun_start, &obd_object_end)) == 0)
2413                         continue;
2414
2415                 /* If this is a continuation FIEMAP call and we are on
2416                  * starting stripe then lun_start needs to be set to
2417                  * fm_end_offset */
2418                 if (fm_end_offset != 0 && cur_stripe == start_stripe)
2419                         lun_start = fm_end_offset;
2420
2421                 if (fm_length != ~0ULL) {
2422                         /* Handle fm_start + fm_length overflow */
2423                         if (fm_start + fm_length < fm_start)
2424                                 fm_length = ~0ULL - fm_start;
2425                         lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
2426                                                      cur_stripe);
2427                 } else {
2428                         lun_end = ~0ULL;
2429                 }
2430
2431                 if (lun_start == lun_end)
2432                         continue;
2433
2434                 req_fm_len = obd_object_end - lun_start;
2435                 fm_local->fm_length = 0;
2436                 len_mapped_single_call = 0;
2437
2438                 /* If the output buffer is very large and the objects have many
2439                  * extents we may need to loop on a single OST repeatedly */
2440                 ost_eof = 0;
2441                 ost_done = 0;
2442                 do {
2443                         if (get_num_extents == 0) {
2444                                 /* Don't get too many extents. */
2445                                 if (current_extent + count_local >
2446                                     fiemap->fm_extent_count)
2447                                         count_local = fiemap->fm_extent_count -
2448                                                                  current_extent;
2449                         }
2450
2451                         lun_start += len_mapped_single_call;
2452                         fm_local->fm_length = req_fm_len - len_mapped_single_call;
2453                         req_fm_len = fm_local->fm_length;
2454                         fm_local->fm_extent_count = count_local;
2455                         fm_local->fm_mapped_extents = 0;
2456                         fm_local->fm_flags = fiemap->fm_flags;
2457
2458                         fm_key->oa.o_id = lsm->lsm_oinfo[cur_stripe]->loi_id;
2459                         fm_key->oa.o_seq = lsm->lsm_oinfo[cur_stripe]->loi_seq;
2460                         ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
2461
2462                         if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
2463                                 GOTO(out, rc = -EINVAL);
2464
2465                         /* If OST is inactive, return extent with UNKNOWN flag */
2466                         if (!lov->lov_tgts[ost_index]->ltd_active) {
2467                                 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
2468                                 fm_local->fm_mapped_extents = 1;
2469
2470                                 lcl_fm_ext[0].fe_logical = lun_start;
2471                                 lcl_fm_ext[0].fe_length = obd_object_end -
2472                                                                       lun_start;
2473                                 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
2474
2475                                 goto inactive_tgt;
2476                         }
2477
2478                         fm_local->fm_start = lun_start;
2479                         fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
2480                         memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
2481                         *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
2482                         rc = obd_get_info(NULL,
2483                                           lov->lov_tgts[ost_index]->ltd_exp,
2484                                           keylen, key, vallen, fm_local, lsm);
2485                         if (rc != 0)
2486                                 GOTO(out, rc);
2487
2488 inactive_tgt:
2489                         ext_count = fm_local->fm_mapped_extents;
2490                         if (ext_count == 0) {
2491                                 ost_done = 1;
2492                                 /* If last stripe has hole at the end,
2493                                  * then we need to return */
2494                                 if (cur_stripe_wrap == last_stripe) {
2495                                         fiemap->fm_mapped_extents = 0;
2496                                         goto finish;
2497                                 }
2498                                 break;
2499                         }
2500
2501                         /* If we just need num of extents then go to next device */
2502                         if (get_num_extents) {
2503                                 current_extent += ext_count;
2504                                 break;
2505                         }
2506
2507                         len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
2508                                   lun_start + lcl_fm_ext[ext_count - 1].fe_length;
2509
2510                         /* Have we finished mapping on this device? */
2511                         if (req_fm_len <= len_mapped_single_call)
2512                                 ost_done = 1;
2513
2514                         /* Clear the EXTENT_LAST flag which can be present on
2515                          * last extent */
2516                         if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
2517                                 lcl_fm_ext[ext_count - 1].fe_flags &=
2518                                                             ~FIEMAP_EXTENT_LAST;
2519
2520                         curr_loc = lov_stripe_size(lsm,
2521                                            lcl_fm_ext[ext_count - 1].fe_logical+
2522                                            lcl_fm_ext[ext_count - 1].fe_length,
2523                                            cur_stripe);
2524                         if (curr_loc >= fm_key->oa.o_size)
2525                                 ost_eof = 1;
2526
2527                         fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
2528                                                      ost_index, ext_count,
2529                                                      current_extent);
2530
2531                         current_extent += ext_count;
2532
2533                         /* Ran out of available extents? */
2534                         if (current_extent >= fiemap->fm_extent_count)
2535                                 goto finish;
2536                 } while (ost_done == 0 && ost_eof == 0);
2537
2538                 if (cur_stripe_wrap == last_stripe)
2539                         goto finish;
2540         }
2541
2542 finish:
2543         /* Indicate that we are returning device offsets unless file just has
2544          * single stripe */
2545         if (lsm->lsm_stripe_count > 1)
2546                 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2547
2548         if (get_num_extents)
2549                 goto skip_last_device_calc;
2550
2551         /* Check if we have reached the last stripe and whether mapping for that
2552          * stripe is done. */
2553         if (cur_stripe_wrap == last_stripe) {
2554                 if (ost_done || ost_eof)
2555                         fiemap->fm_extents[current_extent - 1].fe_flags |=
2556                                                              FIEMAP_EXTENT_LAST;
2557         }
2558
2559 skip_last_device_calc:
2560         fiemap->fm_mapped_extents = current_extent;
2561
2562 out:
2563         OBD_FREE_LARGE(fm_local, buffer_size);
2564         return rc;
2565 }
2566
2567 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
2568                         __u32 keylen, void *key, __u32 *vallen, void *val,
2569                         struct lov_stripe_md *lsm)
2570 {
2571         struct obd_device *obddev = class_exp2obd(exp);
2572         struct lov_obd *lov = &obddev->u.lov;
2573         int i, rc;
2574         ENTRY;
2575
2576         if (!vallen || !val)
2577                 RETURN(-EFAULT);
2578
2579         obd_getref(obddev);
2580
2581         if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
2582                 struct {
2583                         char name[16];
2584                         struct ldlm_lock *lock;
2585                 } *data = key;
2586                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2587                 struct lov_oinfo *loi;
2588                 __u32 *stripe = val;
2589
2590                 if (*vallen < sizeof(*stripe))
2591                         GOTO(out, rc = -EFAULT);
2592                 *vallen = sizeof(*stripe);
2593
2594                 /* XXX This is another one of those bits that will need to
2595                  * change if we ever actually support nested LOVs.  It uses
2596                  * the lock's export to find out which stripe it is. */
2597                 /* XXX - it's assumed all the locks for deleted OSTs have
2598                  * been cancelled. Also, the export for deleted OSTs will
2599                  * be NULL and won't match the lock's export. */
2600                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2601                         loi = lsm->lsm_oinfo[i];
2602                         if (!lov->lov_tgts[loi->loi_ost_idx])
2603                                 continue;
2604                         if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2605                             data->lock->l_conn_export &&
2606                             osc_res_name_eq(loi->loi_id, loi->loi_seq, res_id)) {
2607                                 *stripe = i;
2608                                 GOTO(out, rc = 0);
2609                         }
2610                 }
2611                 LDLM_ERROR(data->lock, "lock on inode without such object");
2612                 dump_lsm(D_ERROR, lsm);
2613                 GOTO(out, rc = -ENXIO);
2614         } else if (KEY_IS(KEY_LAST_ID)) {
2615                 struct obd_id_info *info = val;
2616                 __u32 size = sizeof(obd_id);
2617                 struct lov_tgt_desc *tgt;
2618
2619                 LASSERT(*vallen == sizeof(struct obd_id_info));
2620                 tgt = lov->lov_tgts[info->idx];
2621
2622                 if (!tgt || !tgt->ltd_active)
2623                         GOTO(out, rc = -ESRCH);
2624
2625                 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2626                                   &size, info->data, NULL);
2627                 GOTO(out, rc = 0);
2628         } else if (KEY_IS(KEY_LOVDESC)) {
2629                 struct lov_desc *desc_ret = val;
2630                 *desc_ret = lov->desc;
2631
2632                 GOTO(out, rc = 0);
2633         } else if (KEY_IS(KEY_FIEMAP)) {
2634                 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
2635                 GOTO(out, rc);
2636         } else if (KEY_IS(KEY_CONNECT_FLAG)) {
2637                 struct lov_tgt_desc *tgt;
2638                 __u64 ost_idx = *((__u64*)val);
2639
2640                 LASSERT(*vallen == sizeof(__u64));
2641                 LASSERT(ost_idx < lov->desc.ld_tgt_count);
2642                 tgt = lov->lov_tgts[ost_idx];
2643
2644                 if (!tgt || !tgt->ltd_exp)
2645                         GOTO(out, rc = -ESRCH);
2646
2647                 *((__u64*)val) = tgt->ltd_exp->exp_connect_flags;
2648                 GOTO(out, rc = 0);
2649         } else if (KEY_IS(KEY_TGT_COUNT)) {
2650                 *((int *)val) = lov->desc.ld_tgt_count;
2651                 GOTO(out, rc = 0);
2652         }
2653
2654         rc = -EINVAL;
2655
2656 out:
2657         obd_putref(obddev);
2658         RETURN(rc);
2659 }
2660
2661 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
2662                               obd_count keylen, void *key, obd_count vallen,
2663                               void *val, struct ptlrpc_request_set *set)
2664 {
2665         struct obd_device *obddev = class_exp2obd(exp);
2666         struct lov_obd *lov = &obddev->u.lov;
2667         obd_count count;
2668         int i, rc = 0, err;
2669         struct lov_tgt_desc *tgt;
2670         unsigned incr, check_uuid,
2671                  do_inactive, no_set;
2672         unsigned next_id = 0,  mds_con = 0, capa = 0;
2673         ENTRY;
2674
2675         incr = check_uuid = do_inactive = no_set = 0;
2676         if (set == NULL) {
2677                 no_set = 1;
2678                 set = ptlrpc_prep_set();
2679                 if (!set)
2680                         RETURN(-ENOMEM);
2681         }
2682
2683         obd_getref(obddev);
2684         count = lov->desc.ld_tgt_count;
2685
2686         if (KEY_IS(KEY_NEXT_ID)) {
2687                 count = vallen / sizeof(struct obd_id_info);
2688                 vallen = sizeof(obd_id);
2689                 incr = sizeof(struct obd_id_info);
2690                 do_inactive = 1;
2691                 next_id = 1;
2692         } else if (KEY_IS(KEY_CHECKSUM)) {
2693                 do_inactive = 1;
2694         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2695                 /* use defaults:  do_inactive = incr = 0; */
2696         } else if (KEY_IS(KEY_MDS_CONN)) {
2697                 mds_con = 1;
2698         } else if (KEY_IS(KEY_CAPA_KEY)) {
2699                 capa = 1;
2700         }
2701
2702         for (i = 0; i < count; i++, val = (char *)val + incr) {
2703                 if (next_id) {
2704                         tgt = lov->lov_tgts[((struct obd_id_info*)val)->idx];
2705                 } else {
2706                         tgt = lov->lov_tgts[i];
2707                 }
2708                 /* OST was disconnected */
2709                 if (!tgt || !tgt->ltd_exp)
2710                         continue;
2711
2712                 /* OST is inactive and we don't want inactive OSCs */
2713                 if (!tgt->ltd_active && !do_inactive)
2714                         continue;
2715
2716                 if (mds_con) {
2717                         struct mds_group_info *mgi;
2718
2719                         LASSERT(vallen == sizeof(*mgi));
2720                         mgi = (struct mds_group_info *)val;
2721
2722                         /* Only want a specific OSC */
2723                         if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2724                                                 &tgt->ltd_uuid))
2725                                 continue;
2726
2727                         err = obd_set_info_async(env, tgt->ltd_exp,
2728                                          keylen, key, sizeof(int),
2729                                          &mgi->group, set);
2730                 } else if (next_id) {
2731                         err = obd_set_info_async(env, tgt->ltd_exp,
2732                                          keylen, key, vallen,
2733                                          ((struct obd_id_info*)val)->data, set);
2734                 } else if (capa) {
2735                         struct mds_capa_info *info = (struct mds_capa_info*)val;
2736
2737                         LASSERT(vallen == sizeof(*info));
2738
2739                          /* Only want a specific OSC */
2740                         if (info->uuid &&
2741                             !obd_uuid_equals(info->uuid, &tgt->ltd_uuid))
2742                                 continue;
2743
2744                         err = obd_set_info_async(env, tgt->ltd_exp, keylen,
2745                                                  key, sizeof(*info->capa),
2746                                                  info->capa, set);
2747                 } else {
2748                         /* Only want a specific OSC */
2749                         if (check_uuid &&
2750                             !obd_uuid_equals(val, &tgt->ltd_uuid))
2751                                 continue;
2752
2753                         err = obd_set_info_async(env, tgt->ltd_exp,
2754                                          keylen, key, vallen, val, set);
2755                 }
2756
2757                 if (!rc)
2758                         rc = err;
2759         }
2760
2761         obd_putref(obddev);
2762         if (no_set) {
2763                 err = ptlrpc_set_wait(set);
2764                 if (!rc)
2765                         rc = err;
2766                 ptlrpc_set_destroy(set);
2767         }
2768         RETURN(rc);
2769 }
2770
2771 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm)
2772 {
2773         int i, rc = 0;
2774         ENTRY;
2775
2776         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2777                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2778                 if (loi->loi_ar.ar_rc && !rc)
2779                         rc = loi->loi_ar.ar_rc;
2780                 loi->loi_ar.ar_rc = 0;
2781         }
2782         RETURN(rc);
2783 }
2784 EXPORT_SYMBOL(lov_test_and_clear_async_rc);
2785
2786
2787 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2788                            int cmd, __u64 *offset)
2789 {
2790         __u32 ssize = lsm->lsm_stripe_size;
2791         __u64 start;
2792
2793         start = *offset;
2794         lov_do_div64(start, ssize);
2795         start = start * ssize;
2796
2797         CDEBUG(D_DLMTRACE, "offset "LPU64", stripe %u, start "LPU64
2798                            ", end "LPU64"\n", *offset, ssize, start,
2799                            start + ssize - 1);
2800         if (cmd == OBD_CALC_STRIPE_END) {
2801                 *offset = start + ssize - 1;
2802         } else if (cmd == OBD_CALC_STRIPE_START) {
2803                 *offset = start;
2804         } else {
2805                 LBUG();
2806         }
2807
2808         RETURN(0);
2809 }
2810
2811 void lov_stripe_lock(struct lov_stripe_md *md)
2812 {
2813         LASSERT(md->lsm_lock_owner != cfs_curproc_pid());
2814         cfs_spin_lock(&md->lsm_lock);
2815         LASSERT(md->lsm_lock_owner == 0);
2816         md->lsm_lock_owner = cfs_curproc_pid();
2817 }
2818 EXPORT_SYMBOL(lov_stripe_lock);
2819
2820 void lov_stripe_unlock(struct lov_stripe_md *md)
2821 {
2822         LASSERT(md->lsm_lock_owner == cfs_curproc_pid());
2823         md->lsm_lock_owner = 0;
2824         cfs_spin_unlock(&md->lsm_lock);
2825 }
2826 EXPORT_SYMBOL(lov_stripe_unlock);
2827
2828 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
2829                         struct obd_quotactl *oqctl)
2830 {
2831         struct lov_obd      *lov = &obd->u.lov;
2832         struct lov_tgt_desc *tgt;
2833         __u64                curspace = 0;
2834         __u64                bhardlimit = 0;
2835         int                  i, rc = 0;
2836         ENTRY;
2837
2838         if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
2839             oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
2840             oqctl->qc_cmd != Q_GETOQUOTA &&
2841             oqctl->qc_cmd != Q_INITQUOTA &&
2842             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
2843             oqctl->qc_cmd != Q_FINVALIDATE) {
2844                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
2845                 RETURN(-EFAULT);
2846         }
2847
2848         /* for lov tgt */
2849         obd_getref(obd);
2850         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2851                 int err;
2852
2853                 tgt = lov->lov_tgts[i];
2854
2855                 if (!tgt)
2856                         continue;
2857
2858                 if (!tgt->ltd_active || tgt->ltd_reap) {
2859                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
2860                             lov->lov_tgts[i]->ltd_activate) {
2861                                 rc = -EREMOTEIO;
2862                                 CERROR("ost %d is inactive\n", i);
2863                         } else {
2864                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
2865                         }
2866                         continue;
2867                 }
2868
2869                 err = obd_quotactl(tgt->ltd_exp, oqctl);
2870                 if (err) {
2871                         if (tgt->ltd_active && !rc)
2872                                 rc = err;
2873                         continue;
2874                 }
2875
2876                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2877                         curspace += oqctl->qc_dqblk.dqb_curspace;
2878                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2879                 }
2880         }
2881         obd_putref(obd);
2882
2883         if (oqctl->qc_cmd == Q_GETOQUOTA) {
2884                 oqctl->qc_dqblk.dqb_curspace = curspace;
2885                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2886         }
2887         RETURN(rc);
2888 }
2889
2890 static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2891                           struct obd_quotactl *oqctl)
2892 {
2893         struct lov_obd *lov = &obd->u.lov;
2894         int             i, rc = 0;
2895         ENTRY;
2896
2897         obd_getref(obd);
2898
2899         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2900                 if (!lov->lov_tgts[i])
2901                         continue;
2902
2903                 /* Skip quota check on the administratively disabled OSTs. */
2904                 if (!lov->lov_tgts[i]->ltd_activate) {
2905                         CWARN("lov idx %d was administratively disabled, "
2906                               "skip quotacheck on it.\n", i);
2907                         continue;
2908                 }
2909
2910                 if (!lov->lov_tgts[i]->ltd_active) {
2911                         CERROR("lov idx %d inactive\n", i);
2912                         rc = -EIO;
2913                         goto out;
2914                 }
2915         }
2916
2917         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2918                 int err;
2919
2920                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2921                         continue;
2922
2923                 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2924                 if (err && !rc)
2925                         rc = err;
2926         }
2927
2928 out:
2929         obd_putref(obd);
2930
2931         RETURN(rc);
2932 }
2933
2934 int lov_quota_adjust_qunit(struct obd_export *exp,
2935                            struct quota_adjust_qunit *oqaq,
2936                            struct lustre_quota_ctxt *qctxt,
2937                            struct ptlrpc_request_set *rqset)
2938 {
2939         struct obd_device *obd = class_exp2obd(exp);
2940         struct lov_obd    *lov = &obd->u.lov;
2941         int                i, err, rc = 0;
2942         unsigned           no_set = 0;
2943         ENTRY;
2944
2945         if (!QAQ_IS_ADJBLK(oqaq)) {
2946                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
2947                 RETURN(-EFAULT);
2948         }
2949
2950         if (rqset == NULL) {
2951                 rqset = ptlrpc_prep_set();
2952                 if (!rqset)
2953                         RETURN(-ENOMEM);
2954                 no_set = 1;
2955         }
2956
2957         obd_getref(obd);
2958         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2959
2960                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
2961                         CDEBUG(D_HA, "ost %d is inactive\n", i);
2962                         continue;
2963                 }
2964
2965                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
2966                                              NULL, rqset);
2967                 if (err) {
2968                         if (lov->lov_tgts[i]->ltd_active && !rc)
2969                                 rc = err;
2970                         continue;
2971                 }
2972         }
2973
2974         err = ptlrpc_set_wait(rqset);
2975         if (!rc)
2976                 rc = err;
2977
2978         /* Destroy the set if none was provided by the caller */
2979         if (no_set)
2980                 ptlrpc_set_destroy(rqset);
2981
2982         obd_putref(obd);
2983         RETURN(rc);
2984 }
2985
2986 struct obd_ops lov_obd_ops = {
2987         .o_owner               = THIS_MODULE,
2988         .o_setup               = lov_setup,
2989         .o_precleanup          = lov_precleanup,
2990         .o_cleanup             = lov_cleanup,
2991         //.o_process_config      = lov_process_config,
2992         .o_connect             = lov_connect,
2993         .o_disconnect          = lov_disconnect,
2994         .o_statfs              = lov_statfs,
2995         .o_statfs_async        = lov_statfs_async,
2996         .o_packmd              = lov_packmd,
2997         .o_unpackmd            = lov_unpackmd,
2998         .o_create              = lov_create,
2999         .o_destroy             = lov_destroy,
3000         .o_getattr             = lov_getattr,
3001         .o_getattr_async       = lov_getattr_async,
3002         .o_setattr             = lov_setattr,
3003         .o_setattr_async       = lov_setattr_async,
3004         .o_brw                 = lov_brw,
3005         .o_merge_lvb           = lov_merge_lvb,
3006         .o_adjust_kms          = lov_adjust_kms,
3007         .o_punch               = lov_punch,
3008         .o_sync                = lov_sync,
3009         .o_enqueue             = lov_enqueue,
3010         .o_change_cbdata       = lov_change_cbdata,
3011         .o_find_cbdata         = lov_find_cbdata,
3012         .o_cancel              = lov_cancel,
3013         .o_cancel_unused       = lov_cancel_unused,
3014         .o_iocontrol           = lov_iocontrol,
3015         .o_get_info            = lov_get_info,
3016         .o_set_info_async      = lov_set_info_async,
3017         .o_extent_calc         = lov_extent_calc,
3018         .o_llog_init           = lov_llog_init,
3019         .o_llog_finish         = lov_llog_finish,
3020         .o_notify              = lov_notify,
3021         .o_pool_new            = lov_pool_new,
3022         .o_pool_rem            = lov_pool_remove,
3023         .o_pool_add            = lov_pool_add,
3024         .o_pool_del            = lov_pool_del,
3025         .o_getref              = lov_getref,
3026         .o_putref              = lov_putref,
3027         .o_quotactl            = lov_quotactl,
3028         .o_quotacheck          = lov_quotacheck,
3029         .o_quota_adjust_qunit  = lov_quota_adjust_qunit,
3030 };
3031
3032 static quota_interface_t *quota_interface;
3033 extern quota_interface_t lov_quota_interface;
3034
3035 cfs_mem_cache_t *lov_oinfo_slab;
3036
3037 extern struct lu_kmem_descr lov_caches[];
3038
3039 int __init lov_init(void)
3040 {
3041         struct lprocfs_static_vars lvars = { 0 };
3042         int rc, rc2;
3043         ENTRY;
3044
3045         /* print an address of _any_ initialized kernel symbol from this
3046          * module, to allow debugging with gdb that doesn't support data
3047          * symbols from modules.*/
3048         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
3049
3050         rc = lu_kmem_init(lov_caches);
3051         if (rc)
3052                 return rc;
3053
3054         lov_oinfo_slab = cfs_mem_cache_create("lov_oinfo",
3055                                               sizeof(struct lov_oinfo),
3056                                               0, CFS_SLAB_HWCACHE_ALIGN);
3057         if (lov_oinfo_slab == NULL) {
3058                 lu_kmem_fini(lov_caches);
3059                 return -ENOMEM;
3060         }
3061         lprocfs_lov_init_vars(&lvars);
3062
3063         cfs_request_module("lquota");
3064         quota_interface = PORTAL_SYMBOL_GET(lov_quota_interface);
3065         init_obd_quota_ops(quota_interface, &lov_obd_ops);
3066
3067         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
3068                                  LUSTRE_LOV_NAME, &lov_device_type);
3069
3070         if (rc) {
3071                 if (quota_interface)
3072                         PORTAL_SYMBOL_PUT(lov_quota_interface);
3073                 rc2 = cfs_mem_cache_destroy(lov_oinfo_slab);
3074                 LASSERT(rc2 == 0);
3075                 lu_kmem_fini(lov_caches);
3076         }
3077
3078         RETURN(rc);
3079 }
3080
3081 #ifdef __KERNEL__
3082 static void /*__exit*/ lov_exit(void)
3083 {
3084         int rc;
3085
3086         if (quota_interface)
3087                 PORTAL_SYMBOL_PUT(lov_quota_interface);
3088
3089         class_unregister_type(LUSTRE_LOV_NAME);
3090         rc = cfs_mem_cache_destroy(lov_oinfo_slab);
3091         LASSERT(rc == 0);
3092
3093         lu_kmem_fini(lov_caches);
3094 }
3095
3096 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3097 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
3098 MODULE_LICENSE("GPL");
3099
3100 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);
3101 #endif