Whamcloud - gitweb
LU-2739 mdt: Deny non-DNE client to access remote directory
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2013, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <linux/module.h>
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  */
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
61 #include <obd.h>
62 /* lu2dt_dev() */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_mdt.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lustre_quota.h>
71
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73         [LCK_MINMODE] = MDL_MINMODE,
74         [LCK_EX]      = MDL_EX,
75         [LCK_PW]      = MDL_PW,
76         [LCK_PR]      = MDL_PR,
77         [LCK_CW]      = MDL_CW,
78         [LCK_CR]      = MDL_CR,
79         [LCK_NL]      = MDL_NL,
80         [LCK_GROUP]   = MDL_GROUP
81 };
82
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84         [MDL_MINMODE] = LCK_MINMODE,
85         [MDL_EX]      = LCK_EX,
86         [MDL_PW]      = LCK_PW,
87         [MDL_PR]      = LCK_PR,
88         [MDL_CW]      = LCK_CW,
89         [MDL_CR]      = LCK_CR,
90         [MDL_NL]      = LCK_NL,
91         [MDL_GROUP]   = LCK_GROUP
92 };
93
94 static struct mdt_device *mdt_dev(struct lu_device *d);
95 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
96 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
97                         struct getinfo_fid2path *fp);
98
99 static const struct lu_object_operations mdt_obj_ops;
100
101 /* Slab for MDT object allocation */
102 static cfs_mem_cache_t *mdt_object_kmem;
103
104 static struct lu_kmem_descr mdt_caches[] = {
105         {
106                 .ckd_cache = &mdt_object_kmem,
107                 .ckd_name  = "mdt_obj",
108                 .ckd_size  = sizeof(struct mdt_object)
109         },
110         {
111                 .ckd_cache = NULL
112         }
113 };
114
115 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
116 {
117         if (!rep)
118                 return 0;
119         return (rep->lock_policy_res1 & flag);
120 }
121
122 void mdt_clear_disposition(struct mdt_thread_info *info,
123                            struct ldlm_reply *rep, int flag)
124 {
125         if (info)
126                 info->mti_opdata &= ~flag;
127         if (rep)
128                 rep->lock_policy_res1 &= ~flag;
129 }
130
131 void mdt_set_disposition(struct mdt_thread_info *info,
132                          struct ldlm_reply *rep, int flag)
133 {
134         if (info)
135                 info->mti_opdata |= flag;
136         if (rep)
137                 rep->lock_policy_res1 |= flag;
138 }
139
140 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
141 {
142         lh->mlh_pdo_hash = 0;
143         lh->mlh_reg_mode = lm;
144         lh->mlh_rreg_mode = lm;
145         lh->mlh_type = MDT_REG_LOCK;
146 }
147
148 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
149                        const char *name, int namelen)
150 {
151         lh->mlh_reg_mode = lm;
152         lh->mlh_rreg_mode = lm;
153         lh->mlh_type = MDT_PDO_LOCK;
154
155         if (name != NULL && (name[0] != '\0')) {
156                 LASSERT(namelen > 0);
157                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
158         } else {
159                 LASSERT(namelen == 0);
160                 lh->mlh_pdo_hash = 0ull;
161         }
162 }
163
164 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
165                               struct mdt_lock_handle *lh)
166 {
167         mdl_mode_t mode;
168         ENTRY;
169
170         /*
171          * Any dir access needs couple of locks:
172          *
173          * 1) on part of dir we gonna take lookup/modify;
174          *
175          * 2) on whole dir to protect it from concurrent splitting and/or to
176          * flush client's cache for readdir().
177          *
178          * so, for a given mode and object this routine decides what lock mode
179          * to use for lock #2:
180          *
181          * 1) if caller's gonna lookup in dir then we need to protect dir from
182          * being splitted only - LCK_CR
183          *
184          * 2) if caller's gonna modify dir then we need to protect dir from
185          * being splitted and to flush cache - LCK_CW
186          *
187          * 3) if caller's gonna modify dir and that dir seems ready for
188          * splitting then we need to protect it from any type of access
189          * (lookup/modify/split) - LCK_EX --bzzz
190          */
191
192         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
193         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
194
195         /*
196          * Ask underlaying level its opinion about preferable PDO lock mode
197          * having access type passed as regular lock mode:
198          *
199          * - MDL_MINMODE means that lower layer does not want to specify lock
200          * mode;
201          *
202          * - MDL_NL means that no PDO lock should be taken. This is used in some
203          * cases. Say, for non-splittable directories no need to use PDO locks
204          * at all.
205          */
206         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
207                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
208
209         if (mode != MDL_MINMODE) {
210                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
211         } else {
212                 /*
213                  * Lower layer does not want to specify locking mode. We do it
214                  * our selves. No special protection is needed, just flush
215                  * client's cache on modification and allow concurrent
216                  * mondification.
217                  */
218                 switch (lh->mlh_reg_mode) {
219                 case LCK_EX:
220                         lh->mlh_pdo_mode = LCK_EX;
221                         break;
222                 case LCK_PR:
223                         lh->mlh_pdo_mode = LCK_CR;
224                         break;
225                 case LCK_PW:
226                         lh->mlh_pdo_mode = LCK_CW;
227                         break;
228                 default:
229                         CERROR("Not expected lock type (0x%x)\n",
230                                (int)lh->mlh_reg_mode);
231                         LBUG();
232                 }
233         }
234
235         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
236         EXIT;
237 }
238
239 int mdt_getstatus(struct mdt_thread_info *info)
240 {
241         struct mdt_device *mdt  = info->mti_mdt;
242         struct md_device  *next = mdt->mdt_child;
243         struct mdt_body   *repbody;
244         int                rc;
245
246         ENTRY;
247
248         rc = mdt_check_ucred(info);
249         if (rc)
250                 RETURN(err_serious(rc));
251
252         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
253                 RETURN(err_serious(-ENOMEM));
254
255         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
256         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
257         if (rc != 0)
258                 RETURN(rc);
259
260         repbody->valid |= OBD_MD_FLID;
261
262         if (mdt->mdt_opts.mo_mds_capa &&
263             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
264                 struct mdt_object  *root;
265                 struct lustre_capa *capa;
266
267                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
268                 if (IS_ERR(root))
269                         RETURN(PTR_ERR(root));
270
271                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
272                 LASSERT(capa);
273                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
274                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
275                                  0);
276                 mdt_object_put(info->mti_env, root);
277                 if (rc == 0)
278                         repbody->valid |= OBD_MD_FLMDSCAPA;
279         }
280
281         RETURN(rc);
282 }
283
284 int mdt_statfs(struct mdt_thread_info *info)
285 {
286         struct ptlrpc_request           *req = mdt_info_req(info);
287         struct md_device                *next = info->mti_mdt->mdt_child;
288         struct ptlrpc_service_part      *svcpt;
289         struct obd_statfs               *osfs;
290         int                             rc;
291
292         ENTRY;
293
294         svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
295
296         /* This will trigger a watchdog timeout */
297         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
298                          (MDT_SERVICE_WATCHDOG_FACTOR *
299                           at_get(&svcpt->scp_at_estimate)) + 1);
300
301         rc = mdt_check_ucred(info);
302         if (rc)
303                 RETURN(err_serious(rc));
304
305         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
306                 RETURN(err_serious(-ENOMEM));
307
308         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
309         if (!osfs)
310                 RETURN(-EPROTO);
311
312         /** statfs information are cached in the mdt_device */
313         if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
314                                cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
315                 /** statfs data is too old, get up-to-date one */
316                 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
317                 if (rc)
318                         RETURN(rc);
319                 spin_lock(&info->mti_mdt->mdt_osfs_lock);
320                 info->mti_mdt->mdt_osfs = *osfs;
321                 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
322                 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
323         } else {
324                 /** use cached statfs data */
325                 spin_lock(&info->mti_mdt->mdt_osfs_lock);
326                 *osfs = info->mti_mdt->mdt_osfs;
327                 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
328         }
329
330         if (rc == 0)
331                 mdt_counter_incr(req, LPROC_MDT_STATFS);
332
333         RETURN(rc);
334 }
335
336 /**
337  * Pack SOM attributes into the reply.
338  * Call under a DLM UPDATE lock.
339  */
340 static void mdt_pack_size2body(struct mdt_thread_info *info,
341                                struct mdt_object *mo)
342 {
343         struct mdt_body *b;
344         struct md_attr *ma = &info->mti_attr;
345
346         LASSERT(ma->ma_attr.la_valid & LA_MODE);
347         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
348
349         /* Check if Size-on-MDS is supported, if this is a regular file,
350          * if SOM is enabled on the object and if SOM cache exists and valid.
351          * Otherwise do not pack Size-on-MDS attributes to the reply. */
352         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
353             !S_ISREG(ma->ma_attr.la_mode) ||
354             !mdt_object_is_som_enabled(mo) ||
355             !(ma->ma_valid & MA_SOM))
356                 return;
357
358         b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
359         b->size = ma->ma_som->msd_size;
360         b->blocks = ma->ma_som->msd_blocks;
361 }
362
363 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
364                         const struct lu_attr *attr, const struct lu_fid *fid)
365 {
366         struct md_attr *ma = &info->mti_attr;
367
368         LASSERT(ma->ma_valid & MA_INODE);
369
370         b->atime      = attr->la_atime;
371         b->mtime      = attr->la_mtime;
372         b->ctime      = attr->la_ctime;
373         b->mode       = attr->la_mode;
374         b->size       = attr->la_size;
375         b->blocks     = attr->la_blocks;
376         b->uid        = attr->la_uid;
377         b->gid        = attr->la_gid;
378         b->flags      = attr->la_flags;
379         b->nlink      = attr->la_nlink;
380         b->rdev       = attr->la_rdev;
381
382         /*XXX should pack the reply body according to lu_valid*/
383         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
384                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
385                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
386                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
387
388         if (!S_ISREG(attr->la_mode)) {
389                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
390         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
391                 /* means no objects are allocated on osts. */
392                 LASSERT(!(ma->ma_valid & MA_LOV));
393                 /* just ignore blocks occupied by extend attributes on MDS */
394                 b->blocks = 0;
395                 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
396                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
397         }
398
399         if (fid) {
400                 b->fid1 = *fid;
401                 b->valid |= OBD_MD_FLID;
402                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
403                                 PFID(fid), b->nlink, b->mode, b->size);
404         }
405
406         if (info)
407                 mdt_body_reverse_idmap(info, b);
408
409         if (b->valid & OBD_MD_FLSIZE)
410                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
411                        PFID(fid), (unsigned long long)b->size);
412 }
413
414 static inline int mdt_body_has_lov(const struct lu_attr *la,
415                                    const struct mdt_body *body)
416 {
417         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
418                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
419 }
420
421 void mdt_client_compatibility(struct mdt_thread_info *info)
422 {
423         struct mdt_body       *body;
424         struct ptlrpc_request *req = mdt_info_req(info);
425         struct obd_export     *exp = req->rq_export;
426         struct md_attr        *ma = &info->mti_attr;
427         struct lu_attr        *la = &ma->ma_attr;
428         ENTRY;
429
430         if (exp_connect_layout(exp))
431                 /* the client can deal with 16-bit lmm_stripe_count */
432                 RETURN_EXIT;
433
434         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
435
436         if (!mdt_body_has_lov(la, body))
437                 RETURN_EXIT;
438
439         /* now we have a reply with a lov for a client not compatible with the
440          * layout lock so we have to clean the layout generation number */
441         if (S_ISREG(la->la_mode))
442                 ma->ma_lmm->lmm_layout_gen = 0;
443         EXIT;
444 }
445
446 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
447                              char *name)
448 {
449         const struct lu_env *env = info->mti_env;
450         int rc;
451         ENTRY;
452
453         LASSERT(info->mti_big_lmm_used == 0);
454         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
455         if (rc < 0)
456                 RETURN(rc);
457
458         /* big_lmm may need to be grown */
459         if (info->mti_big_lmmsize < rc) {
460                 int size = size_roundup_power2(rc);
461
462                 if (info->mti_big_lmmsize > 0) {
463                         /* free old buffer */
464                         LASSERT(info->mti_big_lmm);
465                         OBD_FREE_LARGE(info->mti_big_lmm,
466                                        info->mti_big_lmmsize);
467                         info->mti_big_lmm = NULL;
468                         info->mti_big_lmmsize = 0;
469                 }
470
471                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
472                 if (info->mti_big_lmm == NULL)
473                         RETURN(-ENOMEM);
474                 info->mti_big_lmmsize = size;
475         }
476         LASSERT(info->mti_big_lmmsize >= rc);
477
478         info->mti_buf.lb_buf = info->mti_big_lmm;
479         info->mti_buf.lb_len = info->mti_big_lmmsize;
480         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
481
482         RETURN(rc);
483 }
484
485 int mdt_attr_get_lov(struct mdt_thread_info *info,
486                      struct mdt_object *o, struct md_attr *ma)
487 {
488         struct md_object *next = mdt_object_child(o);
489         struct lu_buf    *buf = &info->mti_buf;
490         int rc;
491
492         buf->lb_buf = ma->ma_lmm;
493         buf->lb_len = ma->ma_lmm_size;
494         rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
495         if (rc > 0) {
496                 ma->ma_lmm_size = rc;
497                 ma->ma_valid |= MA_LOV;
498                 rc = 0;
499         } else if (rc == -ENODATA) {
500                 /* no LOV EA */
501                 rc = 0;
502         } else if (rc == -ERANGE) {
503                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
504                 if (rc > 0) {
505                         info->mti_big_lmm_used = 1;
506                         ma->ma_valid |= MA_LOV;
507                         ma->ma_lmm = info->mti_big_lmm;
508                         ma->ma_lmm_size = rc;
509                         /* update mdt_max_mdsize so all clients
510                          * will be aware about that */
511                         if (info->mti_mdt->mdt_max_mdsize < rc)
512                                 info->mti_mdt->mdt_max_mdsize = rc;
513                         rc = 0;
514                 }
515         }
516
517         return rc;
518 }
519
520 int mdt_attr_get_pfid(struct mdt_thread_info *info,
521                       struct mdt_object *o, struct lu_fid *pfid)
522 {
523         struct lu_buf           *buf = &info->mti_buf;
524         struct link_ea_header   *leh;
525         struct link_ea_entry    *lee;
526         int                      rc;
527         ENTRY;
528
529         buf->lb_buf = info->mti_big_lmm;
530         buf->lb_len = info->mti_big_lmmsize;
531         rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
532                           buf, XATTR_NAME_LINK);
533         /* ignore errors, MA_PFID won't be set and it is
534          * up to the caller to treat this as an error */
535         if (rc == -ERANGE || buf->lb_len == 0) {
536                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
537                 buf->lb_buf = info->mti_big_lmm;
538                 buf->lb_len = info->mti_big_lmmsize;
539         }
540
541         if (rc < 0)
542                 RETURN(rc);
543         if (rc < sizeof(*leh)) {
544                 CERROR("short LinkEA on "DFID": rc = %d\n",
545                        PFID(mdt_object_fid(o)), rc);
546                 RETURN(-ENODATA);
547         }
548
549         leh = (struct link_ea_header *) buf->lb_buf;
550         lee = (struct link_ea_entry *)(leh + 1);
551         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
552                 leh->leh_magic = LINK_EA_MAGIC;
553                 leh->leh_reccount = __swab32(leh->leh_reccount);
554                 leh->leh_len = __swab64(leh->leh_len);
555         }
556         if (leh->leh_magic != LINK_EA_MAGIC)
557                 RETURN(-EINVAL);
558         if (leh->leh_reccount == 0)
559                 RETURN(-ENODATA);
560
561         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
562         fid_be_to_cpu(pfid, pfid);
563
564         RETURN(0);
565 }
566
567 int mdt_attr_get_complex(struct mdt_thread_info *info,
568                          struct mdt_object *o, struct md_attr *ma)
569 {
570         const struct lu_env *env = info->mti_env;
571         struct md_object    *next = mdt_object_child(o);
572         struct lu_buf       *buf = &info->mti_buf;
573         u32                  mode = lu_object_attr(&next->mo_lu);
574         int                  need = ma->ma_need;
575         int                  rc = 0, rc2;
576         ENTRY;
577
578         ma->ma_valid = 0;
579
580         if (need & MA_INODE) {
581                 ma->ma_need = MA_INODE;
582                 rc = mo_attr_get(env, next, ma);
583                 if (rc)
584                         GOTO(out, rc);
585                 ma->ma_valid |= MA_INODE;
586         }
587
588         if (need & MA_PFID) {
589                 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
590                 if (rc == 0)
591                         ma->ma_valid |= MA_PFID;
592                 /* ignore this error, parent fid is not mandatory */
593                 rc = 0;
594         }
595
596         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
597                 rc = mdt_attr_get_lov(info, o, ma);
598                 if (rc)
599                         GOTO(out, rc);
600         }
601
602         if (need & MA_LMV && S_ISDIR(mode)) {
603                 buf->lb_buf = ma->ma_lmv;
604                 buf->lb_len = ma->ma_lmv_size;
605                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
606                 if (rc2 > 0) {
607                         ma->ma_lmv_size = rc2;
608                         ma->ma_valid |= MA_LMV;
609                 } else if (rc2 == -ENODATA) {
610                         /* no LMV EA */
611                         ma->ma_lmv_size = 0;
612                 } else
613                         GOTO(out, rc = rc2);
614         }
615
616         if (need & MA_SOM && S_ISREG(mode)) {
617                 buf->lb_buf = info->mti_xattr_buf;
618                 buf->lb_len = sizeof(info->mti_xattr_buf);
619                 CLASSERT(sizeof(struct som_attrs) <=
620                          sizeof(info->mti_xattr_buf));
621                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
622                 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
623                 if (rc2 == 0)
624                         ma->ma_valid |= MA_SOM;
625                 else if (rc2 < 0 && rc2 != -ENODATA)
626                         GOTO(out, rc = rc2);
627         }
628
629         if (need & MA_HSM && S_ISREG(mode)) {
630                 buf->lb_buf = info->mti_xattr_buf;
631                 buf->lb_len = sizeof(info->mti_xattr_buf);
632                 CLASSERT(sizeof(struct hsm_attrs) <=
633                          sizeof(info->mti_xattr_buf));
634                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
635                 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
636                 if (rc2 == 0)
637                         ma->ma_valid |= MA_HSM;
638                 else if (rc2 < 0 && rc2 != -ENODATA)
639                         GOTO(out, rc = rc2);
640         }
641
642 #ifdef CONFIG_FS_POSIX_ACL
643         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
644                 buf->lb_buf = ma->ma_acl;
645                 buf->lb_len = ma->ma_acl_size;
646                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
647                 if (rc2 > 0) {
648                         ma->ma_acl_size = rc2;
649                         ma->ma_valid |= MA_ACL_DEF;
650                 } else if (rc2 == -ENODATA) {
651                         /* no ACLs */
652                         ma->ma_acl_size = 0;
653                 } else
654                         GOTO(out, rc = rc2);
655         }
656 #endif
657 out:
658         ma->ma_need = need;
659         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
660                rc, ma->ma_valid, ma->ma_lmm);
661         RETURN(rc);
662 }
663
664 static int mdt_getattr_internal(struct mdt_thread_info *info,
665                                 struct mdt_object *o, int ma_need)
666 {
667         struct md_object        *next = mdt_object_child(o);
668         const struct mdt_body   *reqbody = info->mti_body;
669         struct ptlrpc_request   *req = mdt_info_req(info);
670         struct md_attr          *ma = &info->mti_attr;
671         struct lu_attr          *la = &ma->ma_attr;
672         struct req_capsule      *pill = info->mti_pill;
673         const struct lu_env     *env = info->mti_env;
674         struct mdt_body         *repbody;
675         struct lu_buf           *buffer = &info->mti_buf;
676         int                     rc;
677         int                     is_root;
678         ENTRY;
679
680         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
681                 RETURN(err_serious(-ENOMEM));
682
683         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
684
685         ma->ma_valid = 0;
686
687         if (mdt_object_remote(o)) {
688                 /* This object is located on remote node.*/
689                 /* Return -EIO for old client */
690                 if (!mdt_is_dne_client(req->rq_export))
691                         GOTO(out, rc = -EIO);
692
693                 repbody->fid1 = *mdt_object_fid(o);
694                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
695                 GOTO(out, rc = 0);
696         }
697
698         buffer->lb_len = reqbody->eadatasize;
699         if (buffer->lb_len > 0)
700                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
701         else
702                 buffer->lb_buf = NULL;
703
704         /* If it is dir object and client require MEA, then we got MEA */
705         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
706             reqbody->valid & OBD_MD_MEA) {
707                 /* Assumption: MDT_MD size is enough for lmv size. */
708                 ma->ma_lmv = buffer->lb_buf;
709                 ma->ma_lmv_size = buffer->lb_len;
710                 ma->ma_need = MA_LMV | MA_INODE;
711         } else {
712                 ma->ma_lmm = buffer->lb_buf;
713                 ma->ma_lmm_size = buffer->lb_len;
714                 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
715         }
716
717         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
718             reqbody->valid & OBD_MD_FLDIREA  &&
719             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
720                 /* get default stripe info for this dir. */
721                 ma->ma_need |= MA_LOV_DEF;
722         }
723         ma->ma_need |= ma_need;
724         if (ma->ma_need & MA_SOM)
725                 ma->ma_som = &info->mti_u.som.data;
726
727         rc = mdt_attr_get_complex(info, o, ma);
728         if (unlikely(rc)) {
729                 CERROR("getattr error for "DFID": %d\n",
730                         PFID(mdt_object_fid(o)), rc);
731                 RETURN(rc);
732         }
733
734         is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
735
736         /* the Lustre protocol supposes to return default striping
737          * on the user-visible root if explicitly requested */
738         if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
739             (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
740                 struct lu_fid      rootfid;
741                 struct mdt_object *root;
742                 struct mdt_device *mdt = info->mti_mdt;
743
744                 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
745                 if (rc)
746                         RETURN(rc);
747                 root = mdt_object_find(env, mdt, &rootfid);
748                 if (IS_ERR(root))
749                         RETURN(PTR_ERR(root));
750                 rc = mdt_attr_get_lov(info, root, ma);
751                 mdt_object_put(info->mti_env, root);
752                 if (unlikely(rc)) {
753                         CERROR("getattr error for "DFID": %d\n",
754                                         PFID(mdt_object_fid(o)), rc);
755                         RETURN(rc);
756                 }
757         }
758
759         if (likely(ma->ma_valid & MA_INODE))
760                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
761         else
762                 RETURN(-EFAULT);
763
764         if (mdt_body_has_lov(la, reqbody)) {
765                 if (ma->ma_valid & MA_LOV) {
766                         LASSERT(ma->ma_lmm_size);
767                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
768                         repbody->eadatasize = ma->ma_lmm_size;
769                         if (S_ISDIR(la->la_mode))
770                                 repbody->valid |= OBD_MD_FLDIREA;
771                         else
772                                 repbody->valid |= OBD_MD_FLEASIZE;
773                 }
774                 if (ma->ma_valid & MA_LMV) {
775                         LASSERT(S_ISDIR(la->la_mode));
776                         repbody->eadatasize = ma->ma_lmv_size;
777                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
778                 }
779         } else if (S_ISLNK(la->la_mode) &&
780                    reqbody->valid & OBD_MD_LINKNAME) {
781                 buffer->lb_buf = ma->ma_lmm;
782                 /* eadatasize from client includes NULL-terminator, so
783                  * there is no need to read it */
784                 buffer->lb_len = reqbody->eadatasize - 1;
785                 rc = mo_readlink(env, next, buffer);
786                 if (unlikely(rc <= 0)) {
787                         CERROR("readlink failed: %d\n", rc);
788                         rc = -EFAULT;
789                 } else {
790                         int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
791
792                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
793                                 rc -= 2;
794                         repbody->valid |= OBD_MD_LINKNAME;
795                         /* we need to report back size with NULL-terminator
796                          * because client expects that */
797                         repbody->eadatasize = rc + 1;
798                         if (repbody->eadatasize != reqbody->eadatasize)
799                                 CERROR("Read shorter symlink %d, expected %d\n",
800                                        rc, reqbody->eadatasize - 1);
801                         /* NULL terminate */
802                         ((char *)ma->ma_lmm)[rc] = 0;
803
804                         /* If the total CDEBUG() size is larger than a page, it
805                          * will print a warning to the console, avoid this by
806                          * printing just the last part of the symlink. */
807                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
808                                print_limit < rc ? "..." : "", print_limit,
809                                (char *)ma->ma_lmm + rc - print_limit, rc);
810                         rc = 0;
811                 }
812         }
813
814         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
815                 repbody->max_cookiesize = 0;
816                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
817                 repbody->valid |= OBD_MD_FLMODEASIZE;
818                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
819                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
820                        repbody->max_cookiesize);
821         }
822
823         if (exp_connect_rmtclient(info->mti_exp) &&
824             reqbody->valid & OBD_MD_FLRMTPERM) {
825                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
826
827                 /* mdt_getattr_lock only */
828                 rc = mdt_pack_remote_perm(info, o, buf);
829                 if (rc) {
830                         repbody->valid &= ~OBD_MD_FLRMTPERM;
831                         repbody->aclsize = 0;
832                         RETURN(rc);
833                 } else {
834                         repbody->valid |= OBD_MD_FLRMTPERM;
835                         repbody->aclsize = sizeof(struct mdt_remote_perm);
836                 }
837         }
838 #ifdef CONFIG_FS_POSIX_ACL
839         else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
840                  (reqbody->valid & OBD_MD_FLACL)) {
841                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
842                 buffer->lb_len = req_capsule_get_size(pill,
843                                                       &RMF_ACL, RCL_SERVER);
844                 if (buffer->lb_len > 0) {
845                         rc = mo_xattr_get(env, next, buffer,
846                                           XATTR_NAME_ACL_ACCESS);
847                         if (rc < 0) {
848                                 if (rc == -ENODATA) {
849                                         repbody->aclsize = 0;
850                                         repbody->valid |= OBD_MD_FLACL;
851                                         rc = 0;
852                                 } else if (rc == -EOPNOTSUPP) {
853                                         rc = 0;
854                                 } else {
855                                         CERROR("got acl size: %d\n", rc);
856                                 }
857                         } else {
858                                 repbody->aclsize = rc;
859                                 repbody->valid |= OBD_MD_FLACL;
860                                 rc = 0;
861                         }
862                 }
863         }
864 #endif
865
866         if (reqbody->valid & OBD_MD_FLMDSCAPA &&
867             info->mti_mdt->mdt_opts.mo_mds_capa &&
868             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
869                 struct lustre_capa *capa;
870
871                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
872                 LASSERT(capa);
873                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
874                 rc = mo_capa_get(env, next, capa, 0);
875                 if (rc)
876                         RETURN(rc);
877                 repbody->valid |= OBD_MD_FLMDSCAPA;
878         }
879
880 out:
881         if (rc == 0)
882                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
883
884         RETURN(rc);
885 }
886
887 static int mdt_renew_capa(struct mdt_thread_info *info)
888 {
889         struct mdt_object  *obj = info->mti_object;
890         struct mdt_body    *body;
891         struct lustre_capa *capa, *c;
892         int rc;
893         ENTRY;
894
895         /* if object doesn't exist, or server has disabled capability,
896          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
897          * flag not set.
898          */
899         if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
900             !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
901                 RETURN(0);
902
903         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
904         LASSERT(body != NULL);
905
906         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
907         LASSERT(c);
908
909         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
910         LASSERT(capa);
911
912         *capa = *c;
913         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
914         if (rc == 0)
915                 body->valid |= OBD_MD_FLOSSCAPA;
916         RETURN(rc);
917 }
918
919 int mdt_getattr(struct mdt_thread_info *info)
920 {
921         struct mdt_object       *obj = info->mti_object;
922         struct req_capsule      *pill = info->mti_pill;
923         struct mdt_body         *reqbody;
924         struct mdt_body         *repbody;
925         mode_t                   mode;
926         int rc, rc2;
927         ENTRY;
928
929         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
930         LASSERT(reqbody);
931
932         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
933                 rc = req_capsule_server_pack(pill);
934                 if (unlikely(rc))
935                         RETURN(err_serious(rc));
936                 rc = mdt_renew_capa(info);
937                 GOTO(out_shrink, rc);
938         }
939
940         LASSERT(obj != NULL);
941         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
942
943         mode = lu_object_attr(&obj->mot_obj.mo_lu);
944
945         /* old clients may not report needed easize, use max value then */
946         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
947                              reqbody->eadatasize == 0 ?
948                              info->mti_mdt->mdt_max_mdsize :
949                              reqbody->eadatasize);
950
951         rc = req_capsule_server_pack(pill);
952         if (unlikely(rc != 0))
953                 RETURN(err_serious(rc));
954
955         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
956         LASSERT(repbody != NULL);
957         repbody->eadatasize = 0;
958         repbody->aclsize = 0;
959
960         if (reqbody->valid & OBD_MD_FLRMTPERM)
961                 rc = mdt_init_ucred(info, reqbody);
962         else
963                 rc = mdt_check_ucred(info);
964         if (unlikely(rc))
965                 GOTO(out_shrink, rc);
966
967         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
968
969         /*
970          * Don't check capability at all, because rename might getattr for
971          * remote obj, and at that time no capability is available.
972          */
973         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
974         rc = mdt_getattr_internal(info, obj, 0);
975         if (reqbody->valid & OBD_MD_FLRMTPERM)
976                 mdt_exit_ucred(info);
977         EXIT;
978 out_shrink:
979         mdt_client_compatibility(info);
980         rc2 = mdt_fix_reply(info);
981         if (rc == 0)
982                 rc = rc2;
983         return rc;
984 }
985
986 int mdt_is_subdir(struct mdt_thread_info *info)
987 {
988         struct mdt_object     *o = info->mti_object;
989         struct req_capsule    *pill = info->mti_pill;
990         const struct mdt_body *body = info->mti_body;
991         struct mdt_body       *repbody;
992         int                    rc;
993         ENTRY;
994
995         LASSERT(o != NULL);
996
997         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
998
999         /*
1000          * We save last checked parent fid to @repbody->fid1 for remote
1001          * directory case.
1002          */
1003         LASSERT(fid_is_sane(&body->fid2));
1004         LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1005         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1006                            &body->fid2, &repbody->fid1);
1007         if (rc == 0 || rc == -EREMOTE)
1008                 repbody->valid |= OBD_MD_FLID;
1009
1010         RETURN(rc);
1011 }
1012
1013 int mdt_swap_layouts(struct mdt_thread_info *info)
1014 {
1015         struct ptlrpc_request   *req = mdt_info_req(info);
1016         struct obd_export       *exp = req->rq_export;
1017         struct mdt_object       *o1, *o2, *o;
1018         struct mdt_lock_handle  *lh1, *lh2;
1019         struct mdc_swap_layouts *msl;
1020         int                      rc;
1021         ENTRY;
1022
1023         /* client does not support layout lock, so layout swaping
1024          * is disabled.
1025          * FIXME: there is a problem for old clients which don't support
1026          * layout lock yet. If those clients have already opened the file
1027          * they won't be notified at all so that old layout may still be
1028          * used to do IO. This can be fixed after file release is landed by
1029          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1030         if (!exp_connect_layout(exp))
1031                 RETURN(-EOPNOTSUPP);
1032
1033         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1034                 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1035                                  req_capsule_client_get(info->mti_pill,
1036                                                         &RMF_CAPA1));
1037
1038         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1039                 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1040                                  req_capsule_client_get(info->mti_pill,
1041                                                         &RMF_CAPA2));
1042
1043         o1 = info->mti_object;
1044         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1045                                 &info->mti_body->fid2);
1046         if (IS_ERR(o))
1047                 GOTO(out, rc = PTR_ERR(o));
1048
1049         if (mdt_object_exists(o) < 0) /* remote object */
1050                 GOTO(put, rc = -ENOENT);
1051
1052         rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1053         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1054                 GOTO(put, rc);
1055
1056         if (rc < 0)
1057                 swap(o1, o2);
1058
1059         /* permission check. Make sure the calling process having permission
1060          * to write both files. */
1061         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1062                                 MAY_WRITE);
1063         if (rc < 0)
1064                 GOTO(put, rc);
1065
1066         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1067                                 MAY_WRITE);
1068         if (rc < 0)
1069                 GOTO(put, rc);
1070
1071         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1072         LASSERT(msl != NULL);
1073
1074         lh1 = &info->mti_lh[MDT_LH_NEW];
1075         mdt_lock_reg_init(lh1, LCK_EX);
1076         lh2 = &info->mti_lh[MDT_LH_OLD];
1077         mdt_lock_reg_init(lh2, LCK_EX);
1078
1079         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1080                              MDT_LOCAL_LOCK);
1081         if (rc < 0)
1082                 GOTO(put, rc);
1083
1084         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1085                              MDT_LOCAL_LOCK);
1086         if (rc < 0)
1087                 GOTO(unlock1, rc);
1088
1089         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1090                              mdt_object_child(o2), msl->msl_flags);
1091         GOTO(unlock2, rc);
1092 unlock2:
1093         mdt_object_unlock(info, o2, lh2, rc);
1094 unlock1:
1095         mdt_object_unlock(info, o1, lh1, rc);
1096 put:
1097         mdt_object_put(info->mti_env, o);
1098 out:
1099         RETURN(rc);
1100 }
1101
1102 static int mdt_raw_lookup(struct mdt_thread_info *info,
1103                           struct mdt_object *parent,
1104                           const struct lu_name *lname,
1105                           struct ldlm_reply *ldlm_rep)
1106 {
1107         struct md_object *next = mdt_object_child(info->mti_object);
1108         const struct mdt_body *reqbody = info->mti_body;
1109         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1110         struct mdt_body *repbody;
1111         int rc;
1112         ENTRY;
1113
1114         if (reqbody->valid != OBD_MD_FLID)
1115                 RETURN(0);
1116
1117         LASSERT(!info->mti_cross_ref);
1118
1119         /* Only got the fid of this obj by name */
1120         fid_zero(child_fid);
1121         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1122                         &info->mti_spec);
1123 #if 0
1124         /* XXX is raw_lookup possible as intent operation? */
1125         if (rc != 0) {
1126                 if (rc == -ENOENT)
1127                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1128                 RETURN(rc);
1129         } else
1130                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1131
1132         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1133 #endif
1134         if (rc == 0) {
1135                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1136                 repbody->fid1 = *child_fid;
1137                 repbody->valid = OBD_MD_FLID;
1138         }
1139         RETURN(1);
1140 }
1141
1142 /*
1143  * UPDATE lock should be taken against parent, and be release before exit;
1144  * child_bits lock should be taken against child, and be returned back:
1145  *            (1)normal request should release the child lock;
1146  *            (2)intent request will grant the lock to client.
1147  */
1148 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1149                                  struct mdt_lock_handle *lhc,
1150                                  __u64 child_bits,
1151                                  struct ldlm_reply *ldlm_rep)
1152 {
1153         struct ptlrpc_request  *req       = mdt_info_req(info);
1154         struct mdt_body        *reqbody   = NULL;
1155         struct mdt_object      *parent    = info->mti_object;
1156         struct mdt_object      *child;
1157         struct md_object       *next      = mdt_object_child(parent);
1158         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
1159         struct lu_name         *lname     = NULL;
1160         const char             *name      = NULL;
1161         int                     namelen   = 0;
1162         struct mdt_lock_handle *lhp       = NULL;
1163         struct ldlm_lock       *lock;
1164         struct ldlm_res_id     *res_id;
1165         int                     is_resent;
1166         int                     ma_need = 0;
1167         int                     rc;
1168
1169         ENTRY;
1170
1171         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1172         LASSERT(ergo(is_resent,
1173                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1174
1175         LASSERT(parent != NULL);
1176         name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1177         if (name == NULL)
1178                 RETURN(err_serious(-EFAULT));
1179
1180         namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1181                                        RCL_CLIENT) - 1;
1182         if (!info->mti_cross_ref) {
1183                 /*
1184                  * XXX: Check for "namelen == 0" is for getattr by fid
1185                  * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1186                  * that is the name must contain at least one character and
1187                  * the terminating '\0'
1188                  */
1189                 if (namelen == 0) {
1190                         reqbody = req_capsule_client_get(info->mti_pill,
1191                                                          &RMF_MDT_BODY);
1192                         if (unlikely(reqbody == NULL))
1193                                 RETURN(err_serious(-EFAULT));
1194
1195                         if (unlikely(!fid_is_sane(&reqbody->fid2)))
1196                                 RETURN(err_serious(-EINVAL));
1197
1198                         name = NULL;
1199                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1200                                "ldlm_rep = %p\n",
1201                                PFID(mdt_object_fid(parent)),
1202                                PFID(&reqbody->fid2), ldlm_rep);
1203                 } else {
1204                         lname = mdt_name(info->mti_env, (char *)name, namelen);
1205                         CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1206                                "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1207                                name, ldlm_rep);
1208                 }
1209         }
1210         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1211
1212         if (unlikely(!mdt_object_exists(parent))) {
1213                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1214                                 &parent->mot_obj.mo_lu,
1215                                 "Parent doesn't exist!\n");
1216                 RETURN(-ESTALE);
1217         } else if (!info->mti_cross_ref) {
1218                 LASSERTF(!mdt_object_remote(parent),
1219                          "Parent "DFID" is on remote server\n",
1220                          PFID(mdt_object_fid(parent)));
1221         }
1222         if (lname) {
1223                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1224                 if (rc != 0) {
1225                         if (rc > 0)
1226                                 rc = 0;
1227                         RETURN(rc);
1228                 }
1229         }
1230
1231         if (info->mti_cross_ref) {
1232                 /* Only getattr on the child. Parent is on another node. */
1233                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1234                 child = parent;
1235                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1236                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1237
1238                 if (is_resent) {
1239                         /* Do not take lock for resent case. */
1240                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1241                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1242                                  lhc->mlh_reg_lh.cookie);
1243                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
1244                                                 &lock->l_resource->lr_name));
1245                         LDLM_LOCK_PUT(lock);
1246                         rc = 0;
1247                 } else {
1248                         mdt_lock_handle_init(lhc);
1249                         mdt_lock_reg_init(lhc, LCK_PR);
1250
1251                         /*
1252                          * Object's name is on another MDS, no lookup lock is
1253                          * needed here but update is.
1254                          */
1255                         child_bits &= ~MDS_INODELOCK_LOOKUP;
1256                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1257
1258                         rc = mdt_object_lock(info, child, lhc, child_bits,
1259                                              MDT_LOCAL_LOCK);
1260                 }
1261                 if (rc == 0) {
1262                         /* Finally, we can get attr for child. */
1263                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
1264                                          BYPASS_CAPA);
1265                         rc = mdt_getattr_internal(info, child, 0);
1266                         if (unlikely(rc != 0))
1267                                 mdt_object_unlock(info, child, lhc, 1);
1268                 }
1269                 RETURN(rc);
1270         }
1271
1272         if (lname) {
1273                 /* step 1: lock parent only if parent is a directory */
1274                 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1275                         lhp = &info->mti_lh[MDT_LH_PARENT];
1276                         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1277                         rc = mdt_object_lock(info, parent, lhp,
1278                                              MDS_INODELOCK_UPDATE,
1279                                              MDT_LOCAL_LOCK);
1280                         if (unlikely(rc != 0))
1281                                 RETURN(rc);
1282                 }
1283
1284                 /* step 2: lookup child's fid by name */
1285                 fid_zero(child_fid);
1286                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1287                                 &info->mti_spec);
1288
1289                 if (rc != 0) {
1290                         if (rc == -ENOENT)
1291                                 mdt_set_disposition(info, ldlm_rep,
1292                                                     DISP_LOOKUP_NEG);
1293                         GOTO(out_parent, rc);
1294                 } else
1295                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1296         } else {
1297                 *child_fid = reqbody->fid2;
1298                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1299         }
1300
1301         /*
1302          *step 3: find the child object by fid & lock it.
1303          *        regardless if it is local or remote.
1304          */
1305         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1306
1307         if (unlikely(IS_ERR(child)))
1308                 GOTO(out_parent, rc = PTR_ERR(child));
1309         if (is_resent) {
1310                 /* Do not take lock for resent case. */
1311                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1312                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1313                          lhc->mlh_reg_lh.cookie);
1314
1315                 res_id = &lock->l_resource->lr_name;
1316                 if (!fid_res_name_eq(mdt_object_fid(child),
1317                                     &lock->l_resource->lr_name)) {
1318                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1319                                                  &lock->l_resource->lr_name),
1320                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1321                                  (unsigned long)res_id->name[0],
1322                                  (unsigned long)res_id->name[1],
1323                                  (unsigned long)res_id->name[2],
1324                                  PFID(mdt_object_fid(parent)));
1325                           CWARN("Although resent, but still not get child lock"
1326                                 "parent:"DFID" child:"DFID"\n",
1327                                 PFID(mdt_object_fid(parent)),
1328                                 PFID(mdt_object_fid(child)));
1329                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1330                           LDLM_LOCK_PUT(lock);
1331                           GOTO(relock, 0);
1332                 }
1333                 LDLM_LOCK_PUT(lock);
1334                 rc = 0;
1335         } else {
1336                 bool try_layout = false;
1337
1338 relock:
1339                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1340                 mdt_lock_handle_init(lhc);
1341                 mdt_lock_reg_init(lhc, LCK_PR);
1342
1343                 if (!mdt_object_exists(child)) {
1344                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1345                                         &child->mot_obj.mo_lu,
1346                                         "Object doesn't exist!\n");
1347                         GOTO(out_child, rc = -ENOENT);
1348                 }
1349
1350                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1351                       mdt_object_exists(child) && !mdt_object_remote(child)) {
1352                         struct md_attr *ma = &info->mti_attr;
1353
1354                         ma->ma_valid = 0;
1355                         ma->ma_need = MA_INODE;
1356                         rc = mdt_attr_get_complex(info, child, ma);
1357                         if (unlikely(rc != 0))
1358                                 GOTO(out_child, rc);
1359
1360                         /* If the file has not been changed for some time, we
1361                          * return not only a LOOKUP lock, but also an UPDATE
1362                          * lock and this might save us RPC on later STAT. For
1363                          * directories, it also let negative dentry starts
1364                          * working for this dir. */
1365                         if (ma->ma_valid & MA_INODE &&
1366                             ma->ma_attr.la_valid & LA_CTIME &&
1367                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1368                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1369                                 child_bits |= MDS_INODELOCK_UPDATE;
1370                 }
1371
1372                 /* layout lock must be granted in a best-effort way
1373                  * for IT operations */
1374                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1375                 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1376                     exp_connect_layout(info->mti_exp) &&
1377                     S_ISREG(lu_object_attr(&child->mot_obj.mo_lu)) &&
1378                     ldlm_rep != NULL) {
1379                         /* try to grant layout lock for regular file. */
1380                         try_layout = true;
1381                 }
1382
1383                 rc = 0;
1384                 if (try_layout) {
1385                         child_bits |= MDS_INODELOCK_LAYOUT;
1386                         /* try layout lock, it may fail to be granted due to
1387                          * contention at LOOKUP or UPDATE */
1388                         if (!mdt_object_lock_try(info, child, lhc, child_bits,
1389                                                  MDT_CROSS_LOCK)) {
1390                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1391                                 LASSERT(child_bits != 0);
1392                                 rc = mdt_object_lock(info, child, lhc,
1393                                                 child_bits, MDT_CROSS_LOCK);
1394                         } else {
1395                                 ma_need |= MA_LOV;
1396                         }
1397                 } else {
1398                         rc = mdt_object_lock(info, child, lhc, child_bits,
1399                                                 MDT_CROSS_LOCK);
1400                 }
1401                 if (unlikely(rc != 0))
1402                         GOTO(out_child, rc);
1403         }
1404
1405         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1406         /* Get MA_SOM attributes if update lock is given. */
1407         if (lock &&
1408             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1409             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1410                 ma_need |= MA_SOM;
1411
1412         /* finally, we can get attr for child. */
1413         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1414         rc = mdt_getattr_internal(info, child, ma_need);
1415         if (unlikely(rc != 0)) {
1416                 mdt_object_unlock(info, child, lhc, 1);
1417         } else if (lock) {
1418                 /* Debugging code. */
1419                 res_id = &lock->l_resource->lr_name;
1420                 LDLM_DEBUG(lock, "Returning lock to client");
1421                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1422                                          &lock->l_resource->lr_name),
1423                          "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1424                          (unsigned long)res_id->name[0],
1425                          (unsigned long)res_id->name[1],
1426                          (unsigned long)res_id->name[2],
1427                          PFID(mdt_object_fid(child)));
1428                 if (mdt_object_exists(child) && !mdt_object_remote(child))
1429                         mdt_pack_size2body(info, child);
1430         }
1431         if (lock)
1432                 LDLM_LOCK_PUT(lock);
1433
1434         EXIT;
1435 out_child:
1436         mdt_object_put(info->mti_env, child);
1437 out_parent:
1438         if (lhp)
1439                 mdt_object_unlock(info, parent, lhp, 1);
1440         return rc;
1441 }
1442
1443 /* normal handler: should release the child lock */
1444 int mdt_getattr_name(struct mdt_thread_info *info)
1445 {
1446         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1447         struct mdt_body        *reqbody;
1448         struct mdt_body        *repbody;
1449         int rc, rc2;
1450         ENTRY;
1451
1452         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1453         LASSERT(reqbody != NULL);
1454         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1455         LASSERT(repbody != NULL);
1456
1457         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1458         repbody->eadatasize = 0;
1459         repbody->aclsize = 0;
1460
1461         rc = mdt_init_ucred(info, reqbody);
1462         if (unlikely(rc))
1463                 GOTO(out_shrink, rc);
1464
1465         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1466         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1467                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1468                 lhc->mlh_reg_lh.cookie = 0;
1469         }
1470         mdt_exit_ucred(info);
1471         EXIT;
1472 out_shrink:
1473         mdt_client_compatibility(info);
1474         rc2 = mdt_fix_reply(info);
1475         if (rc == 0)
1476                 rc = rc2;
1477         return rc;
1478 }
1479
1480 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1481                          void *karg, void *uarg);
1482
1483 int mdt_set_info(struct mdt_thread_info *info)
1484 {
1485         struct ptlrpc_request *req = mdt_info_req(info);
1486         char *key;
1487         void *val;
1488         int keylen, vallen, rc = 0;
1489         ENTRY;
1490
1491         rc = req_capsule_server_pack(info->mti_pill);
1492         if (rc)
1493                 RETURN(rc);
1494
1495         key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1496         if (key == NULL) {
1497                 DEBUG_REQ(D_HA, req, "no set_info key");
1498                 RETURN(-EFAULT);
1499         }
1500
1501         keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1502                                       RCL_CLIENT);
1503
1504         val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1505         if (val == NULL) {
1506                 DEBUG_REQ(D_HA, req, "no set_info val");
1507                 RETURN(-EFAULT);
1508         }
1509
1510         vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1511                                       RCL_CLIENT);
1512
1513         /* Swab any part of val you need to here */
1514         if (KEY_IS(KEY_READ_ONLY)) {
1515                 req->rq_status = 0;
1516                 lustre_msg_set_status(req->rq_repmsg, 0);
1517
1518                 spin_lock(&req->rq_export->exp_lock);
1519                 if (*(__u32 *)val)
1520                         *exp_connect_flags_ptr(req->rq_export) |=
1521                                 OBD_CONNECT_RDONLY;
1522                 else
1523                         *exp_connect_flags_ptr(req->rq_export) &=
1524                                 ~OBD_CONNECT_RDONLY;
1525                 spin_unlock(&req->rq_export->exp_lock);
1526
1527         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1528                 struct changelog_setinfo *cs =
1529                         (struct changelog_setinfo *)val;
1530                 if (vallen != sizeof(*cs)) {
1531                         CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1532                         RETURN(-EINVAL);
1533                 }
1534                 if (ptlrpc_req_need_swab(req)) {
1535                         __swab64s(&cs->cs_recno);
1536                         __swab32s(&cs->cs_id);
1537                 }
1538
1539                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1540                                    vallen, val, NULL);
1541                 lustre_msg_set_status(req->rq_repmsg, rc);
1542
1543         } else {
1544                 RETURN(-EINVAL);
1545         }
1546         RETURN(0);
1547 }
1548
1549 /**
1550  * Top-level handler for MDT connection requests.
1551  */
1552 int mdt_connect(struct mdt_thread_info *info)
1553 {
1554         int rc;
1555         struct obd_connect_data *reply;
1556         struct obd_export *exp;
1557         struct ptlrpc_request *req = mdt_info_req(info);
1558
1559         rc = target_handle_connect(req);
1560         if (rc != 0)
1561                 return err_serious(rc);
1562
1563         LASSERT(req->rq_export != NULL);
1564         info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1565         rc = mdt_init_sec_level(info);
1566         if (rc != 0) {
1567                 obd_disconnect(class_export_get(req->rq_export));
1568                 return rc;
1569         }
1570
1571         /* To avoid exposing partially initialized connection flags, changes up
1572          * to this point have been staged in reply->ocd_connect_flags. Now that
1573          * connection handling has completed successfully, atomically update
1574          * the connect flags in the shared export data structure. LU-1623 */
1575         reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1576         exp = req->rq_export;
1577         spin_lock(&exp->exp_lock);
1578         *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1579         spin_unlock(&exp->exp_lock);
1580
1581         rc = mdt_init_idmap(info);
1582         if (rc != 0)
1583                 obd_disconnect(class_export_get(req->rq_export));
1584
1585         return rc;
1586 }
1587
1588 int mdt_disconnect(struct mdt_thread_info *info)
1589 {
1590         int rc;
1591         ENTRY;
1592
1593         rc = target_handle_disconnect(mdt_info_req(info));
1594         if (rc)
1595                 rc = err_serious(rc);
1596         RETURN(rc);
1597 }
1598
1599 static int mdt_sendpage(struct mdt_thread_info *info,
1600                         struct lu_rdpg *rdpg, int nob)
1601 {
1602         struct ptlrpc_request   *req = mdt_info_req(info);
1603         struct obd_export       *exp = req->rq_export;
1604         struct ptlrpc_bulk_desc *desc;
1605         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1606         int                      tmpcount;
1607         int                      tmpsize;
1608         int                      i;
1609         int                      rc;
1610         ENTRY;
1611
1612         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1613                                     MDS_BULK_PORTAL);
1614         if (desc == NULL)
1615                 RETURN(-ENOMEM);
1616
1617         if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1618                 /* old client requires reply size in it's PAGE_SIZE,
1619                  * which is rdpg->rp_count */
1620                 nob = rdpg->rp_count;
1621
1622         for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1623              i++, tmpcount -= tmpsize) {
1624                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1625                 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1626         }
1627
1628         LASSERT(desc->bd_nob == nob);
1629         rc = target_bulk_io(exp, desc, lwi);
1630         ptlrpc_free_bulk_pin(desc);
1631         RETURN(rc);
1632 }
1633
1634 int mdt_readpage(struct mdt_thread_info *info)
1635 {
1636         struct mdt_object *object = info->mti_object;
1637         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1638         struct mdt_body   *reqbody;
1639         struct mdt_body   *repbody;
1640         int                rc;
1641         int                i;
1642         ENTRY;
1643
1644         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1645                 RETURN(err_serious(-ENOMEM));
1646
1647         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1648         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1649         if (reqbody == NULL || repbody == NULL)
1650                 RETURN(err_serious(-EFAULT));
1651
1652         /*
1653          * prepare @rdpg before calling lower layers and transfer itself. Here
1654          * reqbody->size contains offset of where to start to read and
1655          * reqbody->nlink contains number bytes to read.
1656          */
1657         rdpg->rp_hash = reqbody->size;
1658         if (rdpg->rp_hash != reqbody->size) {
1659                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1660                        rdpg->rp_hash, reqbody->size);
1661                 RETURN(-EFAULT);
1662         }
1663
1664         rdpg->rp_attrs = reqbody->mode;
1665         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1666                 rdpg->rp_attrs |= LUDA_64BITHASH;
1667         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1668                                 exp_max_brw_size(info->mti_exp));
1669         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1670                           CFS_PAGE_SHIFT;
1671         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1672         if (rdpg->rp_pages == NULL)
1673                 RETURN(-ENOMEM);
1674
1675         for (i = 0; i < rdpg->rp_npages; ++i) {
1676                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1677                 if (rdpg->rp_pages[i] == NULL)
1678                         GOTO(free_rdpg, rc = -ENOMEM);
1679         }
1680
1681         /* call lower layers to fill allocated pages with directory data */
1682         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1683         if (rc < 0)
1684                 GOTO(free_rdpg, rc);
1685
1686         /* send pages to client */
1687         rc = mdt_sendpage(info, rdpg, rc);
1688
1689         EXIT;
1690 free_rdpg:
1691
1692         for (i = 0; i < rdpg->rp_npages; i++)
1693                 if (rdpg->rp_pages[i] != NULL)
1694                         cfs_free_page(rdpg->rp_pages[i]);
1695         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1696
1697         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1698                 RETURN(0);
1699
1700         return rc;
1701 }
1702
1703 static int mdt_reint_internal(struct mdt_thread_info *info,
1704                               struct mdt_lock_handle *lhc,
1705                               __u32 op)
1706 {
1707         struct req_capsule      *pill = info->mti_pill;
1708         struct mdt_body         *repbody;
1709         int                      rc = 0, rc2;
1710         ENTRY;
1711
1712
1713         rc = mdt_reint_unpack(info, op);
1714         if (rc != 0) {
1715                 CERROR("Can't unpack reint, rc %d\n", rc);
1716                 RETURN(err_serious(rc));
1717         }
1718
1719         /* for replay (no_create) lmm is not needed, client has it already */
1720         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1721                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1722                                      info->mti_rr.rr_eadatalen);
1723
1724         /* llog cookies are always 0, the field is kept for compatibility */
1725         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1726                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1727
1728         rc = req_capsule_server_pack(pill);
1729         if (rc != 0) {
1730                 CERROR("Can't pack response, rc %d\n", rc);
1731                 RETURN(err_serious(rc));
1732         }
1733
1734         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1735                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1736                 LASSERT(repbody);
1737                 repbody->eadatasize = 0;
1738                 repbody->aclsize = 0;
1739         }
1740
1741         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1742
1743         /* for replay no cookkie / lmm need, because client have this already */
1744         if (info->mti_spec.no_create)
1745                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1746                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1747
1748         rc = mdt_init_ucred_reint(info);
1749         if (rc)
1750                 GOTO(out_shrink, rc);
1751
1752         rc = mdt_fix_attr_ucred(info, op);
1753         if (rc != 0)
1754                 GOTO(out_ucred, rc = err_serious(rc));
1755
1756         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1757                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1758                 GOTO(out_ucred, rc);
1759         }
1760         rc = mdt_reint_rec(info, lhc);
1761         EXIT;
1762 out_ucred:
1763         mdt_exit_ucred(info);
1764 out_shrink:
1765         mdt_client_compatibility(info);
1766         rc2 = mdt_fix_reply(info);
1767         if (rc == 0)
1768                 rc = rc2;
1769         return rc;
1770 }
1771
1772 static long mdt_reint_opcode(struct mdt_thread_info *info,
1773                              const struct req_format **fmt)
1774 {
1775         struct mdt_rec_reint *rec;
1776         long opc;
1777
1778         opc = err_serious(-EFAULT);
1779         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1780         if (rec != NULL) {
1781                 opc = rec->rr_opcode;
1782                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1783                 if (opc < REINT_MAX && fmt[opc] != NULL)
1784                         req_capsule_extend(info->mti_pill, fmt[opc]);
1785                 else {
1786                         CERROR("Unsupported opc: %ld\n", opc);
1787                         opc = err_serious(opc);
1788                 }
1789         }
1790         return opc;
1791 }
1792
1793 int mdt_reint(struct mdt_thread_info *info)
1794 {
1795         long opc;
1796         int  rc;
1797
1798         static const struct req_format *reint_fmts[REINT_MAX] = {
1799                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1800                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1801                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1802                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1803                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1804                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1805                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1806                 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1807         };
1808
1809         ENTRY;
1810
1811         opc = mdt_reint_opcode(info, reint_fmts);
1812         if (opc >= 0) {
1813                 /*
1814                  * No lock possible here from client to pass it to reint code
1815                  * path.
1816                  */
1817                 rc = mdt_reint_internal(info, NULL, opc);
1818         } else {
1819                 rc = opc;
1820         }
1821
1822         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1823         RETURN(rc);
1824 }
1825
1826 /* this should sync the whole device */
1827 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1828 {
1829         struct dt_device *dt = mdt->mdt_bottom;
1830         int rc;
1831         ENTRY;
1832
1833         rc = dt->dd_ops->dt_sync(env, dt);
1834         RETURN(rc);
1835 }
1836
1837 /* this should sync this object */
1838 static int mdt_object_sync(struct mdt_thread_info *info)
1839 {
1840         struct md_object *next;
1841         int rc;
1842         ENTRY;
1843
1844         if (!mdt_object_exists(info->mti_object)) {
1845                 CWARN("Non existing object  "DFID"!\n",
1846                       PFID(mdt_object_fid(info->mti_object)));
1847                 RETURN(-ESTALE);
1848         }
1849         next = mdt_object_child(info->mti_object);
1850         rc = mo_object_sync(info->mti_env, next);
1851
1852         RETURN(rc);
1853 }
1854
1855 int mdt_sync(struct mdt_thread_info *info)
1856 {
1857         struct ptlrpc_request *req = mdt_info_req(info);
1858         struct req_capsule *pill = info->mti_pill;
1859         struct mdt_body *body;
1860         int rc;
1861         ENTRY;
1862
1863         /* The fid may be zero, so we req_capsule_set manually */
1864         req_capsule_set(pill, &RQF_MDS_SYNC);
1865
1866         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1867         if (body == NULL)
1868                 RETURN(err_serious(-EINVAL));
1869
1870         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1871                 RETURN(err_serious(-ENOMEM));
1872
1873         if (fid_seq(&body->fid1) == 0) {
1874                 /* sync the whole device */
1875                 rc = req_capsule_server_pack(pill);
1876                 if (rc == 0)
1877                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1878                 else
1879                         rc = err_serious(rc);
1880         } else {
1881                 /* sync an object */
1882                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1883                 if (rc == 0) {
1884                         rc = mdt_object_sync(info);
1885                         if (rc == 0) {
1886                                 const struct lu_fid *fid;
1887                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1888
1889                                 info->mti_attr.ma_need = MA_INODE;
1890                                 info->mti_attr.ma_valid = 0;
1891                                 rc = mdt_attr_get_complex(info, info->mti_object,
1892                                                           &info->mti_attr);
1893                                 if (rc == 0) {
1894                                         body = req_capsule_server_get(pill,
1895                                                                 &RMF_MDT_BODY);
1896                                         fid = mdt_object_fid(info->mti_object);
1897                                         mdt_pack_attr2body(info, body, la, fid);
1898                                 }
1899                         }
1900                 } else
1901                         rc = err_serious(rc);
1902         }
1903         if (rc == 0)
1904                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1905
1906         RETURN(rc);
1907 }
1908
1909 /*
1910  * Quotacheck handler.
1911  * in-kernel quotacheck isn't supported any more.
1912  */
1913 int mdt_quotacheck(struct mdt_thread_info *info)
1914 {
1915         struct obd_quotactl     *oqctl;
1916         int                      rc;
1917         ENTRY;
1918
1919         oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1920         if (oqctl == NULL)
1921                 RETURN(err_serious(-EPROTO));
1922
1923         rc = req_capsule_server_pack(info->mti_pill);
1924         if (rc)
1925                 RETURN(err_serious(rc));
1926
1927         /* deprecated, not used any more */
1928         RETURN(-EOPNOTSUPP);
1929 }
1930
1931 /*
1932  * Handle quota control requests to consult current usage/limit, but also
1933  * to configure quota enforcement
1934  */
1935 int mdt_quotactl(struct mdt_thread_info *info)
1936 {
1937         struct obd_export       *exp  = info->mti_exp;
1938         struct req_capsule      *pill = info->mti_pill;
1939         struct obd_quotactl     *oqctl, *repoqc;
1940         int                      id, rc;
1941         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
1942         ENTRY;
1943
1944         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1945         if (oqctl == NULL)
1946                 RETURN(err_serious(-EPROTO));
1947
1948         rc = req_capsule_server_pack(pill);
1949         if (rc)
1950                 RETURN(err_serious(rc));
1951
1952         switch (oqctl->qc_cmd) {
1953         case Q_QUOTACHECK:
1954         case LUSTRE_Q_INVALIDATE:
1955         case LUSTRE_Q_FINVALIDATE:
1956         case Q_QUOTAON:
1957         case Q_QUOTAOFF:
1958         case Q_INITQUOTA:
1959                 /* deprecated, not used any more */
1960                 RETURN(-EOPNOTSUPP);
1961                 /* master quotactl */
1962         case Q_GETINFO:
1963         case Q_SETINFO:
1964         case Q_SETQUOTA:
1965         case Q_GETQUOTA:
1966                 if (qmt == NULL)
1967                         RETURN(-EOPNOTSUPP);
1968                 /* slave quotactl */
1969         case Q_GETOINFO:
1970         case Q_GETOQUOTA:
1971                 break;
1972         default:
1973                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1974                 RETURN(-EFAULT);
1975         }
1976
1977         /* map uid/gid for remote client */
1978         id = oqctl->qc_id;
1979         if (exp_connect_rmtclient(exp)) {
1980                 struct lustre_idmap_table *idmap;
1981
1982                 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1983
1984                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1985                              oqctl->qc_cmd != Q_GETINFO))
1986                         RETURN(-EPERM);
1987
1988                 if (oqctl->qc_type == USRQUOTA)
1989                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1990                                                      oqctl->qc_id);
1991                 else if (oqctl->qc_type == GRPQUOTA)
1992                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1993                                                      oqctl->qc_id);
1994                 else
1995                         RETURN(-EINVAL);
1996
1997                 if (id == CFS_IDMAP_NOTFOUND) {
1998                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1999                         RETURN(-EACCES);
2000                 }
2001         }
2002
2003         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2004         if (repoqc == NULL)
2005                 RETURN(err_serious(-EFAULT));
2006
2007         if (oqctl->qc_id != id)
2008                 swap(oqctl->qc_id, id);
2009
2010         switch (oqctl->qc_cmd) {
2011
2012         case Q_GETINFO:
2013         case Q_SETINFO:
2014         case Q_SETQUOTA:
2015         case Q_GETQUOTA:
2016                 /* forward quotactl request to QMT */
2017                 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2018                 break;
2019
2020         case Q_GETOINFO:
2021         case Q_GETOQUOTA:
2022                 /* slave quotactl */
2023                 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2024                                    oqctl);
2025                 break;
2026
2027         default:
2028                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2029                 RETURN(-EFAULT);
2030         }
2031
2032         if (oqctl->qc_id != id)
2033                 swap(oqctl->qc_id, id);
2034
2035         *repoqc = *oqctl;
2036         RETURN(rc);
2037 }
2038
2039 /*
2040  * OBD PING and other handlers.
2041  */
2042 int mdt_obd_ping(struct mdt_thread_info *info)
2043 {
2044         int rc;
2045         ENTRY;
2046
2047         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2048
2049         rc = target_handle_ping(mdt_info_req(info));
2050         if (rc < 0)
2051                 rc = err_serious(rc);
2052         RETURN(rc);
2053 }
2054
2055 /*
2056  * OBD_IDX_READ handler
2057  */
2058 int mdt_obd_idx_read(struct mdt_thread_info *info)
2059 {
2060         struct mdt_device       *mdt = info->mti_mdt;
2061         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2062         struct idx_info         *req_ii, *rep_ii;
2063         int                      rc, i;
2064         ENTRY;
2065
2066         memset(rdpg, 0, sizeof(*rdpg));
2067         req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2068
2069         /* extract idx_info buffer from request & reply */
2070         req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2071         if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2072                 RETURN(err_serious(-EPROTO));
2073
2074         rc = req_capsule_server_pack(info->mti_pill);
2075         if (rc)
2076                 RETURN(err_serious(rc));
2077
2078         rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2079         if (rep_ii == NULL)
2080                 RETURN(err_serious(-EFAULT));
2081         rep_ii->ii_magic = IDX_INFO_MAGIC;
2082
2083         /* extract hash to start with */
2084         rdpg->rp_hash = req_ii->ii_hash_start;
2085
2086         /* extract requested attributes */
2087         rdpg->rp_attrs = req_ii->ii_attrs;
2088
2089         /* check that fid packed in request is valid and supported */
2090         if (!fid_is_sane(&req_ii->ii_fid))
2091                 RETURN(-EINVAL);
2092         rep_ii->ii_fid = req_ii->ii_fid;
2093
2094         /* copy flags */
2095         rep_ii->ii_flags = req_ii->ii_flags;
2096
2097         /* compute number of pages to allocate, ii_count is the number of 4KB
2098          * containers */
2099         if (req_ii->ii_count <= 0)
2100                 GOTO(out, rc = -EFAULT);
2101         rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2102                                exp_max_brw_size(info->mti_exp));
2103         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2104
2105         /* allocate pages to store the containers */
2106         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2107         if (rdpg->rp_pages == NULL)
2108                 GOTO(out, rc = -ENOMEM);
2109         for (i = 0; i < rdpg->rp_npages; i++) {
2110                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2111                 if (rdpg->rp_pages[i] == NULL)
2112                         GOTO(out, rc = -ENOMEM);
2113         }
2114
2115         /* populate pages with key/record pairs */
2116         rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2117         if (rc < 0)
2118                 GOTO(out, rc);
2119
2120         LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2121                  "asked %d > %d\n", rc, rdpg->rp_count);
2122
2123         /* send pages to client */
2124         rc = mdt_sendpage(info, rdpg, rc);
2125
2126         GOTO(out, rc);
2127 out:
2128         if (rdpg->rp_pages) {
2129                 for (i = 0; i < rdpg->rp_npages; i++)
2130                         if (rdpg->rp_pages[i])
2131                                 cfs_free_page(rdpg->rp_pages[i]);
2132                 OBD_FREE(rdpg->rp_pages,
2133                          rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2134         }
2135         return rc;
2136 }
2137
2138 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2139 {
2140         return err_serious(-EOPNOTSUPP);
2141 }
2142
2143 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2144 {
2145         return err_serious(-EOPNOTSUPP);
2146 }
2147
2148 /*
2149  * LLOG handlers.
2150  */
2151
2152 /** clone llog ctxt from child (mdd)
2153  * This allows remote llog (replicator) access.
2154  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2155  * context was originally set up, or we can handle them directly.
2156  * I choose the latter, but that means I need any llog
2157  * contexts set up by child to be accessable by the mdt.  So we clone the
2158  * context into our context list here.
2159  */
2160 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2161                                int idx)
2162 {
2163         struct md_device  *next = mdt->mdt_child;
2164         struct llog_ctxt *ctxt;
2165         int rc;
2166
2167         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2168                 return 0;
2169
2170         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2171         if (rc || ctxt == NULL) {
2172                 return 0;
2173         }
2174
2175         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2176         if (rc)
2177                 CERROR("Can't set mdt ctxt %d\n", rc);
2178
2179         return rc;
2180 }
2181
2182 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2183                                  struct mdt_device *mdt, int idx)
2184 {
2185         struct llog_ctxt *ctxt;
2186
2187         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2188         if (ctxt == NULL)
2189                 return 0;
2190         /* Put once for the get we just did, and once for the clone */
2191         llog_ctxt_put(ctxt);
2192         llog_ctxt_put(ctxt);
2193         return 0;
2194 }
2195
2196 int mdt_llog_create(struct mdt_thread_info *info)
2197 {
2198         int rc;
2199
2200         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2201         rc = llog_origin_handle_open(mdt_info_req(info));
2202         return (rc < 0 ? err_serious(rc) : rc);
2203 }
2204
2205 int mdt_llog_destroy(struct mdt_thread_info *info)
2206 {
2207         int rc;
2208
2209         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2210         rc = llog_origin_handle_destroy(mdt_info_req(info));
2211         return (rc < 0 ? err_serious(rc) : rc);
2212 }
2213
2214 int mdt_llog_read_header(struct mdt_thread_info *info)
2215 {
2216         int rc;
2217
2218         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2219         rc = llog_origin_handle_read_header(mdt_info_req(info));
2220         return (rc < 0 ? err_serious(rc) : rc);
2221 }
2222
2223 int mdt_llog_next_block(struct mdt_thread_info *info)
2224 {
2225         int rc;
2226
2227         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2228         rc = llog_origin_handle_next_block(mdt_info_req(info));
2229         return (rc < 0 ? err_serious(rc) : rc);
2230 }
2231
2232 int mdt_llog_prev_block(struct mdt_thread_info *info)
2233 {
2234         int rc;
2235
2236         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2237         rc = llog_origin_handle_prev_block(mdt_info_req(info));
2238         return (rc < 0 ? err_serious(rc) : rc);
2239 }
2240
2241
2242 /*
2243  * DLM handlers.
2244  */
2245
2246 static struct ldlm_callback_suite cbs = {
2247         .lcs_completion = ldlm_server_completion_ast,
2248         .lcs_blocking   = ldlm_server_blocking_ast,
2249         .lcs_glimpse    = ldlm_server_glimpse_ast
2250 };
2251
2252 int mdt_enqueue(struct mdt_thread_info *info)
2253 {
2254         struct ptlrpc_request *req;
2255         int rc;
2256
2257         /*
2258          * info->mti_dlm_req already contains swapped and (if necessary)
2259          * converted dlm request.
2260          */
2261         LASSERT(info->mti_dlm_req != NULL);
2262
2263         req = mdt_info_req(info);
2264         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2265                                   req, info->mti_dlm_req, &cbs);
2266         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2267         return rc ? err_serious(rc) : req->rq_status;
2268 }
2269
2270 int mdt_convert(struct mdt_thread_info *info)
2271 {
2272         int rc;
2273         struct ptlrpc_request *req;
2274
2275         LASSERT(info->mti_dlm_req);
2276         req = mdt_info_req(info);
2277         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2278         return rc ? err_serious(rc) : req->rq_status;
2279 }
2280
2281 int mdt_bl_callback(struct mdt_thread_info *info)
2282 {
2283         CERROR("bl callbacks should not happen on MDS\n");
2284         LBUG();
2285         return err_serious(-EOPNOTSUPP);
2286 }
2287
2288 int mdt_cp_callback(struct mdt_thread_info *info)
2289 {
2290         CERROR("cp callbacks should not happen on MDS\n");
2291         LBUG();
2292         return err_serious(-EOPNOTSUPP);
2293 }
2294
2295 /*
2296  * sec context handlers
2297  */
2298 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2299 {
2300         int rc;
2301
2302         rc = mdt_handle_idmap(info);
2303
2304         if (unlikely(rc)) {
2305                 struct ptlrpc_request *req = mdt_info_req(info);
2306                 __u32                  opc;
2307
2308                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2309                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2310                         sptlrpc_svc_ctx_invalidate(req);
2311         }
2312
2313         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2314
2315         return rc;
2316 }
2317
2318 /*
2319  * quota request handlers
2320  */
2321 int mdt_quota_dqacq(struct mdt_thread_info *info)
2322 {
2323         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
2324         int                      rc;
2325         ENTRY;
2326
2327         if (qmt == NULL)
2328                 RETURN(err_serious(-EOPNOTSUPP));
2329
2330         rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2331         RETURN(rc);
2332 }
2333
2334 static struct mdt_object *mdt_obj(struct lu_object *o)
2335 {
2336         LASSERT(lu_device_is_mdt(o->lo_dev));
2337         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2338 }
2339
2340 struct mdt_object *mdt_object_new(const struct lu_env *env,
2341                                   struct mdt_device *d,
2342                                   const struct lu_fid *f)
2343 {
2344         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2345         struct lu_object *o;
2346         struct mdt_object *m;
2347         ENTRY;
2348
2349         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2350         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2351         if (unlikely(IS_ERR(o)))
2352                 m = (struct mdt_object *)o;
2353         else
2354                 m = mdt_obj(o);
2355         RETURN(m);
2356 }
2357
2358 struct mdt_object *mdt_object_find(const struct lu_env *env,
2359                                    struct mdt_device *d,
2360                                    const struct lu_fid *f)
2361 {
2362         struct lu_object *o;
2363         struct mdt_object *m;
2364         ENTRY;
2365
2366         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2367         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2368         if (unlikely(IS_ERR(o)))
2369                 m = (struct mdt_object *)o;
2370         else
2371                 m = mdt_obj(o);
2372         RETURN(m);
2373 }
2374
2375 /**
2376  * Asyncronous commit for mdt device.
2377  *
2378  * Pass asynchonous commit call down the MDS stack.
2379  *
2380  * \param env environment
2381  * \param mdt the mdt device
2382  */
2383 static void mdt_device_commit_async(const struct lu_env *env,
2384                                     struct mdt_device *mdt)
2385 {
2386         struct dt_device *dt = mdt->mdt_bottom;
2387         int rc;
2388
2389         rc = dt->dd_ops->dt_commit_async(env, dt);
2390         if (unlikely(rc != 0))
2391                 CWARN("async commit start failed with rc = %d", rc);
2392 }
2393
2394 /**
2395  * Mark the lock as "synchonous".
2396  *
2397  * Mark the lock to deffer transaction commit to the unlock time.
2398  *
2399  * \param lock the lock to mark as "synchonous"
2400  *
2401  * \see mdt_is_lock_sync
2402  * \see mdt_save_lock
2403  */
2404 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2405 {
2406         lock->l_ast_data = (void*)1;
2407 }
2408
2409 /**
2410  * Check whehter the lock "synchonous" or not.
2411  *
2412  * \param lock the lock to check
2413  * \retval 1 the lock is "synchonous"
2414  * \retval 0 the lock isn't "synchronous"
2415  *
2416  * \see mdt_set_lock_sync
2417  * \see mdt_save_lock
2418  */
2419 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2420 {
2421         return lock->l_ast_data != NULL;
2422 }
2423
2424 /**
2425  * Blocking AST for mdt locks.
2426  *
2427  * Starts transaction commit if in case of COS lock conflict or
2428  * deffers such a commit to the mdt_save_lock.
2429  *
2430  * \param lock the lock which blocks a request or cancelling lock
2431  * \param desc unused
2432  * \param data unused
2433  * \param flag indicates whether this cancelling or blocking callback
2434  * \retval 0
2435  * \see ldlm_blocking_ast_nocheck
2436  */
2437 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2438                      void *data, int flag)
2439 {
2440         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2441         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2442         int rc;
2443         ENTRY;
2444
2445         if (flag == LDLM_CB_CANCELING)
2446                 RETURN(0);
2447         lock_res_and_lock(lock);
2448         if (lock->l_blocking_ast != mdt_blocking_ast) {
2449                 unlock_res_and_lock(lock);
2450                 RETURN(0);
2451         }
2452         if (mdt_cos_is_enabled(mdt) &&
2453             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2454             lock->l_blocking_lock != NULL &&
2455             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2456                 mdt_set_lock_sync(lock);
2457         }
2458         rc = ldlm_blocking_ast_nocheck(lock);
2459
2460         /* There is no lock conflict if l_blocking_lock == NULL,
2461          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2462          * when the last reference to a local lock was released */
2463         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2464                 struct lu_env env;
2465
2466                 rc = lu_env_init(&env, LCT_LOCAL);
2467                 if (unlikely(rc != 0))
2468                         CWARN("lu_env initialization failed with rc = %d,"
2469                               "cannot start asynchronous commit\n", rc);
2470                 else
2471                         mdt_device_commit_async(&env, mdt);
2472                 lu_env_fini(&env);
2473         }
2474         RETURN(rc);
2475 }
2476
2477 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2478                         void *data, int flag)
2479 {
2480         struct lustre_handle lockh;
2481         int               rc;
2482
2483         switch (flag) {
2484         case LDLM_CB_BLOCKING:
2485                 ldlm_lock2handle(lock, &lockh);
2486                 rc = ldlm_cli_cancel(&lockh);
2487                 if (rc < 0) {
2488                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2489                         RETURN(rc);
2490                 }
2491                 break;
2492         case LDLM_CB_CANCELING:
2493                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2494                 break;
2495         default:
2496                 LBUG();
2497         }
2498         RETURN(0);
2499 }
2500
2501 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2502                            struct mdt_object *o, struct lustre_handle *lh,
2503                            ldlm_mode_t mode, __u64 ibits)
2504 {
2505         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2506         ldlm_policy_data_t *policy = &mti->mti_policy;
2507         int rc = 0;
2508         ENTRY;
2509
2510         LASSERT(mdt_object_remote(o));
2511
2512         LASSERT((ibits & MDS_INODELOCK_UPDATE));
2513
2514         memset(einfo, 0, sizeof(*einfo));
2515         einfo->ei_type = LDLM_IBITS;
2516         einfo->ei_mode = mode;
2517         einfo->ei_cb_bl = mdt_md_blocking_ast;
2518         einfo->ei_cb_cp = ldlm_completion_ast;
2519
2520         memset(policy, 0, sizeof(*policy));
2521         policy->l_inodebits.bits = ibits;
2522
2523         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2524                             policy);
2525         RETURN(rc);
2526 }
2527
2528 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2529                             struct mdt_lock_handle *lh, __u64 ibits,
2530                             bool nonblock, int locality)
2531 {
2532         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2533         ldlm_policy_data_t *policy = &info->mti_policy;
2534         struct ldlm_res_id *res_id = &info->mti_res_id;
2535         __u64 dlmflags;
2536         int rc;
2537         ENTRY;
2538
2539         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2540         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2541         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2542         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2543
2544         if (mdt_object_remote(o)) {
2545                 if (locality == MDT_CROSS_LOCK) {
2546                         ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2547                         ibits |= MDS_INODELOCK_LOOKUP;
2548                 } else {
2549                         LASSERTF(!(ibits &
2550                                   (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2551                                 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2552                                 mdt_obd_name(info->mti_mdt), ibits,
2553                                 PFID(mdt_object_fid(o)));
2554                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2555                 }
2556                 /* No PDO lock on remote object */
2557                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2558         }
2559
2560         if (lh->mlh_type == MDT_PDO_LOCK) {
2561                 /* check for exists after object is locked */
2562                 if (mdt_object_exists(o) == 0) {
2563                         /* Non-existent object shouldn't have PDO lock */
2564                         RETURN(-ESTALE);
2565                 } else {
2566                         /* Non-dir object shouldn't have PDO lock */
2567                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2568                                 RETURN(-ENOTDIR);
2569                 }
2570         }
2571
2572         memset(policy, 0, sizeof(*policy));
2573         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2574
2575         dlmflags = LDLM_FL_ATOMIC_CB;
2576         if (nonblock)
2577                 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2578
2579         /*
2580          * Take PDO lock on whole directory and build correct @res_id for lock
2581          * on part of directory.
2582          */
2583         if (lh->mlh_pdo_hash != 0) {
2584                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2585                 mdt_lock_pdo_mode(info, o, lh);
2586                 if (lh->mlh_pdo_mode != LCK_NL) {
2587                         /*
2588                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2589                          * is never going to be sent to client and we do not
2590                          * want it slowed down due to possible cancels.
2591                          */
2592                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2593                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2594                                           policy, res_id, dlmflags,
2595                                           &info->mti_exp->exp_handle.h_cookie);
2596                         if (unlikely(rc))
2597                                 RETURN(rc);
2598                 }
2599
2600                 /*
2601                  * Finish res_id initializing by name hash marking part of
2602                  * directory which is taking modification.
2603                  */
2604                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2605         }
2606
2607         policy->l_inodebits.bits = ibits;
2608
2609         /*
2610          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2611          * going to be sent to client. If it is - mdt_intent_policy() path will
2612          * fix it up and turn FL_LOCAL flag off.
2613          */
2614         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2615                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2616                           &info->mti_exp->exp_handle.h_cookie);
2617         if (rc)
2618                 mdt_object_unlock(info, o, lh, 1);
2619         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2620                  lh->mlh_pdo_hash != 0 &&
2621                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2622                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2623         }
2624
2625         RETURN(rc);
2626 }
2627
2628 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2629                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2630 {
2631         return mdt_object_lock0(info, o, lh, ibits, false, locality);
2632 }
2633
2634 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2635                         struct mdt_lock_handle *lh, __u64 ibits, int locality)
2636 {
2637         struct mdt_lock_handle tmp = *lh;
2638         int rc;
2639
2640         rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2641         if (rc == 0)
2642                 *lh = tmp;
2643
2644         return rc == 0;
2645 }
2646
2647 /**
2648  * Save a lock within request object.
2649  *
2650  * Keep the lock referenced until whether client ACK or transaction
2651  * commit happens or release the lock immediately depending on input
2652  * parameters. If COS is ON, a write lock is converted to COS lock
2653  * before saving.
2654  *
2655  * \param info thead info object
2656  * \param h lock handle
2657  * \param mode lock mode
2658  * \param decref force immediate lock releasing
2659  */
2660 static
2661 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2662                    ldlm_mode_t mode, int decref)
2663 {
2664         ENTRY;
2665
2666         if (lustre_handle_is_used(h)) {
2667                 if (decref || !info->mti_has_trans ||
2668                     !(mode & (LCK_PW | LCK_EX))){
2669                         mdt_fid_unlock(h, mode);
2670                 } else {
2671                         struct mdt_device *mdt = info->mti_mdt;
2672                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2673                         struct ptlrpc_request *req = mdt_info_req(info);
2674                         int no_ack = 0;
2675
2676                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2677                                  h->cookie);
2678                         CDEBUG(D_HA, "request = %p reply state = %p"
2679                                " transno = "LPD64"\n",
2680                                req, req->rq_reply_state, req->rq_transno);
2681                         if (mdt_cos_is_enabled(mdt)) {
2682                                 no_ack = 1;
2683                                 ldlm_lock_downgrade(lock, LCK_COS);
2684                                 mode = LCK_COS;
2685                         }
2686                         ptlrpc_save_lock(req, h, mode, no_ack);
2687                         if (mdt_is_lock_sync(lock)) {
2688                                 CDEBUG(D_HA, "found sync-lock,"
2689                                        " async commit started\n");
2690                                 mdt_device_commit_async(info->mti_env,
2691                                                         mdt);
2692                         }
2693                         LDLM_LOCK_PUT(lock);
2694                 }
2695                 h->cookie = 0ull;
2696         }
2697
2698         EXIT;
2699 }
2700
2701 /**
2702  * Unlock mdt object.
2703  *
2704  * Immeditely release the regular lock and the PDO lock or save the
2705  * lock in reqeuest and keep them referenced until client ACK or
2706  * transaction commit.
2707  *
2708  * \param info thread info object
2709  * \param o mdt object
2710  * \param lh mdt lock handle referencing regular and PDO locks
2711  * \param decref force immediate lock releasing
2712  */
2713 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2714                        struct mdt_lock_handle *lh, int decref)
2715 {
2716         ENTRY;
2717
2718         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2719         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2720
2721         if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2722                 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2723
2724         EXIT;
2725 }
2726
2727 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2728                                         const struct lu_fid *f,
2729                                         struct mdt_lock_handle *lh,
2730                                         __u64 ibits)
2731 {
2732         struct mdt_object *o;
2733
2734         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2735         if (!IS_ERR(o)) {
2736                 int rc;
2737
2738                 rc = mdt_object_lock(info, o, lh, ibits,
2739                                      MDT_LOCAL_LOCK);
2740                 if (rc != 0) {
2741                         mdt_object_put(info->mti_env, o);
2742                         o = ERR_PTR(rc);
2743                 }
2744         }
2745         return o;
2746 }
2747
2748 void mdt_object_unlock_put(struct mdt_thread_info * info,
2749                            struct mdt_object * o,
2750                            struct mdt_lock_handle *lh,
2751                            int decref)
2752 {
2753         mdt_object_unlock(info, o, lh, decref);
2754         mdt_object_put(info->mti_env, o);
2755 }
2756
2757 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2758 {
2759         struct mdt_opc_slice *s;
2760         struct mdt_handler   *h;
2761
2762         h = NULL;
2763         for (s = supported; s->mos_hs != NULL; s++) {
2764                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2765                         h = s->mos_hs + (opc - s->mos_opc_start);
2766                         if (likely(h->mh_opc != 0))
2767                                 LASSERTF(h->mh_opc == opc,
2768                                          "opcode mismatch %d != %d\n",
2769                                          h->mh_opc, opc);
2770                         else
2771                                 h = NULL; /* unsupported opc */
2772                         break;
2773                 }
2774         }
2775         return h;
2776 }
2777
2778 static int mdt_lock_resname_compat(struct mdt_device *m,
2779                                    struct ldlm_request *req)
2780 {
2781         /* XXX something... later. */
2782         return 0;
2783 }
2784
2785 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2786 {
2787         /* XXX something... later. */
2788         return 0;
2789 }
2790
2791 /*
2792  * Generic code handling requests that have struct mdt_body passed in:
2793  *
2794  *  - extract mdt_body from request and save it in @info, if present;
2795  *
2796  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2797  *  @info;
2798  *
2799  *  - if HABEO_CORPUS flag is set for this request type check whether object
2800  *  actually exists on storage (lu_object_exists()).
2801  *
2802  */
2803 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2804 {
2805         const struct mdt_body    *body;
2806         struct mdt_object        *obj;
2807         const struct lu_env      *env;
2808         struct req_capsule       *pill;
2809         int                       rc;
2810         ENTRY;
2811
2812         env = info->mti_env;
2813         pill = info->mti_pill;
2814
2815         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2816         if (body == NULL)
2817                 RETURN(-EFAULT);
2818
2819         if (!(body->valid & OBD_MD_FLID))
2820                 RETURN(0);
2821
2822         if (!fid_is_sane(&body->fid1)) {
2823                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2824                 RETURN(-EINVAL);
2825         }
2826
2827         /*
2828          * Do not get size or any capa fields before we check that request
2829          * contains capa actually. There are some requests which do not, for
2830          * instance MDS_IS_SUBDIR.
2831          */
2832         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2833             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2834                 mdt_set_capainfo(info, 0, &body->fid1,
2835                                  req_capsule_client_get(pill, &RMF_CAPA1));
2836
2837         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2838         if (!IS_ERR(obj)) {
2839                 if ((flags & HABEO_CORPUS) &&
2840                     !mdt_object_exists(obj)) {
2841                         mdt_object_put(env, obj);
2842                         /* for capability renew ENOENT will be handled in
2843                          * mdt_renew_capa */
2844                         if (body->valid & OBD_MD_FLOSSCAPA)
2845                                 rc = 0;
2846                         else
2847                                 rc = -ENOENT;
2848                 } else {
2849                         info->mti_object = obj;
2850                         rc = 0;
2851                 }
2852         } else
2853                 rc = PTR_ERR(obj);
2854
2855         RETURN(rc);
2856 }
2857
2858 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2859 {
2860         struct req_capsule *pill = info->mti_pill;
2861         int rc;
2862         ENTRY;
2863
2864         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2865                 rc = mdt_body_unpack(info, flags);
2866         else
2867                 rc = 0;
2868
2869         if (rc == 0 && (flags & HABEO_REFERO)) {
2870                 /* Pack reply. */
2871                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2872                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2873                                              info->mti_body->eadatasize);
2874                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2875                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2876                                              RCL_SERVER, 0);
2877
2878                 rc = req_capsule_server_pack(pill);
2879         }
2880         RETURN(rc);
2881 }
2882
2883 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2884 {
2885         struct md_device *next = m->mdt_child;
2886
2887         return next->md_ops->mdo_init_capa_ctxt(env, next,
2888                                                 m->mdt_opts.mo_mds_capa,
2889                                                 m->mdt_capa_timeout,
2890                                                 m->mdt_capa_alg,
2891                                                 m->mdt_capa_keys);
2892 }
2893
2894 /*
2895  * Invoke handler for this request opc. Also do necessary preprocessing
2896  * (according to handler ->mh_flags), and post-processing (setting of
2897  * ->last_{xid,committed}).
2898  */
2899 static int mdt_req_handle(struct mdt_thread_info *info,
2900                           struct mdt_handler *h, struct ptlrpc_request *req)
2901 {
2902         int   rc, serious = 0;
2903         __u32 flags;
2904
2905         ENTRY;
2906
2907         LASSERT(h->mh_act != NULL);
2908         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2909         LASSERT(current->journal_info == NULL);
2910
2911         /*
2912          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2913          * to put same checks into handlers like mdt_close(), mdt_reint(),
2914          * etc., without talking to mdt authors first. Checking same thing
2915          * there again is useless and returning 0 error without packing reply
2916          * is buggy! Handlers either pack reply or return error.
2917          *
2918          * We return 0 here and do not send any reply in order to emulate
2919          * network failure. Do not send any reply in case any of NET related
2920          * fail_id has occured.
2921          */
2922         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2923                 RETURN(0);
2924
2925         rc = 0;
2926         flags = h->mh_flags;
2927         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2928
2929         if (h->mh_fmt != NULL) {
2930                 req_capsule_set(info->mti_pill, h->mh_fmt);
2931                 rc = mdt_unpack_req_pack_rep(info, flags);
2932         }
2933
2934         if (rc == 0 && flags & MUTABOR &&
2935             exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2936                 /* should it be rq_status? */
2937                 rc = -EROFS;
2938
2939         if (rc == 0 && flags & HABEO_CLAVIS) {
2940                 struct ldlm_request *dlm_req;
2941
2942                 LASSERT(h->mh_fmt != NULL);
2943
2944                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2945                 if (dlm_req != NULL) {
2946                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2947                                         LDLM_IBITS &&
2948                                      dlm_req->lock_desc.l_policy_data.\
2949                                         l_inodebits.bits == 0)) {
2950                                 /*
2951                                  * Lock without inodebits makes no sense and
2952                                  * will oops later in ldlm. If client miss to
2953                                  * set such bits, do not trigger ASSERTION.
2954                                  *
2955                                  * For liblustre flock case, it maybe zero.
2956                                  */
2957                                 rc = -EPROTO;
2958                         } else {
2959                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2960                                         rc = mdt_lock_resname_compat(
2961                                                                 info->mti_mdt,
2962                                                                 dlm_req);
2963                                 info->mti_dlm_req = dlm_req;
2964                         }
2965                 } else {
2966                         rc = -EFAULT;
2967                 }
2968         }
2969
2970         /* capability setting changed via /proc, needs reinitialize ctxt */
2971         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2972                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2973                 info->mti_mdt->mdt_capa_conf = 0;
2974         }
2975
2976         if (likely(rc == 0)) {
2977                 /*
2978                  * Process request, there can be two types of rc:
2979                  * 1) errors with msg unpack/pack, other failures outside the
2980                  * operation itself. This is counted as serious errors;
2981                  * 2) errors during fs operation, should be placed in rq_status
2982                  * only
2983                  */
2984                 rc = h->mh_act(info);
2985                 if (rc == 0 &&
2986                     !req->rq_no_reply && req->rq_reply_state == NULL) {
2987                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2988                                   "pack reply and returned 0 error\n",
2989                                   h->mh_name);
2990                         LBUG();
2991                 }
2992                 serious = is_serious(rc);
2993                 rc = clear_serious(rc);
2994         } else
2995                 serious = 1;
2996
2997         req->rq_status = rc;
2998
2999         /*
3000          * ELDLM_* codes which > 0 should be in rq_status only as well as
3001          * all non-serious errors.
3002          */
3003         if (rc > 0 || !serious)
3004                 rc = 0;
3005
3006         LASSERT(current->journal_info == NULL);
3007
3008         if (rc == 0 && (flags & HABEO_CLAVIS) &&
3009             info->mti_mdt->mdt_opts.mo_compat_resname) {
3010                 struct ldlm_reply *dlmrep;
3011
3012                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3013                 if (dlmrep != NULL)
3014                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3015         }
3016
3017         /* If we're DISCONNECTing, the mdt_export_data is already freed */
3018         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3019                 target_committed_to_req(req);
3020
3021         if (unlikely(req_is_replay(req) &&
3022                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3023                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3024                 LBUG();
3025         }
3026
3027         target_send_reply(req, rc, info->mti_fail_id);
3028         RETURN(0);
3029 }
3030
3031 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3032 {
3033         lh->mlh_type = MDT_NUL_LOCK;
3034         lh->mlh_reg_lh.cookie = 0ull;
3035         lh->mlh_reg_mode = LCK_MINMODE;
3036         lh->mlh_pdo_lh.cookie = 0ull;
3037         lh->mlh_pdo_mode = LCK_MINMODE;
3038         lh->mlh_rreg_lh.cookie = 0ull;
3039         lh->mlh_rreg_mode = LCK_MINMODE;
3040 }
3041
3042 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3043 {
3044         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3045         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3046 }
3047
3048 /*
3049  * Initialize fields of struct mdt_thread_info. Other fields are left in
3050  * uninitialized state, because it's too expensive to zero out whole
3051  * mdt_thread_info (> 1K) on each request arrival.
3052  */
3053 static void mdt_thread_info_init(struct ptlrpc_request *req,
3054                                  struct mdt_thread_info *info)
3055 {
3056         int i;
3057
3058         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3059         info->mti_pill = &req->rq_pill;
3060
3061         /* lock handle */
3062         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3063                 mdt_lock_handle_init(&info->mti_lh[i]);
3064
3065         /* mdt device: it can be NULL while CONNECT */
3066         if (req->rq_export) {
3067                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3068                 info->mti_exp = req->rq_export;
3069         } else
3070                 info->mti_mdt = NULL;
3071         info->mti_env = req->rq_svc_thread->t_env;
3072         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3073         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3074         info->mti_mos = NULL;
3075
3076         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3077         info->mti_body = NULL;
3078         info->mti_object = NULL;
3079         info->mti_dlm_req = NULL;
3080         info->mti_has_trans = 0;
3081         info->mti_cross_ref = 0;
3082         info->mti_opdata = 0;
3083         info->mti_big_lmm_used = 0;
3084
3085         /* To not check for split by default. */
3086         info->mti_spec.no_create = 0;
3087 }
3088
3089 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3090 {
3091         int i;
3092
3093         req_capsule_fini(info->mti_pill);
3094         if (info->mti_object != NULL) {
3095                 mdt_object_put(info->mti_env, info->mti_object);
3096                 info->mti_object = NULL;
3097         }
3098         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3099                 mdt_lock_handle_fini(&info->mti_lh[i]);
3100         info->mti_env = NULL;
3101 }
3102
3103 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3104                                        struct obd_device *obd, int *process)
3105 {
3106         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3107         case MDS_CONNECT: /* This will never get here, but for completeness. */
3108         case OST_CONNECT: /* This will never get here, but for completeness. */
3109         case MDS_DISCONNECT:
3110         case OST_DISCONNECT:
3111         case OBD_IDX_READ:
3112                *process = 1;
3113                RETURN(0);
3114
3115         case MDS_CLOSE:
3116         case MDS_DONE_WRITING:
3117         case MDS_SYNC: /* used in unmounting */
3118         case OBD_PING:
3119         case MDS_REINT:
3120         case SEQ_QUERY:
3121         case FLD_QUERY:
3122         case LDLM_ENQUEUE:
3123                 *process = target_queue_recovery_request(req, obd);
3124                 RETURN(0);
3125
3126         default:
3127                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3128                 *process = -EAGAIN;
3129                 RETURN(0);
3130         }
3131 }
3132
3133 /*
3134  * Handle recovery. Return:
3135  *        +1: continue request processing;
3136  *       -ve: abort immediately with the given error code;
3137  *         0: send reply with error code in req->rq_status;
3138  */
3139 static int mdt_recovery(struct mdt_thread_info *info)
3140 {
3141         struct ptlrpc_request *req = mdt_info_req(info);
3142         struct obd_device *obd;
3143
3144         ENTRY;
3145
3146         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3147         case MDS_CONNECT:
3148         case SEC_CTX_INIT:
3149         case SEC_CTX_INIT_CONT:
3150         case SEC_CTX_FINI:
3151                 {
3152 #if 0
3153                         int rc;
3154
3155                         rc = mdt_handle_idmap(info);
3156                         if (rc)
3157                                 RETURN(rc);
3158                         else
3159 #endif
3160                                 RETURN(+1);
3161                 }
3162         }
3163
3164         if (unlikely(!class_connected_export(req->rq_export))) {
3165                 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3166                        lustre_msg_get_opc(req->rq_reqmsg),
3167                        libcfs_id2str(req->rq_peer));
3168                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3169                  * mds_A will get -ENOTCONN(especially for ping req),
3170                  * which will cause that mds_A deactive timeout, then when
3171                  * mds_A cleanup, the cleanup process will be suspended since
3172                  * deactive timeout is not zero.
3173                  */
3174                 req->rq_status = -ENOTCONN;
3175                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3176                 RETURN(0);
3177         }
3178
3179         /* sanity check: if the xid matches, the request must be marked as a
3180          * resent or replayed */
3181         if (req_xid_is_last(req)) {
3182                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3183                       (MSG_RESENT | MSG_REPLAY))) {
3184                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3185                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3186                                   lustre_msg_get_flags(req->rq_reqmsg));
3187                         LBUG();
3188                         req->rq_status = -ENOTCONN;
3189                         RETURN(-ENOTCONN);
3190                 }
3191         }
3192
3193         /* else: note the opposite is not always true; a RESENT req after a
3194          * failover will usually not match the last_xid, since it was likely
3195          * never committed. A REPLAYed request will almost never match the
3196          * last xid, however it could for a committed, but still retained,
3197          * open. */
3198
3199         obd = req->rq_export->exp_obd;
3200
3201         /* Check for aborted recovery... */
3202         if (unlikely(obd->obd_recovering)) {
3203                 int rc;
3204                 int should_process;
3205                 DEBUG_REQ(D_INFO, req, "Got new replay");
3206                 rc = mdt_filter_recovery_request(req, obd, &should_process);
3207                 if (rc != 0 || !should_process)
3208                         RETURN(rc);
3209                 else if (should_process < 0) {
3210                         req->rq_status = should_process;
3211                         rc = ptlrpc_error(req);
3212                         RETURN(rc);
3213                 }
3214         }
3215         RETURN(+1);
3216 }
3217
3218 static int mdt_msg_check_version(struct lustre_msg *msg)
3219 {
3220         int rc;
3221
3222         switch (lustre_msg_get_opc(msg)) {
3223         case MDS_CONNECT:
3224         case MDS_DISCONNECT:
3225         case OBD_PING:
3226         case SEC_CTX_INIT:
3227         case SEC_CTX_INIT_CONT:
3228         case SEC_CTX_FINI:
3229         case OBD_IDX_READ:
3230                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3231                 if (rc)
3232                         CERROR("bad opc %u version %08x, expecting %08x\n",
3233                                lustre_msg_get_opc(msg),
3234                                lustre_msg_get_version(msg),
3235                                LUSTRE_OBD_VERSION);
3236                 break;
3237         case MDS_GETSTATUS:
3238         case MDS_GETATTR:
3239         case MDS_GETATTR_NAME:
3240         case MDS_STATFS:
3241         case MDS_READPAGE:
3242         case MDS_WRITEPAGE:
3243         case MDS_IS_SUBDIR:
3244         case MDS_REINT:
3245         case MDS_CLOSE:
3246         case MDS_DONE_WRITING:
3247         case MDS_PIN:
3248         case MDS_SYNC:
3249         case MDS_GETXATTR:
3250         case MDS_SETXATTR:
3251         case MDS_SET_INFO:
3252         case MDS_GET_INFO:
3253         case MDS_HSM_PROGRESS:
3254         case MDS_HSM_REQUEST:
3255         case MDS_HSM_CT_REGISTER:
3256         case MDS_HSM_CT_UNREGISTER:
3257         case MDS_HSM_STATE_GET:
3258         case MDS_HSM_STATE_SET:
3259         case MDS_HSM_ACTION:
3260         case MDS_QUOTACHECK:
3261         case MDS_QUOTACTL:
3262         case UPDATE_OBJ:
3263         case MDS_SWAP_LAYOUTS:
3264         case QUOTA_DQACQ:
3265         case QUOTA_DQREL:
3266         case SEQ_QUERY:
3267         case FLD_QUERY:
3268                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3269                 if (rc)
3270                         CERROR("bad opc %u version %08x, expecting %08x\n",
3271                                lustre_msg_get_opc(msg),
3272                                lustre_msg_get_version(msg),
3273                                LUSTRE_MDS_VERSION);
3274                 break;
3275         case LDLM_ENQUEUE:
3276         case LDLM_CONVERT:
3277         case LDLM_BL_CALLBACK:
3278         case LDLM_CP_CALLBACK:
3279                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3280                 if (rc)
3281                         CERROR("bad opc %u version %08x, expecting %08x\n",
3282                                lustre_msg_get_opc(msg),
3283                                lustre_msg_get_version(msg),
3284                                LUSTRE_DLM_VERSION);
3285                 break;
3286         case OBD_LOG_CANCEL:
3287         case LLOG_ORIGIN_HANDLE_CREATE:
3288         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3289         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3290         case LLOG_ORIGIN_HANDLE_CLOSE:
3291         case LLOG_ORIGIN_HANDLE_DESTROY:
3292         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3293         case LLOG_CATINFO:
3294                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3295                 if (rc)
3296                         CERROR("bad opc %u version %08x, expecting %08x\n",
3297                                lustre_msg_get_opc(msg),
3298                                lustre_msg_get_version(msg),
3299                                LUSTRE_LOG_VERSION);
3300                 break;
3301         default:
3302                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3303                 rc = -ENOTSUPP;
3304         }
3305         return rc;
3306 }
3307
3308 static int mdt_handle0(struct ptlrpc_request *req,
3309                        struct mdt_thread_info *info,
3310                        struct mdt_opc_slice *supported)
3311 {
3312         struct mdt_handler *h;
3313         struct lustre_msg  *msg;
3314         int                 rc;
3315
3316         ENTRY;
3317
3318         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3319                 RETURN(0);
3320
3321         LASSERT(current->journal_info == NULL);
3322
3323         msg = req->rq_reqmsg;
3324         rc = mdt_msg_check_version(msg);
3325         if (likely(rc == 0)) {
3326                 rc = mdt_recovery(info);
3327                 if (likely(rc == +1)) {
3328                         h = mdt_handler_find(lustre_msg_get_opc(msg),
3329                                              supported);
3330                         if (likely(h != NULL)) {
3331                                 rc = mdt_req_handle(info, h, req);
3332                         } else {
3333                                 CERROR("The unsupported opc: 0x%x\n",
3334                                        lustre_msg_get_opc(msg) );
3335                                 req->rq_status = -ENOTSUPP;
3336                                 rc = ptlrpc_error(req);
3337                                 RETURN(rc);
3338                         }
3339                 }
3340         } else
3341                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3342         RETURN(rc);
3343 }
3344
3345 /*
3346  * MDT handler function called by ptlrpc service thread when request comes.
3347  *
3348  * XXX common "target" functionality should be factored into separate module
3349  * shared by mdt, ost and stand-alone services like fld.
3350  */
3351 int mdt_handle_common(struct ptlrpc_request *req,
3352                       struct mdt_opc_slice *supported)
3353 {
3354         struct lu_env          *env;
3355         struct mdt_thread_info *info;
3356         int                     rc;
3357         ENTRY;
3358
3359         env = req->rq_svc_thread->t_env;
3360         /* Refill(initilize) the context(mdt_thread_info), in case it is
3361          * not initialized yet. Usually it happens during start up, after
3362          * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3363          * before MDT_thread_info is initialized */
3364         lu_env_refill(env);
3365         LASSERT(env != NULL);
3366         LASSERT(env->le_ses != NULL);
3367         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3368         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3369         LASSERT(info != NULL);
3370
3371         mdt_thread_info_init(req, info);
3372
3373         rc = mdt_handle0(req, info, supported);
3374
3375         mdt_thread_info_fini(info);
3376         RETURN(rc);
3377 }
3378
3379 /*
3380  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3381  * as well.
3382  */
3383 int mdt_recovery_handle(struct ptlrpc_request *req)
3384 {
3385         int rc;
3386         ENTRY;
3387
3388         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3389         case FLD_QUERY:
3390                 rc = mdt_handle_common(req, mdt_fld_handlers);
3391                 break;
3392         case SEQ_QUERY:
3393                 rc = mdt_handle_common(req, mdt_seq_handlers);
3394                 break;
3395         default:
3396                 rc = mdt_handle_common(req, mdt_regular_handlers);
3397                 break;
3398         }
3399
3400         RETURN(rc);
3401 }
3402
3403 enum mdt_it_code {
3404         MDT_IT_OPEN,
3405         MDT_IT_OCREAT,
3406         MDT_IT_CREATE,
3407         MDT_IT_GETATTR,
3408         MDT_IT_READDIR,
3409         MDT_IT_LOOKUP,
3410         MDT_IT_UNLINK,
3411         MDT_IT_TRUNC,
3412         MDT_IT_GETXATTR,
3413         MDT_IT_LAYOUT,
3414         MDT_IT_QUOTA,
3415         MDT_IT_NR
3416 };
3417
3418 static int mdt_intent_getattr(enum mdt_it_code opcode,
3419                               struct mdt_thread_info *info,
3420                               struct ldlm_lock **,
3421                               __u64);
3422 static int mdt_intent_layout(enum mdt_it_code opcode,
3423                              struct mdt_thread_info *info,
3424                              struct ldlm_lock **,
3425                              __u64);
3426 static int mdt_intent_reint(enum mdt_it_code opcode,
3427                             struct mdt_thread_info *info,
3428                             struct ldlm_lock **,
3429                             __u64);
3430
3431 static struct mdt_it_flavor {
3432         const struct req_format *it_fmt;
3433         __u32                    it_flags;
3434         int                    (*it_act)(enum mdt_it_code ,
3435                                          struct mdt_thread_info *,
3436                                          struct ldlm_lock **,
3437                                          __u64);
3438         long                     it_reint;
3439 } mdt_it_flavor[] = {
3440         [MDT_IT_OPEN]     = {
3441                 .it_fmt   = &RQF_LDLM_INTENT,
3442                 /*.it_flags = HABEO_REFERO,*/
3443                 .it_flags = 0,
3444                 .it_act   = mdt_intent_reint,
3445                 .it_reint = REINT_OPEN
3446         },
3447         [MDT_IT_OCREAT]   = {
3448                 .it_fmt   = &RQF_LDLM_INTENT,
3449                 .it_flags = MUTABOR,
3450                 .it_act   = mdt_intent_reint,
3451                 .it_reint = REINT_OPEN
3452         },
3453         [MDT_IT_CREATE]   = {
3454                 .it_fmt   = &RQF_LDLM_INTENT,
3455                 .it_flags = MUTABOR,
3456                 .it_act   = mdt_intent_reint,
3457                 .it_reint = REINT_CREATE
3458         },
3459         [MDT_IT_GETATTR]  = {
3460                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3461                 .it_flags = HABEO_REFERO,
3462                 .it_act   = mdt_intent_getattr
3463         },
3464         [MDT_IT_READDIR]  = {
3465                 .it_fmt   = NULL,
3466                 .it_flags = 0,
3467                 .it_act   = NULL
3468         },
3469         [MDT_IT_LOOKUP]   = {
3470                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3471                 .it_flags = HABEO_REFERO,
3472                 .it_act   = mdt_intent_getattr
3473         },
3474         [MDT_IT_UNLINK]   = {
3475                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3476                 .it_flags = MUTABOR,
3477                 .it_act   = NULL,
3478                 .it_reint = REINT_UNLINK
3479         },
3480         [MDT_IT_TRUNC]    = {
3481                 .it_fmt   = NULL,
3482                 .it_flags = MUTABOR,
3483                 .it_act   = NULL
3484         },
3485         [MDT_IT_GETXATTR] = {
3486                 .it_fmt   = NULL,
3487                 .it_flags = 0,
3488                 .it_act   = NULL
3489         },
3490         [MDT_IT_LAYOUT] = {
3491                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
3492                 .it_flags = 0,
3493                 .it_act   = mdt_intent_layout
3494         }
3495 };
3496
3497 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3498                             struct ldlm_lock **lockp,
3499                             struct ldlm_lock *new_lock,
3500                             struct mdt_lock_handle *lh,
3501                             __u64 flags)
3502 {
3503         struct ptlrpc_request  *req = mdt_info_req(info);
3504         struct ldlm_lock       *lock = *lockp;
3505
3506         /*
3507          * Get new lock only for cases when possible resent did not find any
3508          * lock.
3509          */
3510         if (new_lock == NULL)
3511                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3512
3513         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3514                 lh->mlh_reg_lh.cookie = 0;
3515                 RETURN(0);
3516         }
3517
3518         LASSERTF(new_lock != NULL,
3519                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3520
3521         /*
3522          * If we've already given this lock to a client once, then we should
3523          * have no readers or writers.  Otherwise, we should have one reader
3524          * _or_ writer ref (which will be zeroed below) before returning the
3525          * lock to a client.
3526          */
3527         if (new_lock->l_export == req->rq_export) {
3528                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3529         } else {
3530                 LASSERT(new_lock->l_export == NULL);
3531                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3532         }
3533
3534         *lockp = new_lock;
3535
3536         if (new_lock->l_export == req->rq_export) {
3537                 /*
3538                  * Already gave this to the client, which means that we
3539                  * reconstructed a reply.
3540                  */
3541                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3542                         MSG_RESENT);
3543                 lh->mlh_reg_lh.cookie = 0;
3544                 RETURN(ELDLM_LOCK_REPLACED);
3545         }
3546
3547         /*
3548          * Fixup the lock to be given to the client.
3549          */
3550         lock_res_and_lock(new_lock);
3551         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3552          * possible blocking AST. */
3553         while (new_lock->l_readers > 0) {
3554                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3555                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3556                 new_lock->l_readers--;
3557         }
3558         while (new_lock->l_writers > 0) {
3559                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3560                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3561                 new_lock->l_writers--;
3562         }
3563
3564         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3565         new_lock->l_blocking_ast = lock->l_blocking_ast;
3566         new_lock->l_completion_ast = lock->l_completion_ast;
3567         new_lock->l_remote_handle = lock->l_remote_handle;
3568         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3569
3570         unlock_res_and_lock(new_lock);
3571
3572         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3573                      &new_lock->l_remote_handle,
3574                      &new_lock->l_exp_hash);
3575
3576         LDLM_LOCK_RELEASE(new_lock);
3577         lh->mlh_reg_lh.cookie = 0;
3578
3579         RETURN(ELDLM_LOCK_REPLACED);
3580 }
3581
3582 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3583                                     struct ldlm_lock *new_lock,
3584                                     struct ldlm_lock **old_lock,
3585                                     struct mdt_lock_handle *lh)
3586 {
3587         struct ptlrpc_request  *req = mdt_info_req(info);
3588         struct obd_export      *exp = req->rq_export;
3589         struct lustre_handle    remote_hdl;
3590         struct ldlm_request    *dlmreq;
3591         struct ldlm_lock       *lock;
3592
3593         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3594                 return;
3595
3596         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3597         remote_hdl = dlmreq->lock_handle[0];
3598
3599         /* In the function below, .hs_keycmp resolves to
3600          * ldlm_export_lock_keycmp() */
3601         /* coverity[overrun-buffer-val] */
3602         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3603         if (lock) {
3604                 if (lock != new_lock) {
3605                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3606                         lh->mlh_reg_mode = lock->l_granted_mode;
3607
3608                         LDLM_DEBUG(lock, "Restoring lock cookie");
3609                         DEBUG_REQ(D_DLMTRACE, req,
3610                                   "restoring lock cookie "LPX64,
3611                                   lh->mlh_reg_lh.cookie);
3612                         if (old_lock)
3613                                 *old_lock = LDLM_LOCK_GET(lock);
3614                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3615                         return;
3616                 }
3617
3618                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3619         }
3620
3621         /*
3622          * If the xid matches, then we know this is a resent request, and allow
3623          * it. (It's probably an OPEN, for which we don't send a lock.
3624          */
3625         if (req_xid_is_last(req))
3626                 return;
3627
3628         /*
3629          * This remote handle isn't enqueued, so we never received or processed
3630          * this request.  Clear MSG_RESENT, because it can be handled like any
3631          * normal request now.
3632          */
3633         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3634
3635         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3636                   remote_hdl.cookie);
3637 }
3638
3639 static int mdt_intent_getattr(enum mdt_it_code opcode,
3640                               struct mdt_thread_info *info,
3641                               struct ldlm_lock **lockp,
3642                               __u64 flags)
3643 {
3644         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3645         struct ldlm_lock       *new_lock = NULL;
3646         __u64                   child_bits;
3647         struct ldlm_reply      *ldlm_rep;
3648         struct ptlrpc_request  *req;
3649         struct mdt_body        *reqbody;
3650         struct mdt_body        *repbody;
3651         int                     rc, rc2;
3652         ENTRY;
3653
3654         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3655         LASSERT(reqbody);
3656
3657         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3658         LASSERT(repbody);
3659
3660         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3661         repbody->eadatasize = 0;
3662         repbody->aclsize = 0;
3663
3664         switch (opcode) {
3665         case MDT_IT_LOOKUP:
3666                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3667                 break;
3668         case MDT_IT_GETATTR:
3669                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3670                              MDS_INODELOCK_PERM;
3671                 break;
3672         default:
3673                 CERROR("Unsupported intent (%d)\n", opcode);
3674                 GOTO(out_shrink, rc = -EINVAL);
3675         }
3676
3677         rc = mdt_init_ucred(info, reqbody);
3678         if (rc)
3679                 GOTO(out_shrink, rc);
3680
3681         req = info->mti_pill->rc_req;
3682         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3683         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3684
3685         /* Get lock from request for possible resent case. */
3686         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3687
3688         ldlm_rep->lock_policy_res2 =
3689                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3690
3691         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3692                 ldlm_rep->lock_policy_res2 = 0;
3693         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3694             ldlm_rep->lock_policy_res2) {
3695                 lhc->mlh_reg_lh.cookie = 0ull;
3696                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3697         }
3698
3699         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3700         EXIT;
3701 out_ucred:
3702         mdt_exit_ucred(info);
3703 out_shrink:
3704         mdt_client_compatibility(info);
3705         rc2 = mdt_fix_reply(info);
3706         if (rc == 0)
3707                 rc = rc2;
3708         return rc;
3709 }
3710
3711 static int mdt_intent_layout(enum mdt_it_code opcode,
3712                              struct mdt_thread_info *info,
3713                              struct ldlm_lock **lockp,
3714                              __u64 flags)
3715 {
3716         struct layout_intent *layout;
3717         int rc;
3718         ENTRY;
3719
3720         if (opcode != MDT_IT_LAYOUT) {
3721                 CERROR("%s: Unknown intent (%d)\n",
3722                         info->mti_exp->exp_obd->obd_name, opcode);
3723                 RETURN(-EINVAL);
3724         }
3725
3726         (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3727         req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3728                         ldlm_lvbo_size(*lockp));
3729         rc = req_capsule_server_pack(info->mti_pill);
3730         if (rc != 0)
3731                 RETURN(-EINVAL);
3732
3733         layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3734         LASSERT(layout != NULL);
3735         if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3736                 /* return to normal ldlm handling */
3737                 RETURN(0);
3738
3739         CERROR("%s: Unsupported layout intent (%d)\n",
3740                 info->mti_exp->exp_obd->obd_name, layout->li_opc);
3741         RETURN(-EINVAL);
3742 }
3743
3744 static int mdt_intent_reint(enum mdt_it_code opcode,
3745                             struct mdt_thread_info *info,
3746                             struct ldlm_lock **lockp,
3747                             __u64 flags)
3748 {
3749         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3750         struct ldlm_reply      *rep = NULL;
3751         long                    opc;
3752         int                     rc;
3753
3754         static const struct req_format *intent_fmts[REINT_MAX] = {
3755                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3756                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3757         };
3758
3759         ENTRY;
3760
3761         opc = mdt_reint_opcode(info, intent_fmts);
3762         if (opc < 0)
3763                 RETURN(opc);
3764
3765         if (mdt_it_flavor[opcode].it_reint != opc) {
3766                 CERROR("Reint code %ld doesn't match intent: %d\n",
3767                        opc, opcode);
3768                 RETURN(err_serious(-EPROTO));
3769         }
3770
3771         /* Get lock from request for possible resent case. */
3772         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3773
3774         rc = mdt_reint_internal(info, lhc, opc);
3775
3776         /* Check whether the reply has been packed successfully. */
3777         if (mdt_info_req(info)->rq_repmsg != NULL)
3778                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3779         if (rep == NULL)
3780                 RETURN(err_serious(-EFAULT));
3781
3782         /* MDC expects this in any case */
3783         if (rc != 0)
3784                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3785
3786         /* Cross-ref case, the lock should be returned to the client */
3787         if (rc == -EREMOTE) {
3788                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3789                 rep->lock_policy_res2 = 0;
3790                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3791                 RETURN(rc);
3792         }
3793         rep->lock_policy_res2 = clear_serious(rc);
3794
3795         if (rep->lock_policy_res2 == -ENOENT &&
3796             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3797                 rep->lock_policy_res2 = 0;
3798
3799         if (rc == -ENOTCONN || rc == -ENODEV ||
3800             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3801                 /*
3802                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3803                  * will be returned by rq_status, and client at ptlrpc layer
3804                  * will detect this, then disconnect, reconnect the import
3805                  * immediately, instead of impacting the following the rpc.
3806                  */
3807                 lhc->mlh_reg_lh.cookie = 0ull;
3808                 RETURN(rc);
3809         } else {
3810                 /*
3811                  * For other cases, the error will be returned by intent.
3812                  * and client will retrieve the result from intent.
3813                  */
3814                  /*
3815                   * FIXME: when open lock is finished, that should be
3816                   * checked here.
3817                   */
3818                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3819                         LASSERTF(rc == 0, "Error occurred but lock handle "
3820                                  "is still in use, rc = %d\n", rc);
3821                         rep->lock_policy_res2 = 0;
3822                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3823                         RETURN(rc);
3824                 } else {
3825                         lhc->mlh_reg_lh.cookie = 0ull;
3826                         RETURN(ELDLM_LOCK_ABORTED);
3827                 }
3828         }
3829 }
3830
3831 static int mdt_intent_code(long itcode)
3832 {
3833         int rc;
3834
3835         switch(itcode) {
3836         case IT_OPEN:
3837                 rc = MDT_IT_OPEN;
3838                 break;
3839         case IT_OPEN|IT_CREAT:
3840                 rc = MDT_IT_OCREAT;
3841                 break;
3842         case IT_CREAT:
3843                 rc = MDT_IT_CREATE;
3844                 break;
3845         case IT_READDIR:
3846                 rc = MDT_IT_READDIR;
3847                 break;
3848         case IT_GETATTR:
3849                 rc = MDT_IT_GETATTR;
3850                 break;
3851         case IT_LOOKUP:
3852                 rc = MDT_IT_LOOKUP;
3853                 break;
3854         case IT_UNLINK:
3855                 rc = MDT_IT_UNLINK;
3856                 break;
3857         case IT_TRUNC:
3858                 rc = MDT_IT_TRUNC;
3859                 break;
3860         case IT_GETXATTR:
3861                 rc = MDT_IT_GETXATTR;
3862                 break;
3863         case IT_LAYOUT:
3864                 rc = MDT_IT_LAYOUT;
3865                 break;
3866         case IT_QUOTA_DQACQ:
3867         case IT_QUOTA_CONN:
3868                 rc = MDT_IT_QUOTA;
3869                 break;
3870         default:
3871                 CERROR("Unknown intent opcode: %ld\n", itcode);
3872                 rc = -EINVAL;
3873                 break;
3874         }
3875         return rc;
3876 }
3877
3878 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3879                           struct ldlm_lock **lockp, __u64 flags)
3880 {
3881         struct req_capsule   *pill;
3882         struct mdt_it_flavor *flv;
3883         int opc;
3884         int rc;
3885         ENTRY;
3886
3887         opc = mdt_intent_code(itopc);
3888         if (opc < 0)
3889                 RETURN(-EINVAL);
3890
3891         pill = info->mti_pill;
3892
3893         if (opc == MDT_IT_QUOTA) {
3894                 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3895
3896                 if (qmt == NULL)
3897                         RETURN(-EOPNOTSUPP);
3898
3899                 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3900                 /* pass the request to quota master */
3901                 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3902                                                  mdt_info_req(info), lockp,
3903                                                  flags);
3904                 RETURN(rc);
3905         }
3906
3907         flv  = &mdt_it_flavor[opc];
3908         if (flv->it_fmt != NULL)
3909                 req_capsule_extend(pill, flv->it_fmt);
3910
3911         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3912         if (rc == 0) {
3913                 struct ptlrpc_request *req = mdt_info_req(info);
3914                 if (flv->it_flags & MUTABOR &&
3915                     exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3916                         RETURN(-EROFS);
3917         }
3918         if (rc == 0 && flv->it_act != NULL) {
3919                 /* execute policy */
3920                 rc = flv->it_act(opc, info, lockp, flags);
3921         } else {
3922                 rc = -EOPNOTSUPP;
3923         }
3924         RETURN(rc);
3925 }
3926
3927 static int mdt_intent_policy(struct ldlm_namespace *ns,
3928                              struct ldlm_lock **lockp, void *req_cookie,
3929                              ldlm_mode_t mode, __u64 flags, void *data)
3930 {
3931         struct mdt_thread_info *info;
3932         struct ptlrpc_request  *req  =  req_cookie;
3933         struct ldlm_intent     *it;
3934         struct req_capsule     *pill;
3935         int rc;
3936
3937         ENTRY;
3938
3939         LASSERT(req != NULL);
3940
3941         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3942                                   &mdt_thread_key);
3943         LASSERT(info != NULL);
3944         pill = info->mti_pill;
3945         LASSERT(pill->rc_req == req);
3946
3947         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3948                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3949                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3950                 if (it != NULL) {
3951                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3952                         if (rc == 0)
3953                                 rc = ELDLM_OK;
3954
3955                         /* Lock without inodebits makes no sense and will oops
3956                          * later in ldlm. Let's check it now to see if we have
3957                          * ibits corrupted somewhere in mdt_intent_opc().
3958                          * The case for client miss to set ibits has been
3959                          * processed by others. */
3960                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3961                                         lr_type == LDLM_IBITS,
3962                                      info->mti_dlm_req->lock_desc.\
3963                                         l_policy_data.l_inodebits.bits != 0));
3964                 } else
3965                         rc = err_serious(-EFAULT);
3966         } else {
3967                 /* No intent was provided */
3968                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3969                 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3970                 rc = req_capsule_server_pack(pill);
3971                 if (rc)
3972                         rc = err_serious(rc);
3973         }
3974         RETURN(rc);
3975 }
3976
3977 static int mdt_seq_fini(const struct lu_env *env,
3978                         struct mdt_device *m)
3979 {
3980         return seq_site_fini(env, mdt_seq_site(m));
3981 }
3982
3983 static int mdt_seq_init(const struct lu_env *env,
3984                         const char *uuid,
3985                         struct mdt_device *m)
3986 {
3987         struct seq_server_site *ss;
3988         char *prefix;
3989         int rc;
3990         ENTRY;
3991
3992         ss = mdt_seq_site(m);
3993
3994         /*
3995          * This is sequence-controller node. Init seq-controller server on local
3996          * MDT.
3997          */
3998         if (ss->ss_node_id == 0) {
3999                 LASSERT(ss->ss_control_seq == NULL);
4000
4001                 OBD_ALLOC_PTR(ss->ss_control_seq);
4002                 if (ss->ss_control_seq == NULL)
4003                         RETURN(-ENOMEM);
4004
4005                 rc = seq_server_init(ss->ss_control_seq,
4006                                      m->mdt_bottom, uuid,
4007                                      LUSTRE_SEQ_CONTROLLER,
4008                                      ss,
4009                                      env);
4010
4011                 if (rc)
4012                         GOTO(out_seq_fini, rc);
4013
4014                 OBD_ALLOC_PTR(ss->ss_client_seq);
4015                 if (ss->ss_client_seq == NULL)
4016                         GOTO(out_seq_fini, rc = -ENOMEM);
4017
4018                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4019                 if (prefix == NULL) {
4020                         OBD_FREE_PTR(ss->ss_client_seq);
4021                         GOTO(out_seq_fini, rc = -ENOMEM);
4022                 }
4023
4024                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4025                          uuid);
4026
4027                 /*
4028                  * Init seq-controller client after seq-controller server is
4029                  * ready. Pass ss->ss_control_seq to it for direct talking.
4030                  */
4031                 rc = seq_client_init(ss->ss_client_seq, NULL,
4032                                      LUSTRE_SEQ_METADATA, prefix,
4033                                      ss->ss_control_seq);
4034                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4035
4036                 if (rc)
4037                         GOTO(out_seq_fini, rc);
4038         }
4039
4040         /* Init seq-server on local MDT */
4041         LASSERT(ss->ss_server_seq == NULL);
4042
4043         OBD_ALLOC_PTR(ss->ss_server_seq);
4044         if (ss->ss_server_seq == NULL)
4045                 GOTO(out_seq_fini, rc = -ENOMEM);
4046
4047         rc = seq_server_init(ss->ss_server_seq,
4048                              m->mdt_bottom, uuid,
4049                              LUSTRE_SEQ_SERVER,
4050                              ss,
4051                              env);
4052         if (rc)
4053                 GOTO(out_seq_fini, rc = -ENOMEM);
4054
4055         /* Assign seq-controller client to local seq-server. */
4056         if (ss->ss_node_id == 0) {
4057                 LASSERT(ss->ss_client_seq != NULL);
4058
4059                 rc = seq_server_set_cli(ss->ss_server_seq,
4060                                         ss->ss_client_seq,
4061                                         env);
4062         }
4063
4064         EXIT;
4065 out_seq_fini:
4066         if (rc)
4067                 mdt_seq_fini(env, m);
4068
4069         return rc;
4070 }
4071
4072 /*
4073  * FLD wrappers
4074  */
4075 static int mdt_fld_fini(const struct lu_env *env,
4076                         struct mdt_device *m)
4077 {
4078         struct seq_server_site *ss = mdt_seq_site(m);
4079         ENTRY;
4080
4081         if (ss && ss->ss_server_fld) {
4082                 fld_server_fini(env, ss->ss_server_fld);
4083                 OBD_FREE_PTR(ss->ss_server_fld);
4084                 ss->ss_server_fld = NULL;
4085         }
4086
4087         RETURN(0);
4088 }
4089
4090 static int mdt_fld_init(const struct lu_env *env,
4091                         const char *uuid,
4092                         struct mdt_device *m)
4093 {
4094         struct seq_server_site *ss;
4095         int rc;
4096         ENTRY;
4097
4098         ss = mdt_seq_site(m);
4099
4100         OBD_ALLOC_PTR(ss->ss_server_fld);
4101         if (ss->ss_server_fld == NULL)
4102                 RETURN(rc = -ENOMEM);
4103
4104         rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4105                              ss->ss_node_id, LU_SEQ_RANGE_MDT);
4106         if (rc) {
4107                 OBD_FREE_PTR(ss->ss_server_fld);
4108                 ss->ss_server_fld = NULL;
4109                 RETURN(rc);
4110         }
4111
4112         RETURN(0);
4113 }
4114
4115 static void mdt_stack_pre_fini(const struct lu_env *env,
4116                            struct mdt_device *m, struct lu_device *top)
4117 {
4118         struct obd_device       *obd = mdt2obd_dev(m);
4119         struct lustre_cfg_bufs  *bufs;
4120         struct lustre_cfg       *lcfg;
4121         struct mdt_thread_info  *info;
4122         ENTRY;
4123
4124         LASSERT(top);
4125
4126         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4127         LASSERT(info != NULL);
4128
4129         bufs = &info->mti_u.bufs;
4130
4131         LASSERT(m->mdt_child_exp);
4132         LASSERT(m->mdt_child_exp->exp_obd);
4133         obd = m->mdt_child_exp->exp_obd;
4134
4135         /* process cleanup, pass mdt obd name to get obd umount flags */
4136         /* XXX: this is needed because all layers are referenced by
4137          * objects (some of them are pinned by osd, for example *
4138          * the proper solution should be a model where object used
4139          * by osd only doesn't have mdt/mdd slices -bzzz */
4140         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4141         lustre_cfg_bufs_set_string(bufs, 1, NULL);
4142         lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4143         if (!lcfg) {
4144                 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4145                 return;
4146         }
4147         top->ld_ops->ldo_process_config(env, top, lcfg);
4148         lustre_cfg_free(lcfg);
4149         EXIT;
4150 }
4151
4152 static void mdt_stack_fini(const struct lu_env *env,
4153                            struct mdt_device *m, struct lu_device *top)
4154 {
4155         struct obd_device       *obd = mdt2obd_dev(m);
4156         struct lustre_cfg_bufs  *bufs;
4157         struct lustre_cfg       *lcfg;
4158         struct mdt_thread_info  *info;
4159         char flags[3]="";
4160         ENTRY;
4161
4162         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4163         LASSERT(info != NULL);
4164
4165         lu_dev_del_linkage(top->ld_site, top);
4166
4167         lu_site_purge(env, top->ld_site, -1);
4168
4169         bufs = &info->mti_u.bufs;
4170         /* process cleanup, pass mdt obd name to get obd umount flags */
4171         /* another purpose is to let all layers to release their objects */
4172         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4173         if (obd->obd_force)
4174                 strcat(flags, "F");
4175         if (obd->obd_fail)
4176                 strcat(flags, "A");
4177         lustre_cfg_bufs_set_string(bufs, 1, flags);
4178         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4179         if (!lcfg) {
4180                 CERROR("Cannot alloc lcfg!\n");
4181                 return;
4182         }
4183         LASSERT(top);
4184         top->ld_ops->ldo_process_config(env, top, lcfg);
4185         lustre_cfg_free(lcfg);
4186
4187         lu_site_purge(env, top->ld_site, -1);
4188
4189         m->mdt_child = NULL;
4190         m->mdt_bottom = NULL;
4191
4192         obd_disconnect(m->mdt_child_exp);
4193         m->mdt_child_exp = NULL;
4194
4195         obd_disconnect(m->mdt_bottom_exp);
4196         m->mdt_child_exp = NULL;
4197 }
4198
4199 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4200                                const char *next, struct obd_export **exp)
4201 {
4202         struct obd_connect_data *data = NULL;
4203         struct obd_device       *obd;
4204         int                      rc;
4205         ENTRY;
4206
4207         OBD_ALLOC_PTR(data);
4208         if (data == NULL)
4209                 GOTO(out, rc = -ENOMEM);
4210
4211         obd = class_name2obd(next);
4212         if (obd == NULL) {
4213                 CERROR("%s: can't locate next device: %s\n",
4214                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4215                 GOTO(out, rc = -ENOTCONN);
4216         }
4217
4218         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4219         data->ocd_version = LUSTRE_VERSION_CODE;
4220
4221         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4222         if (rc) {
4223                 CERROR("%s: cannot connect to next dev %s (%d)\n",
4224                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4225                 GOTO(out, rc);
4226         }
4227
4228 out:
4229         if (data)
4230                 OBD_FREE_PTR(data);
4231         RETURN(rc);
4232 }
4233
4234 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4235                           struct lustre_cfg *cfg)
4236 {
4237         char                   *dev = lustre_cfg_string(cfg, 0);
4238         int                     rc, name_size, uuid_size;
4239         char                   *name, *uuid, *p;
4240         struct lustre_cfg_bufs *bufs;
4241         struct lustre_cfg      *lcfg;
4242         struct obd_device      *obd;
4243         struct lustre_profile  *lprof;
4244         struct lu_site         *site;
4245         ENTRY;
4246
4247         /* in 1.8 we had the only device in the stack - MDS.
4248          * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4249          * in 2.3 OSD is instantiated by obd_mount.c, so we need
4250          * to generate names and setup MDT, MDD. MDT will be using
4251          * generated name to connect to MDD. for MDD the next device
4252          * will be LOD with name taken from so called "profile" which
4253          * is generated by mount_option line
4254          *
4255          * 1.8 MGS generates config. commands like this:
4256          *   #06 (104)mount_option 0:  1:lustre-MDT0000  2:lustre-mdtlov
4257          *   #08 (120)setup   0:lustre-MDT0000  1:dev 2:type 3:lustre-MDT0000
4258          * 2.0 MGS generates config. commands like this:
4259          *   #07 (112)mount_option 0:  1:lustre-MDT0000  2:lustre-MDT0000-mdtlov
4260          *   #08 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4261          *                    3:lustre-MDT0000-mdtlov  4:f
4262          *
4263          * we generate MDD name from MDT one, just replacing T with D
4264          *
4265          * after all the preparations, the logical equivalent will be
4266          *   #01 (160)setup   0:lustre-MDD0000  1:lustre-MDD0000_UUID  2:0
4267          *                    3:lustre-MDT0000-mdtlov  4:f
4268          *   #02 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4269          *                    3:lustre-MDD0000  4:f
4270          *
4271          *  notice we build the stack from down to top: MDD first, then MDT */
4272
4273         name_size = MAX_OBD_NAME;
4274         uuid_size = MAX_OBD_NAME;
4275
4276         OBD_ALLOC(name, name_size);
4277         OBD_ALLOC(uuid, uuid_size);
4278         if (name == NULL || uuid == NULL)
4279                 GOTO(cleanup_mem, rc = -ENOMEM);
4280
4281         OBD_ALLOC_PTR(bufs);
4282         if (!bufs)
4283                 GOTO(cleanup_mem, rc = -ENOMEM);
4284
4285         strcpy(name, dev);
4286         p = strstr(name, "-MDT");
4287         if (p == NULL)
4288                 GOTO(cleanup_mem, rc = -ENOMEM);
4289         p[3] = 'D';
4290
4291         snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4292
4293         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4294         if (lprof == NULL || lprof->lp_dt == NULL) {
4295                 CERROR("can't find the profile: %s\n",
4296                        lustre_cfg_string(cfg, 0));
4297                 GOTO(cleanup_mem, rc = -EINVAL);
4298         }
4299
4300         lustre_cfg_bufs_reset(bufs, name);
4301         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4302         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4303         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4304
4305         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4306         if (!lcfg)
4307                 GOTO(free_bufs, rc = -ENOMEM);
4308
4309         rc = class_attach(lcfg);
4310         if (rc)
4311                 GOTO(lcfg_cleanup, rc);
4312
4313         obd = class_name2obd(name);
4314         if (!obd) {
4315                 CERROR("Can not find obd %s (%s in config)\n",
4316                        MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4317                 GOTO(class_detach, rc = -EINVAL);
4318         }
4319
4320         lustre_cfg_free(lcfg);
4321
4322         lustre_cfg_bufs_reset(bufs, name);
4323         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4324         lustre_cfg_bufs_set_string(bufs, 2, dev);
4325         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4326
4327         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4328
4329         rc = class_setup(obd, lcfg);
4330         if (rc)
4331                 GOTO(class_detach, rc);
4332
4333         /* connect to MDD we just setup */
4334         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4335         if (rc)
4336                 RETURN(rc);
4337
4338         site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4339         LASSERT(site);
4340         LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4341         mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4342         site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4343         mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4344
4345
4346         /* now connect to bottom OSD */
4347         snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4348         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4349         if (rc)
4350                 RETURN(rc);
4351         mdt->mdt_bottom =
4352                 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4353
4354
4355         rc = lu_env_refill((struct lu_env *)env);
4356         if (rc != 0)
4357                 CERROR("Failure to refill session: '%d'\n", rc);
4358
4359         lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4360
4361         EXIT;
4362 class_detach:
4363         if (rc)
4364                 class_detach(obd, lcfg);
4365 lcfg_cleanup:
4366         lustre_cfg_free(lcfg);
4367 free_bufs:
4368         OBD_FREE_PTR(bufs);
4369 cleanup_mem:
4370         if (name)
4371                 OBD_FREE(name, name_size);
4372         if (uuid)
4373                 OBD_FREE(uuid, uuid_size);
4374         RETURN(rc);
4375 }
4376
4377 /* setup quota master target on MDT0 */
4378 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4379                           struct lustre_cfg *cfg)
4380 {
4381         struct obd_device       *obd;
4382         char                    *dev = lustre_cfg_string(cfg, 0);
4383         char                    *qmtname, *uuid, *p;
4384         struct lustre_cfg_bufs  *bufs;
4385         struct lustre_cfg       *lcfg;
4386         struct lustre_profile   *lprof;
4387         struct obd_connect_data *data;
4388         int                      rc;
4389         ENTRY;
4390
4391         LASSERT(mdt->mdt_qmt_exp == NULL);
4392         LASSERT(mdt->mdt_qmt_dev == NULL);
4393
4394         /* quota master is on MDT0 only for now */
4395         if (mdt->mdt_seq_site.ss_node_id != 0)
4396                 RETURN(0);
4397
4398         /* MGS generates config commands which look as follows:
4399          *   #01 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4400          *                    3:lustre-MDT0000-mdtlov  4:f
4401          *
4402          * We generate the QMT name from the MDT one, just replacing MD with QM
4403          * after all the preparations, the logical equivalent will be:
4404          *   #01 (160)setup   0:lustre-QMT0000  1:lustre-QMT0000_UUID  2:0
4405          *                    3:lustre-MDT0000-osd  4:f */
4406         OBD_ALLOC(qmtname, MAX_OBD_NAME);
4407         OBD_ALLOC(uuid, UUID_MAX);
4408         OBD_ALLOC_PTR(bufs);
4409         OBD_ALLOC_PTR(data);
4410         if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4411                 GOTO(cleanup_mem, rc = -ENOMEM);
4412
4413         strcpy(qmtname, dev);
4414         p = strstr(qmtname, "-MDT");
4415         if (p == NULL)
4416                 GOTO(cleanup_mem, rc = -ENOMEM);
4417         /* replace MD with QM */
4418         p[1] = 'Q';
4419         p[2] = 'M';
4420
4421         snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4422
4423         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4424         if (lprof == NULL || lprof->lp_dt == NULL) {
4425                 CERROR("can't find profile for %s\n",
4426                        lustre_cfg_string(cfg, 0));
4427                 GOTO(cleanup_mem, rc = -EINVAL);
4428         }
4429
4430         lustre_cfg_bufs_reset(bufs, qmtname);
4431         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4432         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4433         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4434
4435         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4436         if (!lcfg)
4437                 GOTO(cleanup_mem, rc = -ENOMEM);
4438
4439         rc = class_attach(lcfg);
4440         if (rc)
4441                 GOTO(lcfg_cleanup, rc);
4442
4443         obd = class_name2obd(qmtname);
4444         if (!obd) {
4445                 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4446                        lustre_cfg_string(cfg, 0));
4447                 GOTO(class_detach, rc = -EINVAL);
4448         }
4449
4450         lustre_cfg_free(lcfg);
4451
4452         lustre_cfg_bufs_reset(bufs, qmtname);
4453         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4454         lustre_cfg_bufs_set_string(bufs, 2, dev);
4455
4456         /* for quota, the next device should be the OSD device */
4457         lustre_cfg_bufs_set_string(bufs, 3,
4458                                    mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4459
4460         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4461
4462         rc = class_setup(obd, lcfg);
4463         if (rc)
4464                 GOTO(class_detach, rc);
4465
4466         mdt->mdt_qmt_dev = obd->obd_lu_dev;
4467
4468         /* configure local quota objects */
4469         rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4470                                                    &mdt->mdt_md_dev.md_lu_dev,
4471                                                    mdt->mdt_qmt_dev);
4472         if (rc)
4473                 GOTO(class_cleanup, rc);
4474
4475         /* connect to quota master target */
4476         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4477         data->ocd_version = LUSTRE_VERSION_CODE;
4478         rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4479                          data, NULL);
4480         if (rc) {
4481                 CERROR("cannot connect to quota master device %s (%d)\n",
4482                        qmtname, rc);
4483                 GOTO(class_cleanup, rc);
4484         }
4485
4486         EXIT;
4487 class_cleanup:
4488         if (rc) {
4489                 class_manual_cleanup(obd);
4490                 mdt->mdt_qmt_dev = NULL;
4491         }
4492 class_detach:
4493         if (rc)
4494                 class_detach(obd, lcfg);
4495 lcfg_cleanup:
4496         lustre_cfg_free(lcfg);
4497 cleanup_mem:
4498         if (bufs)
4499                 OBD_FREE_PTR(bufs);
4500         if (qmtname)
4501                 OBD_FREE(qmtname, MAX_OBD_NAME);
4502         if (uuid)
4503                 OBD_FREE(uuid, UUID_MAX);
4504         if (data)
4505                 OBD_FREE_PTR(data);
4506         return rc;
4507 }
4508
4509 /* Shutdown quota master target associated with mdt */
4510 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4511 {
4512         ENTRY;
4513
4514         if (mdt->mdt_qmt_exp == NULL)
4515                 RETURN_EXIT;
4516         LASSERT(mdt->mdt_qmt_dev != NULL);
4517
4518         /* the qmt automatically shuts down when the mdt disconnects */
4519         obd_disconnect(mdt->mdt_qmt_exp);
4520         mdt->mdt_qmt_exp = NULL;
4521         mdt->mdt_qmt_dev = NULL;
4522         EXIT;
4523 }
4524
4525 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4526 {
4527         struct md_device  *next = m->mdt_child;
4528         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4529         struct obd_device *obd = mdt2obd_dev(m);
4530         ENTRY;
4531
4532         target_recovery_fini(obd);
4533
4534         ping_evictor_stop();
4535
4536
4537         mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4538         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4539         obd_exports_barrier(obd);
4540         obd_zombie_barrier();
4541
4542         mdt_procfs_fini(m);
4543
4544         tgt_fini(env, &m->mdt_lut);
4545         mdt_fs_cleanup(env, m);
4546         upcall_cache_cleanup(m->mdt_identity_cache);
4547         m->mdt_identity_cache = NULL;
4548
4549         if (m->mdt_namespace != NULL) {
4550                 ldlm_namespace_free(m->mdt_namespace, NULL,
4551                                     d->ld_obd->obd_force);
4552                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4553         }
4554
4555         mdt_quota_fini(env, m);
4556
4557         cfs_free_nidlist(&m->mdt_nosquash_nids);
4558         if (m->mdt_nosquash_str) {
4559                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4560                 m->mdt_nosquash_str = NULL;
4561                 m->mdt_nosquash_strlen = 0;
4562         }
4563
4564         next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
4565                                     0, NULL);
4566         mdt_seq_fini(env, m);
4567         mdt_fld_fini(env, m);
4568         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4569
4570         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4571         cfs_timer_disarm(&m->mdt_ck_timer);
4572         mdt_ck_thread_stop(m);
4573
4574         /*
4575          * Finish the stack
4576          */
4577         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4578
4579         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4580
4581         server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4582
4583         EXIT;
4584 }
4585
4586 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4587 {
4588         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4589         struct sptlrpc_rule_set  tmp_rset;
4590         int                      rc;
4591
4592         sptlrpc_rule_set_init(&tmp_rset);
4593         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4594         if (rc) {
4595                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4596                        obd->obd_name, rc);
4597                 return rc;
4598         }
4599
4600         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4601
4602         write_lock(&m->mdt_sptlrpc_lock);
4603         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4604         m->mdt_sptlrpc_rset = tmp_rset;
4605         write_unlock(&m->mdt_sptlrpc_lock);
4606
4607         return 0;
4608 }
4609
4610 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4611
4612 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4613                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4614 {
4615         struct mdt_thread_info    *info;
4616         struct obd_device         *obd;
4617         const char                *dev = lustre_cfg_string(cfg, 0);
4618         const char                *num = lustre_cfg_string(cfg, 2);
4619         struct lustre_mount_info  *lmi = NULL;
4620         struct lustre_sb_info     *lsi;
4621         struct lu_site            *s;
4622         struct seq_server_site    *ss_site;
4623         const char                *identity_upcall = "NONE";
4624         struct md_device          *next;
4625         int                        rc;
4626         int                        node_id;
4627         mntopt_t                   mntopts;
4628         ENTRY;
4629
4630         md_device_init(&m->mdt_md_dev, ldt);
4631         /*
4632          * Environment (env) might be missing mdt_thread_key values at that
4633          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4634          * mode.
4635          *
4636          * Usually device allocation path doesn't use module key values, but
4637          * mdt has to do a lot of work here, so allocate key value.
4638          */
4639         rc = lu_env_refill((struct lu_env *)env);
4640         if (rc != 0)
4641                 RETURN(rc);
4642
4643         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4644         LASSERT(info != NULL);
4645
4646         obd = class_name2obd(dev);
4647         LASSERT(obd != NULL);
4648
4649         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4650
4651         m->mdt_som_conf = 0;
4652
4653         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4654         lmi = server_get_mount(dev);
4655         if (lmi == NULL) {
4656                 CERROR("Cannot get mount info for %s!\n", dev);
4657                 RETURN(-EFAULT);
4658         } else {
4659                 lsi = s2lsi(lmi->lmi_sb);
4660                 /* CMD is supported only in IAM mode */
4661                 LASSERT(num);
4662                 node_id = simple_strtol(num, NULL, 10);
4663                 obd->u.obt.obt_magic = OBT_MAGIC;
4664         }
4665
4666         rwlock_init(&m->mdt_sptlrpc_lock);
4667         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4668
4669         spin_lock_init(&m->mdt_ioepoch_lock);
4670         m->mdt_opts.mo_compat_resname = 0;
4671         m->mdt_opts.mo_mds_capa = 1;
4672         m->mdt_opts.mo_oss_capa = 1;
4673         m->mdt_capa_timeout = CAPA_TIMEOUT;
4674         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4675         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4676         m->mdt_squash_uid = 0;
4677         m->mdt_squash_gid = 0;
4678         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4679         m->mdt_nosquash_str = NULL;
4680         m->mdt_nosquash_strlen = 0;
4681         init_rwsem(&m->mdt_squash_sem);
4682         spin_lock_init(&m->mdt_osfs_lock);
4683         m->mdt_osfs_age = cfs_time_shift_64(-1000);
4684         m->mdt_enable_remote_dir = 0;
4685
4686         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4687         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4688         /* set this lu_device to obd, because error handling need it */
4689         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4690
4691         /* init the stack */
4692         rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4693         if (rc) {
4694                 CERROR("Can't init device stack, rc %d\n", rc);
4695                 GOTO(err_lmi, rc);
4696         }
4697
4698         s = m->mdt_md_dev.md_lu_dev.ld_site;
4699         ss_site = &m->mdt_seq_site;
4700         s->ld_seq_site = ss_site;
4701         ss_site->ss_lu = s;
4702
4703         /* set server index */
4704         ss_site->ss_node_id = node_id;
4705
4706         /* failover is the default
4707          * FIXME: we do not failout mds0/mgs, which may cause some problems.
4708          * assumed whose ss_node_id == 0 XXX
4709          * */
4710         obd->obd_replayable = 1;
4711         /* No connection accepted until configurations will finish */
4712         obd->obd_no_conn = 1;
4713
4714         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4715                 char *str = lustre_cfg_string(cfg, 4);
4716                 if (strchr(str, 'n')) {
4717                         CWARN("%s: recovery disabled\n", obd->obd_name);
4718                         obd->obd_replayable = 0;
4719                 }
4720         }
4721
4722         rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4723         if (rc)
4724                 GOTO(err_fini_stack, rc);
4725
4726         rc = mdt_fld_init(env, obd->obd_name, m);
4727         if (rc)
4728                 GOTO(err_lut, rc);
4729
4730         rc = mdt_seq_init(env, obd->obd_name, m);
4731         if (rc)
4732                 GOTO(err_fini_fld, rc);
4733
4734         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4735                  LUSTRE_MDT_NAME"-%p", m);
4736         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4737                                               LDLM_NAMESPACE_SERVER,
4738                                               LDLM_NAMESPACE_GREEDY,
4739                                               LDLM_NS_TYPE_MDT);
4740         if (m->mdt_namespace == NULL)
4741                 GOTO(err_fini_seq, rc = -ENOMEM);
4742
4743         m->mdt_namespace->ns_lvbp = m;
4744         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4745
4746         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4747         /* set obd_namespace for compatibility with old code */
4748         obd->obd_namespace = m->mdt_namespace;
4749
4750         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4751
4752         rc = mdt_ck_thread_start(m);
4753         if (rc)
4754                 GOTO(err_free_ns, rc);
4755
4756         rc = mdt_fs_setup(env, m, obd, lsi);
4757         if (rc)
4758                 GOTO(err_capa, rc);
4759
4760         mdt_adapt_sptlrpc_conf(obd, 1);
4761
4762         next = m->mdt_child;
4763         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4764                                          &mntopts);
4765         if (rc)
4766                 GOTO(err_llog_cleanup, rc);
4767
4768         if (mntopts & MNTOPT_USERXATTR)
4769                 m->mdt_opts.mo_user_xattr = 1;
4770         else
4771                 m->mdt_opts.mo_user_xattr = 0;
4772
4773         if (mntopts & MNTOPT_ACL)
4774                 m->mdt_opts.mo_acl = 1;
4775         else
4776                 m->mdt_opts.mo_acl = 0;
4777
4778         /* XXX: to support suppgid for ACL, we enable identity_upcall
4779          * by default, otherwise, maybe got unexpected -EACCESS. */
4780         if (m->mdt_opts.mo_acl)
4781                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4782
4783         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4784                                                 &mdt_identity_upcall_cache_ops);
4785         if (IS_ERR(m->mdt_identity_cache)) {
4786                 rc = PTR_ERR(m->mdt_identity_cache);
4787                 m->mdt_identity_cache = NULL;
4788                 GOTO(err_llog_cleanup, rc);
4789         }
4790
4791         rc = mdt_procfs_init(m, dev);
4792         if (rc) {
4793                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4794                 GOTO(err_recovery, rc);
4795         }
4796
4797         rc = mdt_quota_init(env, m, cfg);
4798         if (rc)
4799                 GOTO(err_procfs, rc);
4800
4801         m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4802         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4803                            "mdt_ldlm_client", m->mdt_ldlm_client);
4804
4805         ping_evictor_start();
4806
4807         /* recovery will be started upon mdt_prepare()
4808          * when the whole stack is complete and ready
4809          * to serve the requests */
4810
4811         mdt_init_capa_ctxt(env, m);
4812
4813         /* Reduce the initial timeout on an MDS because it doesn't need such
4814          * a long timeout as an OST does. Adaptive timeouts will adjust this
4815          * value appropriately. */
4816         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4817                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4818
4819         RETURN(0);
4820
4821 err_procfs:
4822         mdt_procfs_fini(m);
4823 err_recovery:
4824         target_recovery_fini(obd);
4825         upcall_cache_cleanup(m->mdt_identity_cache);
4826         m->mdt_identity_cache = NULL;
4827 err_llog_cleanup:
4828         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4829         mdt_fs_cleanup(env, m);
4830 err_capa:
4831         cfs_timer_disarm(&m->mdt_ck_timer);
4832         mdt_ck_thread_stop(m);
4833 err_free_ns:
4834         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4835         obd->obd_namespace = m->mdt_namespace = NULL;
4836 err_fini_seq:
4837         mdt_seq_fini(env, m);
4838 err_fini_fld:
4839         mdt_fld_fini(env, m);
4840 err_lut:
4841         tgt_fini(env, &m->mdt_lut);
4842 err_fini_stack:
4843         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4844 err_lmi:
4845         if (lmi)
4846                 server_put_mount(dev, lmi->lmi_mnt);
4847         return (rc);
4848 }
4849
4850 /* For interoperability, the left element is old parameter, the right one
4851  * is the new version of the parameter, if some parameter is deprecated,
4852  * the new version should be set as NULL. */
4853 static struct cfg_interop_param mdt_interop_param[] = {
4854         { "mdt.group_upcall",   NULL },
4855         { "mdt.quota_type",     NULL },
4856         { "mdd.quota_type",     NULL },
4857         { "mdt.rootsquash",     "mdt.root_squash" },
4858         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
4859         { NULL }
4860 };
4861
4862 /* used by MGS to process specific configurations */
4863 static int mdt_process_config(const struct lu_env *env,
4864                               struct lu_device *d, struct lustre_cfg *cfg)
4865 {
4866         struct mdt_device *m = mdt_dev(d);
4867         struct md_device *md_next = m->mdt_child;
4868         struct lu_device *next = md2lu_dev(md_next);
4869         int rc;
4870         ENTRY;
4871
4872         switch (cfg->lcfg_command) {
4873         case LCFG_PARAM: {
4874                 struct lprocfs_static_vars  lvars;
4875                 struct obd_device          *obd = d->ld_obd;
4876
4877                 /* For interoperability */
4878                 struct cfg_interop_param   *ptr = NULL;
4879                 struct lustre_cfg          *old_cfg = NULL;
4880                 char                       *param = NULL;
4881
4882                 param = lustre_cfg_string(cfg, 1);
4883                 if (param == NULL) {
4884                         CERROR("param is empty\n");
4885                         rc = -EINVAL;
4886                         break;
4887                 }
4888
4889                 ptr = class_find_old_param(param, mdt_interop_param);
4890                 if (ptr != NULL) {
4891                         if (ptr->new_param == NULL) {
4892                                 rc = 0;
4893                                 CWARN("For interoperability, skip this %s."
4894                                       " It is obsolete.\n", ptr->old_param);
4895                                 break;
4896                         }
4897
4898                         CWARN("Found old param %s, changed it to %s.\n",
4899                               ptr->old_param, ptr->new_param);
4900
4901                         old_cfg = cfg;
4902                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4903                         if (IS_ERR(cfg)) {
4904                                 rc = PTR_ERR(cfg);
4905                                 break;
4906                         }
4907                 }
4908
4909                 lprocfs_mdt_init_vars(&lvars);
4910                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4911                                               cfg, obd);
4912                 if (rc > 0 || rc == -ENOSYS)
4913                         /* we don't understand; pass it on */
4914                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
4915
4916                 if (old_cfg != NULL)
4917                         lustre_cfg_free(cfg);
4918
4919                 break;
4920         }
4921         default:
4922                 /* others are passed further */
4923                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4924                 break;
4925         }
4926         RETURN(rc);
4927 }
4928
4929 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4930                                           const struct lu_object_header *hdr,
4931                                           struct lu_device *d)
4932 {
4933         struct mdt_object *mo;
4934
4935         ENTRY;
4936
4937         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4938         if (mo != NULL) {
4939                 struct lu_object *o;
4940                 struct lu_object_header *h;
4941
4942                 o = &mo->mot_obj.mo_lu;
4943                 h = &mo->mot_header;
4944                 lu_object_header_init(h);
4945                 lu_object_init(o, h, d);
4946                 lu_object_add_top(h, o);
4947                 o->lo_ops = &mdt_obj_ops;
4948                 mutex_init(&mo->mot_ioepoch_mutex);
4949                 mutex_init(&mo->mot_lov_mutex);
4950                 RETURN(o);
4951         } else
4952                 RETURN(NULL);
4953 }
4954
4955 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4956                            const struct lu_object_conf *unused)
4957 {
4958         struct mdt_device *d = mdt_dev(o->lo_dev);
4959         struct lu_device  *under;
4960         struct lu_object  *below;
4961         int                rc = 0;
4962         ENTRY;
4963
4964         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4965                PFID(lu_object_fid(o)));
4966
4967         under = &d->mdt_child->md_lu_dev;
4968         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4969         if (below != NULL) {
4970                 lu_object_add(o, below);
4971         } else
4972                 rc = -ENOMEM;
4973
4974         RETURN(rc);
4975 }
4976
4977 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4978 {
4979         struct mdt_object *mo = mdt_obj(o);
4980         struct lu_object_header *h;
4981         ENTRY;
4982
4983         h = o->lo_header;
4984         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4985                PFID(lu_object_fid(o)));
4986
4987         lu_object_fini(o);
4988         lu_object_header_fini(h);
4989         OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4990
4991         EXIT;
4992 }
4993
4994 static int mdt_object_print(const struct lu_env *env, void *cookie,
4995                             lu_printer_t p, const struct lu_object *o)
4996 {
4997         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4998         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4999                     "flags="LPX64", epochcount=%d, writecount=%d)",
5000                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
5001                     mdto->mot_ioepoch_count, mdto->mot_writecount);
5002 }
5003
5004 static int mdt_prepare(const struct lu_env *env,
5005                 struct lu_device *pdev,
5006                 struct lu_device *cdev)
5007 {
5008         struct mdt_device *mdt = mdt_dev(cdev);
5009         struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5010         struct obd_device *obd = cdev->ld_obd;
5011         int rc;
5012
5013         ENTRY;
5014
5015         LASSERT(obd);
5016
5017         rc = next->ld_ops->ldo_prepare(env, cdev, next);
5018         if (rc)
5019                 RETURN(rc);
5020
5021         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5022         if (rc)
5023                 RETURN(rc);
5024
5025         rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5026                                                    OBD_IOC_START_LFSCK,
5027                                                    0, NULL);
5028         if (rc != 0)
5029                 CWARN("Fail to auto trigger paused LFSCK.\n");
5030
5031         rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5032                                                   &mdt->mdt_md_root_fid);
5033         if (rc)
5034                 RETURN(rc);
5035
5036         LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5037         target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5038         set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5039         LASSERT(obd->obd_no_conn);
5040         spin_lock(&obd->obd_dev_lock);
5041         obd->obd_no_conn = 0;
5042         spin_unlock(&obd->obd_dev_lock);
5043
5044         if (obd->obd_recovering == 0)
5045                 mdt_postrecov(env, mdt);
5046
5047         RETURN(rc);
5048 }
5049
5050 const struct lu_device_operations mdt_lu_ops = {
5051         .ldo_object_alloc   = mdt_object_alloc,
5052         .ldo_process_config = mdt_process_config,
5053         .ldo_prepare        = mdt_prepare,
5054 };
5055
5056 static const struct lu_object_operations mdt_obj_ops = {
5057         .loo_object_init    = mdt_object_init,
5058         .loo_object_free    = mdt_object_free,
5059         .loo_object_print   = mdt_object_print
5060 };
5061
5062 static int mdt_obd_set_info_async(const struct lu_env *env,
5063                                   struct obd_export *exp,
5064                                   __u32 keylen, void *key,
5065                                   __u32 vallen, void *val,
5066                                   struct ptlrpc_request_set *set)
5067 {
5068         struct obd_device     *obd = exp->exp_obd;
5069         int                    rc;
5070         ENTRY;
5071
5072         LASSERT(obd);
5073
5074         if (KEY_IS(KEY_SPTLRPC_CONF)) {
5075                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5076                 RETURN(rc);
5077         }
5078
5079         RETURN(0);
5080 }
5081
5082 /**
5083  * Match client and server connection feature flags.
5084  *
5085  * Compute the compatibility flags for a connection request based on
5086  * features mutually supported by client and server.
5087  *
5088  * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5089  * must not be updated here, otherwise a partially initialized value may
5090  * be exposed. After the connection request is successfully processed,
5091  * the top-level MDT connect request handler atomically updates the export
5092  * connect flags from the obd_connect_data::ocd_connect_flags field of the
5093  * reply. \see mdt_connect().
5094  *
5095  * \param exp   the obd_export associated with this client/target pair
5096  * \param mdt   the target device for the connection
5097  * \param data  stores data for this connect request
5098  *
5099  * \retval 0       success
5100  * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5101  * \retval -EBADE  client and server feature requirements are incompatible
5102  */
5103 static int mdt_connect_internal(struct obd_export *exp,
5104                                 struct mdt_device *mdt,
5105                                 struct obd_connect_data *data)
5106 {
5107         LASSERT(data != NULL);
5108
5109         data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5110         data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5111
5112         /* If no known bits (which should not happen, probably,
5113            as everybody should support LOOKUP and UPDATE bits at least)
5114            revert to compat mode with plain locks. */
5115         if (!data->ocd_ibits_known &&
5116             data->ocd_connect_flags & OBD_CONNECT_IBITS)
5117                 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5118
5119         if (!mdt->mdt_opts.mo_acl)
5120                 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5121
5122         if (!mdt->mdt_opts.mo_user_xattr)
5123                 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5124
5125         if (!mdt->mdt_som_conf)
5126                 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5127
5128         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5129                 data->ocd_brw_size = min(data->ocd_brw_size,
5130                                          (__u32)MD_MAX_BRW_SIZE);
5131                 if (data->ocd_brw_size == 0) {
5132                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5133                                " ocd_version: %x ocd_grant: %d "
5134                                "ocd_index: %u ocd_brw_size is "
5135                                "unexpectedly zero, network data "
5136                                "corruption? Refusing connection of this"
5137                                " client\n",
5138                                exp->exp_obd->obd_name,
5139                                exp->exp_client_uuid.uuid,
5140                                exp, data->ocd_connect_flags, data->ocd_version,
5141                                data->ocd_grant, data->ocd_index);
5142                         return -EPROTO;
5143                 }
5144         }
5145
5146         /* NB: Disregard the rule against updating
5147          * exp_connect_data.ocd_connect_flags in this case, since
5148          * tgt_client_new() needs to know if this is a lightweight
5149          * connection, and it is safe to expose this flag before
5150          * connection processing completes. */
5151         if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5152                 spin_lock(&exp->exp_lock);
5153                 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5154                 spin_unlock(&exp->exp_lock);
5155         }
5156
5157         data->ocd_version = LUSTRE_VERSION_CODE;
5158         exp->exp_connect_data = *data;
5159         exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5160
5161         if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5162                 CWARN("%s: MDS requires FID support, but client not\n",
5163                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5164                 return -EBADE;
5165         }
5166
5167         if (mdt->mdt_som_conf &&
5168             !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5169                                          OBD_CONNECT_MDS_MDS |
5170                                          OBD_CONNECT_SOM))) {
5171                 CWARN("%s: MDS has SOM enabled, but client does not support "
5172                       "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5173                 return -EBADE;
5174         }
5175
5176         if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
5177                 if (suppress_pings) {
5178                         spin_lock(&exp->exp_obd->obd_dev_lock);
5179                         list_del_init(&exp->exp_obd_chain_timed);
5180                         spin_unlock(&exp->exp_obd->obd_dev_lock);
5181                 } else {
5182                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5183                 }
5184         }
5185
5186         return 0;
5187 }
5188
5189 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5190                                      struct obd_export *exp,
5191                                      struct ptlrpc_request *req)
5192 {
5193         struct sptlrpc_flavor   flvr;
5194         int                     rc = 0;
5195
5196         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5197                 read_lock(&mdt->mdt_sptlrpc_lock);
5198                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5199                                              req->rq_sp_from,
5200                                              req->rq_peer.nid,
5201                                              &flvr);
5202                 read_unlock(&mdt->mdt_sptlrpc_lock);
5203
5204                 spin_lock(&exp->exp_lock);
5205
5206                 exp->exp_sp_peer = req->rq_sp_from;
5207                 exp->exp_flvr = flvr;
5208
5209                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5210                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5211                         CERROR("unauthorized rpc flavor %x from %s, "
5212                                "expect %x\n", req->rq_flvr.sf_rpc,
5213                                libcfs_nid2str(req->rq_peer.nid),
5214                                exp->exp_flvr.sf_rpc);
5215                         rc = -EACCES;
5216                 }
5217
5218                 spin_unlock(&exp->exp_lock);
5219         } else {
5220                 if (exp->exp_sp_peer != req->rq_sp_from) {
5221                         CERROR("RPC source %s doesn't match %s\n",
5222                                sptlrpc_part2name(req->rq_sp_from),
5223                                sptlrpc_part2name(exp->exp_sp_peer));
5224                         rc = -EACCES;
5225                 } else {
5226                         rc = sptlrpc_target_export_check(exp, req);
5227                 }
5228         }
5229
5230         return rc;
5231 }
5232
5233 /* mds_connect copy */
5234 static int mdt_obd_connect(const struct lu_env *env,
5235                            struct obd_export **exp, struct obd_device *obd,
5236                            struct obd_uuid *cluuid,
5237                            struct obd_connect_data *data,
5238                            void *localdata)
5239 {
5240         struct mdt_thread_info *info;
5241         struct obd_export      *lexp;
5242         struct lustre_handle    conn = { 0 };
5243         struct mdt_device      *mdt;
5244         struct ptlrpc_request  *req;
5245         int                     rc;
5246         ENTRY;
5247
5248         LASSERT(env != NULL);
5249         if (!exp || !obd || !cluuid)
5250                 RETURN(-EINVAL);
5251
5252         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5253         req = info->mti_pill->rc_req;
5254         mdt = mdt_dev(obd->obd_lu_dev);
5255
5256         /*
5257          * first, check whether the stack is ready to handle requests
5258          * XXX: probably not very appropriate method is used now
5259          *      at some point we should find a better one
5260          */
5261         if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5262             !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5263                 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5264                 if (rc)
5265                         RETURN(-EAGAIN);
5266                 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5267         }
5268
5269         rc = class_connect(&conn, obd, cluuid);
5270         if (rc)
5271                 RETURN(rc);
5272
5273         lexp = class_conn2export(&conn);
5274         LASSERT(lexp != NULL);
5275
5276         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5277         if (rc)
5278                 GOTO(out, rc);
5279
5280         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5281                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5282
5283         rc = mdt_connect_internal(lexp, mdt, data);
5284         if (rc == 0) {
5285                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5286
5287                 LASSERT(lcd);
5288                 info->mti_exp = lexp;
5289                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5290                 rc = tgt_client_new(env, lexp);
5291                 if (rc == 0)
5292                         mdt_export_stats_init(obd, lexp, localdata);
5293         }
5294
5295 out:
5296         if (rc != 0) {
5297                 class_disconnect(lexp);
5298                 *exp = NULL;
5299         } else {
5300                 *exp = lexp;
5301         }
5302
5303         RETURN(rc);
5304 }
5305
5306 static int mdt_obd_reconnect(const struct lu_env *env,
5307                              struct obd_export *exp, struct obd_device *obd,
5308                              struct obd_uuid *cluuid,
5309                              struct obd_connect_data *data,
5310                              void *localdata)
5311 {
5312         struct mdt_thread_info *info;
5313         struct mdt_device      *mdt;
5314         struct ptlrpc_request  *req;
5315         int                     rc;
5316         ENTRY;
5317
5318         if (exp == NULL || obd == NULL || cluuid == NULL)
5319                 RETURN(-EINVAL);
5320
5321         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5322         req = info->mti_pill->rc_req;
5323         mdt = mdt_dev(obd->obd_lu_dev);
5324
5325         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5326         if (rc)
5327                 RETURN(rc);
5328
5329         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5330         if (rc == 0)
5331                 mdt_export_stats_init(obd, exp, localdata);
5332
5333         RETURN(rc);
5334 }
5335
5336 static int mdt_export_cleanup(struct obd_export *exp)
5337 {
5338         struct mdt_export_data *med = &exp->exp_mdt_data;
5339         struct obd_device      *obd = exp->exp_obd;
5340         struct mdt_device      *mdt;
5341         struct mdt_thread_info *info;
5342         struct lu_env           env;
5343         CFS_LIST_HEAD(closing_list);
5344         struct mdt_file_data *mfd, *n;
5345         int rc = 0;
5346         ENTRY;
5347
5348         spin_lock(&med->med_open_lock);
5349         while (!cfs_list_empty(&med->med_open_head)) {
5350                 cfs_list_t *tmp = med->med_open_head.next;
5351                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5352
5353                 /* Remove mfd handle so it can't be found again.
5354                  * We are consuming the mfd_list reference here. */
5355                 class_handle_unhash(&mfd->mfd_handle);
5356                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5357         }
5358         spin_unlock(&med->med_open_lock);
5359         mdt = mdt_dev(obd->obd_lu_dev);
5360         LASSERT(mdt != NULL);
5361
5362         rc = lu_env_init(&env, LCT_MD_THREAD);
5363         if (rc)
5364                 RETURN(rc);
5365
5366         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5367         LASSERT(info != NULL);
5368         memset(info, 0, sizeof *info);
5369         info->mti_env = &env;
5370         info->mti_mdt = mdt;
5371         info->mti_exp = exp;
5372
5373         if (!cfs_list_empty(&closing_list)) {
5374                 struct md_attr *ma = &info->mti_attr;
5375
5376                 /* Close any open files (which may also cause orphan unlinking). */
5377                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5378                         cfs_list_del_init(&mfd->mfd_list);
5379                         ma->ma_need = ma->ma_valid = 0;
5380                         /* Don't unlink orphan on failover umount, LU-184 */
5381                         if (exp->exp_flags & OBD_OPT_FAILOVER) {
5382                                 ma->ma_valid = MA_FLAGS;
5383                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5384                         }
5385                         mdt_mfd_close(info, mfd);
5386                 }
5387         }
5388         info->mti_mdt = NULL;
5389         /* cleanup client slot early */
5390         /* Do not erase record for recoverable client. */
5391         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5392                 tgt_client_del(&env, exp);
5393         lu_env_fini(&env);
5394
5395         RETURN(rc);
5396 }
5397
5398 static int mdt_obd_disconnect(struct obd_export *exp)
5399 {
5400         int rc;
5401         ENTRY;
5402
5403         LASSERT(exp);
5404         class_export_get(exp);
5405
5406         rc = server_disconnect_export(exp);
5407         if (rc != 0)
5408                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5409
5410         rc = mdt_export_cleanup(exp);
5411         class_export_put(exp);
5412         RETURN(rc);
5413 }
5414
5415 /* FIXME: Can we avoid using these two interfaces? */
5416 static int mdt_init_export(struct obd_export *exp)
5417 {
5418         struct mdt_export_data *med = &exp->exp_mdt_data;
5419         int                     rc;
5420         ENTRY;
5421
5422         CFS_INIT_LIST_HEAD(&med->med_open_head);
5423         spin_lock_init(&med->med_open_lock);
5424         mutex_init(&med->med_idmap_mutex);
5425         med->med_idmap = NULL;
5426         spin_lock(&exp->exp_lock);
5427         exp->exp_connecting = 1;
5428         spin_unlock(&exp->exp_lock);
5429
5430         /* self-export doesn't need client data and ldlm initialization */
5431         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5432                                      &exp->exp_client_uuid)))
5433                 RETURN(0);
5434
5435         rc = tgt_client_alloc(exp);
5436         if (rc)
5437                 GOTO(err, rc);
5438
5439         rc = ldlm_init_export(exp);
5440         if (rc)
5441                 GOTO(err_free, rc);
5442
5443         RETURN(rc);
5444
5445 err_free:
5446         tgt_client_free(exp);
5447 err:
5448         CERROR("%s: Failed to initialize export: rc = %d\n",
5449                exp->exp_obd->obd_name, rc);
5450         return rc;
5451 }
5452
5453 static int mdt_destroy_export(struct obd_export *exp)
5454 {
5455         ENTRY;
5456
5457         if (exp_connect_rmtclient(exp))
5458                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5459
5460         target_destroy_export(exp);
5461         /* destroy can be called from failed obd_setup, so
5462          * checking uuid is safer than obd_self_export */
5463         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5464                                      &exp->exp_client_uuid)))
5465                 RETURN(0);
5466
5467         ldlm_destroy_export(exp);
5468         tgt_client_free(exp);
5469
5470         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5471         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5472
5473         RETURN(0);
5474 }
5475
5476 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5477                             void *val, int vallen)
5478 {
5479         struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5480         struct getinfo_fid2path *fpout, *fpin;
5481         int rc = 0;
5482
5483         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5484         fpout = val;
5485
5486         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5487                 lustre_swab_fid2path(fpin);
5488
5489         memcpy(fpout, fpin, sizeof(*fpin));
5490         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5491                 RETURN(-EINVAL);
5492
5493         rc = mdt_fid2path(info->mti_env, mdt, fpout);
5494         RETURN(rc);
5495 }
5496
5497 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5498                 struct getinfo_fid2path *fp)
5499 {
5500         struct mdt_object *obj;
5501         struct obd_device *obd = mdt2obd_dev(mdt);
5502         int    rc;
5503         ENTRY;
5504
5505         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5506                 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5507
5508         if (!fid_is_sane(&fp->gf_fid))
5509                 RETURN(-EINVAL);
5510
5511         if (!fid_is_client_mdt_visible(&fp->gf_fid)) {
5512                 CWARN("%s: "DFID" is invalid, sequence should be "
5513                         ">= "LPX64"\n", obd->obd_name,
5514                         PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5515                 RETURN(-EINVAL);
5516         }
5517
5518         obj = mdt_object_find(env, mdt, &fp->gf_fid);
5519         if (obj == NULL || IS_ERR(obj)) {
5520                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5521                         PTR_ERR(obj));
5522                 RETURN(-EINVAL);
5523         }
5524
5525         if (mdt_object_remote(obj))
5526                 rc = -EREMOTE;
5527         else if (!mdt_object_exists(obj))
5528                 rc = -ENOENT;
5529
5530         if (rc < 0) {
5531                 mdt_object_put(env, obj);
5532                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5533                         PFID(&fp->gf_fid), rc);
5534                 RETURN(rc);
5535         }
5536
5537         rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5538                         fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5539         mdt_object_put(env, obj);
5540
5541         RETURN(rc);
5542 }
5543
5544 int mdt_get_info(struct mdt_thread_info *info)
5545 {
5546         struct ptlrpc_request *req = mdt_info_req(info);
5547         char *key;
5548         int keylen;
5549         __u32 *vallen;
5550         void *valout;
5551         int rc;
5552         ENTRY;
5553
5554         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5555         if (key == NULL) {
5556                 CDEBUG(D_IOCTL, "No GETINFO key");
5557                 RETURN(-EFAULT);
5558         }
5559         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5560                                       RCL_CLIENT);
5561
5562         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5563         if (vallen == NULL) {
5564                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5565                 RETURN(-EFAULT);
5566         }
5567
5568         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5569                              *vallen);
5570         rc = req_capsule_server_pack(info->mti_pill);
5571         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5572         if (valout == NULL) {
5573                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5574                 RETURN(-EFAULT);
5575         }
5576
5577         if (KEY_IS(KEY_FID2PATH))
5578                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5579         else
5580                 rc = -EINVAL;
5581
5582         lustre_msg_set_status(req->rq_repmsg, rc);
5583
5584         RETURN(rc);
5585 }
5586
5587 /* Pass the ioc down */
5588 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5589                          unsigned int cmd, int len, void *data)
5590 {
5591         struct lu_context ioctl_session;
5592         struct md_device *next = mdt->mdt_child;
5593         int rc;
5594         ENTRY;
5595
5596         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5597         if (rc)
5598                 RETURN(rc);
5599         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5600         lu_context_enter(&ioctl_session);
5601         env->le_ses = &ioctl_session;
5602
5603         LASSERT(next->md_ops->mdo_iocontrol);
5604         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5605
5606         lu_context_exit(&ioctl_session);
5607         lu_context_fini(&ioctl_session);
5608         RETURN(rc);
5609 }
5610
5611 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5612 {
5613         struct obd_ioctl_data *data = karg;
5614         struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5615         __u64 version;
5616         struct mdt_object *obj;
5617         struct mdt_lock_handle  *lh;
5618         int rc;
5619         ENTRY;
5620
5621         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5622         if (!fid_is_sane(fid))
5623                 RETURN(-EINVAL);
5624
5625         lh = &mti->mti_lh[MDT_LH_PARENT];
5626         mdt_lock_reg_init(lh, LCK_CR);
5627
5628         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5629         if (IS_ERR(obj))
5630                 RETURN(PTR_ERR(obj));
5631
5632         if (mdt_object_remote(obj)) {
5633                 rc = -EREMOTE;
5634                 /**
5635                  * before calling version get the correct MDS should be
5636                  * fid, this is error to find remote object here
5637                  */
5638                 CERROR("nonlocal object "DFID"\n", PFID(fid));
5639         } else if (!mdt_object_exists(obj)) {
5640                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5641                 rc = -ENOENT;
5642         } else {
5643                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5644                *(__u64 *)data->ioc_inlbuf2 = version;
5645                 rc = 0;
5646         }
5647         mdt_object_unlock_put(mti, obj, lh, 1);
5648         RETURN(rc);
5649 }
5650
5651 /* ioctls on obd dev */
5652 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5653                          void *karg, void *uarg)
5654 {
5655         struct lu_env      env;
5656         struct obd_device *obd = exp->exp_obd;
5657         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5658         struct dt_device  *dt = mdt->mdt_bottom;
5659         int rc;
5660
5661         ENTRY;
5662         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5663         rc = lu_env_init(&env, LCT_MD_THREAD);
5664         if (rc)
5665                 RETURN(rc);
5666
5667         switch (cmd) {
5668         case OBD_IOC_SYNC:
5669                 rc = mdt_device_sync(&env, mdt);
5670                 break;
5671         case OBD_IOC_SET_READONLY:
5672                 rc = dt->dd_ops->dt_ro(&env, dt);
5673                 break;
5674         case OBD_IOC_ABORT_RECOVERY:
5675                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5676                 target_stop_recovery_thread(obd);
5677                 rc = 0;
5678                 break;
5679         case OBD_IOC_CHANGELOG_REG:
5680         case OBD_IOC_CHANGELOG_DEREG:
5681         case OBD_IOC_CHANGELOG_CLEAR:
5682                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5683                 break;
5684         case OBD_IOC_START_LFSCK:
5685         case OBD_IOC_STOP_LFSCK: {
5686                 struct md_device *next = mdt->mdt_child;
5687                 struct obd_ioctl_data *data = karg;
5688
5689                 if (unlikely(data == NULL)) {
5690                         rc = -EINVAL;
5691                         break;
5692                 }
5693
5694                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5695                                                  data->ioc_inllen1,
5696                                                  data->ioc_inlbuf1);
5697                 break;
5698         }
5699         case OBD_IOC_GET_OBJ_VERSION: {
5700                 struct mdt_thread_info *mti;
5701                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5702                 memset(mti, 0, sizeof *mti);
5703                 mti->mti_env = &env;
5704                 mti->mti_mdt = mdt;
5705                 mti->mti_exp = exp;
5706
5707                 rc = mdt_ioc_version_get(mti, karg);
5708                 break;
5709         }
5710         default:
5711                 CERROR("Not supported cmd = %d for device %s\n",
5712                        cmd, obd->obd_name);
5713                 rc = -EOPNOTSUPP;
5714         }
5715
5716         lu_env_fini(&env);
5717         RETURN(rc);
5718 }
5719
5720 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5721 {
5722         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5723         int rc;
5724         ENTRY;
5725
5726         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5727         RETURN(rc);
5728 }
5729
5730 int mdt_obd_postrecov(struct obd_device *obd)
5731 {
5732         struct lu_env env;
5733         int rc;
5734
5735         rc = lu_env_init(&env, LCT_MD_THREAD);
5736         if (rc)
5737                 RETURN(rc);
5738         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5739         lu_env_fini(&env);
5740         return rc;
5741 }
5742
5743 static struct obd_ops mdt_obd_device_ops = {
5744         .o_owner          = THIS_MODULE,
5745         .o_set_info_async = mdt_obd_set_info_async,
5746         .o_connect        = mdt_obd_connect,
5747         .o_reconnect      = mdt_obd_reconnect,
5748         .o_disconnect     = mdt_obd_disconnect,
5749         .o_init_export    = mdt_init_export,
5750         .o_destroy_export = mdt_destroy_export,
5751         .o_iocontrol      = mdt_iocontrol,
5752         .o_postrecov      = mdt_obd_postrecov,
5753 };
5754
5755 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5756                                          struct lu_device *d)
5757 {
5758         struct mdt_device *m = mdt_dev(d);
5759         ENTRY;
5760
5761         mdt_fini(env, m);
5762         RETURN(NULL);
5763 }
5764
5765 static struct lu_device *mdt_device_free(const struct lu_env *env,
5766                                          struct lu_device *d)
5767 {
5768         struct mdt_device *m = mdt_dev(d);
5769         ENTRY;
5770
5771         md_device_fini(&m->mdt_md_dev);
5772         OBD_FREE_PTR(m);
5773         RETURN(NULL);
5774 }
5775
5776 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5777                                           struct lu_device_type *t,
5778                                           struct lustre_cfg *cfg)
5779 {
5780         struct lu_device  *l;
5781         struct mdt_device *m;
5782
5783         OBD_ALLOC_PTR(m);
5784         if (m != NULL) {
5785                 int rc;
5786
5787                 l = &m->mdt_md_dev.md_lu_dev;
5788                 rc = mdt_init0(env, m, t, cfg);
5789                 if (rc != 0) {
5790                         mdt_device_free(env, l);
5791                         l = ERR_PTR(rc);
5792                         return l;
5793                 }
5794         } else
5795                 l = ERR_PTR(-ENOMEM);
5796         return l;
5797 }
5798
5799 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5800 LU_KEY_INIT(mdt, struct mdt_thread_info);
5801
5802 static void mdt_key_fini(const struct lu_context *ctx,
5803                          struct lu_context_key *key, void* data)
5804 {
5805         struct mdt_thread_info *info = data;
5806
5807         if (info->mti_big_lmm) {
5808                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5809                 info->mti_big_lmm = NULL;
5810                 info->mti_big_lmmsize = 0;
5811         }
5812         OBD_FREE_PTR(info);
5813 }
5814
5815 /* context key: mdt_thread_key */
5816 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5817
5818 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5819 {
5820         return lu_ucred(info->mti_env);
5821 }
5822
5823 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5824 {
5825         return lu_ucred_check(info->mti_env);
5826 }
5827
5828 /**
5829  * Enable/disable COS (Commit On Sharing).
5830  *
5831  * Set/Clear the COS flag in mdt options.
5832  *
5833  * \param mdt mdt device
5834  * \param val 0 disables COS, other values enable COS
5835  */
5836 void mdt_enable_cos(struct mdt_device *mdt, int val)
5837 {
5838         struct lu_env env;
5839         int rc;
5840
5841         mdt->mdt_opts.mo_cos = !!val;
5842         rc = lu_env_init(&env, LCT_LOCAL);
5843         if (unlikely(rc != 0)) {
5844                 CWARN("lu_env initialization failed with rc = %d,"
5845                       "cannot sync\n", rc);
5846                 return;
5847         }
5848         mdt_device_sync(&env, mdt);
5849         lu_env_fini(&env);
5850 }
5851
5852 /**
5853  * Check COS (Commit On Sharing) status.
5854  *
5855  * Return COS flag status.
5856  *
5857  * \param mdt mdt device
5858  */
5859 int mdt_cos_is_enabled(struct mdt_device *mdt)
5860 {
5861         return mdt->mdt_opts.mo_cos != 0;
5862 }
5863
5864 static struct lu_device_type_operations mdt_device_type_ops = {
5865         .ldto_device_alloc = mdt_device_alloc,
5866         .ldto_device_free  = mdt_device_free,
5867         .ldto_device_fini  = mdt_device_fini
5868 };
5869
5870 static struct lu_device_type mdt_device_type = {
5871         .ldt_tags     = LU_DEVICE_MD,
5872         .ldt_name     = LUSTRE_MDT_NAME,
5873         .ldt_ops      = &mdt_device_type_ops,
5874         .ldt_ctx_tags = LCT_MD_THREAD
5875 };
5876
5877 static int __init mdt_mod_init(void)
5878 {
5879         struct lprocfs_static_vars lvars;
5880         int rc;
5881
5882         rc = lu_kmem_init(mdt_caches);
5883         if (rc)
5884                 return rc;
5885
5886         rc = mds_mod_init();
5887         if (rc)
5888                 GOTO(lu_fini, rc);
5889
5890         lprocfs_mdt_init_vars(&lvars);
5891         rc = class_register_type(&mdt_obd_device_ops, NULL,
5892                                  lvars.module_vars, LUSTRE_MDT_NAME,
5893                                  &mdt_device_type);
5894         if (rc)
5895                 GOTO(mds_fini, rc);
5896 lu_fini:
5897         if (rc)
5898                 lu_kmem_fini(mdt_caches);
5899 mds_fini:
5900         if (rc)
5901                 mds_mod_exit();
5902         return rc;
5903 }
5904
5905 static void __exit mdt_mod_exit(void)
5906 {
5907         class_unregister_type(LUSTRE_MDT_NAME);
5908         mds_mod_exit();
5909         lu_kmem_fini(mdt_caches);
5910 }
5911
5912 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5913 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5914 MODULE_LICENSE("GPL");
5915
5916 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);