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