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