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