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