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