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