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