Whamcloud - gitweb
001ef1de171b4b035dee7d55ce75e102117e89a4
[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 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/mdt/mdt_handler.c
40  *
41  * Lustre Metadata Target (mdt) request handler
42  *
43  * Author: Peter Braam <braam@clusterfs.com>
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  * Author: Phil Schwan <phil@clusterfs.com>
46  * Author: Mike Shaver <shaver@clusterfs.com>
47  * Author: Nikita Danilov <nikita@clusterfs.com>
48  * Author: Huang Hua <huanghua@clusterfs.com>
49  * Author: Yury Umanets <umka@clusterfs.com>
50  */
51
52 #ifndef EXPORT_SYMTAB
53 # define EXPORT_SYMTAB
54 #endif
55 #define DEBUG_SUBSYSTEM S_MDS
56
57 #include <linux/module.h>
58 /*
59  * struct OBD_{ALLOC,FREE}*()
60  */
61 #include <obd_support.h>
62 /* struct ptlrpc_request */
63 #include <lustre_net.h>
64 /* struct obd_export */
65 #include <lustre_export.h>
66 /* struct obd_device */
67 #include <obd.h>
68 /* lu2dt_dev() */
69 #include <dt_object.h>
70 #include <lustre_mds.h>
71 #include <lustre_mdt.h>
72 #include "mdt_internal.h"
73 #ifdef HAVE_QUOTA_SUPPORT
74 # include <lustre_quota.h>
75 #endif
76 #include <lustre_acl.h>
77 #include <lustre_param.h>
78 #include <lustre_fsfilt.h>
79
80 mdl_mode_t mdt_mdl_lock_modes[] = {
81         [LCK_MINMODE] = MDL_MINMODE,
82         [LCK_EX]      = MDL_EX,
83         [LCK_PW]      = MDL_PW,
84         [LCK_PR]      = MDL_PR,
85         [LCK_CW]      = MDL_CW,
86         [LCK_CR]      = MDL_CR,
87         [LCK_NL]      = MDL_NL,
88         [LCK_GROUP]   = MDL_GROUP
89 };
90
91 ldlm_mode_t mdt_dlm_lock_modes[] = {
92         [MDL_MINMODE] = LCK_MINMODE,
93         [MDL_EX]      = LCK_EX,
94         [MDL_PW]      = LCK_PW,
95         [MDL_PR]      = LCK_PR,
96         [MDL_CW]      = LCK_CW,
97         [MDL_CR]      = LCK_CR,
98         [MDL_NL]      = LCK_NL,
99         [MDL_GROUP]   = LCK_GROUP
100 };
101
102 /*
103  * Initialized in mdt_mod_init().
104  */
105 static unsigned long mdt_num_threads;
106 static unsigned long mdt_min_threads;
107 static unsigned long mdt_max_threads;
108
109 /* ptlrpc request handler for MDT. All handlers are
110  * grouped into several slices - struct mdt_opc_slice,
111  * and stored in an array - mdt_handlers[].
112  */
113 struct mdt_handler {
114         /* The name of this handler. */
115         const char *mh_name;
116         /* Fail id for this handler, checked at the beginning of this handler*/
117         int         mh_fail_id;
118         /* Operation code for this handler */
119         __u32       mh_opc;
120         /* flags are listed in enum mdt_handler_flags below. */
121         __u32       mh_flags;
122         /* The actual handler function to execute. */
123         int (*mh_act)(struct mdt_thread_info *info);
124         /* Request format for this request. */
125         const struct req_format *mh_fmt;
126 };
127
128 enum mdt_handler_flags {
129         /*
130          * struct mdt_body is passed in the incoming message, and object
131          * identified by this fid exists on disk.
132          *
133          * "habeo corpus" == "I have a body"
134          */
135         HABEO_CORPUS = (1 << 0),
136         /*
137          * struct ldlm_request is passed in the incoming message.
138          *
139          * "habeo clavis" == "I have a key"
140          */
141         HABEO_CLAVIS = (1 << 1),
142         /*
143          * this request has fixed reply format, so that reply message can be
144          * packed by generic code.
145          *
146          * "habeo refero" == "I have a reply"
147          */
148         HABEO_REFERO = (1 << 2),
149         /*
150          * this request will modify something, so check whether the filesystem
151          * is readonly or not, then return -EROFS to client asap if necessary.
152          *
153          * "mutabor" == "I shall modify"
154          */
155         MUTABOR      = (1 << 3)
156 };
157
158 struct mdt_opc_slice {
159         __u32               mos_opc_start;
160         int                 mos_opc_end;
161         struct mdt_handler *mos_hs;
162 };
163
164 static struct mdt_opc_slice mdt_regular_handlers[];
165 static struct mdt_opc_slice mdt_readpage_handlers[];
166 static struct mdt_opc_slice mdt_xmds_handlers[];
167 static struct mdt_opc_slice mdt_seq_handlers[];
168 static struct mdt_opc_slice mdt_fld_handlers[];
169
170 static struct mdt_device *mdt_dev(struct lu_device *d);
171 static int mdt_regular_handle(struct ptlrpc_request *req);
172 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
173 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
174                         struct getinfo_fid2path *fp);
175
176 static const struct lu_object_operations mdt_obj_ops;
177
178 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
179 {
180         if (!rep)
181                 return 0;
182         return (rep->lock_policy_res1 & flag);
183 }
184
185 void mdt_clear_disposition(struct mdt_thread_info *info,
186                            struct ldlm_reply *rep, int flag)
187 {
188         if (info)
189                 info->mti_opdata &= ~flag;
190         if (rep)
191                 rep->lock_policy_res1 &= ~flag;
192 }
193
194 void mdt_set_disposition(struct mdt_thread_info *info,
195                          struct ldlm_reply *rep, int flag)
196 {
197         if (info)
198                 info->mti_opdata |= flag;
199         if (rep)
200                 rep->lock_policy_res1 |= flag;
201 }
202
203 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
204 {
205         lh->mlh_pdo_hash = 0;
206         lh->mlh_reg_mode = lm;
207         lh->mlh_type = MDT_REG_LOCK;
208 }
209
210 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
211                        const char *name, int namelen)
212 {
213         lh->mlh_reg_mode = lm;
214         lh->mlh_type = MDT_PDO_LOCK;
215
216         if (name != NULL) {
217                 LASSERT(namelen > 0);
218                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
219         } else {
220                 LASSERT(namelen == 0);
221                 lh->mlh_pdo_hash = 0ull;
222         }
223 }
224
225 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
226                               struct mdt_lock_handle *lh)
227 {
228         mdl_mode_t mode;
229         ENTRY;
230
231         /*
232          * Any dir access needs couple of locks:
233          *
234          * 1) on part of dir we gonna take lookup/modify;
235          *
236          * 2) on whole dir to protect it from concurrent splitting and/or to
237          * flush client's cache for readdir().
238          *
239          * so, for a given mode and object this routine decides what lock mode
240          * to use for lock #2:
241          *
242          * 1) if caller's gonna lookup in dir then we need to protect dir from
243          * being splitted only - LCK_CR
244          *
245          * 2) if caller's gonna modify dir then we need to protect dir from
246          * being splitted and to flush cache - LCK_CW
247          *
248          * 3) if caller's gonna modify dir and that dir seems ready for
249          * splitting then we need to protect it from any type of access
250          * (lookup/modify/split) - LCK_EX --bzzz
251          */
252
253         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
254         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
255
256         /*
257          * Ask underlaying level its opinion about preferable PDO lock mode
258          * having access type passed as regular lock mode:
259          *
260          * - MDL_MINMODE means that lower layer does not want to specify lock
261          * mode;
262          *
263          * - MDL_NL means that no PDO lock should be taken. This is used in some
264          * cases. Say, for non-splittable directories no need to use PDO locks
265          * at all.
266          */
267         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
268                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
269
270         if (mode != MDL_MINMODE) {
271                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
272         } else {
273                 /*
274                  * Lower layer does not want to specify locking mode. We do it
275                  * our selves. No special protection is needed, just flush
276                  * client's cache on modification and allow concurrent
277                  * mondification.
278                  */
279                 switch (lh->mlh_reg_mode) {
280                 case LCK_EX:
281                         lh->mlh_pdo_mode = LCK_EX;
282                         break;
283                 case LCK_PR:
284                         lh->mlh_pdo_mode = LCK_CR;
285                         break;
286                 case LCK_PW:
287                         lh->mlh_pdo_mode = LCK_CW;
288                         break;
289                 default:
290                         CERROR("Not expected lock type (0x%x)\n",
291                                (int)lh->mlh_reg_mode);
292                         LBUG();
293                 }
294         }
295
296         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
297         EXIT;
298 }
299
300 static int mdt_getstatus(struct mdt_thread_info *info)
301 {
302         struct mdt_device *mdt  = info->mti_mdt;
303         struct md_device  *next = mdt->mdt_child;
304         struct mdt_body   *repbody;
305         int                rc;
306
307         ENTRY;
308
309         rc = mdt_check_ucred(info);
310         if (rc)
311                 RETURN(err_serious(rc));
312
313         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
314                 RETURN(err_serious(-ENOMEM));
315
316         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
317         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
318         if (rc != 0)
319                 RETURN(rc);
320
321         repbody->valid |= OBD_MD_FLID;
322
323         if (mdt->mdt_opts.mo_mds_capa &&
324             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
325                 struct mdt_object  *root;
326                 struct lustre_capa *capa;
327
328                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
329                 if (IS_ERR(root))
330                         RETURN(PTR_ERR(root));
331
332                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
333                 LASSERT(capa);
334                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
335                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
336                                  0);
337                 mdt_object_put(info->mti_env, root);
338                 if (rc == 0)
339                         repbody->valid |= OBD_MD_FLMDSCAPA;
340         }
341
342         RETURN(rc);
343 }
344
345 static int mdt_statfs(struct mdt_thread_info *info)
346 {
347         struct ptlrpc_request *req = mdt_info_req(info);
348         struct md_device      *next  = info->mti_mdt->mdt_child;
349         struct ptlrpc_service *svc;
350         struct obd_statfs     *osfs;
351         int                    rc;
352
353         ENTRY;
354
355         svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
356
357         /* This will trigger a watchdog timeout */
358         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
359                          (MDT_SERVICE_WATCHDOG_FACTOR *
360                           at_get(&svc->srv_at_estimate)) + 1);
361
362         rc = mdt_check_ucred(info);
363         if (rc)
364                 RETURN(err_serious(rc));
365
366         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
367                 rc = err_serious(-ENOMEM);
368         } else {
369                 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
370                 rc = next->md_ops->mdo_statfs(info->mti_env, next,
371                                               &info->mti_u.ksfs);
372                 statfs_pack(osfs, &info->mti_u.ksfs);
373         }
374
375         if (rc == 0)
376                 mdt_counter_incr(req->rq_export, LPROC_MDT_STATFS);
377
378         RETURN(rc);
379 }
380
381 /**
382  * Pack SOM attributes into the reply.
383  * Call under a DLM UPDATE lock.
384  */
385 static void mdt_pack_size2body(struct mdt_thread_info *info,
386                                struct mdt_object *mo)
387 {
388         struct mdt_body *b;
389         struct md_attr *ma = &info->mti_attr;
390
391         LASSERT(ma->ma_attr.la_valid & LA_MODE);
392         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
393
394         /* Check if Size-on-MDS is supported, if this is a regular file,
395          * if SOM is enabled on the object and if SOM cache exists and valid.
396          * Otherwise do not pack Size-on-MDS attributes to the reply. */
397         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
398             !S_ISREG(ma->ma_attr.la_mode) ||
399             !mdt_object_is_som_enabled(mo) ||
400             !(ma->ma_valid & MA_SOM))
401                 return;
402
403         b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
404         b->size = ma->ma_som->msd_size;
405         b->blocks = ma->ma_som->msd_blocks;
406 }
407
408 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
409                         const struct lu_attr *attr, const struct lu_fid *fid)
410 {
411         struct md_attr *ma = &info->mti_attr;
412
413         LASSERT(ma->ma_valid & MA_INODE);
414
415         b->atime      = attr->la_atime;
416         b->mtime      = attr->la_mtime;
417         b->ctime      = attr->la_ctime;
418         b->mode       = attr->la_mode;
419         b->size       = attr->la_size;
420         b->blocks     = attr->la_blocks;
421         b->uid        = attr->la_uid;
422         b->gid        = attr->la_gid;
423         b->flags      = attr->la_flags;
424         b->nlink      = attr->la_nlink;
425         b->rdev       = attr->la_rdev;
426
427         /*XXX should pack the reply body according to lu_valid*/
428         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
429                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
430                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
431                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
432
433         if (!S_ISREG(attr->la_mode)) {
434                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
435         } else if (ma->ma_need & MA_LOV && ma->ma_lmm_size == 0) {
436                 /* means no objects are allocated on osts. */
437                 LASSERT(!(ma->ma_valid & MA_LOV));
438                 /* just ignore blocks occupied by extend attributes on MDS */
439                 b->blocks = 0;
440                 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
441                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
442         }
443
444         if (fid) {
445                 b->fid1 = *fid;
446                 b->valid |= OBD_MD_FLID;
447
448                 /* FIXME: these should be fixed when new igif ready.*/
449                 b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
450                 b->generation = fid_ver(fid);  /* 1.6 compatibility */
451                 b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
452
453                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
454                                 PFID(fid), b->nlink, b->mode, b->size);
455         }
456
457         if (info)
458                 mdt_body_reverse_idmap(info, b);
459
460         if (b->valid & OBD_MD_FLSIZE)
461                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
462                        PFID(fid), (unsigned long long)b->size);
463 }
464
465 static inline int mdt_body_has_lov(const struct lu_attr *la,
466                                    const struct mdt_body *body)
467 {
468         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
469                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
470 }
471
472 static int mdt_getattr_internal(struct mdt_thread_info *info,
473                                 struct mdt_object *o, int ma_need)
474 {
475         struct md_object        *next = mdt_object_child(o);
476         const struct mdt_body   *reqbody = info->mti_body;
477         struct ptlrpc_request   *req = mdt_info_req(info);
478         struct md_attr          *ma = &info->mti_attr;
479         struct lu_attr          *la = &ma->ma_attr;
480         struct req_capsule      *pill = info->mti_pill;
481         const struct lu_env     *env = info->mti_env;
482         struct mdt_body         *repbody;
483         struct lu_buf           *buffer = &info->mti_buf;
484         int                     rc;
485         ENTRY;
486
487         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
488                 RETURN(err_serious(-ENOMEM));
489
490         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
491
492         ma->ma_valid = 0;
493
494         rc = mdt_object_exists(o);
495         if (rc < 0) {
496                 /* This object is located on remote node.*/
497                 repbody->fid1 = *mdt_object_fid(o);
498                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
499                 RETURN(0);
500         }
501
502         buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
503         buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
504
505         /* If it is dir object and client require MEA, then we got MEA */
506         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
507             reqbody->valid & OBD_MD_MEA) {
508                 /* Assumption: MDT_MD size is enough for lmv size. */
509                 ma->ma_lmv = buffer->lb_buf;
510                 ma->ma_lmv_size = buffer->lb_len;
511                 ma->ma_need = MA_LMV | MA_INODE;
512         } else {
513                 ma->ma_lmm = buffer->lb_buf;
514                 ma->ma_lmm_size = buffer->lb_len;
515                 ma->ma_need = MA_LOV | MA_INODE;
516         }
517
518         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
519             reqbody->valid & OBD_MD_FLDIREA  &&
520             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
521                 /* get default stripe info for this dir. */
522                 ma->ma_need |= MA_LOV_DEF;
523         }
524         ma->ma_need |= ma_need;
525         if (ma->ma_need & MA_SOM)
526                 ma->ma_som = &info->mti_u.som.data;
527
528         rc = mo_attr_get(env, next, ma);
529         if (unlikely(rc)) {
530                 CERROR("getattr error for "DFID": %d\n",
531                         PFID(mdt_object_fid(o)), rc);
532                 RETURN(rc);
533         }
534
535         if (likely(ma->ma_valid & MA_INODE))
536                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
537         else
538                 RETURN(-EFAULT);
539
540         if (mdt_body_has_lov(la, reqbody)) {
541                 if (ma->ma_valid & MA_LOV) {
542                         LASSERT(ma->ma_lmm_size);
543                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
544                         repbody->eadatasize = ma->ma_lmm_size;
545                         if (S_ISDIR(la->la_mode))
546                                 repbody->valid |= OBD_MD_FLDIREA;
547                         else
548                                 repbody->valid |= OBD_MD_FLEASIZE;
549                 }
550                 if (ma->ma_valid & MA_LMV) {
551                         LASSERT(S_ISDIR(la->la_mode));
552                         repbody->eadatasize = ma->ma_lmv_size;
553                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
554                 }
555         } else if (S_ISLNK(la->la_mode) &&
556                    reqbody->valid & OBD_MD_LINKNAME) {
557                 buffer->lb_buf = ma->ma_lmm;
558                 buffer->lb_len = reqbody->eadatasize;
559                 rc = mo_readlink(env, next, buffer);
560                 if (unlikely(rc <= 0)) {
561                         CERROR("readlink failed: %d\n", rc);
562                         rc = -EFAULT;
563                 } else {
564                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
565                                  rc -= 2;
566                         repbody->valid |= OBD_MD_LINKNAME;
567                         repbody->eadatasize = rc;
568                         /* NULL terminate */
569                         ((char*)ma->ma_lmm)[rc - 1] = 0;
570                         CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
571                                (char*)ma->ma_lmm, rc);
572                         rc = 0;
573                 }
574         }
575
576         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
577                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
578                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
579                 repbody->valid |= OBD_MD_FLMODEASIZE;
580                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
581                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
582                        repbody->max_cookiesize);
583         }
584
585         if (exp_connect_rmtclient(info->mti_exp) &&
586             reqbody->valid & OBD_MD_FLRMTPERM) {
587                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
588
589                 /* mdt_getattr_lock only */
590                 rc = mdt_pack_remote_perm(info, o, buf);
591                 if (rc) {
592                         repbody->valid &= ~OBD_MD_FLRMTPERM;
593                         repbody->aclsize = 0;
594                         RETURN(rc);
595                 } else {
596                         repbody->valid |= OBD_MD_FLRMTPERM;
597                         repbody->aclsize = sizeof(struct mdt_remote_perm);
598                 }
599         }
600 #ifdef CONFIG_FS_POSIX_ACL
601         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
602                  (reqbody->valid & OBD_MD_FLACL)) {
603                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
604                 buffer->lb_len = req_capsule_get_size(pill,
605                                                       &RMF_ACL, RCL_SERVER);
606                 if (buffer->lb_len > 0) {
607                         rc = mo_xattr_get(env, next, buffer,
608                                           XATTR_NAME_ACL_ACCESS);
609                         if (rc < 0) {
610                                 if (rc == -ENODATA) {
611                                         repbody->aclsize = 0;
612                                         repbody->valid |= OBD_MD_FLACL;
613                                         rc = 0;
614                                 } else if (rc == -EOPNOTSUPP) {
615                                         rc = 0;
616                                 } else {
617                                         CERROR("got acl size: %d\n", rc);
618                                 }
619                         } else {
620                                 repbody->aclsize = rc;
621                                 repbody->valid |= OBD_MD_FLACL;
622                                 rc = 0;
623                         }
624                 }
625         }
626 #endif
627
628         if (reqbody->valid & OBD_MD_FLMDSCAPA &&
629             info->mti_mdt->mdt_opts.mo_mds_capa &&
630             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
631                 struct lustre_capa *capa;
632
633                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
634                 LASSERT(capa);
635                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
636                 rc = mo_capa_get(env, next, capa, 0);
637                 if (rc)
638                         RETURN(rc);
639                 repbody->valid |= OBD_MD_FLMDSCAPA;
640         }
641         RETURN(rc);
642 }
643
644 static int mdt_renew_capa(struct mdt_thread_info *info)
645 {
646         struct mdt_object  *obj = info->mti_object;
647         struct mdt_body    *body;
648         struct lustre_capa *capa, *c;
649         int rc;
650         ENTRY;
651
652         /* if object doesn't exist, or server has disabled capability,
653          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
654          * flag not set.
655          */
656         if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
657             !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
658                 RETURN(0);
659
660         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
661         LASSERT(body != NULL);
662
663         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
664         LASSERT(c);
665
666         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
667         LASSERT(capa);
668
669         *capa = *c;
670         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
671         if (rc == 0)
672                 body->valid |= OBD_MD_FLOSSCAPA;
673         RETURN(rc);
674 }
675
676 static int mdt_getattr(struct mdt_thread_info *info)
677 {
678         struct ptlrpc_request   *req = mdt_info_req(info);
679         struct mdt_object       *obj = info->mti_object;
680         struct req_capsule      *pill = info->mti_pill;
681         struct mdt_body         *reqbody;
682         struct mdt_body         *repbody;
683         mode_t                   mode;
684         int                      md_size;
685         int rc;
686         ENTRY;
687
688         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
689         LASSERT(reqbody);
690
691         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
692                 rc = req_capsule_server_pack(pill);
693                 if (unlikely(rc))
694                         RETURN(err_serious(rc));
695                 rc = mdt_renew_capa(info);
696                 GOTO(out_shrink, rc);
697         }
698
699         LASSERT(obj != NULL);
700         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
701
702         mode = lu_object_attr(&obj->mot_obj.mo_lu);
703         if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
704             (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
705                 md_size = reqbody->eadatasize;
706         else
707                 md_size = info->mti_mdt->mdt_max_mdsize;
708
709         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
710
711         rc = req_capsule_server_pack(pill);
712         if (unlikely(rc != 0))
713                 RETURN(err_serious(rc));
714
715         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
716         LASSERT(repbody != NULL);
717         repbody->eadatasize = 0;
718         repbody->aclsize = 0;
719
720         if (reqbody->valid & OBD_MD_FLRMTPERM)
721                 rc = mdt_init_ucred(info, reqbody);
722         else
723                 rc = mdt_check_ucred(info);
724         if (unlikely(rc))
725                 GOTO(out_shrink, rc);
726
727         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
728         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
729
730         /*
731          * Don't check capability at all, because rename might getattr for
732          * remote obj, and at that time no capability is available.
733          */
734         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
735         rc = mdt_getattr_internal(info, obj, 0);
736         if (reqbody->valid & OBD_MD_FLRMTPERM)
737                 mdt_exit_ucred(info);
738         EXIT;
739 out_shrink:
740         if (rc == 0)
741                 mdt_counter_incr(req->rq_export, LPROC_MDT_GETATTR);
742
743         mdt_shrink_reply(info);
744         return rc;
745 }
746
747 static int mdt_is_subdir(struct mdt_thread_info *info)
748 {
749         struct mdt_object     *o = info->mti_object;
750         struct req_capsule    *pill = info->mti_pill;
751         const struct mdt_body *body = info->mti_body;
752         struct mdt_body       *repbody;
753         int                    rc;
754         ENTRY;
755
756         LASSERT(o != NULL);
757
758         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
759
760         /*
761          * We save last checked parent fid to @repbody->fid1 for remote
762          * directory case.
763          */
764         LASSERT(fid_is_sane(&body->fid2));
765         LASSERT(mdt_object_exists(o) > 0);
766         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
767                            &body->fid2, &repbody->fid1);
768         if (rc == 0 || rc == -EREMOTE)
769                 repbody->valid |= OBD_MD_FLID;
770
771         RETURN(rc);
772 }
773
774 static int mdt_raw_lookup(struct mdt_thread_info *info,
775                           struct mdt_object *parent,
776                           const struct lu_name *lname,
777                           struct ldlm_reply *ldlm_rep)
778 {
779         struct md_object *next = mdt_object_child(info->mti_object);
780         const struct mdt_body *reqbody = info->mti_body;
781         struct lu_fid *child_fid = &info->mti_tmp_fid1;
782         struct mdt_body *repbody;
783         int rc;
784         ENTRY;
785
786         if (reqbody->valid != OBD_MD_FLID)
787                 RETURN(0);
788
789         LASSERT(!info->mti_cross_ref);
790
791         /* Only got the fid of this obj by name */
792         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
793                         &info->mti_spec);
794 #if 0
795         /* XXX is raw_lookup possible as intent operation? */
796         if (rc != 0) {
797                 if (rc == -ENOENT)
798                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
799                 RETURN(rc);
800         } else
801                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
802
803         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
804 #endif
805         if (rc == 0) {
806                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
807                 repbody->fid1 = *child_fid;
808                 repbody->valid = OBD_MD_FLID;
809         }
810         RETURN(1);
811 }
812
813 /*
814  * UPDATE lock should be taken against parent, and be release before exit;
815  * child_bits lock should be taken against child, and be returned back:
816  *            (1)normal request should release the child lock;
817  *            (2)intent request will grant the lock to client.
818  */
819 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
820                                  struct mdt_lock_handle *lhc,
821                                  __u64 child_bits,
822                                  struct ldlm_reply *ldlm_rep)
823 {
824         struct ptlrpc_request  *req       = mdt_info_req(info);
825         struct mdt_body        *reqbody   = NULL;
826         struct mdt_object      *parent    = info->mti_object;
827         struct mdt_object      *child;
828         struct md_object       *next      = mdt_object_child(parent);
829         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
830         struct lu_name         *lname     = NULL;
831         const char             *name      = NULL;
832         int                     namelen   = 0;
833         struct mdt_lock_handle *lhp       = NULL;
834         struct ldlm_lock       *lock;
835         struct ldlm_res_id     *res_id;
836         int                     is_resent;
837         int                     ma_need = 0;
838         int                     rc;
839
840         ENTRY;
841
842         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
843         LASSERT(ergo(is_resent,
844                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
845
846         LASSERT(parent != NULL);
847         name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
848         if (name == NULL)
849                 RETURN(err_serious(-EFAULT));
850
851         namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
852                                        RCL_CLIENT) - 1;
853         if (!info->mti_cross_ref) {
854                 /*
855                  * XXX: Check for "namelen == 0" is for getattr by fid
856                  * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
857                  * that is the name must contain at least one character and
858                  * the terminating '\0'
859                  */
860                 if (namelen == 0) {
861                         reqbody = req_capsule_client_get(info->mti_pill,
862                                                          &RMF_MDT_BODY);
863                         if (unlikely(reqbody == NULL))
864                                 RETURN(err_serious(-EFAULT));
865
866                         if (unlikely(!fid_is_sane(&reqbody->fid2)))
867                                 RETURN(err_serious(-EINVAL));
868
869                         name = NULL;
870                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
871                                "ldlm_rep = %p\n",
872                                PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
873                                ldlm_rep);
874                 } else {
875                         lname = mdt_name(info->mti_env, (char *)name, namelen);
876                         CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
877                                "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
878                                name, ldlm_rep);
879                 }
880         }
881         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
882
883         rc = mdt_object_exists(parent);
884         if (unlikely(rc == 0)) {
885                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
886                                 &parent->mot_obj.mo_lu,
887                                 "Parent doesn't exist!\n");
888                 RETURN(-ESTALE);
889         } else if (!info->mti_cross_ref) {
890                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
891                          PFID(mdt_object_fid(parent)));
892         }
893         if (lname) {
894                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
895                 if (rc != 0) {
896                         if (rc > 0)
897                                 rc = 0;
898                         RETURN(rc);
899                 }
900         }
901
902         if (info->mti_cross_ref) {
903                 /* Only getattr on the child. Parent is on another node. */
904                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
905                 child = parent;
906                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
907                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
908
909                 if (is_resent) {
910                         /* Do not take lock for resent case. */
911                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
912                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
913                                  lhc->mlh_reg_lh.cookie);
914                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
915                                                 &lock->l_resource->lr_name));
916                         LDLM_LOCK_PUT(lock);
917                         rc = 0;
918                 } else {
919                         mdt_lock_handle_init(lhc);
920                         mdt_lock_reg_init(lhc, LCK_PR);
921
922                         /*
923                          * Object's name is on another MDS, no lookup lock is
924                          * needed here but update is.
925                          */
926                         child_bits &= ~MDS_INODELOCK_LOOKUP;
927                         child_bits |= MDS_INODELOCK_UPDATE;
928
929                         rc = mdt_object_lock(info, child, lhc, child_bits,
930                                              MDT_LOCAL_LOCK);
931                 }
932                 if (rc == 0) {
933                         /* Finally, we can get attr for child. */
934                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
935                                          BYPASS_CAPA);
936                         rc = mdt_getattr_internal(info, child, 0);
937                         if (unlikely(rc != 0))
938                                 mdt_object_unlock(info, child, lhc, 1);
939                 }
940                 RETURN(rc);
941         }
942
943         if (lname) {
944                 /* step 1: lock parent */
945                 lhp = &info->mti_lh[MDT_LH_PARENT];
946                 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
947                 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
948                                      MDT_LOCAL_LOCK);
949                 if (unlikely(rc != 0))
950                         RETURN(rc);
951
952                 /* step 2: lookup child's fid by name */
953                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
954                                 &info->mti_spec);
955
956                 if (rc != 0) {
957                         if (rc == -ENOENT)
958                                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
959                         GOTO(out_parent, rc);
960                 } else
961                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
962         } else {
963                 *child_fid = reqbody->fid2;
964                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
965         }
966
967         /*
968          *step 3: find the child object by fid & lock it.
969          *        regardless if it is local or remote.
970          */
971         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
972
973         if (unlikely(IS_ERR(child)))
974                 GOTO(out_parent, rc = PTR_ERR(child));
975         if (is_resent) {
976                 /* Do not take lock for resent case. */
977                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
978                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
979                          lhc->mlh_reg_lh.cookie);
980
981                 res_id = &lock->l_resource->lr_name;
982                 if (!fid_res_name_eq(mdt_object_fid(child),
983                                     &lock->l_resource->lr_name)) {
984                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
985                                                  &lock->l_resource->lr_name),
986                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
987                                  (unsigned long)res_id->name[0],
988                                  (unsigned long)res_id->name[1],
989                                  (unsigned long)res_id->name[2],
990                                  PFID(mdt_object_fid(parent)));
991                           CWARN("Although resent, but still not get child lock"
992                                 "parent:"DFID" child:"DFID"\n",
993                                 PFID(mdt_object_fid(parent)),
994                                 PFID(mdt_object_fid(child)));
995                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
996                           LDLM_LOCK_PUT(lock);
997                           GOTO(relock, 0);
998                 }
999                 LDLM_LOCK_PUT(lock);
1000                 rc = 0;
1001         } else {
1002 relock:
1003                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1004                 mdt_lock_handle_init(lhc);
1005                 mdt_lock_reg_init(lhc, LCK_PR);
1006
1007                 if (mdt_object_exists(child) == 0) {
1008                         LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1009                                         &child->mot_obj.mo_lu,
1010                                         "Object doesn't exist!\n");
1011                         GOTO(out_child, rc = -ENOENT);
1012                 }
1013
1014                 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1015                         struct md_attr *ma = &info->mti_attr;
1016
1017                         ma->ma_valid = 0;
1018                         ma->ma_need = MA_INODE;
1019                         rc = mo_attr_get(info->mti_env,
1020                                          mdt_object_child(child), ma);
1021                         if (unlikely(rc != 0))
1022                                 GOTO(out_child, rc);
1023
1024                         /* If the file has not been changed for some time, we
1025                          * return not only a LOOKUP lock, but also an UPDATE
1026                          * lock and this might save us RPC on later STAT. For
1027                          * directories, it also let negative dentry starts
1028                          * working for this dir. */
1029                         if (ma->ma_valid & MA_INODE &&
1030                             ma->ma_attr.la_valid & LA_CTIME &&
1031                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1032                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1033                                 child_bits |= MDS_INODELOCK_UPDATE;
1034                 }
1035
1036                 rc = mdt_object_lock(info, child, lhc, child_bits,
1037                                      MDT_CROSS_LOCK);
1038
1039                 if (unlikely(rc != 0))
1040                         GOTO(out_child, rc);
1041         }
1042
1043         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1044         /* Get MA_SOM attributes if update lock is given. */
1045         if (lock &&
1046             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1047             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1048                 ma_need = MA_SOM;
1049
1050         /* finally, we can get attr for child. */
1051         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1052         rc = mdt_getattr_internal(info, child, ma_need);
1053         if (unlikely(rc != 0)) {
1054                 mdt_object_unlock(info, child, lhc, 1);
1055         } else if (lock) {
1056                 /* Debugging code. */
1057                 res_id = &lock->l_resource->lr_name;
1058                 LDLM_DEBUG(lock, "Returning lock to client");
1059                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1060                                          &lock->l_resource->lr_name),
1061                          "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1062                          (unsigned long)res_id->name[0],
1063                          (unsigned long)res_id->name[1],
1064                          (unsigned long)res_id->name[2],
1065                          PFID(mdt_object_fid(child)));
1066                 mdt_pack_size2body(info, child);
1067         }
1068         if (lock)
1069                 LDLM_LOCK_PUT(lock);
1070
1071         EXIT;
1072 out_child:
1073         mdt_object_put(info->mti_env, child);
1074 out_parent:
1075         if (lhp)
1076                 mdt_object_unlock(info, parent, lhp, 1);
1077         return rc;
1078 }
1079
1080 /* normal handler: should release the child lock */
1081 static int mdt_getattr_name(struct mdt_thread_info *info)
1082 {
1083         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1084         struct mdt_body        *reqbody;
1085         struct mdt_body        *repbody;
1086         int rc;
1087         ENTRY;
1088
1089         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1090         LASSERT(reqbody != NULL);
1091         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1092         LASSERT(repbody != NULL);
1093
1094         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1095         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1096         repbody->eadatasize = 0;
1097         repbody->aclsize = 0;
1098
1099         rc = mdt_init_ucred(info, reqbody);
1100         if (unlikely(rc))
1101                 GOTO(out_shrink, rc);
1102
1103         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1104         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1105                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1106                 lhc->mlh_reg_lh.cookie = 0;
1107         }
1108         mdt_exit_ucred(info);
1109         EXIT;
1110 out_shrink:
1111         mdt_shrink_reply(info);
1112         return rc;
1113 }
1114
1115 static const struct lu_device_operations mdt_lu_ops;
1116
1117 static int lu_device_is_mdt(struct lu_device *d)
1118 {
1119         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1120 }
1121
1122 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1123                          void *karg, void *uarg);
1124
1125 static int mdt_set_info(struct mdt_thread_info *info)
1126 {
1127         struct ptlrpc_request *req = mdt_info_req(info);
1128         char *key;
1129         void *val;
1130         int keylen, vallen, rc = 0;
1131         ENTRY;
1132
1133         rc = req_capsule_server_pack(info->mti_pill);
1134         if (rc)
1135                 RETURN(rc);
1136
1137         key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1138         if (key == NULL) {
1139                 DEBUG_REQ(D_HA, req, "no set_info key");
1140                 RETURN(-EFAULT);
1141         }
1142
1143         keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1144                                       RCL_CLIENT);
1145
1146         val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1147         if (val == NULL) {
1148                 DEBUG_REQ(D_HA, req, "no set_info val");
1149                 RETURN(-EFAULT);
1150         }
1151
1152         vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1153                                       RCL_CLIENT);
1154
1155         /* Swab any part of val you need to here */
1156         if (KEY_IS(KEY_READ_ONLY)) {
1157                 req->rq_status = 0;
1158                 lustre_msg_set_status(req->rq_repmsg, 0);
1159
1160                 cfs_spin_lock(&req->rq_export->exp_lock);
1161                 if (*(__u32 *)val)
1162                         req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1163                 else
1164                         req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1165                 cfs_spin_unlock(&req->rq_export->exp_lock);
1166
1167         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1168                 struct changelog_setinfo *cs =
1169                         (struct changelog_setinfo *)val;
1170                 if (vallen != sizeof(*cs)) {
1171                         CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1172                         RETURN(-EINVAL);
1173                 }
1174                 if (ptlrpc_req_need_swab(req)) {
1175                         __swab64s(&cs->cs_recno);
1176                         __swab32s(&cs->cs_id);
1177                 }
1178
1179                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1180                                    vallen, val, NULL);
1181                 lustre_msg_set_status(req->rq_repmsg, rc);
1182
1183         } else {
1184                 RETURN(-EINVAL);
1185         }
1186         RETURN(0);
1187 }
1188
1189 static int mdt_connect(struct mdt_thread_info *info)
1190 {
1191         int rc;
1192         struct ptlrpc_request *req;
1193
1194         req = mdt_info_req(info);
1195         rc = target_handle_connect(req);
1196         if (rc == 0) {
1197                 LASSERT(req->rq_export != NULL);
1198                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1199                 rc = mdt_init_sec_level(info);
1200                 if (rc == 0)
1201                         rc = mdt_init_idmap(info);
1202                 if (rc != 0)
1203                         obd_disconnect(class_export_get(req->rq_export));
1204         } else {
1205                 rc = err_serious(rc);
1206         }
1207         return rc;
1208 }
1209
1210 static int mdt_disconnect(struct mdt_thread_info *info)
1211 {
1212         int rc;
1213         ENTRY;
1214
1215         rc = target_handle_disconnect(mdt_info_req(info));
1216         if (rc)
1217                 rc = err_serious(rc);
1218         RETURN(rc);
1219 }
1220
1221 static int mdt_sendpage(struct mdt_thread_info *info,
1222                         struct lu_rdpg *rdpg, int nob)
1223 {
1224         struct ptlrpc_request   *req = mdt_info_req(info);
1225         struct obd_export       *exp = req->rq_export;
1226         struct ptlrpc_bulk_desc *desc;
1227         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1228         int                      tmpcount;
1229         int                      tmpsize;
1230         int                      i;
1231         int                      rc;
1232         ENTRY;
1233
1234         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1235                                     MDS_BULK_PORTAL);
1236         if (desc == NULL)
1237                 RETURN(-ENOMEM);
1238
1239         for (i = 0, tmpcount = nob;
1240                 i < rdpg->rp_npages && tmpcount > 0; i++, tmpcount -= tmpsize) {
1241                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1242                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1243         }
1244
1245         LASSERT(desc->bd_nob == nob);
1246         rc = target_bulk_io(exp, desc, lwi);
1247         ptlrpc_free_bulk(desc);
1248         RETURN(rc);
1249 }
1250
1251 #ifdef HAVE_SPLIT_SUPPORT
1252 /*
1253  * Retrieve dir entry from the page and insert it to the slave object, actually,
1254  * this should be in osd layer, but since it will not in the final product, so
1255  * just do it here and do not define more moo api anymore for this.
1256  */
1257 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1258                               int size)
1259 {
1260         struct mdt_object *object = info->mti_object;
1261         struct lu_fid *lf = &info->mti_tmp_fid2;
1262         struct md_attr *ma = &info->mti_attr;
1263         struct lu_dirpage *dp;
1264         struct lu_dirent *ent;
1265         int rc = 0, offset = 0;
1266         ENTRY;
1267
1268         /* Make sure we have at least one entry. */
1269         if (size == 0)
1270                 RETURN(-EINVAL);
1271
1272         /*
1273          * Disable trans for this name insert, since it will include many trans
1274          * for this.
1275          */
1276         info->mti_no_need_trans = 1;
1277         /*
1278          * When write_dir_page, no need update parent's ctime,
1279          * and no permission check for name_insert.
1280          */
1281         ma->ma_attr.la_ctime = 0;
1282         ma->ma_attr.la_valid = LA_MODE;
1283         ma->ma_valid = MA_INODE;
1284
1285         cfs_kmap(page);
1286         dp = page_address(page);
1287         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1288
1289         for (ent = lu_dirent_start(dp); ent != NULL;
1290              ent = lu_dirent_next(ent)) {
1291                 struct lu_name *lname;
1292                 char *name;
1293
1294                 if (le16_to_cpu(ent->lde_namelen) == 0)
1295                         continue;
1296
1297                 fid_le_to_cpu(lf, &ent->lde_fid);
1298                 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1299                         ma->ma_attr.la_mode = S_IFDIR;
1300                 else
1301                         ma->ma_attr.la_mode = 0;
1302                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1303                 if (name == NULL)
1304                         GOTO(out, rc = -ENOMEM);
1305
1306                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1307                 lname = mdt_name(info->mti_env, name,
1308                                  le16_to_cpu(ent->lde_namelen));
1309                 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1310                 rc = mdo_name_insert(info->mti_env,
1311                                      md_object_next(&object->mot_obj),
1312                                      lname, lf, ma);
1313                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1314                 if (rc) {
1315                         CERROR("Can't insert %*.*s, rc %d\n",
1316                                le16_to_cpu(ent->lde_namelen),
1317                                le16_to_cpu(ent->lde_namelen),
1318                                ent->lde_name, rc);
1319                         GOTO(out, rc);
1320                 }
1321
1322                 offset += lu_dirent_size(ent);
1323                 if (offset >= size)
1324                         break;
1325         }
1326         EXIT;
1327 out:
1328         cfs_kunmap(page);
1329         return rc;
1330 }
1331
1332 static int mdt_bulk_timeout(void *data)
1333 {
1334         ENTRY;
1335
1336         CERROR("mdt bulk transfer timeout \n");
1337
1338         RETURN(1);
1339 }
1340
1341 static int mdt_writepage(struct mdt_thread_info *info)
1342 {
1343         struct ptlrpc_request   *req = mdt_info_req(info);
1344         struct mdt_body         *reqbody;
1345         struct l_wait_info      *lwi;
1346         struct ptlrpc_bulk_desc *desc;
1347         struct page             *page;
1348         int                rc;
1349         ENTRY;
1350
1351
1352         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1353         if (reqbody == NULL)
1354                 RETURN(err_serious(-EFAULT));
1355
1356         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1357         if (desc == NULL)
1358                 RETURN(err_serious(-ENOMEM));
1359
1360         /* allocate the page for the desc */
1361         page = cfs_alloc_page(CFS_ALLOC_STD);
1362         if (page == NULL)
1363                 GOTO(desc_cleanup, rc = -ENOMEM);
1364
1365         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1366                (int)reqbody->size, (int)reqbody->nlink);
1367
1368         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1369                               (int)reqbody->nlink);
1370
1371         rc = sptlrpc_svc_prep_bulk(req, desc);
1372         if (rc != 0)
1373                 GOTO(cleanup_page, rc);
1374         /*
1375          * Check if client was evicted while we were doing i/o before touching
1376          * network.
1377          */
1378         OBD_ALLOC_PTR(lwi);
1379         if (!lwi)
1380                 GOTO(cleanup_page, rc = -ENOMEM);
1381
1382         if (desc->bd_export->exp_failed)
1383                 rc = -ENOTCONN;
1384         else
1385                 rc = ptlrpc_start_bulk_transfer (desc);
1386         if (rc == 0) {
1387                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * CFS_HZ / 4, CFS_HZ,
1388                                             mdt_bulk_timeout, desc);
1389                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1390                                   desc->bd_export->exp_failed, lwi);
1391                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1392                 if (rc == -ETIMEDOUT) {
1393                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1394                         ptlrpc_abort_bulk(desc);
1395                 } else if (desc->bd_export->exp_failed) {
1396                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1397                         rc = -ENOTCONN;
1398                         ptlrpc_abort_bulk(desc);
1399                 } else if (!desc->bd_success ||
1400                            desc->bd_nob_transferred != desc->bd_nob) {
1401                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1402                                   desc->bd_success ?
1403                                   "truncated" : "network error on",
1404                                   desc->bd_nob_transferred, desc->bd_nob);
1405                         /* XXX should this be a different errno? */
1406                         rc = -ETIMEDOUT;
1407                 }
1408         } else {
1409                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1410         }
1411         if (rc)
1412                 GOTO(cleanup_lwi, rc);
1413         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1414
1415 cleanup_lwi:
1416         OBD_FREE_PTR(lwi);
1417 cleanup_page:
1418         cfs_free_page(page);
1419 desc_cleanup:
1420         ptlrpc_free_bulk(desc);
1421         RETURN(rc);
1422 }
1423 #endif
1424
1425 static int mdt_readpage(struct mdt_thread_info *info)
1426 {
1427         struct mdt_object *object = info->mti_object;
1428         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1429         struct mdt_body   *reqbody;
1430         struct mdt_body   *repbody;
1431         int                rc;
1432         int                i;
1433         ENTRY;
1434
1435         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1436                 RETURN(err_serious(-ENOMEM));
1437
1438         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1439         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1440         if (reqbody == NULL || repbody == NULL)
1441                 RETURN(err_serious(-EFAULT));
1442
1443         /*
1444          * prepare @rdpg before calling lower layers and transfer itself. Here
1445          * reqbody->size contains offset of where to start to read and
1446          * reqbody->nlink contains number bytes to read.
1447          */
1448         rdpg->rp_hash = reqbody->size;
1449         if (rdpg->rp_hash != reqbody->size) {
1450                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1451                        rdpg->rp_hash, reqbody->size);
1452                 RETURN(-EFAULT);
1453         }
1454
1455         rdpg->rp_attrs = reqbody->mode;
1456         if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1457                 rdpg->rp_attrs |= LUDA_64BITHASH;
1458         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1459                                 PTLRPC_MAX_BRW_SIZE);
1460         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1461                           CFS_PAGE_SHIFT;
1462         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1463         if (rdpg->rp_pages == NULL)
1464                 RETURN(-ENOMEM);
1465
1466         for (i = 0; i < rdpg->rp_npages; ++i) {
1467                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1468                 if (rdpg->rp_pages[i] == NULL)
1469                         GOTO(free_rdpg, rc = -ENOMEM);
1470         }
1471
1472         /* call lower layers to fill allocated pages with directory data */
1473         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1474         if (rc < 0)
1475                 GOTO(free_rdpg, rc);
1476
1477         /* send pages to client */
1478         rc = mdt_sendpage(info, rdpg, rc);
1479
1480         EXIT;
1481 free_rdpg:
1482
1483         for (i = 0; i < rdpg->rp_npages; i++)
1484                 if (rdpg->rp_pages[i] != NULL)
1485                         cfs_free_page(rdpg->rp_pages[i]);
1486         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1487
1488         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1489                 RETURN(0);
1490
1491         return rc;
1492 }
1493
1494 static int mdt_reint_internal(struct mdt_thread_info *info,
1495                               struct mdt_lock_handle *lhc,
1496                               __u32 op)
1497 {
1498         struct req_capsule      *pill = info->mti_pill;
1499         struct mdt_device       *mdt = info->mti_mdt;
1500         struct md_quota         *mq = md_quota(info->mti_env);
1501         struct mdt_body         *repbody;
1502         int                      rc = 0;
1503         ENTRY;
1504
1505         /* pack reply */
1506         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1507                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1508                                      mdt->mdt_max_mdsize);
1509         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1510                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1511                                      mdt->mdt_max_cookiesize);
1512
1513         rc = req_capsule_server_pack(pill);
1514         if (rc != 0) {
1515                 CERROR("Can't pack response, rc %d\n", rc);
1516                 RETURN(err_serious(rc));
1517         }
1518
1519         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1520                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1521                 LASSERT(repbody);
1522                 repbody->eadatasize = 0;
1523                 repbody->aclsize = 0;
1524         }
1525
1526         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1527                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1528
1529         rc = mdt_reint_unpack(info, op);
1530         if (rc != 0) {
1531                 CERROR("Can't unpack reint, rc %d\n", rc);
1532                 GOTO(out_shrink, rc = err_serious(rc));
1533         }
1534
1535         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1536
1537         /* for replay no cookkie / lmm need, because client have this already */
1538         if (info->mti_spec.no_create == 1)  {
1539                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1540                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1541
1542                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1543                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1544                                              0);
1545         }
1546
1547         rc = mdt_init_ucred_reint(info);
1548         if (rc)
1549                 GOTO(out_shrink, rc);
1550
1551         rc = mdt_fix_attr_ucred(info, op);
1552         if (rc != 0)
1553                 GOTO(out_ucred, rc = err_serious(rc));
1554
1555         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1556                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1557                 GOTO(out_ucred, rc);
1558         }
1559         mq->mq_exp = info->mti_exp;
1560         rc = mdt_reint_rec(info, lhc);
1561         EXIT;
1562 out_ucred:
1563         mdt_exit_ucred(info);
1564 out_shrink:
1565         mdt_shrink_reply(info);
1566         return rc;
1567 }
1568
1569 static long mdt_reint_opcode(struct mdt_thread_info *info,
1570                              const struct req_format **fmt)
1571 {
1572         struct mdt_rec_reint *rec;
1573         long opc;
1574
1575         opc = err_serious(-EFAULT);
1576         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1577         if (rec != NULL) {
1578                 opc = rec->rr_opcode;
1579                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1580                 if (opc < REINT_MAX && fmt[opc] != NULL)
1581                         req_capsule_extend(info->mti_pill, fmt[opc]);
1582                 else {
1583                         CERROR("Unsupported opc: %ld\n", opc);
1584                         opc = err_serious(opc);
1585                 }
1586         }
1587         return opc;
1588 }
1589
1590 static int mdt_reint(struct mdt_thread_info *info)
1591 {
1592         long opc;
1593         int  rc;
1594
1595         static const struct req_format *reint_fmts[REINT_MAX] = {
1596                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1597                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1598                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1599                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1600                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1601                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1602                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1603         };
1604
1605         ENTRY;
1606
1607         opc = mdt_reint_opcode(info, reint_fmts);
1608         if (opc >= 0) {
1609                 /*
1610                  * No lock possible here from client to pass it to reint code
1611                  * path.
1612                  */
1613                 rc = mdt_reint_internal(info, NULL, opc);
1614         } else {
1615                 rc = opc;
1616         }
1617
1618         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1619         RETURN(rc);
1620 }
1621
1622 /* this should sync the whole device */
1623 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1624 {
1625         struct dt_device *dt = mdt->mdt_bottom;
1626         int rc;
1627         ENTRY;
1628
1629         rc = dt->dd_ops->dt_sync(env, dt);
1630         RETURN(rc);
1631 }
1632
1633 /* this should sync this object */
1634 static int mdt_object_sync(struct mdt_thread_info *info)
1635 {
1636         struct md_object *next;
1637         int rc;
1638         ENTRY;
1639
1640         if (!mdt_object_exists(info->mti_object)) {
1641                 CWARN("Non existing object  "DFID"!\n",
1642                       PFID(mdt_object_fid(info->mti_object)));
1643                 RETURN(-ESTALE);
1644         }
1645         next = mdt_object_child(info->mti_object);
1646         rc = mo_object_sync(info->mti_env, next);
1647
1648         RETURN(rc);
1649 }
1650
1651 static int mdt_sync(struct mdt_thread_info *info)
1652 {
1653         struct ptlrpc_request *req = mdt_info_req(info);
1654         struct req_capsule *pill = info->mti_pill;
1655         struct mdt_body *body;
1656         int rc;
1657         ENTRY;
1658
1659         /* The fid may be zero, so we req_capsule_set manually */
1660         req_capsule_set(pill, &RQF_MDS_SYNC);
1661
1662         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1663         if (body == NULL)
1664                 RETURN(err_serious(-EINVAL));
1665
1666         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1667                 RETURN(err_serious(-ENOMEM));
1668
1669         if (fid_seq(&body->fid1) == 0) {
1670                 /* sync the whole device */
1671                 rc = req_capsule_server_pack(pill);
1672                 if (rc == 0)
1673                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1674                 else
1675                         rc = err_serious(rc);
1676         } else {
1677                 /* sync an object */
1678                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1679                 if (rc == 0) {
1680                         rc = mdt_object_sync(info);
1681                         if (rc == 0) {
1682                                 struct md_object *next;
1683                                 const struct lu_fid *fid;
1684                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1685
1686                                 next = mdt_object_child(info->mti_object);
1687                                 info->mti_attr.ma_need = MA_INODE;
1688                                 info->mti_attr.ma_valid = 0;
1689                                 rc = mo_attr_get(info->mti_env, next,
1690                                                  &info->mti_attr);
1691                                 if (rc == 0) {
1692                                         body = req_capsule_server_get(pill,
1693                                                                 &RMF_MDT_BODY);
1694                                         fid = mdt_object_fid(info->mti_object);
1695                                         mdt_pack_attr2body(info, body, la, fid);
1696                                 }
1697                         }
1698                 } else
1699                         rc = err_serious(rc);
1700         }
1701         if (rc == 0)
1702                 mdt_counter_incr(req->rq_export, LPROC_MDT_SYNC);
1703
1704         RETURN(rc);
1705 }
1706
1707 #ifdef HAVE_QUOTA_SUPPORT
1708 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1709 {
1710         struct obd_quotactl *oqctl;
1711         struct req_capsule *pill = info->mti_pill;
1712         struct obd_export *exp = info->mti_exp;
1713         struct md_quota *mq = md_quota(info->mti_env);
1714         struct md_device *next = info->mti_mdt->mdt_child;
1715         int rc;
1716         ENTRY;
1717
1718         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1719         if (oqctl == NULL)
1720                 RETURN(-EPROTO);
1721
1722         /* remote client has no permission for quotacheck */
1723         if (unlikely(exp_connect_rmtclient(exp)))
1724                 RETURN(-EPERM);
1725
1726         rc = req_capsule_server_pack(pill);
1727         if (rc)
1728                 RETURN(rc);
1729
1730         mq->mq_exp = exp;
1731         rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next,
1732                                                oqctl->qc_type);
1733         RETURN(rc);
1734 }
1735
1736 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1737 {
1738         struct obd_quotactl *oqctl, *repoqc;
1739         struct req_capsule *pill = info->mti_pill;
1740         struct obd_export *exp = info->mti_exp;
1741         struct md_quota *mq = md_quota(info->mti_env);
1742         struct md_device *next = info->mti_mdt->mdt_child;
1743         const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1744         int id, rc;
1745         ENTRY;
1746
1747         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1748         if (oqctl == NULL)
1749                 RETURN(-EPROTO);
1750
1751         id = oqctl->qc_id;
1752         if (exp_connect_rmtclient(exp)) {
1753                 struct ptlrpc_request *req = mdt_info_req(info);
1754                 struct mdt_export_data *med = mdt_req2med(req);
1755                 struct lustre_idmap_table *idmap = med->med_idmap;
1756
1757                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1758                              oqctl->qc_cmd != Q_GETINFO))
1759                         RETURN(-EPERM);
1760
1761
1762                 if (oqctl->qc_type == USRQUOTA)
1763                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1764                                                      oqctl->qc_id);
1765                 else if (oqctl->qc_type == GRPQUOTA)
1766                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1767                                                      oqctl->qc_id);
1768                 else
1769                         RETURN(-EINVAL);
1770
1771                 if (id == CFS_IDMAP_NOTFOUND) {
1772                         CDEBUG(D_QUOTA, "no mapping for id %u\n",
1773                                oqctl->qc_id);
1774                         RETURN(-EACCES);
1775                 }
1776         }
1777
1778         rc = req_capsule_server_pack(pill);
1779         if (rc)
1780                 RETURN(rc);
1781
1782         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1783         LASSERT(repoqc != NULL);
1784
1785         mq->mq_exp = exp;
1786         switch (oqctl->qc_cmd) {
1787         case Q_QUOTAON:
1788                 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1789                 break;
1790         case Q_QUOTAOFF:
1791                 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1792                 break;
1793         case Q_SETINFO:
1794                 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1795                                       &oqctl->qc_dqinfo);
1796                 break;
1797         case Q_GETINFO:
1798                 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1799                                       &oqctl->qc_dqinfo);
1800                 break;
1801         case Q_SETQUOTA:
1802                 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1803                                        &oqctl->qc_dqblk);
1804                 break;
1805         case Q_GETQUOTA:
1806                 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1807                                        &oqctl->qc_dqblk);
1808                 break;
1809         case Q_GETOINFO:
1810                 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1811                                        &oqctl->qc_dqinfo);
1812                 break;
1813         case Q_GETOQUOTA:
1814                 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1815                                         &oqctl->qc_dqblk);
1816                 break;
1817         case LUSTRE_Q_INVALIDATE:
1818                 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1819                 break;
1820         case LUSTRE_Q_FINVALIDATE:
1821                 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1822                 break;
1823         default:
1824                 CERROR("unsupported mdt_quotactl command: %d\n",
1825                        oqctl->qc_cmd);
1826                 RETURN(-EFAULT);
1827         }
1828
1829         *repoqc = *oqctl;
1830         RETURN(rc);
1831 }
1832 #endif
1833
1834
1835 /*
1836  * OBD PING and other handlers.
1837  */
1838 static int mdt_obd_ping(struct mdt_thread_info *info)
1839 {
1840         int rc;
1841         ENTRY;
1842
1843         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1844
1845         rc = target_handle_ping(mdt_info_req(info));
1846         if (rc < 0)
1847                 rc = err_serious(rc);
1848         RETURN(rc);
1849 }
1850
1851 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1852 {
1853         return err_serious(-EOPNOTSUPP);
1854 }
1855
1856 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1857 {
1858         return err_serious(-EOPNOTSUPP);
1859 }
1860
1861
1862 /*
1863  * LLOG handlers.
1864  */
1865
1866 /** clone llog ctxt from child (mdd)
1867  * This allows remote llog (replicator) access.
1868  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1869  * context was originally set up, or we can handle them directly.
1870  * I choose the latter, but that means I need any llog
1871  * contexts set up by child to be accessable by the mdt.  So we clone the
1872  * context into our context list here.
1873  */
1874 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1875                                int idx)
1876 {
1877         struct md_device  *next = mdt->mdt_child;
1878         struct llog_ctxt *ctxt;
1879         int rc;
1880
1881         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1882                 return 0;
1883
1884         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1885         if (rc || ctxt == NULL) {
1886                 CERROR("Can't get mdd ctxt %d\n", rc);
1887                 return rc;
1888         }
1889
1890         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1891         if (rc)
1892                 CERROR("Can't set mdt ctxt %d\n", rc);
1893
1894         return rc;
1895 }
1896
1897 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1898                                  struct mdt_device *mdt, int idx)
1899 {
1900         struct llog_ctxt *ctxt;
1901
1902         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1903         if (ctxt == NULL)
1904                 return 0;
1905         /* Put once for the get we just did, and once for the clone */
1906         llog_ctxt_put(ctxt);
1907         llog_ctxt_put(ctxt);
1908         return 0;
1909 }
1910
1911 static int mdt_llog_create(struct mdt_thread_info *info)
1912 {
1913         int rc;
1914
1915         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1916         rc = llog_origin_handle_create(mdt_info_req(info));
1917         return (rc < 0 ? err_serious(rc) : rc);
1918 }
1919
1920 static int mdt_llog_destroy(struct mdt_thread_info *info)
1921 {
1922         int rc;
1923
1924         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1925         rc = llog_origin_handle_destroy(mdt_info_req(info));
1926         return (rc < 0 ? err_serious(rc) : rc);
1927 }
1928
1929 static int mdt_llog_read_header(struct mdt_thread_info *info)
1930 {
1931         int rc;
1932
1933         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1934         rc = llog_origin_handle_read_header(mdt_info_req(info));
1935         return (rc < 0 ? err_serious(rc) : rc);
1936 }
1937
1938 static int mdt_llog_next_block(struct mdt_thread_info *info)
1939 {
1940         int rc;
1941
1942         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1943         rc = llog_origin_handle_next_block(mdt_info_req(info));
1944         return (rc < 0 ? err_serious(rc) : rc);
1945 }
1946
1947 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1948 {
1949         int rc;
1950
1951         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1952         rc = llog_origin_handle_prev_block(mdt_info_req(info));
1953         return (rc < 0 ? err_serious(rc) : rc);
1954 }
1955
1956
1957 /*
1958  * DLM handlers.
1959  */
1960 static struct ldlm_callback_suite cbs = {
1961         .lcs_completion = ldlm_server_completion_ast,
1962         .lcs_blocking   = ldlm_server_blocking_ast,
1963         .lcs_glimpse    = NULL
1964 };
1965
1966 static int mdt_enqueue(struct mdt_thread_info *info)
1967 {
1968         struct ptlrpc_request *req;
1969         int rc;
1970
1971         /*
1972          * info->mti_dlm_req already contains swapped and (if necessary)
1973          * converted dlm request.
1974          */
1975         LASSERT(info->mti_dlm_req != NULL);
1976
1977         req = mdt_info_req(info);
1978         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1979                                   req, info->mti_dlm_req, &cbs);
1980         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1981         return rc ? err_serious(rc) : req->rq_status;
1982 }
1983
1984 static int mdt_convert(struct mdt_thread_info *info)
1985 {
1986         int rc;
1987         struct ptlrpc_request *req;
1988
1989         LASSERT(info->mti_dlm_req);
1990         req = mdt_info_req(info);
1991         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1992         return rc ? err_serious(rc) : req->rq_status;
1993 }
1994
1995 static int mdt_bl_callback(struct mdt_thread_info *info)
1996 {
1997         CERROR("bl callbacks should not happen on MDS\n");
1998         LBUG();
1999         return err_serious(-EOPNOTSUPP);
2000 }
2001
2002 static int mdt_cp_callback(struct mdt_thread_info *info)
2003 {
2004         CERROR("cp callbacks should not happen on MDS\n");
2005         LBUG();
2006         return err_serious(-EOPNOTSUPP);
2007 }
2008
2009 /*
2010  * sec context handlers
2011  */
2012 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2013 {
2014         int rc;
2015
2016         rc = mdt_handle_idmap(info);
2017
2018         if (unlikely(rc)) {
2019                 struct ptlrpc_request *req = mdt_info_req(info);
2020                 __u32                  opc;
2021
2022                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2023                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2024                         sptlrpc_svc_ctx_invalidate(req);
2025         }
2026
2027         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2028
2029         return rc;
2030 }
2031
2032 static struct mdt_object *mdt_obj(struct lu_object *o)
2033 {
2034         LASSERT(lu_device_is_mdt(o->lo_dev));
2035         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2036 }
2037
2038 struct mdt_object *mdt_object_find(const struct lu_env *env,
2039                                    struct mdt_device *d,
2040                                    const struct lu_fid *f)
2041 {
2042         struct lu_object *o;
2043         struct mdt_object *m;
2044         ENTRY;
2045
2046         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2047         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2048         if (unlikely(IS_ERR(o)))
2049                 m = (struct mdt_object *)o;
2050         else
2051                 m = mdt_obj(o);
2052         RETURN(m);
2053 }
2054
2055 /**
2056  * Asyncronous commit for mdt device.
2057  *
2058  * Pass asynchonous commit call down the MDS stack.
2059  *
2060  * \param env environment
2061  * \param mdt the mdt device
2062  */
2063 static void mdt_device_commit_async(const struct lu_env *env,
2064                                     struct mdt_device *mdt)
2065 {
2066         struct dt_device *dt = mdt->mdt_bottom;
2067         int rc;
2068
2069         rc = dt->dd_ops->dt_commit_async(env, dt);
2070         if (unlikely(rc != 0))
2071                 CWARN("async commit start failed with rc = %d", rc);
2072 }
2073
2074 /**
2075  * Mark the lock as "synchonous".
2076  *
2077  * Mark the lock to deffer transaction commit to the unlock time.
2078  *
2079  * \param lock the lock to mark as "synchonous"
2080  *
2081  * \see mdt_is_lock_sync
2082  * \see mdt_save_lock
2083  */
2084 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2085 {
2086         lock->l_ast_data = (void*)1;
2087 }
2088
2089 /**
2090  * Check whehter the lock "synchonous" or not.
2091  *
2092  * \param lock the lock to check
2093  * \retval 1 the lock is "synchonous"
2094  * \retval 0 the lock isn't "synchronous"
2095  *
2096  * \see mdt_set_lock_sync
2097  * \see mdt_save_lock
2098  */
2099 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2100 {
2101         return lock->l_ast_data != NULL;
2102 }
2103
2104 /**
2105  * Blocking AST for mdt locks.
2106  *
2107  * Starts transaction commit if in case of COS lock conflict or
2108  * deffers such a commit to the mdt_save_lock.
2109  *
2110  * \param lock the lock which blocks a request or cancelling lock
2111  * \param desc unused
2112  * \param data unused
2113  * \param flag indicates whether this cancelling or blocking callback
2114  * \retval 0
2115  * \see ldlm_blocking_ast_nocheck
2116  */
2117 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2118                      void *data, int flag)
2119 {
2120         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2121         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2122         int rc;
2123         ENTRY;
2124
2125         if (flag == LDLM_CB_CANCELING)
2126                 RETURN(0);
2127         lock_res_and_lock(lock);
2128         if (lock->l_blocking_ast != mdt_blocking_ast) {
2129                 unlock_res_and_lock(lock);
2130                 RETURN(0);
2131         }
2132         if (mdt_cos_is_enabled(mdt) &&
2133             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2134             lock->l_blocking_lock != NULL &&
2135             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2136                 mdt_set_lock_sync(lock);
2137         }
2138         rc = ldlm_blocking_ast_nocheck(lock);
2139
2140         /* There is no lock conflict if l_blocking_lock == NULL,
2141          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2142          * when the last reference to a local lock was released */
2143         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2144                 struct lu_env env;
2145
2146                 rc = lu_env_init(&env, LCT_MD_THREAD);
2147                 if (unlikely(rc != 0))
2148                         CWARN("lu_env initialization failed with rc = %d,"
2149                               "cannot start asynchronous commit\n", rc);
2150                 else
2151                         mdt_device_commit_async(&env, mdt);
2152                 lu_env_fini(&env);
2153         }
2154         RETURN(rc);
2155 }
2156
2157 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2158                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2159 {
2160         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2161         ldlm_policy_data_t *policy = &info->mti_policy;
2162         struct ldlm_res_id *res_id = &info->mti_res_id;
2163         int rc;
2164         ENTRY;
2165
2166         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2167         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2168         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2169         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2170
2171         if (mdt_object_exists(o) < 0) {
2172                 if (locality == MDT_CROSS_LOCK) {
2173                         /* cross-ref object fix */
2174                         ibits &= ~MDS_INODELOCK_UPDATE;
2175                         ibits |= MDS_INODELOCK_LOOKUP;
2176                 } else {
2177                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2178                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2179                 }
2180                 /* No PDO lock on remote object */
2181                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2182         }
2183
2184         if (lh->mlh_type == MDT_PDO_LOCK) {
2185                 /* check for exists after object is locked */
2186                 if (mdt_object_exists(o) == 0) {
2187                         /* Non-existent object shouldn't have PDO lock */
2188                         RETURN(-ESTALE);
2189                 } else {
2190                         /* Non-dir object shouldn't have PDO lock */
2191                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2192                                 RETURN(-ENOTDIR);
2193                 }
2194         }
2195
2196         memset(policy, 0, sizeof(*policy));
2197         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2198
2199         /*
2200          * Take PDO lock on whole directory and build correct @res_id for lock
2201          * on part of directory.
2202          */
2203         if (lh->mlh_pdo_hash != 0) {
2204                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2205                 mdt_lock_pdo_mode(info, o, lh);
2206                 if (lh->mlh_pdo_mode != LCK_NL) {
2207                         /*
2208                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2209                          * is never going to be sent to client and we do not
2210                          * want it slowed down due to possible cancels.
2211                          */
2212                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2213                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2214                                           policy, res_id, LDLM_FL_ATOMIC_CB,
2215                                           &info->mti_exp->exp_handle.h_cookie);
2216                         if (unlikely(rc))
2217                                 RETURN(rc);
2218                 }
2219
2220                 /*
2221                  * Finish res_id initializing by name hash marking part of
2222                  * directory which is taking modification.
2223                  */
2224                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2225         }
2226
2227         policy->l_inodebits.bits = ibits;
2228
2229         /*
2230          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2231          * going to be sent to client. If it is - mdt_intent_policy() path will
2232          * fix it up and turn FL_LOCAL flag off.
2233          */
2234         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2235                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2236                           &info->mti_exp->exp_handle.h_cookie);
2237         if (rc)
2238                 mdt_object_unlock(info, o, lh, 1);
2239         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2240                  lh->mlh_pdo_hash != 0 &&
2241                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2242                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2243         }
2244
2245         RETURN(rc);
2246 }
2247
2248 /**
2249  * Save a lock within request object.
2250  *
2251  * Keep the lock referenced until whether client ACK or transaction
2252  * commit happens or release the lock immediately depending on input
2253  * parameters. If COS is ON, a write lock is converted to COS lock
2254  * before saving.
2255  *
2256  * \param info thead info object
2257  * \param h lock handle
2258  * \param mode lock mode
2259  * \param decref force immediate lock releasing
2260  */
2261 static
2262 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2263                    ldlm_mode_t mode, int decref)
2264 {
2265         ENTRY;
2266
2267         if (lustre_handle_is_used(h)) {
2268                 if (decref || !info->mti_has_trans ||
2269                     !(mode & (LCK_PW | LCK_EX))){
2270                         mdt_fid_unlock(h, mode);
2271                 } else {
2272                         struct mdt_device *mdt = info->mti_mdt;
2273                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2274                         struct ptlrpc_request *req = mdt_info_req(info);
2275                         int no_ack = 0;
2276
2277                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2278                                  h->cookie);
2279                         CDEBUG(D_HA, "request = %p reply state = %p"
2280                                " transno = "LPD64"\n",
2281                                req, req->rq_reply_state, req->rq_transno);
2282                         if (mdt_cos_is_enabled(mdt)) {
2283                                 no_ack = 1;
2284                                 ldlm_lock_downgrade(lock, LCK_COS);
2285                                 mode = LCK_COS;
2286                         }
2287                         ptlrpc_save_lock(req, h, mode, no_ack);
2288                         if (mdt_is_lock_sync(lock)) {
2289                                 CDEBUG(D_HA, "found sync-lock,"
2290                                        " async commit started\n");
2291                                 mdt_device_commit_async(info->mti_env,
2292                                                         mdt);
2293                         }
2294                         LDLM_LOCK_PUT(lock);
2295                 }
2296                 h->cookie = 0ull;
2297         }
2298
2299         EXIT;
2300 }
2301
2302 /**
2303  * Unlock mdt object.
2304  *
2305  * Immeditely release the regular lock and the PDO lock or save the
2306  * lock in reqeuest and keep them referenced until client ACK or
2307  * transaction commit.
2308  *
2309  * \param info thread info object
2310  * \param o mdt object
2311  * \param lh mdt lock handle referencing regular and PDO locks
2312  * \param decref force immediate lock releasing
2313  */
2314 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2315                        struct mdt_lock_handle *lh, int decref)
2316 {
2317         ENTRY;
2318
2319         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2320         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2321
2322         EXIT;
2323 }
2324
2325 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2326                                         const struct lu_fid *f,
2327                                         struct mdt_lock_handle *lh,
2328                                         __u64 ibits)
2329 {
2330         struct mdt_object *o;
2331
2332         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2333         if (!IS_ERR(o)) {
2334                 int rc;
2335
2336                 rc = mdt_object_lock(info, o, lh, ibits,
2337                                      MDT_LOCAL_LOCK);
2338                 if (rc != 0) {
2339                         mdt_object_put(info->mti_env, o);
2340                         o = ERR_PTR(rc);
2341                 }
2342         }
2343         return o;
2344 }
2345
2346 void mdt_object_unlock_put(struct mdt_thread_info * info,
2347                            struct mdt_object * o,
2348                            struct mdt_lock_handle *lh,
2349                            int decref)
2350 {
2351         mdt_object_unlock(info, o, lh, decref);
2352         mdt_object_put(info->mti_env, o);
2353 }
2354
2355 static struct mdt_handler *mdt_handler_find(__u32 opc,
2356                                             struct mdt_opc_slice *supported)
2357 {
2358         struct mdt_opc_slice *s;
2359         struct mdt_handler   *h;
2360
2361         h = NULL;
2362         for (s = supported; s->mos_hs != NULL; s++) {
2363                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2364                         h = s->mos_hs + (opc - s->mos_opc_start);
2365                         if (likely(h->mh_opc != 0))
2366                                 LASSERTF(h->mh_opc == opc,
2367                                          "opcode mismatch %d != %d\n",
2368                                          h->mh_opc, opc);
2369                         else
2370                                 h = NULL; /* unsupported opc */
2371                         break;
2372                 }
2373         }
2374         return h;
2375 }
2376
2377 static int mdt_lock_resname_compat(struct mdt_device *m,
2378                                    struct ldlm_request *req)
2379 {
2380         /* XXX something... later. */
2381         return 0;
2382 }
2383
2384 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2385 {
2386         /* XXX something... later. */
2387         return 0;
2388 }
2389
2390 /*
2391  * Generic code handling requests that have struct mdt_body passed in:
2392  *
2393  *  - extract mdt_body from request and save it in @info, if present;
2394  *
2395  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2396  *  @info;
2397  *
2398  *  - if HABEO_CORPUS flag is set for this request type check whether object
2399  *  actually exists on storage (lu_object_exists()).
2400  *
2401  */
2402 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2403 {
2404         const struct mdt_body    *body;
2405         struct mdt_object        *obj;
2406         const struct lu_env      *env;
2407         struct req_capsule       *pill;
2408         int                       rc;
2409         ENTRY;
2410
2411         env = info->mti_env;
2412         pill = info->mti_pill;
2413
2414         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2415         if (body == NULL)
2416                 RETURN(-EFAULT);
2417
2418         if (!(body->valid & OBD_MD_FLID))
2419                 RETURN(0);
2420
2421         if (!fid_is_sane(&body->fid1)) {
2422                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2423                 RETURN(-EINVAL);
2424         }
2425
2426         /*
2427          * Do not get size or any capa fields before we check that request
2428          * contains capa actually. There are some requests which do not, for
2429          * instance MDS_IS_SUBDIR.
2430          */
2431         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2432             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2433                 mdt_set_capainfo(info, 0, &body->fid1,
2434                                  req_capsule_client_get(pill, &RMF_CAPA1));
2435
2436         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2437         if (!IS_ERR(obj)) {
2438                 if ((flags & HABEO_CORPUS) &&
2439                     !mdt_object_exists(obj)) {
2440                         mdt_object_put(env, obj);
2441                         /* for capability renew ENOENT will be handled in
2442                          * mdt_renew_capa */
2443                         if (body->valid & OBD_MD_FLOSSCAPA)
2444                                 rc = 0;
2445                         else
2446                                 rc = -ENOENT;
2447                 } else {
2448                         info->mti_object = obj;
2449                         rc = 0;
2450                 }
2451         } else
2452                 rc = PTR_ERR(obj);
2453
2454         RETURN(rc);
2455 }
2456
2457 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2458 {
2459         struct req_capsule *pill = info->mti_pill;
2460         int rc;
2461         ENTRY;
2462
2463         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2464                 rc = mdt_body_unpack(info, flags);
2465         else
2466                 rc = 0;
2467
2468         if (rc == 0 && (flags & HABEO_REFERO)) {
2469                 struct mdt_device *mdt = info->mti_mdt;
2470
2471                 /* Pack reply. */
2472
2473                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2474                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2475                                              mdt->mdt_max_mdsize);
2476                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2477                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2478                                              mdt->mdt_max_cookiesize);
2479
2480                 rc = req_capsule_server_pack(pill);
2481         }
2482         RETURN(rc);
2483 }
2484
2485 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2486 {
2487         struct md_device *next = m->mdt_child;
2488
2489         return next->md_ops->mdo_init_capa_ctxt(env, next,
2490                                                 m->mdt_opts.mo_mds_capa,
2491                                                 m->mdt_capa_timeout,
2492                                                 m->mdt_capa_alg,
2493                                                 m->mdt_capa_keys);
2494 }
2495
2496 /*
2497  * Invoke handler for this request opc. Also do necessary preprocessing
2498  * (according to handler ->mh_flags), and post-processing (setting of
2499  * ->last_{xid,committed}).
2500  */
2501 static int mdt_req_handle(struct mdt_thread_info *info,
2502                           struct mdt_handler *h, struct ptlrpc_request *req)
2503 {
2504         int   rc, serious = 0;
2505         __u32 flags;
2506
2507         ENTRY;
2508
2509         LASSERT(h->mh_act != NULL);
2510         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2511         LASSERT(current->journal_info == NULL);
2512
2513         /*
2514          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2515          * to put same checks into handlers like mdt_close(), mdt_reint(),
2516          * etc., without talking to mdt authors first. Checking same thing
2517          * there again is useless and returning 0 error without packing reply
2518          * is buggy! Handlers either pack reply or return error.
2519          *
2520          * We return 0 here and do not send any reply in order to emulate
2521          * network failure. Do not send any reply in case any of NET related
2522          * fail_id has occured.
2523          */
2524         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2525                 RETURN(0);
2526
2527         rc = 0;
2528         flags = h->mh_flags;
2529         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2530
2531         if (h->mh_fmt != NULL) {
2532                 req_capsule_set(info->mti_pill, h->mh_fmt);
2533                 rc = mdt_unpack_req_pack_rep(info, flags);
2534         }
2535
2536         if (rc == 0 && flags & MUTABOR &&
2537             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2538                 /* should it be rq_status? */
2539                 rc = -EROFS;
2540
2541         if (rc == 0 && flags & HABEO_CLAVIS) {
2542                 struct ldlm_request *dlm_req;
2543
2544                 LASSERT(h->mh_fmt != NULL);
2545
2546                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2547                 if (dlm_req != NULL) {
2548                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2549                                         LDLM_IBITS &&
2550                                      dlm_req->lock_desc.l_policy_data.\
2551                                         l_inodebits.bits == 0)) {
2552                                 /*
2553                                  * Lock without inodebits makes no sense and
2554                                  * will oops later in ldlm. If client miss to
2555                                  * set such bits, do not trigger ASSERTION.
2556                                  *
2557                                  * For liblustre flock case, it maybe zero.
2558                                  */
2559                                 rc = -EPROTO;
2560                         } else {
2561                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2562                                         rc = mdt_lock_resname_compat(
2563                                                                 info->mti_mdt,
2564                                                                 dlm_req);
2565                                 info->mti_dlm_req = dlm_req;
2566                         }
2567                 } else {
2568                         rc = -EFAULT;
2569                 }
2570         }
2571
2572         /* capability setting changed via /proc, needs reinitialize ctxt */
2573         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2574                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2575                 info->mti_mdt->mdt_capa_conf = 0;
2576         }
2577
2578         if (likely(rc == 0)) {
2579                 /*
2580                  * Process request, there can be two types of rc:
2581                  * 1) errors with msg unpack/pack, other failures outside the
2582                  * operation itself. This is counted as serious errors;
2583                  * 2) errors during fs operation, should be placed in rq_status
2584                  * only
2585                  */
2586                 rc = h->mh_act(info);
2587                 if (rc == 0 &&
2588                     !req->rq_no_reply && req->rq_reply_state == NULL) {
2589                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2590                                   "pack reply and returned 0 error\n",
2591                                   h->mh_name);
2592                         LBUG();
2593                 }
2594                 serious = is_serious(rc);
2595                 rc = clear_serious(rc);
2596         } else
2597                 serious = 1;
2598
2599         req->rq_status = rc;
2600
2601         /*
2602          * ELDLM_* codes which > 0 should be in rq_status only as well as
2603          * all non-serious errors.
2604          */
2605         if (rc > 0 || !serious)
2606                 rc = 0;
2607
2608         LASSERT(current->journal_info == NULL);
2609
2610         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2611             info->mti_mdt->mdt_opts.mo_compat_resname) {
2612                 struct ldlm_reply *dlmrep;
2613
2614                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2615                 if (dlmrep != NULL)
2616                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2617         }
2618
2619         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2620         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2621                 target_committed_to_req(req);
2622
2623         if (unlikely(req_is_replay(req) &&
2624                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2625                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2626                 LBUG();
2627         }
2628
2629         target_send_reply(req, rc, info->mti_fail_id);
2630         RETURN(0);
2631 }
2632
2633 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2634 {
2635         lh->mlh_type = MDT_NUL_LOCK;
2636         lh->mlh_reg_lh.cookie = 0ull;
2637         lh->mlh_reg_mode = LCK_MINMODE;
2638         lh->mlh_pdo_lh.cookie = 0ull;
2639         lh->mlh_pdo_mode = LCK_MINMODE;
2640 }
2641
2642 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2643 {
2644         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2645         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2646 }
2647
2648 /*
2649  * Initialize fields of struct mdt_thread_info. Other fields are left in
2650  * uninitialized state, because it's too expensive to zero out whole
2651  * mdt_thread_info (> 1K) on each request arrival.
2652  */
2653 static void mdt_thread_info_init(struct ptlrpc_request *req,
2654                                  struct mdt_thread_info *info)
2655 {
2656         int i;
2657         struct md_capainfo *ci;
2658
2659         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2660         info->mti_pill = &req->rq_pill;
2661
2662         /* lock handle */
2663         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2664                 mdt_lock_handle_init(&info->mti_lh[i]);
2665
2666         /* mdt device: it can be NULL while CONNECT */
2667         if (req->rq_export) {
2668                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2669                 info->mti_exp = req->rq_export;
2670         } else
2671                 info->mti_mdt = NULL;
2672         info->mti_env = req->rq_svc_thread->t_env;
2673         ci = md_capainfo(info->mti_env);
2674         memset(ci, 0, sizeof *ci);
2675         if (req->rq_export) {
2676                 if (exp_connect_rmtclient(req->rq_export))
2677                         ci->mc_auth = LC_ID_CONVERT;
2678                 else if (req->rq_export->exp_connect_flags &
2679                          OBD_CONNECT_MDS_CAPA)
2680                         ci->mc_auth = LC_ID_PLAIN;
2681                 else
2682                         ci->mc_auth = LC_ID_NONE;
2683         }
2684
2685         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2686         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2687         info->mti_mos = NULL;
2688
2689         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2690         info->mti_body = NULL;
2691         info->mti_object = NULL;
2692         info->mti_dlm_req = NULL;
2693         info->mti_has_trans = 0;
2694         info->mti_no_need_trans = 0;
2695         info->mti_cross_ref = 0;
2696         info->mti_opdata = 0;
2697
2698         /* To not check for split by default. */
2699         info->mti_spec.sp_ck_split = 0;
2700         info->mti_spec.no_create = 0;
2701 }
2702
2703 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2704 {
2705         int i;
2706
2707         req_capsule_fini(info->mti_pill);
2708         if (info->mti_object != NULL) {
2709                 /*
2710                  * freeing an object may lead to OSD level transaction, do not
2711                  * let it mess with MDT. bz19385.
2712                  */
2713                 info->mti_no_need_trans = 1;
2714                 mdt_object_put(info->mti_env, info->mti_object);
2715                 info->mti_object = NULL;
2716         }
2717         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2718                 mdt_lock_handle_fini(&info->mti_lh[i]);
2719         info->mti_env = NULL;
2720 }
2721
2722 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2723                                        struct obd_device *obd, int *process)
2724 {
2725         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2726         case MDS_CONNECT: /* This will never get here, but for completeness. */
2727         case OST_CONNECT: /* This will never get here, but for completeness. */
2728         case MDS_DISCONNECT:
2729         case OST_DISCONNECT:
2730                *process = 1;
2731                RETURN(0);
2732
2733         case MDS_CLOSE:
2734         case MDS_DONE_WRITING:
2735         case MDS_SYNC: /* used in unmounting */
2736         case OBD_PING:
2737         case MDS_REINT:
2738         case SEQ_QUERY:
2739         case FLD_QUERY:
2740         case LDLM_ENQUEUE:
2741                 *process = target_queue_recovery_request(req, obd);
2742                 RETURN(0);
2743
2744         default:
2745                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2746                 *process = -EAGAIN;
2747                 RETURN(0);
2748         }
2749 }
2750
2751 /*
2752  * Handle recovery. Return:
2753  *        +1: continue request processing;
2754  *       -ve: abort immediately with the given error code;
2755  *         0: send reply with error code in req->rq_status;
2756  */
2757 static int mdt_recovery(struct mdt_thread_info *info)
2758 {
2759         struct ptlrpc_request *req = mdt_info_req(info);
2760         struct obd_device *obd;
2761
2762         ENTRY;
2763
2764         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2765         case MDS_CONNECT:
2766         case SEC_CTX_INIT:
2767         case SEC_CTX_INIT_CONT:
2768         case SEC_CTX_FINI:
2769                 {
2770 #if 0
2771                         int rc;
2772
2773                         rc = mdt_handle_idmap(info);
2774                         if (rc)
2775                                 RETURN(rc);
2776                         else
2777 #endif
2778                                 RETURN(+1);
2779                 }
2780         }
2781
2782         if (unlikely(!class_connected_export(req->rq_export))) {
2783                 CERROR("operation %d on unconnected MDS from %s\n",
2784                        lustre_msg_get_opc(req->rq_reqmsg),
2785                        libcfs_id2str(req->rq_peer));
2786                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2787                  * mds_A will get -ENOTCONN(especially for ping req),
2788                  * which will cause that mds_A deactive timeout, then when
2789                  * mds_A cleanup, the cleanup process will be suspended since
2790                  * deactive timeout is not zero.
2791                  */
2792                 req->rq_status = -ENOTCONN;
2793                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2794                 RETURN(0);
2795         }
2796
2797         /* sanity check: if the xid matches, the request must be marked as a
2798          * resent or replayed */
2799         if (req_xid_is_last(req)) {
2800                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2801                       (MSG_RESENT | MSG_REPLAY))) {
2802                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2803                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2804                                   lustre_msg_get_flags(req->rq_reqmsg));
2805                         LBUG();
2806                         req->rq_status = -ENOTCONN;
2807                         RETURN(-ENOTCONN);
2808                 }
2809         }
2810
2811         /* else: note the opposite is not always true; a RESENT req after a
2812          * failover will usually not match the last_xid, since it was likely
2813          * never committed. A REPLAYed request will almost never match the
2814          * last xid, however it could for a committed, but still retained,
2815          * open. */
2816
2817         obd = req->rq_export->exp_obd;
2818
2819         /* Check for aborted recovery... */
2820         if (unlikely(obd->obd_recovering)) {
2821                 int rc;
2822                 int should_process;
2823                 DEBUG_REQ(D_INFO, req, "Got new replay");
2824                 rc = mdt_filter_recovery_request(req, obd, &should_process);
2825                 if (rc != 0 || !should_process)
2826                         RETURN(rc);
2827                 else if (should_process < 0) {
2828                         req->rq_status = should_process;
2829                         rc = ptlrpc_error(req);
2830                         RETURN(rc);
2831                 }
2832         }
2833         RETURN(+1);
2834 }
2835
2836 static int mdt_msg_check_version(struct lustre_msg *msg)
2837 {
2838         int rc;
2839
2840         switch (lustre_msg_get_opc(msg)) {
2841         case MDS_CONNECT:
2842         case MDS_DISCONNECT:
2843         case OBD_PING:
2844         case SEC_CTX_INIT:
2845         case SEC_CTX_INIT_CONT:
2846         case SEC_CTX_FINI:
2847                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2848                 if (rc)
2849                         CERROR("bad opc %u version %08x, expecting %08x\n",
2850                                lustre_msg_get_opc(msg),
2851                                lustre_msg_get_version(msg),
2852                                LUSTRE_OBD_VERSION);
2853                 break;
2854         case MDS_GETSTATUS:
2855         case MDS_GETATTR:
2856         case MDS_GETATTR_NAME:
2857         case MDS_STATFS:
2858         case MDS_READPAGE:
2859         case MDS_WRITEPAGE:
2860         case MDS_IS_SUBDIR:
2861         case MDS_REINT:
2862         case MDS_CLOSE:
2863         case MDS_DONE_WRITING:
2864         case MDS_PIN:
2865         case MDS_SYNC:
2866         case MDS_GETXATTR:
2867         case MDS_SETXATTR:
2868         case MDS_SET_INFO:
2869         case MDS_GET_INFO:
2870         case MDS_QUOTACHECK:
2871         case MDS_QUOTACTL:
2872         case QUOTA_DQACQ:
2873         case QUOTA_DQREL:
2874         case SEQ_QUERY:
2875         case FLD_QUERY:
2876                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2877                 if (rc)
2878                         CERROR("bad opc %u version %08x, expecting %08x\n",
2879                                lustre_msg_get_opc(msg),
2880                                lustre_msg_get_version(msg),
2881                                LUSTRE_MDS_VERSION);
2882                 break;
2883         case LDLM_ENQUEUE:
2884         case LDLM_CONVERT:
2885         case LDLM_BL_CALLBACK:
2886         case LDLM_CP_CALLBACK:
2887                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2888                 if (rc)
2889                         CERROR("bad opc %u version %08x, expecting %08x\n",
2890                                lustre_msg_get_opc(msg),
2891                                lustre_msg_get_version(msg),
2892                                LUSTRE_DLM_VERSION);
2893                 break;
2894         case OBD_LOG_CANCEL:
2895         case LLOG_ORIGIN_HANDLE_CREATE:
2896         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2897         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2898         case LLOG_ORIGIN_HANDLE_CLOSE:
2899         case LLOG_ORIGIN_HANDLE_DESTROY:
2900         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2901         case LLOG_CATINFO:
2902                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2903                 if (rc)
2904                         CERROR("bad opc %u version %08x, expecting %08x\n",
2905                                lustre_msg_get_opc(msg),
2906                                lustre_msg_get_version(msg),
2907                                LUSTRE_LOG_VERSION);
2908                 break;
2909         default:
2910                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2911                 rc = -ENOTSUPP;
2912         }
2913         return rc;
2914 }
2915
2916 static int mdt_handle0(struct ptlrpc_request *req,
2917                        struct mdt_thread_info *info,
2918                        struct mdt_opc_slice *supported)
2919 {
2920         struct mdt_handler *h;
2921         struct lustre_msg  *msg;
2922         int                 rc;
2923
2924         ENTRY;
2925
2926         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2927                 RETURN(0);
2928
2929         LASSERT(current->journal_info == NULL);
2930
2931         msg = req->rq_reqmsg;
2932         rc = mdt_msg_check_version(msg);
2933         if (likely(rc == 0)) {
2934                 rc = mdt_recovery(info);
2935                 if (likely(rc == +1)) {
2936                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2937                                              supported);
2938                         if (likely(h != NULL)) {
2939                                 rc = mdt_req_handle(info, h, req);
2940                         } else {
2941                                 CERROR("The unsupported opc: 0x%x\n",
2942                                        lustre_msg_get_opc(msg) );
2943                                 req->rq_status = -ENOTSUPP;
2944                                 rc = ptlrpc_error(req);
2945                                 RETURN(rc);
2946                         }
2947                 }
2948         } else
2949                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2950         RETURN(rc);
2951 }
2952
2953 /*
2954  * MDT handler function called by ptlrpc service thread when request comes.
2955  *
2956  * XXX common "target" functionality should be factored into separate module
2957  * shared by mdt, ost and stand-alone services like fld.
2958  */
2959 static int mdt_handle_common(struct ptlrpc_request *req,
2960                              struct mdt_opc_slice *supported)
2961 {
2962         struct lu_env          *env;
2963         struct mdt_thread_info *info;
2964         int                     rc;
2965         ENTRY;
2966
2967         env = req->rq_svc_thread->t_env;
2968         LASSERT(env != NULL);
2969         LASSERT(env->le_ses != NULL);
2970         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2971         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2972         LASSERT(info != NULL);
2973
2974         mdt_thread_info_init(req, info);
2975
2976         rc = mdt_handle0(req, info, supported);
2977
2978         mdt_thread_info_fini(info);
2979         RETURN(rc);
2980 }
2981
2982 /*
2983  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2984  * as well.
2985  */
2986 int mdt_recovery_handle(struct ptlrpc_request *req)
2987 {
2988         int rc;
2989         ENTRY;
2990
2991         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2992         case FLD_QUERY:
2993                 rc = mdt_handle_common(req, mdt_fld_handlers);
2994                 break;
2995         case SEQ_QUERY:
2996                 rc = mdt_handle_common(req, mdt_seq_handlers);
2997                 break;
2998         default:
2999                 rc = mdt_handle_common(req, mdt_regular_handlers);
3000                 break;
3001         }
3002
3003         RETURN(rc);
3004 }
3005
3006 static int mdt_regular_handle(struct ptlrpc_request *req)
3007 {
3008         return mdt_handle_common(req, mdt_regular_handlers);
3009 }
3010
3011 static int mdt_readpage_handle(struct ptlrpc_request *req)
3012 {
3013         return mdt_handle_common(req, mdt_readpage_handlers);
3014 }
3015
3016 static int mdt_xmds_handle(struct ptlrpc_request *req)
3017 {
3018         return mdt_handle_common(req, mdt_xmds_handlers);
3019 }
3020
3021 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3022 {
3023         return mdt_handle_common(req, mdt_seq_handlers);
3024 }
3025
3026 static int mdt_mdss_handle(struct ptlrpc_request *req)
3027 {
3028         return mdt_handle_common(req, mdt_seq_handlers);
3029 }
3030
3031 static int mdt_dtss_handle(struct ptlrpc_request *req)
3032 {
3033         return mdt_handle_common(req, mdt_seq_handlers);
3034 }
3035
3036 static int mdt_fld_handle(struct ptlrpc_request *req)
3037 {
3038         return mdt_handle_common(req, mdt_fld_handlers);
3039 }
3040
3041 enum mdt_it_code {
3042         MDT_IT_OPEN,
3043         MDT_IT_OCREAT,
3044         MDT_IT_CREATE,
3045         MDT_IT_GETATTR,
3046         MDT_IT_READDIR,
3047         MDT_IT_LOOKUP,
3048         MDT_IT_UNLINK,
3049         MDT_IT_TRUNC,
3050         MDT_IT_GETXATTR,
3051         MDT_IT_NR
3052 };
3053
3054 static int mdt_intent_getattr(enum mdt_it_code opcode,
3055                               struct mdt_thread_info *info,
3056                               struct ldlm_lock **,
3057                               int);
3058 static int mdt_intent_reint(enum mdt_it_code opcode,
3059                             struct mdt_thread_info *info,
3060                             struct ldlm_lock **,
3061                             int);
3062
3063 static struct mdt_it_flavor {
3064         const struct req_format *it_fmt;
3065         __u32                    it_flags;
3066         int                    (*it_act)(enum mdt_it_code ,
3067                                          struct mdt_thread_info *,
3068                                          struct ldlm_lock **,
3069                                          int);
3070         long                     it_reint;
3071 } mdt_it_flavor[] = {
3072         [MDT_IT_OPEN]     = {
3073                 .it_fmt   = &RQF_LDLM_INTENT,
3074                 /*.it_flags = HABEO_REFERO,*/
3075                 .it_flags = 0,
3076                 .it_act   = mdt_intent_reint,
3077                 .it_reint = REINT_OPEN
3078         },
3079         [MDT_IT_OCREAT]   = {
3080                 .it_fmt   = &RQF_LDLM_INTENT,
3081                 .it_flags = MUTABOR,
3082                 .it_act   = mdt_intent_reint,
3083                 .it_reint = REINT_OPEN
3084         },
3085         [MDT_IT_CREATE]   = {
3086                 .it_fmt   = &RQF_LDLM_INTENT,
3087                 .it_flags = MUTABOR,
3088                 .it_act   = mdt_intent_reint,
3089                 .it_reint = REINT_CREATE
3090         },
3091         [MDT_IT_GETATTR]  = {
3092                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3093                 .it_flags = HABEO_REFERO,
3094                 .it_act   = mdt_intent_getattr
3095         },
3096         [MDT_IT_READDIR]  = {
3097                 .it_fmt   = NULL,
3098                 .it_flags = 0,
3099                 .it_act   = NULL
3100         },
3101         [MDT_IT_LOOKUP]   = {
3102                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3103                 .it_flags = HABEO_REFERO,
3104                 .it_act   = mdt_intent_getattr
3105         },
3106         [MDT_IT_UNLINK]   = {
3107                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3108                 .it_flags = MUTABOR,
3109                 .it_act   = NULL,
3110                 .it_reint = REINT_UNLINK
3111         },
3112         [MDT_IT_TRUNC]    = {
3113                 .it_fmt   = NULL,
3114                 .it_flags = MUTABOR,
3115                 .it_act   = NULL
3116         },
3117         [MDT_IT_GETXATTR] = {
3118                 .it_fmt   = NULL,
3119                 .it_flags = 0,
3120                 .it_act   = NULL
3121         }
3122 };
3123
3124 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3125                             struct ldlm_lock **lockp,
3126                             struct ldlm_lock *new_lock,
3127                             struct mdt_lock_handle *lh,
3128                             int flags)
3129 {
3130         struct ptlrpc_request  *req = mdt_info_req(info);
3131         struct ldlm_lock       *lock = *lockp;
3132
3133         /*
3134          * Get new lock only for cases when possible resent did not find any
3135          * lock.
3136          */
3137         if (new_lock == NULL)
3138                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3139
3140         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3141                 lh->mlh_reg_lh.cookie = 0;
3142                 RETURN(0);
3143         }
3144
3145         LASSERTF(new_lock != NULL,
3146                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3147
3148         /*
3149          * If we've already given this lock to a client once, then we should
3150          * have no readers or writers.  Otherwise, we should have one reader
3151          * _or_ writer ref (which will be zeroed below) before returning the
3152          * lock to a client.
3153          */
3154         if (new_lock->l_export == req->rq_export) {
3155                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3156         } else {
3157                 LASSERT(new_lock->l_export == NULL);
3158                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3159         }
3160
3161         *lockp = new_lock;
3162
3163         if (new_lock->l_export == req->rq_export) {
3164                 /*
3165                  * Already gave this to the client, which means that we
3166                  * reconstructed a reply.
3167                  */
3168                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3169                         MSG_RESENT);
3170                 lh->mlh_reg_lh.cookie = 0;
3171                 RETURN(ELDLM_LOCK_REPLACED);
3172         }
3173
3174         /*
3175          * Fixup the lock to be given to the client.
3176          */
3177         lock_res_and_lock(new_lock);
3178         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3179          * possible blocking AST. */
3180         while (new_lock->l_readers > 0) {
3181                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3182                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3183                 new_lock->l_readers--;
3184         }
3185         while (new_lock->l_writers > 0) {
3186                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3187                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3188                 new_lock->l_writers--;
3189         }
3190
3191         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3192         new_lock->l_blocking_ast = lock->l_blocking_ast;
3193         new_lock->l_completion_ast = lock->l_completion_ast;
3194         new_lock->l_remote_handle = lock->l_remote_handle;
3195         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3196
3197         unlock_res_and_lock(new_lock);
3198
3199         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3200                      &new_lock->l_remote_handle,
3201                      &new_lock->l_exp_hash);
3202
3203         LDLM_LOCK_RELEASE(new_lock);
3204         lh->mlh_reg_lh.cookie = 0;
3205
3206         RETURN(ELDLM_LOCK_REPLACED);
3207 }
3208
3209 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3210                                     struct ldlm_lock *new_lock,
3211                                     struct ldlm_lock **old_lock,
3212                                     struct mdt_lock_handle *lh)
3213 {
3214         struct ptlrpc_request  *req = mdt_info_req(info);
3215         struct obd_export      *exp = req->rq_export;
3216         struct lustre_handle    remote_hdl;
3217         struct ldlm_request    *dlmreq;
3218         struct ldlm_lock       *lock;
3219
3220         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3221                 return;
3222
3223         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3224         remote_hdl = dlmreq->lock_handle[0];
3225
3226         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3227         if (lock) {
3228                 if (lock != new_lock) {
3229                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3230                         lh->mlh_reg_mode = lock->l_granted_mode;
3231
3232                         LDLM_DEBUG(lock, "Restoring lock cookie");
3233                         DEBUG_REQ(D_DLMTRACE, req,
3234                                   "restoring lock cookie "LPX64,
3235                                   lh->mlh_reg_lh.cookie);
3236                         if (old_lock)
3237                                 *old_lock = LDLM_LOCK_GET(lock);
3238                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3239                         return;
3240                 }
3241
3242                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3243         }
3244
3245         /*
3246          * If the xid matches, then we know this is a resent request, and allow
3247          * it. (It's probably an OPEN, for which we don't send a lock.
3248          */
3249         if (req_xid_is_last(req))
3250                 return;
3251
3252         /*
3253          * This remote handle isn't enqueued, so we never received or processed
3254          * this request.  Clear MSG_RESENT, because it can be handled like any
3255          * normal request now.
3256          */
3257         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3258
3259         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3260                   remote_hdl.cookie);
3261 }
3262
3263 static int mdt_intent_getattr(enum mdt_it_code opcode,
3264                               struct mdt_thread_info *info,
3265                               struct ldlm_lock **lockp,
3266                               int flags)
3267 {
3268         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3269         struct ldlm_lock       *new_lock = NULL;
3270         __u64                   child_bits;
3271         struct ldlm_reply      *ldlm_rep;
3272         struct ptlrpc_request  *req;
3273         struct mdt_body        *reqbody;
3274         struct mdt_body        *repbody;
3275         int                     rc;
3276         ENTRY;
3277
3278         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3279         LASSERT(reqbody);
3280
3281         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3282         LASSERT(repbody);
3283
3284         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3285         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3286         repbody->eadatasize = 0;
3287         repbody->aclsize = 0;
3288
3289         switch (opcode) {
3290         case MDT_IT_LOOKUP:
3291                 child_bits = MDS_INODELOCK_LOOKUP;
3292                 break;
3293         case MDT_IT_GETATTR:
3294                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3295                 break;
3296         default:
3297                 CERROR("Unhandled till now");
3298                 GOTO(out_shrink, rc = -EINVAL);
3299         }
3300
3301         rc = mdt_init_ucred(info, reqbody);
3302         if (rc)
3303                 GOTO(out_shrink, rc);
3304
3305         req = info->mti_pill->rc_req;
3306         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3307         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3308
3309         /* Get lock from request for possible resent case. */
3310         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3311
3312         ldlm_rep->lock_policy_res2 =
3313                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3314
3315         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3316                 ldlm_rep->lock_policy_res2 = 0;
3317         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3318             ldlm_rep->lock_policy_res2) {
3319                 lhc->mlh_reg_lh.cookie = 0ull;
3320                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3321         }
3322
3323         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3324         EXIT;
3325 out_ucred:
3326         mdt_exit_ucred(info);
3327 out_shrink:
3328         mdt_shrink_reply(info);
3329         return rc;
3330 }
3331
3332 static int mdt_intent_reint(enum mdt_it_code opcode,
3333                             struct mdt_thread_info *info,
3334                             struct ldlm_lock **lockp,
3335                             int flags)
3336 {
3337         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3338         struct ldlm_reply      *rep = NULL;
3339         long                    opc;
3340         int                     rc;
3341
3342         static const struct req_format *intent_fmts[REINT_MAX] = {
3343                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3344                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3345         };
3346
3347         ENTRY;
3348
3349         opc = mdt_reint_opcode(info, intent_fmts);
3350         if (opc < 0)
3351                 RETURN(opc);
3352
3353         if (mdt_it_flavor[opcode].it_reint != opc) {
3354                 CERROR("Reint code %ld doesn't match intent: %d\n",
3355                        opc, opcode);
3356                 RETURN(err_serious(-EPROTO));
3357         }
3358
3359         /* Get lock from request for possible resent case. */
3360         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3361
3362         rc = mdt_reint_internal(info, lhc, opc);
3363
3364         /* Check whether the reply has been packed successfully. */
3365         if (mdt_info_req(info)->rq_repmsg != NULL)
3366                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3367         if (rep == NULL)
3368                 RETURN(err_serious(-EFAULT));
3369
3370         /* MDC expects this in any case */
3371         if (rc != 0)
3372                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3373
3374         /* Cross-ref case, the lock should be returned to the client */
3375         if (rc == -EREMOTE) {
3376                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3377                 rep->lock_policy_res2 = 0;
3378                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3379                 RETURN(rc);
3380         }
3381         rep->lock_policy_res2 = clear_serious(rc);
3382
3383         if (rep->lock_policy_res2 == -ENOENT &&
3384             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3385                 rep->lock_policy_res2 = 0;
3386
3387         if (rc == -ENOTCONN || rc == -ENODEV ||
3388             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3389                 /*
3390                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3391                  * will be returned by rq_status, and client at ptlrpc layer
3392                  * will detect this, then disconnect, reconnect the import
3393                  * immediately, instead of impacting the following the rpc.
3394                  */
3395                 lhc->mlh_reg_lh.cookie = 0ull;
3396                 RETURN(rc);
3397         } else {
3398                 /*
3399                  * For other cases, the error will be returned by intent.
3400                  * and client will retrieve the result from intent.
3401                  */
3402                  /*
3403                   * FIXME: when open lock is finished, that should be
3404                   * checked here.
3405                   */
3406                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3407                         LASSERTF(rc == 0, "Error occurred but lock handle "
3408                                  "is still in use\n");
3409                         rep->lock_policy_res2 = 0;
3410                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3411                         RETURN(rc);
3412                 } else {
3413                         lhc->mlh_reg_lh.cookie = 0ull;
3414                         RETURN(ELDLM_LOCK_ABORTED);
3415                 }
3416         }
3417 }
3418
3419 static int mdt_intent_code(long itcode)
3420 {
3421         int rc;
3422
3423         switch(itcode) {
3424         case IT_OPEN:
3425                 rc = MDT_IT_OPEN;
3426                 break;
3427         case IT_OPEN|IT_CREAT:
3428                 rc = MDT_IT_OCREAT;
3429                 break;
3430         case IT_CREAT:
3431                 rc = MDT_IT_CREATE;
3432                 break;
3433         case IT_READDIR:
3434                 rc = MDT_IT_READDIR;
3435                 break;
3436         case IT_GETATTR:
3437                 rc = MDT_IT_GETATTR;
3438                 break;
3439         case IT_LOOKUP:
3440                 rc = MDT_IT_LOOKUP;
3441                 break;
3442         case IT_UNLINK:
3443                 rc = MDT_IT_UNLINK;
3444                 break;
3445         case IT_TRUNC:
3446                 rc = MDT_IT_TRUNC;
3447                 break;
3448         case IT_GETXATTR:
3449                 rc = MDT_IT_GETXATTR;
3450                 break;
3451         default:
3452                 CERROR("Unknown intent opcode: %ld\n", itcode);
3453                 rc = -EINVAL;
3454                 break;
3455         }
3456         return rc;
3457 }
3458
3459 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3460                           struct ldlm_lock **lockp, int flags)
3461 {
3462         struct req_capsule   *pill;
3463         struct mdt_it_flavor *flv;
3464         int opc;
3465         int rc;
3466         ENTRY;
3467
3468         opc = mdt_intent_code(itopc);
3469         if (opc < 0)
3470                 RETURN(-EINVAL);
3471
3472         pill = info->mti_pill;
3473         flv  = &mdt_it_flavor[opc];
3474
3475         if (flv->it_fmt != NULL)
3476                 req_capsule_extend(pill, flv->it_fmt);
3477
3478         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3479         if (rc == 0) {
3480                 struct ptlrpc_request *req = mdt_info_req(info);
3481                 if (flv->it_flags & MUTABOR &&
3482                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3483                         RETURN(-EROFS);
3484         }
3485         if (rc == 0 && flv->it_act != NULL) {
3486                 /* execute policy */
3487                 rc = flv->it_act(opc, info, lockp, flags);
3488         } else {
3489                 rc = -EOPNOTSUPP;
3490         }
3491         RETURN(rc);
3492 }
3493
3494 static int mdt_intent_policy(struct ldlm_namespace *ns,
3495                              struct ldlm_lock **lockp, void *req_cookie,
3496                              ldlm_mode_t mode, int flags, void *data)
3497 {
3498         struct mdt_thread_info *info;
3499         struct ptlrpc_request  *req  =  req_cookie;
3500         struct ldlm_intent     *it;
3501         struct req_capsule     *pill;
3502         int rc;
3503
3504         ENTRY;
3505
3506         LASSERT(req != NULL);
3507
3508         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3509                                   &mdt_thread_key);
3510         LASSERT(info != NULL);
3511         pill = info->mti_pill;
3512         LASSERT(pill->rc_req == req);
3513
3514         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3515                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3516                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3517                 if (it != NULL) {
3518                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3519                         if (rc == 0)
3520                                 rc = ELDLM_OK;
3521
3522                         /* Lock without inodebits makes no sense and will oops
3523                          * later in ldlm. Let's check it now to see if we have
3524                          * ibits corrupted somewhere in mdt_intent_opc().
3525                          * The case for client miss to set ibits has been
3526                          * processed by others. */
3527                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3528                                         lr_type == LDLM_IBITS,
3529                                      info->mti_dlm_req->lock_desc.\
3530                                         l_policy_data.l_inodebits.bits != 0));
3531                 } else
3532                         rc = err_serious(-EFAULT);
3533         } else {
3534                 /* No intent was provided */
3535                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3536                 rc = req_capsule_server_pack(pill);
3537                 if (rc)
3538                         rc = err_serious(rc);
3539         }
3540         RETURN(rc);
3541 }
3542
3543 static int mdt_seq_fini(const struct lu_env *env,
3544                         struct mdt_device *m)
3545 {
3546         struct md_site *ms = mdt_md_site(m);
3547         ENTRY;
3548
3549         if (ms != NULL) {
3550                 if (ms->ms_server_seq) {
3551                         seq_server_fini(ms->ms_server_seq, env);
3552                         OBD_FREE_PTR(ms->ms_server_seq);
3553                         ms->ms_server_seq = NULL;
3554         }
3555
3556                 if (ms->ms_control_seq) {
3557                         seq_server_fini(ms->ms_control_seq, env);
3558                         OBD_FREE_PTR(ms->ms_control_seq);
3559                         ms->ms_control_seq = NULL;
3560         }
3561
3562                 if (ms->ms_client_seq) {
3563                         seq_client_fini(ms->ms_client_seq);
3564                         OBD_FREE_PTR(ms->ms_client_seq);
3565                         ms->ms_client_seq = NULL;
3566                 }
3567         }
3568
3569         RETURN(0);
3570 }
3571
3572 static int mdt_seq_init(const struct lu_env *env,
3573                         const char *uuid,
3574                         struct mdt_device *m)
3575 {
3576         struct md_site *ms;
3577         char *prefix;
3578         int rc;
3579         ENTRY;
3580
3581         ms = mdt_md_site(m);
3582
3583         /*
3584          * This is sequence-controller node. Init seq-controller server on local
3585          * MDT.
3586          */
3587         if (ms->ms_node_id == 0) {
3588                 LASSERT(ms->ms_control_seq == NULL);
3589
3590                 OBD_ALLOC_PTR(ms->ms_control_seq);
3591                 if (ms->ms_control_seq == NULL)
3592                         RETURN(-ENOMEM);
3593
3594                 rc = seq_server_init(ms->ms_control_seq,
3595                                      m->mdt_bottom, uuid,
3596                                      LUSTRE_SEQ_CONTROLLER,
3597                                      ms,
3598                                      env);
3599
3600                 if (rc)
3601                         GOTO(out_seq_fini, rc);
3602
3603                 OBD_ALLOC_PTR(ms->ms_client_seq);
3604                 if (ms->ms_client_seq == NULL)
3605                         GOTO(out_seq_fini, rc = -ENOMEM);
3606
3607                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3608                 if (prefix == NULL) {
3609                         OBD_FREE_PTR(ms->ms_client_seq);
3610                         GOTO(out_seq_fini, rc = -ENOMEM);
3611                 }
3612
3613                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3614                          uuid);
3615
3616                 /*
3617                  * Init seq-controller client after seq-controller server is
3618                  * ready. Pass ms->ms_control_seq to it for direct talking.
3619                  */
3620                 rc = seq_client_init(ms->ms_client_seq, NULL,
3621                                      LUSTRE_SEQ_METADATA, prefix,
3622                                      ms->ms_control_seq);
3623                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3624
3625                 if (rc)
3626                         GOTO(out_seq_fini, rc);
3627         }
3628
3629         /* Init seq-server on local MDT */
3630         LASSERT(ms->ms_server_seq == NULL);
3631
3632         OBD_ALLOC_PTR(ms->ms_server_seq);
3633         if (ms->ms_server_seq == NULL)
3634                 GOTO(out_seq_fini, rc = -ENOMEM);
3635
3636         rc = seq_server_init(ms->ms_server_seq,
3637                              m->mdt_bottom, uuid,
3638                              LUSTRE_SEQ_SERVER,
3639                              ms,
3640                              env);
3641         if (rc)
3642                 GOTO(out_seq_fini, rc = -ENOMEM);
3643
3644         /* Assign seq-controller client to local seq-server. */
3645         if (ms->ms_node_id == 0) {
3646                 LASSERT(ms->ms_client_seq != NULL);
3647
3648                 rc = seq_server_set_cli(ms->ms_server_seq,
3649                                         ms->ms_client_seq,
3650                                         env);
3651         }
3652
3653         EXIT;
3654 out_seq_fini:
3655         if (rc)
3656                 mdt_seq_fini(env, m);
3657
3658         return rc;
3659 }
3660 /*
3661  * Init client sequence manager which is used by local MDS to talk to sequence
3662  * controller on remote node.
3663  */
3664 static int mdt_seq_init_cli(const struct lu_env *env,
3665                             struct mdt_device *m,
3666                             struct lustre_cfg *cfg)
3667 {
3668         struct md_site    *ms = mdt_md_site(m);
3669         struct obd_device *mdc;
3670         struct obd_uuid   *uuidp, *mdcuuidp;
3671         char              *uuid_str, *mdc_uuid_str;
3672         int                rc;
3673         int                index;
3674         struct mdt_thread_info *info;
3675         char *p, *index_string = lustre_cfg_string(cfg, 2);
3676         ENTRY;
3677
3678         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3679         uuidp = &info->mti_u.uuid[0];
3680         mdcuuidp = &info->mti_u.uuid[1];
3681
3682         LASSERT(index_string);
3683
3684         index = simple_strtol(index_string, &p, 10);
3685         if (*p) {
3686                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3687                 RETURN(-EINVAL);
3688         }
3689
3690         /* check if this is adding the first MDC and controller is not yet
3691          * initialized. */
3692         if (index != 0 || ms->ms_client_seq)
3693                 RETURN(0);
3694
3695         uuid_str = lustre_cfg_string(cfg, 1);
3696         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3697         obd_str2uuid(uuidp, uuid_str);
3698         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3699
3700         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3701         if (!mdc) {
3702                 CERROR("can't find controller MDC by uuid %s\n",
3703                        uuid_str);
3704                 rc = -ENOENT;
3705         } else if (!mdc->obd_set_up) {
3706                 CERROR("target %s not set up\n", mdc->obd_name);
3707                 rc = -EINVAL;
3708         } else {
3709                 LASSERT(ms->ms_control_exp);
3710                 OBD_ALLOC_PTR(ms->ms_client_seq);
3711                 if (ms->ms_client_seq != NULL) {
3712                         char *prefix;
3713
3714                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3715                         if (!prefix)
3716                                 RETURN(-ENOMEM);
3717
3718                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3719                                  mdc->obd_name);
3720
3721                         rc = seq_client_init(ms->ms_client_seq,
3722                                              ms->ms_control_exp,
3723                                              LUSTRE_SEQ_METADATA,
3724                                              prefix, NULL);
3725                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3726                 } else
3727                         rc = -ENOMEM;
3728
3729                 if (rc)
3730                         RETURN(rc);
3731
3732                 LASSERT(ms->ms_server_seq != NULL);
3733                 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3734                                         env);
3735         }
3736
3737         RETURN(rc);
3738 }
3739
3740 static void mdt_seq_fini_cli(struct mdt_device *m)
3741 {
3742         struct md_site *ms;
3743
3744         ENTRY;
3745
3746         ms = mdt_md_site(m);
3747
3748         if (ms != NULL) {
3749                 if (ms->ms_server_seq)
3750