Whamcloud - gitweb
LU-5275 obdclass: Remove lprocfs_vars argument from class_register_type function
[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, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * 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 #include <libcfs/libcfs.h>
46
47 #include <obd_support.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_lib.h>
50 #include <lustre_net.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre_dlm.h>
53 #include <lustre_mds.h>
54 #include <obd_class.h>
55 #include <lprocfs_status.h>
56 #include <lustre_param.h>
57 #include <cl_object.h>
58 #include <lclient.h>
59 #include <lustre/ll_fiemap.h>
60 #include <lustre_fid.h>
61
62 #include "lov_internal.h"
63
64 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
65    Any function that expects lov_tgts to remain stationary must take a ref. */
66 static void lov_getref(struct obd_device *obd)
67 {
68         struct lov_obd *lov = &obd->u.lov;
69
70         /* nobody gets through here until lov_putref is done */
71         mutex_lock(&lov->lov_lock);
72         atomic_inc(&lov->lov_refcount);
73         mutex_unlock(&lov->lov_lock);
74         return;
75 }
76
77 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
78
79 static void lov_putref(struct obd_device *obd)
80 {
81         struct lov_obd *lov = &obd->u.lov;
82
83         mutex_lock(&lov->lov_lock);
84         /* ok to dec to 0 more than once -- ltd_exp's will be null */
85         if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
86                 struct list_head kill = LIST_HEAD_INIT(kill);
87                 struct lov_tgt_desc *tgt, *n;
88                 int i;
89
90                 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
91                        lov->lov_death_row);
92                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
93                         tgt = lov->lov_tgts[i];
94
95                         if (!tgt || !tgt->ltd_reap)
96                                 continue;
97                         list_add(&tgt->ltd_kill, &kill);
98                         /* XXX - right now there is a dependency on ld_tgt_count
99                          * being the maximum tgt index for computing the
100                          * mds_max_easize. So we can't shrink it. */
101                         lov_ost_pool_remove(&lov->lov_packed, i);
102                         lov->lov_tgts[i] = NULL;
103                         lov->lov_death_row--;
104                 }
105                 mutex_unlock(&lov->lov_lock);
106
107                 list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
108                         list_del(&tgt->ltd_kill);
109                         /* Disconnect */
110                         __lov_del_obd(obd, tgt);
111                 }
112         } else {
113                 mutex_unlock(&lov->lov_lock);
114         }
115 }
116
117 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
118                               enum obd_notify_event ev);
119 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
120                       enum obd_notify_event ev, void *data);
121
122 int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
123                     struct obd_connect_data *data)
124 {
125         struct lov_obd *lov = &obd->u.lov;
126         struct obd_uuid *tgt_uuid;
127         struct obd_device *tgt_obd;
128         static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
129         struct obd_import *imp;
130         int rc;
131         ENTRY;
132
133         if (lov->lov_tgts[index] == NULL)
134                 RETURN(-EINVAL);
135
136         tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
137         tgt_obd = lov->lov_tgts[index]->ltd_obd;
138
139         if (!tgt_obd->obd_set_up) {
140                 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
141                 RETURN(-EINVAL);
142         }
143
144         /* override the sp_me from lov */
145         tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
146
147         if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
148                 data->ocd_index = index;
149
150         /*
151          * Divine LOV knows that OBDs under it are OSCs.
152          */
153         imp = tgt_obd->u.cli.cl_import;
154
155         if (activate) {
156                 tgt_obd->obd_no_recov = 0;
157                 /* FIXME this is probably supposed to be
158                    ptlrpc_set_import_active.  Horrible naming. */
159                 ptlrpc_activate_import(imp);
160         }
161
162         rc = obd_register_observer(tgt_obd, obd);
163         if (rc) {
164                 CERROR("Target %s register_observer error %d\n",
165                        obd_uuid2str(tgt_uuid), rc);
166                 RETURN(rc);
167         }
168
169
170         if (imp->imp_invalid) {
171                 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
172                        "disabled\n", obd_uuid2str(tgt_uuid));
173                 RETURN(0);
174         }
175
176         rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
177                          &lov_osc_uuid, data, NULL);
178         if (rc || !lov->lov_tgts[index]->ltd_exp) {
179                 CERROR("Target %s connect error %d\n",
180                        obd_uuid2str(tgt_uuid), rc);
181                 RETURN(-ENODEV);
182         }
183
184         lov->lov_tgts[index]->ltd_reap = 0;
185
186         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
187                obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
188
189         if (lov->targets_proc_entry != NULL) {
190                 struct proc_dir_entry *osc_symlink;
191                 struct obd_device *osc_obd;
192
193                 osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
194
195                 LASSERT(osc_obd != NULL);
196                 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
197                 LASSERT(osc_obd->obd_type->typ_name != NULL);
198
199                 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
200                                                   lov->targets_proc_entry,
201                                                   "../../../%s/%s",
202                                                   osc_obd->obd_type->typ_name,
203                                                   osc_obd->obd_name);
204                 if (osc_symlink == NULL) {
205                         CERROR("cannot register LOV target "
206                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
207                                obd->obd_type->typ_name, obd->obd_name,
208                                osc_obd->obd_name);
209                 }
210         }
211         RETURN(0);
212 }
213
214 static int lov_connect(const struct lu_env *env,
215                        struct obd_export **exp, struct obd_device *obd,
216                        struct obd_uuid *cluuid, struct obd_connect_data *data,
217                        void *localdata)
218 {
219         struct lov_obd *lov = &obd->u.lov;
220         struct lov_tgt_desc *tgt;
221         struct lustre_handle conn;
222         int i, rc;
223         ENTRY;
224
225         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
226
227         rc = class_connect(&conn, obd, cluuid);
228         if (rc)
229                 RETURN(rc);
230
231         *exp = class_conn2export(&conn);
232
233         /* Why should there ever be more than 1 connect? */
234         lov->lov_connects++;
235         LASSERT(lov->lov_connects == 1);
236
237         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
238         if (data)
239                 lov->lov_ocd = *data;
240
241         lov->targets_proc_entry = lprocfs_seq_register("target_obds",
242                                                        obd->obd_proc_entry,
243                                                        NULL, NULL);
244         if (IS_ERR(lov->targets_proc_entry)) {
245                 CERROR("%s: cannot register "
246                        "/proc/fs/lustre/%s/%s/target_obds\n",
247                        obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
248                 lov->targets_proc_entry = NULL;
249         }
250
251         obd_getref(obd);
252         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
253                 tgt = lov->lov_tgts[i];
254                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
255                         continue;
256                 /* Flags will be lowest common denominator */
257                 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
258                 if (rc) {
259                         CERROR("%s: lov connect tgt %d failed: %d\n",
260                                obd->obd_name, i, rc);
261                         continue;
262                 }
263                 /* connect to administrative disabled ost */
264                 if (!lov->lov_tgts[i]->ltd_exp)
265                         continue;
266
267                 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
268                                 OBD_NOTIFY_CONNECT, (void *)&i);
269                 if (rc) {
270                         CERROR("%s error sending notify %d\n",
271                                obd->obd_name, rc);
272                 }
273         }
274         obd_putref(obd);
275
276         RETURN(0);
277 }
278
279 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
280 {
281         struct lov_obd *lov = &obd->u.lov;
282         struct obd_device *osc_obd;
283         int rc;
284         ENTRY;
285
286         osc_obd = class_exp2obd(tgt->ltd_exp);
287         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
288                obd->obd_name, osc_obd->obd_name);
289
290         if (tgt->ltd_active) {
291                 tgt->ltd_active = 0;
292                 lov->desc.ld_active_tgt_count--;
293                 tgt->ltd_exp->exp_obd->obd_inactive = 1;
294         }
295
296         if (osc_obd) {
297                 /* Pass it on to our clients.
298                  * XXX This should be an argument to disconnect,
299                  * XXX not a back-door flag on the OBD.  Ah well.
300                  */
301                 osc_obd->obd_force = obd->obd_force;
302                 osc_obd->obd_fail = obd->obd_fail;
303                 osc_obd->obd_no_recov = obd->obd_no_recov;
304
305                 if (lov->targets_proc_entry != NULL)
306                         lprocfs_remove_proc_entry(osc_obd->obd_name,
307                                                   lov->targets_proc_entry);
308         }
309
310         obd_register_observer(osc_obd, NULL);
311
312         rc = obd_disconnect(tgt->ltd_exp);
313         if (rc) {
314                 CERROR("Target %s disconnect error %d\n",
315                        tgt->ltd_uuid.uuid, rc);
316                 rc = 0;
317         }
318
319         tgt->ltd_exp = NULL;
320         RETURN(0);
321 }
322
323 static int lov_disconnect(struct obd_export *exp)
324 {
325         struct obd_device *obd = class_exp2obd(exp);
326         struct lov_obd *lov = &obd->u.lov;
327         int i, rc;
328         ENTRY;
329
330         if (!lov->lov_tgts)
331                 goto out;
332
333         /* Only disconnect the underlying layers on the final disconnect. */
334         lov->lov_connects--;
335         if (lov->lov_connects != 0) {
336                 /* why should there be more than 1 connect? */
337                 CERROR("disconnect #%d\n", lov->lov_connects);
338                 goto out;
339         }
340
341         /* Let's hold another reference so lov_del_obd doesn't spin through
342            putref every time */
343         obd_getref(obd);
344
345         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
346                 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
347                         /* Disconnection is the last we know about an obd */
348                         lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
349                 }
350         }
351         obd_putref(obd);
352
353         if (lov->targets_proc_entry != NULL)
354                 lprocfs_remove(&lov->targets_proc_entry);
355
356 out:
357         rc = class_disconnect(exp); /* bz 9811 */
358         RETURN(rc);
359 }
360
361 /* Error codes:
362  *
363  *  -EINVAL  : UUID can't be found in the LOV's target list
364  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
365  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
366  *  any >= 0 : is log target index
367  */
368 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
369                               enum obd_notify_event ev)
370 {
371         struct lov_obd *lov = &obd->u.lov;
372         struct lov_tgt_desc *tgt;
373         int index, activate, active;
374         ENTRY;
375
376         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
377                lov, uuid->uuid, ev);
378
379         obd_getref(obd);
380         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
381                 tgt = lov->lov_tgts[index];
382                 if (!tgt)
383                         continue;
384                 /*
385                  * LU-642, initially inactive OSC could miss the obd_connect,
386                  * we make up for it here.
387                  */
388                 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
389                     obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
390                         struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
391
392                         obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
393                                     &lov_osc_uuid, &lov->lov_ocd, NULL);
394                 }
395                 if (!tgt->ltd_exp)
396                         continue;
397
398                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
399                        index, obd_uuid2str(&tgt->ltd_uuid),
400                        tgt->ltd_exp->exp_handle.h_cookie);
401                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
402                         break;
403         }
404
405         if (index == lov->desc.ld_tgt_count)
406                 GOTO(out, index = -EINVAL);
407
408         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
409                 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
410
411                 if (lov->lov_tgts[index]->ltd_activate == activate) {
412                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
413                                uuid->uuid, activate ? "" : "de");
414                 } else {
415                         lov->lov_tgts[index]->ltd_activate = activate;
416                         CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
417                                activate ? "" : "de", obd_uuid2str(uuid));
418                 }
419
420         } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
421                 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
422
423                 if (lov->lov_tgts[index]->ltd_active == active) {
424                         CDEBUG(D_INFO, "OSC %s already %sactive!\n",
425                                uuid->uuid, active ? "" : "in");
426                         GOTO(out, index);
427                 } else {
428                         CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
429                                obd_uuid2str(uuid), active ? "" : "in");
430                 }
431
432                 lov->lov_tgts[index]->ltd_active = active;
433                 if (active) {
434                         lov->desc.ld_active_tgt_count++;
435                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
436                 } else {
437                         lov->desc.ld_active_tgt_count--;
438                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
439                 }
440         } else {
441                 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
442                        ev, uuid->uuid);
443         }
444
445  out:
446         obd_putref(obd);
447         RETURN(index);
448 }
449
450 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
451                       enum obd_notify_event ev, void *data)
452 {
453         int rc = 0;
454         struct lov_obd *lov = &obd->u.lov;
455         ENTRY;
456
457         down_read(&lov->lov_notify_lock);
458         if (!lov->lov_connects) {
459                 up_read(&lov->lov_notify_lock);
460                 RETURN(rc);
461         }
462
463         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
464             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
465                 struct obd_uuid *uuid;
466
467                 LASSERT(watched);
468
469                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
470                         up_read(&lov->lov_notify_lock);
471                         CERROR("unexpected notification of %s %s!\n",
472                                watched->obd_type->typ_name,
473                                watched->obd_name);
474                         RETURN(-EINVAL);
475                 }
476                 uuid = &watched->u.cli.cl_target_uuid;
477
478                 /* Set OSC as active before notifying the observer, so the
479                  * observer can use the OSC normally.
480                  */
481                 rc = lov_set_osc_active(obd, uuid, ev);
482                 if (rc < 0) {
483                         up_read(&lov->lov_notify_lock);
484                         CERROR("event(%d) of %s failed: %d\n", ev,
485                                obd_uuid2str(uuid), rc);
486                         RETURN(rc);
487                 }
488                 /* active event should be pass lov target index as data */
489                 data = &rc;
490         }
491
492         /* Pass the notification up the chain. */
493         if (watched) {
494                 rc = obd_notify_observer(obd, watched, ev, data);
495         } else {
496                 /* NULL watched means all osc's in the lov (only for syncs) */
497                 /* sync event should be send lov idx as data */
498                 struct lov_obd *lov = &obd->u.lov;
499                 int i, is_sync;
500
501                 data = &i;
502                 is_sync = (ev == OBD_NOTIFY_SYNC) ||
503                           (ev == OBD_NOTIFY_SYNC_NONBLOCK);
504
505                 obd_getref(obd);
506                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
507                         if (!lov->lov_tgts[i])
508                                 continue;
509
510                         /* don't send sync event if target not
511                          * connected/activated */
512                         if (is_sync &&  !lov->lov_tgts[i]->ltd_active)
513                                 continue;
514
515                         rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
516                                                  ev, data);
517                         if (rc) {
518                                 CERROR("%s: notify %s of %s failed %d\n",
519                                        obd->obd_name,
520                                        obd->obd_observer->obd_name,
521                                        lov->lov_tgts[i]->ltd_obd->obd_name,
522                                        rc);
523                         }
524                 }
525                 obd_putref(obd);
526         }
527
528         up_read(&lov->lov_notify_lock);
529         RETURN(rc);
530 }
531
532 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
533                           __u32 index, int gen, int active)
534 {
535         struct lov_obd *lov = &obd->u.lov;
536         struct lov_tgt_desc *tgt;
537         struct obd_device *tgt_obd;
538         int rc;
539         ENTRY;
540
541         CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
542                uuidp->uuid, index, gen, active);
543
544         if (gen <= 0) {
545                 CERROR("request to add OBD %s with invalid generation: %d\n",
546                        uuidp->uuid, gen);
547                 RETURN(-EINVAL);
548         }
549
550         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
551                                         &obd->obd_uuid);
552         if (tgt_obd == NULL)
553                 RETURN(-EINVAL);
554
555         mutex_lock(&lov->lov_lock);
556
557         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
558                 tgt = lov->lov_tgts[index];
559                 CERROR("UUID %s already assigned at LOV target index %d\n",
560                        obd_uuid2str(&tgt->ltd_uuid), index);
561                 mutex_unlock(&lov->lov_lock);
562                 RETURN(-EEXIST);
563         }
564
565         if (index >= lov->lov_tgt_size) {
566                 /* We need to reallocate the lov target array. */
567                 struct lov_tgt_desc **newtgts, **old = NULL;
568                 __u32 newsize, oldsize = 0;
569
570                 newsize = max(lov->lov_tgt_size, (__u32)2);
571                 while (newsize < index + 1)
572                         newsize = newsize << 1;
573                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
574                 if (newtgts == NULL) {
575                         mutex_unlock(&lov->lov_lock);
576                         RETURN(-ENOMEM);
577                 }
578
579                 if (lov->lov_tgt_size) {
580                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
581                                lov->lov_tgt_size);
582                         old = lov->lov_tgts;
583                         oldsize = lov->lov_tgt_size;
584                 }
585
586                 lov->lov_tgts = newtgts;
587                 lov->lov_tgt_size = newsize;
588                 smp_rmb();
589                 if (old)
590                         OBD_FREE(old, sizeof(*old) * oldsize);
591
592                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
593                        lov->lov_tgts, lov->lov_tgt_size);
594         }
595
596         OBD_ALLOC_PTR(tgt);
597         if (!tgt) {
598                 mutex_unlock(&lov->lov_lock);
599                 RETURN(-ENOMEM);
600         }
601
602         rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
603         if (rc) {
604                 mutex_unlock(&lov->lov_lock);
605                 OBD_FREE_PTR(tgt);
606                 RETURN(rc);
607         }
608
609         tgt->ltd_uuid = *uuidp;
610         tgt->ltd_obd = tgt_obd;
611         /* XXX - add a sanity check on the generation number. */
612         tgt->ltd_gen = gen;
613         tgt->ltd_index = index;
614         tgt->ltd_activate = active;
615         lov->lov_tgts[index] = tgt;
616         if (index >= lov->desc.ld_tgt_count)
617                 lov->desc.ld_tgt_count = index + 1;
618
619         mutex_unlock(&lov->lov_lock);
620
621         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
622                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
623
624         rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
625
626         if (lov->lov_connects == 0) {
627                 /* lov_connect hasn't been called yet. We'll do the
628                    lov_connect_obd on this target when that fn first runs,
629                    because we don't know the connect flags yet. */
630                 RETURN(0);
631         }
632
633         obd_getref(obd);
634
635         rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
636         if (rc)
637                 GOTO(out, rc);
638
639         /* connect to administrative disabled ost */
640         if (!tgt->ltd_exp)
641                 GOTO(out, rc = 0);
642
643         if (lov->lov_cache != NULL) {
644                 rc = obd_set_info_async(NULL, tgt->ltd_exp,
645                                 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
646                                 sizeof(struct cl_client_cache), lov->lov_cache,
647                                 NULL);
648                 if (rc < 0)
649                         GOTO(out, rc);
650         }
651
652         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
653                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
654                         (void *)&index);
655
656 out:
657         if (rc) {
658                 CERROR("add failed (%d), deleting %s\n", rc,
659                        obd_uuid2str(&tgt->ltd_uuid));
660                 lov_del_target(obd, index, 0, 0);
661         }
662         obd_putref(obd);
663         RETURN(rc);
664 }
665
666 /* Schedule a target for deletion */
667 int lov_del_target(struct obd_device *obd, __u32 index,
668                    struct obd_uuid *uuidp, int gen)
669 {
670         struct lov_obd *lov = &obd->u.lov;
671         int count = lov->desc.ld_tgt_count;
672         int rc = 0;
673         ENTRY;
674
675         if (index >= count) {
676                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
677                        index, count);
678                 RETURN(-EINVAL);
679         }
680
681         /* to make sure there's no ongoing lov_notify() now */
682         down_write(&lov->lov_notify_lock);
683         obd_getref(obd);
684
685         if (!lov->lov_tgts[index]) {
686                 CERROR("LOV target at index %d is not setup.\n", index);
687                 GOTO(out, rc = -EINVAL);
688         }
689
690         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
691                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
692                        lov_uuid2str(lov, index), index,
693                        obd_uuid2str(uuidp));
694                 GOTO(out, rc = -EINVAL);
695         }
696
697         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
698                lov_uuid2str(lov, index), index,
699                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
700                lov->lov_tgts[index]->ltd_active);
701
702         lov->lov_tgts[index]->ltd_reap = 1;
703         lov->lov_death_row++;
704         /* we really delete it from obd_putref */
705 out:
706         obd_putref(obd);
707         up_write(&lov->lov_notify_lock);
708
709         RETURN(rc);
710 }
711
712 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
713 {
714         struct obd_device *osc_obd;
715
716         LASSERT(tgt);
717         LASSERT(tgt->ltd_reap);
718
719         osc_obd = class_exp2obd(tgt->ltd_exp);
720
721         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
722                tgt->ltd_uuid.uuid,
723                osc_obd ? osc_obd->obd_name : "<no obd>");
724
725         if (tgt->ltd_exp)
726                 lov_disconnect_obd(obd, tgt);
727
728         OBD_FREE_PTR(tgt);
729
730         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
731            do it ourselves. And we can't do it from lov_cleanup,
732            because we just lost our only reference to it. */
733         if (osc_obd)
734                 class_manual_cleanup(osc_obd);
735 }
736
737 void lov_fix_desc_stripe_size(__u64 *val)
738 {
739         if (*val < LOV_MIN_STRIPE_SIZE) {
740                 if (*val != 0)
741                         LCONSOLE_INFO("Increasing default stripe size to "
742                                       "minimum %u\n",
743                                       LOV_DESC_STRIPE_SIZE_DEFAULT);
744                 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
745         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
746                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
747                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
748                               "multiple of %u)\n",
749                               *val, LOV_MIN_STRIPE_SIZE);
750         }
751 }
752
753 void lov_fix_desc_stripe_count(__u32 *val)
754 {
755         if (*val == 0)
756                 *val = 1;
757 }
758
759 void lov_fix_desc_pattern(__u32 *val)
760 {
761         /* from lov_setstripe */
762         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
763                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
764                 *val = 0;
765         }
766 }
767
768 void lov_fix_desc_qos_maxage(__u32 *val)
769 {
770         if (*val == 0)
771                 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
772 }
773
774 void lov_fix_desc(struct lov_desc *desc)
775 {
776         lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
777         lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
778         lov_fix_desc_pattern(&desc->ld_pattern);
779         lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
780 }
781
782 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
783 {
784         struct lov_desc *desc;
785         struct lov_obd *lov = &obd->u.lov;
786 #ifdef LPROCFS
787         struct obd_type *type;
788 #endif
789         int rc;
790         ENTRY;
791
792         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
793                 CERROR("LOV setup requires a descriptor\n");
794                 RETURN(-EINVAL);
795         }
796
797         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
798
799         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
800                 CERROR("descriptor size wrong: %d > %d\n",
801                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
802                 RETURN(-EINVAL);
803         }
804
805         if (desc->ld_magic != LOV_DESC_MAGIC) {
806                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
807                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
808                                    obd->obd_name, desc);
809                             lustre_swab_lov_desc(desc);
810                 } else {
811                         CERROR("%s: Bad lov desc magic: %#x\n",
812                                obd->obd_name, desc->ld_magic);
813                         RETURN(-EINVAL);
814                 }
815         }
816
817         lov_fix_desc(desc);
818
819         desc->ld_active_tgt_count = 0;
820         lov->desc = *desc;
821         lov->lov_tgt_size = 0;
822
823         mutex_init(&lov->lov_lock);
824         atomic_set(&lov->lov_refcount, 0);
825         lov->lov_sp_me = LUSTRE_SP_CLI;
826
827         init_rwsem(&lov->lov_notify_lock);
828
829         lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
830                                                    HASH_POOLS_MAX_BITS,
831                                                    HASH_POOLS_BKT_BITS, 0,
832                                                    CFS_HASH_MIN_THETA,
833                                                    CFS_HASH_MAX_THETA,
834                                                    &pool_hash_operations,
835                                                    CFS_HASH_DEFAULT);
836         INIT_LIST_HEAD(&lov->lov_pool_list);
837         lov->lov_pool_count = 0;
838         rc = lov_ost_pool_init(&lov->lov_packed, 0);
839         if (rc)
840                 GOTO(out, rc);
841
842 #ifdef LPROCFS
843         obd->obd_vars = lprocfs_lov_obd_vars;
844         /* If this is true then both client (lov) and server
845          * (lod) are on the same node. The lod layer if loaded
846          * first will register the lov proc directory. In that
847          * case obd->obd_type->typ_procroot will be not set.
848          * Instead we use type->typ_procsym as the parent. */
849         type = class_search_type(LUSTRE_LOD_NAME);
850         if (type != NULL && type->typ_procsym != NULL) {
851                 obd->obd_proc_entry = lprocfs_seq_register(obd->obd_name,
852                                                            type->typ_procsym,
853                                                            obd->obd_vars, obd);
854                 if (IS_ERR(obd->obd_proc_entry)) {
855                         rc = PTR_ERR(obd->obd_proc_entry);
856                         CERROR("error %d setting up lprocfs for %s\n", rc,
857                                obd->obd_name);
858                         obd->obd_proc_entry = NULL;
859                 }
860         } else {
861                 rc = lprocfs_obd_setup(obd);
862         }
863
864         if (rc == 0) {
865                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
866                                         0444, &lov_proc_target_fops, obd);
867                 if (rc)
868                         CWARN("Error adding the target_obd file\n");
869
870                 lov->lov_pool_proc_entry = lprocfs_seq_register("pools",
871                                                         obd->obd_proc_entry,
872                                                         NULL, NULL);
873                 if (IS_ERR(lov->lov_pool_proc_entry)) {
874                         rc = PTR_ERR(lov->lov_pool_proc_entry);
875                         CERROR("error %d setting up lprocfs for pools\n", rc);
876                         lov->lov_pool_proc_entry = NULL;
877                 }
878         }
879 #endif
880         RETURN(0);
881
882 out:
883         return rc;
884 }
885
886 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
887 {
888         int rc = 0;
889         struct lov_obd *lov = &obd->u.lov;
890
891         ENTRY;
892
893         switch (stage) {
894         case OBD_CLEANUP_EARLY: {
895                 int i;
896                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
897                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
898                                 continue;
899                         obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
900                                        OBD_CLEANUP_EARLY);
901                 }
902                 break;
903         }
904         default:
905                 break;
906         }
907
908         RETURN(rc);
909 }
910
911 static int lov_cleanup(struct obd_device *obd)
912 {
913         struct lov_obd *lov = &obd->u.lov;
914         struct list_head *pos, *tmp;
915         struct pool_desc *pool;
916         ENTRY;
917
918         list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
919                 pool = list_entry(pos, struct pool_desc, pool_list);
920                 /* free pool structs */
921                 CDEBUG(D_INFO, "delete pool %p\n", pool);
922                 /* In the function below, .hs_keycmp resolves to
923                  * pool_hashkey_keycmp() */
924                 /* coverity[overrun-buffer-val] */
925                 lov_pool_del(obd, pool->pool_name);
926         }
927         cfs_hash_putref(lov->lov_pools_hash_body);
928         lov_ost_pool_free(&lov->lov_packed);
929
930         lprocfs_obd_cleanup(obd);
931         if (lov->lov_tgts) {
932                 int i;
933                 obd_getref(obd);
934                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
935                         if (!lov->lov_tgts[i])
936                                 continue;
937
938                         /* Inactive targets may never have connected */
939                         if (lov->lov_tgts[i]->ltd_active ||
940                             atomic_read(&lov->lov_refcount))
941                                 /* We should never get here - these
942                                  * should have been removed in the
943                                  * disconnect. */
944                                 CERROR("%s: lov tgt %d not cleaned! "
945                                        "deathrow=%d, lovrc=%d\n",
946                                        obd->obd_name, i, lov->lov_death_row,
947                                        atomic_read(&lov->lov_refcount));
948                         lov_del_target(obd, i, 0, 0);
949                 }
950                 obd_putref(obd);
951                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
952                          lov->lov_tgt_size);
953                 lov->lov_tgt_size = 0;
954         }
955         RETURN(0);
956 }
957
958 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
959                             __u32 *indexp, int *genp)
960 {
961         struct obd_uuid obd_uuid;
962         int cmd;
963         int rc = 0;
964         ENTRY;
965
966         switch(cmd = lcfg->lcfg_command) {
967         case LCFG_LOV_ADD_OBD:
968         case LCFG_LOV_ADD_INA:
969         case LCFG_LOV_DEL_OBD: {
970                 __u32 index;
971                 int gen;
972                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
973                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
974                         GOTO(out, rc = -EINVAL);
975
976                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
977
978                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
979                         GOTO(out, rc = -EINVAL);
980                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
981                         GOTO(out, rc = -EINVAL);
982                 index = *indexp;
983                 gen = *genp;
984                 if (cmd == LCFG_LOV_ADD_OBD)
985                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
986                 else if (cmd == LCFG_LOV_ADD_INA)
987                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
988                 else
989                         rc = lov_del_target(obd, index, &obd_uuid, gen);
990                 GOTO(out, rc);
991         }
992         case LCFG_PARAM: {
993                 struct lov_desc *desc = &(obd->u.lov.desc);
994
995                 if (!desc)
996                         GOTO(out, rc = -EINVAL);
997
998                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
999                                               lcfg, obd);
1000                 if (rc > 0)
1001                         rc = 0;
1002                 GOTO(out, rc);
1003         }
1004         case LCFG_POOL_NEW:
1005         case LCFG_POOL_ADD:
1006         case LCFG_POOL_DEL:
1007         case LCFG_POOL_REM:
1008                 GOTO(out, rc);
1009
1010         default: {
1011                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1012                 GOTO(out, rc = -EINVAL);
1013
1014         }
1015         }
1016 out:
1017         RETURN(rc);
1018 }
1019
1020 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
1021                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
1022 {
1023         struct lov_stripe_md *obj_mdp, *lsm;
1024         struct lov_obd *lov = &exp->exp_obd->u.lov;
1025         unsigned ost_idx;
1026         int rc, i;
1027         ENTRY;
1028
1029         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1030                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
1031
1032         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
1033         if (obj_mdp == NULL)
1034                 RETURN(-ENOMEM);
1035
1036         ost_idx = src_oa->o_nlink;
1037         lsm = *ea;
1038         if (lsm == NULL)
1039                 GOTO(out, rc = -EINVAL);
1040         if (ost_idx >= lov->desc.ld_tgt_count ||
1041             !lov->lov_tgts[ost_idx])
1042                 GOTO(out, rc = -EINVAL);
1043
1044         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1045                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1046
1047                 if (lov_oinfo_is_dummy(loi))
1048                         continue;
1049
1050                 if (loi->loi_ost_idx == ost_idx) {
1051                         if (ostid_id(&loi->loi_oi) !=
1052                                         ostid_id(&src_oa->o_oi))
1053                                 GOTO(out, rc = -EINVAL);
1054                         break;
1055                 }
1056         }
1057         if (i == lsm->lsm_stripe_count)
1058                 GOTO(out, rc = -EINVAL);
1059
1060         rc = obd_create(NULL, lov->lov_tgts[ost_idx]->ltd_exp,
1061                         src_oa, &obj_mdp, oti);
1062 out:
1063         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
1064         RETURN(rc);
1065 }
1066
1067 /* the LOV expects oa->o_id to be set to the LOV object id */
1068 static int lov_create(const struct lu_env *env, struct obd_export *exp,
1069                       struct obdo *src_oa, struct lov_stripe_md **ea,
1070                       struct obd_trans_info *oti)
1071 {
1072         struct lov_obd *lov;
1073         int rc = 0;
1074         ENTRY;
1075
1076         LASSERT(ea != NULL);
1077         if (exp == NULL)
1078                 RETURN(-EINVAL);
1079
1080         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1081             src_oa->o_flags == OBD_FL_DELORPHAN) {
1082                 /* should be used with LOV anymore */
1083                 LBUG();
1084         }
1085
1086         lov = &exp->exp_obd->u.lov;
1087         if (!lov->desc.ld_active_tgt_count)
1088                 RETURN(-EIO);
1089
1090         obd_getref(exp->exp_obd);
1091         /* Recreate a specific object id at the given OST index */
1092         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1093             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1094                  rc = lov_recreate(exp, src_oa, ea, oti);
1095         }
1096
1097         obd_putref(exp->exp_obd);
1098         RETURN(rc);
1099 }
1100
1101 #define ASSERT_LSM_MAGIC(lsmp)                                                  \
1102 do {                                                                            \
1103         LASSERT((lsmp) != NULL);                                                \
1104         LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 ||                          \
1105                  (lsmp)->lsm_magic == LOV_MAGIC_V3),                            \
1106                  "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic);              \
1107 } while (0)
1108
1109 static int lov_destroy(const struct lu_env *env, struct obd_export *exp,
1110                        struct obdo *oa, struct lov_stripe_md *lsm,
1111                        struct obd_trans_info *oti, struct obd_export *md_exp,
1112                        void *capa)
1113 {
1114         struct lov_request_set *set;
1115         struct obd_info oinfo;
1116         struct lov_request *req;
1117         struct list_head *pos;
1118         struct lov_obd *lov;
1119         int rc = 0, err = 0;
1120         ENTRY;
1121
1122         ASSERT_LSM_MAGIC(lsm);
1123
1124         if (!exp || !exp->exp_obd)
1125                 RETURN(-ENODEV);
1126
1127         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1128                 LASSERT(oti);
1129                 LASSERT(oti->oti_logcookies);
1130         }
1131
1132         lov = &exp->exp_obd->u.lov;
1133         obd_getref(exp->exp_obd);
1134         rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1135         if (rc)
1136                 GOTO(out, rc);
1137
1138         list_for_each(pos, &set->set_list) {
1139                 req = list_entry(pos, struct lov_request, rq_link);
1140
1141                 if (oa->o_valid & OBD_MD_FLCOOKIE)
1142                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1143
1144                 err = obd_destroy(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1145                                   req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1146                 err = lov_update_common_set(set, req, err);
1147                 if (err) {
1148                         CERROR("%s: destroying objid "DOSTID" subobj "
1149                                DOSTID" on OST idx %d: rc = %d\n",
1150                                exp->exp_obd->obd_name, POSTID(&oa->o_oi),
1151                                POSTID(&req->rq_oi.oi_oa->o_oi),
1152                                req->rq_idx, err);
1153                         if (!rc)
1154                                 rc = err;
1155                 }
1156         }
1157
1158         if (rc == 0) {
1159                 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1160                 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1161         }
1162         err = lov_fini_destroy_set(set);
1163 out:
1164         obd_putref(exp->exp_obd);
1165         RETURN(rc ? rc : err);
1166 }
1167
1168 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1169                                  void *data, int rc)
1170 {
1171         struct lov_request_set *lovset = (struct lov_request_set *)data;
1172         int err;
1173         ENTRY;
1174
1175         /* don't do attribute merge if this aysnc op failed */
1176         if (rc)
1177                 atomic_set(&lovset->set_completes, 0);
1178         err = lov_fini_getattr_set(lovset);
1179         RETURN(rc ? rc : err);
1180 }
1181
1182 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1183                               struct ptlrpc_request_set *rqset)
1184 {
1185         struct lov_request_set *lovset;
1186         struct lov_obd *lov;
1187         struct list_head *pos;
1188         struct lov_request *req;
1189         int rc = 0, err;
1190         ENTRY;
1191
1192         LASSERT(oinfo);
1193         ASSERT_LSM_MAGIC(oinfo->oi_md);
1194
1195         if (!exp || !exp->exp_obd)
1196                 RETURN(-ENODEV);
1197
1198         lov = &exp->exp_obd->u.lov;
1199
1200         rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1201         if (rc)
1202                 RETURN(rc);
1203
1204         CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1205                POSTID(&oinfo->oi_md->lsm_oi), oinfo->oi_md->lsm_stripe_count,
1206                oinfo->oi_md->lsm_stripe_size);
1207
1208         list_for_each(pos, &lovset->set_list) {
1209                 req = list_entry(pos, struct lov_request, rq_link);
1210
1211                 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1212                        "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1213                        POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1214                 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1215                                        &req->rq_oi, rqset);
1216                 if (rc) {
1217                         CERROR("%s: getattr objid "DOSTID" subobj"
1218                                DOSTID" on OST idx %d: rc = %d\n",
1219                                exp->exp_obd->obd_name,
1220                                POSTID(&oinfo->oi_oa->o_oi),
1221                                POSTID(&req->rq_oi.oi_oa->o_oi),
1222                                req->rq_idx, rc);
1223                         GOTO(out, rc);
1224                 }
1225         }
1226
1227         if (!list_empty(&rqset->set_requests)) {
1228                 LASSERT(rc == 0);
1229                 LASSERT (rqset->set_interpret == NULL);
1230                 rqset->set_interpret = lov_getattr_interpret;
1231                 rqset->set_arg = (void *)lovset;
1232                 RETURN(rc);
1233         }
1234 out:
1235         if (rc)
1236                 atomic_set(&lovset->set_completes, 0);
1237         err = lov_fini_getattr_set(lovset);
1238         RETURN(rc ? rc : err);
1239 }
1240
1241 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1242                                  void *data, int rc)
1243 {
1244         struct lov_request_set *lovset = (struct lov_request_set *)data;
1245         int err;
1246         ENTRY;
1247
1248         if (rc)
1249                 atomic_set(&lovset->set_completes, 0);
1250         err = lov_fini_setattr_set(lovset);
1251         RETURN(rc ? rc : err);
1252 }
1253
1254 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1255    needed. Otherwise, a client is waiting for responses. */
1256 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1257                              struct obd_trans_info *oti,
1258                              struct ptlrpc_request_set *rqset)
1259 {
1260         struct lov_request_set *set;
1261         struct lov_request *req;
1262         struct list_head *pos;
1263         struct lov_obd *lov;
1264         int rc = 0;
1265         ENTRY;
1266
1267         LASSERT(oinfo);
1268         ASSERT_LSM_MAGIC(oinfo->oi_md);
1269         if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1270                 LASSERT(oti);
1271                 LASSERT(oti->oti_logcookies);
1272         }
1273
1274         if (!exp || !exp->exp_obd)
1275                 RETURN(-ENODEV);
1276
1277         lov = &exp->exp_obd->u.lov;
1278         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1279         if (rc)
1280                 RETURN(rc);
1281
1282         CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1283                POSTID(&oinfo->oi_md->lsm_oi),
1284                oinfo->oi_md->lsm_stripe_count,
1285                oinfo->oi_md->lsm_stripe_size);
1286
1287         list_for_each(pos, &set->set_list) {
1288                 req = list_entry(pos, struct lov_request, rq_link);
1289
1290                 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1291                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1292
1293                 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1294                        "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1295                        POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1296
1297                 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1298                                        &req->rq_oi, oti, rqset);
1299                 if (rc) {
1300                         CERROR("error: setattr objid "DOSTID" subobj"
1301                                DOSTID" on OST idx %d: rc = %d\n",
1302                                POSTID(&set->set_oi->oi_oa->o_oi),
1303                                POSTID(&req->rq_oi.oi_oa->o_oi),
1304                                req->rq_idx, rc);
1305                         break;
1306                 }
1307         }
1308
1309         /* If we are not waiting for responses on async requests, return. */
1310         if (rc || !rqset || list_empty(&rqset->set_requests)) {
1311                 int err;
1312                 if (rc)
1313                         atomic_set(&set->set_completes, 0);
1314                 err = lov_fini_setattr_set(set);
1315                 RETURN(rc ? rc : err);
1316         }
1317
1318         LASSERT(rqset->set_interpret == NULL);
1319         rqset->set_interpret = lov_setattr_interpret;
1320         rqset->set_arg = (void *)set;
1321
1322         RETURN(0);
1323 }
1324
1325 static int lov_change_cbdata(struct obd_export *exp,
1326                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1327                              void *data)
1328 {
1329         struct lov_obd *lov;
1330         int rc = 0, i;
1331         ENTRY;
1332
1333         ASSERT_LSM_MAGIC(lsm);
1334
1335         if (!exp || !exp->exp_obd)
1336                 RETURN(-ENODEV);
1337
1338         lov = &exp->exp_obd->u.lov;
1339         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1340                 struct lov_stripe_md submd;
1341                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1342
1343                 if (lov_oinfo_is_dummy(loi))
1344                         continue;
1345
1346                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1347                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1348                         continue;
1349                 }
1350
1351                 submd.lsm_oi = loi->loi_oi;
1352                 submd.lsm_stripe_count = 0;
1353                 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1354                                        &submd, it, data);
1355         }
1356         RETURN(rc);
1357 }
1358
1359 /* find any ldlm lock of the inode in lov
1360  * return 0    not find
1361  *        1    find one
1362  *      < 0    error */
1363 static int lov_find_cbdata(struct obd_export *exp,
1364                            struct lov_stripe_md *lsm, ldlm_iterator_t it,
1365                            void *data)
1366 {
1367         struct lov_obd *lov;
1368         int rc = 0, i;
1369         ENTRY;
1370
1371         ASSERT_LSM_MAGIC(lsm);
1372
1373         if (!exp || !exp->exp_obd)
1374                 RETURN(-ENODEV);
1375
1376         lov = &exp->exp_obd->u.lov;
1377         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1378                 struct lov_stripe_md submd;
1379                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1380
1381                 if (lov_oinfo_is_dummy(loi))
1382                         continue;
1383
1384                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1385                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1386                         continue;
1387                 }
1388
1389                 submd.lsm_oi = loi->loi_oi;
1390                 submd.lsm_stripe_count = 0;
1391                 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1392                                      &submd, it, data);
1393                 if (rc != 0)
1394                         RETURN(rc);
1395         }
1396         RETURN(rc);
1397 }
1398
1399 int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1400 {
1401         struct lov_request_set *lovset = (struct lov_request_set *)data;
1402         int err;
1403         ENTRY;
1404
1405         if (rc)
1406                 atomic_set(&lovset->set_completes, 0);
1407
1408         err = lov_fini_statfs_set(lovset);
1409         RETURN(rc ? rc : err);
1410 }
1411
1412 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1413                             __u64 max_age, struct ptlrpc_request_set *rqset)
1414 {
1415         struct obd_device      *obd = class_exp2obd(exp);
1416         struct lov_request_set *set;
1417         struct lov_request *req;
1418         struct list_head *pos;
1419         struct lov_obd *lov;
1420         int rc = 0;
1421         ENTRY;
1422
1423         LASSERT(oinfo != NULL);
1424         LASSERT(oinfo->oi_osfs != NULL);
1425
1426         lov = &obd->u.lov;
1427         rc = lov_prep_statfs_set(obd, oinfo, &set);
1428         if (rc)
1429                 RETURN(rc);
1430
1431         list_for_each(pos, &set->set_list) {
1432                 req = list_entry(pos, struct lov_request, rq_link);
1433                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1434                                       &req->rq_oi, max_age, rqset);
1435                 if (rc)
1436                         break;
1437         }
1438
1439         if (rc || list_empty(&rqset->set_requests)) {
1440                 int err;
1441                 if (rc)
1442                         atomic_set(&set->set_completes, 0);
1443                 err = lov_fini_statfs_set(set);
1444                 RETURN(rc ? rc : err);
1445         }
1446
1447         LASSERT(rqset->set_interpret == NULL);
1448         rqset->set_interpret = lov_statfs_interpret;
1449         rqset->set_arg = (void *)set;
1450         RETURN(0);
1451 }
1452
1453 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1454                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1455 {
1456         struct ptlrpc_request_set *set = NULL;
1457         struct obd_info oinfo = { { { 0 } } };
1458         int rc = 0;
1459         ENTRY;
1460
1461
1462         /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1463          * statfs requests */
1464         set = ptlrpc_prep_set();
1465         if (set == NULL)
1466                 RETURN(-ENOMEM);
1467
1468         oinfo.oi_osfs = osfs;
1469         oinfo.oi_flags = flags;
1470         rc = lov_statfs_async(exp, &oinfo, max_age, set);
1471         if (rc == 0)
1472                 rc = ptlrpc_set_wait(set);
1473         ptlrpc_set_destroy(set);
1474
1475         RETURN(rc);
1476 }
1477
1478 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1479                          void *karg, void *uarg)
1480 {
1481         struct obd_device *obddev = class_exp2obd(exp);
1482         struct lov_obd *lov = &obddev->u.lov;
1483         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1484         struct obd_uuid *uuidp;
1485         ENTRY;
1486
1487         switch (cmd) {
1488         case IOC_OBD_STATFS: {
1489                 struct obd_ioctl_data *data = karg;
1490                 struct obd_device *osc_obd;
1491                 struct obd_statfs stat_buf = {0};
1492                 __u32 index;
1493                 __u32 flags;
1494
1495                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1496                 if ((index >= count))
1497                         RETURN(-ENODEV);
1498
1499                 if (!lov->lov_tgts[index])
1500                         /* Try again with the next index */
1501                         RETURN(-EAGAIN);
1502                 if (!lov->lov_tgts[index]->ltd_active)
1503                         RETURN(-ENODATA);
1504
1505                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1506                 if (!osc_obd)
1507                         RETURN(-EINVAL);
1508
1509                 /* copy UUID */
1510                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1511                                  min((int)data->ioc_plen2,
1512                                      (int)sizeof(struct obd_uuid))))
1513                         RETURN(-EFAULT);
1514
1515                 flags = uarg ? *(__u32*)uarg : 0;
1516                 /* got statfs data */
1517                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1518                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1519                                 flags);
1520                 if (rc)
1521                         RETURN(rc);
1522                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1523                                      min((int) data->ioc_plen1,
1524                                          (int) sizeof(stat_buf))))
1525                         RETURN(-EFAULT);
1526                 break;
1527         }
1528         case OBD_IOC_LOV_GET_CONFIG: {
1529                 struct obd_ioctl_data *data;
1530                 struct lov_desc *desc;
1531                 char *buf = NULL;
1532                 __u32 *genp;
1533
1534                 len = 0;
1535                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
1536                         RETURN(-EINVAL);
1537
1538                 data = (struct obd_ioctl_data *)buf;
1539
1540                 if (sizeof(*desc) > data->ioc_inllen1) {
1541                         obd_ioctl_freedata(buf, len);
1542                         RETURN(-EINVAL);
1543                 }
1544
1545                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1546                         obd_ioctl_freedata(buf, len);
1547                         RETURN(-EINVAL);
1548                 }
1549
1550                 if (sizeof(__u32) * count > data->ioc_inllen3) {
1551                         obd_ioctl_freedata(buf, len);
1552                         RETURN(-EINVAL);
1553                 }
1554
1555                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1556                 memcpy(desc, &(lov->desc), sizeof(*desc));
1557
1558                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1559                 genp = (__u32 *)data->ioc_inlbuf3;
1560                 /* the uuid will be empty for deleted OSTs */
1561                 for (i = 0; i < count; i++, uuidp++, genp++) {
1562                         if (!lov->lov_tgts[i])
1563                                 continue;
1564                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
1565                         *genp = lov->lov_tgts[i]->ltd_gen;
1566                 }
1567
1568                 if (copy_to_user((void *)uarg, buf, len))
1569                         rc = -EFAULT;
1570                 obd_ioctl_freedata(buf, len);
1571                 break;
1572         }
1573         case LL_IOC_LOV_GETSTRIPE:
1574                 rc = lov_getstripe(exp, karg, uarg);
1575                 break;
1576         case OBD_IOC_QUOTACTL: {
1577                 struct if_quotactl *qctl = karg;
1578                 struct lov_tgt_desc *tgt = NULL;
1579                 struct obd_quotactl *oqctl;
1580
1581                 if (qctl->qc_valid == QC_OSTIDX) {
1582                         if (count <= qctl->qc_idx)
1583                                 RETURN(-EINVAL);
1584
1585                         tgt = lov->lov_tgts[qctl->qc_idx];
1586                         if (!tgt || !tgt->ltd_exp)
1587                                 RETURN(-EINVAL);
1588                 } else if (qctl->qc_valid == QC_UUID) {
1589                         for (i = 0; i < count; i++) {
1590                                 tgt = lov->lov_tgts[i];
1591                                 if (!tgt ||
1592                                     !obd_uuid_equals(&tgt->ltd_uuid,
1593                                                      &qctl->obd_uuid))
1594                                         continue;
1595
1596                                 if (tgt->ltd_exp == NULL)
1597                                         RETURN(-EINVAL);
1598
1599                                 break;
1600                         }
1601                 } else {
1602                         RETURN(-EINVAL);
1603                 }
1604
1605                 if (i >= count)
1606                         RETURN(-EAGAIN);
1607
1608                 LASSERT(tgt && tgt->ltd_exp);
1609                 OBD_ALLOC_PTR(oqctl);
1610                 if (!oqctl)
1611                         RETURN(-ENOMEM);
1612
1613                 QCTL_COPY(oqctl, qctl);
1614                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1615                 if (rc == 0) {
1616                         QCTL_COPY(qctl, oqctl);
1617                         qctl->qc_valid = QC_OSTIDX;
1618                         qctl->obd_uuid = tgt->ltd_uuid;
1619                 }
1620                 OBD_FREE_PTR(oqctl);
1621                 break;
1622         }
1623         default: {
1624                 int set = 0;
1625
1626                 if (count == 0)
1627                         RETURN(-ENOTTY);
1628
1629                 for (i = 0; i < count; i++) {
1630                         int err;
1631                         struct obd_device *osc_obd;
1632
1633                         /* OST was disconnected */
1634                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1635                                 continue;
1636
1637                         /* ll_umount_begin() sets force flag but for lov, not
1638                          * osc. Let's pass it through */
1639                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1640                         osc_obd->obd_force = obddev->obd_force;
1641                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1642                                             len, karg, uarg);
1643                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1644                                 RETURN(err);
1645                         } else if (err) {
1646                                 if (lov->lov_tgts[i]->ltd_active) {
1647                                         CDEBUG(err == -ENOTTY ?
1648                                                D_IOCTL : D_WARNING,
1649                                                "iocontrol OSC %s on OST "
1650                                                "idx %d cmd %x: err = %d\n",
1651                                                lov_uuid2str(lov, i),
1652                                                i, cmd, err);
1653                                         if (!rc)
1654                                                 rc = err;
1655                                 }
1656                         } else {
1657                                 set = 1;
1658                         }
1659                 }
1660                 if (!set && !rc)
1661                         rc = -EIO;
1662         }
1663         }
1664
1665         RETURN(rc);
1666 }
1667
1668 #define FIEMAP_BUFFER_SIZE 4096
1669
1670 /**
1671  * Non-zero fe_logical indicates that this is a continuation FIEMAP
1672  * call. The local end offset and the device are sent in the first
1673  * fm_extent. This function calculates the stripe number from the index.
1674  * This function returns a stripe_no on which mapping is to be restarted.
1675  *
1676  * This function returns fm_end_offset which is the in-OST offset at which
1677  * mapping should be restarted. If fm_end_offset=0 is returned then caller
1678  * will re-calculate proper offset in next stripe.
1679  * Note that the first extent is passed to lov_get_info via the value field.
1680  *
1681  * \param fiemap fiemap request header
1682  * \param lsm striping information for the file
1683  * \param fm_start logical start of mapping
1684  * \param fm_end logical end of mapping
1685  * \param start_stripe starting stripe will be returned in this
1686  */
1687 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
1688                                    struct lov_stripe_md *lsm, obd_size fm_start,
1689                                    obd_size fm_end, int *start_stripe)
1690 {
1691         obd_size local_end = fiemap->fm_extents[0].fe_logical;
1692         obd_off lun_start, lun_end;
1693         obd_size fm_end_offset;
1694         int stripe_no = -1, i;
1695
1696         if (fiemap->fm_extent_count == 0 ||
1697             fiemap->fm_extents[0].fe_logical == 0)
1698                 return 0;
1699
1700         /* Find out stripe_no from ost_index saved in the fe_device */
1701         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1702                 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
1703
1704                 if (lov_oinfo_is_dummy(oinfo))
1705                         continue;
1706
1707                 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1708                         stripe_no = i;
1709                         break;
1710                 }
1711         }
1712
1713         if (stripe_no == -1)
1714                 return -EINVAL;
1715
1716         /* If we have finished mapping on previous device, shift logical
1717          * offset to start of next device */
1718         if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
1719                                    &lun_start, &lun_end)) != 0 &&
1720                                    local_end < lun_end) {
1721                 fm_end_offset = local_end;
1722                 *start_stripe = stripe_no;
1723         } else {
1724                 /* This is a special value to indicate that caller should
1725                  * calculate offset in next stripe. */
1726                 fm_end_offset = 0;
1727                 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
1728         }
1729
1730         return fm_end_offset;
1731 }
1732
1733 /**
1734  * We calculate on which OST the mapping will end. If the length of mapping
1735  * is greater than (stripe_size * stripe_count) then the last_stripe will
1736  * will be one just before start_stripe. Else we check if the mapping
1737  * intersects each OST and find last_stripe.
1738  * This function returns the last_stripe and also sets the stripe_count
1739  * over which the mapping is spread
1740  *
1741  * \param lsm striping information for the file
1742  * \param fm_start logical start of mapping
1743  * \param fm_end logical end of mapping
1744  * \param start_stripe starting stripe of the mapping
1745  * \param stripe_count the number of stripes across which to map is returned
1746  *
1747  * \retval last_stripe return the last stripe of the mapping
1748  */
1749 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
1750                             obd_size fm_end, int start_stripe,
1751                             int *stripe_count)
1752 {
1753         int last_stripe;
1754         obd_off obd_start, obd_end;
1755         int i, j;
1756
1757         if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
1758                 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
1759                                                               start_stripe - 1);
1760                 *stripe_count = lsm->lsm_stripe_count;
1761         } else {
1762                 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
1763                      i = (i + 1) % lsm->lsm_stripe_count, j++) {
1764                         if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
1765                                                    &obd_start, &obd_end)) == 0)
1766                                 break;
1767                 }
1768                 *stripe_count = j;
1769                 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
1770         }
1771
1772         return last_stripe;
1773 }
1774
1775 /**
1776  * Set fe_device and copy extents from local buffer into main return buffer.
1777  *
1778  * \param fiemap fiemap request header
1779  * \param lcl_fm_ext array of local fiemap extents to be copied
1780  * \param ost_index OST index to be written into the fm_device field for each
1781                     extent
1782  * \param ext_count number of extents to be copied
1783  * \param current_extent where to start copying in main extent array
1784  */
1785 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
1786                                   struct ll_fiemap_extent *lcl_fm_ext,
1787                                   int ost_index, unsigned int ext_count,
1788                                   int current_extent)
1789 {
1790         char *to;
1791         int ext;
1792
1793         for (ext = 0; ext < ext_count; ext++) {
1794                 lcl_fm_ext[ext].fe_device = ost_index;
1795                 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1796         }
1797
1798         /* Copy fm_extent's from fm_local to return buffer */
1799         to = (char *)fiemap + fiemap_count_to_size(current_extent);
1800         memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
1801 }
1802
1803 /**
1804  * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1805  * This also handles the restarting of FIEMAP calls in case mapping overflows
1806  * the available number of extents in single call.
1807  */
1808 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
1809                       __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1810 {
1811         struct ll_fiemap_info_key *fm_key = key;
1812         struct ll_user_fiemap *fiemap = val;
1813         struct ll_user_fiemap *fm_local = NULL;
1814         struct ll_fiemap_extent *lcl_fm_ext;
1815         int count_local;
1816         unsigned int get_num_extents = 0;
1817         int ost_index = 0, actual_start_stripe, start_stripe;
1818         obd_size fm_start, fm_end, fm_length, fm_end_offset;
1819         obd_size curr_loc;
1820         int current_extent = 0, rc = 0, i;
1821         int ost_eof = 0; /* EOF for object */
1822         int ost_done = 0; /* done with required mapping for this OST? */
1823         int last_stripe;
1824         int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
1825         unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1826
1827         if (!lsm_has_objects(lsm)) {
1828                 if (lsm && lsm_is_released(lsm) && (fm_key->fiemap.fm_start <
1829                     fm_key->oa.o_size)) {
1830                         /* released file, return a minimal FIEMAP if
1831                          * request fits in file-size.
1832                          */
1833                         fiemap->fm_mapped_extents = 1;
1834                         fiemap->fm_extents[0].fe_logical =
1835                                                 fm_key->fiemap.fm_start;
1836                         if (fm_key->fiemap.fm_start + fm_key->fiemap.fm_length <
1837                             fm_key->oa.o_size)
1838                                 fiemap->fm_extents[0].fe_length =
1839                                                 fm_key->fiemap.fm_length;
1840                         else
1841                                 fiemap->fm_extents[0].fe_length =
1842                                                 fm_key->oa.o_size -
1843                                                 fm_key->fiemap.fm_start;
1844                         fiemap->fm_extents[0].fe_flags |=
1845                                                 (FIEMAP_EXTENT_UNKNOWN |
1846                                                  FIEMAP_EXTENT_LAST);
1847                 }
1848                 GOTO(out, rc = 0);
1849         }
1850
1851         if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
1852                 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
1853
1854         OBD_ALLOC_LARGE(fm_local, buffer_size);
1855         if (fm_local == NULL)
1856                 GOTO(out, rc = -ENOMEM);
1857         lcl_fm_ext = &fm_local->fm_extents[0];
1858
1859         count_local = fiemap_size_to_count(buffer_size);
1860
1861         memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
1862         fm_start = fiemap->fm_start;
1863         fm_length = fiemap->fm_length;
1864         /* Calculate start stripe, last stripe and length of mapping */
1865         actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
1866         fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
1867                                                 fm_start + fm_length - 1);
1868         /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
1869         if (fm_end > fm_key->oa.o_size)
1870                 fm_end = fm_key->oa.o_size;
1871
1872         last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
1873                                             actual_start_stripe, &stripe_count);
1874
1875         fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start,
1876                                                   fm_end, &start_stripe);
1877         if (fm_end_offset == -EINVAL)
1878                 GOTO(out, rc = -EINVAL);
1879
1880         if (fiemap_count_to_size(fiemap->fm_extent_count) > *vallen)
1881                 fiemap->fm_extent_count = fiemap_size_to_count(*vallen);
1882         if (fiemap->fm_extent_count == 0) {
1883                 get_num_extents = 1;
1884                 count_local = 0;
1885         }
1886         /* Check each stripe */
1887         for (cur_stripe = start_stripe, i = 0; i < stripe_count;
1888              i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
1889                 obd_size req_fm_len; /* Stores length of required mapping */
1890                 obd_size len_mapped_single_call;
1891                 obd_off lun_start, lun_end, obd_object_end;
1892                 unsigned int ext_count;
1893
1894                 cur_stripe_wrap = cur_stripe;
1895
1896                 /* Find out range of mapping on this stripe */
1897                 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
1898                                            &lun_start, &obd_object_end)) == 0)
1899                         continue;
1900
1901                 if (lov_oinfo_is_dummy(lsm->lsm_oinfo[cur_stripe]))
1902                         GOTO(out, rc = -EIO);
1903
1904                 /* If this is a continuation FIEMAP call and we are on
1905                  * starting stripe then lun_start needs to be set to
1906                  * fm_end_offset */
1907                 if (fm_end_offset != 0 && cur_stripe == start_stripe)
1908                         lun_start = fm_end_offset;
1909
1910                 if (fm_length != ~0ULL) {
1911                         /* Handle fm_start + fm_length overflow */
1912                         if (fm_start + fm_length < fm_start)
1913                                 fm_length = ~0ULL - fm_start;
1914                         lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
1915                                                      cur_stripe);
1916                 } else {
1917                         lun_end = ~0ULL;
1918                 }
1919
1920                 if (lun_start == lun_end)
1921                         continue;
1922
1923                 req_fm_len = obd_object_end - lun_start;
1924                 fm_local->fm_length = 0;
1925                 len_mapped_single_call = 0;
1926
1927                 /* If the output buffer is very large and the objects have many
1928                  * extents we may need to loop on a single OST repeatedly */
1929                 ost_eof = 0;
1930                 ost_done = 0;
1931                 do {
1932                         if (get_num_extents == 0) {
1933                                 /* Don't get too many extents. */
1934                                 if (current_extent + count_local >
1935                                     fiemap->fm_extent_count)
1936                                         count_local = fiemap->fm_extent_count -
1937                                                                  current_extent;
1938                         }
1939
1940                         lun_start += len_mapped_single_call;
1941                         fm_local->fm_length = req_fm_len - len_mapped_single_call;
1942                         req_fm_len = fm_local->fm_length;
1943                         fm_local->fm_extent_count = count_local;
1944                         fm_local->fm_mapped_extents = 0;
1945                         fm_local->fm_flags = fiemap->fm_flags;
1946
1947                         fm_key->oa.o_oi = lsm->lsm_oinfo[cur_stripe]->loi_oi;
1948                         ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
1949
1950                         if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
1951                                 GOTO(out, rc = -EINVAL);
1952
1953                         /* If OST is inactive, return extent with UNKNOWN flag */
1954                         if (!lov->lov_tgts[ost_index]->ltd_active) {
1955                                 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
1956                                 fm_local->fm_mapped_extents = 1;
1957
1958                                 lcl_fm_ext[0].fe_logical = lun_start;
1959                                 lcl_fm_ext[0].fe_length = obd_object_end -
1960                                                                       lun_start;
1961                                 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1962
1963                                 goto inactive_tgt;
1964                         }
1965
1966                         fm_local->fm_start = lun_start;
1967                         fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1968                         memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
1969                         *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
1970                         rc = obd_get_info(NULL,
1971                                           lov->lov_tgts[ost_index]->ltd_exp,
1972                                           keylen, key, vallen, fm_local, lsm);
1973                         if (rc != 0)
1974                                 GOTO(out, rc);
1975
1976 inactive_tgt:
1977                         ext_count = fm_local->fm_mapped_extents;
1978                         if (ext_count == 0) {
1979                                 ost_done = 1;
1980                                 /* If last stripe has hole at the end,
1981                                  * then we need to return */
1982                                 if (cur_stripe_wrap == last_stripe) {
1983                                         fiemap->fm_mapped_extents = 0;
1984                                         goto finish;
1985                                 }
1986                                 break;
1987                         }
1988
1989                         /* If we just need num of extents then go to next device */
1990                         if (get_num_extents) {
1991                                 current_extent += ext_count;
1992                                 break;
1993                         }
1994
1995                         len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
1996                                   lun_start + lcl_fm_ext[ext_count - 1].fe_length;
1997
1998                         /* Have we finished mapping on this device? */
1999                         if (req_fm_len <= len_mapped_single_call)
2000                                 ost_done = 1;
2001
2002                         /* Clear the EXTENT_LAST flag which can be present on
2003                          * last extent */
2004                         if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
2005                                 lcl_fm_ext[ext_count - 1].fe_flags &=
2006                                                             ~FIEMAP_EXTENT_LAST;
2007
2008                         curr_loc = lov_stripe_size(lsm,
2009                                            lcl_fm_ext[ext_count - 1].fe_logical+
2010                                            lcl_fm_ext[ext_count - 1].fe_length,
2011                                            cur_stripe);
2012                         if (curr_loc >= fm_key->oa.o_size)
2013                                 ost_eof = 1;
2014
2015                         fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
2016                                                      ost_index, ext_count,
2017                                                      current_extent);
2018
2019                         current_extent += ext_count;
2020
2021                         /* Ran out of available extents? */
2022                         if (current_extent >= fiemap->fm_extent_count)
2023                                 goto finish;
2024                 } while (ost_done == 0 && ost_eof == 0);
2025
2026                 if (cur_stripe_wrap == last_stripe)
2027                         goto finish;
2028         }
2029
2030 finish:
2031         /* Indicate that we are returning device offsets unless file just has
2032          * single stripe */
2033         if (lsm->lsm_stripe_count > 1)
2034                 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2035
2036         if (get_num_extents)
2037                 goto skip_last_device_calc;
2038
2039         /* Check if we have reached the last stripe and whether mapping for that
2040          * stripe is done. */
2041         if (cur_stripe_wrap == last_stripe) {
2042                 if (ost_done || ost_eof)
2043                         fiemap->fm_extents[current_extent - 1].fe_flags |=
2044                                                              FIEMAP_EXTENT_LAST;
2045         }
2046
2047 skip_last_device_calc:
2048         fiemap->fm_mapped_extents = current_extent;
2049
2050 out:
2051         if (fm_local)
2052                 OBD_FREE_LARGE(fm_local, buffer_size);
2053         return rc;
2054 }
2055
2056 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
2057                         __u32 keylen, void *key, __u32 *vallen, void *val,
2058                         struct lov_stripe_md *lsm)
2059 {
2060         struct obd_device *obddev = class_exp2obd(exp);
2061         struct lov_obd *lov = &obddev->u.lov;
2062         int i, rc;
2063         ENTRY;
2064
2065         if (!vallen || !val)
2066                 RETURN(-EFAULT);
2067
2068         obd_getref(obddev);
2069
2070         if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
2071                 struct {
2072                         char name[16];
2073                         struct ldlm_lock *lock;
2074                 } *data = key;
2075                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2076                 struct lov_oinfo *loi;
2077                 __u32 *stripe = val;
2078
2079                 if (*vallen < sizeof(*stripe))
2080                         GOTO(out, rc = -EFAULT);
2081                 *vallen = sizeof(*stripe);
2082
2083                 /* XXX This is another one of those bits that will need to
2084                  * change if we ever actually support nested LOVs.  It uses
2085                  * the lock's export to find out which stripe it is. */
2086                 /* XXX - it's assumed all the locks for deleted OSTs have
2087                  * been cancelled. Also, the export for deleted OSTs will
2088                  * be NULL and won't match the lock's export. */
2089                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2090                         loi = lsm->lsm_oinfo[i];
2091                         if (lov_oinfo_is_dummy(loi))
2092                                 continue;
2093
2094                         if (!lov->lov_tgts[loi->loi_ost_idx])
2095                                 continue;
2096                         if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2097                             data->lock->l_conn_export &&
2098                             ostid_res_name_eq(&loi->loi_oi, res_id)) {
2099                                 *stripe = i;
2100                                 GOTO(out, rc = 0);
2101                         }
2102                 }
2103                 LDLM_ERROR(data->lock, "lock on inode without such object");
2104                 dump_lsm(D_ERROR, lsm);
2105                 GOTO(out, rc = -ENXIO);
2106         } else if (KEY_IS(KEY_LAST_ID)) {
2107                 struct obd_id_info *info = val;
2108                 __u32 size = sizeof(obd_id);
2109                 struct lov_tgt_desc *tgt;
2110
2111                 LASSERT(*vallen == sizeof(struct obd_id_info));
2112                 tgt = lov->lov_tgts[info->idx];
2113
2114                 if (!tgt || !tgt->ltd_active)
2115                         GOTO(out, rc = -ESRCH);
2116
2117                 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2118                                   &size, info->data, NULL);
2119                 GOTO(out, rc = 0);
2120         } else if (KEY_IS(KEY_LOVDESC)) {
2121                 struct lov_desc *desc_ret = val;
2122                 *desc_ret = lov->desc;
2123
2124                 GOTO(out, rc = 0);
2125         } else if (KEY_IS(KEY_FIEMAP)) {
2126                 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
2127                 GOTO(out, rc);
2128         } else if (KEY_IS(KEY_CONNECT_FLAG)) {
2129                 struct lov_tgt_desc *tgt;
2130                 __u64 ost_idx = *((__u64*)val);
2131
2132                 LASSERT(*vallen == sizeof(__u64));
2133                 LASSERT(ost_idx < lov->desc.ld_tgt_count);
2134                 tgt = lov->lov_tgts[ost_idx];
2135
2136                 if (!tgt || !tgt->ltd_exp)
2137                         GOTO(out, rc = -ESRCH);
2138
2139                 *((__u64 *)val) = exp_connect_flags(tgt->ltd_exp);
2140                 GOTO(out, rc = 0);
2141         } else if (KEY_IS(KEY_TGT_COUNT)) {
2142                 *((int *)val) = lov->desc.ld_tgt_count;
2143                 GOTO(out, rc = 0);
2144         }
2145
2146         rc = -EINVAL;
2147
2148 out:
2149         obd_putref(obddev);
2150         RETURN(rc);
2151 }
2152
2153 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
2154                               obd_count keylen, void *key, obd_count vallen,
2155                               void *val, struct ptlrpc_request_set *set)
2156 {
2157         struct obd_device *obddev = class_exp2obd(exp);
2158         struct lov_obd *lov = &obddev->u.lov;
2159         obd_count count;
2160         int i, rc = 0, err;
2161         struct lov_tgt_desc *tgt;
2162         unsigned incr, check_uuid,
2163                  do_inactive, no_set;
2164         unsigned next_id = 0,  mds_con = 0, capa = 0;
2165         ENTRY;
2166
2167         incr = check_uuid = do_inactive = no_set = 0;
2168         if (set == NULL) {
2169                 no_set = 1;
2170                 set = ptlrpc_prep_set();
2171                 if (!set)
2172                         RETURN(-ENOMEM);
2173         }
2174
2175         obd_getref(obddev);
2176         count = lov->desc.ld_tgt_count;
2177
2178         if (KEY_IS(KEY_NEXT_ID)) {
2179                 count = vallen / sizeof(struct obd_id_info);
2180                 vallen = sizeof(obd_id);
2181                 incr = sizeof(struct obd_id_info);
2182                 do_inactive = 1;
2183                 next_id = 1;
2184         } else if (KEY_IS(KEY_CHECKSUM)) {
2185                 do_inactive = 1;
2186         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2187                 /* use defaults:  do_inactive = incr = 0; */
2188         } else if (KEY_IS(KEY_MDS_CONN)) {
2189                 mds_con = 1;
2190         } else if (KEY_IS(KEY_CAPA_KEY)) {
2191                 capa = 1;
2192         } else if (KEY_IS(KEY_CACHE_SET)) {
2193                 LASSERT(lov->lov_cache == NULL);
2194                 lov->lov_cache = val;
2195                 do_inactive = 1;
2196         }
2197
2198         for (i = 0; i < count; i++, val = (char *)val + incr) {
2199                 if (next_id) {
2200                         tgt = lov->lov_tgts[((struct obd_id_info*)val)->idx];
2201                 } else {
2202                         tgt = lov->lov_tgts[i];
2203                 }
2204                 /* OST was disconnected */
2205                 if (!tgt || !tgt->ltd_exp)
2206                         continue;
2207
2208                 /* OST is inactive and we don't want inactive OSCs */
2209                 if (!tgt->ltd_active && !do_inactive)
2210                         continue;
2211
2212                 if (mds_con) {
2213                         struct mds_group_info *mgi;
2214
2215                         LASSERT(vallen == sizeof(*mgi));
2216                         mgi = (struct mds_group_info *)val;
2217
2218                         /* Only want a specific OSC */
2219                         if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2220                                                 &tgt->ltd_uuid))
2221                                 continue;
2222
2223                         err = obd_set_info_async(env, tgt->ltd_exp,
2224                                          keylen, key, sizeof(int),
2225                                          &mgi->group, set);
2226                 } else if (next_id) {
2227                         err = obd_set_info_async(env, tgt->ltd_exp,
2228                                          keylen, key, vallen,
2229                                          ((struct obd_id_info*)val)->data, set);
2230                 } else if (capa) {
2231                         struct mds_capa_info *info = (struct mds_capa_info*)val;
2232
2233                         LASSERT(vallen == sizeof(*info));
2234
2235                          /* Only want a specific OSC */
2236                         if (info->uuid &&
2237                             !obd_uuid_equals(info->uuid, &tgt->ltd_uuid))
2238                                 continue;
2239
2240                         err = obd_set_info_async(env, tgt->ltd_exp, keylen,
2241                                                  key, sizeof(*info->capa),
2242                                                  info->capa, set);
2243                 } else {
2244                         /* Only want a specific OSC */
2245                         if (check_uuid &&
2246                             !obd_uuid_equals(val, &tgt->ltd_uuid))
2247                                 continue;
2248
2249                         err = obd_set_info_async(env, tgt->ltd_exp,
2250                                          keylen, key, vallen, val, set);
2251                 }
2252
2253                 if (!rc)
2254                         rc = err;
2255         }
2256
2257         obd_putref(obddev);
2258         if (no_set) {
2259                 err = ptlrpc_set_wait(set);
2260                 if (!rc)
2261                         rc = err;
2262                 ptlrpc_set_destroy(set);
2263         }
2264         RETURN(rc);
2265 }
2266
2267 void lov_stripe_lock(struct lov_stripe_md *md)
2268 __acquires(&md->lsm_lock)
2269 {
2270         LASSERT(md->lsm_lock_owner != current_pid());
2271         spin_lock(&md->lsm_lock);
2272         LASSERT(md->lsm_lock_owner == 0);
2273         md->lsm_lock_owner = current_pid();
2274 }
2275
2276 void lov_stripe_unlock(struct lov_stripe_md *md)
2277 __releases(&md->lsm_lock)
2278 {
2279         LASSERT(md->lsm_lock_owner == current_pid());
2280         md->lsm_lock_owner = 0;
2281         spin_unlock(&md->lsm_lock);
2282 }
2283
2284 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
2285                         struct obd_quotactl *oqctl)
2286 {
2287         struct lov_obd      *lov = &obd->u.lov;
2288         struct lov_tgt_desc *tgt;
2289         __u64                curspace = 0;
2290         __u64                bhardlimit = 0;
2291         int                  i, rc = 0;
2292         ENTRY;
2293
2294         if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
2295             oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
2296             oqctl->qc_cmd != Q_GETOQUOTA &&
2297             oqctl->qc_cmd != Q_INITQUOTA &&
2298             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
2299             oqctl->qc_cmd != Q_FINVALIDATE) {
2300                 CERROR("%s: bad quota opc %x for lov obd\n",
2301                        obd->obd_name, oqctl->qc_cmd);
2302                 RETURN(-EFAULT);
2303         }
2304
2305         /* for lov tgt */
2306         obd_getref(obd);
2307         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2308                 int err;
2309
2310                 tgt = lov->lov_tgts[i];
2311
2312                 if (!tgt)
2313                         continue;
2314
2315                 if (!tgt->ltd_active || tgt->ltd_reap) {
2316                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
2317                             lov->lov_tgts[i]->ltd_activate) {
2318                                 rc = -ENETDOWN;
2319                                 CERROR("ost %d is inactive\n", i);
2320                         } else {
2321                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
2322                         }
2323                         continue;
2324                 }
2325
2326                 err = obd_quotactl(tgt->ltd_exp, oqctl);
2327                 if (err) {
2328                         if (tgt->ltd_active && !rc)
2329                                 rc = err;
2330                         continue;
2331                 }
2332
2333                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2334                         curspace += oqctl->qc_dqblk.dqb_curspace;
2335                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2336                 }
2337         }
2338         obd_putref(obd);
2339
2340         if (oqctl->qc_cmd == Q_GETOQUOTA) {
2341                 oqctl->qc_dqblk.dqb_curspace = curspace;
2342                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2343         }
2344         RETURN(rc);
2345 }
2346
2347 static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2348                           struct obd_quotactl *oqctl)
2349 {
2350         struct lov_obd *lov = &obd->u.lov;
2351         int             i, rc = 0;
2352         ENTRY;
2353
2354         obd_getref(obd);
2355
2356         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2357                 if (!lov->lov_tgts[i])
2358                         continue;
2359
2360                 /* Skip quota check on the administratively disabled OSTs. */
2361                 if (!lov->lov_tgts[i]->ltd_activate) {
2362                         CWARN("lov idx %d was administratively disabled, "
2363                               "skip quotacheck on it.\n", i);
2364                         continue;
2365                 }
2366
2367                 if (!lov->lov_tgts[i]->ltd_active) {
2368                         CERROR("lov idx %d inactive\n", i);
2369                         rc = -EIO;
2370                         goto out;
2371                 }
2372         }
2373
2374         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2375                 int err;
2376
2377                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2378                         continue;
2379
2380                 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2381                 if (err && !rc)
2382                         rc = err;
2383         }
2384
2385 out:
2386         obd_putref(obd);
2387
2388         RETURN(rc);
2389 }
2390
2391 static struct obd_ops lov_obd_ops = {
2392         .o_owner                = THIS_MODULE,
2393         .o_setup                = lov_setup,
2394         .o_precleanup           = lov_precleanup,
2395         .o_cleanup              = lov_cleanup,
2396         .o_connect              = lov_connect,
2397         .o_disconnect           = lov_disconnect,
2398         .o_statfs               = lov_statfs,
2399         .o_statfs_async         = lov_statfs_async,
2400         .o_packmd               = lov_packmd,
2401         .o_unpackmd             = lov_unpackmd,
2402         .o_create               = lov_create,
2403         .o_destroy              = lov_destroy,
2404         .o_getattr_async        = lov_getattr_async,
2405         .o_setattr_async        = lov_setattr_async,
2406         .o_change_cbdata        = lov_change_cbdata,
2407         .o_find_cbdata          = lov_find_cbdata,
2408         .o_iocontrol            = lov_iocontrol,
2409         .o_get_info             = lov_get_info,
2410         .o_set_info_async       = lov_set_info_async,
2411         .o_notify               = lov_notify,
2412         .o_pool_new             = lov_pool_new,
2413         .o_pool_rem             = lov_pool_remove,
2414         .o_pool_add             = lov_pool_add,
2415         .o_pool_del             = lov_pool_del,
2416         .o_getref               = lov_getref,
2417         .o_putref               = lov_putref,
2418         .o_quotactl             = lov_quotactl,
2419         .o_quotacheck           = lov_quotacheck,
2420 };
2421
2422 struct kmem_cache *lov_oinfo_slab;
2423
2424 extern struct lu_kmem_descr lov_caches[];
2425
2426 int __init lov_init(void)
2427 {
2428         bool enable_proc = true;
2429         struct obd_type *type;
2430         int rc;
2431         ENTRY;
2432
2433         /* print an address of _any_ initialized kernel symbol from this
2434          * module, to allow debugging with gdb that doesn't support data
2435          * symbols from modules.*/
2436         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
2437
2438         rc = lu_kmem_init(lov_caches);
2439         if (rc)
2440                 return rc;
2441
2442         lov_oinfo_slab = kmem_cache_create("lov_oinfo",
2443                                            sizeof(struct lov_oinfo), 0,
2444                                            SLAB_HWCACHE_ALIGN, NULL);
2445         if (lov_oinfo_slab == NULL) {
2446                 lu_kmem_fini(lov_caches);
2447                 return -ENOMEM;
2448         }
2449
2450         type = class_search_type(LUSTRE_LOD_NAME);
2451         if (type != NULL && type->typ_procsym != NULL)
2452                 enable_proc = false;
2453
2454         rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
2455                                  LUSTRE_LOV_NAME, &lov_device_type);
2456
2457         if (rc) {
2458                 kmem_cache_destroy(lov_oinfo_slab);
2459                 lu_kmem_fini(lov_caches);
2460         }
2461
2462         RETURN(rc);
2463 }
2464
2465 static void /*__exit*/ lov_exit(void)
2466 {
2467         class_unregister_type(LUSTRE_LOV_NAME);
2468         kmem_cache_destroy(lov_oinfo_slab);
2469         lu_kmem_fini(lov_caches);
2470 }
2471
2472 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2473 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2474 MODULE_LICENSE("GPL");
2475
2476 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);