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