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