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