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