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