Whamcloud - gitweb
4a30926137ee45e6a5ad968788cb38557b56fad4
[fs/lustre-release.git] / lustre / lmv / lmv_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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LMV
41 #ifdef __KERNEL__
42 #include <linux/slab.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/pagemap.h>
47 #include <linux/mm.h>
48 #include <asm/div64.h>
49 #include <linux/seq_file.h>
50 #include <linux/namei.h>
51 #else
52 #include <liblustre.h>
53 #endif
54
55 #include <lustre/lustre_idl.h>
56 #include <lustre_log.h>
57 #include <obd_support.h>
58 #include <lustre_lib.h>
59 #include <lustre_net.h>
60 #include <obd_class.h>
61 #include <lprocfs_status.h>
62 #include <lustre_lite.h>
63 #include <lustre_fid.h>
64 #include "lmv_internal.h"
65
66 /* object cache. */
67 cfs_mem_cache_t *obj_cache;
68 atomic_t obj_cache_count = ATOMIC_INIT(0);
69
70 static void lmv_activate_target(struct lmv_obd *lmv,
71                                 struct lmv_tgt_desc *tgt,
72                                 int activate)
73 {
74         if (tgt->ltd_active == activate)
75                 return;
76
77         tgt->ltd_active = activate;
78         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
79 }
80
81 /* Error codes:
82  *
83  *  -EINVAL  : UUID can't be found in the LMV's target list
84  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
85  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
86  */
87 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
88                               int activate)
89 {
90         struct lmv_tgt_desc *tgt;
91         struct obd_device *obd;
92         int i, rc = 0;
93         ENTRY;
94
95         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
96                lmv, uuid->uuid, activate);
97
98         spin_lock(&lmv->lmv_lock);
99         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
100                 if (tgt->ltd_exp == NULL)
101                         continue;
102
103                 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
104                        i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
105
106                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
107                         break;
108         }
109
110         if (i == lmv->desc.ld_tgt_count)
111                 GOTO(out_lmv_lock, rc = -EINVAL);
112
113         obd = class_exp2obd(tgt->ltd_exp);
114         if (obd == NULL)
115                 GOTO(out_lmv_lock, rc = -ENOTCONN);
116
117         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
118                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
119                obd->obd_type->typ_name, i);
120         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
121
122         if (tgt->ltd_active == activate) {
123                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
124                        activate ? "" : "in");
125                 GOTO(out_lmv_lock, rc);
126         }
127
128         CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
129                obd, activate ? "" : "in");
130
131         lmv_activate_target(lmv, tgt, activate);
132
133         EXIT;
134
135  out_lmv_lock:
136         spin_unlock(&lmv->lmv_lock);
137         return rc;
138 }
139
140 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
141                             struct obd_connect_data *data)
142 {
143         struct lmv_tgt_desc *tgt;
144         int i;
145         ENTRY;
146
147         LASSERT(data != NULL);
148
149         spin_lock(&lmv->lmv_lock);
150         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
151                 if (tgt->ltd_exp == NULL)
152                         continue;
153
154                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
155                         lmv->datas[tgt->ltd_idx] = *data;
156                         break;
157                 }
158         }
159         spin_unlock(&lmv->lmv_lock);
160         RETURN(0);
161 }
162
163 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
164         struct obd_device *obd = exp->exp_obd;
165         struct lmv_obd *lmv = &obd->u.lmv;
166         return obd_get_uuid(lmv->tgts[0].ltd_exp);
167 }
168
169 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
170                       enum obd_notify_event ev, void *data)
171 {
172         struct lmv_obd *lmv = &obd->u.lmv;
173         struct obd_uuid *uuid;
174         int rc = 0;
175         ENTRY;
176
177         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
178                 CERROR("unexpected notification of %s %s!\n",
179                        watched->obd_type->typ_name,
180                        watched->obd_name);
181                 RETURN(-EINVAL);
182         }
183
184         uuid = &watched->u.cli.cl_target_uuid;
185         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
186                 /*
187                  * Set MDC as active before notifying the observer, so the
188                  * observer can use the MDC normally.
189                  */
190                 rc = lmv_set_mdc_active(lmv, uuid,
191                                         ev == OBD_NOTIFY_ACTIVE);
192                 if (rc) {
193                         CERROR("%sactivation of %s failed: %d\n",
194                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
195                                uuid->uuid, rc);
196                         RETURN(rc);
197                 }
198         } else if (ev == OBD_NOTIFY_OCD) {
199                 struct obd_connect_data *conn_data =
200                         &watched->u.cli.cl_import->imp_connect_data;
201
202                 /* Set connect data to desired target, update
203                  * exp_connect_flags. */
204                 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
205                 if (rc) {
206                         CERROR("can't set connect data to target %s, rc %d\n",
207                                uuid->uuid, rc);
208                         RETURN(rc);
209                 }
210
211                 /*
212                  * XXX: Make sure that ocd_connect_flags from all targets are
213                  * the same. Otherwise one of MDTs runs wrong version or
214                  * something like this.  --umka
215                  */
216                 obd->obd_self_export->exp_connect_flags =
217                         conn_data->ocd_connect_flags;
218         }
219 #if 0
220         else if (ev == OBD_NOTIFY_DISCON) {
221                 /* For disconnect event, flush fld cache for failout MDS case. */
222                 fld_client_flush(&lmv->lmv_fld);
223         }
224 #endif
225         /* Pass the notification up the chain. */
226         if (obd->obd_observer)
227                 rc = obd_notify(obd->obd_observer, watched, ev, data);
228
229         RETURN(rc);
230 }
231
232 /* this is fake connect function. Its purpose is to initialize lmv and say
233  * caller that everything is okay. Real connection will be performed later. */
234 static int lmv_connect(const struct lu_env *env,
235                        struct lustre_handle *conn, struct obd_device *obd,
236                        struct obd_uuid *cluuid, struct obd_connect_data *data,
237                        void *localdata)
238 {
239 #ifdef __KERNEL__
240         struct proc_dir_entry *lmv_proc_dir;
241 #endif
242         struct lmv_obd *lmv = &obd->u.lmv;
243         struct obd_export *exp;
244         int rc = 0;
245         ENTRY;
246
247         rc = class_connect(conn, obd, cluuid);
248         if (rc) {
249                 CERROR("class_connection() returned %d\n", rc);
250                 RETURN(rc);
251         }
252
253         exp = class_conn2export(conn);
254
255         /* we don't want to actually do the underlying connections more than
256          * once, so keep track. */
257         lmv->refcount++;
258         if (lmv->refcount > 1) {
259                 class_export_put(exp);
260                 RETURN(0);
261         }
262
263         lmv->exp = exp;
264         lmv->connected = 0;
265         lmv->cluuid = *cluuid;
266
267         if (data)
268                 lmv->conn_data = *data;
269
270 #ifdef __KERNEL__
271         lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
272                                         NULL, NULL);
273         if (IS_ERR(lmv_proc_dir)) {
274                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
275                        obd->obd_type->typ_name, obd->obd_name);
276                 lmv_proc_dir = NULL;
277         }
278 #endif
279
280         /* all real clients should perform actual connection right away, because
281          * it is possible, that LMV will not have opportunity to connect targets
282          * and MDC stuff will be called directly, for instance while reading
283          * ../mdc/../kbytesfree procfs file, etc. */
284         if (data->ocd_connect_flags & OBD_CONNECT_REAL)
285                 rc = lmv_check_connect(obd);
286
287 #ifdef __KERNEL__
288         if (rc) {
289                 if (lmv_proc_dir)
290                         lprocfs_remove(&lmv_proc_dir);
291         }
292 #endif
293
294         RETURN(rc);
295 }
296
297 static void lmv_set_timeouts(struct obd_device *obd)
298 {
299         struct lmv_tgt_desc *tgts;
300         struct lmv_obd *lmv;
301         int i;
302
303         lmv = &obd->u.lmv;
304         if (lmv->server_timeout == 0)
305                 return;
306
307         if (lmv->connected == 0)
308                 return;
309
310         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
311                 if (tgts->ltd_exp == NULL)
312                         continue;
313
314                 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
315                                    KEY_INTERMDS, 0, NULL, NULL);
316         }
317 }
318
319 static int lmv_init_ea_size(struct obd_export *exp, int easize,
320                             int def_easize, int cookiesize)
321 {
322         struct obd_device *obd = exp->exp_obd;
323         struct lmv_obd *lmv = &obd->u.lmv;
324         int i, rc = 0, change = 0;
325         ENTRY;
326
327         if (lmv->max_easize < easize) {
328                 lmv->max_easize = easize;
329                 change = 1;
330         }
331         if (lmv->max_def_easize < def_easize) {
332                 lmv->max_def_easize = def_easize;
333                 change = 1;
334         }
335         if (lmv->max_cookiesize < cookiesize) {
336                 lmv->max_cookiesize = cookiesize;
337                 change = 1;
338         }
339         if (change == 0)
340                 RETURN(0);
341
342         if (lmv->connected == 0)
343                 RETURN(0);
344
345         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
346                 if (lmv->tgts[i].ltd_exp == NULL) {
347                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
348                         continue;
349                 }
350
351                 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
352                                      cookiesize);
353                 if (rc) {
354                         CERROR("obd_init_ea_size() failed on MDT target %d, "
355                                "error %d.\n", i, rc);
356                         break;
357                 }
358         }
359         RETURN(rc);
360 }
361
362 #define MAX_STRING_SIZE 128
363
364 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
365 {
366         struct lmv_obd *lmv = &obd->u.lmv;
367         struct obd_uuid *cluuid = &lmv->cluuid;
368         struct obd_connect_data *mdc_data = NULL;
369         struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
370         struct lustre_handle conn = {0, };
371         struct obd_device *mdc_obd;
372         struct obd_export *mdc_exp;
373         struct lu_fld_target target;
374         int rc;
375 #ifdef __KERNEL__
376         struct proc_dir_entry *lmv_proc_dir;
377 #endif
378         ENTRY;
379
380         /* for MDS: don't connect to yourself */
381         if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
382                 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
383                 /* XXX - the old code didn't increment active tgt count.
384                  *       should we ? */
385                 RETURN(0);
386         }
387
388         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
389                                         &obd->obd_uuid);
390         if (!mdc_obd) {
391                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
392                 RETURN(-EINVAL);
393         }
394
395         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
396                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
397                 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
398                 cluuid->uuid);
399
400         if (!mdc_obd->obd_set_up) {
401                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
402                 RETURN(-EINVAL);
403         }
404
405         rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
406                          &lmv->conn_data, NULL);
407         if (rc) {
408                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
409                 RETURN(rc);
410         }
411
412         mdc_exp = class_conn2export(&conn);
413
414         /* Init fid sequence client for this mdc. */
415         rc = obd_fid_init(mdc_exp);
416         if (rc)
417                 RETURN(rc);
418
419         /* Add new FLD target. */
420         target.ft_srv = NULL;
421         target.ft_exp = mdc_exp;
422         target.ft_idx = tgt->ltd_idx;
423
424         fld_client_add_target(&lmv->lmv_fld, &target);
425
426         mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
427
428         rc = obd_register_observer(mdc_obd, obd);
429         if (rc) {
430                 obd_disconnect(mdc_exp);
431                 CERROR("target %s register_observer error %d\n",
432                        tgt->ltd_uuid.uuid, rc);
433                 RETURN(rc);
434         }
435
436         if (obd->obd_observer) {
437                 /* tell the mds_lmv about the new target */
438                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
439                                 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
440                 if (rc) {
441                         obd_disconnect(mdc_exp);
442                         RETURN(rc);
443                 }
444         }
445
446         tgt->ltd_active = 1;
447         tgt->ltd_exp = mdc_exp;
448         lmv->desc.ld_active_tgt_count++;
449
450         /* copy connect data, it may be used later */
451         lmv->datas[tgt->ltd_idx] = *mdc_data;
452
453         md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
454                         lmv->max_def_easize, lmv->max_cookiesize);
455
456         CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
457                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
458                 atomic_read(&obd->obd_refcount));
459
460 #ifdef __KERNEL__
461         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
462         if (lmv_proc_dir) {
463                 struct proc_dir_entry *mdc_symlink;
464                 char name[MAX_STRING_SIZE + 1];
465
466                 LASSERT(mdc_obd->obd_type != NULL);
467                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
468                 name[MAX_STRING_SIZE] = '\0';
469                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
470                          mdc_obd->obd_type->typ_name,
471                          mdc_obd->obd_name);
472                 mdc_symlink = proc_symlink(mdc_obd->obd_name,
473                                            lmv_proc_dir, name);
474                 if (mdc_symlink == NULL) {
475                         CERROR("could not register LMV target "
476                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
477                                obd->obd_type->typ_name, obd->obd_name,
478                                mdc_obd->obd_name);
479                         lprocfs_remove(&lmv_proc_dir);
480                         lmv_proc_dir = NULL;
481                 }
482         }
483 #endif
484         RETURN(0);
485 }
486
487 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
488 {
489         struct lmv_obd *lmv = &obd->u.lmv;
490         struct lmv_tgt_desc *tgt;
491         int rc = 0;
492         ENTRY;
493
494         CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
495
496         lmv_init_lock(lmv);
497
498         if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
499                 lmv_init_unlock(lmv);
500                 CERROR("can't add %s, LMV module compiled for %d MDCs. "
501                        "That many MDCs already configured.\n",
502                        tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
503                 RETURN(-EINVAL);
504         }
505         if (lmv->desc.ld_tgt_count == 0) {
506                 struct obd_device *mdc_obd;
507
508                 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
509                                                 &obd->obd_uuid);
510                 if (!mdc_obd) {
511                         lmv_init_unlock(lmv);
512                         CERROR("Target %s not attached\n", tgt_uuid->uuid);
513                         RETURN(-EINVAL);
514                 }
515
516                 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
517                 if (rc) {
518                         lmv_init_unlock(lmv);
519                         CERROR("lmv failed to setup llogging subsystems\n");
520                 }
521         }
522         spin_lock(&lmv->lmv_lock);
523         tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
524         tgt->ltd_uuid = *tgt_uuid;
525         spin_unlock(&lmv->lmv_lock);
526
527         if (lmv->connected) {
528                 rc = lmv_connect_mdc(obd, tgt);
529                 if (rc) {
530                         spin_lock(&lmv->lmv_lock);
531                         lmv->desc.ld_tgt_count--;
532                         memset(tgt, 0, sizeof(*tgt));
533                         spin_unlock(&lmv->lmv_lock);
534                 } else {
535                         int easize = sizeof(struct lmv_stripe_md) +
536                                      lmv->desc.ld_tgt_count *
537                                      sizeof(struct lu_fid);
538                         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
539                 }
540         }
541
542         lmv_init_unlock(lmv);
543         RETURN(rc);
544 }
545
546 /* performs a check if passed obd is connected. If no - connect it. */
547 int lmv_check_connect(struct obd_device *obd)
548 {
549         struct lmv_obd *lmv = &obd->u.lmv;
550         struct lmv_tgt_desc *tgt;
551         int i, rc, easize;
552         ENTRY;
553
554         if (lmv->connected)
555                 RETURN(0);
556
557         lmv_init_lock(lmv);
558         if (lmv->connected) {
559                 lmv_init_unlock(lmv);
560                 RETURN(0);
561         }
562
563         if (lmv->desc.ld_tgt_count == 0) {
564                 CERROR("%s: no targets configured.\n", obd->obd_name);
565                 RETURN(-EINVAL);
566         }
567
568         CDEBUG(D_CONFIG, "time to connect %s to %s\n",
569                lmv->cluuid.uuid, obd->obd_name);
570
571         LASSERT(lmv->tgts != NULL);
572
573         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
574                 rc = lmv_connect_mdc(obd, tgt);
575                 if (rc)
576                         GOTO(out_disc, rc);
577         }
578
579         lmv_set_timeouts(obd);
580         class_export_put(lmv->exp);
581         lmv->connected = 1;
582         easize = lmv_get_easize(lmv);
583         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
584         lmv_init_unlock(lmv);
585         RETURN(0);
586
587  out_disc:
588         while (i-- > 0) {
589                 int rc2;
590                 --tgt;
591                 tgt->ltd_active = 0;
592                 if (tgt->ltd_exp) {
593                         --lmv->desc.ld_active_tgt_count;
594                         rc2 = obd_disconnect(tgt->ltd_exp);
595                         if (rc2) {
596                                 CERROR("error: LMV target %s disconnect on "
597                                        "MDC idx %d: error %d\n",
598                                        tgt->ltd_uuid.uuid, i, rc2);
599                         }
600                 }
601         }
602         class_disconnect(lmv->exp);
603         lmv_init_unlock(lmv);
604         RETURN(rc);
605 }
606
607 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
608 {
609 #ifdef __KERNEL__
610         struct proc_dir_entry *lmv_proc_dir;
611 #endif
612         struct lmv_obd *lmv = &obd->u.lmv;
613         struct obd_device *mdc_obd;
614         int rc;
615         ENTRY;
616
617         LASSERT(tgt != NULL);
618         LASSERT(obd != NULL);
619
620         mdc_obd = class_exp2obd(tgt->ltd_exp);
621
622         if (mdc_obd)
623                 mdc_obd->obd_no_recov = obd->obd_no_recov;
624
625 #ifdef __KERNEL__
626         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
627         if (lmv_proc_dir) {
628                 struct proc_dir_entry *mdc_symlink;
629
630                 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
631                 if (mdc_symlink) {
632                         lprocfs_remove(&mdc_symlink);
633                 } else {
634                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
635                                obd->obd_type->typ_name, obd->obd_name,
636                                mdc_obd->obd_name);
637                 }
638         }
639 #endif
640         rc = obd_fid_fini(tgt->ltd_exp);
641         if (rc)
642                 CERROR("Can't finanize fids factory\n");
643
644         CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
645                tgt->ltd_exp->exp_obd->obd_name,
646                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
647
648         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
649         rc = obd_disconnect(tgt->ltd_exp);
650         if (rc) {
651                 if (tgt->ltd_active) {
652                         CERROR("Target %s disconnect error %d\n",
653                                tgt->ltd_uuid.uuid, rc);
654                 }
655         }
656
657         lmv_activate_target(lmv, tgt, 0);
658         tgt->ltd_exp = NULL;
659         RETURN(0);
660 }
661
662 static int lmv_disconnect(struct obd_export *exp)
663 {
664         struct obd_device *obd = class_exp2obd(exp);
665 #ifdef __KERNEL__
666         struct proc_dir_entry *lmv_proc_dir;
667 #endif
668         struct lmv_obd *lmv = &obd->u.lmv;
669         int rc, i;
670         ENTRY;
671
672         if (!lmv->tgts)
673                 goto out_local;
674
675         /* Only disconnect the underlying layers on the final disconnect. */
676         lmv->refcount--;
677         if (lmv->refcount != 0)
678                 goto out_local;
679
680         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
681                 if (lmv->tgts[i].ltd_exp == NULL)
682                         continue;
683                 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
684         }
685
686 #ifdef __KERNEL__
687         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
688         if (lmv_proc_dir) {
689                 lprocfs_remove(&lmv_proc_dir);
690         } else {
691                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
692                        obd->obd_type->typ_name, obd->obd_name);
693         }
694 #endif
695
696 out_local:
697         /*
698          * This is the case when no real connection is established by
699          * lmv_check_connect().
700          */
701         if (!lmv->connected)
702                 class_export_put(exp);
703         rc = class_disconnect(exp);
704         if (lmv->refcount == 0)
705                 lmv->connected = 0;
706         RETURN(rc);
707 }
708
709 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
710                          int len, void *karg, void *uarg)
711 {
712         struct obd_device *obddev = class_exp2obd(exp);
713         struct lmv_obd *lmv = &obddev->u.lmv;
714         int i, rc = 0, set = 0;
715         ENTRY;
716
717         if (lmv->desc.ld_tgt_count == 0)
718                 RETURN(-ENOTTY);
719
720         switch (cmd) {
721         case IOC_OBD_STATFS: {
722                 struct obd_ioctl_data *data = karg;
723                 struct obd_device *mdc_obd;
724                 struct obd_statfs stat_buf = {0};
725                 __u32 index;
726
727                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
728                 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
729
730                 if ((index >= lmv->desc.ld_tgt_count))
731                         RETURN(-ENODEV);
732
733                 if (!lmv->tgts[index].ltd_active)
734                         RETURN(-ENODATA);
735
736                 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
737                 if (!mdc_obd)
738                         RETURN(-EINVAL);
739
740                 /* got statfs data */
741                 rc = obd_statfs(mdc_obd, &stat_buf,
742                                 cfs_time_current_64() - HZ, 0);
743                 if (rc)
744                         RETURN(rc);
745                 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
746                         RETURN(rc);
747                 /* copy UUID */
748                 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
749                                   data->ioc_plen2);
750                 break;
751         }
752         default : {
753                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
754                         int err;
755
756                         if (lmv->tgts[i].ltd_exp == NULL)
757                                 continue;
758
759                         err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
760                                             karg, uarg);
761                         if (err) {
762                                 if (lmv->tgts[i].ltd_active) {
763                                         CERROR("error: iocontrol MDC %s on MDT"
764                                                "idx %d cmd %x: err = %d\n",
765                                                 lmv->tgts[i].ltd_uuid.uuid,
766                                                 i, cmd, err);
767                                         if (!rc)
768                                                 rc = err;
769                                 }
770                         } else
771                                 set = 1;
772                 }
773                 if (!set && !rc)
774                         rc = -EIO;
775         }
776         }
777         RETURN(rc);
778 }
779
780 enum MDS_POLICY {
781      CHAR_TYPE,
782      NID_TYPE
783 };
784
785 static int lmv_all_chars_policy(int count, const char *name,
786                                 int len)
787 {
788         unsigned int c = 0;
789
790         while (len > 0)
791                 c += name[--len];
792         c = c % count;
793         return c;
794 }
795
796 static int lmv_nid_policy(struct lmv_obd *lmv)
797 {
798         struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
799         __u32 id;
800         /*
801          * XXX Hack: to get nid we assume that underlying obd device is mdc.
802          */
803         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
804         return id % lmv->desc.ld_tgt_count;
805 }
806
807 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
808                           int type)
809 {
810         switch (type) {
811         case CHAR_TYPE:
812                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
813                                             op_data->op_name,
814                                             op_data->op_namelen);
815         case NID_TYPE:
816                 return lmv_nid_policy(lmv);
817
818         default:
819                 break;
820         }
821
822         CERROR("unsupport type %d \n", type);
823         return -EINVAL;
824 }
825
826 /* This is _inode_ placement policy function (not name). */
827 static int lmv_placement_policy(struct obd_device *obd,
828                                 struct md_op_data *op_data,
829                                 mdsno_t *mds)
830 {
831         struct lmv_obd *lmv = &obd->u.lmv;
832         struct lmv_obj *obj;
833         int rc;
834         ENTRY;
835
836         LASSERT(mds != NULL);
837
838         /*
839          * Allocate new fid on target according to operation type and parent
840          * home mds.
841          */
842         obj = lmv_obj_grab(obd, &op_data->op_fid1);
843         if (obj != NULL || op_data->op_name == NULL ||
844             op_data->op_opc != LUSTRE_OPC_MKDIR) {
845                 /*
846                  * Allocate fid for non-dir or for null name or for case parent
847                  * dir is split.
848                  */
849                 if (obj) {
850                         lmv_obj_put(obj);
851
852                         /*
853                          * If we have this flag turned on, and we see that
854                          * parent dir is split, this means, that caller did not
855                          * notice split yet. This is race and we would like to
856                          * let caller know that.
857                          */
858                         if (op_data->op_bias & MDS_CHECK_SPLIT)
859                                 RETURN(-ERESTART);
860                 }
861
862                 /*
863                  * Allocate new fid on same mds where parent fid is located and
864                  * where operation will be sent. In case of split dir, ->op_fid1
865                  * and ->op_mds here will contain fid and mds of slave directory
866                  * object (assigned by caller).
867                  */
868                 *mds = op_data->op_mds;
869                 rc = 0;
870
871 #if 0
872                 /* XXX: This should be removed later wehn we sure it is not
873                  * needed. */
874                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
875                 if (rc)
876                         GOTO(out, rc);
877 #endif
878         } else {
879                 /*
880                  * Parent directory is not split and we want to create a
881                  * directory in it. Let's calculate where to place it according
882                  * to name.
883                  */
884                 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
885                 rc = 0;
886         }
887         EXIT;
888 #if 0
889 out:
890 #endif
891         if (rc) {
892                 CERROR("Can't choose MDS, err = %d\n", rc);
893         } else {
894                 LASSERT(*mds < lmv->desc.ld_tgt_count);
895         }
896
897         return rc;
898 }
899
900 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
901                     mdsno_t mds)
902 {
903         struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
904         int rc;
905         ENTRY;
906
907         /* New seq alloc and FLD setup should be atomic. */
908         down(&tgt->ltd_fid_sem);
909
910         /* Asking underlaying tgt layer to allocate new fid. */
911         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
912         if (rc > 0) {
913                 LASSERT(fid_is_sane(fid));
914
915                 /* Client switches to new sequence, setup FLD. */
916                 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
917                                        mds, NULL);
918                 if (rc) {
919                         CERROR("Can't create fld entry, rc %d\n", rc);
920                         /* Delete just allocated fid sequence */
921                         obd_fid_delete(tgt->ltd_exp, NULL);
922                 }
923         }
924         up(&tgt->ltd_fid_sem);
925         RETURN(rc);
926 }
927
928 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
929                   struct md_op_data *op_data)
930 {
931         struct obd_device *obd = class_exp2obd(exp);
932         struct lmv_obd *lmv = &obd->u.lmv;
933         mdsno_t mds;
934         int rc;
935         ENTRY;
936
937         LASSERT(op_data != NULL);
938         LASSERT(fid != NULL);
939
940         rc = lmv_placement_policy(obd, op_data, &mds);
941         if (rc) {
942                 CERROR("Can't get target for allocating fid, "
943                        "rc %d\n", rc);
944                 RETURN(rc);
945         }
946
947         rc = __lmv_fid_alloc(lmv, fid, mds);
948         if (rc) {
949                 CERROR("Can't alloc new fid, rc %d\n", rc);
950                 RETURN(rc);
951         }
952
953         RETURN(rc);
954 }
955
956 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
957 {
958         ENTRY;
959
960         LASSERT(exp && fid);
961         if (lmv_obj_delete(exp, fid)) {
962                 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
963                        PFID(fid));
964         }
965         RETURN(0);
966 }
967
968 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
969 {
970         struct lmv_obd *lmv = &obd->u.lmv;
971         struct lprocfs_static_vars lvars;
972         struct lmv_desc *desc;
973         int rc, i = 0;
974         ENTRY;
975
976         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
977                 CERROR("LMV setup requires a descriptor\n");
978                 RETURN(-EINVAL);
979         }
980
981         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
982         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
983                 CERROR("descriptor size wrong: %d > %d\n",
984                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
985                 RETURN(-EINVAL);
986         }
987
988         lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
989
990         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
991         if (lmv->tgts == NULL)
992                 RETURN(-ENOMEM);
993
994         for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
995                 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
996                 lmv->tgts[i].ltd_idx = i;
997         }
998
999         lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1000
1001         OBD_ALLOC(lmv->datas, lmv->datas_size);
1002         if (lmv->datas == NULL)
1003                 GOTO(out_free_tgts, rc = -ENOMEM);
1004
1005         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1006         lmv->desc.ld_tgt_count = 0;
1007         lmv->desc.ld_active_tgt_count = 0;
1008         lmv->max_cookiesize = 0;
1009         lmv->max_def_easize = 0;
1010         lmv->max_easize = 0;
1011
1012         spin_lock_init(&lmv->lmv_lock);
1013         sema_init(&lmv->init_sem, 1);
1014
1015         rc = lmv_obj_setup(obd);
1016         if (rc) {
1017                 CERROR("Can't setup LMV object manager, "
1018                        "error %d.\n", rc);
1019                 GOTO(out_free_datas, rc);
1020         }
1021
1022         lprocfs_lmv_init_vars(&lvars);
1023         lprocfs_obd_setup(obd, lvars.obd_vars);
1024 #ifdef LPROCFS
1025         {
1026                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1027                                         0444, &lmv_proc_target_fops, obd);
1028                 if (rc)
1029                         CWARN("Error adding the target_obd_status file\n");
1030        }
1031 #endif
1032         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1033                              LUSTRE_CLI_FLD_HASH_DHT);
1034         if (rc) {
1035                 CERROR("can't init FLD, err %d\n",
1036                        rc);
1037                 GOTO(out_free_datas, rc);
1038         }
1039
1040         RETURN(0);
1041
1042 out_free_datas:
1043         OBD_FREE(lmv->datas, lmv->datas_size);
1044         lmv->datas = NULL;
1045 out_free_tgts:
1046         OBD_FREE(lmv->tgts, lmv->tgts_size);
1047         lmv->tgts = NULL;
1048         return rc;
1049 }
1050
1051 static int lmv_cleanup(struct obd_device *obd)
1052 {
1053         struct lmv_obd *lmv = &obd->u.lmv;
1054         ENTRY;
1055
1056         fld_client_fini(&lmv->lmv_fld);
1057         lprocfs_obd_cleanup(obd);
1058         lmv_obj_cleanup(obd);
1059         OBD_FREE(lmv->datas, lmv->datas_size);
1060         OBD_FREE(lmv->tgts, lmv->tgts_size);
1061
1062         RETURN(0);
1063 }
1064
1065 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1066 {
1067         struct lustre_cfg *lcfg = buf;
1068         struct obd_uuid tgt_uuid;
1069         int rc;
1070         ENTRY;
1071
1072         switch(lcfg->lcfg_command) {
1073         case LCFG_ADD_MDC:
1074                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1075                         GOTO(out, rc = -EINVAL);
1076
1077                 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1078                 rc = lmv_add_target(obd, &tgt_uuid);
1079                 GOTO(out, rc);
1080         default: {
1081                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1082                 GOTO(out, rc = -EINVAL);
1083         }
1084         }
1085 out:
1086         RETURN(rc);
1087 }
1088
1089 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1090                       __u64 max_age, __u32 flags)
1091 {
1092         struct lmv_obd *lmv = &obd->u.lmv;
1093         struct obd_statfs *temp;
1094         int rc = 0, i;
1095         ENTRY;
1096
1097         rc = lmv_check_connect(obd);
1098         if (rc)
1099                 RETURN(rc);
1100
1101         OBD_ALLOC(temp, sizeof(*temp));
1102         if (temp == NULL)
1103                 RETURN(-ENOMEM);
1104
1105         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1106                 if (lmv->tgts[i].ltd_exp == NULL)
1107                         continue;
1108
1109                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1110                                 max_age, flags);
1111                 if (rc) {
1112                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1113                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1114                                rc);
1115                         GOTO(out_free_temp, rc);
1116                 }
1117                 if (i == 0) {
1118                         *osfs = *temp;
1119                 } else {
1120                         osfs->os_bavail += temp->os_bavail;
1121                         osfs->os_blocks += temp->os_blocks;
1122                         osfs->os_ffree += temp->os_ffree;
1123                         osfs->os_files += temp->os_files;
1124                 }
1125         }
1126
1127         EXIT;
1128 out_free_temp:
1129         OBD_FREE(temp, sizeof(*temp));
1130         return rc;
1131 }
1132
1133 static int lmv_getstatus(struct obd_export *exp,
1134                          struct lu_fid *fid,
1135                          struct obd_capa **pc)
1136 {
1137         struct obd_device *obd = exp->exp_obd;
1138         struct lmv_obd *lmv = &obd->u.lmv;
1139         int rc;
1140         ENTRY;
1141
1142         rc = lmv_check_connect(obd);
1143         if (rc)
1144                 RETURN(rc);
1145
1146         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1147
1148         RETURN(rc);
1149 }
1150
1151 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1152                         struct obd_capa *oc, obd_valid valid, const char *name,
1153                         const char *input, int input_size, int output_size,
1154                         int flags, struct ptlrpc_request **request)
1155 {
1156         struct obd_device *obd = exp->exp_obd;
1157         struct lmv_obd *lmv = &obd->u.lmv;
1158         struct obd_export *tgt_exp;
1159         int rc;
1160         ENTRY;
1161
1162         rc = lmv_check_connect(obd);
1163         if (rc)
1164                 RETURN(rc);
1165
1166         tgt_exp = lmv_find_export(lmv, fid);
1167         if (IS_ERR(tgt_exp))
1168                 RETURN(PTR_ERR(tgt_exp));
1169
1170         rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1171                          output_size, flags, request);
1172
1173         RETURN(rc);
1174 }
1175
1176 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1177                         struct obd_capa *oc, obd_valid valid, const char *name,
1178                         const char *input, int input_size, int output_size,
1179                         int flags, __u32 suppgid,
1180                         struct ptlrpc_request **request)
1181 {
1182         struct obd_device *obd = exp->exp_obd;
1183         struct lmv_obd *lmv = &obd->u.lmv;
1184         struct obd_export *tgt_exp;
1185         int rc;
1186         ENTRY;
1187
1188         rc = lmv_check_connect(obd);
1189         if (rc)
1190                 RETURN(rc);
1191
1192         tgt_exp = lmv_find_export(lmv, fid);
1193         if (IS_ERR(tgt_exp))
1194                 RETURN(PTR_ERR(tgt_exp));
1195
1196         rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1197                          input, input_size, output_size, flags, suppgid,
1198                          request);
1199
1200         RETURN(rc);
1201 }
1202
1203 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1204                        struct obd_capa *oc, obd_valid valid, int ea_size,
1205                        struct ptlrpc_request **request)
1206 {
1207         struct obd_device *obd = exp->exp_obd;
1208         struct lmv_obd *lmv = &obd->u.lmv;
1209         struct obd_export *tgt_exp;
1210         struct lmv_obj *obj;
1211         int rc, i;
1212         ENTRY;
1213
1214         rc = lmv_check_connect(obd);
1215         if (rc)
1216                 RETURN(rc);
1217
1218         tgt_exp = lmv_find_export(lmv, fid);
1219         if (IS_ERR(tgt_exp))
1220                 RETURN(PTR_ERR(tgt_exp));
1221
1222         rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1223         if (rc)
1224                 RETURN(rc);
1225
1226         obj = lmv_obj_grab(obd, fid);
1227
1228         CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1229                obj ? "(split)" : "");
1230
1231         /*
1232          * If object is split, then we loop over all the slaves and gather size
1233          * attribute. In ideal world we would have to gather also mds field from
1234          * all slaves, as object is spread over the cluster and this is
1235          * definitely interesting information and it is not good to loss it,
1236          * but...
1237          */
1238         if (obj) {
1239                 struct mdt_body *body;
1240
1241                 if (*request == NULL) {
1242                         lmv_obj_put(obj);
1243                         RETURN(rc);
1244                 }
1245
1246                 body = req_capsule_server_get(&(*request)->rq_pill,
1247                                               &RMF_MDT_BODY);
1248                 LASSERT(body != NULL);
1249
1250                 lmv_obj_lock(obj);
1251
1252                 for (i = 0; i < obj->lo_objcount; i++) {
1253                         if (lmv->tgts[i].ltd_exp == NULL) {
1254                                 CWARN("%s: NULL export for %d\n",
1255                                       obd->obd_name, i);
1256                                 continue;
1257                         }
1258
1259                         /* skip master obj. */
1260                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1261                                 continue;
1262
1263                         lmv_update_body(body, &obj->lo_inodes[i]);
1264                 }
1265
1266                 lmv_obj_unlock(obj);
1267                 lmv_obj_put(obj);
1268         }
1269
1270         RETURN(rc);
1271 }
1272
1273 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1274                              ldlm_iterator_t it, void *data)
1275 {
1276         struct obd_device *obd = exp->exp_obd;
1277         struct lmv_obd *lmv = &obd->u.lmv;
1278         int i, rc;
1279         ENTRY;
1280
1281         rc = lmv_check_connect(obd);
1282         if (rc)
1283                 RETURN(rc);
1284
1285         CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1286
1287         /*
1288          * With CMD every object can have two locks in different namespaces:
1289          * lookup lock in space of mds storing direntry and update/open lock in
1290          * space of mds storing inode.
1291          */
1292         for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1293                 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1294
1295         RETURN(0);
1296 }
1297
1298 static int lmv_close(struct obd_export *exp,
1299                      struct md_op_data *op_data,
1300                      struct md_open_data *mod,
1301                      struct ptlrpc_request **request)
1302 {
1303         struct obd_device *obd = exp->exp_obd;
1304         struct lmv_obd *lmv = &obd->u.lmv;
1305         struct obd_export *tgt_exp;
1306         int rc;
1307         ENTRY;
1308
1309         rc = lmv_check_connect(obd);
1310         if (rc)
1311                 RETURN(rc);
1312
1313         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1314         if (IS_ERR(tgt_exp))
1315                 RETURN(PTR_ERR(tgt_exp));
1316
1317         CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1318         rc = md_close(tgt_exp, op_data, mod, request);
1319         RETURN(rc);
1320 }
1321
1322 /*
1323  * Called in the case MDS returns -ERESTART on create on open, what means that
1324  * directory is split and its LMV presentation object has to be updated.
1325  */
1326 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1327 {
1328         struct obd_device *obd = exp->exp_obd;
1329         struct lmv_obd *lmv = &obd->u.lmv;
1330         struct ptlrpc_request *req = NULL;
1331         struct obd_export *tgt_exp;
1332         struct lmv_obj *obj;
1333         struct lustre_md md;
1334         int mealen, rc;
1335         __u64 valid;
1336         ENTRY;
1337
1338         md.mea = NULL;
1339         mealen = lmv_get_easize(lmv);
1340
1341         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1342
1343         tgt_exp = lmv_find_export(lmv, fid);
1344         if (IS_ERR(tgt_exp))
1345                 RETURN(PTR_ERR(tgt_exp));
1346
1347         /* time to update mea of parent fid */
1348         rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1349         if (rc) {
1350                 CERROR("md_getattr() failed, error %d\n", rc);
1351                 GOTO(cleanup, rc);
1352         }
1353
1354         rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1355         if (rc) {
1356                 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1357                 GOTO(cleanup, rc);
1358         }
1359
1360         if (md.mea == NULL)
1361                 GOTO(cleanup, rc = -ENODATA);
1362
1363         obj = lmv_obj_create(exp, fid, md.mea);
1364         if (IS_ERR(obj))
1365                 rc = PTR_ERR(obj);
1366         else
1367                 lmv_obj_put(obj);
1368
1369         /* XXX LOV STACKING */
1370         obd_free_memmd(exp, (void *)&md.mea);
1371
1372         EXIT;
1373 cleanup:
1374         if (req)
1375                 ptlrpc_req_finished(req);
1376         return rc;
1377 }
1378
1379 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1380                const void *data, int datalen, int mode, __u32 uid,
1381                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1382                struct ptlrpc_request **request)
1383 {
1384         struct obd_device *obd = exp->exp_obd;
1385         struct lmv_obd *lmv = &obd->u.lmv;
1386         struct obd_export *tgt_exp;
1387         struct lmv_obj *obj;
1388         int rc, loop = 0;
1389         ENTRY;
1390
1391         rc = lmv_check_connect(obd);
1392         if (rc)
1393                 RETURN(rc);
1394
1395         if (!lmv->desc.ld_active_tgt_count)
1396                 RETURN(-EIO);
1397 repeat:
1398         ++loop;
1399         LASSERT(loop <= 2);
1400         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1401         if (obj) {
1402                 int mea_idx;
1403
1404                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1405                                        op_data->op_name, op_data->op_namelen);
1406                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1407                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1408                 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1409                 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1410                 lmv_obj_put(obj);
1411         } else {
1412                 struct lmv_tgt_desc *tgt;
1413
1414                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1415                 op_data->op_bias |= MDS_CHECK_SPLIT;
1416                 op_data->op_mds = tgt->ltd_idx;
1417                 tgt_exp = tgt->ltd_exp;
1418         }
1419
1420         if (IS_ERR(tgt_exp))
1421                 RETURN(PTR_ERR(tgt_exp));
1422
1423         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1424         if (rc == -ERESTART)
1425                 goto repeat;
1426         else if (rc)
1427                 RETURN(rc);
1428
1429         CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1430                op_data->op_name, PFID(&op_data->op_fid1));
1431
1432         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1433         rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1434                        cap_effective, rdev, request);
1435         if (rc == 0) {
1436                 if (*request == NULL)
1437                         RETURN(rc);
1438                 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1439         } else if (rc == -ERESTART) {
1440                 LASSERT(*request != NULL);
1441                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1442                           "Got -ERESTART during create!\n");
1443                 ptlrpc_req_finished(*request);
1444                 *request = NULL;
1445
1446                 /*
1447                  * Directory got split. Time to update local object and repeat
1448                  * the request with proper MDS.
1449                  */
1450                 rc = lmv_handle_split(exp, &op_data->op_fid1);
1451                 if (rc == 0) {
1452                         rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1453                                                   op_data, &op_data->op_fid2);
1454                         if (rc)
1455                                 RETURN(rc);
1456                         goto repeat;
1457                 }
1458         }
1459         RETURN(rc);
1460 }
1461
1462 static int lmv_done_writing(struct obd_export *exp,
1463                             struct md_op_data *op_data,
1464                             struct md_open_data *mod)
1465 {
1466         struct obd_device *obd = exp->exp_obd;
1467         struct lmv_obd *lmv = &obd->u.lmv;
1468         struct obd_export *tgt_exp;
1469         int rc;
1470         ENTRY;
1471
1472         rc = lmv_check_connect(obd);
1473         if (rc)
1474                 RETURN(rc);
1475
1476         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1477         if (IS_ERR(tgt_exp))
1478                 RETURN(PTR_ERR(tgt_exp));
1479
1480         rc = md_done_writing(tgt_exp, op_data, mod);
1481         RETURN(rc);
1482 }
1483
1484 static int
1485 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1486                    struct lookup_intent *it, struct md_op_data *op_data,
1487                    struct lustre_handle *lockh, void *lmm, int lmmsize)
1488 {
1489         struct obd_device *obd = exp->exp_obd;
1490         struct lmv_obd *lmv = &obd->u.lmv;
1491         struct lmv_stripe_md *mea = op_data->op_mea1;
1492         struct md_op_data *op_data2;
1493         struct obd_export *tgt_exp;
1494         int i, rc = 0;
1495         ENTRY;
1496
1497         OBD_ALLOC_PTR(op_data2);
1498         if (op_data2 == NULL)
1499                 RETURN(-ENOMEM);
1500
1501         LASSERT(mea != NULL);
1502         for (i = 0; i < mea->mea_count; i++) {
1503                 memset(op_data2, 0, sizeof(*op_data2));
1504                 op_data2->op_fid1 = mea->mea_ids[i];
1505                 op_data2->op_bias = 0;
1506
1507                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1508                 if (IS_ERR(tgt_exp))
1509                         GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1510
1511                 if (tgt_exp == NULL)
1512                         continue;
1513
1514                 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1515                                 lockh + i, lmm, lmmsize, NULL, 0);
1516
1517                 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1518                        PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1519
1520                 if (rc)
1521                         GOTO(cleanup, rc);
1522
1523                 if (it->d.lustre.it_data) {
1524                         struct ptlrpc_request *req;
1525                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1526                         ptlrpc_req_finished(req);
1527                 }
1528
1529                 if (it->d.lustre.it_status)
1530                         GOTO(cleanup, rc = it->d.lustre.it_status);
1531         }
1532
1533         EXIT;
1534 cleanup:
1535         OBD_FREE_PTR(op_data2);
1536
1537         if (rc != 0) {
1538                 /* drop all taken locks */
1539                 while (--i >= 0) {
1540                         if (lockh[i].cookie)
1541                                 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1542                         lockh[i].cookie = 0;
1543                 }
1544         }
1545         return rc;
1546 }
1547
1548 static int
1549 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1550                    struct lookup_intent *it, struct md_op_data *op_data,
1551                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1552                    int extra_lock_flags)
1553 {
1554         struct ptlrpc_request *req = it->d.lustre.it_data;
1555         struct obd_device *obd = exp->exp_obd;
1556         struct lmv_obd *lmv = &obd->u.lmv;
1557         struct lustre_handle plock;
1558         struct obd_export *tgt_exp;
1559         struct md_op_data *rdata;
1560         struct lu_fid fid_copy;
1561         struct mdt_body *body;
1562         int rc = 0, pmode;
1563         ENTRY;
1564
1565         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1566         LASSERT(body != NULL);
1567
1568         if (!(body->valid & OBD_MD_MDS))
1569                 RETURN(0);
1570
1571         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1572                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1573
1574         /* We got LOOKUP lock, but we really need attrs */
1575         pmode = it->d.lustre.it_lock_mode;
1576         LASSERT(pmode != 0);
1577         memcpy(&plock, lockh, sizeof(plock));
1578         it->d.lustre.it_lock_mode = 0;
1579         it->d.lustre.it_data = NULL;
1580         fid_copy = body->fid1;
1581
1582         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1583         ptlrpc_req_finished(req);
1584
1585         tgt_exp = lmv_find_export(lmv, &fid_copy);
1586         if (IS_ERR(tgt_exp))
1587                 GOTO(out, rc = PTR_ERR(tgt_exp));
1588
1589         OBD_ALLOC_PTR(rdata);
1590         if (rdata == NULL)
1591                 GOTO(out, rc = -ENOMEM);
1592
1593         rdata->op_fid1 = fid_copy;
1594         rdata->op_bias = MDS_CROSS_REF;
1595
1596         rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1597                         lmm, lmmsize, NULL, extra_lock_flags);
1598         OBD_FREE_PTR(rdata);
1599         EXIT;
1600 out:
1601         ldlm_lock_decref(&plock, pmode);
1602         return rc;
1603 }
1604
1605 static int
1606 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1607             struct lookup_intent *it, struct md_op_data *op_data,
1608             struct lustre_handle *lockh, void *lmm, int lmmsize,
1609             struct ptlrpc_request **req, int extra_lock_flags)
1610 {
1611         struct obd_device *obd = exp->exp_obd;
1612         struct lmv_obd *lmv = &obd->u.lmv;
1613         struct obd_export *tgt_exp = NULL;
1614         struct lmv_obj *obj;
1615         int rc;
1616         ENTRY;
1617
1618         rc = lmv_check_connect(obd);
1619         if (rc)
1620                 RETURN(rc);
1621
1622         if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1623                 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1624                                         lockh, lmm, lmmsize);
1625                 RETURN(rc);
1626         }
1627
1628         if (op_data->op_namelen) {
1629                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1630                 if (obj) {
1631                         int mea_idx;
1632
1633                         /* directory is split. look for right mds for this
1634                          * name */
1635                         mea_idx = raw_name2idx(obj->lo_hashtype,
1636                                                obj->lo_objcount,
1637                                                (char *)op_data->op_name,
1638                                                op_data->op_namelen);
1639                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1640                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1641                         lmv_obj_put(obj);
1642                 }
1643         }
1644
1645         if (tgt_exp == NULL)
1646                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1647         if (IS_ERR(tgt_exp))
1648                 RETURN(PTR_ERR(tgt_exp));
1649
1650         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1651                PFID(&op_data->op_fid1));
1652
1653         rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1654                         lmm, lmmsize, req, extra_lock_flags);
1655
1656         if (rc == 0 && it && it->it_op == IT_OPEN)
1657                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1658                                         lmm, lmmsize, extra_lock_flags);
1659         RETURN(rc);
1660 }
1661
1662 static int
1663 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1664                  struct obd_capa *oc, const char *filename, int namelen,
1665                  obd_valid valid, int ea_size, __u32 suppgid,
1666                  struct ptlrpc_request **request)
1667 {
1668         struct obd_device *obd = exp->exp_obd;
1669         struct lmv_obd *lmv = &obd->u.lmv;
1670         struct lu_fid rid = *fid;
1671         struct obd_export *tgt_exp;
1672         struct mdt_body *body;
1673         struct lmv_obj *obj;
1674         int rc, loop = 0;
1675         ENTRY;
1676
1677         rc = lmv_check_connect(obd);
1678         if (rc)
1679                 RETURN(rc);
1680
1681 repeat:
1682         ++loop;
1683         LASSERT(loop <= 2);
1684         obj = lmv_obj_grab(obd, &rid);
1685         if (obj) {
1686                 int mea_idx;
1687
1688                 /* Directory is split. Look for right mds for this name */
1689                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1690                                        filename, namelen - 1);
1691                 rid = obj->lo_inodes[mea_idx].li_fid;
1692                 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1693                 lmv_obj_put(obj);
1694                 valid &= ~OBD_MD_FLCKSPLIT;
1695         } else {
1696                 tgt_exp = lmv_find_export(lmv, &rid);
1697                 valid |= OBD_MD_FLCKSPLIT;
1698         }
1699         if (IS_ERR(tgt_exp))
1700                 RETURN(PTR_ERR(tgt_exp));
1701
1702         CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1703                namelen, filename, PFID(fid), PFID(&rid));
1704
1705         rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1706                              ea_size, suppgid, request);
1707         if (rc == 0) {
1708                 body = req_capsule_server_get(&(*request)->rq_pill,
1709                                               &RMF_MDT_BODY);
1710                 LASSERT(body != NULL);
1711
1712                 if (body->valid & OBD_MD_MDS) {
1713                         struct ptlrpc_request *req = NULL;
1714
1715                         rid = body->fid1;
1716                         CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1717                                PFID(&rid));
1718
1719                         tgt_exp = lmv_find_export(lmv, &rid);
1720                         if (IS_ERR(tgt_exp)) {
1721                                 ptlrpc_req_finished(*request);
1722                                 RETURN(PTR_ERR(tgt_exp));
1723                         }
1724
1725                         rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1726                                              valid | OBD_MD_FLCROSSREF,
1727                                              ea_size, suppgid, &req);
1728                         ptlrpc_req_finished(*request);
1729                         *request = req;
1730                 }
1731         } else if (rc == -ERESTART) {
1732                 LASSERT(*request != NULL);
1733                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1734                           "Got -ERESTART during getattr!\n");
1735                 ptlrpc_req_finished(*request);
1736                 *request = NULL;
1737
1738                 /*
1739                  * Directory got split. Time to update local object and repeat
1740                  * the request with proper MDS.
1741                  */
1742                 rc = lmv_handle_split(exp, &rid);
1743                 if (rc == 0)
1744                         goto repeat;
1745         }
1746         RETURN(rc);
1747 }
1748
1749 #define md_op_data_fid(op_data, fl)                     \
1750         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1751          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1752          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1753          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1754          NULL)
1755
1756 /* @tgt_exp is the export the metadata request is sent.
1757  * @fid_exp is the export the cancel should be sent for the current fid.
1758  * if @fid_exp is NULL, the export is found for the current fid.
1759  * @op_data keeps the current fid, which is pointed through @flag.
1760  * @mode, @bits -- lock match parameters. */
1761 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1762                             struct obd_export *fid_exp,
1763                             struct md_op_data *op_data,
1764                             ldlm_mode_t mode, int bits, int flag)
1765 {
1766         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1767         ldlm_policy_data_t policy = {{0}};
1768         int rc = 0;
1769         ENTRY;
1770
1771         if (!fid_is_sane(fid))
1772                 RETURN(0);
1773
1774         if (fid_exp == NULL)
1775                 fid_exp = lmv_find_export(lmv, fid);
1776
1777         if (tgt_exp == fid_exp) {
1778                 /* The export is the same as on the target server, cancel 
1779                  * will be sent along with the main metadata operation. */
1780                 op_data->op_flags |= flag;
1781                 RETURN(0);
1782         }
1783
1784         policy.l_inodebits.bits = bits;
1785         rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1786         RETURN(rc);
1787 }
1788
1789 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1790 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1791  * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1792  * md_cancel_unused for child export(s). */
1793 static int lmv_early_cancel_stripes(struct obd_export *exp,
1794                                     struct obd_export *tgt_exp,
1795                                     struct md_op_data *op_data,
1796                                     ldlm_mode_t mode, int bits, int flag)
1797 {
1798         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1799         struct obd_device *obd = exp->exp_obd;
1800         struct lmv_obd *lmv = &obd->u.lmv;
1801         struct obd_export *st_exp;
1802         struct lmv_obj *obj;
1803         int rc = 0;
1804         ENTRY;
1805
1806         if (!fid_is_sane(fid))
1807                 RETURN(0);
1808
1809         obj = lmv_obj_grab(obd, fid);
1810         if (obj) {
1811                 ldlm_policy_data_t policy = {{0}};
1812                 struct lu_fid *st_fid;
1813                 int i;
1814
1815                 policy.l_inodebits.bits = bits;
1816                 for (i = 0; i < obj->lo_objcount; i++) {
1817                         st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1818                         st_fid = &obj->lo_inodes[i].li_fid;
1819                         if (tgt_exp != st_exp) {
1820                                 rc = md_cancel_unused(st_exp, st_fid, &policy,
1821                                                       mode, LDLM_FL_ASYNC,
1822                                                       NULL);
1823                                 if (rc)
1824                                         break;
1825                         } else {
1826                                 /* Some export matches to @tgt_exp, do cancel
1827                                  * for its fid in mdc */
1828                                 *fid = *st_fid;
1829                                 op_data->op_flags |= flag;
1830                         }
1831                 }
1832                 lmv_obj_put(obj);
1833         } else {
1834                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1835                                       mode, bits, flag);
1836         }
1837         RETURN(rc);
1838 }
1839 #endif
1840
1841 /*
1842  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1843  * op_data->op_fid2
1844  */
1845 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1846                     struct ptlrpc_request **request)
1847 {
1848         struct obd_device *obd = exp->exp_obd;
1849         struct lmv_obd *lmv = &obd->u.lmv;
1850         struct obd_export *tgt_exp;
1851         struct lmv_obj *obj;
1852         int rc, loop = 0;
1853         mdsno_t mds;
1854         ENTRY;
1855
1856         rc = lmv_check_connect(obd);
1857         if (rc)
1858                 RETURN(rc);
1859
1860 repeat:
1861         ++loop;
1862         LASSERT(loop <= 2);
1863         if (op_data->op_namelen != 0) {
1864                 int mea_idx;
1865
1866                 /* Usual link request */
1867                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1868                 if (obj) {
1869                         mea_idx = raw_name2idx(obj->lo_hashtype,
1870                                                obj->lo_objcount,
1871                                                op_data->op_name,
1872                                                op_data->op_namelen);
1873                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1874                         mds = obj->lo_inodes[mea_idx].li_mds;
1875                         lmv_obj_put(obj);
1876                 } else {
1877                         rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1878                         if (rc)
1879                                 RETURN(rc);
1880                 }
1881
1882                 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1883                        PFID(&op_data->op_fid2), op_data->op_namelen,
1884                        op_data->op_name, PFID(&op_data->op_fid1));
1885         } else {
1886                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1887                 if (rc)
1888                         RETURN(rc);
1889
1890                 /* request from MDS to acquire i_links for inode by fid1 */
1891                 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1892                        PFID(&op_data->op_fid1));
1893         }
1894
1895         CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1896                mds, PFID(&op_data->op_fid1));
1897
1898         op_data->op_fsuid = current->fsuid;
1899         op_data->op_fsgid = current->fsgid;
1900         op_data->op_cap = cfs_curproc_cap_pack();
1901
1902         tgt_exp = lmv->tgts[mds].ltd_exp;
1903         if (op_data->op_namelen) {
1904                 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1905                 /* Cancel UPDATE lock on child (fid1). */
1906                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1907                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1908         }
1909         if (rc == 0)
1910                 rc = md_link(tgt_exp, op_data, request);
1911         if (rc == -ERESTART) {
1912                 LASSERT(*request != NULL);
1913                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1914                           "Got -ERESTART during link!\n");
1915                 ptlrpc_req_finished(*request);
1916                 *request = NULL;
1917
1918                 /*
1919                  * Directory got split. Time to update local object and repeat
1920                  * the request with proper MDS.
1921                  */
1922                 rc = lmv_handle_split(exp, &op_data->op_fid2);
1923                 if (rc == 0)
1924                         goto repeat;
1925         }
1926
1927         RETURN(rc);
1928 }
1929
1930 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1931                       const char *old, int oldlen, const char *new, int newlen,
1932                       struct ptlrpc_request **request)
1933 {
1934         struct obd_export *tgt_exp = NULL, *src_exp;
1935         struct obd_device *obd = exp->exp_obd;
1936         struct lmv_obd *lmv = &obd->u.lmv;
1937         int rc, mea_idx, loop = 0;
1938         struct lmv_obj *obj;
1939         mdsno_t mds1, mds2;
1940         ENTRY;
1941
1942         CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1943                oldlen, old, PFID(&op_data->op_fid1),
1944                newlen, new, PFID(&op_data->op_fid2));
1945
1946         rc = lmv_check_connect(obd);
1947         if (rc)
1948                 RETURN(rc);
1949
1950         if (oldlen == 0) {
1951                 /*
1952                  * MDS with old dir entry is asking another MDS to create name
1953                  * there.
1954                  */
1955                 CDEBUG(D_OTHER,
1956                        "create %*s(%d/%d) in "DFID" pointing "
1957                        "to "DFID"\n", newlen, new, oldlen, newlen,
1958                        PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1959
1960                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1961                 if (rc)
1962                         RETURN(rc);
1963
1964                 /*
1965                  * Target directory can be split, sowe should forward request to
1966                  * the right MDS.
1967                  */
1968                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1969                 if (obj) {
1970                         mea_idx = raw_name2idx(obj->lo_hashtype,
1971                                                obj->lo_objcount,
1972                                                (char *)new, newlen);
1973                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1974                         CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1975                                PFID(&op_data->op_fid2));
1976                         lmv_obj_put(obj);
1977                 }
1978                 goto request;
1979         }
1980
1981 repeat:
1982         ++loop;
1983         LASSERT(loop <= 2);
1984         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1985         if (obj) {
1986                 /*
1987                  * directory is already split, so we have to forward request to
1988                  * the right MDS.
1989                  */
1990                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1991                                        (char *)old, oldlen);
1992                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1993                 mds1 = obj->lo_inodes[mea_idx].li_mds;
1994                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1995                 lmv_obj_put(obj);
1996         } else {
1997                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1998                 if (rc)
1999                         RETURN(rc);
2000         }
2001
2002         obj = lmv_obj_grab(obd, &op_data->op_fid2);
2003         if (obj) {
2004                 /*
2005                  * Directory is already split, so we have to forward request to
2006                  * the right MDS.
2007                  */
2008                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2009                                        (char *)new, newlen);
2010
2011                 mds2 = obj->lo_inodes[mea_idx].li_mds;
2012                 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2013                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2014                 lmv_obj_put(obj);
2015         } else {
2016                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2017                 if (rc)
2018                         RETURN(rc);
2019         }
2020
2021 request:
2022         op_data->op_fsuid = current->fsuid;
2023         op_data->op_fsgid = current->fsgid;
2024         op_data->op_cap = cfs_curproc_cap_pack();
2025
2026         src_exp = lmv_get_export(lmv, mds1);
2027         tgt_exp = lmv_get_export(lmv, mds2);
2028         if (oldlen) {
2029                 /* LOOKUP lock on src child (fid3) should also be cancelled for
2030                  * src_exp in mdc_rename. */
2031                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2032
2033                 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2034                  * own export. */
2035                 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2036                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2037
2038                 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2039                 if (rc == 0)
2040                         rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2041                                               LCK_EX, MDS_INODELOCK_LOOKUP,
2042                                               MF_MDC_CANCEL_FID4);
2043
2044                 /* XXX: the case when child is a striped dir is not supported.
2045                  * Only the master stripe has all locks cancelled early. */
2046                 /* Cancel all the locks on tgt child (fid4). */
2047                 if (rc == 0)
2048                         rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2049                                               LCK_EX, MDS_INODELOCK_FULL,
2050                                               MF_MDC_CANCEL_FID4);
2051         }
2052
2053         if (rc == 0)
2054                 rc = md_rename(src_exp, op_data, old, oldlen,
2055                                new, newlen, request);
2056         if (rc == -ERESTART) {
2057                 LASSERT(*request != NULL);
2058                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2059                           "Got -ERESTART during rename!\n");
2060                 ptlrpc_req_finished(*request);
2061                 *request = NULL;
2062
2063                 /*
2064                  * Directory got split. Time to update local object and repeat
2065                  * the request with proper MDS.
2066                  */
2067                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2068                 if (rc == 0)
2069                         goto repeat;
2070         }
2071         RETURN(rc);
2072 }
2073
2074 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2075                        void *ea, int ealen, void *ea2, int ea2len,
2076                        struct ptlrpc_request **request,
2077                        struct md_open_data **mod)
2078 {
2079         struct obd_device *obd = exp->exp_obd;
2080         struct lmv_obd *lmv = &obd->u.lmv;
2081         struct ptlrpc_request *req;
2082         struct obd_export *tgt_exp;
2083         struct lmv_obj *obj;
2084         int rc = 0, i;
2085         ENTRY;
2086
2087         rc = lmv_check_connect(obd);
2088         if (rc)
2089                 RETURN(rc);
2090
2091         obj = lmv_obj_grab(obd, &op_data->op_fid1);
2092
2093         CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2094                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2095                obj ? ", split" : "");
2096
2097         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2098         if (obj) {
2099                 for (i = 0; i < obj->lo_objcount; i++) {
2100                         op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2101
2102                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2103                         if (IS_ERR(tgt_exp)) {
2104                                 rc = PTR_ERR(tgt_exp);
2105                                 break;
2106                         }
2107
2108                         rc = md_setattr(tgt_exp, op_data, ea, ealen,
2109                                         ea2, ea2len, &req, mod);
2110
2111                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2112                                 /*
2113                                  * this is master object and this request should
2114                                  * be returned back to llite.
2115                                  */
2116                                 *request = req;
2117                         } else {
2118                                 ptlrpc_req_finished(req);
2119                         }
2120
2121                         if (rc)
2122                                 break;
2123                 }
2124                 lmv_obj_put(obj);
2125         } else {
2126                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2127                 if (IS_ERR(tgt_exp))
2128                         RETURN(PTR_ERR(tgt_exp));
2129
2130                 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2131                                 ea2len, request, mod);
2132         }
2133         RETURN(rc);
2134 }
2135
2136 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2137                     struct obd_capa *oc, struct ptlrpc_request **request)
2138 {
2139         struct obd_device *obd = exp->exp_obd;
2140         struct lmv_obd *lmv = &obd->u.lmv;
2141         struct obd_export *tgt_exp;
2142         int rc;
2143         ENTRY;
2144
2145         rc = lmv_check_connect(obd);
2146         if (rc)
2147                 RETURN(rc);
2148
2149         tgt_exp = lmv_find_export(lmv, fid);
2150         if (IS_ERR(tgt_exp))
2151                 RETURN(PTR_ERR(tgt_exp));
2152
2153         rc = md_sync(tgt_exp, fid, oc, request);
2154         RETURN(rc);
2155 }
2156
2157 /* main purpose of LMV blocking ast is to remove split directory LMV
2158  * presentation object (struct lmv_obj) attached to the lock being revoked. */
2159 int lmv_blocking_ast(struct ldlm_lock *lock,
2160                      struct ldlm_lock_desc *desc,
2161                      void *data, int flag)
2162 {
2163         struct lustre_handle lockh;
2164         struct lmv_obj *obj;
2165         int rc;
2166         ENTRY;
2167
2168         switch (flag) {
2169         case LDLM_CB_BLOCKING:
2170                 ldlm_lock2handle(lock, &lockh);
2171                 rc = ldlm_cli_cancel(&lockh);
2172                 if (rc < 0) {
2173                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2174                         RETURN(rc);
2175                 }
2176                 break;
2177         case LDLM_CB_CANCELING:
2178                 /* time to drop cached attrs for dirobj */
2179                 obj = lock->l_ast_data;
2180                 if (obj) {
2181                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2182                                ", master "DFID"\n",
2183                                lock->l_resource->lr_name.name[3] == 1 ?
2184                                "LOOKUP" : "UPDATE",
2185                                lock->l_resource->lr_name.name[0],
2186                                lock->l_resource->lr_name.name[1],
2187                                PFID(&obj->lo_fid));
2188                         lmv_obj_put(obj);
2189                 }
2190                 break;
2191         default:
2192                 LBUG();
2193         }
2194         RETURN(0);
2195 }
2196
2197 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2198 {
2199         __u64 val;
2200
2201         val = le64_to_cpu(*hash);
2202         if (val < hash_adj)
2203                 val += MAX_HASH_SIZE;
2204         if (val != DIR_END_OFF)
2205                 *hash = cpu_to_le64(val - hash_adj);
2206 }
2207
2208 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2209 {
2210         __u64 id;
2211         struct obd_import *imp;
2212
2213         /*
2214          * XXX Hack: to get nid we assume that underlying obd device is mdc.
2215          */
2216         imp  = class_exp2cliimp(exp);
2217         id   = imp->imp_connection->c_self + fid_flatten(fid);
2218
2219         CDEBUG(D_INFO, "node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2220                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2221
2222         return id ^ (id >> 32);
2223 }
2224
2225 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2226                         struct obd_capa *oc, __u64 offset64, struct page *page,
2227                         struct ptlrpc_request **request)
2228 {
2229         struct obd_device *obd = exp->exp_obd;
2230         struct lmv_obd *lmv = &obd->u.lmv;
2231         struct obd_export *tgt_exp;
2232         struct lu_fid rid = *fid;
2233         struct lmv_obj *obj;
2234         __u64 offset;
2235         __u64 hash_adj = 0;
2236         __u32 rank = 0;
2237         __u64 seg_size = 0;
2238         __u64 tgt_tmp = 0;
2239         int tgt = 0;
2240         int tgt0 = 0;
2241         int rc;
2242         int nr = 0;
2243         ENTRY;
2244
2245         offset = offset64;
2246
2247         rc = lmv_check_connect(obd);
2248         if (rc)
2249                 RETURN(rc);
2250
2251         CDEBUG(D_INFO, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2252
2253         obj = lmv_obj_grab(obd, fid);
2254         if (obj) {
2255
2256         /*
2257          * This case handle directory lookup in clustered metadata case (i.e.
2258          * split directory is located on multiple md servers.)
2259          * each server keeps directory entries for certain range of hashes.
2260          * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2261          * first server will keep records with hashes [ 0 ... MAX_HASH / N  - 1],
2262          * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2263          * so on....
2264          *      readdir can simply start reading entries from 0 - N server in
2265          * order but that will not scale well as all client will request dir in
2266          * to server in same order.
2267          * Following algorithm does optimization:
2268          * Instead of doing readdir in 1, 2, ...., N order, client with a
2269          * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2270          * (every client has rank R)
2271          *      But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2272          * since client ask dir from MDS{R} client has pages with offsets
2273          * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2274          * on hash  values that we get.
2275          */
2276
2277                 struct lmv_inode *loi;
2278
2279                 lmv_obj_lock(obj);
2280
2281                 nr       = obj->lo_objcount;
2282                 LASSERT(nr > 0);
2283                 seg_size = MAX_HASH_SIZE;
2284                 do_div(seg_size, nr);
2285                 loi      = obj->lo_inodes;
2286                 rank     = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2287                                          fid) % nr;
2288                 tgt_tmp = offset;
2289                 do_div(tgt_tmp, seg_size);
2290                 tgt0     = do_div(tgt_tmp,  nr);
2291                 tgt      = (tgt0 + rank) % nr;
2292
2293                 if (tgt < tgt0)
2294                         /*
2295                          * Wrap around.
2296                          *
2297                          * Last segment has unusual length due to division
2298                          * rounding.
2299                          */
2300                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2301                 else
2302                         hash_adj = 0;
2303
2304                 hash_adj += rank * seg_size;
2305
2306                 CDEBUG(D_INFO, "hash_adj: %x "LPX64" "LPX64"/%x -> "LPX64"/%x\n",
2307                        rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2308
2309                 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2310                 rid = obj->lo_inodes[tgt].li_fid;
2311                 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2312
2313                 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2314                        PFID(&rid), (unsigned long)offset, tgt);
2315         } else
2316                 tgt_exp = lmv_find_export(lmv, &rid);
2317
2318         if (IS_ERR(tgt_exp))
2319                 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2320
2321         rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2322         if (rc)
2323                 GOTO(cleanup, rc);
2324         if (obj) {
2325                 struct lu_dirpage *dp;
2326                 struct lu_dirent  *ent;
2327
2328                 dp = cfs_kmap(page);
2329
2330                 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2331                 lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2332                 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2333
2334                 for (ent = lu_dirent_start(dp); ent != NULL;
2335                      ent = lu_dirent_next(ent))
2336                         lmv_hash_adjust(&ent->lde_hash, hash_adj);
2337
2338                 if (tgt0 != nr - 1) {
2339                         __u64 end;
2340
2341                         end = le64_to_cpu(dp->ldp_hash_end);
2342                         if (end == DIR_END_OFF) {
2343                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2344                                                                (tgt0 + 1));
2345                                 CDEBUG(D_INFO,
2346                                        ""DFID" reset end "LPX64" tgt %d\n",
2347                                        PFID(&rid),
2348                                        le64_to_cpu(dp->ldp_hash_end), tgt);
2349                         }
2350                 }
2351                 cfs_kunmap(page);
2352         }
2353         /*
2354          * Here we could remove "." and ".." from all pages which at not from
2355          * master. But MDS has only "." and ".." for master dir.
2356          */
2357         EXIT;
2358 cleanup:
2359         if (obj) {
2360                 lmv_obj_unlock(obj);
2361                 lmv_obj_put(obj);
2362         }
2363         return rc;
2364 }
2365
2366 static int lmv_unlink_slaves(struct obd_export *exp,
2367                              struct md_op_data *op_data,
2368                              struct ptlrpc_request **req)
2369 {
2370         struct obd_device *obd = exp->exp_obd;
2371         struct lmv_obd *lmv = &obd->u.lmv;
2372         struct lmv_stripe_md *mea = op_data->op_mea1;
2373         struct md_op_data *op_data2;
2374         struct obd_export *tgt_exp;
2375         int i, rc = 0;
2376         ENTRY;
2377
2378         OBD_ALLOC_PTR(op_data2);
2379         if (op_data2 == NULL)
2380                 RETURN(-ENOMEM);
2381
2382         op_data2->op_mode = S_IFDIR;
2383         op_data2->op_fsuid = current->fsuid;
2384         op_data2->op_fsgid = current->fsgid;
2385         op_data2->op_bias = 0;
2386
2387         LASSERT(mea != NULL);
2388         for (i = 0; i < mea->mea_count; i++) {
2389                 memset(op_data2, 0, sizeof(*op_data2));
2390                 op_data2->op_fid1 = mea->mea_ids[i];
2391                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2392                 if (IS_ERR(tgt_exp))
2393                         GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2394
2395                 if (tgt_exp == NULL)
2396                         continue;
2397
2398                 rc = md_unlink(tgt_exp, op_data2, req);
2399
2400                 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2401                        PFID(&mea->mea_ids[i]), rc);
2402
2403                 if (*req) {
2404                         ptlrpc_req_finished(*req);
2405                         *req = NULL;
2406                 }
2407                 if (rc)
2408                         GOTO(out_free_op_data2, rc);
2409         }
2410
2411         EXIT;
2412 out_free_op_data2:
2413         OBD_FREE_PTR(op_data2);
2414         return rc;
2415 }
2416
2417 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2418                       struct ptlrpc_request **request)
2419 {
2420         struct obd_device *obd = exp->exp_obd;
2421         struct lmv_obd *lmv = &obd->u.lmv;
2422         struct obd_export *tgt_exp = NULL;
2423         struct lmv_obj *obj;
2424         int rc, loop = 0;
2425         ENTRY;
2426
2427         rc = lmv_check_connect(obd);
2428         if (rc)
2429                 RETURN(rc);
2430
2431         if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2432                 /* mds asks to remove slave objects */
2433                 rc = lmv_unlink_slaves(exp, op_data, request);
2434                 RETURN(rc);
2435         }
2436
2437 repeat:
2438         ++loop;
2439         LASSERT(loop <= 2);
2440         if (op_data->op_namelen != 0) {
2441                 int mea_idx;
2442
2443                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2444                 if (obj) {
2445                         mea_idx = raw_name2idx(obj->lo_hashtype,
2446                                                obj->lo_objcount,
2447                                                op_data->op_name,
2448                                                op_data->op_namelen);
2449                         op_data->op_bias &= ~MDS_CHECK_SPLIT;
2450                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2451                         tgt_exp = lmv_get_export(lmv,
2452                                                  obj->lo_inodes[mea_idx].li_mds);
2453                         lmv_obj_put(obj);
2454                         CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2455                                op_data->op_namelen, op_data->op_name,
2456                                PFID(&op_data->op_fid1), mea_idx);
2457                 }
2458         } else {
2459                 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2460                        PFID(&op_data->op_fid1));
2461         }
2462         if (tgt_exp == NULL) {
2463                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2464                 if (IS_ERR(tgt_exp))
2465                         RETURN(PTR_ERR(tgt_exp));
2466                 op_data->op_bias |= MDS_CHECK_SPLIT;
2467         }
2468
2469         op_data->op_fsuid = current->fsuid;
2470         op_data->op_fsgid = current->fsgid;
2471         op_data->op_cap = cfs_curproc_cap_pack();
2472
2473         /* If child's fid is given, cancel unused locks for it if it is from
2474          * another export than parent. */
2475         if (op_data->op_namelen) {
2476                 /* LOOKUP lock for child (fid3) should also be cancelled on 
2477                  * parent tgt_exp in mdc_unlink(). */
2478                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2479
2480                 /* XXX: the case when child is a striped dir is not supported.
2481                  * Only the master stripe has all locks cancelled early. */
2482                 /* Cancel FULL locks on child (fid3). */
2483                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2484                                       MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2485         }
2486         if (rc == 0)
2487                 rc = md_unlink(tgt_exp, op_data, request);
2488         if (rc == -ERESTART) {
2489                 LASSERT(*request != NULL);
2490                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2491                           "Got -ERESTART during unlink!\n");
2492                 ptlrpc_req_finished(*request);
2493                 *request = NULL;
2494
2495                 /*
2496                  * Directory got split. Time to update local object and repeat
2497                  * the request with proper MDS.
2498                  */
2499                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2500                 if (rc == 0)
2501                         goto repeat;
2502         }
2503         RETURN(rc);
2504 }
2505
2506 static int lmv_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2507                          struct obd_device *tgt, int count,
2508                          struct llog_catid *logid, struct obd_uuid *uuid)
2509 {
2510 #if 0
2511         struct llog_ctxt *ctxt;
2512         int rc;
2513         ENTRY;
2514
2515         LASSERT(group == OBD_LLOG_GROUP);
2516         rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2517                         &llog_client_ops);
2518         if (rc == 0) {
2519                 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2520                 llog_initiator_connect(ctxt, tgt);
2521                 llog_ctxt_put(ctxt);
2522         }
2523         RETURN(rc);
2524 #else
2525         return 0;
2526 #endif
2527 }
2528
2529 static int lmv_llog_finish(struct obd_device *obd, int count)
2530 {
2531         struct llog_ctxt *ctxt;
2532         int rc = 0;
2533         ENTRY;
2534
2535         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2536         if (ctxt)
2537                 rc = llog_cleanup(ctxt);
2538
2539         RETURN(rc);
2540 }
2541
2542 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2543 {
2544         int rc = 0;
2545
2546         switch (stage) {
2547         case OBD_CLEANUP_EARLY:
2548                 /* XXX: here should be calling obd_precleanup() down to
2549                  * stack. */
2550                 break;
2551         case OBD_CLEANUP_EXPORTS:
2552                 rc = obd_llog_finish(obd, 0);
2553                 if (rc != 0)
2554                         CERROR("failed to cleanup llogging subsystems\n");
2555                 break;
2556         default:
2557                 break;
2558         }
2559         RETURN(rc);
2560 }
2561
2562 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2563                         void *key, __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2564 {
2565         struct obd_device *obd;
2566         struct lmv_obd *lmv;
2567         int rc = 0;
2568         ENTRY;
2569
2570         obd = class_exp2obd(exp);
2571         if (obd == NULL) {
2572                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2573                        exp->exp_handle.h_cookie);
2574                 RETURN(-EINVAL);
2575         }
2576
2577         lmv = &obd->u.lmv;
2578         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2579                 struct lmv_tgt_desc *tgts;
2580                 int i;
2581
2582                 rc = lmv_check_connect(obd);
2583                 if (rc)
2584                         RETURN(rc);
2585
2586                 LASSERT(*vallen == sizeof(__u32));
2587                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2588                      i++, tgts++) {
2589
2590                         /* all tgts should be connected when this get called. */
2591                         if (!tgts || !tgts->ltd_exp) {
2592                                 CERROR("target not setup?\n");
2593                                 continue;
2594                         }
2595
2596                         if (!obd_get_info(tgts->ltd_exp, keylen, key,
2597                                           vallen, val, NULL))
2598                                 RETURN(0);
2599                 }
2600                 RETURN(-EINVAL);
2601         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2602                 rc = lmv_check_connect(obd);
2603                 if (rc)
2604                         RETURN(rc);
2605
2606                 /* forwarding this request to first MDS, it should know LOV
2607                  * desc. */
2608                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2609                                   vallen, val, NULL);
2610                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2611                         exp->exp_connect_flags =
2612                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2613                 }
2614                 RETURN(rc);
2615         }
2616
2617         CDEBUG(D_IOCTL, "invalid key\n");
2618         RETURN(-EINVAL);
2619 }
2620
2621 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2622                        void *key, obd_count vallen, void *val,
2623                        struct ptlrpc_request_set *set)
2624 {
2625         struct lmv_tgt_desc    *tgt;
2626         struct obd_device      *obd;
2627         struct lmv_obd         *lmv;
2628         int rc = 0;
2629         ENTRY;
2630
2631         obd = class_exp2obd(exp);
2632         if (obd == NULL) {
2633                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2634                        exp->exp_handle.h_cookie);
2635                 RETURN(-EINVAL);
2636         }
2637         lmv = &obd->u.lmv;
2638
2639         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2640             KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2641                 int i, err = 0;
2642
2643                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2644                         tgt = &lmv->tgts[i];
2645
2646                         if (!tgt->ltd_exp)
2647                                 continue;
2648
2649                         err = obd_set_info_async(tgt->ltd_exp,
2650                                                  keylen, key, vallen, val, set);
2651                         if (err && rc == 0)
2652                                 rc = err;
2653                 }
2654
2655                 RETURN(rc);
2656         }
2657
2658         RETURN(-EINVAL);
2659 }
2660
2661 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2662                struct lov_stripe_md *lsm)
2663 {
2664         struct obd_device *obd = class_exp2obd(exp);
2665         struct lmv_obd *lmv = &obd->u.lmv;
2666         struct lmv_stripe_md *meap, *lsmp;
2667         int mea_size, i;
2668         ENTRY;
2669
2670         mea_size = lmv_get_easize(lmv);
2671         if (!lmmp)
2672                 RETURN(mea_size);
2673
2674         if (*lmmp && !lsm) {
2675                 OBD_FREE(*lmmp, mea_size);
2676                 *lmmp = NULL;
2677                 RETURN(0);
2678         }
2679
2680         if (*lmmp == NULL) {
2681                 OBD_ALLOC(*lmmp, mea_size);
2682                 if (*lmmp == NULL)
2683                         RETURN(-ENOMEM);
2684         }
2685
2686         if (!lsm)
2687                 RETURN(mea_size);
2688
2689         lsmp = (struct lmv_stripe_md *)lsm;
2690         meap = (struct lmv_stripe_md *)*lmmp;
2691
2692         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2693             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2694                 RETURN(-EINVAL);
2695
2696         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2697         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2698         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2699
2700         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2701                 meap->mea_ids[i] = meap->mea_ids[i];
2702                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2703         }
2704
2705         RETURN(mea_size);
2706 }
2707
2708 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2709                  struct lov_mds_md *lmm, int lmm_size)
2710 {
2711         struct obd_device *obd = class_exp2obd(exp);
2712         struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2713         struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2714         struct lmv_obd *lmv = &obd->u.lmv;
2715         int mea_size, i;
2716         __u32 magic;
2717         ENTRY;
2718
2719         mea_size = lmv_get_easize(lmv);
2720         if (lsmp == NULL)
2721                 return mea_size;
2722
2723         if (*lsmp != NULL && lmm == NULL) {
2724                 OBD_FREE(*tmea, mea_size);
2725                 *lsmp = NULL;
2726                 RETURN(0);
2727         }
2728
2729         LASSERT(mea_size == lmm_size);
2730
2731         OBD_ALLOC(*tmea, mea_size);
2732         if (*tmea == NULL)
2733                 RETURN(-ENOMEM);
2734
2735         if (!lmm)
2736                 RETURN(mea_size);
2737
2738         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2739             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2740             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2741         {
2742                 magic = le32_to_cpu(mea->mea_magic);
2743         } else {
2744                 /* old mea is not handled here */
2745                 LBUG();
2746         }
2747
2748         (*tmea)->mea_magic = magic;
2749         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2750         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2751
2752         for (i = 0; i < (*tmea)->mea_count; i++) {
2753                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2754                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2755         }
2756         RETURN(mea_size);
2757 }
2758
2759 static int lmv_cancel_unused(struct obd_export *exp,
2760                              const struct lu_fid *fid,
2761                              ldlm_policy_data_t *policy,
2762                              ldlm_mode_t mode, int flags, void *opaque)
2763 {
2764         struct obd_device *obd = exp->exp_obd;
2765         struct lmv_obd *lmv = &obd->u.lmv;
2766         int rc = 0, err, i;
2767         ENTRY;
2768
2769         LASSERT(fid != NULL);
2770
2771         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2772                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2773                         continue;
2774
2775                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2776                                        policy, mode, flags, opaque);
2777                 if (!rc)
2778                         rc = err;
2779         }
2780         RETURN(rc);
2781 }
2782
2783 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2784 {
2785         struct obd_device *obd = exp->exp_obd;
2786         struct lmv_obd *lmv = &obd->u.lmv;
2787
2788         ENTRY;
2789         RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2790 }
2791
2792 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2793                            const struct lu_fid *fid, ldlm_type_t type,
2794                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2795                            struct lustre_handle *lockh)
2796 {
2797         struct obd_device *obd = exp->exp_obd;
2798         struct lmv_obd *lmv = &obd->u.lmv;
2799         ldlm_mode_t rc;
2800         int i;
2801         ENTRY;
2802
2803         CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2804
2805         /* with CMD every object can have two locks in different namespaces:
2806          * lookup lock in space of mds storing direntry and update/open lock in
2807          * space of mds storing inode. Thus we check all targets, not only that
2808          * one fid was created in. */
2809         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2810                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2811                                    type, policy, mode, lockh);
2812                 if (rc)
2813                         RETURN(rc);
2814         }
2815
2816         RETURN(0);
2817 }
2818
2819 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2820                       struct obd_export *dt_exp, struct obd_export *md_exp,
2821                       struct lustre_md *md)
2822 {
2823         struct obd_device *obd = exp->exp_obd;
2824         struct lmv_obd *lmv = &obd->u.lmv;
2825         int rc;
2826
2827         ENTRY;
2828         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2829         RETURN(rc);
2830 }
2831
2832 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2833 {
2834         struct obd_device *obd = exp->exp_obd;
2835         struct lmv_obd *lmv = &obd->u.lmv;
2836
2837         ENTRY;
2838
2839         /* XXX LOV STACKING */
2840         if (md->mea)
2841                 obd_free_memmd(exp, (void *)&md->mea);
2842         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2843 }
2844
2845 int lmv_set_open_replay_data(struct obd_export *exp,
2846                              struct obd_client_handle *och,
2847                              struct ptlrpc_request *open_req)
2848 {
2849         struct obd_device *obd = exp->exp_obd;
2850         struct lmv_obd *lmv = &obd->u.lmv;
2851         struct obd_export *tgt_exp;
2852
2853         ENTRY;
2854
2855         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2856         if (IS_ERR(tgt_exp))
2857                 RETURN(PTR_ERR(tgt_exp));
2858
2859         RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2860 }
2861
2862 int lmv_clear_open_replay_data(struct obd_export *exp,
2863                                struct obd_client_handle *och)
2864 {
2865         struct obd_device *obd = exp->exp_obd;
2866         struct lmv_obd *lmv = &obd->u.lmv;
2867         struct obd_export *tgt_exp;
2868         ENTRY;
2869
2870         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2871         if (IS_ERR(tgt_exp))
2872                 RETURN(PTR_ERR(tgt_exp));
2873
2874         RETURN(md_clear_open_replay_data(tgt_exp, och));
2875 }
2876
2877 static int lmv_get_remote_perm(struct obd_export *exp,
2878                                const struct lu_fid *fid,
2879                                struct obd_capa *oc, __u32 suppgid,
2880                                struct ptlrpc_request **request)
2881 {
2882         struct obd_device *obd = exp->exp_obd;
2883         struct lmv_obd *lmv = &obd->u.lmv;
2884         struct obd_export *tgt_exp;
2885         int rc;
2886
2887         ENTRY;
2888
2889         rc = lmv_check_connect(obd);
2890         if (rc)
2891                 RETURN(rc);
2892
2893         tgt_exp = lmv_find_export(lmv, fid);
2894         if (IS_ERR(tgt_exp))
2895                 RETURN(PTR_ERR(tgt_exp));
2896
2897         rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2898
2899         RETURN(rc);
2900 }
2901
2902 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2903                           renew_capa_cb_t cb)
2904 {
2905         struct obd_device *obd = exp->exp_obd;
2906         struct lmv_obd *lmv = &obd->u.lmv;
2907         struct obd_export *tgt_exp;
2908         int rc;
2909         ENTRY;
2910
2911         rc = lmv_check_connect(obd);
2912         if (rc)
2913                 RETURN(rc);
2914
2915         tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2916         if (IS_ERR(tgt_exp))
2917                 RETURN(PTR_ERR(tgt_exp));
2918
2919         rc = md_renew_capa(tgt_exp, oc, cb);
2920         RETURN(rc);
2921 }
2922
2923 int lmv_intent_getattr_async(struct obd_export *exp,
2924                              struct md_enqueue_info *minfo,
2925                              struct ldlm_enqueue_info *einfo)
2926 {
2927         struct obd_device *obd = exp->exp_obd;
2928         struct lmv_obd    *lmv = &obd->u.lmv;
2929         struct obd_export *tgt_exp;
2930         int rc;
2931         ENTRY;
2932
2933         rc = lmv_check_connect(obd);
2934         if (rc)
2935                 RETURN(rc);
2936
2937         if (fid_is_zero(&minfo->mi_data.op_fid2))
2938                 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2939         else
2940                 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2941         if (IS_ERR(tgt_exp))
2942                 RETURN(PTR_ERR(tgt_exp));
2943
2944         rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2945         RETURN(rc);
2946 }
2947
2948 int lmv_revalidate_lock(struct obd_export *exp,
2949                         struct lookup_intent *it,
2950                         struct lu_fid *fid)
2951 {
2952         struct obd_device *obd = exp->exp_obd;
2953         struct lmv_obd    *lmv = &obd->u.lmv;
2954         struct obd_export *tgt_exp;
2955         int rc;
2956         ENTRY;
2957
2958         rc = lmv_check_connect(obd);
2959         if (rc)
2960                 RETURN(rc);
2961
2962         tgt_exp = lmv_find_export(lmv, fid);
2963         if (IS_ERR(tgt_exp))
2964                 RETURN(PTR_ERR(tgt_exp));
2965
2966         rc = md_revalidate_lock(tgt_exp, it, fid);
2967         RETURN(rc);
2968 }
2969
2970
2971 struct obd_ops lmv_obd_ops = {
2972         .o_owner                = THIS_MODULE,
2973         .o_setup                = lmv_setup,
2974         .o_cleanup              = lmv_cleanup,
2975         .o_precleanup           = lmv_precleanup,
2976         .o_process_config       = lmv_process_config,
2977         .o_connect              = lmv_connect,
2978         .o_disconnect           = lmv_disconnect,
2979         .o_statfs               = lmv_statfs,
2980         .o_llog_init            = lmv_llog_init,
2981         .o_llog_finish          = lmv_llog_finish,
2982         .o_get_info             = lmv_get_info,
2983         .o_set_info_async       = lmv_set_info_async,
2984         .o_packmd               = lmv_packmd,
2985         .o_unpackmd             = lmv_unpackmd,
2986         .o_notify               = lmv_notify,
2987         .o_get_uuid             = lmv_get_uuid,
2988         .o_iocontrol            = lmv_iocontrol,
2989         .o_fid_delete           = lmv_fid_delete
2990 };
2991
2992 struct md_ops lmv_md_ops = {
2993         .m_getstatus            = lmv_getstatus,
2994         .m_change_cbdata        = lmv_change_cbdata,
2995         .m_close                = lmv_close,
2996         .m_create               = lmv_create,
2997         .m_done_writing         = lmv_done_writing,
2998         .m_enqueue              = lmv_enqueue,
2999         .m_getattr              = lmv_getattr,
3000         .m_getxattr             = lmv_getxattr,
3001         .m_getattr_name         = lmv_getattr_name,
3002         .m_intent_lock          = lmv_intent_lock,
3003         .m_link                 = lmv_link,
3004         .m_rename               = lmv_rename,
3005         .m_setattr              = lmv_setattr,
3006         .m_setxattr             = lmv_setxattr,
3007         .m_sync                 = lmv_sync,
3008         .m_readpage             = lmv_readpage,
3009         .m_unlink               = lmv_unlink,
3010         .m_init_ea_size         = lmv_init_ea_size,
3011         .m_cancel_unused        = lmv_cancel_unused,
3012         .m_set_lock_data        = lmv_set_lock_data,
3013         .m_lock_match           = lmv_lock_match,
3014         .m_get_lustre_md        = lmv_get_lustre_md,
3015         .m_free_lustre_md       = lmv_free_lustre_md,
3016         .m_set_open_replay_data = lmv_set_open_replay_data,
3017         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3018         .m_renew_capa           = lmv_renew_capa,
3019         .m_get_remote_perm      = lmv_get_remote_perm,
3020         .m_intent_getattr_async = lmv_intent_getattr_async,
3021         .m_revalidate_lock      = lmv_revalidate_lock
3022 };
3023
3024 int __init lmv_init(void)
3025 {
3026         struct lprocfs_static_vars lvars;
3027         int rc;
3028
3029         obj_cache = cfs_mem_cache_create("lmv_objects",
3030                                       sizeof(struct lmv_obj),
3031                                       0, 0);
3032         if (!obj_cache) {
3033                 CERROR("error allocating lmv objects cache\n");
3034                 return -ENOMEM;
3035         }
3036
3037         lprocfs_lmv_init_vars(&lvars);
3038         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3039                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3040         if (rc)
3041                 cfs_mem_cache_destroy(obj_cache);
3042
3043         return rc;
3044 }
3045
3046 #ifdef __KERNEL__
3047 static void lmv_exit(void)
3048 {
3049         int rc;
3050
3051         class_unregister_type(LUSTRE_LMV_NAME);
3052
3053         rc = cfs_mem_cache_destroy(obj_cache);
3054         LASSERTF(rc == 0,
3055                  "can't free lmv objects cache, %d object(s)"
3056                  "still in use\n", atomic_read(&obj_cache_count));
3057 }
3058
3059 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3060 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3061 MODULE_LICENSE("GPL");
3062
3063 module_init(lmv_init);
3064 module_exit(lmv_exit);
3065 #endif