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