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