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