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