Whamcloud - gitweb
LU-1842 quota: remove quota code
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_handler.c
37  *
38  * Lustre Metadata Target (mdt) request handler
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  * Author: Mike Shaver <shaver@clusterfs.com>
44  * Author: Nikita Danilov <nikita@clusterfs.com>
45  * Author: Huang Hua <huanghua@clusterfs.com>
46  * Author: Yury Umanets <umka@clusterfs.com>
47  */
48
49 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <linux/module.h>
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  */
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
61 #include <obd.h>
62 /* lu2dt_dev() */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_mdt.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lquota.h>
71
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73         [LCK_MINMODE] = MDL_MINMODE,
74         [LCK_EX]      = MDL_EX,
75         [LCK_PW]      = MDL_PW,
76         [LCK_PR]      = MDL_PR,
77         [LCK_CW]      = MDL_CW,
78         [LCK_CR]      = MDL_CR,
79         [LCK_NL]      = MDL_NL,
80         [LCK_GROUP]   = MDL_GROUP
81 };
82
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84         [MDL_MINMODE] = LCK_MINMODE,
85         [MDL_EX]      = LCK_EX,
86         [MDL_PW]      = LCK_PW,
87         [MDL_PR]      = LCK_PR,
88         [MDL_CW]      = LCK_CW,
89         [MDL_CR]      = LCK_CR,
90         [MDL_NL]      = LCK_NL,
91         [MDL_GROUP]   = LCK_GROUP
92 };
93
94 /*
95  * Initialized in mdt_mod_init().
96  */
97 static unsigned long mdt_num_threads;
98 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
99                 "number of MDS service threads to start "
100                 "(deprecated in favor of mds_num_threads)");
101
102 static unsigned long mds_num_threads;
103 CFS_MODULE_PARM(mds_num_threads, "ul", ulong, 0444,
104                 "number of MDS service threads to start");
105
106 static char *mds_num_cpts;
107 CFS_MODULE_PARM(mds_num_cpts, "c", charp, 0444,
108                 "CPU partitions MDS threads should run on");
109
110 static unsigned long mds_rdpg_num_threads;
111 CFS_MODULE_PARM(mds_rdpg_num_threads, "ul", ulong, 0444,
112                 "number of MDS readpage service threads to start");
113
114 static char *mds_rdpg_num_cpts;
115 CFS_MODULE_PARM(mds_rdpg_num_cpts, "c", charp, 0444,
116                 "CPU partitions MDS readpage threads should run on");
117
118 /* NB: these two should be removed along with setattr service in the future */
119 static unsigned long mds_attr_num_threads;
120 CFS_MODULE_PARM(mds_attr_num_threads, "ul", ulong, 0444,
121                 "number of MDS setattr service threads to start");
122
123 static char *mds_attr_num_cpts;
124 CFS_MODULE_PARM(mds_attr_num_cpts, "c", charp, 0444,
125                 "CPU partitions MDS setattr threads should run on");
126
127 /* ptlrpc request handler for MDT. All handlers are
128  * grouped into several slices - struct mdt_opc_slice,
129  * and stored in an array - mdt_handlers[].
130  */
131 struct mdt_handler {
132         /* The name of this handler. */
133         const char *mh_name;
134         /* Fail id for this handler, checked at the beginning of this handler*/
135         int         mh_fail_id;
136         /* Operation code for this handler */
137         __u32       mh_opc;
138         /* flags are listed in enum mdt_handler_flags below. */
139         __u32       mh_flags;
140         /* The actual handler function to execute. */
141         int (*mh_act)(struct mdt_thread_info *info);
142         /* Request format for this request. */
143         const struct req_format *mh_fmt;
144 };
145
146 enum mdt_handler_flags {
147         /*
148          * struct mdt_body is passed in the incoming message, and object
149          * identified by this fid exists on disk.
150          *
151          * "habeo corpus" == "I have a body"
152          */
153         HABEO_CORPUS = (1 << 0),
154         /*
155          * struct ldlm_request is passed in the incoming message.
156          *
157          * "habeo clavis" == "I have a key"
158          */
159         HABEO_CLAVIS = (1 << 1),
160         /*
161          * this request has fixed reply format, so that reply message can be
162          * packed by generic code.
163          *
164          * "habeo refero" == "I have a reply"
165          */
166         HABEO_REFERO = (1 << 2),
167         /*
168          * this request will modify something, so check whether the filesystem
169          * is readonly or not, then return -EROFS to client asap if necessary.
170          *
171          * "mutabor" == "I shall modify"
172          */
173         MUTABOR      = (1 << 3)
174 };
175
176 struct mdt_opc_slice {
177         __u32               mos_opc_start;
178         int                 mos_opc_end;
179         struct mdt_handler *mos_hs;
180 };
181
182 static struct mdt_opc_slice mdt_regular_handlers[];
183 static struct mdt_opc_slice mdt_readpage_handlers[];
184 static struct mdt_opc_slice mdt_xmds_handlers[];
185 static struct mdt_opc_slice mdt_seq_handlers[];
186 static struct mdt_opc_slice mdt_fld_handlers[];
187
188 static struct mdt_device *mdt_dev(struct lu_device *d);
189 static int mdt_regular_handle(struct ptlrpc_request *req);
190 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
191 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
192                         struct getinfo_fid2path *fp);
193
194 static const struct lu_object_operations mdt_obj_ops;
195
196 /* Slab for MDT object allocation */
197 static cfs_mem_cache_t *mdt_object_kmem;
198
199 static struct lu_kmem_descr mdt_caches[] = {
200         {
201                 .ckd_cache = &mdt_object_kmem,
202                 .ckd_name  = "mdt_obj",
203                 .ckd_size  = sizeof(struct mdt_object)
204         },
205         {
206                 .ckd_cache = NULL
207         }
208 };
209
210 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
211 {
212         if (!rep)
213                 return 0;
214         return (rep->lock_policy_res1 & flag);
215 }
216
217 void mdt_clear_disposition(struct mdt_thread_info *info,
218                            struct ldlm_reply *rep, int flag)
219 {
220         if (info)
221                 info->mti_opdata &= ~flag;
222         if (rep)
223                 rep->lock_policy_res1 &= ~flag;
224 }
225
226 void mdt_set_disposition(struct mdt_thread_info *info,
227                          struct ldlm_reply *rep, int flag)
228 {
229         if (info)
230                 info->mti_opdata |= flag;
231         if (rep)
232                 rep->lock_policy_res1 |= flag;
233 }
234
235 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
236 {
237         lh->mlh_pdo_hash = 0;
238         lh->mlh_reg_mode = lm;
239         lh->mlh_type = MDT_REG_LOCK;
240 }
241
242 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
243                        const char *name, int namelen)
244 {
245         lh->mlh_reg_mode = lm;
246         lh->mlh_type = MDT_PDO_LOCK;
247
248         if (name != NULL && (name[0] != '\0')) {
249                 LASSERT(namelen > 0);
250                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
251         } else {
252                 LASSERT(namelen == 0);
253                 lh->mlh_pdo_hash = 0ull;
254         }
255 }
256
257 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
258                               struct mdt_lock_handle *lh)
259 {
260         mdl_mode_t mode;
261         ENTRY;
262
263         /*
264          * Any dir access needs couple of locks:
265          *
266          * 1) on part of dir we gonna take lookup/modify;
267          *
268          * 2) on whole dir to protect it from concurrent splitting and/or to
269          * flush client's cache for readdir().
270          *
271          * so, for a given mode and object this routine decides what lock mode
272          * to use for lock #2:
273          *
274          * 1) if caller's gonna lookup in dir then we need to protect dir from
275          * being splitted only - LCK_CR
276          *
277          * 2) if caller's gonna modify dir then we need to protect dir from
278          * being splitted and to flush cache - LCK_CW
279          *
280          * 3) if caller's gonna modify dir and that dir seems ready for
281          * splitting then we need to protect it from any type of access
282          * (lookup/modify/split) - LCK_EX --bzzz
283          */
284
285         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
286         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
287
288         /*
289          * Ask underlaying level its opinion about preferable PDO lock mode
290          * having access type passed as regular lock mode:
291          *
292          * - MDL_MINMODE means that lower layer does not want to specify lock
293          * mode;
294          *
295          * - MDL_NL means that no PDO lock should be taken. This is used in some
296          * cases. Say, for non-splittable directories no need to use PDO locks
297          * at all.
298          */
299         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
300                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
301
302         if (mode != MDL_MINMODE) {
303                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
304         } else {
305                 /*
306                  * Lower layer does not want to specify locking mode. We do it
307                  * our selves. No special protection is needed, just flush
308                  * client's cache on modification and allow concurrent
309                  * mondification.
310                  */
311                 switch (lh->mlh_reg_mode) {
312                 case LCK_EX:
313                         lh->mlh_pdo_mode = LCK_EX;
314                         break;
315                 case LCK_PR:
316                         lh->mlh_pdo_mode = LCK_CR;
317                         break;
318                 case LCK_PW:
319                         lh->mlh_pdo_mode = LCK_CW;
320                         break;
321                 default:
322                         CERROR("Not expected lock type (0x%x)\n",
323                                (int)lh->mlh_reg_mode);
324                         LBUG();
325                 }
326         }
327
328         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
329         EXIT;
330 }
331
332 static int mdt_getstatus(struct mdt_thread_info *info)
333 {
334         struct mdt_device *mdt  = info->mti_mdt;
335         struct md_device  *next = mdt->mdt_child;
336         struct mdt_body   *repbody;
337         int                rc;
338
339         ENTRY;
340
341         rc = mdt_check_ucred(info);
342         if (rc)
343                 RETURN(err_serious(rc));
344
345         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
346                 RETURN(err_serious(-ENOMEM));
347
348         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
349         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
350         if (rc != 0)
351                 RETURN(rc);
352
353         repbody->valid |= OBD_MD_FLID;
354
355         if (mdt->mdt_opts.mo_mds_capa &&
356             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
357                 struct mdt_object  *root;
358                 struct lustre_capa *capa;
359
360                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
361                 if (IS_ERR(root))
362                         RETURN(PTR_ERR(root));
363
364                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
365                 LASSERT(capa);
366                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
367                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
368                                  0);
369                 mdt_object_put(info->mti_env, root);
370                 if (rc == 0)
371                         repbody->valid |= OBD_MD_FLMDSCAPA;
372         }
373
374         RETURN(rc);
375 }
376
377 static int mdt_statfs(struct mdt_thread_info *info)
378 {
379         struct ptlrpc_request           *req = mdt_info_req(info);
380         struct md_device                *next = info->mti_mdt->mdt_child;
381         struct ptlrpc_service_part      *svcpt;
382         struct obd_statfs               *osfs;
383         int                             rc;
384
385         ENTRY;
386
387         svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
388
389         /* This will trigger a watchdog timeout */
390         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
391                          (MDT_SERVICE_WATCHDOG_FACTOR *
392                           at_get(&svcpt->scp_at_estimate)) + 1);
393
394         rc = mdt_check_ucred(info);
395         if (rc)
396                 RETURN(err_serious(rc));
397
398         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
399                 RETURN(err_serious(-ENOMEM));
400
401         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
402         if (!osfs)
403                 RETURN(-EPROTO);
404
405         /** statfs information are cached in the mdt_device */
406         if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
407                                cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
408                 /** statfs data is too old, get up-to-date one */
409                 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
410                 if (rc)
411                         RETURN(rc);
412                 cfs_spin_lock(&info->mti_mdt->mdt_osfs_lock);
413                 info->mti_mdt->mdt_osfs = *osfs;
414                 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
415                 cfs_spin_unlock(&info->mti_mdt->mdt_osfs_lock);
416         } else {
417                 /** use cached statfs data */
418                 cfs_spin_lock(&info->mti_mdt->mdt_osfs_lock);
419                 *osfs = info->mti_mdt->mdt_osfs;
420                 cfs_spin_unlock(&info->mti_mdt->mdt_osfs_lock);
421         }
422
423         if (rc == 0)
424                 mdt_counter_incr(req, LPROC_MDT_STATFS);
425
426         RETURN(rc);
427 }
428
429 /**
430  * Pack SOM attributes into the reply.
431  * Call under a DLM UPDATE lock.
432  */
433 static void mdt_pack_size2body(struct mdt_thread_info *info,
434                                struct mdt_object *mo)
435 {
436         struct mdt_body *b;
437         struct md_attr *ma = &info->mti_attr;
438
439         LASSERT(ma->ma_attr.la_valid & LA_MODE);
440         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
441
442         /* Check if Size-on-MDS is supported, if this is a regular file,
443          * if SOM is enabled on the object and if SOM cache exists and valid.
444          * Otherwise do not pack Size-on-MDS attributes to the reply. */
445         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
446             !S_ISREG(ma->ma_attr.la_mode) ||
447             !mdt_object_is_som_enabled(mo) ||
448             !(ma->ma_valid & MA_SOM))
449                 return;
450
451         b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
452         b->size = ma->ma_som->msd_size;
453         b->blocks = ma->ma_som->msd_blocks;
454 }
455
456 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
457                         const struct lu_attr *attr, const struct lu_fid *fid)
458 {
459         struct md_attr *ma = &info->mti_attr;
460
461         LASSERT(ma->ma_valid & MA_INODE);
462
463         b->atime      = attr->la_atime;
464         b->mtime      = attr->la_mtime;
465         b->ctime      = attr->la_ctime;
466         b->mode       = attr->la_mode;
467         b->size       = attr->la_size;
468         b->blocks     = attr->la_blocks;
469         b->uid        = attr->la_uid;
470         b->gid        = attr->la_gid;
471         b->flags      = attr->la_flags;
472         b->nlink      = attr->la_nlink;
473         b->rdev       = attr->la_rdev;
474
475         /*XXX should pack the reply body according to lu_valid*/
476         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
477                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
478                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
479                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
480
481         if (!S_ISREG(attr->la_mode)) {
482                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
483         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
484                 /* means no objects are allocated on osts. */
485                 LASSERT(!(ma->ma_valid & MA_LOV));
486                 /* just ignore blocks occupied by extend attributes on MDS */
487                 b->blocks = 0;
488                 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
489                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
490         }
491
492         if (fid) {
493                 b->fid1 = *fid;
494                 b->valid |= OBD_MD_FLID;
495
496                 /* FIXME: these should be fixed when new igif ready.*/
497                 b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
498                 b->generation = fid_ver(fid);  /* 1.6 compatibility */
499                 b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
500
501                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
502                                 PFID(fid), b->nlink, b->mode, b->size);
503         }
504
505         if (info)
506                 mdt_body_reverse_idmap(info, b);
507
508         if (b->valid & OBD_MD_FLSIZE)
509                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
510                        PFID(fid), (unsigned long long)b->size);
511 }
512
513 static inline int mdt_body_has_lov(const struct lu_attr *la,
514                                    const struct mdt_body *body)
515 {
516         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
517                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
518 }
519
520 void mdt_client_compatibility(struct mdt_thread_info *info)
521 {
522         struct mdt_body       *body;
523         struct ptlrpc_request *req = mdt_info_req(info);
524         struct obd_export     *exp = req->rq_export;
525         struct md_attr        *ma = &info->mti_attr;
526         struct lu_attr        *la = &ma->ma_attr;
527         ENTRY;
528
529         if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
530                 /* the client can deal with 16-bit lmm_stripe_count */
531                 RETURN_EXIT;
532
533         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
534
535         if (!mdt_body_has_lov(la, body))
536                 RETURN_EXIT;
537
538         /* now we have a reply with a lov for a client not compatible with the
539          * layout lock so we have to clean the layout generation number */
540         if (S_ISREG(la->la_mode))
541                 ma->ma_lmm->lmm_layout_gen = 0;
542         EXIT;
543 }
544
545 static int mdt_big_lmm_get(const struct lu_env *env, struct mdt_object *o,
546                            struct md_attr *ma)
547 {
548         struct mdt_thread_info *info;
549         int rc;
550         ENTRY;
551
552         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
553         LASSERT(info != NULL);
554         LASSERT(ma->ma_lmm_size > 0);
555         LASSERT(info->mti_big_lmm_used == 0);
556         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
557                           XATTR_NAME_LOV);
558         if (rc < 0)
559                 RETURN(rc);
560
561         /* big_lmm may need to be grown */
562         if (info->mti_big_lmmsize < rc) {
563                 int size = size_roundup_power2(rc);
564
565                 if (info->mti_big_lmmsize > 0) {
566                         /* free old buffer */
567                         LASSERT(info->mti_big_lmm);
568                         OBD_FREE_LARGE(info->mti_big_lmm,
569                                        info->mti_big_lmmsize);
570                         info->mti_big_lmm = NULL;
571                         info->mti_big_lmmsize = 0;
572                 }
573
574                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
575                 if (info->mti_big_lmm == NULL)
576                         RETURN(-ENOMEM);
577                 info->mti_big_lmmsize = size;
578         }
579         LASSERT(info->mti_big_lmmsize >= rc);
580
581         info->mti_buf.lb_buf = info->mti_big_lmm;
582         info->mti_buf.lb_len = info->mti_big_lmmsize;
583         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf,
584                           XATTR_NAME_LOV);
585         if (rc < 0)
586                 RETURN(rc);
587
588         info->mti_big_lmm_used = 1;
589         ma->ma_valid |= MA_LOV;
590         ma->ma_lmm = info->mti_big_lmm;
591         ma->ma_lmm_size = rc;
592
593         /* update mdt_max_mdsize so all clients will be aware about that */
594         if (info->mti_mdt->mdt_max_mdsize < rc)
595                 info->mti_mdt->mdt_max_mdsize = rc;
596
597         RETURN(0);
598 }
599
600 int mdt_attr_get_lov(struct mdt_thread_info *info,
601                      struct mdt_object *o, struct md_attr *ma)
602 {
603         struct md_object *next = mdt_object_child(o);
604         struct lu_buf    *buf = &info->mti_buf;
605         int rc;
606
607         buf->lb_buf = ma->ma_lmm;
608         buf->lb_len = ma->ma_lmm_size;
609         rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
610         if (rc > 0) {
611                 ma->ma_lmm_size = rc;
612                 ma->ma_valid |= MA_LOV;
613                 rc = 0;
614         } else if (rc == -ENODATA) {
615                 /* no LOV EA */
616                 rc = 0;
617         } else if (rc == -ERANGE) {
618                 rc = mdt_big_lmm_get(info->mti_env, o, ma);
619         }
620
621         return rc;
622 }
623
624 int mdt_attr_get_complex(struct mdt_thread_info *info,
625                          struct mdt_object *o, struct md_attr *ma)
626 {
627         const struct lu_env *env = info->mti_env;
628         struct md_object    *next = mdt_object_child(o);
629         struct lu_buf       *buf = &info->mti_buf;
630         u32                  mode = lu_object_attr(&next->mo_lu);
631         int                  need = ma->ma_need;
632         int                  rc = 0, rc2;
633         ENTRY;
634
635         /* do we really need PFID */
636         LASSERT((ma->ma_need & MA_PFID) == 0);
637
638         ma->ma_valid = 0;
639
640         if (need & MA_INODE) {
641                 ma->ma_need = MA_INODE;
642                 rc = mo_attr_get(env, next, ma);
643                 if (rc)
644                         GOTO(out, rc);
645                 ma->ma_valid |= MA_INODE;
646         }
647
648         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
649                 rc = mdt_attr_get_lov(info, o, ma);
650                 if (rc)
651                         GOTO(out, rc);
652         }
653
654         if (need & MA_LMV && S_ISDIR(mode)) {
655                 buf->lb_buf = ma->ma_lmv;
656                 buf->lb_len = ma->ma_lmv_size;
657                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
658                 if (rc2 > 0) {
659                         ma->ma_lmv_size = rc2;
660                         ma->ma_valid |= MA_LMV;
661                 } else if (rc2 == -ENODATA) {
662                         /* no LMV EA */
663                         ma->ma_lmv_size = 0;
664                 } else
665                         GOTO(out, rc = rc2);
666         }
667
668
669         if (rc == 0 && S_ISREG(mode) && (need & (MA_HSM | MA_SOM))) {
670                 struct lustre_mdt_attrs *lma;
671
672                 lma = (struct lustre_mdt_attrs *)info->mti_xattr_buf;
673                 CLASSERT(sizeof(*lma) <= sizeof(info->mti_xattr_buf));
674
675                 buf->lb_buf = lma;
676                 buf->lb_len = sizeof(info->mti_xattr_buf);
677                 rc = mo_xattr_get(env, next, buf, XATTR_NAME_LMA);
678                 if (rc > 0) {
679                         lustre_lma_swab(lma);
680                         /* Swab and copy LMA */
681                         if (need & MA_HSM) {
682                                 if (lma->lma_compat & LMAC_HSM)
683                                         ma->ma_hsm.mh_flags =
684                                                 lma->lma_flags & HSM_FLAGS_MASK;
685                                 else
686                                         ma->ma_hsm.mh_flags = 0;
687                                 ma->ma_valid |= MA_HSM;
688                         }
689                         /* Copy SOM */
690                         if (need & MA_SOM && lma->lma_compat & LMAC_SOM) {
691                                 LASSERT(ma->ma_som != NULL);
692                                 ma->ma_som->msd_ioepoch = lma->lma_ioepoch;
693                                 ma->ma_som->msd_size    = lma->lma_som_size;
694                                 ma->ma_som->msd_blocks  = lma->lma_som_blocks;
695                                 ma->ma_som->msd_mountid = lma->lma_som_mountid;
696                                 ma->ma_valid |= MA_SOM;
697                         }
698                         rc = 0;
699                 } else if (rc == -ENODATA) {
700                         rc = 0;
701                 }
702         }
703
704 #ifdef CONFIG_FS_POSIX_ACL
705         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
706                 buf->lb_buf = ma->ma_acl;
707                 buf->lb_len = ma->ma_acl_size;
708                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
709                 if (rc2 > 0) {
710                         ma->ma_acl_size = rc2;
711                         ma->ma_valid |= MA_ACL_DEF;
712                 } else if (rc2 == -ENODATA) {
713                         /* no ACLs */
714                         ma->ma_acl_size = 0;
715                 } else
716                         GOTO(out, rc = rc2);
717         }
718 #endif
719 out:
720         ma->ma_need = need;
721         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
722                rc, ma->ma_valid, ma->ma_lmm);
723         RETURN(rc);
724 }
725
726 static int mdt_getattr_internal(struct mdt_thread_info *info,
727                                 struct mdt_object *o, int ma_need)
728 {
729         struct md_object        *next = mdt_object_child(o);
730         const struct mdt_body   *reqbody = info->mti_body;
731         struct ptlrpc_request   *req = mdt_info_req(info);
732         struct md_attr          *ma = &info->mti_attr;
733         struct lu_attr          *la = &ma->ma_attr;
734         struct req_capsule      *pill = info->mti_pill;
735         const struct lu_env     *env = info->mti_env;
736         struct mdt_body         *repbody;
737         struct lu_buf           *buffer = &info->mti_buf;
738         int                     rc;
739         int                     is_root;
740         ENTRY;
741
742         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
743                 RETURN(err_serious(-ENOMEM));
744
745         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
746
747         ma->ma_valid = 0;
748
749         rc = mdt_object_exists(o);
750         if (rc < 0) {
751                 /* This object is located on remote node.*/
752                 repbody->fid1 = *mdt_object_fid(o);
753                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
754                 GOTO(out, rc = 0);
755         }
756
757         buffer->lb_len = reqbody->eadatasize;
758         if (buffer->lb_len > 0)
759                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
760         else
761                 buffer->lb_buf = NULL;
762
763         /* If it is dir object and client require MEA, then we got MEA */
764         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
765             reqbody->valid & OBD_MD_MEA) {
766                 /* Assumption: MDT_MD size is enough for lmv size. */
767                 ma->ma_lmv = buffer->lb_buf;
768                 ma->ma_lmv_size = buffer->lb_len;
769                 ma->ma_need = MA_LMV | MA_INODE;
770         } else {
771                 ma->ma_lmm = buffer->lb_buf;
772                 ma->ma_lmm_size = buffer->lb_len;
773                 ma->ma_need = MA_LOV | MA_INODE;
774         }
775
776         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
777             reqbody->valid & OBD_MD_FLDIREA  &&
778             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
779                 /* get default stripe info for this dir. */
780                 ma->ma_need |= MA_LOV_DEF;
781         }
782         ma->ma_need |= ma_need;
783         if (ma->ma_need & MA_SOM)
784                 ma->ma_som = &info->mti_u.som.data;
785
786         rc = mdt_attr_get_complex(info, o, ma);
787         if (unlikely(rc)) {
788                 CERROR("getattr error for "DFID": %d\n",
789                         PFID(mdt_object_fid(o)), rc);
790                 RETURN(rc);
791         }
792
793         is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
794
795         /* the Lustre protocol supposes to return default striping
796          * on the user-visible root if explicitly requested */
797         if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
798             (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
799                 struct lu_fid      rootfid;
800                 struct mdt_object *root;
801                 struct mdt_device *mdt = info->mti_mdt;
802
803                 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
804                 if (rc)
805                         RETURN(rc);
806                 root = mdt_object_find(env, mdt, &rootfid);
807                 if (IS_ERR(root))
808                         RETURN(PTR_ERR(root));
809                 rc = mdt_attr_get_lov(info, root, ma);
810                 mdt_object_put(info->mti_env, root);
811                 if (unlikely(rc)) {
812                         CERROR("getattr error for "DFID": %d\n",
813                                         PFID(mdt_object_fid(o)), rc);
814                         RETURN(rc);
815                 }
816         }
817
818         if (likely(ma->ma_valid & MA_INODE))
819                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
820         else
821                 RETURN(-EFAULT);
822
823         if (mdt_body_has_lov(la, reqbody)) {
824                 if (ma->ma_valid & MA_LOV) {
825                         LASSERT(ma->ma_lmm_size);
826                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
827                         repbody->eadatasize = ma->ma_lmm_size;
828                         if (S_ISDIR(la->la_mode))
829                                 repbody->valid |= OBD_MD_FLDIREA;
830                         else
831                                 repbody->valid |= OBD_MD_FLEASIZE;
832                 }
833                 if (ma->ma_valid & MA_LMV) {
834                         LASSERT(S_ISDIR(la->la_mode));
835                         repbody->eadatasize = ma->ma_lmv_size;
836                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
837                 }
838         } else if (S_ISLNK(la->la_mode) &&
839                    reqbody->valid & OBD_MD_LINKNAME) {
840                 buffer->lb_buf = ma->ma_lmm;
841                 /* eadatasize from client includes NULL-terminator, so
842                  * there is no need to read it */
843                 buffer->lb_len = reqbody->eadatasize - 1;
844                 rc = mo_readlink(env, next, buffer);
845                 if (unlikely(rc <= 0)) {
846                         CERROR("readlink failed: %d\n", rc);
847                         rc = -EFAULT;
848                 } else {
849                         int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
850
851                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
852                                 rc -= 2;
853                         repbody->valid |= OBD_MD_LINKNAME;
854                         /* we need to report back size with NULL-terminator
855                          * because client expects that */
856                         repbody->eadatasize = rc + 1;
857                         if (repbody->eadatasize != reqbody->eadatasize)
858                                 CERROR("Read shorter symlink %d, expected %d\n",
859                                        rc, reqbody->eadatasize - 1);
860                         /* NULL terminate */
861                         ((char *)ma->ma_lmm)[rc] = 0;
862
863                         /* If the total CDEBUG() size is larger than a page, it
864                          * will print a warning to the console, avoid this by
865                          * printing just the last part of the symlink. */
866                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
867                                print_limit < rc ? "..." : "", print_limit,
868                                (char *)ma->ma_lmm + rc - print_limit, rc);
869                         rc = 0;
870                 }
871         }
872
873         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
874                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
875                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
876                 repbody->valid |= OBD_MD_FLMODEASIZE;
877                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
878                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
879                        repbody->max_cookiesize);
880         }
881
882         if (exp_connect_rmtclient(info->mti_exp) &&
883             reqbody->valid & OBD_MD_FLRMTPERM) {
884                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
885
886                 /* mdt_getattr_lock only */
887                 rc = mdt_pack_remote_perm(info, o, buf);
888                 if (rc) {
889                         repbody->valid &= ~OBD_MD_FLRMTPERM;
890                         repbody->aclsize = 0;
891                         RETURN(rc);
892                 } else {
893                         repbody->valid |= OBD_MD_FLRMTPERM;
894                         repbody->aclsize = sizeof(struct mdt_remote_perm);
895                 }
896         }
897 #ifdef CONFIG_FS_POSIX_ACL
898         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
899                  (reqbody->valid & OBD_MD_FLACL)) {
900                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
901                 buffer->lb_len = req_capsule_get_size(pill,
902                                                       &RMF_ACL, RCL_SERVER);
903                 if (buffer->lb_len > 0) {
904                         rc = mo_xattr_get(env, next, buffer,
905                                           XATTR_NAME_ACL_ACCESS);
906                         if (rc < 0) {
907                                 if (rc == -ENODATA) {
908                                         repbody->aclsize = 0;
909                                         repbody->valid |= OBD_MD_FLACL;
910                                         rc = 0;
911                                 } else if (rc == -EOPNOTSUPP) {
912                                         rc = 0;
913                                 } else {
914                                         CERROR("got acl size: %d\n", rc);
915                                 }
916                         } else {
917                                 repbody->aclsize = rc;
918                                 repbody->valid |= OBD_MD_FLACL;
919                                 rc = 0;
920                         }
921                 }
922         }
923 #endif
924
925         if (reqbody->valid & OBD_MD_FLMDSCAPA &&
926             info->mti_mdt->mdt_opts.mo_mds_capa &&
927             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
928                 struct lustre_capa *capa;
929
930                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
931                 LASSERT(capa);
932                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
933                 rc = mo_capa_get(env, next, capa, 0);
934                 if (rc)
935                         RETURN(rc);
936                 repbody->valid |= OBD_MD_FLMDSCAPA;
937         }
938
939 out:
940         if (rc == 0)
941                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
942
943         RETURN(rc);
944 }
945
946 static int mdt_renew_capa(struct mdt_thread_info *info)
947 {
948         struct mdt_object  *obj = info->mti_object;
949         struct mdt_body    *body;
950         struct lustre_capa *capa, *c;
951         int rc;
952         ENTRY;
953
954         /* if object doesn't exist, or server has disabled capability,
955          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
956          * flag not set.
957          */
958         if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
959             !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
960                 RETURN(0);
961
962         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
963         LASSERT(body != NULL);
964
965         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
966         LASSERT(c);
967
968         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
969         LASSERT(capa);
970
971         *capa = *c;
972         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
973         if (rc == 0)
974                 body->valid |= OBD_MD_FLOSSCAPA;
975         RETURN(rc);
976 }
977
978 static int mdt_getattr(struct mdt_thread_info *info)
979 {
980         struct mdt_object       *obj = info->mti_object;
981         struct req_capsule      *pill = info->mti_pill;
982         struct mdt_body         *reqbody;
983         struct mdt_body         *repbody;
984         mode_t                   mode;
985         int rc, rc2;
986         ENTRY;
987
988         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
989         LASSERT(reqbody);
990
991         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
992                 rc = req_capsule_server_pack(pill);
993                 if (unlikely(rc))
994                         RETURN(err_serious(rc));
995                 rc = mdt_renew_capa(info);
996                 GOTO(out_shrink, rc);
997         }
998
999         LASSERT(obj != NULL);
1000         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
1001
1002         mode = lu_object_attr(&obj->mot_obj.mo_lu);
1003
1004         /* old clients may not report needed easize, use max value then */
1005         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1006                              reqbody->eadatasize == 0 ?
1007                              info->mti_mdt->mdt_max_mdsize :
1008                              reqbody->eadatasize);
1009
1010         rc = req_capsule_server_pack(pill);
1011         if (unlikely(rc != 0))
1012                 RETURN(err_serious(rc));
1013
1014         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1015         LASSERT(repbody != NULL);
1016         repbody->eadatasize = 0;
1017         repbody->aclsize = 0;
1018
1019         if (reqbody->valid & OBD_MD_FLRMTPERM)
1020                 rc = mdt_init_ucred(info, reqbody);
1021         else
1022                 rc = mdt_check_ucred(info);
1023         if (unlikely(rc))
1024                 GOTO(out_shrink, rc);
1025
1026         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1027         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1028
1029         /*
1030          * Don't check capability at all, because rename might getattr for
1031          * remote obj, and at that time no capability is available.
1032          */
1033         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1034         rc = mdt_getattr_internal(info, obj, 0);
1035         if (reqbody->valid & OBD_MD_FLRMTPERM)
1036                 mdt_exit_ucred(info);
1037         EXIT;
1038 out_shrink:
1039         mdt_client_compatibility(info);
1040         rc2 = mdt_fix_reply(info);
1041         if (rc == 0)
1042                 rc = rc2;
1043         return rc;
1044 }
1045
1046 static int mdt_is_subdir(struct mdt_thread_info *info)
1047 {
1048         struct mdt_object     *o = info->mti_object;
1049         struct req_capsule    *pill = info->mti_pill;
1050         const struct mdt_body *body = info->mti_body;
1051         struct mdt_body       *repbody;
1052         int                    rc;
1053         ENTRY;
1054
1055         LASSERT(o != NULL);
1056
1057         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1058
1059         /*
1060          * We save last checked parent fid to @repbody->fid1 for remote
1061          * directory case.
1062          */
1063         LASSERT(fid_is_sane(&body->fid2));
1064         LASSERT(mdt_object_exists(o) > 0);
1065         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1066                            &body->fid2, &repbody->fid1);
1067         if (rc == 0 || rc == -EREMOTE)
1068                 repbody->valid |= OBD_MD_FLID;
1069
1070         RETURN(rc);
1071 }
1072
1073 static int mdt_raw_lookup(struct mdt_thread_info *info,
1074                           struct mdt_object *parent,
1075                           const struct lu_name *lname,
1076                           struct ldlm_reply *ldlm_rep)
1077 {
1078         struct md_object *next = mdt_object_child(info->mti_object);
1079         const struct mdt_body *reqbody = info->mti_body;
1080         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1081         struct mdt_body *repbody;
1082         int rc;
1083         ENTRY;
1084
1085         if (reqbody->valid != OBD_MD_FLID)
1086                 RETURN(0);
1087
1088         LASSERT(!info->mti_cross_ref);
1089
1090         /* Only got the fid of this obj by name */
1091         fid_zero(child_fid);
1092         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1093                         &info->mti_spec);
1094 #if 0
1095         /* XXX is raw_lookup possible as intent operation? */
1096         if (rc != 0) {
1097                 if (rc == -ENOENT)
1098                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1099                 RETURN(rc);
1100         } else
1101                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1102
1103         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1104 #endif
1105         if (rc == 0) {
1106                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1107                 repbody->fid1 = *child_fid;
1108                 repbody->valid = OBD_MD_FLID;
1109         }
1110         RETURN(1);
1111 }
1112
1113 /*
1114  * UPDATE lock should be taken against parent, and be release before exit;
1115  * child_bits lock should be taken against child, and be returned back:
1116  *            (1)normal request should release the child lock;
1117  *            (2)intent request will grant the lock to client.
1118  */
1119 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1120                                  struct mdt_lock_handle *lhc,
1121                                  __u64 child_bits,
1122                                  struct ldlm_reply *ldlm_rep)
1123 {
1124         struct ptlrpc_request  *req       = mdt_info_req(info);
1125         struct mdt_body        *reqbody   = NULL;
1126         struct mdt_object      *parent    = info->mti_object;
1127         struct mdt_object      *child;
1128         struct md_object       *next      = mdt_object_child(parent);
1129         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
1130         struct lu_name         *lname     = NULL;
1131         const char             *name      = NULL;
1132         int                     namelen   = 0;
1133         struct mdt_lock_handle *lhp       = NULL;
1134         struct ldlm_lock       *lock;
1135         struct ldlm_res_id     *res_id;
1136         int                     is_resent;
1137         int                     ma_need = 0;
1138         int                     rc;
1139
1140         ENTRY;
1141
1142         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1143         LASSERT(ergo(is_resent,
1144                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1145
1146         LASSERT(parent != NULL);
1147         name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1148         if (name == NULL)
1149                 RETURN(err_serious(-EFAULT));
1150
1151         namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1152                                        RCL_CLIENT) - 1;
1153         if (!info->mti_cross_ref) {
1154                 /*
1155                  * XXX: Check for "namelen == 0" is for getattr by fid
1156                  * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1157                  * that is the name must contain at least one character and
1158                  * the terminating '\0'
1159                  */
1160                 if (namelen == 0) {
1161                         reqbody = req_capsule_client_get(info->mti_pill,
1162                                                          &RMF_MDT_BODY);
1163                         if (unlikely(reqbody == NULL))
1164                                 RETURN(err_serious(-EFAULT));
1165
1166                         if (unlikely(!fid_is_sane(&reqbody->fid2)))
1167                                 RETURN(err_serious(-EINVAL));
1168
1169                         name = NULL;
1170                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1171                                "ldlm_rep = %p\n",
1172                                PFID(mdt_object_fid(parent)),
1173                                PFID(&reqbody->fid2), ldlm_rep);
1174                 } else {
1175                         lname = mdt_name(info->mti_env, (char *)name, namelen);
1176                         CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1177                                "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1178                                name, ldlm_rep);
1179                 }
1180         }
1181         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1182
1183         rc = mdt_object_exists(parent);
1184         if (unlikely(rc == 0)) {
1185                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1186                                 &parent->mot_obj.mo_lu,
1187                                 "Parent doesn't exist!\n");
1188                 RETURN(-ESTALE);
1189         } else if (!info->mti_cross_ref) {
1190                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1191                          PFID(mdt_object_fid(parent)));
1192         }
1193         if (lname) {
1194                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1195                 if (rc != 0) {
1196                         if (rc > 0)
1197                                 rc = 0;
1198                         RETURN(rc);
1199                 }
1200         }
1201
1202         if (info->mti_cross_ref) {
1203                 /* Only getattr on the child. Parent is on another node. */
1204                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1205                 child = parent;
1206                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1207                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1208
1209                 if (is_resent) {
1210                         /* Do not take lock for resent case. */
1211                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1212                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1213                                  lhc->mlh_reg_lh.cookie);
1214                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
1215                                                 &lock->l_resource->lr_name));
1216                         LDLM_LOCK_PUT(lock);
1217                         rc = 0;
1218                 } else {
1219                         mdt_lock_handle_init(lhc);
1220                         mdt_lock_reg_init(lhc, LCK_PR);
1221
1222                         /*
1223                          * Object's name is on another MDS, no lookup lock is
1224                          * needed here but update is.
1225                          */
1226                         child_bits &= ~MDS_INODELOCK_LOOKUP;
1227                         child_bits |= MDS_INODELOCK_UPDATE;
1228
1229                         rc = mdt_object_lock(info, child, lhc, child_bits,
1230                                              MDT_LOCAL_LOCK);
1231                 }
1232                 if (rc == 0) {
1233                         /* Finally, we can get attr for child. */
1234                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
1235                                          BYPASS_CAPA);
1236                         rc = mdt_getattr_internal(info, child, 0);
1237                         if (unlikely(rc != 0))
1238                                 mdt_object_unlock(info, child, lhc, 1);
1239                 }
1240                 RETURN(rc);
1241         }
1242
1243         if (lname) {
1244                 /* step 1: lock parent only if parent is a directory */
1245                 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1246                         lhp = &info->mti_lh[MDT_LH_PARENT];
1247                         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1248                         rc = mdt_object_lock(info, parent, lhp,
1249                                              MDS_INODELOCK_UPDATE,
1250                                              MDT_LOCAL_LOCK);
1251                         if (unlikely(rc != 0))
1252                                 RETURN(rc);
1253                 }
1254
1255                 /* step 2: lookup child's fid by name */
1256                 fid_zero(child_fid);
1257                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1258                                 &info->mti_spec);
1259
1260                 if (rc != 0) {
1261                         if (rc == -ENOENT)
1262                                 mdt_set_disposition(info, ldlm_rep,
1263                                                     DISP_LOOKUP_NEG);
1264                         GOTO(out_parent, rc);
1265                 } else
1266                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1267         } else {
1268                 *child_fid = reqbody->fid2;
1269                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1270         }
1271
1272         /*
1273          *step 3: find the child object by fid & lock it.
1274          *        regardless if it is local or remote.
1275          */
1276         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1277
1278         if (unlikely(IS_ERR(child)))
1279                 GOTO(out_parent, rc = PTR_ERR(child));
1280         if (is_resent) {
1281                 /* Do not take lock for resent case. */
1282                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1283                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1284                          lhc->mlh_reg_lh.cookie);
1285
1286                 res_id = &lock->l_resource->lr_name;
1287                 if (!fid_res_name_eq(mdt_object_fid(child),
1288                                     &lock->l_resource->lr_name)) {
1289                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1290                                                  &lock->l_resource->lr_name),
1291                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1292                                  (unsigned long)res_id->name[0],
1293                                  (unsigned long)res_id->name[1],
1294                                  (unsigned long)res_id->name[2],
1295                                  PFID(mdt_object_fid(parent)));
1296                           CWARN("Although resent, but still not get child lock"
1297                                 "parent:"DFID" child:"DFID"\n",
1298                                 PFID(mdt_object_fid(parent)),
1299                                 PFID(mdt_object_fid(child)));
1300                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1301                           LDLM_LOCK_PUT(lock);
1302                           GOTO(relock, 0);
1303                 }
1304                 LDLM_LOCK_PUT(lock);
1305                 rc = 0;
1306         } else {
1307 relock:
1308                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1309                 mdt_lock_handle_init(lhc);
1310                 if (child_bits == MDS_INODELOCK_LAYOUT)
1311                         mdt_lock_reg_init(lhc, LCK_CR);
1312                 else
1313                         mdt_lock_reg_init(lhc, LCK_PR);
1314
1315                 if (mdt_object_exists(child) == 0) {
1316                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1317                                         &child->mot_obj.mo_lu,
1318                                         "Object doesn't exist!\n");
1319                         GOTO(out_child, rc = -ENOENT);
1320                 }
1321
1322                 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1323                         struct md_attr *ma = &info->mti_attr;
1324
1325                         ma->ma_valid = 0;
1326                         ma->ma_need = MA_INODE;
1327                         rc = mdt_attr_get_complex(info, child, ma);
1328                         if (unlikely(rc != 0))
1329                                 GOTO(out_child, rc);
1330
1331                         /* layout lock is used only on regular files */
1332                         if ((ma->ma_valid & MA_INODE) &&
1333                             (ma->ma_attr.la_valid & LA_MODE) &&
1334                             !S_ISREG(ma->ma_attr.la_mode))
1335                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1336
1337                         /* If the file has not been changed for some time, we
1338                          * return not only a LOOKUP lock, but also an UPDATE
1339                          * lock and this might save us RPC on later STAT. For
1340                          * directories, it also let negative dentry starts
1341                          * working for this dir. */
1342                         if (ma->ma_valid & MA_INODE &&
1343                             ma->ma_attr.la_valid & LA_CTIME &&
1344                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1345                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1346                                 child_bits |= MDS_INODELOCK_UPDATE;
1347                 }
1348
1349                 rc = mdt_object_lock(info, child, lhc, child_bits,
1350                                      MDT_CROSS_LOCK);
1351
1352                 if (unlikely(rc != 0))
1353                         GOTO(out_child, rc);
1354         }
1355
1356         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1357         /* Get MA_SOM attributes if update lock is given. */
1358         if (lock &&
1359             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1360             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1361                 ma_need = MA_SOM;
1362
1363         /* finally, we can get attr for child. */
1364         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1365         rc = mdt_getattr_internal(info, child, ma_need);
1366         if (unlikely(rc != 0)) {
1367                 mdt_object_unlock(info, child, lhc, 1);
1368         } else if (lock) {
1369                 /* Debugging code. */
1370                 res_id = &lock->l_resource->lr_name;
1371                 LDLM_DEBUG(lock, "Returning lock to client");
1372                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1373                                          &lock->l_resource->lr_name),
1374                          "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1375                          (unsigned long)res_id->name[0],
1376                          (unsigned long)res_id->name[1],
1377                          (unsigned long)res_id->name[2],
1378                          PFID(mdt_object_fid(child)));
1379                 mdt_pack_size2body(info, child);
1380         }
1381         if (lock)
1382                 LDLM_LOCK_PUT(lock);
1383
1384         EXIT;
1385 out_child:
1386         mdt_object_put(info->mti_env, child);
1387 out_parent:
1388         if (lhp)
1389                 mdt_object_unlock(info, parent, lhp, 1);
1390         return rc;
1391 }
1392
1393 /* normal handler: should release the child lock */
1394 static int mdt_getattr_name(struct mdt_thread_info *info)
1395 {
1396         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1397         struct mdt_body        *reqbody;
1398         struct mdt_body        *repbody;
1399         int rc, rc2;
1400         ENTRY;
1401
1402         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1403         LASSERT(reqbody != NULL);
1404         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1405         LASSERT(repbody != NULL);
1406
1407         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1408         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1409         repbody->eadatasize = 0;
1410         repbody->aclsize = 0;
1411
1412         rc = mdt_init_ucred(info, reqbody);
1413         if (unlikely(rc))
1414                 GOTO(out_shrink, rc);
1415
1416         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1417         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1418                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1419                 lhc->mlh_reg_lh.cookie = 0;
1420         }
1421         mdt_exit_ucred(info);
1422         EXIT;
1423 out_shrink:
1424         mdt_client_compatibility(info);
1425         rc2 = mdt_fix_reply(info);
1426         if (rc == 0)
1427                 rc = rc2;
1428         return rc;
1429 }
1430
1431 static const struct lu_device_operations mdt_lu_ops;
1432
1433 static int lu_device_is_mdt(struct lu_device *d)
1434 {
1435         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1436 }
1437
1438 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1439                          void *karg, void *uarg);
1440
1441 static int mdt_set_info(struct mdt_thread_info *info)
1442 {
1443         struct ptlrpc_request *req = mdt_info_req(info);
1444         char *key;
1445         void *val;
1446         int keylen, vallen, rc = 0;
1447         ENTRY;
1448
1449         rc = req_capsule_server_pack(info->mti_pill);
1450         if (rc)
1451                 RETURN(rc);
1452
1453         key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1454         if (key == NULL) {
1455                 DEBUG_REQ(D_HA, req, "no set_info key");
1456                 RETURN(-EFAULT);
1457         }
1458
1459         keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1460                                       RCL_CLIENT);
1461
1462         val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1463         if (val == NULL) {
1464                 DEBUG_REQ(D_HA, req, "no set_info val");
1465                 RETURN(-EFAULT);
1466         }
1467
1468         vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1469                                       RCL_CLIENT);
1470
1471         /* Swab any part of val you need to here */
1472         if (KEY_IS(KEY_READ_ONLY)) {
1473                 req->rq_status = 0;
1474                 lustre_msg_set_status(req->rq_repmsg, 0);
1475
1476                 cfs_spin_lock(&req->rq_export->exp_lock);
1477                 if (*(__u32 *)val)
1478                         req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1479                 else
1480                         req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1481                 cfs_spin_unlock(&req->rq_export->exp_lock);
1482
1483         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1484                 struct changelog_setinfo *cs =
1485                         (struct changelog_setinfo *)val;
1486                 if (vallen != sizeof(*cs)) {
1487                         CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1488                         RETURN(-EINVAL);
1489                 }
1490                 if (ptlrpc_req_need_swab(req)) {
1491                         __swab64s(&cs->cs_recno);
1492                         __swab32s(&cs->cs_id);
1493                 }
1494
1495                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1496                                    vallen, val, NULL);
1497                 lustre_msg_set_status(req->rq_repmsg, rc);
1498
1499         } else {
1500                 RETURN(-EINVAL);
1501         }
1502         RETURN(0);
1503 }
1504
1505 static int mdt_connect(struct mdt_thread_info *info)
1506 {
1507         int rc;
1508         struct ptlrpc_request *req;
1509
1510         req = mdt_info_req(info);
1511         rc = target_handle_connect(req);
1512         if (rc == 0) {
1513                 LASSERT(req->rq_export != NULL);
1514                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1515                 rc = mdt_init_sec_level(info);
1516                 if (rc == 0)
1517                         rc = mdt_init_idmap(info);
1518                 if (rc != 0)
1519                         obd_disconnect(class_export_get(req->rq_export));
1520         } else {
1521                 rc = err_serious(rc);
1522         }
1523         return rc;
1524 }
1525
1526 static int mdt_disconnect(struct mdt_thread_info *info)
1527 {
1528         int rc;
1529         ENTRY;
1530
1531         rc = target_handle_disconnect(mdt_info_req(info));
1532         if (rc)
1533                 rc = err_serious(rc);
1534         RETURN(rc);
1535 }
1536
1537 static int mdt_sendpage(struct mdt_thread_info *info,
1538                         struct lu_rdpg *rdpg, int nob)
1539 {
1540         struct ptlrpc_request   *req = mdt_info_req(info);
1541         struct obd_export       *exp = req->rq_export;
1542         struct ptlrpc_bulk_desc *desc;
1543         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1544         int                      tmpcount;
1545         int                      tmpsize;
1546         int                      i;
1547         int                      rc;
1548         ENTRY;
1549
1550         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1551                                     MDS_BULK_PORTAL);
1552         if (desc == NULL)
1553                 RETURN(-ENOMEM);
1554
1555         if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1556                 /* old client requires reply size in it's PAGE_SIZE,
1557                  * which is rdpg->rp_count */
1558                 nob = rdpg->rp_count;
1559
1560         for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1561              i++, tmpcount -= tmpsize) {
1562                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1563                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1564         }
1565
1566         LASSERT(desc->bd_nob == nob);
1567         rc = target_bulk_io(exp, desc, lwi);
1568         ptlrpc_free_bulk(desc);
1569         RETURN(rc);
1570 }
1571
1572 #ifdef HAVE_SPLIT_SUPPORT
1573 /*
1574  * Retrieve dir entry from the page and insert it to the slave object, actually,
1575  * this should be in osd layer, but since it will not in the final product, so
1576  * just do it here and do not define more moo api anymore for this.
1577  */
1578 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1579                               int size)
1580 {
1581         struct mdt_object *object = info->mti_object;
1582         struct lu_fid *lf = &info->mti_tmp_fid2;
1583         struct md_attr *ma = &info->mti_attr;
1584         struct lu_dirpage *dp;
1585         struct lu_dirent *ent;
1586         int rc = 0, offset = 0;
1587         ENTRY;
1588
1589         /* Make sure we have at least one entry. */
1590         if (size == 0)
1591                 RETURN(-EINVAL);
1592
1593         /*
1594          * Disable trans for this name insert, since it will include many trans
1595          * for this.
1596          */
1597         info->mti_no_need_trans = 1;
1598         /*
1599          * When write_dir_page, no need update parent's ctime,
1600          * and no permission check for name_insert.
1601          */
1602         ma->ma_attr.la_ctime = 0;
1603         ma->ma_attr.la_valid = LA_MODE;
1604         ma->ma_valid = MA_INODE;
1605
1606         cfs_kmap(page);
1607         dp = page_address(page);
1608         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1609
1610         for (ent = lu_dirent_start(dp); ent != NULL;
1611              ent = lu_dirent_next(ent)) {
1612                 struct lu_name *lname;
1613                 char *name;
1614
1615                 if (le16_to_cpu(ent->lde_namelen) == 0)
1616                         continue;
1617
1618                 fid_le_to_cpu(lf, &ent->lde_fid);
1619                 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1620                         ma->ma_attr.la_mode = S_IFDIR;
1621                 else
1622                         ma->ma_attr.la_mode = 0;
1623                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1624                 if (name == NULL)
1625                         GOTO(out, rc = -ENOMEM);
1626
1627                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1628                 lname = mdt_name(info->mti_env, name,
1629                                  le16_to_cpu(ent->lde_namelen));
1630                 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1631                 rc = mdo_name_insert(info->mti_env,
1632                                      md_object_next(&object->mot_obj),
1633                                      lname, lf, ma);
1634                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1635                 if (rc) {
1636                         CERROR("Can't insert %*.*s, rc %d\n",
1637                                le16_to_cpu(ent->lde_namelen),
1638                                le16_to_cpu(ent->lde_namelen),
1639                                ent->lde_name, rc);
1640                         GOTO(out, rc);
1641                 }
1642
1643                 offset += lu_dirent_size(ent);
1644                 if (offset >= size)
1645                         break;
1646         }
1647         EXIT;
1648 out:
1649         cfs_kunmap(page);
1650         return rc;
1651 }
1652
1653 static int mdt_bulk_timeout(void *data)
1654 {
1655         ENTRY;
1656
1657         CERROR("mdt bulk transfer timeout \n");
1658
1659         RETURN(1);
1660 }
1661
1662 static int mdt_writepage(struct mdt_thread_info *info)
1663 {
1664         struct ptlrpc_request   *req = mdt_info_req(info);
1665         struct mdt_body         *reqbody;
1666         struct l_wait_info      *lwi;
1667         struct ptlrpc_bulk_desc *desc;
1668         struct page             *page;
1669         int                rc;
1670         ENTRY;
1671
1672
1673         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1674         if (reqbody == NULL)
1675                 RETURN(err_serious(-EFAULT));
1676
1677         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1678         if (desc == NULL)
1679                 RETURN(err_serious(-ENOMEM));
1680
1681         /* allocate the page for the desc */
1682         page = cfs_alloc_page(CFS_ALLOC_STD);
1683         if (page == NULL)
1684                 GOTO(desc_cleanup, rc = -ENOMEM);
1685
1686         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1687                (int)reqbody->size, (int)reqbody->nlink);
1688
1689         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1690                               (int)reqbody->nlink);
1691
1692         rc = sptlrpc_svc_prep_bulk(req, desc);
1693         if (rc != 0)
1694                 GOTO(cleanup_page, rc);
1695         /*
1696          * Check if client was evicted while we were doing i/o before touching
1697          * network.
1698          */
1699         OBD_ALLOC_PTR(lwi);
1700         if (!lwi)
1701                 GOTO(cleanup_page, rc = -ENOMEM);
1702
1703         if (desc->bd_export->exp_failed)
1704                 rc = -ENOTCONN;
1705         else
1706                 rc = ptlrpc_start_bulk_transfer (desc);
1707         if (rc == 0) {
1708                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * CFS_HZ / 4, CFS_HZ,
1709                                             mdt_bulk_timeout, desc);
1710                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1711                                   desc->bd_export->exp_failed, lwi);
1712                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1713                 if (rc == -ETIMEDOUT) {
1714                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1715                         ptlrpc_abort_bulk(desc);
1716                 } else if (desc->bd_export->exp_failed) {
1717                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1718                         rc = -ENOTCONN;
1719                         ptlrpc_abort_bulk(desc);
1720                 } else if (!desc->bd_success ||
1721                            desc->bd_nob_transferred != desc->bd_nob) {
1722                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1723                                   desc->bd_success ?
1724                                   "truncated" : "network error on",
1725                                   desc->bd_nob_transferred, desc->bd_nob);
1726                         /* XXX should this be a different errno? */
1727                         rc = -ETIMEDOUT;
1728                 }
1729         } else {
1730                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1731         }
1732         if (rc)
1733                 GOTO(cleanup_lwi, rc);
1734         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1735
1736 cleanup_lwi:
1737         OBD_FREE_PTR(lwi);
1738 cleanup_page:
1739         cfs_free_page(page);
1740 desc_cleanup:
1741         ptlrpc_free_bulk(desc);
1742         RETURN(rc);
1743 }
1744 #endif
1745
1746 static int mdt_readpage(struct mdt_thread_info *info)
1747 {
1748         struct mdt_object *object = info->mti_object;
1749         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1750         struct mdt_body   *reqbody;
1751         struct mdt_body   *repbody;
1752         int                rc;
1753         int                i;
1754         ENTRY;
1755
1756         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1757                 RETURN(err_serious(-ENOMEM));
1758
1759         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1760         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1761         if (reqbody == NULL || repbody == NULL)
1762                 RETURN(err_serious(-EFAULT));
1763
1764         /*
1765          * prepare @rdpg before calling lower layers and transfer itself. Here
1766          * reqbody->size contains offset of where to start to read and
1767          * reqbody->nlink contains number bytes to read.
1768          */
1769         rdpg->rp_hash = reqbody->size;
1770         if (rdpg->rp_hash != reqbody->size) {
1771                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1772                        rdpg->rp_hash, reqbody->size);
1773                 RETURN(-EFAULT);
1774         }
1775
1776         rdpg->rp_attrs = reqbody->mode;
1777         if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1778                 rdpg->rp_attrs |= LUDA_64BITHASH;
1779         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1780                                 PTLRPC_MAX_BRW_SIZE);
1781         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1782                           CFS_PAGE_SHIFT;
1783         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1784         if (rdpg->rp_pages == NULL)
1785                 RETURN(-ENOMEM);
1786
1787         for (i = 0; i < rdpg->rp_npages; ++i) {
1788                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1789                 if (rdpg->rp_pages[i] == NULL)
1790                         GOTO(free_rdpg, rc = -ENOMEM);
1791         }
1792
1793         /* call lower layers to fill allocated pages with directory data */
1794         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1795         if (rc < 0)
1796                 GOTO(free_rdpg, rc);
1797
1798         /* send pages to client */
1799         rc = mdt_sendpage(info, rdpg, rc);
1800
1801         EXIT;
1802 free_rdpg:
1803
1804         for (i = 0; i < rdpg->rp_npages; i++)
1805                 if (rdpg->rp_pages[i] != NULL)
1806                         cfs_free_page(rdpg->rp_pages[i]);
1807         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1808
1809         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1810                 RETURN(0);
1811
1812         return rc;
1813 }
1814
1815 static int mdt_reint_internal(struct mdt_thread_info *info,
1816                               struct mdt_lock_handle *lhc,
1817                               __u32 op)
1818 {
1819         struct req_capsule      *pill = info->mti_pill;
1820         struct mdt_body         *repbody;
1821         int                      rc = 0, rc2;
1822         ENTRY;
1823
1824
1825         rc = mdt_reint_unpack(info, op);
1826         if (rc != 0) {
1827                 CERROR("Can't unpack reint, rc %d\n", rc);
1828                 RETURN(err_serious(rc));
1829         }
1830
1831         /* for replay (no_create) lmm is not needed, client has it already */
1832         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1833                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1834                                      info->mti_rr.rr_eadatalen);
1835
1836         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1837                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1838                                      info->mti_mdt->mdt_max_cookiesize);
1839
1840         rc = req_capsule_server_pack(pill);
1841         if (rc != 0) {
1842                 CERROR("Can't pack response, rc %d\n", rc);
1843                 RETURN(err_serious(rc));
1844         }
1845
1846         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1847                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1848                 LASSERT(repbody);
1849                 repbody->eadatasize = 0;
1850                 repbody->aclsize = 0;
1851         }
1852
1853         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1854
1855         /* for replay no cookkie / lmm need, because client have this already */
1856         if (info->mti_spec.no_create)
1857                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1858                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1859
1860         rc = mdt_init_ucred_reint(info);
1861         if (rc)
1862                 GOTO(out_shrink, rc);
1863
1864         rc = mdt_fix_attr_ucred(info, op);
1865         if (rc != 0)
1866                 GOTO(out_ucred, rc = err_serious(rc));
1867
1868         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1869                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1870                 GOTO(out_ucred, rc);
1871         }
1872         rc = mdt_reint_rec(info, lhc);
1873         EXIT;
1874 out_ucred:
1875         mdt_exit_ucred(info);
1876 out_shrink:
1877         mdt_client_compatibility(info);
1878         rc2 = mdt_fix_reply(info);
1879         if (rc == 0)
1880                 rc = rc2;
1881         return rc;
1882 }
1883
1884 static long mdt_reint_opcode(struct mdt_thread_info *info,
1885                              const struct req_format **fmt)
1886 {
1887         struct mdt_rec_reint *rec;
1888         long opc;
1889
1890         opc = err_serious(-EFAULT);
1891         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1892         if (rec != NULL) {
1893                 opc = rec->rr_opcode;
1894                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1895                 if (opc < REINT_MAX && fmt[opc] != NULL)
1896                         req_capsule_extend(info->mti_pill, fmt[opc]);
1897                 else {
1898                         CERROR("Unsupported opc: %ld\n", opc);
1899                         opc = err_serious(opc);
1900                 }
1901         }
1902         return opc;
1903 }
1904
1905 static int mdt_reint(struct mdt_thread_info *info)
1906 {
1907         long opc;
1908         int  rc;
1909
1910         static const struct req_format *reint_fmts[REINT_MAX] = {
1911                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1912                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1913                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1914                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1915                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1916                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1917                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1918         };
1919
1920         ENTRY;
1921
1922         opc = mdt_reint_opcode(info, reint_fmts);
1923         if (opc >= 0) {
1924                 /*
1925                  * No lock possible here from client to pass it to reint code
1926                  * path.
1927                  */
1928                 rc = mdt_reint_internal(info, NULL, opc);
1929         } else {
1930                 rc = opc;
1931         }
1932
1933         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1934         RETURN(rc);
1935 }
1936
1937 /* this should sync the whole device */
1938 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1939 {
1940         struct dt_device *dt = mdt->mdt_bottom;
1941         int rc;
1942         ENTRY;
1943
1944         rc = dt->dd_ops->dt_sync(env, dt);
1945         RETURN(rc);
1946 }
1947
1948 /* this should sync this object */
1949 static int mdt_object_sync(struct mdt_thread_info *info)
1950 {
1951         struct md_object *next;
1952         int rc;
1953         ENTRY;
1954
1955         if (!mdt_object_exists(info->mti_object)) {
1956                 CWARN("Non existing object  "DFID"!\n",
1957                       PFID(mdt_object_fid(info->mti_object)));
1958                 RETURN(-ESTALE);
1959         }
1960         next = mdt_object_child(info->mti_object);
1961         rc = mo_object_sync(info->mti_env, next);
1962
1963         RETURN(rc);
1964 }
1965
1966 static int mdt_sync(struct mdt_thread_info *info)
1967 {
1968         struct ptlrpc_request *req = mdt_info_req(info);
1969         struct req_capsule *pill = info->mti_pill;
1970         struct mdt_body *body;
1971         int rc;
1972         ENTRY;
1973
1974         /* The fid may be zero, so we req_capsule_set manually */
1975         req_capsule_set(pill, &RQF_MDS_SYNC);
1976
1977         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1978         if (body == NULL)
1979                 RETURN(err_serious(-EINVAL));
1980
1981         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1982                 RETURN(err_serious(-ENOMEM));
1983
1984         if (fid_seq(&body->fid1) == 0) {
1985                 /* sync the whole device */
1986                 rc = req_capsule_server_pack(pill);
1987                 if (rc == 0)
1988                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1989                 else
1990                         rc = err_serious(rc);
1991         } else {
1992                 /* sync an object */
1993                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1994                 if (rc == 0) {
1995                         rc = mdt_object_sync(info);
1996                         if (rc == 0) {
1997                                 const struct lu_fid *fid;
1998                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1999
2000                                 info->mti_attr.ma_need = MA_INODE;
2001                                 info->mti_attr.ma_valid = 0;
2002                                 rc = mdt_attr_get_complex(info, info->mti_object,
2003                                                           &info->mti_attr);
2004                                 if (rc == 0) {
2005                                         body = req_capsule_server_get(pill,
2006                                                                 &RMF_MDT_BODY);
2007                                         fid = mdt_object_fid(info->mti_object);
2008                                         mdt_pack_attr2body(info, body, la, fid);
2009                                 }
2010                         }
2011                 } else
2012                         rc = err_serious(rc);
2013         }
2014         if (rc == 0)
2015                 mdt_counter_incr(req, LPROC_MDT_SYNC);
2016
2017         RETURN(rc);
2018 }
2019
2020 /*
2021  * Quotacheck handler.
2022  * in-kernel quotacheck isn't supported any more.
2023  */
2024 static int mdt_quotacheck(struct mdt_thread_info *info)
2025 {
2026         struct obd_quotactl     *oqctl;
2027         int                      rc;
2028         ENTRY;
2029
2030         oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
2031         if (oqctl == NULL)
2032                 RETURN(err_serious(-EPROTO));
2033
2034         rc = req_capsule_server_pack(info->mti_pill);
2035         if (rc)
2036                 RETURN(err_serious(rc));
2037
2038         /* deprecated, not used any more */
2039         RETURN(-EOPNOTSUPP);
2040 }
2041
2042 /*
2043  * Handle quota control requests to consult current usage/limit, but also
2044  * to configure quota enforcement
2045  */
2046 static int mdt_quotactl(struct mdt_thread_info *info)
2047 {
2048         struct obd_export       *exp  = info->mti_exp;
2049         struct req_capsule      *pill = info->mti_pill;
2050         struct obd_quotactl     *oqctl, *repoqc;
2051         int                      id, rc;
2052         ENTRY;
2053
2054         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2055         if (oqctl == NULL)
2056                 RETURN(err_serious(-EPROTO));
2057
2058         rc = req_capsule_server_pack(pill);
2059         if (rc)
2060                 RETURN(err_serious(rc));
2061
2062         switch (oqctl->qc_cmd) {
2063         case Q_QUOTACHECK:
2064         case LUSTRE_Q_INVALIDATE:
2065         case LUSTRE_Q_FINVALIDATE:
2066         case Q_QUOTAON:
2067         case Q_QUOTAOFF:
2068         case Q_INITQUOTA:
2069                 /* deprecated, not used any more */
2070                 RETURN(-EOPNOTSUPP);
2071                 /* master quotactl */
2072         case Q_GETINFO:
2073         case Q_SETINFO:
2074         case Q_SETQUOTA:
2075         case Q_GETQUOTA:
2076                 /* slave quotactl */
2077         case Q_GETOINFO:
2078         case Q_GETOQUOTA:
2079                 break;
2080         default:
2081                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2082                 RETURN(-EFAULT);
2083         }
2084
2085         /* map uid/gid for remote client */
2086         id = oqctl->qc_id;
2087         if (exp_connect_rmtclient(exp)) {
2088                 struct lustre_idmap_table *idmap;
2089
2090                 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2091
2092                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2093                              oqctl->qc_cmd != Q_GETINFO))
2094                         RETURN(-EPERM);
2095
2096                 if (oqctl->qc_type == USRQUOTA)
2097                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2098                                                      oqctl->qc_id);
2099                 else if (oqctl->qc_type == GRPQUOTA)
2100                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2101                                                      oqctl->qc_id);
2102                 else
2103                         RETURN(-EINVAL);
2104
2105                 if (id == CFS_IDMAP_NOTFOUND) {
2106                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2107                         RETURN(-EACCES);
2108                 }
2109         }
2110
2111         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2112         if (repoqc == NULL)
2113                 RETURN(err_serious(-EFAULT));
2114
2115         if (oqctl->qc_id != id)
2116                 swap(oqctl->qc_id, id);
2117
2118         switch (oqctl->qc_cmd) {
2119
2120                 /* master quotactl */
2121         case Q_GETINFO:
2122         case Q_SETINFO:
2123         case Q_SETQUOTA:
2124                 /* XXX: not implemented yet. Will be when qmt support is
2125                  * added */
2126                 CERROR("quotactl operation %d not implemented yet\n",
2127                                         oqctl->qc_cmd);
2128                 RETURN(-EOPNOTSUPP);
2129         case Q_GETQUOTA:
2130                 /* XXX: return no limit for now, just for testing purpose */
2131                 memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
2132                 oqctl->qc_dqblk.dqb_valid = QIF_LIMITS;
2133                 rc = 0;
2134                 break;
2135
2136                 /* slave quotactl */
2137         case Q_GETOINFO:
2138         case Q_GETOQUOTA:
2139                 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2140                                    oqctl);
2141                 break;
2142
2143         default:
2144                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2145                 RETURN(-EFAULT);
2146         }
2147
2148         if (oqctl->qc_id != id)
2149                 swap(oqctl->qc_id, id);
2150
2151         *repoqc = *oqctl;
2152         RETURN(rc);
2153 }
2154
2155 /*
2156  * OBD PING and other handlers.
2157  */
2158 static int mdt_obd_ping(struct mdt_thread_info *info)
2159 {
2160         int rc;
2161         ENTRY;
2162
2163         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2164
2165         rc = target_handle_ping(mdt_info_req(info));
2166         if (rc < 0)
2167                 rc = err_serious(rc);
2168         RETURN(rc);
2169 }
2170
2171 /*
2172  * OBD_IDX_READ handler
2173  */
2174 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2175 {
2176         struct mdt_device       *mdt = info->mti_mdt;
2177         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2178         struct idx_info         *req_ii, *rep_ii;
2179         int                      rc, i;
2180         ENTRY;
2181
2182         memset(rdpg, 0, sizeof(*rdpg));
2183         req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2184
2185         /* extract idx_info buffer from request & reply */
2186         req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2187         if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2188                 RETURN(err_serious(-EPROTO));
2189
2190         rc = req_capsule_server_pack(info->mti_pill);
2191         if (rc)
2192                 RETURN(err_serious(rc));
2193
2194         rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2195         if (rep_ii == NULL)
2196                 RETURN(err_serious(-EFAULT));
2197         rep_ii->ii_magic = IDX_INFO_MAGIC;
2198
2199         /* extract hash to start with */
2200         rdpg->rp_hash = req_ii->ii_hash_start;
2201
2202         /* extract requested attributes */
2203         rdpg->rp_attrs = req_ii->ii_attrs;
2204
2205         /* check that fid packed in request is valid and supported */
2206         if (!fid_is_sane(&req_ii->ii_fid))
2207                 RETURN(-EINVAL);
2208         rep_ii->ii_fid = req_ii->ii_fid;
2209
2210         /* copy flags */
2211         rep_ii->ii_flags = req_ii->ii_flags;
2212
2213         /* compute number of pages to allocate, ii_count is the number of 4KB
2214          * containers */
2215         if (req_ii->ii_count <= 0)
2216                 GOTO(out, rc = -EFAULT);
2217         rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2218                                PTLRPC_MAX_BRW_SIZE);
2219         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2220
2221         /* allocate pages to store the containers */
2222         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2223         if (rdpg->rp_pages == NULL)
2224                 GOTO(out, rc = -ENOMEM);
2225         for (i = 0; i < rdpg->rp_npages; i++) {
2226                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2227                 if (rdpg->rp_pages[i] == NULL)
2228                         GOTO(out, rc = -ENOMEM);
2229         }
2230
2231         /* populate pages with key/record pairs */
2232         rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2233         if (rc < 0)
2234                 GOTO(out, rc);
2235
2236         LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2237                  "asked %d > %d\n", rc, rdpg->rp_count);
2238
2239         /* send pages to client */
2240         rc = mdt_sendpage(info, rdpg, rc);
2241
2242         GOTO(out, rc);
2243 out:
2244         if (rdpg->rp_pages) {
2245                 for (i = 0; i < rdpg->rp_npages; i++)
2246                         if (rdpg->rp_pages[i])
2247                                 cfs_free_page(rdpg->rp_pages[i]);
2248                 OBD_FREE(rdpg->rp_pages,
2249                          rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2250         }
2251         return rc;
2252 }
2253
2254 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2255 {
2256         return err_serious(-EOPNOTSUPP);
2257 }
2258
2259 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2260 {
2261         return err_serious(-EOPNOTSUPP);
2262 }
2263
2264
2265 /*
2266  * LLOG handlers.
2267  */
2268
2269 /** clone llog ctxt from child (mdd)
2270  * This allows remote llog (replicator) access.
2271  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2272  * context was originally set up, or we can handle them directly.
2273  * I choose the latter, but that means I need any llog
2274  * contexts set up by child to be accessable by the mdt.  So we clone the
2275  * context into our context list here.
2276  */
2277 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2278                                int idx)
2279 {
2280         struct md_device  *next = mdt->mdt_child;
2281         struct llog_ctxt *ctxt;
2282         int rc;
2283
2284         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2285                 return 0;
2286
2287         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2288         if (rc || ctxt == NULL) {
2289                 CERROR("Can't get mdd ctxt %d\n", rc);
2290                 return rc;
2291         }
2292
2293         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2294         if (rc)
2295                 CERROR("Can't set mdt ctxt %d\n", rc);
2296
2297         return rc;
2298 }
2299
2300 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2301                                  struct mdt_device *mdt, int idx)
2302 {
2303         struct llog_ctxt *ctxt;
2304
2305         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2306         if (ctxt == NULL)
2307                 return 0;
2308         /* Put once for the get we just did, and once for the clone */
2309         llog_ctxt_put(ctxt);
2310         llog_ctxt_put(ctxt);
2311         return 0;
2312 }
2313
2314 static int mdt_llog_create(struct mdt_thread_info *info)
2315 {
2316         int rc;
2317
2318         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2319         rc = llog_origin_handle_open(mdt_info_req(info));
2320         return (rc < 0 ? err_serious(rc) : rc);
2321 }
2322
2323 static int mdt_llog_destroy(struct mdt_thread_info *info)
2324 {
2325         int rc;
2326
2327         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2328         rc = llog_origin_handle_destroy(mdt_info_req(info));
2329         return (rc < 0 ? err_serious(rc) : rc);
2330 }
2331
2332 static int mdt_llog_read_header(struct mdt_thread_info *info)
2333 {
2334         int rc;
2335
2336         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2337         rc = llog_origin_handle_read_header(mdt_info_req(info));
2338         return (rc < 0 ? err_serious(rc) : rc);
2339 }
2340
2341 static int mdt_llog_next_block(struct mdt_thread_info *info)
2342 {
2343         int rc;
2344
2345         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2346         rc = llog_origin_handle_next_block(mdt_info_req(info));
2347         return (rc < 0 ? err_serious(rc) : rc);
2348 }
2349
2350 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2351 {
2352         int rc;
2353
2354         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2355         rc = llog_origin_handle_prev_block(mdt_info_req(info));
2356         return (rc < 0 ? err_serious(rc) : rc);
2357 }
2358
2359
2360 /*
2361  * DLM handlers.
2362  */
2363 static struct ldlm_callback_suite cbs = {
2364         .lcs_completion = ldlm_server_completion_ast,
2365         .lcs_blocking   = ldlm_server_blocking_ast,
2366         .lcs_glimpse    = NULL
2367 };
2368
2369 static int mdt_enqueue(struct mdt_thread_info *info)
2370 {
2371         struct ptlrpc_request *req;
2372         int rc;
2373
2374         /*
2375          * info->mti_dlm_req already contains swapped and (if necessary)
2376          * converted dlm request.
2377          */
2378         LASSERT(info->mti_dlm_req != NULL);
2379
2380         req = mdt_info_req(info);
2381         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2382                                   req, info->mti_dlm_req, &cbs);
2383         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2384         return rc ? err_serious(rc) : req->rq_status;
2385 }
2386
2387 static int mdt_convert(struct mdt_thread_info *info)
2388 {
2389         int rc;
2390         struct ptlrpc_request *req;
2391
2392         LASSERT(info->mti_dlm_req);
2393         req = mdt_info_req(info);
2394         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2395         return rc ? err_serious(rc) : req->rq_status;
2396 }
2397
2398 static int mdt_bl_callback(struct mdt_thread_info *info)
2399 {
2400         CERROR("bl callbacks should not happen on MDS\n");
2401         LBUG();
2402         return err_serious(-EOPNOTSUPP);
2403 }
2404
2405 static int mdt_cp_callback(struct mdt_thread_info *info)
2406 {
2407         CERROR("cp callbacks should not happen on MDS\n");
2408         LBUG();
2409         return err_serious(-EOPNOTSUPP);
2410 }
2411
2412 /*
2413  * sec context handlers
2414  */
2415 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2416 {
2417         int rc;
2418
2419         rc = mdt_handle_idmap(info);
2420
2421         if (unlikely(rc)) {
2422                 struct ptlrpc_request *req = mdt_info_req(info);
2423                 __u32                  opc;
2424
2425                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2426                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2427                         sptlrpc_svc_ctx_invalidate(req);
2428         }
2429
2430         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2431
2432         return rc;
2433 }
2434
2435 static struct mdt_object *mdt_obj(struct lu_object *o)
2436 {
2437         LASSERT(lu_device_is_mdt(o->lo_dev));
2438         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2439 }
2440
2441 struct mdt_object *mdt_object_new(const struct lu_env *env,
2442                                   struct mdt_device *d,
2443                                   const struct lu_fid *f)
2444 {
2445         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2446         struct lu_object *o;
2447         struct mdt_object *m;
2448         ENTRY;
2449
2450         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2451         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2452         if (unlikely(IS_ERR(o)))
2453                 m = (struct mdt_object *)o;
2454         else
2455                 m = mdt_obj(o);
2456         RETURN(m);
2457 }
2458
2459 struct mdt_object *mdt_object_find(const struct lu_env *env,
2460                                    struct mdt_device *d,
2461                                    const struct lu_fid *f)
2462 {
2463         struct lu_object *o;
2464         struct mdt_object *m;
2465         ENTRY;
2466
2467         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2468         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2469         if (unlikely(IS_ERR(o)))
2470                 m = (struct mdt_object *)o;
2471         else
2472                 m = mdt_obj(o);
2473         RETURN(m);
2474 }
2475
2476 /**
2477  * Asyncronous commit for mdt device.
2478  *
2479  * Pass asynchonous commit call down the MDS stack.
2480  *
2481  * \param env environment
2482  * \param mdt the mdt device
2483  */
2484 static void mdt_device_commit_async(const struct lu_env *env,
2485                                     struct mdt_device *mdt)
2486 {
2487         struct dt_device *dt = mdt->mdt_bottom;
2488         int rc;
2489
2490         rc = dt->dd_ops->dt_commit_async(env, dt);
2491         if (unlikely(rc != 0))
2492                 CWARN("async commit start failed with rc = %d", rc);
2493 }
2494
2495 /**
2496  * Mark the lock as "synchonous".
2497  *
2498  * Mark the lock to deffer transaction commit to the unlock time.
2499  *
2500  * \param lock the lock to mark as "synchonous"
2501  *
2502  * \see mdt_is_lock_sync
2503  * \see mdt_save_lock
2504  */
2505 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2506 {
2507         lock->l_ast_data = (void*)1;
2508 }
2509
2510 /**
2511  * Check whehter the lock "synchonous" or not.
2512  *
2513  * \param lock the lock to check
2514  * \retval 1 the lock is "synchonous"
2515  * \retval 0 the lock isn't "synchronous"
2516  *
2517  * \see mdt_set_lock_sync
2518  * \see mdt_save_lock
2519  */
2520 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2521 {
2522         return lock->l_ast_data != NULL;
2523 }
2524
2525 /**
2526  * Blocking AST for mdt locks.
2527  *
2528  * Starts transaction commit if in case of COS lock conflict or
2529  * deffers such a commit to the mdt_save_lock.
2530  *
2531  * \param lock the lock which blocks a request or cancelling lock
2532  * \param desc unused
2533  * \param data unused
2534  * \param flag indicates whether this cancelling or blocking callback
2535  * \retval 0
2536  * \see ldlm_blocking_ast_nocheck
2537  */
2538 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2539                      void *data, int flag)
2540 {
2541         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2542         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2543         int rc;
2544         ENTRY;
2545
2546         if (flag == LDLM_CB_CANCELING)
2547                 RETURN(0);
2548         lock_res_and_lock(lock);
2549         if (lock->l_blocking_ast != mdt_blocking_ast) {
2550                 unlock_res_and_lock(lock);
2551                 RETURN(0);
2552         }
2553         if (mdt_cos_is_enabled(mdt) &&
2554             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2555             lock->l_blocking_lock != NULL &&
2556             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2557                 mdt_set_lock_sync(lock);
2558         }
2559         rc = ldlm_blocking_ast_nocheck(lock);
2560
2561         /* There is no lock conflict if l_blocking_lock == NULL,
2562          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2563          * when the last reference to a local lock was released */
2564         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2565                 struct lu_env env;
2566
2567                 rc = lu_env_init(&env, LCT_LOCAL);
2568                 if (unlikely(rc != 0))
2569                         CWARN("lu_env initialization failed with rc = %d,"
2570                               "cannot start asynchronous commit\n", rc);
2571                 else
2572                         mdt_device_commit_async(&env, mdt);
2573                 lu_env_fini(&env);
2574         }
2575         RETURN(rc);
2576 }
2577
2578 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2579                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2580 {
2581         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2582         ldlm_policy_data_t *policy = &info->mti_policy;
2583         struct ldlm_res_id *res_id = &info->mti_res_id;
2584         int rc;
2585         ENTRY;
2586
2587         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2588         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2589         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2590         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2591
2592         if (mdt_object_exists(o) < 0) {
2593                 if (locality == MDT_CROSS_LOCK) {
2594                         /* cross-ref object fix */
2595                         ibits &= ~MDS_INODELOCK_UPDATE;
2596                         ibits |= MDS_INODELOCK_LOOKUP;
2597                 } else {
2598                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2599                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2600                 }
2601                 /* No PDO lock on remote object */
2602                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2603         }
2604
2605         if (lh->mlh_type == MDT_PDO_LOCK) {
2606                 /* check for exists after object is locked */
2607                 if (mdt_object_exists(o) == 0) {
2608                         /* Non-existent object shouldn't have PDO lock */
2609                         RETURN(-ESTALE);
2610                 } else {
2611                         /* Non-dir object shouldn't have PDO lock */
2612                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2613                                 RETURN(-ENOTDIR);
2614                 }
2615         }
2616
2617         memset(policy, 0, sizeof(*policy));
2618         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2619
2620         /*
2621          * Take PDO lock on whole directory and build correct @res_id for lock
2622          * on part of directory.
2623          */
2624         if (lh->mlh_pdo_hash != 0) {
2625                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2626                 mdt_lock_pdo_mode(info, o, lh);
2627                 if (lh->mlh_pdo_mode != LCK_NL) {
2628                         /*
2629                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2630                          * is never going to be sent to client and we do not
2631                          * want it slowed down due to possible cancels.
2632                          */
2633                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2634                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2635                                           policy, res_id, LDLM_FL_ATOMIC_CB,
2636                                           &info->mti_exp->exp_handle.h_cookie);
2637                         if (unlikely(rc))
2638                                 RETURN(rc);
2639                 }
2640
2641                 /*
2642                  * Finish res_id initializing by name hash marking part of
2643                  * directory which is taking modification.
2644                  */
2645                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2646         }
2647
2648         policy->l_inodebits.bits = ibits;
2649
2650         /*
2651          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2652          * going to be sent to client. If it is - mdt_intent_policy() path will
2653          * fix it up and turn FL_LOCAL flag off.
2654          */
2655         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2656                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2657                           &info->mti_exp->exp_handle.h_cookie);
2658         if (rc)
2659                 mdt_object_unlock(info, o, lh, 1);
2660         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2661                  lh->mlh_pdo_hash != 0 &&
2662                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2663                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2664         }
2665
2666         RETURN(rc);
2667 }
2668
2669 /**
2670  * Save a lock within request object.
2671  *
2672  * Keep the lock referenced until whether client ACK or transaction
2673  * commit happens or release the lock immediately depending on input
2674  * parameters. If COS is ON, a write lock is converted to COS lock
2675  * before saving.
2676  *
2677  * \param info thead info object
2678  * \param h lock handle
2679  * \param mode lock mode
2680  * \param decref force immediate lock releasing
2681  */
2682 static
2683 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2684                    ldlm_mode_t mode, int decref)
2685 {
2686         ENTRY;
2687
2688         if (lustre_handle_is_used(h)) {
2689                 if (decref || !info->mti_has_trans ||
2690                     !(mode & (LCK_PW | LCK_EX))){
2691                         mdt_fid_unlock(h, mode);
2692                 } else {
2693                         struct mdt_device *mdt = info->mti_mdt;
2694                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2695                         struct ptlrpc_request *req = mdt_info_req(info);
2696                         int no_ack = 0;
2697
2698                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2699                                  h->cookie);
2700                         CDEBUG(D_HA, "request = %p reply state = %p"
2701                                " transno = "LPD64"\n",
2702                                req, req->rq_reply_state, req->rq_transno);
2703                         if (mdt_cos_is_enabled(mdt)) {
2704                                 no_ack = 1;
2705                                 ldlm_lock_downgrade(lock, LCK_COS);
2706                                 mode = LCK_COS;
2707                         }
2708                         ptlrpc_save_lock(req, h, mode, no_ack);
2709                         if (mdt_is_lock_sync(lock)) {
2710                                 CDEBUG(D_HA, "found sync-lock,"
2711                                        " async commit started\n");
2712                                 mdt_device_commit_async(info->mti_env,
2713                                                         mdt);
2714                         }
2715                         LDLM_LOCK_PUT(lock);
2716                 }
2717                 h->cookie = 0ull;
2718         }
2719
2720         EXIT;
2721 }
2722
2723 /**
2724  * Unlock mdt object.
2725  *
2726  * Immeditely release the regular lock and the PDO lock or save the
2727  * lock in reqeuest and keep them referenced until client ACK or
2728  * transaction commit.
2729  *
2730  * \param info thread info object
2731  * \param o mdt object
2732  * \param lh mdt lock handle referencing regular and PDO locks
2733  * \param decref force immediate lock releasing
2734  */
2735 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2736                        struct mdt_lock_handle *lh, int decref)
2737 {
2738         ENTRY;
2739
2740         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2741         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2742
2743         EXIT;
2744 }
2745
2746 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2747                                         const struct lu_fid *f,
2748                                         struct mdt_lock_handle *lh,
2749                                         __u64 ibits)
2750 {
2751         struct mdt_object *o;
2752
2753         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2754         if (!IS_ERR(o)) {
2755                 int rc;
2756
2757                 rc = mdt_object_lock(info, o, lh, ibits,
2758                                      MDT_LOCAL_LOCK);
2759                 if (rc != 0) {
2760                         mdt_object_put(info->mti_env, o);
2761                         o = ERR_PTR(rc);
2762                 }
2763         }
2764         return o;
2765 }
2766
2767 void mdt_object_unlock_put(struct mdt_thread_info * info,
2768                            struct mdt_object * o,
2769                            struct mdt_lock_handle *lh,
2770                            int decref)
2771 {
2772         mdt_object_unlock(info, o, lh, decref);
2773         mdt_object_put(info->mti_env, o);
2774 }
2775
2776 static struct mdt_handler *mdt_handler_find(__u32 opc,
2777                                             struct mdt_opc_slice *supported)
2778 {
2779         struct mdt_opc_slice *s;
2780         struct mdt_handler   *h;
2781
2782         h = NULL;
2783         for (s = supported; s->mos_hs != NULL; s++) {
2784                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2785                         h = s->mos_hs + (opc - s->mos_opc_start);
2786                         if (likely(h->mh_opc != 0))
2787                                 LASSERTF(h->mh_opc == opc,
2788                                          "opcode mismatch %d != %d\n",
2789                                          h->mh_opc, opc);
2790                         else
2791                                 h = NULL; /* unsupported opc */
2792                         break;
2793                 }
2794         }
2795         return h;
2796 }
2797
2798 static int mdt_lock_resname_compat(struct mdt_device *m,
2799                                    struct ldlm_request *req)
2800 {
2801         /* XXX something... later. */
2802         return 0;
2803 }
2804
2805 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2806 {
2807         /* XXX something... later. */
2808         return 0;
2809 }
2810
2811 /*
2812  * Generic code handling requests that have struct mdt_body passed in:
2813  *
2814  *  - extract mdt_body from request and save it in @info, if present;
2815  *
2816  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2817  *  @info;
2818  *
2819  *  - if HABEO_CORPUS flag is set for this request type check whether object
2820  *  actually exists on storage (lu_object_exists()).
2821  *
2822  */
2823 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2824 {
2825         const struct mdt_body    *body;
2826         struct mdt_object        *obj;
2827         const struct lu_env      *env;
2828         struct req_capsule       *pill;
2829         int                       rc;
2830         ENTRY;
2831
2832         env = info->mti_env;
2833         pill = info->mti_pill;
2834
2835         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2836         if (body == NULL)
2837                 RETURN(-EFAULT);
2838
2839         if (!(body->valid & OBD_MD_FLID))
2840                 RETURN(0);
2841
2842         if (!fid_is_sane(&body->fid1)) {
2843                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2844                 RETURN(-EINVAL);
2845         }
2846
2847         /*
2848          * Do not get size or any capa fields before we check that request
2849          * contains capa actually. There are some requests which do not, for
2850          * instance MDS_IS_SUBDIR.
2851          */
2852         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2853             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2854                 mdt_set_capainfo(info, 0, &body->fid1,
2855                                  req_capsule_client_get(pill, &RMF_CAPA1));
2856
2857         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2858         if (!IS_ERR(obj)) {
2859                 if ((flags & HABEO_CORPUS) &&
2860                     !mdt_object_exists(obj)) {
2861                         mdt_object_put(env, obj);
2862                         /* for capability renew ENOENT will be handled in
2863                          * mdt_renew_capa */
2864                         if (body->valid & OBD_MD_FLOSSCAPA)
2865                                 rc = 0;
2866                         else
2867                                 rc = -ENOENT;
2868                 } else {
2869                         info->mti_object = obj;
2870                         rc = 0;
2871                 }
2872         } else
2873                 rc = PTR_ERR(obj);
2874
2875         RETURN(rc);
2876 }
2877
2878 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2879 {
2880         struct req_capsule *pill = info->mti_pill;
2881         int rc;
2882         ENTRY;
2883
2884         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2885                 rc = mdt_body_unpack(info, flags);
2886         else
2887                 rc = 0;
2888
2889         if (rc == 0 && (flags & HABEO_REFERO)) {
2890                 /* Pack reply. */
2891                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2892                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2893                                              info->mti_body->eadatasize);
2894                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2895                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2896                                              info->mti_mdt->mdt_max_cookiesize);
2897
2898                 rc = req_capsule_server_pack(pill);
2899         }
2900         RETURN(rc);
2901 }
2902
2903 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2904 {
2905         struct md_device *next = m->mdt_child;
2906
2907         return next->md_ops->mdo_init_capa_ctxt(env, next,
2908                                                 m->mdt_opts.mo_mds_capa,
2909                                                 m->mdt_capa_timeout,
2910                                                 m->mdt_capa_alg,
2911                                                 m->mdt_capa_keys);
2912 }
2913
2914 /*
2915  * Invoke handler for this request opc. Also do necessary preprocessing
2916  * (according to handler ->mh_flags), and post-processing (setting of
2917  * ->last_{xid,committed}).
2918  */
2919 static int mdt_req_handle(struct mdt_thread_info *info,
2920                           struct mdt_handler *h, struct ptlrpc_request *req)
2921 {
2922         int   rc, serious = 0;
2923         __u32 flags;
2924
2925         ENTRY;
2926
2927         LASSERT(h->mh_act != NULL);
2928         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2929         LASSERT(current->journal_info == NULL);
2930
2931         /*
2932          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2933          * to put same checks into handlers like mdt_close(), mdt_reint(),
2934          * etc., without talking to mdt authors first. Checking same thing
2935          * there again is useless and returning 0 error without packing reply
2936          * is buggy! Handlers either pack reply or return error.
2937          *
2938          * We return 0 here and do not send any reply in order to emulate
2939          * network failure. Do not send any reply in case any of NET related
2940          * fail_id has occured.
2941          */
2942         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2943                 RETURN(0);
2944
2945         rc = 0;
2946         flags = h->mh_flags;
2947         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2948
2949         if (h->mh_fmt != NULL) {
2950                 req_capsule_set(info->mti_pill, h->mh_fmt);
2951                 rc = mdt_unpack_req_pack_rep(info, flags);
2952         }
2953
2954         if (rc == 0 && flags & MUTABOR &&
2955             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2956                 /* should it be rq_status? */
2957                 rc = -EROFS;
2958
2959         if (rc == 0 && flags & HABEO_CLAVIS) {
2960                 struct ldlm_request *dlm_req;
2961
2962                 LASSERT(h->mh_fmt != NULL);
2963
2964                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2965                 if (dlm_req != NULL) {
2966                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2967                                         LDLM_IBITS &&
2968                                      dlm_req->lock_desc.l_policy_data.\
2969                                         l_inodebits.bits == 0)) {
2970                                 /*
2971                                  * Lock without inodebits makes no sense and
2972                                  * will oops later in ldlm. If client miss to
2973                                  * set such bits, do not trigger ASSERTION.
2974                                  *
2975                                  * For liblustre flock case, it maybe zero.
2976                                  */
2977                                 rc = -EPROTO;
2978                         } else {
2979                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2980                                         rc = mdt_lock_resname_compat(
2981                                                                 info->mti_mdt,
2982                                                                 dlm_req);
2983                                 info->mti_dlm_req = dlm_req;
2984                         }
2985                 } else {
2986                         rc = -EFAULT;
2987                 }
2988         }
2989
2990         /* capability setting changed via /proc, needs reinitialize ctxt */
2991         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2992                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2993                 info->mti_mdt->mdt_capa_conf = 0;
2994         }
2995
2996         if (likely(rc == 0)) {
2997                 /*
2998                  * Process request, there can be two types of rc:
2999                  * 1) errors with msg unpack/pack, other failures outside the
3000                  * operation itself. This is counted as serious errors;
3001                  * 2) errors during fs operation, should be placed in rq_status
3002                  * only
3003                  */
3004                 rc = h->mh_act(info);
3005                 if (rc == 0 &&
3006                     !req->rq_no_reply && req->rq_reply_state == NULL) {
3007                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3008                                   "pack reply and returned 0 error\n",
3009                                   h->mh_name);
3010                         LBUG();
3011                 }
3012                 serious = is_serious(rc);
3013                 rc = clear_serious(rc);
3014         } else
3015                 serious = 1;
3016
3017         req->rq_status = rc;
3018
3019         /*
3020          * ELDLM_* codes which > 0 should be in rq_status only as well as
3021          * all non-serious errors.
3022          */
3023         if (rc > 0 || !serious)
3024                 rc = 0;
3025
3026         LASSERT(current->journal_info == NULL);
3027
3028         if (rc == 0 && (flags & HABEO_CLAVIS) &&
3029             info->mti_mdt->mdt_opts.mo_compat_resname) {
3030                 struct ldlm_reply *dlmrep;
3031
3032                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3033                 if (dlmrep != NULL)
3034                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3035         }
3036
3037         /* If we're DISCONNECTing, the mdt_export_data is already freed */
3038         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3039                 target_committed_to_req(req);
3040
3041         if (unlikely(req_is_replay(req) &&
3042                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3043                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3044                 LBUG();
3045         }
3046
3047         target_send_reply(req, rc, info->mti_fail_id);
3048         RETURN(0);
3049 }
3050
3051 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3052 {
3053         lh->mlh_type = MDT_NUL_LOCK;
3054         lh->mlh_reg_lh.cookie = 0ull;
3055         lh->mlh_reg_mode = LCK_MINMODE;
3056         lh->mlh_pdo_lh.cookie = 0ull;
3057         lh->mlh_pdo_mode = LCK_MINMODE;
3058 }
3059
3060 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3061 {
3062         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3063         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3064 }
3065
3066 /*
3067  * Initialize fields of struct mdt_thread_info. Other fields are left in
3068  * uninitialized state, because it's too expensive to zero out whole
3069  * mdt_thread_info (> 1K) on each request arrival.
3070  */
3071 static void mdt_thread_info_init(struct ptlrpc_request *req,
3072                                  struct mdt_thread_info *info)
3073 {
3074         int i;
3075         struct md_capainfo *ci;
3076
3077         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3078         info->mti_pill = &req->rq_pill;
3079
3080         /* lock handle */
3081         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3082                 mdt_lock_handle_init(&info->mti_lh[i]);
3083
3084         /* mdt device: it can be NULL while CONNECT */
3085         if (req->rq_export) {
3086                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3087                 info->mti_exp = req->rq_export;
3088         } else
3089                 info->mti_mdt = NULL;
3090         info->mti_env = req->rq_svc_thread->t_env;
3091         ci = md_capainfo(info->mti_env);
3092         memset(ci, 0, sizeof *ci);
3093         if (req->rq_export) {
3094                 if (exp_connect_rmtclient(req->rq_export))
3095                         ci->mc_auth = LC_ID_CONVERT;
3096                 else if (req->rq_export->exp_connect_flags &
3097                          OBD_CONNECT_MDS_CAPA)
3098                         ci->mc_auth = LC_ID_PLAIN;
3099                 else
3100                         ci->mc_auth = LC_ID_NONE;
3101         }
3102
3103         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3104         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3105         info->mti_mos = NULL;
3106
3107         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3108         info->mti_body = NULL;
3109         info->mti_object = NULL;
3110         info->mti_dlm_req = NULL;
3111         info->mti_has_trans = 0;
3112         info->mti_no_need_trans = 0;
3113         info->mti_cross_ref = 0;
3114         info->mti_opdata = 0;
3115         info->mti_big_lmm_used = 0;
3116
3117         /* To not check for split by default. */
3118         info->mti_spec.sp_ck_split = 0;
3119         info->mti_spec.no_create = 0;
3120 }
3121
3122 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3123 {
3124         int i;
3125
3126         req_capsule_fini(info->mti_pill);
3127         if (info->mti_object != NULL) {
3128                 /*
3129                  * freeing an object may lead to OSD level transaction, do not
3130                  * let it mess with MDT. bz19385.
3131                  */
3132                 info->mti_no_need_trans = 1;
3133                 mdt_object_put(info->mti_env, info->mti_object);
3134                 info->mti_object = NULL;
3135         }
3136         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3137                 mdt_lock_handle_fini(&info->mti_lh[i]);
3138         info->mti_env = NULL;
3139 }
3140
3141 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3142                                        struct obd_device *obd, int *process)
3143 {
3144         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3145         case MDS_CONNECT: /* This will never get here, but for completeness. */
3146         case OST_CONNECT: /* This will never get here, but for completeness. */
3147         case MDS_DISCONNECT:
3148         case OST_DISCONNECT:
3149                *process = 1;
3150                RETURN(0);
3151
3152         case MDS_CLOSE:
3153         case MDS_DONE_WRITING:
3154         case MDS_SYNC: /* used in unmounting */
3155         case OBD_PING:
3156         case MDS_REINT:
3157         case SEQ_QUERY:
3158         case FLD_QUERY:
3159         case LDLM_ENQUEUE:
3160                 *process = target_queue_recovery_request(req, obd);
3161                 RETURN(0);
3162
3163         default:
3164                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3165                 *process = -EAGAIN;
3166                 RETURN(0);
3167         }
3168 }
3169
3170 /*
3171  * Handle recovery. Return:
3172  *        +1: continue request processing;
3173  *       -ve: abort immediately with the given error code;
3174  *         0: send reply with error code in req->rq_status;
3175  */
3176 static int mdt_recovery(struct mdt_thread_info *info)
3177 {
3178         struct ptlrpc_request *req = mdt_info_req(info);
3179         struct obd_device *obd;
3180
3181         ENTRY;
3182
3183         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3184         case MDS_CONNECT:
3185         case SEC_CTX_INIT:
3186         case SEC_CTX_INIT_CONT:
3187         case SEC_CTX_FINI:
3188                 {
3189 #if 0
3190                         int rc;
3191
3192                         rc = mdt_handle_idmap(info);
3193                         if (rc)
3194                                 RETURN(rc);
3195                         else
3196 #endif
3197                                 RETURN(+1);
3198                 }
3199         }
3200
3201         if (unlikely(!class_connected_export(req->rq_export))) {
3202                 CERROR("operation %d on unconnected MDS from %s\n",
3203                        lustre_msg_get_opc(req->rq_reqmsg),
3204                        libcfs_id2str(req->rq_peer));
3205                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3206                  * mds_A will get -ENOTCONN(especially for ping req),
3207                  * which will cause that mds_A deactive timeout, then when
3208                  * mds_A cleanup, the cleanup process will be suspended since
3209                  * deactive timeout is not zero.
3210                  */
3211                 req->rq_status = -ENOTCONN;
3212                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3213                 RETURN(0);
3214         }
3215
3216         /* sanity check: if the xid matches, the request must be marked as a
3217          * resent or replayed */
3218         if (req_xid_is_last(req)) {
3219                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3220                       (MSG_RESENT | MSG_REPLAY))) {
3221                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3222                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3223                                   lustre_msg_get_flags(req->rq_reqmsg));
3224                         LBUG();
3225                         req->rq_status = -ENOTCONN;
3226                         RETURN(-ENOTCONN);
3227                 }
3228         }
3229
3230         /* else: note the opposite is not always true; a RESENT req after a
3231          * failover will usually not match the last_xid, since it was likely
3232          * never committed. A REPLAYed request will almost never match the
3233          * last xid, however it could for a committed, but still retained,
3234          * open. */
3235
3236         obd = req->rq_export->exp_obd;
3237
3238         /* Check for aborted recovery... */
3239         if (unlikely(obd->obd_recovering)) {
3240                 int rc;
3241                 int should_process;
3242                 DEBUG_REQ(D_INFO, req, "Got new replay");
3243                 rc = mdt_filter_recovery_request(req, obd, &should_process);
3244                 if (rc != 0 || !should_process)
3245                         RETURN(rc);
3246                 else if (should_process < 0) {
3247                         req->rq_status = should_process;
3248                         rc = ptlrpc_error(req);
3249                         RETURN(rc);
3250                 }
3251         }
3252         RETURN(+1);
3253 }
3254
3255 static int mdt_msg_check_version(struct lustre_msg *msg)
3256 {
3257         int rc;
3258
3259         switch (lustre_msg_get_opc(msg)) {
3260         case MDS_CONNECT:
3261         case MDS_DISCONNECT:
3262         case OBD_PING:
3263         case SEC_CTX_INIT:
3264         case SEC_CTX_INIT_CONT:
3265         case SEC_CTX_FINI:
3266         case OBD_IDX_READ:
3267                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3268                 if (rc)
3269                         CERROR("bad opc %u version %08x, expecting %08x\n",
3270                                lustre_msg_get_opc(msg),
3271                                lustre_msg_get_version(msg),
3272                                LUSTRE_OBD_VERSION);
3273                 break;
3274         case MDS_GETSTATUS:
3275         case MDS_GETATTR:
3276         case MDS_GETATTR_NAME:
3277         case MDS_STATFS:
3278         case MDS_READPAGE:
3279         case MDS_WRITEPAGE:
3280         case MDS_IS_SUBDIR:
3281         case MDS_REINT:
3282         case MDS_CLOSE:
3283         case MDS_DONE_WRITING:
3284         case MDS_PIN:
3285         case MDS_SYNC:
3286         case MDS_GETXATTR:
3287         case MDS_SETXATTR:
3288         case MDS_SET_INFO:
3289         case MDS_GET_INFO:
3290         case MDS_QUOTACHECK:
3291         case MDS_QUOTACTL:
3292         case QUOTA_DQACQ:
3293         case QUOTA_DQREL:
3294         case SEQ_QUERY:
3295         case FLD_QUERY:
3296                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3297                 if (rc)
3298                         CERROR("bad opc %u version %08x, expecting %08x\n",
3299                                lustre_msg_get_opc(msg),
3300                                lustre_msg_get_version(msg),
3301                                LUSTRE_MDS_VERSION);
3302                 break;
3303         case LDLM_ENQUEUE:
3304         case LDLM_CONVERT:
3305         case LDLM_BL_CALLBACK:
3306         case LDLM_CP_CALLBACK:
3307                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3308                 if (rc)
3309                         CERROR("bad opc %u version %08x, expecting %08x\n",
3310                                lustre_msg_get_opc(msg),
3311                                lustre_msg_get_version(msg),
3312                                LUSTRE_DLM_VERSION);
3313                 break;
3314         case OBD_LOG_CANCEL:
3315         case LLOG_ORIGIN_HANDLE_CREATE:
3316         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3317         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3318         case LLOG_ORIGIN_HANDLE_CLOSE:
3319         case LLOG_ORIGIN_HANDLE_DESTROY:
3320         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3321         case LLOG_CATINFO:
3322                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3323                 if (rc)
3324                         CERROR("bad opc %u version %08x, expecting %08x\n",
3325                                lustre_msg_get_opc(msg),
3326                                lustre_msg_get_version(msg),
3327                                LUSTRE_LOG_VERSION);
3328                 break;
3329         default:
3330                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3331                 rc = -ENOTSUPP;
3332         }
3333         return rc;
3334 }
3335
3336 static int mdt_handle0(struct ptlrpc_request *req,
3337                        struct mdt_thread_info *info,
3338                        struct mdt_opc_slice *supported)
3339 {
3340         struct mdt_handler *h;
3341         struct lustre_msg  *msg;
3342         int                 rc;
3343
3344         ENTRY;
3345
3346         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3347                 RETURN(0);
3348
3349         LASSERT(current->journal_info == NULL);
3350
3351         msg = req->rq_reqmsg;
3352         rc = mdt_msg_check_version(msg);
3353         if (likely(rc == 0)) {
3354                 rc = mdt_recovery(info);
3355                 if (likely(rc == +1)) {
3356                         h = mdt_handler_find(lustre_msg_get_opc(msg),
3357                                              supported);
3358                         if (likely(h != NULL)) {
3359                                 rc = mdt_req_handle(info, h, req);
3360                         } else {
3361                                 CERROR("The unsupported opc: 0x%x\n",
3362                                        lustre_msg_get_opc(msg) );
3363                                 req->rq_status = -ENOTSUPP;
3364                                 rc = ptlrpc_error(req);
3365                                 RETURN(rc);
3366                         }
3367                 }
3368         } else
3369                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3370         RETURN(rc);
3371 }
3372
3373 /*
3374  * MDT handler function called by ptlrpc service thread when request comes.
3375  *
3376  * XXX common "target" functionality should be factored into separate module
3377  * shared by mdt, ost and stand-alone services like fld.
3378  */
3379 static int mdt_handle_common(struct ptlrpc_request *req,
3380                              struct mdt_opc_slice *supported)
3381 {
3382         struct lu_env          *env;
3383         struct mdt_thread_info *info;
3384         int                     rc;
3385         ENTRY;
3386
3387         env = req->rq_svc_thread->t_env;
3388         LASSERT(env != NULL);
3389         LASSERT(env->le_ses != NULL);
3390         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3391         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3392         LASSERT(info != NULL);
3393
3394         mdt_thread_info_init(req, info);
3395
3396         rc = mdt_handle0(req, info, supported);
3397
3398         mdt_thread_info_fini(info);
3399         RETURN(rc);
3400 }
3401
3402 /*
3403  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3404  * as well.
3405  */
3406 int mdt_recovery_handle(struct ptlrpc_request *req)
3407 {
3408         int rc;
3409         ENTRY;
3410
3411         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3412         case FLD_QUERY:
3413                 rc = mdt_handle_common(req, mdt_fld_handlers);
3414                 break;
3415         case SEQ_QUERY:
3416                 rc = mdt_handle_common(req, mdt_seq_handlers);
3417                 break;
3418         default:
3419                 rc = mdt_handle_common(req, mdt_regular_handlers);
3420                 break;
3421         }
3422
3423         RETURN(rc);
3424 }
3425
3426 static int mdt_regular_handle(struct ptlrpc_request *req)
3427 {
3428         return mdt_handle_common(req, mdt_regular_handlers);
3429 }
3430
3431 static int mdt_readpage_handle(struct ptlrpc_request *req)
3432 {
3433         return mdt_handle_common(req, mdt_readpage_handlers);
3434 }
3435
3436 static int mdt_xmds_handle(struct ptlrpc_request *req)
3437 {
3438         return mdt_handle_common(req, mdt_xmds_handlers);
3439 }
3440
3441 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3442 {
3443         return mdt_handle_common(req, mdt_seq_handlers);
3444 }
3445
3446 static int mdt_mdss_handle(struct ptlrpc_request *req)
3447 {
3448         return mdt_handle_common(req, mdt_seq_handlers);
3449 }
3450
3451 static int mdt_dtss_handle(struct ptlrpc_request *req)
3452 {
3453         return mdt_handle_common(req, mdt_seq_handlers);
3454 }
3455
3456 static int mdt_fld_handle(struct ptlrpc_request *req)
3457 {
3458         return mdt_handle_common(req, mdt_fld_handlers);
3459 }
3460
3461 enum mdt_it_code {
3462         MDT_IT_OPEN,
3463         MDT_IT_OCREAT,
3464         MDT_IT_CREATE,
3465         MDT_IT_GETATTR,
3466         MDT_IT_READDIR,
3467         MDT_IT_LOOKUP,
3468         MDT_IT_UNLINK,
3469         MDT_IT_TRUNC,
3470         MDT_IT_GETXATTR,
3471         MDT_IT_LAYOUT,
3472         MDT_IT_NR
3473 };
3474
3475 static int mdt_intent_getattr(enum mdt_it_code opcode,
3476                               struct mdt_thread_info *info,
3477                               struct ldlm_lock **,
3478                               int);
3479 static int mdt_intent_reint(enum mdt_it_code opcode,
3480                             struct mdt_thread_info *info,
3481                             struct ldlm_lock **,
3482                             int);
3483
3484 static struct mdt_it_flavor {
3485         const struct req_format *it_fmt;
3486         __u32                    it_flags;
3487         int                    (*it_act)(enum mdt_it_code ,
3488                                          struct mdt_thread_info *,
3489                                          struct ldlm_lock **,
3490                                          int);
3491         long                     it_reint;
3492 } mdt_it_flavor[] = {
3493         [MDT_IT_OPEN]     = {
3494                 .it_fmt   = &RQF_LDLM_INTENT,
3495                 /*.it_flags = HABEO_REFERO,*/
3496                 .it_flags = 0,
3497                 .it_act   = mdt_intent_reint,
3498                 .it_reint = REINT_OPEN
3499         },
3500         [MDT_IT_OCREAT]   = {
3501                 .it_fmt   = &RQF_LDLM_INTENT,
3502                 .it_flags = MUTABOR,
3503                 .it_act   = mdt_intent_reint,
3504                 .it_reint = REINT_OPEN
3505         },
3506         [MDT_IT_CREATE]   = {
3507                 .it_fmt   = &RQF_LDLM_INTENT,
3508                 .it_flags = MUTABOR,
3509                 .it_act   = mdt_intent_reint,
3510                 .it_reint = REINT_CREATE
3511         },
3512         [MDT_IT_GETATTR]  = {
3513                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3514                 .it_flags = HABEO_REFERO,
3515                 .it_act   = mdt_intent_getattr
3516         },
3517         [MDT_IT_READDIR]  = {
3518                 .it_fmt   = NULL,
3519                 .it_flags = 0,
3520                 .it_act   = NULL
3521         },
3522         [MDT_IT_LOOKUP]   = {
3523                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3524                 .it_flags = HABEO_REFERO,
3525                 .it_act   = mdt_intent_getattr
3526         },
3527         [MDT_IT_UNLINK]   = {
3528                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3529                 .it_flags = MUTABOR,
3530                 .it_act   = NULL,
3531                 .it_reint = REINT_UNLINK
3532         },
3533         [MDT_IT_TRUNC]    = {
3534                 .it_fmt   = NULL,
3535                 .it_flags = MUTABOR,
3536                 .it_act   = NULL
3537         },
3538         [MDT_IT_GETXATTR] = {
3539                 .it_fmt   = NULL,
3540                 .it_flags = 0,
3541                 .it_act   = NULL
3542         },
3543         [MDT_IT_LAYOUT] = {
3544                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3545                 .it_flags = HABEO_REFERO,
3546                 .it_act   = mdt_intent_getattr
3547         }
3548 };
3549
3550 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3551                             struct ldlm_lock **lockp,
3552                             struct ldlm_lock *new_lock,
3553                             struct mdt_lock_handle *lh,
3554                             int flags)
3555 {
3556         struct ptlrpc_request  *req = mdt_info_req(info);
3557         struct ldlm_lock       *lock = *lockp;
3558
3559         /*
3560          * Get new lock only for cases when possible resent did not find any
3561          * lock.
3562          */
3563         if (new_lock == NULL)
3564                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3565
3566         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3567                 lh->mlh_reg_lh.cookie = 0;
3568                 RETURN(0);
3569         }
3570
3571         LASSERTF(new_lock != NULL,
3572                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3573
3574         /*
3575          * If we've already given this lock to a client once, then we should
3576          * have no readers or writers.  Otherwise, we should have one reader
3577          * _or_ writer ref (which will be zeroed below) before returning the
3578          * lock to a client.
3579          */
3580         if (new_lock->l_export == req->rq_export) {
3581                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3582         } else {
3583                 LASSERT(new_lock->l_export == NULL);
3584                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3585         }
3586
3587         *lockp = new_lock;
3588
3589         if (new_lock->l_export == req->rq_export) {
3590                 /*
3591                  * Already gave this to the client, which means that we
3592                  * reconstructed a reply.
3593                  */
3594                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3595                         MSG_RESENT);
3596                 lh->mlh_reg_lh.cookie = 0;
3597                 RETURN(ELDLM_LOCK_REPLACED);
3598         }
3599
3600         /*
3601          * Fixup the lock to be given to the client.
3602          */
3603         lock_res_and_lock(new_lock);
3604         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3605          * possible blocking AST. */
3606         while (new_lock->l_readers > 0) {
3607                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3608                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3609                 new_lock->l_readers--;
3610         }
3611         while (new_lock->l_writers > 0) {
3612                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3613                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3614                 new_lock->l_writers--;
3615         }
3616
3617         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3618         new_lock->l_blocking_ast = lock->l_blocking_ast;
3619         new_lock->l_completion_ast = lock->l_completion_ast;
3620         new_lock->l_remote_handle = lock->l_remote_handle;
3621         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3622
3623         unlock_res_and_lock(new_lock);
3624
3625         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3626                      &new_lock->l_remote_handle,
3627                      &new_lock->l_exp_hash);
3628
3629         LDLM_LOCK_RELEASE(new_lock);
3630         lh->mlh_reg_lh.cookie = 0;
3631
3632         RETURN(ELDLM_LOCK_REPLACED);
3633 }
3634
3635 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3636                                     struct ldlm_lock *new_lock,
3637                                     struct ldlm_lock **old_lock,
3638                                     struct mdt_lock_handle *lh)
3639 {
3640         struct ptlrpc_request  *req = mdt_info_req(info);
3641         struct obd_export      *exp = req->rq_export;
3642         struct lustre_handle    remote_hdl;
3643         struct ldlm_request    *dlmreq;
3644         struct ldlm_lock       *lock;
3645
3646         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3647                 return;
3648
3649         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3650         remote_hdl = dlmreq->lock_handle[0];
3651
3652         /* In the function below, .hs_keycmp resolves to
3653          * ldlm_export_lock_keycmp() */
3654         /* coverity[overrun-buffer-val] */
3655         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3656         if (lock) {
3657                 if (lock != new_lock) {
3658                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3659                         lh->mlh_reg_mode = lock->l_granted_mode;
3660
3661                         LDLM_DEBUG(lock, "Restoring lock cookie");
3662                         DEBUG_REQ(D_DLMTRACE, req,
3663                                   "restoring lock cookie "LPX64,
3664                                   lh->mlh_reg_lh.cookie);
3665                         if (old_lock)
3666                                 *old_lock = LDLM_LOCK_GET(lock);
3667                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3668                         return;
3669                 }
3670
3671                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3672         }
3673
3674         /*
3675          * If the xid matches, then we know this is a resent request, and allow
3676          * it. (It's probably an OPEN, for which we don't send a lock.
3677          */
3678         if (req_xid_is_last(req))
3679                 return;
3680
3681         /*
3682          * This remote handle isn't enqueued, so we never received or processed
3683          * this request.  Clear MSG_RESENT, because it can be handled like any
3684          * normal request now.
3685          */
3686         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3687
3688         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3689                   remote_hdl.cookie);
3690 }
3691
3692 static int mdt_intent_getattr(enum mdt_it_code opcode,
3693                               struct mdt_thread_info *info,
3694                               struct ldlm_lock **lockp,
3695                               int flags)
3696 {
3697         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3698         struct ldlm_lock       *new_lock = NULL;
3699         __u64                   child_bits;
3700         struct ldlm_reply      *ldlm_rep;
3701         struct ptlrpc_request  *req;
3702         struct mdt_body        *reqbody;
3703         struct mdt_body        *repbody;
3704         int                     rc, rc2;
3705         ENTRY;
3706
3707         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3708         LASSERT(reqbody);
3709
3710         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3711         LASSERT(repbody);
3712
3713         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3714         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3715         repbody->eadatasize = 0;
3716         repbody->aclsize = 0;
3717
3718         switch (opcode) {
3719         case MDT_IT_LOOKUP:
3720                 child_bits = MDS_INODELOCK_LOOKUP;
3721                 break;
3722         case MDT_IT_GETATTR:
3723                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3724                 break;
3725         case MDT_IT_LAYOUT: {
3726                 static int printed = 0;
3727
3728                 if (!printed) {
3729                         CERROR("layout lock not supported by this version\n");
3730                         printed = 1;
3731                 }
3732                 GOTO(out_shrink, rc = -EINVAL);
3733                 break;
3734         }
3735         default:
3736                 CERROR("Unsupported intent (%d)\n", opcode);
3737                 GOTO(out_shrink, rc = -EINVAL);
3738         }
3739
3740         rc = mdt_init_ucred(info, reqbody);
3741         if (rc)
3742                 GOTO(out_shrink, rc);
3743
3744         req = info->mti_pill->rc_req;
3745         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3746         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3747
3748         /* Get lock from request for possible resent case. */
3749         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3750
3751         ldlm_rep->lock_policy_res2 =
3752                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3753
3754         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3755                 ldlm_rep->lock_policy_res2 = 0;
3756         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3757             ldlm_rep->lock_policy_res2) {
3758                 lhc->mlh_reg_lh.cookie = 0ull;
3759                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3760         }
3761
3762         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3763         EXIT;
3764 out_ucred:
3765         mdt_exit_ucred(info);
3766 out_shrink:
3767         mdt_client_compatibility(info);
3768         rc2 = mdt_fix_reply(info);
3769         if (rc == 0)
3770                 rc = rc2;
3771         return rc;
3772 }
3773
3774 static int mdt_intent_reint(enum mdt_it_code opcode,
3775                             struct mdt_thread_info *info,
3776                             struct ldlm_lock **lockp,
3777                             int flags)
3778 {
3779         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3780         struct ldlm_reply      *rep = NULL;
3781         long                    opc;
3782         int                     rc;
3783
3784         static const struct req_format *intent_fmts[REINT_MAX] = {
3785                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3786                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3787         };
3788
3789         ENTRY;
3790
3791         opc = mdt_reint_opcode(info, intent_fmts);
3792         if (opc < 0)
3793                 RETURN(opc);
3794
3795         if (mdt_it_flavor[opcode].it_reint != opc) {
3796                 CERROR("Reint code %ld doesn't match intent: %d\n",
3797                        opc, opcode);
3798                 RETURN(err_serious(-EPROTO));
3799         }
3800
3801         /* Get lock from request for possible resent case. */
3802         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3803
3804         rc = mdt_reint_internal(info, lhc, opc);
3805
3806         /* Check whether the reply has been packed successfully. */
3807         if (mdt_info_req(info)->rq_repmsg != NULL)
3808                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3809         if (rep == NULL)
3810                 RETURN(err_serious(-EFAULT));
3811
3812         /* MDC expects this in any case */
3813         if (rc != 0)
3814                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3815
3816         /* Cross-ref case, the lock should be returned to the client */
3817         if (rc == -EREMOTE) {
3818                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3819                 rep->lock_policy_res2 = 0;
3820                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3821                 RETURN(rc);
3822         }
3823         rep->lock_policy_res2 = clear_serious(rc);
3824
3825         if (rep->lock_policy_res2 == -ENOENT &&
3826             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3827                 rep->lock_policy_res2 = 0;
3828
3829         if (rc == -ENOTCONN || rc == -ENODEV ||
3830             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3831                 /*
3832                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3833                  * will be returned by rq_status, and client at ptlrpc layer
3834                  * will detect this, then disconnect, reconnect the import
3835                  * immediately, instead of impacting the following the rpc.
3836                  */
3837                 lhc->mlh_reg_lh.cookie = 0ull;
3838                 RETURN(rc);
3839         } else {
3840                 /*
3841                  * For other cases, the error will be returned by intent.
3842                  * and client will retrieve the result from intent.
3843                  */
3844                  /*
3845                   * FIXME: when open lock is finished, that should be
3846                   * checked here.
3847                   */
3848                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3849                         LASSERTF(rc == 0, "Error occurred but lock handle "
3850                                  "is still in use\n");
3851                         rep->lock_policy_res2 = 0;
3852                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3853                         RETURN(rc);
3854                 } else {
3855                         lhc->mlh_reg_lh.cookie = 0ull;
3856                         RETURN(ELDLM_LOCK_ABORTED);
3857                 }
3858         }
3859 }
3860
3861 static int mdt_intent_code(long itcode)
3862 {
3863         int rc;
3864
3865         switch(itcode) {
3866         case IT_OPEN:
3867                 rc = MDT_IT_OPEN;
3868                 break;
3869         case IT_OPEN|IT_CREAT:
3870                 rc = MDT_IT_OCREAT;
3871                 break;
3872         case IT_CREAT:
3873                 rc = MDT_IT_CREATE;
3874                 break;
3875         case IT_READDIR:
3876                 rc = MDT_IT_READDIR;
3877                 break;
3878         case IT_GETATTR:
3879                 rc = MDT_IT_GETATTR;
3880                 break;
3881         case IT_LOOKUP:
3882                 rc = MDT_IT_LOOKUP;
3883                 break;
3884         case IT_UNLINK:
3885                 rc = MDT_IT_UNLINK;
3886                 break;
3887         case IT_TRUNC:
3888                 rc = MDT_IT_TRUNC;
3889                 break;
3890         case IT_GETXATTR:
3891                 rc = MDT_IT_GETXATTR;
3892                 break;
3893         case IT_LAYOUT:
3894                 rc = MDT_IT_LAYOUT;
3895                 break;
3896         default:
3897                 CERROR("Unknown intent opcode: %ld\n", itcode);
3898                 rc = -EINVAL;
3899                 break;
3900         }
3901         return rc;
3902 }
3903
3904 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3905                           struct ldlm_lock **lockp, int flags)
3906 {
3907         struct req_capsule   *pill;
3908         struct mdt_it_flavor *flv;
3909         int opc;
3910         int rc;
3911         ENTRY;
3912
3913         opc = mdt_intent_code(itopc);
3914         if (opc < 0)
3915                 RETURN(-EINVAL);
3916
3917         pill = info->mti_pill;
3918         flv  = &mdt_it_flavor[opc];
3919
3920         if (flv->it_fmt != NULL)
3921                 req_capsule_extend(pill, flv->it_fmt);
3922
3923         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3924         if (rc == 0) {
3925                 struct ptlrpc_request *req = mdt_info_req(info);
3926                 if (flv->it_flags & MUTABOR &&
3927                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3928                         RETURN(-EROFS);
3929         }
3930         if (rc == 0 && flv->it_act != NULL) {
3931                 /* execute policy */
3932                 rc = flv->it_act(opc, info, lockp, flags);
3933         } else {
3934                 rc = -EOPNOTSUPP;
3935         }
3936         RETURN(rc);
3937 }
3938
3939 static int mdt_intent_policy(struct ldlm_namespace *ns,
3940                              struct ldlm_lock **lockp, void *req_cookie,
3941                              ldlm_mode_t mode, int flags, void *data)
3942 {
3943         struct mdt_thread_info *info;
3944         struct ptlrpc_request  *req  =  req_cookie;
3945         struct ldlm_intent     *it;
3946         struct req_capsule     *pill;
3947         int rc;
3948
3949         ENTRY;
3950
3951         LASSERT(req != NULL);
3952
3953         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3954                                   &mdt_thread_key);
3955         LASSERT(info != NULL);
3956         pill = info->mti_pill;
3957         LASSERT(pill->rc_req == req);
3958
3959         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3960                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3961                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3962                 if (it != NULL) {
3963                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3964                         if (rc == 0)
3965                                 rc = ELDLM_OK;
3966
3967                         /* Lock without inodebits makes no sense and will oops
3968                          * later in ldlm. Let's check it now to see if we have
3969                          * ibits corrupted somewhere in mdt_intent_opc().
3970                          * The case for client miss to set ibits has been
3971                          * processed by others. */
3972                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3973                                         lr_type == LDLM_IBITS,
3974                                      info->mti_dlm_req->lock_desc.\
3975                                         l_policy_data.l_inodebits.bits != 0));
3976                 } else
3977                         rc = err_serious(-EFAULT);
3978         } else {
3979                 /* No intent was provided */
3980                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3981                 rc = req_capsule_server_pack(pill);
3982                 if (rc)
3983                         rc = err_serious(rc);
3984         }
3985         RETURN(rc);
3986 }
3987
3988 static int mdt_seq_fini(const struct lu_env *env,
3989                         struct mdt_device *m)
3990 {
3991         struct md_site *ms = mdt_md_site(m);
3992         ENTRY;
3993
3994         if (ms != NULL) {
3995                 if (ms->ms_server_seq) {
3996                         seq_server_fini(ms->ms_server_seq, env);
3997                         OBD_FREE_PTR(ms->ms_server_seq);
3998                         ms->ms_server_seq = NULL;
3999         }
4000
4001                 if (ms->ms_control_seq) {
4002                         seq_server_fini(ms->ms_control_seq, env);
4003                         OBD_FREE_PTR(ms->ms_control_seq);
4004                         ms->ms_control_seq = NULL;
4005         }
4006
4007                 if (ms->ms_client_seq) {
4008                         seq_client_fini(ms->ms_client_seq);
4009                         OBD_FREE_PTR(ms->ms_client_seq);
4010                         ms->ms_client_seq = NULL;
4011                 }
4012         }
4013
4014         RETURN(0);
4015 }
4016
4017 static int mdt_seq_init(const struct lu_env *env,
4018                         const char *uuid,
4019                         struct mdt_device *m)
4020 {
4021         struct md_site *ms;
4022         char *prefix;
4023         int rc;
4024         ENTRY;
4025
4026         ms = mdt_md_site(m);
4027
4028         /*
4029          * This is sequence-controller node. Init seq-controller server on local
4030          * MDT.
4031          */
4032         if (ms->ms_node_id == 0) {
4033                 LASSERT(ms->ms_control_seq == NULL);
4034
4035                 OBD_ALLOC_PTR(ms->ms_control_seq);
4036                 if (ms->ms_control_seq == NULL)
4037                         RETURN(-ENOMEM);
4038
4039                 rc = seq_server_init(ms->ms_control_seq,
4040                                      m->mdt_bottom, uuid,
4041                                      LUSTRE_SEQ_CONTROLLER,
4042                                      ms,
4043                                      env);
4044
4045                 if (rc)
4046                         GOTO(out_seq_fini, rc);
4047
4048                 OBD_ALLOC_PTR(ms->ms_client_seq);
4049                 if (ms->ms_client_seq == NULL)
4050                         GOTO(out_seq_fini, rc = -ENOMEM);
4051
4052                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4053                 if (prefix == NULL) {
4054                         OBD_FREE_PTR(ms->ms_client_seq);
4055                         GOTO(out_seq_fini, rc = -ENOMEM);
4056                 }
4057
4058                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4059                          uuid);
4060
4061                 /*
4062                  * Init seq-controller client after seq-controller server is
4063                  * ready. Pass ms->ms_control_seq to it for direct talking.
4064                  */
4065                 rc = seq_client_init(ms->ms_client_seq, NULL,
4066                                      LUSTRE_SEQ_METADATA, prefix,
4067                                      ms->ms_control_seq);
4068                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4069
4070                 if (rc)
4071                         GOTO(out_seq_fini, rc);
4072         }
4073
4074         /* Init seq-server on local MDT */
4075         LASSERT(ms->ms_server_seq == NULL);
4076
4077         OBD_ALLOC_PTR(ms->ms_server_seq);
4078         if (ms->ms_server_seq == NULL)
4079                 GOTO(out_seq_fini, rc = -ENOMEM);
4080
4081         rc = seq_server_init(ms->ms_server_seq,
4082                              m->mdt_bottom, uuid,
4083                              LUSTRE_SEQ_SERVER,
4084                              ms,
4085                              env);
4086         if (rc)
4087                 GOTO(out_seq_fini, rc = -ENOMEM);
4088
4089         /* Assign seq-controller client to local seq-server. */
4090         if (ms->ms_node_id == 0) {
4091                 LASSERT(ms->ms_client_seq != NULL);
4092
4093                 rc = seq_server_set_cli(ms->ms_server_seq,
4094                                         ms->ms_client_seq,
4095                                         env);
4096         }
4097
4098         EXIT;
4099 out_seq_fini:
4100         if (rc)
4101                 mdt_seq_fini(env, m);
4102
4103         return rc;
4104 }
4105 /*
4106  * Init client sequence manager which is used by local MDS to talk to sequence
4107  * controller on remote node.
4108  */
4109 static int mdt_seq_init_cli(const struct lu_env *env,
4110                             struct mdt_device *m,
4111                             struct lustre_cfg *cfg)
4112 {
4113         struct md_site    *ms = mdt_md_site(m);
4114         struct obd_device *mdc;
4115         struct obd_uuid   *uuidp, *mdcuuidp;
4116         char              *uuid_str, *mdc_uuid_str;
4117         int                rc;
4118         int                index;
4119         struct mdt_thread_info *info;
4120         char *p, *index_string = lustre_cfg_string(cfg, 2);
4121         ENTRY;
4122
4123         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4124         uuidp = &info->mti_u.uuid[0];
4125         mdcuuidp = &info->mti_u.uuid[1];
4126
4127         LASSERT(index_string);
4128
4129         index = simple_strtol(index_string, &p, 10);
4130         if (*p) {
4131                 CERROR("Invalid index in lustre_cgf, offset 2\n");
4132                 RETURN(-EINVAL);
4133         }
4134
4135         /* check if this is adding the first MDC and controller is not yet
4136          * initialized. */
4137         if (index != 0 || ms->ms_client_seq)
4138                 RETURN(0);
4139
4140         uuid_str = lustre_cfg_string(cfg, 1);
4141         mdc_uuid_str = lustre_cfg_string(cfg, 4);
4142         obd_str2uuid(uuidp, uuid_str);
4143         obd_str2uuid(mdcuuidp, mdc_uuid_str);
4144
4145         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4146         if (!mdc) {
4147                 CERROR("can't find controller MDC by uuid %s\n",
4148                        uuid_str);
4149                 rc = -ENOENT;
4150         } else if (!mdc->obd_set_up) {
4151                 CERROR("target %s not set up\n", mdc->obd_name);
4152                 rc = -EINVAL;
4153         } else {
4154                 LASSERT(ms->ms_control_exp);
4155                 OBD_ALLOC_PTR(ms->ms_client_seq);
4156                 if (ms->ms_client_seq != NULL) {
4157                         char *prefix;
4158
4159                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4160                         if (!prefix)
4161                                 RETURN(-ENOMEM);
4162
4163                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4164                                  mdc->obd_name);
4165
4166                         rc = seq_client_init(ms->ms_client_seq,
4167                                              ms->ms_control_exp,
4168                                              LUSTRE_SEQ_METADATA,
4169                                              prefix, NULL);
4170                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
4171                 } else
4172                         rc = -ENOMEM;
4173
4174                 if (rc)
4175                         RETURN(rc);
4176
4177                 LASSERT(ms->ms_server_seq != NULL);
4178                 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4179                                         env);
4180         }
4181
4182         RETURN(rc);
4183 }
4184
4185 static void mdt_seq_fini_cli(struct mdt_device *m)
4186 {
4187         struct md_site *ms;
4188
4189         ENTRY;
4190
4191         ms = mdt_md_site(m);
4192
4193         if (ms != NULL) {
4194                 if (ms->ms_server_seq)
4195                         seq_server_set_cli(ms->ms_server_seq,
4196                                    NULL, NULL);
4197
4198                 if (ms->ms_control_exp) {
4199                         class_export_put(ms->ms_control_exp);
4200                         ms->ms_control_exp = NULL;
4201                 }
4202         }
4203         EXIT;
4204 }
4205
4206 /*
4207  * FLD wrappers
4208  */
4209 static int mdt_fld_fini(const struct lu_env *env,
4210                         struct mdt_device *m)
4211 {
4212         struct md_site *ms = mdt_md_site(m);
4213         ENTRY;
4214
4215         if (ms && ms->ms_server_fld) {
4216                 fld_server_fini(ms->ms_server_fld, env);
4217                 OBD_FREE_PTR(ms->ms_server_fld);
4218                 ms->ms_server_fld = NULL;
4219         }
4220
4221         RETURN(0);
4222 }
4223
4224 static int mdt_fld_init(const struct lu_env *env,
4225                         const char *uuid,
4226                         struct mdt_device *m)
4227 {
4228         struct md_site *ms;
4229         int rc;
4230         ENTRY;
4231
4232         ms = mdt_md_site(m);
4233
4234         OBD_ALLOC_PTR(ms->ms_server_fld);
4235         if (ms->ms_server_fld == NULL)
4236                 RETURN(rc = -ENOMEM);
4237
4238         rc = fld_server_init(ms->ms_server_fld,
4239                              m->mdt_bottom, uuid,
4240                              env, ms->ms_node_id);
4241         if (rc) {
4242                 OBD_FREE_PTR(ms->ms_server_fld);
4243                 ms->ms_server_fld = NULL;
4244                 RETURN(rc);
4245         }
4246
4247         RETURN(0);
4248 }
4249
4250 /* device init/fini methods */
4251 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4252 {
4253         ENTRY;
4254         if (m->mdt_regular_service != NULL) {
4255                 ptlrpc_unregister_service(m->mdt_regular_service);
4256                 m->mdt_regular_service = NULL;
4257         }
4258         if (m->mdt_readpage_service != NULL) {
4259                 ptlrpc_unregister_service(m->mdt_readpage_service);
4260                 m->mdt_readpage_service = NULL;
4261         }
4262         if (m->mdt_xmds_service != NULL) {
4263                 ptlrpc_unregister_service(m->mdt_xmds_service);
4264                 m->mdt_xmds_service = NULL;
4265         }
4266         if (m->mdt_setattr_service != NULL) {
4267                 ptlrpc_unregister_service(m->mdt_setattr_service);
4268                 m->mdt_setattr_service = NULL;
4269         }
4270         if (m->mdt_mdsc_service != NULL) {
4271                 ptlrpc_unregister_service(m->mdt_mdsc_service);
4272                 m->mdt_mdsc_service = NULL;
4273         }
4274         if (m->mdt_mdss_service != NULL) {
4275                 ptlrpc_unregister_service(m->mdt_mdss_service);
4276                 m->mdt_mdss_service = NULL;
4277         }
4278         if (m->mdt_dtss_service != NULL) {
4279                 ptlrpc_unregister_service(m->mdt_dtss_service);
4280                 m->mdt_dtss_service = NULL;
4281         }
4282         if (m->mdt_fld_service != NULL) {
4283                 ptlrpc_unregister_service(m->mdt_fld_service);
4284                 m->mdt_fld_service = NULL;
4285         }
4286         EXIT;
4287 }
4288
4289 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4290 {
4291         static struct ptlrpc_service_conf conf;
4292         cfs_proc_dir_entry_t *procfs_entry;
4293         int rc = 0;
4294         ENTRY;
4295
4296         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4297         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4298                            "mdt_ldlm_client", m->mdt_ldlm_client);
4299
4300         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4301
4302         conf = (typeof(conf)) {
4303                 .psc_name               = LUSTRE_MDT_NAME,
4304                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4305                 .psc_buf                = {
4306                         .bc_nbufs               = MDS_NBUFS,
4307                         .bc_buf_size            = MDS_BUFSIZE,
4308                         .bc_req_max_size        = MDS_MAXREQSIZE,
4309                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4310                         .bc_req_portal          = MDS_REQUEST_PORTAL,
4311                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4312                 },
4313                 /*
4314                  * We'd like to have a mechanism to set this on a per-device
4315                  * basis, but alas...
4316                  */
4317                 .psc_thr                = {
4318                         .tc_thr_name            = LUSTRE_MDT_NAME,
4319                         .tc_thr_factor          = MDT_THR_FACTOR,
4320                         .tc_nthrs_init          = MDT_NTHRS_INIT,
4321                         .tc_nthrs_base          = MDT_NTHRS_BASE,
4322                         .tc_nthrs_max           = MDT_NTHRS_MAX,
4323                         .tc_nthrs_user          = mds_num_threads,
4324                         .tc_cpu_affinity        = 1,
4325                         .tc_ctx_tags            = LCT_MD_THREAD,
4326                 },
4327                 .psc_cpt                = {
4328                         .cc_pattern             = mds_num_cpts,
4329                 },
4330                 .psc_ops                = {
4331                         .so_req_handler         = mdt_regular_handle,
4332                         .so_req_printer         = target_print_req,
4333                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
4334                 },
4335         };
4336         m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4337         if (IS_ERR(m->mdt_regular_service)) {
4338                 rc = PTR_ERR(m->mdt_regular_service);
4339                 CERROR("failed to start regular mdt service: %d\n", rc);
4340                 m->mdt_regular_service = NULL;
4341
4342                 RETURN(rc);
4343         }
4344
4345         /*
4346          * readpage service configuration. Parameters have to be adjusted,
4347          * ideally.
4348          */
4349         memset(&conf, 0, sizeof(conf));
4350         conf = (typeof(conf)) {
4351                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
4352                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4353                 .psc_buf                = {
4354                         .bc_nbufs               = MDS_NBUFS,
4355                         .bc_buf_size            = MDS_BUFSIZE,
4356                         .bc_req_max_size        = MDS_MAXREQSIZE,
4357                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4358                         .bc_req_portal          = MDS_READPAGE_PORTAL,
4359                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4360                 },
4361                 .psc_thr                = {
4362                         .tc_thr_name            = "mdt_rdpg",
4363                         .tc_thr_factor          = MDT_RDPG_THR_FACTOR,
4364                         .tc_nthrs_init          = MDT_RDPG_NTHRS_INIT,
4365                         .tc_nthrs_base          = MDT_RDPG_NTHRS_BASE,
4366                         .tc_nthrs_max           = MDT_RDPG_NTHRS_MAX,
4367                         .tc_nthrs_user          = mds_rdpg_num_threads,
4368                         .tc_cpu_affinity        = 1,
4369                         .tc_ctx_tags            = LCT_MD_THREAD,
4370                 },
4371                 .psc_cpt                = {
4372                         .cc_pattern             = mds_rdpg_num_cpts,
4373                 },
4374                 .psc_ops                = {
4375                         .so_req_handler         = mdt_readpage_handle,
4376                         .so_req_printer         = target_print_req,
4377                         .so_hpreq_handler       = NULL,
4378                 },
4379         };
4380         m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4381         if (IS_ERR(m->mdt_readpage_service)) {
4382                 rc = PTR_ERR(m->mdt_readpage_service);
4383                 CERROR("failed to start readpage service: %d\n", rc);
4384                 m->mdt_readpage_service = NULL;
4385
4386                 GOTO(err_mdt_svc, rc);
4387         }
4388
4389         /*
4390          * setattr service configuration.
4391          *
4392          * XXX To keep the compatibility with old client(< 2.2), we need to
4393          * preserve this portal for a certain time, it should be removed
4394          * eventually. LU-617.
4395          */
4396         memset(&conf, 0, sizeof(conf));
4397         conf = (typeof(conf)) {
4398                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
4399                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4400                 .psc_buf                = {
4401                         .bc_nbufs               = MDS_NBUFS,
4402                         .bc_buf_size            = MDS_BUFSIZE,
4403                         .bc_req_max_size        = MDS_MAXREQSIZE,
4404                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4405                         .bc_req_portal          = MDS_SETATTR_PORTAL,
4406                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4407                 },
4408                 .psc_thr                = {
4409                         .tc_thr_name            = "mdt_attr",
4410                         .tc_thr_factor          = MDT_SETA_THR_FACTOR,
4411                         .tc_nthrs_init          = MDT_SETA_NTHRS_INIT,
4412                         .tc_nthrs_base          = MDT_SETA_NTHRS_BASE,
4413                         .tc_nthrs_max           = MDT_SETA_NTHRS_MAX,
4414                         .tc_nthrs_user          = mds_attr_num_threads,
4415                         .tc_cpu_affinity        = 1,
4416                         .tc_ctx_tags            = LCT_MD_THREAD,
4417                 },
4418                 .psc_cpt                = {
4419                         .cc_pattern             = mds_attr_num_cpts,
4420                 },
4421                 .psc_ops                = {
4422                         .so_req_handler         = mdt_regular_handle,
4423                         .so_req_printer         = target_print_req,
4424                         .so_hpreq_handler       = NULL,
4425                 },
4426         };
4427         m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4428         if (IS_ERR(m->mdt_setattr_service)) {
4429                 rc = PTR_ERR(m->mdt_setattr_service);
4430                 CERROR("failed to start setattr service: %d\n", rc);
4431                 m->mdt_setattr_service = NULL;
4432
4433                 GOTO(err_mdt_svc, rc);
4434         }
4435
4436         /*
4437          * sequence controller service configuration
4438          */
4439         memset(&conf, 0, sizeof(conf));
4440         conf = (typeof(conf)) {
4441                 .psc_name               = LUSTRE_MDT_NAME "_mdsc",
4442                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4443                 .psc_buf                = {
4444                         .bc_nbufs               = MDS_NBUFS,
4445                         .bc_buf_size            = MDS_BUFSIZE,
4446                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4447                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4448                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
4449                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4450                 },
4451                 .psc_thr                = {
4452                         .tc_thr_name            = "mdt_mdsc",
4453                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4454                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4455                         .tc_ctx_tags            = LCT_MD_THREAD,
4456                 },
4457                 .psc_ops                = {
4458                         .so_req_handler         = mdt_mdsc_handle,
4459                         .so_req_printer         = target_print_req,
4460                         .so_hpreq_handler       = NULL,
4461                 },
4462         };
4463         m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4464         if (IS_ERR(m->mdt_mdsc_service)) {
4465                 rc = PTR_ERR(m->mdt_mdsc_service);
4466                 CERROR("failed to start seq controller service: %d\n", rc);
4467                 m->mdt_mdsc_service = NULL;
4468
4469                 GOTO(err_mdt_svc, rc);
4470         }
4471
4472         /*
4473          * metadata sequence server service configuration
4474          */
4475         memset(&conf, 0, sizeof(conf));
4476         conf = (typeof(conf)) {
4477                 .psc_name               = LUSTRE_MDT_NAME "_mdss",
4478                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4479                 .psc_buf                = {
4480                         .bc_nbufs               = MDS_NBUFS,
4481                         .bc_buf_size            = MDS_BUFSIZE,
4482                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4483                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4484                         .bc_req_portal          = SEQ_METADATA_PORTAL,
4485                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4486                 },
4487                 .psc_thr                = {
4488                         .tc_thr_name            = "mdt_mdss",
4489                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4490                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4491                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4492                 },
4493                 .psc_ops                = {
4494                         .so_req_handler         = mdt_mdss_handle,
4495                         .so_req_printer         = target_print_req,
4496                         .so_hpreq_handler       = NULL,
4497                 },
4498         };
4499         m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4500         if (IS_ERR(m->mdt_mdss_service)) {
4501                 rc = PTR_ERR(m->mdt_mdss_service);
4502                 CERROR("failed to start metadata seq server service: %d\n", rc);
4503                 m->mdt_mdss_service = NULL;
4504
4505                 GOTO(err_mdt_svc, rc);
4506         }
4507
4508         /*
4509          * Data sequence server service configuration. We want to have really
4510          * cluster-wide sequences space. This is why we start only one sequence
4511          * controller which manages space.
4512          */
4513         memset(&conf, 0, sizeof(conf));
4514         conf = (typeof(conf)) {
4515                 .psc_name               = LUSTRE_MDT_NAME "_dtss",
4516                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4517                 .psc_buf                = {
4518                         .bc_nbufs               = MDS_NBUFS,
4519                         .bc_buf_size            = MDS_BUFSIZE,
4520                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4521                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4522                         .bc_req_portal          = SEQ_DATA_PORTAL,
4523                         .bc_rep_portal          = OSC_REPLY_PORTAL,
4524                 },
4525                 .psc_thr                = {
4526                         .tc_thr_name            = "mdt_dtss",
4527                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4528                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4529                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4530                 },
4531                 .psc_ops                = {
4532                         .so_req_handler         = mdt_dtss_handle,
4533                         .so_req_printer         = target_print_req,
4534                         .so_hpreq_handler       = NULL,
4535                 },
4536         };
4537         m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4538         if (IS_ERR(m->mdt_dtss_service)) {
4539                 rc = PTR_ERR(m->mdt_dtss_service);
4540                 CERROR("failed to start data seq server service: %d\n", rc);
4541                 m->mdt_dtss_service = NULL;
4542
4543                 GOTO(err_mdt_svc, rc);
4544         }
4545
4546         /* FLD service start */
4547         memset(&conf, 0, sizeof(conf));
4548         conf = (typeof(conf)) {
4549                 .psc_name            = LUSTRE_MDT_NAME "_fld",
4550                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4551                 .psc_buf                = {
4552                         .bc_nbufs               = MDS_NBUFS,
4553                         .bc_buf_size            = MDS_BUFSIZE,
4554                         .bc_req_max_size        = FLD_MAXREQSIZE,
4555                         .bc_rep_max_size        = FLD_MAXREPSIZE,
4556                         .bc_req_portal          = FLD_REQUEST_PORTAL,
4557                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4558                 },
4559                 .psc_thr                = {
4560                         .tc_thr_name            = "mdt_fld",
4561                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4562                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4563                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
4564                 },
4565                 .psc_ops                = {
4566                         .so_req_handler         = mdt_fld_handle,
4567                         .so_req_printer         = target_print_req,
4568                         .so_hpreq_handler       = NULL,
4569                 },
4570         };
4571         m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4572         if (IS_ERR(m->mdt_fld_service)) {
4573                 rc = PTR_ERR(m->mdt_fld_service);
4574                 CERROR("failed to start fld service: %d\n", rc);
4575                 m->mdt_fld_service = NULL;
4576
4577                 GOTO(err_mdt_svc, rc);
4578         }
4579
4580         /*
4581          * mds-mds service configuration. Separate portal is used to allow
4582          * mds-mds requests be not blocked during recovery.
4583          */
4584         memset(&conf, 0, sizeof(conf));
4585         conf = (typeof(conf)) {
4586                 .psc_name               = LUSTRE_MDT_NAME "_mds",
4587                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4588                 .psc_buf                = {
4589                         .bc_nbufs               = MDS_NBUFS,
4590                         .bc_buf_size            = MDS_BUFSIZE,
4591                         .bc_req_max_size        = MDS_MAXREQSIZE,
4592                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4593                         .bc_req_portal          = MDS_MDS_PORTAL,
4594                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4595                 },
4596                 .psc_thr                = {
4597                         .tc_thr_name            = "mdt_mds",
4598                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4599                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4600                         .tc_ctx_tags            = LCT_MD_THREAD,
4601                 },
4602                 .psc_ops                = {
4603                         .so_req_handler         = mdt_xmds_handle,
4604                         .so_req_printer         = target_print_req,
4605                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
4606                 },
4607         };
4608         m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4609         if (IS_ERR(m->mdt_xmds_service)) {
4610                 rc = PTR_ERR(m->mdt_xmds_service);
4611                 CERROR("failed to start xmds service: %d\n", rc);
4612                 m->mdt_xmds_service = NULL;
4613
4614                 GOTO(err_mdt_svc, rc);
4615         }
4616
4617         EXIT;
4618 err_mdt_svc:
4619         if (rc)
4620                 mdt_stop_ptlrpc_service(m);
4621
4622         return rc;
4623 }
4624
4625 static void mdt_stack_fini(const struct lu_env *env,
4626                            struct mdt_device *m, struct lu_device *top)
4627 {
4628         struct obd_device       *obd = mdt2obd_dev(m);
4629         struct lustre_cfg_bufs  *bufs;
4630         struct lustre_cfg       *lcfg;
4631         struct mdt_thread_info  *info;
4632         char flags[3]="";
4633         ENTRY;
4634
4635         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4636         LASSERT(info != NULL);
4637
4638         lu_dev_del_linkage(top->ld_site, top);
4639
4640         bufs = &info->mti_u.bufs;
4641         /* process cleanup, pass mdt obd name to get obd umount flags */
4642         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4643         if (obd->obd_force)
4644                 strcat(flags, "F");
4645         if (obd->obd_fail)
4646                 strcat(flags, "A");
4647         lustre_cfg_bufs_set_string(bufs, 1, flags);
4648         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4649         if (!lcfg) {
4650                 CERROR("Cannot alloc lcfg!\n");
4651                 return;
4652         }
4653         LASSERT(top);
4654         top->ld_ops->ldo_process_config(env, top, lcfg);
4655         lustre_cfg_free(lcfg);
4656
4657         lu_stack_fini(env, top);
4658         m->mdt_child = NULL;
4659         m->mdt_bottom = NULL;
4660
4661         obd_disconnect(m->mdt_bottom_exp);
4662 }
4663
4664 static struct lu_device *mdt_layer_setup(struct lu_env *env,
4665                                          const char *typename,
4666                                          struct lu_device *child,
4667                                          struct lustre_cfg *cfg)
4668 {
4669         const char            *dev = lustre_cfg_string(cfg, 0);
4670         struct obd_type       *type;
4671         struct lu_device_type *ldt;
4672         struct lu_device      *d;
4673         int rc;
4674         ENTRY;
4675
4676         /* find the type */
4677         type = class_get_type(typename);
4678         if (!type) {
4679                 CERROR("Unknown type: '%s'\n", typename);
4680                 GOTO(out, rc = -ENODEV);
4681         }
4682
4683         rc = lu_env_refill((struct lu_env *)env);
4684         if (rc != 0) {
4685                 CERROR("Failure to refill session: '%d'\n", rc);
4686                 GOTO(out_type, rc);
4687         }
4688
4689         ldt = type->typ_lu;
4690         if (ldt == NULL) {
4691                 CERROR("type: '%s'\n", typename);
4692                 GOTO(out_type, rc = -EINVAL);
4693         }
4694
4695         ldt->ldt_obd_type = type;
4696         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
4697         if (IS_ERR(d)) {
4698                 CERROR("Cannot allocate device: '%s'\n", typename);
4699                 GOTO(out_type, rc = -ENODEV);
4700         }
4701
4702         LASSERT(child->ld_site);
4703         d->ld_site = child->ld_site;
4704
4705         type->typ_refcnt++;
4706         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
4707         if (rc) {
4708                 CERROR("can't init device '%s', rc %d\n", typename, rc);
4709                 GOTO(out_alloc, rc);
4710         }
4711         lu_device_get(d);
4712         lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
4713
4714         lu_dev_add_linkage(d->ld_site, d);
4715         RETURN(d);
4716 out_alloc:
4717         ldt->ldt_ops->ldto_device_free(env, d);
4718         type->typ_refcnt--;
4719 out_type:
4720         class_put_type(type);
4721 out:
4722         return ERR_PTR(rc);
4723 }
4724
4725 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4726                                const char *next, struct obd_export **exp)
4727 {
4728         struct obd_connect_data *data = NULL;
4729         struct obd_device       *obd;
4730         int                      rc;
4731         ENTRY;
4732
4733         OBD_ALLOC_PTR(data);
4734         if (data == NULL)
4735                 GOTO(out, rc = -ENOMEM);
4736
4737         obd = class_name2obd(next);
4738         if (obd == NULL) {
4739                 CERROR("%s: can't locate next device: %s\n",
4740                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4741                 GOTO(out, rc = -ENOTCONN);
4742         }
4743
4744         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4745         data->ocd_version = LUSTRE_VERSION_CODE;
4746
4747         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4748         if (rc) {
4749                 CERROR("%s: cannot connect to next dev %s (%d)\n",
4750                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4751                 GOTO(out, rc);
4752         }
4753
4754 out:
4755         if (data)
4756                 OBD_FREE_PTR(data);
4757         RETURN(rc);
4758 }
4759
4760 static int mdt_stack_init(struct lu_env *env,
4761                           struct mdt_device *m,
4762                           struct lustre_cfg *cfg,
4763                           struct lustre_mount_info  *lmi)
4764 {
4765         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
4766         struct lu_device  *tmp;
4767         struct md_device  *md;
4768         struct lu_device  *child_lu_dev;
4769         char              *osdname;
4770         int rc;
4771         ENTRY;
4772
4773         /* find bottom osd */
4774         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
4775         if (osdname == NULL)
4776                 RETURN(-ENOMEM);
4777
4778         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", lustre_cfg_string(cfg, 0));
4779         rc = mdt_connect_to_next(env, m, osdname, &m->mdt_bottom_exp);
4780         OBD_FREE(osdname, MTI_NAME_MAXLEN);
4781         if (rc)
4782                 RETURN(rc);
4783
4784         tmp = m->mdt_bottom_exp->exp_obd->obd_lu_dev;
4785         LASSERT(tmp);
4786         m->mdt_bottom = lu2dt_dev(tmp);
4787
4788         /* initialize site's pointers: md_site, top device */
4789         d->ld_site = tmp->ld_site;
4790         d->ld_site->ls_top_dev = d;
4791         m->mdt_mite.ms_lu = tmp->ld_site;
4792         tmp->ld_site->ld_md_site = &m->mdt_mite;
4793         LASSERT(d->ld_site);
4794         d = tmp;
4795
4796         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
4797         if (IS_ERR(tmp)) {
4798                 GOTO(out, rc = PTR_ERR(tmp));
4799         }
4800         d = tmp;
4801         md = lu2md_dev(d);
4802
4803         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
4804         if (IS_ERR(tmp)) {
4805                 GOTO(out, rc = PTR_ERR(tmp));
4806         }
4807         d = tmp;
4808         /*set mdd upcall device*/
4809         md_upcall_dev_set(md, lu2md_dev(d));
4810
4811         md = lu2md_dev(d);
4812         /*set cmm upcall device*/
4813         md_upcall_dev_set(md, &m->mdt_md_dev);
4814
4815         m->mdt_child = lu2md_dev(d);
4816
4817         /* process setup config */
4818         tmp = &m->mdt_md_dev.md_lu_dev;
4819         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
4820         if (rc)
4821                 GOTO(out, rc);
4822
4823         /* initialize local objects */
4824         child_lu_dev = &m->mdt_child->md_lu_dev;
4825
4826         rc = child_lu_dev->ld_ops->ldo_prepare(env,
4827                                                &m->mdt_md_dev.md_lu_dev,
4828                                                child_lu_dev);
4829         if (rc)
4830                 GOTO(out, rc);
4831
4832         rc = m->mdt_child->md_ops->mdo_root_get(env, m->mdt_child,
4833                                                 &m->mdt_md_root_fid);
4834
4835 out:
4836         /* fini from last known good lu_device */
4837         if (rc)
4838                 mdt_stack_fini(env, m, d);
4839
4840         return rc;
4841 }
4842
4843 /**
4844  * setup CONFIG_ORIG context, used to access local config log.
4845  * this may need to be rewrite as part of llog rewrite for lu-api.
4846  */
4847 static int mdt_obd_llog_setup(struct obd_device *obd,
4848                               struct lustre_sb_info *lsi)
4849 {
4850         int     rc;
4851
4852         LASSERT(obd->obd_fsops == NULL);
4853
4854         obd->obd_fsops = fsfilt_get_ops(lsi->lsi_fstype);
4855         if (IS_ERR(obd->obd_fsops))
4856                 return PTR_ERR(obd->obd_fsops);
4857
4858         rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4859         if (rc) {
4860                 fsfilt_put_ops(obd->obd_fsops);
4861                 return rc;
4862         }
4863
4864         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4865         obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4866         obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4867         obd->obd_lvfs_ctxt.fs = get_ds();
4868
4869         rc = llog_setup(NULL, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4870                         &llog_lvfs_ops);
4871         if (rc) {
4872                 CERROR("llog_setup() failed: %d\n", rc);
4873                 fsfilt_put_ops(obd->obd_fsops);
4874         }
4875
4876         return rc;
4877 }
4878
4879 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4880 {
4881         struct llog_ctxt *ctxt;
4882
4883         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4884         if (ctxt)
4885                 llog_cleanup(NULL, ctxt);
4886
4887         if (obd->obd_fsops) {
4888                 fsfilt_put_ops(obd->obd_fsops);
4889                 obd->obd_fsops = NULL;
4890         }
4891 }
4892
4893 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4894 {
4895         struct md_device  *next = m->mdt_child;
4896         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4897         struct obd_device *obd = mdt2obd_dev(m);
4898         ENTRY;
4899
4900         target_recovery_fini(obd);
4901
4902         ping_evictor_stop();
4903
4904         mdt_stop_ptlrpc_service(m);
4905         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4906         mdt_obd_llog_cleanup(obd);
4907         obd_exports_barrier(obd);
4908         obd_zombie_barrier();
4909
4910         mdt_procfs_fini(m);
4911
4912         lut_fini(env, &m->mdt_lut);
4913         mdt_fs_cleanup(env, m);
4914         upcall_cache_cleanup(m->mdt_identity_cache);
4915         m->mdt_identity_cache = NULL;
4916
4917         if (m->mdt_namespace != NULL) {
4918                 ldlm_namespace_free(m->mdt_namespace, NULL,
4919                                     d->ld_obd->obd_force);
4920                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4921         }
4922
4923         cfs_free_nidlist(&m->mdt_nosquash_nids);
4924         if (m->mdt_nosquash_str) {
4925                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4926                 m->mdt_nosquash_str = NULL;
4927                 m->mdt_nosquash_strlen = 0;
4928         }
4929
4930         mdt_seq_fini(env, m);
4931         mdt_seq_fini_cli(m);
4932         mdt_fld_fini(env, m);
4933         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4934
4935         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4936         cfs_timer_disarm(&m->mdt_ck_timer);
4937         mdt_ck_thread_stop(m);
4938
4939         /*
4940          * Finish the stack
4941          */
4942         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4943
4944         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4945
4946         server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4947
4948         EXIT;
4949 }
4950
4951 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4952 {
4953         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4954         struct sptlrpc_rule_set  tmp_rset;
4955         int                      rc;
4956
4957         sptlrpc_rule_set_init(&tmp_rset);
4958         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4959         if (rc) {
4960                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4961                        obd->obd_name, rc);
4962                 return rc;
4963         }
4964
4965         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4966
4967         cfs_write_lock(&m->mdt_sptlrpc_lock);
4968         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4969         m->mdt_sptlrpc_rset = tmp_rset;
4970         cfs_write_unlock(&m->mdt_sptlrpc_lock);
4971
4972         return 0;
4973 }
4974
4975 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4976
4977 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4978                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4979 {
4980         struct mdt_thread_info    *info;
4981         struct obd_device         *obd;
4982         const char                *dev = lustre_cfg_string(cfg, 0);
4983         const char                *num = lustre_cfg_string(cfg, 2);
4984         struct lustre_mount_info  *lmi = NULL;
4985         struct lustre_sb_info     *lsi;
4986         struct lu_site            *s;
4987         struct md_site            *mite;
4988         const char                *identity_upcall = "NONE";
4989         struct md_device          *next;
4990         int                        rc;
4991         int                        node_id;
4992         mntopt_t                   mntopts;
4993         ENTRY;
4994
4995         md_device_init(&m->mdt_md_dev, ldt);
4996         /*
4997          * Environment (env) might be missing mdt_thread_key values at that
4998          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4999          * mode.
5000          *
5001          * Usually device allocation path doesn't use module key values, but
5002          * mdt has to do a lot of work here, so allocate key value.
5003          */
5004         rc = lu_env_refill((struct lu_env *)env);
5005         if (rc != 0)
5006                 RETURN(rc);
5007
5008         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5009         LASSERT(info != NULL);
5010
5011         obd = class_name2obd(dev);
5012         LASSERT(obd != NULL);
5013
5014         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
5015         m->mdt_max_cookiesize = sizeof(struct llog_cookie);
5016
5017         m->mdt_som_conf = 0;
5018
5019         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5020         lmi = server_get_mount(dev);
5021         if (lmi == NULL) {
5022                 CERROR("Cannot get mount info for %s!\n", dev);
5023                 RETURN(-EFAULT);
5024         } else {
5025                 lsi = s2lsi(lmi->lmi_sb);
5026                 /* CMD is supported only in IAM mode */
5027                 LASSERT(num);
5028                 node_id = simple_strtol(num, NULL, 10);
5029                 if (!(lsi->lsi_flags & LDD_F_IAM_DIR) && node_id) {
5030                         CERROR("CMD Operation not allowed in IOP mode\n");
5031                         GOTO(err_lmi, rc = -EINVAL);
5032                 }
5033
5034                 obd->u.obt.obt_magic = OBT_MAGIC;
5035         }
5036
5037         cfs_rwlock_init(&m->mdt_sptlrpc_lock);
5038         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
5039
5040         cfs_spin_lock_init(&m->mdt_ioepoch_lock);
5041         m->mdt_opts.mo_compat_resname = 0;
5042         m->mdt_opts.mo_mds_capa = 1;
5043         m->mdt_opts.mo_oss_capa = 1;
5044         m->mdt_capa_timeout = CAPA_TIMEOUT;
5045         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5046         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5047         m->mdt_squash_uid = 0;
5048         m->mdt_squash_gid = 0;
5049         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5050         m->mdt_nosquash_str = NULL;
5051         m->mdt_nosquash_strlen = 0;
5052         cfs_init_rwsem(&m->mdt_squash_sem);
5053         cfs_spin_lock_init(&m->mdt_osfs_lock);
5054         m->mdt_osfs_age = cfs_time_shift_64(-1000);
5055
5056         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5057         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5058         /* set this lu_device to obd, because error handling need it */
5059         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5060
5061         /* init the stack */
5062         rc = mdt_stack_init((struct lu_env *)env, m, cfg, lmi);
5063         if (rc) {
5064                 CERROR("Can't init device stack, rc %d\n", rc);
5065                 RETURN(rc);
5066         }
5067
5068         s = m->mdt_md_dev.md_lu_dev.ld_site;
5069         mite = &m->mdt_mite;
5070
5071         /* set server index */
5072         mite->ms_node_id = node_id;
5073
5074         /* failover is the default
5075          * FIXME: we do not failout mds0/mgs, which may cause some problems.
5076          * assumed whose ms_node_id == 0 XXX
5077          * */
5078         obd->obd_replayable = 1;
5079         /* No connection accepted until configurations will finish */
5080         obd->obd_no_conn = 1;
5081
5082         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5083                 char *str = lustre_cfg_string(cfg, 4);
5084                 if (strchr(str, 'n')) {
5085                         CWARN("%s: recovery disabled\n", obd->obd_name);
5086                         obd->obd_replayable = 0;
5087                 }
5088         }
5089
5090         rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5091         if (rc)
5092                 GOTO(err_fini_stack, rc);
5093
5094         rc = mdt_fld_init(env, obd->obd_name, m);
5095         if (rc)
5096                 GOTO(err_lut, rc);
5097
5098         rc = mdt_seq_init(env, obd->obd_name, m);
5099         if (rc)
5100                 GOTO(err_fini_fld, rc);
5101
5102         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5103                  LUSTRE_MDT_NAME"-%p", m);
5104         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5105                                               LDLM_NAMESPACE_SERVER,
5106                                               LDLM_NAMESPACE_GREEDY,
5107                                               LDLM_NS_TYPE_MDT);
5108         if (m->mdt_namespace == NULL)
5109                 GOTO(err_fini_seq, rc = -ENOMEM);
5110
5111         m->mdt_namespace->ns_lvbp = m;
5112         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5113
5114         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5115         /* set obd_namespace for compatibility with old code */
5116         obd->obd_namespace = m->mdt_namespace;
5117
5118         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5119
5120         rc = mdt_ck_thread_start(m);
5121         if (rc)
5122                 GOTO(err_free_ns, rc);
5123
5124         rc = mdt_fs_setup(env, m, obd, lsi);
5125         if (rc)
5126                 GOTO(err_capa, rc);
5127
5128         rc = mdt_obd_llog_setup(obd, lsi);
5129         if (rc)
5130                 GOTO(err_fs_cleanup, rc);
5131
5132         rc = mdt_llog_ctxt_clone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5133         if (rc)
5134                 GOTO(err_llog_cleanup, rc);
5135
5136         mdt_adapt_sptlrpc_conf(obd, 1);
5137
5138         next = m->mdt_child;
5139
5140         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5141                                          &mntopts);
5142         if (rc)
5143                 GOTO(err_llog_cleanup, rc);
5144
5145         if (mntopts & MNTOPT_USERXATTR)
5146                 m->mdt_opts.mo_user_xattr = 1;
5147         else
5148                 m->mdt_opts.mo_user_xattr = 0;
5149
5150         if (mntopts & MNTOPT_ACL)
5151                 m->mdt_opts.mo_acl = 1;
5152         else
5153                 m->mdt_opts.mo_acl = 0;
5154
5155         /* XXX: to support suppgid for ACL, we enable identity_upcall
5156          * by default, otherwise, maybe got unexpected -EACCESS. */
5157         if (m->mdt_opts.mo_acl)
5158                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5159
5160         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5161                                                 &mdt_identity_upcall_cache_ops);
5162         if (IS_ERR(m->mdt_identity_cache)) {
5163                 rc = PTR_ERR(m->mdt_identity_cache);
5164                 m->mdt_identity_cache = NULL;
5165                 GOTO(err_llog_cleanup, rc);
5166         }
5167
5168         target_recovery_init(&m->mdt_lut, mdt_recovery_handle);
5169
5170         rc = mdt_procfs_init(m, dev);
5171         if (rc) {
5172                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5173                 GOTO(err_recovery, rc);
5174         }
5175
5176         rc = mdt_start_ptlrpc_service(m);
5177         if (rc)
5178                 GOTO(err_procfs, rc);
5179
5180         ping_evictor_start();
5181
5182         if (obd->obd_recovering == 0)
5183                 mdt_postrecov(env, m);
5184
5185         mdt_init_capa_ctxt(env, m);
5186
5187         /* Reduce the initial timeout on an MDS because it doesn't need such
5188          * a long timeout as an OST does. Adaptive timeouts will adjust this
5189          * value appropriately. */
5190         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5191                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5192
5193         RETURN(0);
5194
5195         ping_evictor_stop();
5196         mdt_stop_ptlrpc_service(m);
5197 err_procfs:
5198         mdt_procfs_fini(m);
5199 err_recovery:
5200         target_recovery_fini(obd);
5201         upcall_cache_cleanup(m->mdt_identity_cache);
5202         m->mdt_identity_cache = NULL;
5203 err_llog_cleanup:
5204         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5205         mdt_obd_llog_cleanup(obd);
5206 err_fs_cleanup:
5207         mdt_fs_cleanup(env, m);
5208 err_capa:
5209         cfs_timer_disarm(&m->mdt_ck_timer);
5210         mdt_ck_thread_stop(m);
5211 err_free_ns:
5212         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5213         obd->obd_namespace = m->mdt_namespace = NULL;
5214 err_fini_seq:
5215         mdt_seq_fini(env, m);
5216 err_fini_fld:
5217         mdt_fld_fini(env, m);
5218 err_lut:
5219         lut_fini(env, &m->mdt_lut);
5220 err_fini_stack:
5221         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5222 err_lmi:
5223         if (lmi)
5224                 server_put_mount(dev, lmi->lmi_mnt);
5225         return (rc);
5226 }
5227
5228 /* For interoperability between 1.8 and 2.0. */
5229 static struct cfg_interop_param mdt_interop_param[] = {
5230         { "mdt.group_upcall",   NULL },
5231         { "mdt.quota_type",     "mdd.quota_type" },
5232         { "mdt.rootsquash",     "mdt.root_squash" },
5233         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
5234         { NULL }
5235 };
5236
5237 /* used by MGS to process specific configurations */
5238 static int mdt_process_config(const struct lu_env *env,
5239                               struct lu_device *d, struct lustre_cfg *cfg)
5240 {
5241         struct mdt_device *m = mdt_dev(d);
5242         struct md_device *md_next = m->mdt_child;
5243         struct lu_device *next = md2lu_dev(md_next);
5244         int rc = 0;
5245         ENTRY;
5246
5247         switch (cfg->lcfg_command) {
5248         case LCFG_PARAM: {
5249                 struct lprocfs_static_vars  lvars;
5250                 struct obd_device          *obd = d->ld_obd;
5251
5252                 /* For interoperability between 1.8 and 2.0. */
5253                 struct cfg_interop_param   *ptr = NULL;
5254                 struct lustre_cfg          *old_cfg = NULL;
5255                 char                       *param = NULL;
5256
5257                 param = lustre_cfg_string(cfg, 1);
5258                 if (param == NULL) {
5259                         CERROR("param is empty\n");
5260                         rc = -EINVAL;
5261                         break;
5262                 }
5263
5264                 ptr = class_find_old_param(param, mdt_interop_param);
5265                 if (ptr != NULL) {
5266                         if (ptr->new_param == NULL) {
5267                                 CWARN("For 1.8 interoperability, skip this %s."
5268                                       " It is obsolete.\n", ptr->old_param);
5269                                         break;
5270                         }
5271
5272                         CWARN("Found old param %s, changed it to %s.\n",
5273                               ptr->old_param, ptr->new_param);
5274
5275                         old_cfg = cfg;
5276                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5277                         if (IS_ERR(cfg)) {
5278                                 rc = PTR_ERR(cfg);
5279                                 break;
5280                         }
5281                 }
5282
5283                 lprocfs_mdt_init_vars(&lvars);
5284                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5285                                               cfg, obd);
5286                 if (rc > 0 || rc == -ENOSYS)
5287                         /* we don't understand; pass it on */
5288                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
5289
5290                 if (old_cfg != NULL)
5291                         lustre_cfg_free(cfg);
5292
5293                 break;
5294         }
5295         case LCFG_ADD_MDC:
5296                 /*
5297                  * Add mdc hook to get first MDT uuid and connect it to
5298                  * ls->controller to use for seq manager.
5299                  */
5300                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5301                 if (rc)
5302                         CERROR("Can't add mdc, rc %d\n", rc);
5303                 else
5304                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5305                 break;
5306         default:
5307                 /* others are passed further */
5308                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5309                 break;
5310         }
5311         RETURN(rc);
5312 }
5313
5314 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5315                                           const struct lu_object_header *hdr,
5316                                           struct lu_device *d)
5317 {
5318         struct mdt_object *mo;
5319
5320         ENTRY;
5321
5322         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5323         if (mo != NULL) {
5324                 struct lu_object *o;
5325                 struct lu_object_header *h;
5326
5327                 o = &mo->mot_obj.mo_lu;
5328                 h = &mo->mot_header;
5329                 lu_object_header_init(h);
5330                 lu_object_init(o, h, d);
5331                 lu_object_add_top(h, o);
5332                 o->lo_ops = &mdt_obj_ops;
5333                 cfs_mutex_init(&mo->mot_ioepoch_mutex);
5334                 cfs_mutex_init(&mo->mot_lov_mutex);
5335                 RETURN(o);
5336         } else
5337                 RETURN(NULL);
5338 }
5339
5340 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5341                            const struct lu_object_conf *unused)
5342 {
5343         struct mdt_device *d = mdt_dev(o->lo_dev);
5344         struct lu_device  *under;
5345         struct lu_object  *below;
5346         int                rc = 0;
5347         ENTRY;
5348
5349         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5350                PFID(lu_object_fid(o)));
5351
5352         under = &d->mdt_child->md_lu_dev;
5353         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5354         if (below != NULL) {
5355                 lu_object_add(o, below);
5356         } else
5357                 rc = -ENOMEM;
5358
5359         RETURN(rc);
5360 }
5361
5362 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5363 {
5364         struct mdt_object *mo = mdt_obj(o);
5365         struct lu_object_header *h;
5366         ENTRY;
5367
5368         h = o->lo_header;
5369         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5370                PFID(lu_object_fid(o)));
5371
5372         lu_object_fini(o);
5373         lu_object_header_fini(h);
5374         OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5375
5376         EXIT;
5377 }
5378
5379 static int mdt_object_print(const struct lu_env *env, void *cookie,
5380                             lu_printer_t p, const struct lu_object *o)
5381 {
5382         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5383         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5384                     "flags="LPX64", epochcount=%d, writecount=%d)",
5385                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
5386                     mdto->mot_ioepoch_count, mdto->mot_writecount);
5387 }
5388
5389 static const struct lu_device_operations mdt_lu_ops = {
5390         .ldo_object_alloc   = mdt_object_alloc,
5391         .ldo_process_config = mdt_process_config,
5392 };
5393
5394 static const struct lu_object_operations mdt_obj_ops = {
5395         .loo_object_init    = mdt_object_init,
5396         .loo_object_free    = mdt_object_free,
5397         .loo_object_print   = mdt_object_print
5398 };
5399
5400 static int mdt_obd_set_info_async(const struct lu_env *env,
5401                                   struct obd_export *exp,
5402                                   __u32 keylen, void *key,
5403                                   __u32 vallen, void *val,
5404                                   struct ptlrpc_request_set *set)
5405 {
5406         struct obd_device     *obd = exp->exp_obd;
5407         int                    rc;
5408         ENTRY;
5409
5410         LASSERT(obd);
5411
5412         if (KEY_IS(KEY_SPTLRPC_CONF)) {
5413                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5414                 RETURN(rc);
5415         }
5416
5417         RETURN(0);
5418 }
5419
5420 /* mds_connect_internal */
5421 static int mdt_connect_internal(struct obd_export *exp,
5422                                 struct mdt_device *mdt,
5423                                 struct obd_connect_data *data)
5424 {
5425         if (data != NULL) {
5426                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5427                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5428
5429                 /* If no known bits (which should not happen, probably,
5430                    as everybody should support LOOKUP and UPDATE bits at least)
5431                    revert to compat mode with plain locks. */
5432                 if (!data->ocd_ibits_known &&
5433                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
5434                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5435
5436                 if (!mdt->mdt_opts.mo_acl)
5437                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5438
5439                 if (!mdt->mdt_opts.mo_user_xattr)
5440                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5441
5442                 if (!mdt->mdt_som_conf)
5443                         data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5444
5445                 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5446                         data->ocd_brw_size = min(data->ocd_brw_size,
5447                                (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5448                         if (data->ocd_brw_size == 0) {
5449                                 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5450                                        " ocd_version: %x ocd_grant: %d "
5451                                        "ocd_index: %u ocd_brw_size is "
5452                                        "unexpectedly zero, network data "
5453                                        "corruption? Refusing connection of this"
5454                                        " client\n",
5455                                        exp->exp_obd->obd_name,
5456                                        exp->exp_client_uuid.uuid,
5457                                        exp, data->ocd_connect_flags, data->ocd_version,
5458                                        data->ocd_grant, data->ocd_index);
5459                                 return -EPROTO;
5460                         }
5461                 }
5462
5463                 cfs_spin_lock(&exp->exp_lock);
5464                 exp->exp_connect_flags = data->ocd_connect_flags;
5465                 cfs_spin_unlock(&exp->exp_lock);
5466                 data->ocd_version = LUSTRE_VERSION_CODE;
5467                 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5468         }
5469
5470 #if 0
5471         if (mdt->mdt_opts.mo_acl &&
5472             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
5473                 CWARN("%s: MDS requires ACL support but client does not\n",
5474                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5475                 return -EBADE;
5476         }
5477 #endif
5478
5479         if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
5480                 CWARN("%s: MDS requires FID support, but client not\n",
5481                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5482                 return -EBADE;
5483         }
5484
5485         if (mdt->mdt_som_conf && !exp_connect_som(exp) &&
5486             !(exp->exp_connect_flags & OBD_CONNECT_MDS_MDS)) {
5487                 CWARN("%s: MDS has SOM enabled, but client does not support "
5488                       "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5489                 return -EBADE;
5490         }
5491
5492         return 0;
5493 }
5494
5495 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5496                                      struct obd_export *exp,
5497                                      struct ptlrpc_request *req)
5498 {
5499         struct sptlrpc_flavor   flvr;
5500         int                     rc = 0;
5501
5502         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5503                 cfs_read_lock(&mdt->mdt_sptlrpc_lock);
5504                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5505                                              req->rq_sp_from,
5506                                              req->rq_peer.nid,
5507                                              &flvr);
5508                 cfs_read_unlock(&mdt->mdt_sptlrpc_lock);
5509
5510                 cfs_spin_lock(&exp->exp_lock);
5511
5512                 exp->exp_sp_peer = req->rq_sp_from;
5513                 exp->exp_flvr = flvr;
5514
5515                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5516                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5517                         CERROR("unauthorized rpc flavor %x from %s, "
5518                                "expect %x\n", req->rq_flvr.sf_rpc,
5519                                libcfs_nid2str(req->rq_peer.nid),
5520                                exp->exp_flvr.sf_rpc);
5521                         rc = -EACCES;
5522                 }
5523
5524                 cfs_spin_unlock(&exp->exp_lock);
5525         } else {
5526                 if (exp->exp_sp_peer != req->rq_sp_from) {
5527                         CERROR("RPC source %s doesn't match %s\n",
5528                                sptlrpc_part2name(req->rq_sp_from),
5529                                sptlrpc_part2name(exp->exp_sp_peer));
5530                         rc = -EACCES;
5531                 } else {
5532                         rc = sptlrpc_target_export_check(exp, req);
5533                 }
5534         }
5535
5536         return rc;
5537 }
5538
5539 /* mds_connect copy */
5540 static int mdt_obd_connect(const struct lu_env *env,
5541                            struct obd_export **exp, struct obd_device *obd,
5542                            struct obd_uuid *cluuid,
5543                            struct obd_connect_data *data,
5544                            void *localdata)
5545 {
5546         struct mdt_thread_info *info;
5547         struct obd_export      *lexp;
5548         struct lustre_handle    conn = { 0 };
5549         struct mdt_device      *mdt;
5550         struct ptlrpc_request  *req;
5551         int                     rc;
5552         ENTRY;
5553
5554         LASSERT(env != NULL);
5555         if (!exp || !obd || !cluuid)
5556                 RETURN(-EINVAL);
5557
5558         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5559         req = info->mti_pill->rc_req;
5560         mdt = mdt_dev(obd->obd_lu_dev);
5561
5562         rc = class_connect(&conn, obd, cluuid);
5563         if (rc)
5564                 RETURN(rc);
5565
5566         lexp = class_conn2export(&conn);
5567         LASSERT(lexp != NULL);
5568
5569         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5570         if (rc)
5571                 GOTO(out, rc);
5572
5573         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5574                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5575
5576         rc = mdt_connect_internal(lexp, mdt, data);
5577         if (rc == 0) {
5578                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5579
5580                 LASSERT(lcd);
5581                 info->mti_exp = lexp;
5582                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5583                 rc = lut_client_new(env, lexp);
5584                 if (rc == 0)
5585                         mdt_export_stats_init(obd, lexp, localdata);
5586         }
5587
5588 out:
5589         if (rc != 0) {
5590                 class_disconnect(lexp);
5591                 *exp = NULL;
5592         } else {
5593                 *exp = lexp;
5594         }
5595
5596         RETURN(rc);
5597 }
5598
5599 static int mdt_obd_reconnect(const struct lu_env *env,
5600                              struct obd_export *exp, struct obd_device *obd,
5601                              struct obd_uuid *cluuid,
5602                              struct obd_connect_data *data,
5603                              void *localdata)
5604 {
5605         struct mdt_thread_info *info;
5606         struct mdt_device      *mdt;
5607         struct ptlrpc_request  *req;
5608         int                     rc;
5609         ENTRY;
5610
5611         if (exp == NULL || obd == NULL || cluuid == NULL)
5612                 RETURN(-EINVAL);
5613
5614         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5615         req = info->mti_pill->rc_req;
5616         mdt = mdt_dev(obd->obd_lu_dev);
5617
5618         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5619         if (rc)
5620                 RETURN(rc);
5621
5622         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5623         if (rc == 0)
5624                 mdt_export_stats_init(obd, exp, localdata);
5625
5626         RETURN(rc);
5627 }
5628
5629 static int mdt_export_cleanup(struct obd_export *exp)
5630 {
5631         struct mdt_export_data *med = &exp->exp_mdt_data;
5632         struct obd_device      *obd = exp->exp_obd;
5633         struct mdt_device      *mdt;
5634         struct mdt_thread_info *info;
5635         struct lu_env           env;
5636         CFS_LIST_HEAD(closing_list);
5637         struct mdt_file_data *mfd, *n;
5638         int rc = 0;
5639         ENTRY;
5640
5641         cfs_spin_lock(&med->med_open_lock);
5642         while (!cfs_list_empty(&med->med_open_head)) {
5643                 cfs_list_t *tmp = med->med_open_head.next;
5644                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5645
5646                 /* Remove mfd handle so it can't be found again.
5647                  * We are consuming the mfd_list reference here. */
5648                 class_handle_unhash(&mfd->mfd_handle);
5649                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5650         }
5651         cfs_spin_unlock(&med->med_open_lock);
5652         mdt = mdt_dev(obd->obd_lu_dev);
5653         LASSERT(mdt != NULL);
5654
5655         rc = lu_env_init(&env, LCT_MD_THREAD);
5656         if (rc)
5657                 RETURN(rc);
5658
5659         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5660         LASSERT(info != NULL);
5661         memset(info, 0, sizeof *info);
5662         info->mti_env = &env;
5663         info->mti_mdt = mdt;
5664         info->mti_exp = exp;
5665
5666         if (!cfs_list_empty(&closing_list)) {
5667                 struct md_attr *ma = &info->mti_attr;
5668                 int lmm_size;
5669                 int cookie_size;
5670
5671                 lmm_size = mdt->mdt_max_mdsize;
5672                 OBD_ALLOC_LARGE(ma->ma_lmm, lmm_size);
5673                 if (ma->ma_lmm == NULL)
5674                         GOTO(out_lmm, rc = -ENOMEM);
5675
5676                 cookie_size = mdt->mdt_max_cookiesize;
5677                 OBD_ALLOC_LARGE(ma->ma_cookie, cookie_size);
5678                 if (ma->ma_cookie == NULL)
5679                         GOTO(out_cookie, rc = -ENOMEM);
5680
5681                 /* Close any open files (which may also cause orphan unlinking). */
5682                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5683                         cfs_list_del_init(&mfd->mfd_list);
5684                         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
5685                         ma->ma_lmm_size = lmm_size;
5686                         ma->ma_cookie_size = cookie_size;
5687                         ma->ma_need = 0;
5688                         /* It is not for setattr, just tell MDD to send
5689                          * DESTROY RPC to OSS if needed */
5690                         ma->ma_valid = MA_FLAGS;
5691                         ma->ma_attr_flags = MDS_CLOSE_CLEANUP;
5692                         /* Don't unlink orphan on failover umount, LU-184 */
5693                         if (exp->exp_flags & OBD_OPT_FAILOVER)
5694                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5695                         mdt_mfd_close(info, mfd);
5696                 }
5697                 OBD_FREE_LARGE(ma->ma_cookie, cookie_size);
5698                 ma->ma_cookie = NULL;
5699 out_cookie:
5700                 OBD_FREE_LARGE(ma->ma_lmm, lmm_size);
5701                 ma->ma_lmm = NULL;
5702         }
5703 out_lmm:
5704         info->mti_mdt = NULL;
5705         /* cleanup client slot early */
5706         /* Do not erase record for recoverable client. */
5707         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5708                 lut_client_del(&env, exp);
5709         lu_env_fini(&env);
5710
5711         RETURN(rc);
5712 }
5713
5714 static int mdt_obd_disconnect(struct obd_export *exp)
5715 {
5716         int rc;
5717         ENTRY;
5718
5719         LASSERT(exp);
5720         class_export_get(exp);
5721
5722         rc = server_disconnect_export(exp);
5723         if (rc != 0)
5724                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5725
5726         rc = mdt_export_cleanup(exp);
5727         class_export_put(exp);
5728         RETURN(rc);
5729 }
5730
5731 /* FIXME: Can we avoid using these two interfaces? */
5732 static int mdt_init_export(struct obd_export *exp)
5733 {
5734         struct mdt_export_data *med = &exp->exp_mdt_data;
5735         int                     rc;
5736         ENTRY;
5737
5738         CFS_INIT_LIST_HEAD(&med->med_open_head);
5739         cfs_spin_lock_init(&med->med_open_lock);
5740         cfs_mutex_init(&med->med_idmap_mutex);
5741         med->med_idmap = NULL;
5742         cfs_spin_lock(&exp->exp_lock);
5743         exp->exp_connecting = 1;
5744         cfs_spin_unlock(&exp->exp_lock);
5745
5746         /* self-export doesn't need client data and ldlm initialization */
5747         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5748                                      &exp->exp_client_uuid)))
5749                 RETURN(0);
5750
5751         rc = lut_client_alloc(exp);
5752         if (rc)
5753                 GOTO(err, rc);
5754
5755         rc = ldlm_init_export(exp);
5756         if (rc)
5757                 GOTO(err_free, rc);
5758
5759         RETURN(rc);
5760
5761 err_free:
5762         lut_client_free(exp);
5763 err:
5764         CERROR("%s: Failed to initialize export: rc = %d\n",
5765                exp->exp_obd->obd_name, rc);
5766         return rc;
5767 }
5768
5769 static int mdt_destroy_export(struct obd_export *exp)
5770 {
5771         ENTRY;
5772
5773         if (exp_connect_rmtclient(exp))
5774                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5775
5776         target_destroy_export(exp);
5777         /* destroy can be called from failed obd_setup, so
5778          * checking uuid is safer than obd_self_export */
5779         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5780                                      &exp->exp_client_uuid)))
5781                 RETURN(0);
5782
5783         ldlm_destroy_export(exp);
5784         lut_client_free(exp);
5785
5786         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5787         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5788
5789         RETURN(0);
5790 }
5791
5792 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
5793 {
5794         if (flag & CONFIG_LOG)
5795                 cfs_set_bit(MDT_FL_CFGLOG, &m->mdt_state);
5796
5797         /* also notify active event */
5798         if (flag & CONFIG_SYNC)
5799                 cfs_set_bit(MDT_FL_SYNCED, &m->mdt_state);
5800
5801         if (cfs_test_bit(MDT_FL_CFGLOG, &m->mdt_state) &&
5802             cfs_test_bit(MDT_FL_SYNCED, &m->mdt_state)) {
5803                 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
5804
5805                 /* Open for clients */
5806                 if (obd->obd_no_conn) {
5807                         cfs_spin_lock(&obd->obd_dev_lock);
5808                         obd->obd_no_conn = 0;
5809                         cfs_spin_unlock(&obd->obd_dev_lock);
5810                 }
5811         }
5812 }
5813
5814 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
5815                       enum md_upcall_event ev, void *data)
5816 {
5817         struct mdt_device *m = mdt_dev(&md->md_lu_dev);
5818         struct md_device  *next  = m->mdt_child;
5819         struct mdt_thread_info *mti;
5820         int rc = 0;
5821         ENTRY;
5822
5823         switch (ev) {
5824                 case MD_LOV_SYNC:
5825                         rc = next->md_ops->mdo_maxsize_get(env, next,
5826                                         &m->mdt_max_mdsize,
5827                                         &m->mdt_max_cookiesize);
5828                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
5829                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
5830                         mdt_allow_cli(m, CONFIG_SYNC);
5831                         if (data)
5832                                 (*(__u64 *)data) =
5833                                       m->mdt_lut.lut_obd->u.obt.obt_mount_count;
5834                         break;
5835                 case MD_NO_TRANS:
5836                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5837                         mti->mti_no_need_trans = 1;
5838                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
5839                         break;
5840                 case MD_LOV_CONFIG:
5841                         /* Check that MDT is not yet configured */
5842                         LASSERT(!cfs_test_bit(MDT_FL_CFGLOG, &m->mdt_state));
5843                         break;
5844                 default:
5845                         CERROR("invalid event\n");
5846                         rc = -EINVAL;
5847                         break;
5848         }
5849         RETURN(rc);
5850 }
5851
5852 static int mdt_obd_notify(struct obd_device *obd,
5853                           struct obd_device *watched,
5854                           enum obd_notify_event ev, void *data)
5855 {
5856         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5857         ENTRY;
5858
5859         switch (ev) {
5860         case OBD_NOTIFY_CONFIG:
5861                 mdt_allow_cli(mdt, (unsigned long)data);
5862                 break;
5863         default:
5864                 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
5865         }
5866         RETURN(0);
5867 }
5868
5869 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5870                             void *val, int vallen)
5871 {
5872         struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5873         struct getinfo_fid2path *fpout, *fpin;
5874         int rc = 0;
5875
5876         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5877         fpout = val;
5878
5879         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5880                 lustre_swab_fid2path(fpin);
5881
5882         memcpy(fpout, fpin, sizeof(*fpin));
5883         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5884                 RETURN(-EINVAL);
5885
5886         rc = mdt_fid2path(info->mti_env, mdt, fpout);
5887         RETURN(rc);
5888 }
5889
5890 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5891                         struct getinfo_fid2path *fp)
5892 {
5893         struct mdt_object *obj;
5894         int    rc;
5895         ENTRY;
5896
5897         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5898                PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5899
5900         if (!fid_is_sane(&fp->gf_fid))
5901                 RETURN(-EINVAL);
5902
5903         obj = mdt_object_find(env, mdt, &fp->gf_fid);
5904         if (obj == NULL || IS_ERR(obj)) {
5905                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5906                        PTR_ERR(obj));
5907                 RETURN(-EINVAL);
5908         }
5909
5910         rc = lu_object_exists(&obj->mot_obj.mo_lu);
5911         if (rc <= 0) {
5912                 if (rc == -1)
5913                         rc = -EREMOTE;
5914                 else
5915                         rc = -ENOENT;
5916                 mdt_object_put(env, obj);
5917                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5918                        PFID(&fp->gf_fid), rc);
5919                 RETURN(rc);
5920         }
5921
5922         rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5923                      fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5924         mdt_object_put(env, obj);
5925
5926         RETURN(rc);
5927 }
5928
5929 static int mdt_get_info(struct mdt_thread_info *info)
5930 {
5931         struct ptlrpc_request *req = mdt_info_req(info);
5932         char *key;
5933         int keylen;
5934         __u32 *vallen;
5935         void *valout;
5936         int rc;
5937         ENTRY;
5938
5939         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5940         if (key == NULL) {
5941                 CDEBUG(D_IOCTL, "No GETINFO key");
5942                 RETURN(-EFAULT);
5943         }
5944         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5945                                       RCL_CLIENT);
5946
5947         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5948         if (vallen == NULL) {
5949                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5950                 RETURN(-EFAULT);
5951         }
5952
5953         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5954                              *vallen);
5955         rc = req_capsule_server_pack(info->mti_pill);
5956         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5957         if (valout == NULL) {
5958                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5959                 RETURN(-EFAULT);
5960         }
5961
5962         if (KEY_IS(KEY_FID2PATH))
5963                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5964         else
5965                 rc = -EINVAL;
5966
5967         lustre_msg_set_status(req->rq_repmsg, rc);
5968
5969         RETURN(rc);
5970 }
5971
5972 /* Pass the ioc down */
5973 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5974                          unsigned int cmd, int len, void *data)
5975 {
5976         struct lu_context ioctl_session;
5977         struct md_device *next = mdt->mdt_child;
5978         int rc;
5979         ENTRY;
5980
5981         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5982         if (rc)
5983                 RETURN(rc);
5984         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5985         lu_context_enter(&ioctl_session);
5986         env->le_ses = &ioctl_session;
5987
5988         LASSERT(next->md_ops->mdo_iocontrol);
5989         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5990
5991         lu_context_exit(&ioctl_session);
5992         lu_context_fini(&ioctl_session);
5993         RETURN(rc);
5994 }
5995
5996 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5997 {
5998         struct obd_ioctl_data *data = karg;
5999         struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
6000         __u64 version;
6001         struct mdt_object *obj;
6002         struct mdt_lock_handle  *lh;
6003         int rc;
6004         ENTRY;
6005
6006         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6007         if (!fid_is_sane(fid))
6008                 RETURN(-EINVAL);
6009
6010         lh = &mti->mti_lh[MDT_LH_PARENT];
6011         mdt_lock_reg_init(lh, LCK_CR);
6012
6013         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6014         if (IS_ERR(obj))
6015                 RETURN(PTR_ERR(obj));
6016
6017         rc = mdt_object_exists(obj);
6018         if (rc < 0) {
6019                 rc = -EREMOTE;
6020                 /**
6021                  * before calling version get the correct MDS should be
6022                  * fid, this is error to find remote object here
6023                  */
6024                 CERROR("nonlocal object "DFID"\n", PFID(fid));
6025         } else if (rc == 0) {
6026                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6027                 rc = -ENOENT;
6028         } else {
6029                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6030                *(__u64 *)data->ioc_inlbuf2 = version;
6031                 rc = 0;
6032         }
6033         mdt_object_unlock_put(mti, obj, lh, 1);
6034         RETURN(rc);
6035 }
6036
6037 /* ioctls on obd dev */
6038 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6039                          void *karg, void *uarg)
6040 {
6041         struct lu_env      env;
6042         struct obd_device *obd = exp->exp_obd;
6043         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6044         struct dt_device  *dt = mdt->mdt_bottom;
6045         int rc;
6046
6047         ENTRY;
6048         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6049         rc = lu_env_init(&env, LCT_MD_THREAD);
6050         if (rc)
6051                 RETURN(rc);
6052
6053         switch (cmd) {
6054         case OBD_IOC_SYNC:
6055                 rc = mdt_device_sync(&env, mdt);
6056                 break;
6057         case OBD_IOC_SET_READONLY:
6058                 rc = dt->dd_ops->dt_ro(&env, dt);
6059                 break;
6060         case OBD_IOC_ABORT_RECOVERY:
6061                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
6062                 target_stop_recovery_thread(obd);
6063                 rc = 0;
6064                 break;
6065         case OBD_IOC_CHANGELOG_REG:
6066         case OBD_IOC_CHANGELOG_DEREG:
6067         case OBD_IOC_CHANGELOG_CLEAR:
6068                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6069                 break;
6070         case OBD_IOC_START_LFSCK:
6071         case OBD_IOC_STOP_LFSCK: {
6072                 struct md_device *next = mdt->mdt_child;
6073                 struct obd_ioctl_data *data = karg;
6074
6075                 if (unlikely(data == NULL)) {
6076                         rc = -EINVAL;
6077                         break;
6078                 }
6079
6080                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6081                                                  data->ioc_inllen1,
6082                                                  data->ioc_inlbuf1);
6083                 break;
6084         }
6085         case OBD_IOC_GET_OBJ_VERSION: {
6086                 struct mdt_thread_info *mti;
6087                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6088                 memset(mti, 0, sizeof *mti);
6089                 mti->mti_env = &env;
6090                 mti->mti_mdt = mdt;
6091                 mti->mti_exp = exp;
6092
6093                 rc = mdt_ioc_version_get(mti, karg);
6094                 break;
6095         }
6096         default:
6097                 CERROR("Not supported cmd = %d for device %s\n",
6098                        cmd, obd->obd_name);
6099                 rc = -EOPNOTSUPP;
6100         }
6101
6102         lu_env_fini(&env);
6103         RETURN(rc);
6104 }
6105
6106 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6107 {
6108         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6109         int rc;
6110         ENTRY;
6111
6112         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6113         RETURN(rc);
6114 }
6115
6116 int mdt_obd_postrecov(struct obd_device *obd)
6117 {
6118         struct lu_env env;
6119         int rc;
6120
6121         rc = lu_env_init(&env, LCT_MD_THREAD);
6122         if (rc)
6123                 RETURN(rc);
6124         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6125         lu_env_fini(&env);
6126         return rc;
6127 }
6128
6129 /**
6130  * Send a copytool req to a client
6131  * Note this sends a request RPC from a server (MDT) to a client (MDC),
6132  * backwards of normal comms.
6133  */
6134 int mdt_hsm_copytool_send(struct obd_export *exp)
6135 {
6136         struct kuc_hdr *lh;
6137         struct hsm_action_list *hal;
6138         struct hsm_action_item *hai;
6139         int rc, len;
6140         ENTRY;
6141
6142         CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6143               libcfs_nid2str(exp->exp_connection->c_peer.nid));
6144
6145         len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6146                 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6147         OBD_ALLOC(lh, len);
6148         if (lh == NULL)
6149                 RETURN(-ENOMEM);
6150
6151         lh->kuc_magic = KUC_MAGIC;
6152         lh->kuc_transport = KUC_TRANSPORT_HSM;
6153         lh->kuc_msgtype = HMT_ACTION_LIST;
6154         lh->kuc_msglen = len;
6155
6156         hal = (struct hsm_action_list *)(lh + 1);
6157         hal->hal_version = HAL_VERSION;
6158         hal->hal_archive_num = 1;
6159         obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6160                         MTI_NAME_MAXLEN);
6161
6162         /* mock up an action list */
6163         hal->hal_count = 2;
6164         hai = hai_zero(hal);
6165         hai->hai_action = HSMA_ARCHIVE;
6166         hai->hai_fid.f_oid = 0xA00A;
6167         hai->hai_len = sizeof(*hai);
6168         hai = hai_next(hai);
6169         hai->hai_action = HSMA_RESTORE;
6170         hai->hai_fid.f_oid = 0xB00B;
6171         hai->hai_len = sizeof(*hai);
6172
6173         /* Uses the ldlm reverse import; this rpc will be seen by
6174           the ldlm_callback_handler */
6175         rc = do_set_info_async(exp->exp_imp_reverse,
6176                                LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6177                                sizeof(KEY_HSM_COPYTOOL_SEND),
6178                                KEY_HSM_COPYTOOL_SEND,
6179                                len, lh, NULL);
6180
6181         OBD_FREE(lh, len);
6182
6183         RETURN(rc);
6184 }
6185
6186 static struct obd_ops mdt_obd_device_ops = {
6187         .o_owner          = THIS_MODULE,
6188         .o_set_info_async = mdt_obd_set_info_async,
6189         .o_connect        = mdt_obd_connect,
6190         .o_reconnect      = mdt_obd_reconnect,
6191         .o_disconnect     = mdt_obd_disconnect,
6192         .o_init_export    = mdt_init_export,
6193         .o_destroy_export = mdt_destroy_export,
6194         .o_iocontrol      = mdt_iocontrol,
6195         .o_postrecov      = mdt_obd_postrecov,
6196         .o_notify         = mdt_obd_notify
6197 };
6198
6199 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6200                                          struct lu_device *d)
6201 {
6202         struct mdt_device *m = mdt_dev(d);
6203         ENTRY;
6204
6205         mdt_fini(env, m);
6206         RETURN(NULL);
6207 }
6208
6209 static struct lu_device *mdt_device_free(const struct lu_env *env,
6210                                          struct lu_device *d)
6211 {
6212         struct mdt_device *m = mdt_dev(d);
6213         ENTRY;
6214
6215         md_device_fini(&m->mdt_md_dev);
6216         OBD_FREE_PTR(m);
6217         RETURN(NULL);
6218 }
6219
6220 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6221                                           struct lu_device_type *t,
6222                                           struct lustre_cfg *cfg)
6223 {
6224         struct lu_device  *l;
6225         struct mdt_device *m;
6226
6227         OBD_ALLOC_PTR(m);
6228         if (m != NULL) {
6229                 int rc;
6230
6231                 l = &m->mdt_md_dev.md_lu_dev;
6232                 rc = mdt_init0(env, m, t, cfg);
6233                 if (rc != 0) {
6234                         mdt_device_free(env, l);
6235                         l = ERR_PTR(rc);
6236                         return l;
6237                 }
6238                 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
6239         } else
6240                 l = ERR_PTR(-ENOMEM);
6241         return l;
6242 }
6243
6244 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6245 LU_KEY_INIT(mdt, struct mdt_thread_info);
6246
6247 static void mdt_key_fini(const struct lu_context *ctx,
6248                          struct lu_context_key *key, void* data)
6249 {
6250         struct mdt_thread_info *info = data;
6251
6252         if (info->mti_big_lmm) {
6253                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6254                 info->mti_big_lmm = NULL;
6255                 info->mti_big_lmmsize = 0;
6256         }
6257         OBD_FREE_PTR(info);
6258 }
6259
6260 /* context key: mdt_thread_key */
6261 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6262
6263 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
6264 {
6265         return md_ucred(info->mti_env);
6266 }
6267
6268 /**
6269  * Enable/disable COS (Commit On Sharing).
6270  *
6271  * Set/Clear the COS flag in mdt options.
6272  *
6273  * \param mdt mdt device
6274  * \param val 0 disables COS, other values enable COS
6275  */
6276 void mdt_enable_cos(struct mdt_device *mdt, int val)
6277 {
6278         struct lu_env env;
6279         int rc;
6280
6281         mdt->mdt_opts.mo_cos = !!val;
6282         rc = lu_env_init(&env, LCT_LOCAL);
6283         if (unlikely(rc != 0)) {
6284                 CWARN("lu_env initialization failed with rc = %d,"
6285                       "cannot sync\n", rc);
6286                 return;
6287         }
6288         mdt_device_sync(&env, mdt);
6289         lu_env_fini(&env);
6290 }
6291
6292 /**
6293  * Check COS (Commit On Sharing) status.
6294  *
6295  * Return COS flag status.
6296  *
6297  * \param mdt mdt device
6298  */
6299 int mdt_cos_is_enabled(struct mdt_device *mdt)
6300 {
6301         return mdt->mdt_opts.mo_cos != 0;
6302 }
6303
6304 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6305 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6306
6307 static struct lu_device_type_operations mdt_device_type_ops = {
6308         .ldto_init = mdt_type_init,
6309         .ldto_fini = mdt_type_fini,
6310
6311         .ldto_start = mdt_type_start,
6312         .ldto_stop  = mdt_type_stop,
6313
6314         .ldto_device_alloc = mdt_device_alloc,
6315         .ldto_device_free  = mdt_device_free,
6316         .ldto_device_fini  = mdt_device_fini
6317 };
6318
6319 static struct lu_device_type mdt_device_type = {
6320         .ldt_tags     = LU_DEVICE_MD,
6321         .ldt_name     = LUSTRE_MDT_NAME,
6322         .ldt_ops      = &mdt_device_type_ops,
6323         .ldt_ctx_tags = LCT_MD_THREAD
6324 };
6325
6326 static int __init mdt_mod_init(void)
6327 {
6328         struct lprocfs_static_vars lvars;
6329         int rc;
6330
6331         rc = lu_kmem_init(mdt_caches);
6332         if (rc)
6333                 return rc;
6334
6335         if (mdt_num_threads != 0 && mds_num_threads == 0) {
6336                 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6337                               "use mds_num_threads instead or unset both for"
6338                               "dynamic thread startup\n");
6339                 mds_num_threads = mdt_num_threads;
6340         }
6341
6342         lprocfs_mdt_init_vars(&lvars);
6343         rc = class_register_type(&mdt_obd_device_ops, NULL,
6344                                  lvars.module_vars, LUSTRE_MDT_NAME,
6345                                  &mdt_device_type);
6346
6347         if (rc)
6348                 lu_kmem_fini(mdt_caches);
6349         return rc;
6350 }
6351
6352 static void __exit mdt_mod_exit(void)
6353 {
6354         class_unregister_type(LUSTRE_MDT_NAME);
6355         lu_kmem_fini(mdt_caches);
6356 }
6357
6358
6359 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt)             \
6360 [prefix ## _ ## opc - prefix ## _ ## base] = {                          \
6361         .mh_name    = #opc,                                             \
6362         .mh_fail_id = OBD_FAIL_ ## prefix ## _  ## opc ## suffix,       \
6363         .mh_opc     = prefix ## _  ## opc,                              \
6364         .mh_flags   = flags,                                            \
6365         .mh_act     = fn,                                               \
6366         .mh_fmt     = fmt                                               \
6367 }
6368
6369 #define DEF_MDT_HNDL(flags, name, fn, fmt)                                  \
6370         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
6371
6372 #define DEF_SEQ_HNDL(flags, name, fn, fmt)                      \
6373         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
6374
6375 #define DEF_FLD_HNDL(flags, name, fn, fmt)                      \
6376         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
6377 /*
6378  * Request with a format known in advance
6379  */
6380 #define DEF_MDT_HNDL_F(flags, name, fn)                                 \
6381         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
6382
6383 #define DEF_SEQ_HNDL_F(flags, name, fn)                                 \
6384         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
6385
6386 #define DEF_FLD_HNDL_F(flags, name, fn)                                 \
6387         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
6388 /*
6389  * Request with a format we do not yet know
6390  */
6391 #define DEF_MDT_HNDL_0(flags, name, fn)                                 \
6392         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
6393
6394 static struct mdt_handler mdt_mds_ops[] = {
6395 DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
6396 DEF_MDT_HNDL_F(0,                         DISCONNECT,   mdt_disconnect),
6397 DEF_MDT_HNDL  (0,                         SET_INFO,     mdt_set_info,
6398                                                              &RQF_OBD_SET_INFO),
6399 DEF_MDT_HNDL_F(0,                         GET_INFO,     mdt_get_info),
6400 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
6401 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
6402 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
6403 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
6404 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
6405 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
6406 DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,        mdt_close),
6407 DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
6408 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
6409 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
6410 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
6411 DEF_MDT_HNDL_F(0,                         QUOTACHECK,   mdt_quotacheck),
6412 DEF_MDT_HNDL_F(0,                         QUOTACTL,     mdt_quotactl)
6413 };
6414
6415 #define DEF_OBD_HNDL(flags, name, fn)                   \
6416         DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
6417
6418
6419 static struct mdt_handler mdt_obd_ops[] = {
6420         DEF_OBD_HNDL(0, PING,           mdt_obd_ping),
6421         DEF_OBD_HNDL(0, LOG_CANCEL,     mdt_obd_log_cancel),
6422         DEF_OBD_HNDL(0, QC_CALLBACK,    mdt_obd_qc_callback),
6423         DEF_OBD_HNDL(0, IDX_READ,       mdt_obd_idx_read)
6424 };
6425
6426 #define DEF_DLM_HNDL_0(flags, name, fn)                   \
6427         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
6428 #define DEF_DLM_HNDL_F(flags, name, fn)                   \
6429         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
6430
6431 static struct mdt_handler mdt_dlm_ops[] = {
6432         DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE,        mdt_enqueue),
6433         DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT,        mdt_convert),
6434         DEF_DLM_HNDL_0(0,            BL_CALLBACK,    mdt_bl_callback),
6435         DEF_DLM_HNDL_0(0,            CP_CALLBACK,    mdt_cp_callback)
6436 };
6437
6438 #define DEF_LLOG_HNDL(flags, name, fn)                   \
6439         DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
6440
6441 static struct mdt_handler mdt_llog_ops[] = {
6442         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE,      mdt_llog_create),
6443         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK,  mdt_llog_next_block),
6444         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
6445         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC,   NULL),
6446         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE,       NULL),
6447         DEF_LLOG_HNDL(0, ORIGIN_CONNECT,            NULL),
6448         DEF_LLOG_HNDL(0, CATINFO,                   NULL),
6449         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK,  mdt_llog_prev_block),
6450         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY,     mdt_llog_destroy),
6451 };
6452
6453 #define DEF_SEC_CTX_HNDL(name, fn)                      \
6454         DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
6455
6456 static struct mdt_handler mdt_sec_ctx_ops[] = {
6457         DEF_SEC_CTX_HNDL(INIT,          mdt_sec_ctx_handle),
6458         DEF_SEC_CTX_HNDL(INIT_CONT,     mdt_sec_ctx_handle),
6459         DEF_SEC_CTX_HNDL(FINI,          mdt_sec_ctx_handle)
6460 };
6461
6462 static struct mdt_opc_slice mdt_regular_handlers[] = {
6463         {
6464                 .mos_opc_start = MDS_GETATTR,
6465                 .mos_opc_end   = MDS_LAST_OPC,
6466                 .mos_hs        = mdt_mds_ops
6467         },
6468         {
6469                 .mos_opc_start = OBD_PING,
6470                 .mos_opc_end   = OBD_LAST_OPC,
6471                 .mos_hs        = mdt_obd_ops
6472         },
6473         {
6474                 .mos_opc_start = LDLM_ENQUEUE,
6475                 .mos_opc_end   = LDLM_LAST_OPC,
6476                 .mos_hs        = mdt_dlm_ops
6477         },
6478         {
6479                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6480                 .mos_opc_end   = LLOG_LAST_OPC,
6481                 .mos_hs        = mdt_llog_ops
6482         },
6483         {
6484                 .mos_opc_start = SEC_CTX_INIT,
6485                 .mos_opc_end   = SEC_LAST_OPC,
6486                 .mos_hs        = mdt_sec_ctx_ops
6487         },
6488         {
6489                 .mos_hs        = NULL
6490         }
6491 };
6492
6493 static struct mdt_handler mdt_readpage_ops[] = {
6494         DEF_MDT_HNDL_F(0,                         CONNECT,  mdt_connect),
6495         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
6496 #ifdef HAVE_SPLIT_SUPPORT
6497         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
6498 #endif
6499
6500         /*
6501          * XXX: this is ugly and should be fixed one day, see mdc_close() for
6502          * detailed comments. --umka
6503          */
6504         DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,    mdt_close),
6505         DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING,    mdt_done_writing),
6506 };
6507
6508 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6509         {
6510                 .mos_opc_start = MDS_GETATTR,
6511                 .mos_opc_end   = MDS_LAST_OPC,
6512                 .mos_hs        = mdt_readpage_ops
6513         },
6514         {
6515                 .mos_opc_start = OBD_FIRST_OPC,
6516                 .mos_opc_end   = OBD_LAST_OPC,
6517                 .mos_hs        = mdt_obd_ops
6518         },
6519         {
6520                 .mos_hs        = NULL
6521         }
6522 };
6523
6524 static struct mdt_handler mdt_xmds_ops[] = {
6525         DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
6526         DEF_MDT_HNDL_F(HABEO_CORPUS             , GETATTR,      mdt_getattr),
6527         DEF_MDT_HNDL_F(0 | MUTABOR              , REINT,        mdt_reint),
6528         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
6529 };
6530
6531 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6532         {
6533                 .mos_opc_start = MDS_GETATTR,
6534                 .mos_opc_end   = MDS_LAST_OPC,
6535                 .mos_hs        = mdt_xmds_ops
6536         },
6537         {
6538                 .mos_opc_start = OBD_PING,
6539                 .mos_opc_end   = OBD_LAST_OPC,
6540                 .mos_hs        = mdt_obd_ops
6541         },
6542         {
6543                 .mos_opc_start = SEC_CTX_INIT,
6544                 .mos_opc_end   = SEC_LAST_OPC,
6545                 .mos_hs        = mdt_sec_ctx_ops
6546         },
6547         {
6548                 .mos_hs        = NULL
6549         }
6550 };
6551
6552 static struct mdt_handler mdt_seq_ops[] = {
6553         DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
6554 };
6555
6556 static struct mdt_opc_slice mdt_seq_handlers[] = {
6557         {
6558                 .mos_opc_start = SEQ_QUERY,
6559                 .mos_opc_end   = SEQ_LAST_OPC,
6560                 .mos_hs        = mdt_seq_ops
6561         },
6562         {
6563                 .mos_hs        = NULL
6564         }
6565 };
6566
6567 static struct mdt_handler mdt_fld_ops[] = {
6568         DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
6569 };
6570
6571 static struct mdt_opc_slice mdt_fld_handlers[] = {
6572         {
6573                 .mos_opc_start = FLD_QUERY,
6574                 .mos_opc_end   = FLD_LAST_OPC,
6575                 .mos_hs        = mdt_fld_ops
6576         },
6577         {
6578                 .mos_hs        = NULL
6579         }
6580 };
6581
6582 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6583 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
6584 MODULE_LICENSE("GPL");
6585
6586 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);