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