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