Whamcloud - gitweb
LU-1187 out: add resend check for update.
[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_remote(o) || !mdt_object_exists(o)) /* 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         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1779         if (rec != NULL) {
1780                 opc = rec->rr_opcode;
1781                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1782                 if (opc < REINT_MAX && fmt[opc] != NULL)
1783                         req_capsule_extend(info->mti_pill, fmt[opc]);
1784                 else {
1785                         CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1786                                "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1787                                info->mti_mdt->mdt_ldlm_client->cli_name,
1788                                -EFAULT);
1789                         opc = err_serious(-EFAULT);
1790                 }
1791         } else {
1792                 opc = err_serious(-EFAULT);
1793         }
1794         return opc;
1795 }
1796
1797 int mdt_reint(struct mdt_thread_info *info)
1798 {
1799         long opc;
1800         int  rc;
1801
1802         static const struct req_format *reint_fmts[REINT_MAX] = {
1803                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1804                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1805                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1806                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1807                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1808                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1809                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1810                 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1811         };
1812
1813         ENTRY;
1814
1815         opc = mdt_reint_opcode(info, reint_fmts);
1816         if (opc >= 0) {
1817                 /*
1818                  * No lock possible here from client to pass it to reint code
1819                  * path.
1820                  */
1821                 rc = mdt_reint_internal(info, NULL, opc);
1822         } else {
1823                 rc = opc;
1824         }
1825
1826         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1827         RETURN(rc);
1828 }
1829
1830 /* this should sync the whole device */
1831 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1832 {
1833         struct dt_device *dt = mdt->mdt_bottom;
1834         int rc;
1835         ENTRY;
1836
1837         rc = dt->dd_ops->dt_sync(env, dt);
1838         RETURN(rc);
1839 }
1840
1841 /* this should sync this object */
1842 static int mdt_object_sync(struct mdt_thread_info *info)
1843 {
1844         struct md_object *next;
1845         int rc;
1846         ENTRY;
1847
1848         if (!mdt_object_exists(info->mti_object)) {
1849                 CWARN("Non existing object  "DFID"!\n",
1850                       PFID(mdt_object_fid(info->mti_object)));
1851                 RETURN(-ESTALE);
1852         }
1853         next = mdt_object_child(info->mti_object);
1854         rc = mo_object_sync(info->mti_env, next);
1855
1856         RETURN(rc);
1857 }
1858
1859 int mdt_sync(struct mdt_thread_info *info)
1860 {
1861         struct ptlrpc_request *req = mdt_info_req(info);
1862         struct req_capsule *pill = info->mti_pill;
1863         struct mdt_body *body;
1864         int rc;
1865         ENTRY;
1866
1867         /* The fid may be zero, so we req_capsule_set manually */
1868         req_capsule_set(pill, &RQF_MDS_SYNC);
1869
1870         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1871         if (body == NULL)
1872                 RETURN(err_serious(-EINVAL));
1873
1874         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1875                 RETURN(err_serious(-ENOMEM));
1876
1877         if (fid_seq(&body->fid1) == 0) {
1878                 /* sync the whole device */
1879                 rc = req_capsule_server_pack(pill);
1880                 if (rc == 0)
1881                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1882                 else
1883                         rc = err_serious(rc);
1884         } else {
1885                 /* sync an object */
1886                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1887                 if (rc == 0) {
1888                         rc = mdt_object_sync(info);
1889                         if (rc == 0) {
1890                                 const struct lu_fid *fid;
1891                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1892
1893                                 info->mti_attr.ma_need = MA_INODE;
1894                                 info->mti_attr.ma_valid = 0;
1895                                 rc = mdt_attr_get_complex(info, info->mti_object,
1896                                                           &info->mti_attr);
1897                                 if (rc == 0) {
1898                                         body = req_capsule_server_get(pill,
1899                                                                 &RMF_MDT_BODY);
1900                                         fid = mdt_object_fid(info->mti_object);
1901                                         mdt_pack_attr2body(info, body, la, fid);
1902                                 }
1903                         }
1904                 } else
1905                         rc = err_serious(rc);
1906         }
1907         if (rc == 0)
1908                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1909
1910         RETURN(rc);
1911 }
1912
1913 /*
1914  * Quotacheck handler.
1915  * in-kernel quotacheck isn't supported any more.
1916  */
1917 int mdt_quotacheck(struct mdt_thread_info *info)
1918 {
1919         struct obd_quotactl     *oqctl;
1920         int                      rc;
1921         ENTRY;
1922
1923         oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1924         if (oqctl == NULL)
1925                 RETURN(err_serious(-EPROTO));
1926
1927         rc = req_capsule_server_pack(info->mti_pill);
1928         if (rc)
1929                 RETURN(err_serious(rc));
1930
1931         /* deprecated, not used any more */
1932         RETURN(-EOPNOTSUPP);
1933 }
1934
1935 /*
1936  * Handle quota control requests to consult current usage/limit, but also
1937  * to configure quota enforcement
1938  */
1939 int mdt_quotactl(struct mdt_thread_info *info)
1940 {
1941         struct obd_export       *exp  = info->mti_exp;
1942         struct req_capsule      *pill = info->mti_pill;
1943         struct obd_quotactl     *oqctl, *repoqc;
1944         int                      id, rc;
1945         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
1946         ENTRY;
1947
1948         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1949         if (oqctl == NULL)
1950                 RETURN(err_serious(-EPROTO));
1951
1952         rc = req_capsule_server_pack(pill);
1953         if (rc)
1954                 RETURN(err_serious(rc));
1955
1956         switch (oqctl->qc_cmd) {
1957         case Q_QUOTACHECK:
1958         case LUSTRE_Q_INVALIDATE:
1959         case LUSTRE_Q_FINVALIDATE:
1960         case Q_QUOTAON:
1961         case Q_QUOTAOFF:
1962         case Q_INITQUOTA:
1963                 /* deprecated, not used any more */
1964                 RETURN(-EOPNOTSUPP);
1965                 /* master quotactl */
1966         case Q_GETINFO:
1967         case Q_SETINFO:
1968         case Q_SETQUOTA:
1969         case Q_GETQUOTA:
1970                 if (qmt == NULL)
1971                         RETURN(-EOPNOTSUPP);
1972                 /* slave quotactl */
1973         case Q_GETOINFO:
1974         case Q_GETOQUOTA:
1975                 break;
1976         default:
1977                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1978                 RETURN(-EFAULT);
1979         }
1980
1981         /* map uid/gid for remote client */
1982         id = oqctl->qc_id;
1983         if (exp_connect_rmtclient(exp)) {
1984                 struct lustre_idmap_table *idmap;
1985
1986                 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1987
1988                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1989                              oqctl->qc_cmd != Q_GETINFO))
1990                         RETURN(-EPERM);
1991
1992                 if (oqctl->qc_type == USRQUOTA)
1993                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1994                                                      oqctl->qc_id);
1995                 else if (oqctl->qc_type == GRPQUOTA)
1996                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1997                                                      oqctl->qc_id);
1998                 else
1999                         RETURN(-EINVAL);
2000
2001                 if (id == CFS_IDMAP_NOTFOUND) {
2002                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2003                         RETURN(-EACCES);
2004                 }
2005         }
2006
2007         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2008         if (repoqc == NULL)
2009                 RETURN(err_serious(-EFAULT));
2010
2011         if (oqctl->qc_id != id)
2012                 swap(oqctl->qc_id, id);
2013
2014         switch (oqctl->qc_cmd) {
2015
2016         case Q_GETINFO:
2017         case Q_SETINFO:
2018         case Q_SETQUOTA:
2019         case Q_GETQUOTA:
2020                 /* forward quotactl request to QMT */
2021                 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2022                 break;
2023
2024         case Q_GETOINFO:
2025         case Q_GETOQUOTA:
2026                 /* slave quotactl */
2027                 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2028                                    oqctl);
2029                 break;
2030
2031         default:
2032                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2033                 RETURN(-EFAULT);
2034         }
2035
2036         if (oqctl->qc_id != id)
2037                 swap(oqctl->qc_id, id);
2038
2039         *repoqc = *oqctl;
2040         RETURN(rc);
2041 }
2042
2043 /*
2044  * OBD PING and other handlers.
2045  */
2046 int mdt_obd_ping(struct mdt_thread_info *info)
2047 {
2048         int rc;
2049         ENTRY;
2050
2051         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2052
2053         rc = target_handle_ping(mdt_info_req(info));
2054         if (rc < 0)
2055                 rc = err_serious(rc);
2056         RETURN(rc);
2057 }
2058
2059 /*
2060  * OBD_IDX_READ handler
2061  */
2062 int mdt_obd_idx_read(struct mdt_thread_info *info)
2063 {
2064         struct mdt_device       *mdt = info->mti_mdt;
2065         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2066         struct idx_info         *req_ii, *rep_ii;
2067         int                      rc, i;
2068         ENTRY;
2069
2070         memset(rdpg, 0, sizeof(*rdpg));
2071         req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2072
2073         /* extract idx_info buffer from request & reply */
2074         req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2075         if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2076                 RETURN(err_serious(-EPROTO));
2077
2078         rc = req_capsule_server_pack(info->mti_pill);
2079         if (rc)
2080                 RETURN(err_serious(rc));
2081
2082         rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2083         if (rep_ii == NULL)
2084                 RETURN(err_serious(-EFAULT));
2085         rep_ii->ii_magic = IDX_INFO_MAGIC;
2086
2087         /* extract hash to start with */
2088         rdpg->rp_hash = req_ii->ii_hash_start;
2089
2090         /* extract requested attributes */
2091         rdpg->rp_attrs = req_ii->ii_attrs;
2092
2093         /* check that fid packed in request is valid and supported */
2094         if (!fid_is_sane(&req_ii->ii_fid))
2095                 RETURN(-EINVAL);
2096         rep_ii->ii_fid = req_ii->ii_fid;
2097
2098         /* copy flags */
2099         rep_ii->ii_flags = req_ii->ii_flags;
2100
2101         /* compute number of pages to allocate, ii_count is the number of 4KB
2102          * containers */
2103         if (req_ii->ii_count <= 0)
2104                 GOTO(out, rc = -EFAULT);
2105         rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2106                                exp_max_brw_size(info->mti_exp));
2107         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2108
2109         /* allocate pages to store the containers */
2110         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2111         if (rdpg->rp_pages == NULL)
2112                 GOTO(out, rc = -ENOMEM);
2113         for (i = 0; i < rdpg->rp_npages; i++) {
2114                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2115                 if (rdpg->rp_pages[i] == NULL)
2116                         GOTO(out, rc = -ENOMEM);
2117         }
2118
2119         /* populate pages with key/record pairs */
2120         rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2121         if (rc < 0)
2122                 GOTO(out, rc);
2123
2124         LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2125                  "asked %d > %d\n", rc, rdpg->rp_count);
2126
2127         /* send pages to client */
2128         rc = mdt_sendpage(info, rdpg, rc);
2129
2130         GOTO(out, rc);
2131 out:
2132         if (rdpg->rp_pages) {
2133                 for (i = 0; i < rdpg->rp_npages; i++)
2134                         if (rdpg->rp_pages[i])
2135                                 cfs_free_page(rdpg->rp_pages[i]);
2136                 OBD_FREE(rdpg->rp_pages,
2137                          rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2138         }
2139         return rc;
2140 }
2141
2142 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2143 {
2144         return err_serious(-EOPNOTSUPP);
2145 }
2146
2147 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2148 {
2149         return err_serious(-EOPNOTSUPP);
2150 }
2151
2152 /*
2153  * LLOG handlers.
2154  */
2155
2156 /** clone llog ctxt from child (mdd)
2157  * This allows remote llog (replicator) access.
2158  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2159  * context was originally set up, or we can handle them directly.
2160  * I choose the latter, but that means I need any llog
2161  * contexts set up by child to be accessable by the mdt.  So we clone the
2162  * context into our context list here.
2163  */
2164 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2165                                int idx)
2166 {
2167         struct md_device  *next = mdt->mdt_child;
2168         struct llog_ctxt *ctxt;
2169         int rc;
2170
2171         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2172                 return 0;
2173
2174         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2175         if (rc || ctxt == NULL) {
2176                 return 0;
2177         }
2178
2179         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2180         if (rc)
2181                 CERROR("Can't set mdt ctxt %d\n", rc);
2182
2183         return rc;
2184 }
2185
2186 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2187                                  struct mdt_device *mdt, int idx)
2188 {
2189         struct llog_ctxt *ctxt;
2190
2191         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2192         if (ctxt == NULL)
2193                 return 0;
2194         /* Put once for the get we just did, and once for the clone */
2195         llog_ctxt_put(ctxt);
2196         llog_ctxt_put(ctxt);
2197         return 0;
2198 }
2199
2200 int mdt_llog_create(struct mdt_thread_info *info)
2201 {
2202         int rc;
2203
2204         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2205         rc = llog_origin_handle_open(mdt_info_req(info));
2206         return (rc < 0 ? err_serious(rc) : rc);
2207 }
2208
2209 int mdt_llog_destroy(struct mdt_thread_info *info)
2210 {
2211         int rc;
2212
2213         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2214         rc = llog_origin_handle_destroy(mdt_info_req(info));
2215         return (rc < 0 ? err_serious(rc) : rc);
2216 }
2217
2218 int mdt_llog_read_header(struct mdt_thread_info *info)
2219 {
2220         int rc;
2221
2222         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2223         rc = llog_origin_handle_read_header(mdt_info_req(info));
2224         return (rc < 0 ? err_serious(rc) : rc);
2225 }
2226
2227 int mdt_llog_next_block(struct mdt_thread_info *info)
2228 {
2229         int rc;
2230
2231         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2232         rc = llog_origin_handle_next_block(mdt_info_req(info));
2233         return (rc < 0 ? err_serious(rc) : rc);
2234 }
2235
2236 int mdt_llog_prev_block(struct mdt_thread_info *info)
2237 {
2238         int rc;
2239
2240         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2241         rc = llog_origin_handle_prev_block(mdt_info_req(info));
2242         return (rc < 0 ? err_serious(rc) : rc);
2243 }
2244
2245
2246 /*
2247  * DLM handlers.
2248  */
2249
2250 static struct ldlm_callback_suite cbs = {
2251         .lcs_completion = ldlm_server_completion_ast,
2252         .lcs_blocking   = ldlm_server_blocking_ast,
2253         .lcs_glimpse    = ldlm_server_glimpse_ast
2254 };
2255
2256 int mdt_enqueue(struct mdt_thread_info *info)
2257 {
2258         struct ptlrpc_request *req;
2259         int rc;
2260
2261         /*
2262          * info->mti_dlm_req already contains swapped and (if necessary)
2263          * converted dlm request.
2264          */
2265         LASSERT(info->mti_dlm_req != NULL);
2266
2267         req = mdt_info_req(info);
2268         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2269                                   req, info->mti_dlm_req, &cbs);
2270         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2271         return rc ? err_serious(rc) : req->rq_status;
2272 }
2273
2274 int mdt_convert(struct mdt_thread_info *info)
2275 {
2276         int rc;
2277         struct ptlrpc_request *req;
2278
2279         LASSERT(info->mti_dlm_req);
2280         req = mdt_info_req(info);
2281         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2282         return rc ? err_serious(rc) : req->rq_status;
2283 }
2284
2285 int mdt_bl_callback(struct mdt_thread_info *info)
2286 {
2287         CERROR("bl callbacks should not happen on MDS\n");
2288         LBUG();
2289         return err_serious(-EOPNOTSUPP);
2290 }
2291
2292 int mdt_cp_callback(struct mdt_thread_info *info)
2293 {
2294         CERROR("cp callbacks should not happen on MDS\n");
2295         LBUG();
2296         return err_serious(-EOPNOTSUPP);
2297 }
2298
2299 /*
2300  * sec context handlers
2301  */
2302 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2303 {
2304         int rc;
2305
2306         rc = mdt_handle_idmap(info);
2307
2308         if (unlikely(rc)) {
2309                 struct ptlrpc_request *req = mdt_info_req(info);
2310                 __u32                  opc;
2311
2312                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2313                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2314                         sptlrpc_svc_ctx_invalidate(req);
2315         }
2316
2317         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2318
2319         return rc;
2320 }
2321
2322 /*
2323  * quota request handlers
2324  */
2325 int mdt_quota_dqacq(struct mdt_thread_info *info)
2326 {
2327         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
2328         int                      rc;
2329         ENTRY;
2330
2331         if (qmt == NULL)
2332                 RETURN(err_serious(-EOPNOTSUPP));
2333
2334         rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2335         RETURN(rc);
2336 }
2337
2338 static struct mdt_object *mdt_obj(struct lu_object *o)
2339 {
2340         LASSERT(lu_device_is_mdt(o->lo_dev));
2341         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2342 }
2343
2344 struct mdt_object *mdt_object_new(const struct lu_env *env,
2345                                   struct mdt_device *d,
2346                                   const struct lu_fid *f)
2347 {
2348         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2349         struct lu_object *o;
2350         struct mdt_object *m;
2351         ENTRY;
2352
2353         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2354         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2355         if (unlikely(IS_ERR(o)))
2356                 m = (struct mdt_object *)o;
2357         else
2358                 m = mdt_obj(o);
2359         RETURN(m);
2360 }
2361
2362 struct mdt_object *mdt_object_find(const struct lu_env *env,
2363                                    struct mdt_device *d,
2364                                    const struct lu_fid *f)
2365 {
2366         struct lu_object *o;
2367         struct mdt_object *m;
2368         ENTRY;
2369
2370         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2371         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2372         if (unlikely(IS_ERR(o)))
2373                 m = (struct mdt_object *)o;
2374         else
2375                 m = mdt_obj(o);
2376         RETURN(m);
2377 }
2378
2379 /**
2380  * Asyncronous commit for mdt device.
2381  *
2382  * Pass asynchonous commit call down the MDS stack.
2383  *
2384  * \param env environment
2385  * \param mdt the mdt device
2386  */
2387 static void mdt_device_commit_async(const struct lu_env *env,
2388                                     struct mdt_device *mdt)
2389 {
2390         struct dt_device *dt = mdt->mdt_bottom;
2391         int rc;
2392
2393         rc = dt->dd_ops->dt_commit_async(env, dt);
2394         if (unlikely(rc != 0))
2395                 CWARN("async commit start failed with rc = %d", rc);
2396 }
2397
2398 /**
2399  * Mark the lock as "synchonous".
2400  *
2401  * Mark the lock to deffer transaction commit to the unlock time.
2402  *
2403  * \param lock the lock to mark as "synchonous"
2404  *
2405  * \see mdt_is_lock_sync
2406  * \see mdt_save_lock
2407  */
2408 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2409 {
2410         lock->l_ast_data = (void*)1;
2411 }
2412
2413 /**
2414  * Check whehter the lock "synchonous" or not.
2415  *
2416  * \param lock the lock to check
2417  * \retval 1 the lock is "synchonous"
2418  * \retval 0 the lock isn't "synchronous"
2419  *
2420  * \see mdt_set_lock_sync
2421  * \see mdt_save_lock
2422  */
2423 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2424 {
2425         return lock->l_ast_data != NULL;
2426 }
2427
2428 /**
2429  * Blocking AST for mdt locks.
2430  *
2431  * Starts transaction commit if in case of COS lock conflict or
2432  * deffers such a commit to the mdt_save_lock.
2433  *
2434  * \param lock the lock which blocks a request or cancelling lock
2435  * \param desc unused
2436  * \param data unused
2437  * \param flag indicates whether this cancelling or blocking callback
2438  * \retval 0
2439  * \see ldlm_blocking_ast_nocheck
2440  */
2441 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2442                      void *data, int flag)
2443 {
2444         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2445         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2446         int rc;
2447         ENTRY;
2448
2449         if (flag == LDLM_CB_CANCELING)
2450                 RETURN(0);
2451         lock_res_and_lock(lock);
2452         if (lock->l_blocking_ast != mdt_blocking_ast) {
2453                 unlock_res_and_lock(lock);
2454                 RETURN(0);
2455         }
2456         if (mdt_cos_is_enabled(mdt) &&
2457             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2458             lock->l_blocking_lock != NULL &&
2459             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2460                 mdt_set_lock_sync(lock);
2461         }
2462         rc = ldlm_blocking_ast_nocheck(lock);
2463
2464         /* There is no lock conflict if l_blocking_lock == NULL,
2465          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2466          * when the last reference to a local lock was released */
2467         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2468                 struct lu_env env;
2469
2470                 rc = lu_env_init(&env, LCT_LOCAL);
2471                 if (unlikely(rc != 0))
2472                         CWARN("lu_env initialization failed with rc = %d,"
2473                               "cannot start asynchronous commit\n", rc);
2474                 else
2475                         mdt_device_commit_async(&env, mdt);
2476                 lu_env_fini(&env);
2477         }
2478         RETURN(rc);
2479 }
2480
2481 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2482                         void *data, int flag)
2483 {
2484         struct lustre_handle lockh;
2485         int               rc;
2486
2487         switch (flag) {
2488         case LDLM_CB_BLOCKING:
2489                 ldlm_lock2handle(lock, &lockh);
2490                 rc = ldlm_cli_cancel(&lockh);
2491                 if (rc < 0) {
2492                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2493                         RETURN(rc);
2494                 }
2495                 break;
2496         case LDLM_CB_CANCELING:
2497                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2498                 break;
2499         default:
2500                 LBUG();
2501         }
2502         RETURN(0);
2503 }
2504
2505 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2506                            struct mdt_object *o, struct lustre_handle *lh,
2507                            ldlm_mode_t mode, __u64 ibits)
2508 {
2509         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2510         ldlm_policy_data_t *policy = &mti->mti_policy;
2511         int rc = 0;
2512         ENTRY;
2513
2514         LASSERT(mdt_object_remote(o));
2515
2516         LASSERT((ibits & MDS_INODELOCK_UPDATE));
2517
2518         memset(einfo, 0, sizeof(*einfo));
2519         einfo->ei_type = LDLM_IBITS;
2520         einfo->ei_mode = mode;
2521         einfo->ei_cb_bl = mdt_md_blocking_ast;
2522         einfo->ei_cb_cp = ldlm_completion_ast;
2523
2524         memset(policy, 0, sizeof(*policy));
2525         policy->l_inodebits.bits = ibits;
2526
2527         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2528                             policy);
2529         RETURN(rc);
2530 }
2531
2532 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2533                             struct mdt_lock_handle *lh, __u64 ibits,
2534                             bool nonblock, int locality)
2535 {
2536         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2537         ldlm_policy_data_t *policy = &info->mti_policy;
2538         struct ldlm_res_id *res_id = &info->mti_res_id;
2539         __u64 dlmflags;
2540         int rc;
2541         ENTRY;
2542
2543         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2544         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2545         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2546         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2547
2548         if (mdt_object_remote(o)) {
2549                 if (locality == MDT_CROSS_LOCK) {
2550                         ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2551                         ibits |= MDS_INODELOCK_LOOKUP;
2552                 } else {
2553                         LASSERTF(!(ibits &
2554                                   (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2555                                 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2556                                 mdt_obd_name(info->mti_mdt), ibits,
2557                                 PFID(mdt_object_fid(o)));
2558                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2559                 }
2560                 /* No PDO lock on remote object */
2561                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2562         }
2563
2564         if (lh->mlh_type == MDT_PDO_LOCK) {
2565                 /* check for exists after object is locked */
2566                 if (mdt_object_exists(o) == 0) {
2567                         /* Non-existent object shouldn't have PDO lock */
2568                         RETURN(-ESTALE);
2569                 } else {
2570                         /* Non-dir object shouldn't have PDO lock */
2571                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2572                                 RETURN(-ENOTDIR);
2573                 }
2574         }
2575
2576         memset(policy, 0, sizeof(*policy));
2577         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2578
2579         dlmflags = LDLM_FL_ATOMIC_CB;
2580         if (nonblock)
2581                 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2582
2583         /*
2584          * Take PDO lock on whole directory and build correct @res_id for lock
2585          * on part of directory.
2586          */
2587         if (lh->mlh_pdo_hash != 0) {
2588                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2589                 mdt_lock_pdo_mode(info, o, lh);
2590                 if (lh->mlh_pdo_mode != LCK_NL) {
2591                         /*
2592                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2593                          * is never going to be sent to client and we do not
2594                          * want it slowed down due to possible cancels.
2595                          */
2596                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2597                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2598                                           policy, res_id, dlmflags,
2599                                           &info->mti_exp->exp_handle.h_cookie);
2600                         if (unlikely(rc))
2601                                 RETURN(rc);
2602                 }
2603
2604                 /*
2605                  * Finish res_id initializing by name hash marking part of
2606                  * directory which is taking modification.
2607                  */
2608                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2609         }
2610
2611         policy->l_inodebits.bits = ibits;
2612
2613         /*
2614          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2615          * going to be sent to client. If it is - mdt_intent_policy() path will
2616          * fix it up and turn FL_LOCAL flag off.
2617          */
2618         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2619                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2620                           &info->mti_exp->exp_handle.h_cookie);
2621         if (rc)
2622                 mdt_object_unlock(info, o, lh, 1);
2623         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2624                  lh->mlh_pdo_hash != 0 &&
2625                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2626                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2627         }
2628
2629         RETURN(rc);
2630 }
2631
2632 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2633                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2634 {
2635         return mdt_object_lock0(info, o, lh, ibits, false, locality);
2636 }
2637
2638 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2639                         struct mdt_lock_handle *lh, __u64 ibits, int locality)
2640 {
2641         struct mdt_lock_handle tmp = *lh;
2642         int rc;
2643
2644         rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2645         if (rc == 0)
2646                 *lh = tmp;
2647
2648         return rc == 0;
2649 }
2650
2651 /**
2652  * Save a lock within request object.
2653  *
2654  * Keep the lock referenced until whether client ACK or transaction
2655  * commit happens or release the lock immediately depending on input
2656  * parameters. If COS is ON, a write lock is converted to COS lock
2657  * before saving.
2658  *
2659  * \param info thead info object
2660  * \param h lock handle
2661  * \param mode lock mode
2662  * \param decref force immediate lock releasing
2663  */
2664 static
2665 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2666                    ldlm_mode_t mode, int decref)
2667 {
2668         ENTRY;
2669
2670         if (lustre_handle_is_used(h)) {
2671                 if (decref || !info->mti_has_trans ||
2672                     !(mode & (LCK_PW | LCK_EX))){
2673                         mdt_fid_unlock(h, mode);
2674                 } else {
2675                         struct mdt_device *mdt = info->mti_mdt;
2676                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2677                         struct ptlrpc_request *req = mdt_info_req(info);
2678                         int no_ack = 0;
2679
2680                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2681                                  h->cookie);
2682                         CDEBUG(D_HA, "request = %p reply state = %p"
2683                                " transno = "LPD64"\n",
2684                                req, req->rq_reply_state, req->rq_transno);
2685                         if (mdt_cos_is_enabled(mdt)) {
2686                                 no_ack = 1;
2687                                 ldlm_lock_downgrade(lock, LCK_COS);
2688                                 mode = LCK_COS;
2689                         }
2690                         ptlrpc_save_lock(req, h, mode, no_ack);
2691                         if (mdt_is_lock_sync(lock)) {
2692                                 CDEBUG(D_HA, "found sync-lock,"
2693                                        " async commit started\n");
2694                                 mdt_device_commit_async(info->mti_env,
2695                                                         mdt);
2696                         }
2697                         LDLM_LOCK_PUT(lock);
2698                 }
2699                 h->cookie = 0ull;
2700         }
2701
2702         EXIT;
2703 }
2704
2705 /**
2706  * Unlock mdt object.
2707  *
2708  * Immeditely release the regular lock and the PDO lock or save the
2709  * lock in reqeuest and keep them referenced until client ACK or
2710  * transaction commit.
2711  *
2712  * \param info thread info object
2713  * \param o mdt object
2714  * \param lh mdt lock handle referencing regular and PDO locks
2715  * \param decref force immediate lock releasing
2716  */
2717 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2718                        struct mdt_lock_handle *lh, int decref)
2719 {
2720         ENTRY;
2721
2722         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2723         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2724
2725         if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2726                 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2727
2728         EXIT;
2729 }
2730
2731 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2732                                         const struct lu_fid *f,
2733                                         struct mdt_lock_handle *lh,
2734                                         __u64 ibits)
2735 {
2736         struct mdt_object *o;
2737
2738         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2739         if (!IS_ERR(o)) {
2740                 int rc;
2741
2742                 rc = mdt_object_lock(info, o, lh, ibits,
2743                                      MDT_LOCAL_LOCK);
2744                 if (rc != 0) {
2745                         mdt_object_put(info->mti_env, o);
2746                         o = ERR_PTR(rc);
2747                 }
2748         }
2749         return o;
2750 }
2751
2752 void mdt_object_unlock_put(struct mdt_thread_info * info,
2753                            struct mdt_object * o,
2754                            struct mdt_lock_handle *lh,
2755                            int decref)
2756 {
2757         mdt_object_unlock(info, o, lh, decref);
2758         mdt_object_put(info->mti_env, o);
2759 }
2760
2761 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2762 {
2763         struct mdt_opc_slice *s;
2764         struct mdt_handler   *h;
2765
2766         h = NULL;
2767         for (s = supported; s->mos_hs != NULL; s++) {
2768                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2769                         h = s->mos_hs + (opc - s->mos_opc_start);
2770                         if (likely(h->mh_opc != 0))
2771                                 LASSERTF(h->mh_opc == opc,
2772                                          "opcode mismatch %d != %d\n",
2773                                          h->mh_opc, opc);
2774                         else
2775                                 h = NULL; /* unsupported opc */
2776                         break;
2777                 }
2778         }
2779         return h;
2780 }
2781
2782 static int mdt_lock_resname_compat(struct mdt_device *m,
2783                                    struct ldlm_request *req)
2784 {
2785         /* XXX something... later. */
2786         return 0;
2787 }
2788
2789 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2790 {
2791         /* XXX something... later. */
2792         return 0;
2793 }
2794
2795 /*
2796  * Generic code handling requests that have struct mdt_body passed in:
2797  *
2798  *  - extract mdt_body from request and save it in @info, if present;
2799  *
2800  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2801  *  @info;
2802  *
2803  *  - if HABEO_CORPUS flag is set for this request type check whether object
2804  *  actually exists on storage (lu_object_exists()).
2805  *
2806  */
2807 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2808 {
2809         const struct mdt_body    *body;
2810         struct mdt_object        *obj;
2811         const struct lu_env      *env;
2812         struct req_capsule       *pill;
2813         int                       rc;
2814         ENTRY;
2815
2816         env = info->mti_env;
2817         pill = info->mti_pill;
2818
2819         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2820         if (body == NULL)
2821                 RETURN(-EFAULT);
2822
2823         if (!(body->valid & OBD_MD_FLID))
2824                 RETURN(0);
2825
2826         if (!fid_is_sane(&body->fid1)) {
2827                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2828                 RETURN(-EINVAL);
2829         }
2830
2831         /*
2832          * Do not get size or any capa fields before we check that request
2833          * contains capa actually. There are some requests which do not, for
2834          * instance MDS_IS_SUBDIR.
2835          */
2836         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2837             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2838                 mdt_set_capainfo(info, 0, &body->fid1,
2839                                  req_capsule_client_get(pill, &RMF_CAPA1));
2840
2841         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2842         if (!IS_ERR(obj)) {
2843                 if ((flags & HABEO_CORPUS) &&
2844                     !mdt_object_exists(obj)) {
2845                         mdt_object_put(env, obj);
2846                         /* for capability renew ENOENT will be handled in
2847                          * mdt_renew_capa */
2848                         if (body->valid & OBD_MD_FLOSSCAPA)
2849                                 rc = 0;
2850                         else
2851                                 rc = -ENOENT;
2852                 } else {
2853                         info->mti_object = obj;
2854                         rc = 0;
2855                 }
2856         } else
2857                 rc = PTR_ERR(obj);
2858
2859         RETURN(rc);
2860 }
2861
2862 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2863 {
2864         struct req_capsule *pill = info->mti_pill;
2865         int rc;
2866         ENTRY;
2867
2868         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2869                 rc = mdt_body_unpack(info, flags);
2870         else
2871                 rc = 0;
2872
2873         if (rc == 0 && (flags & HABEO_REFERO)) {
2874                 /* Pack reply. */
2875                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2876                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2877                                              info->mti_body->eadatasize);
2878                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2879                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2880                                              RCL_SERVER, 0);
2881
2882                 rc = req_capsule_server_pack(pill);
2883         }
2884         RETURN(rc);
2885 }
2886
2887 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2888 {
2889         struct md_device *next = m->mdt_child;
2890
2891         return next->md_ops->mdo_init_capa_ctxt(env, next,
2892                                                 m->mdt_opts.mo_mds_capa,
2893                                                 m->mdt_capa_timeout,
2894                                                 m->mdt_capa_alg,
2895                                                 m->mdt_capa_keys);
2896 }
2897
2898 /*
2899  * Invoke handler for this request opc. Also do necessary preprocessing
2900  * (according to handler ->mh_flags), and post-processing (setting of
2901  * ->last_{xid,committed}).
2902  */
2903 static int mdt_req_handle(struct mdt_thread_info *info,
2904                           struct mdt_handler *h, struct ptlrpc_request *req)
2905 {
2906         int   rc, serious = 0;
2907         __u32 flags;
2908
2909         ENTRY;
2910
2911         LASSERT(h->mh_act != NULL);
2912         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2913         LASSERT(current->journal_info == NULL);
2914
2915         /*
2916          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2917          * to put same checks into handlers like mdt_close(), mdt_reint(),
2918          * etc., without talking to mdt authors first. Checking same thing
2919          * there again is useless and returning 0 error without packing reply
2920          * is buggy! Handlers either pack reply or return error.
2921          *
2922          * We return 0 here and do not send any reply in order to emulate
2923          * network failure. Do not send any reply in case any of NET related
2924          * fail_id has occured.
2925          */
2926         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2927                 RETURN(0);
2928
2929         rc = 0;
2930         flags = h->mh_flags;
2931         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2932
2933         if (h->mh_fmt != NULL) {
2934                 req_capsule_set(info->mti_pill, h->mh_fmt);
2935                 rc = mdt_unpack_req_pack_rep(info, flags);
2936         }
2937
2938         if (rc == 0 && flags & MUTABOR &&
2939             exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2940                 /* should it be rq_status? */
2941                 rc = -EROFS;
2942
2943         if (rc == 0 && flags & HABEO_CLAVIS) {
2944                 struct ldlm_request *dlm_req;
2945
2946                 LASSERT(h->mh_fmt != NULL);
2947
2948                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2949                 if (dlm_req != NULL) {
2950                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2951                                         LDLM_IBITS &&
2952                                      dlm_req->lock_desc.l_policy_data.\
2953                                         l_inodebits.bits == 0)) {
2954                                 /*
2955                                  * Lock without inodebits makes no sense and
2956                                  * will oops later in ldlm. If client miss to
2957                                  * set such bits, do not trigger ASSERTION.
2958                                  *
2959                                  * For liblustre flock case, it maybe zero.
2960                                  */
2961                                 rc = -EPROTO;
2962                         } else {
2963                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2964                                         rc = mdt_lock_resname_compat(
2965                                                                 info->mti_mdt,
2966                                                                 dlm_req);
2967                                 info->mti_dlm_req = dlm_req;
2968                         }
2969                 } else {
2970                         rc = -EFAULT;
2971                 }
2972         }
2973
2974         /* capability setting changed via /proc, needs reinitialize ctxt */
2975         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2976                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2977                 info->mti_mdt->mdt_capa_conf = 0;
2978         }
2979
2980         if (likely(rc == 0)) {
2981                 /*
2982                  * Process request, there can be two types of rc:
2983                  * 1) errors with msg unpack/pack, other failures outside the
2984                  * operation itself. This is counted as serious errors;
2985                  * 2) errors during fs operation, should be placed in rq_status
2986                  * only
2987                  */
2988                 rc = h->mh_act(info);
2989                 if (rc == 0 &&
2990                     !req->rq_no_reply && req->rq_reply_state == NULL) {
2991                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2992                                   "pack reply and returned 0 error\n",
2993                                   h->mh_name);
2994                         LBUG();
2995                 }
2996                 serious = is_serious(rc);
2997                 rc = clear_serious(rc);
2998         } else
2999                 serious = 1;
3000
3001         req->rq_status = rc;
3002
3003         /*
3004          * ELDLM_* codes which > 0 should be in rq_status only as well as
3005          * all non-serious errors.
3006          */
3007         if (rc > 0 || !serious)
3008                 rc = 0;
3009
3010         LASSERT(current->journal_info == NULL);
3011
3012         if (rc == 0 && (flags & HABEO_CLAVIS) &&
3013             info->mti_mdt->mdt_opts.mo_compat_resname) {
3014                 struct ldlm_reply *dlmrep;
3015
3016                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3017                 if (dlmrep != NULL)
3018                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3019         }
3020
3021         /* If we're DISCONNECTing, the mdt_export_data is already freed */
3022         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3023                 target_committed_to_req(req);
3024
3025         if (unlikely(req_is_replay(req) &&
3026                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3027                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3028                 LBUG();
3029         }
3030
3031         target_send_reply(req, rc, info->mti_fail_id);
3032         RETURN(0);
3033 }
3034
3035 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3036 {
3037         lh->mlh_type = MDT_NUL_LOCK;
3038         lh->mlh_reg_lh.cookie = 0ull;
3039         lh->mlh_reg_mode = LCK_MINMODE;
3040         lh->mlh_pdo_lh.cookie = 0ull;
3041         lh->mlh_pdo_mode = LCK_MINMODE;
3042         lh->mlh_rreg_lh.cookie = 0ull;
3043         lh->mlh_rreg_mode = LCK_MINMODE;
3044 }
3045
3046 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3047 {
3048         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3049         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3050 }
3051
3052 /*
3053  * Initialize fields of struct mdt_thread_info. Other fields are left in
3054  * uninitialized state, because it's too expensive to zero out whole
3055  * mdt_thread_info (> 1K) on each request arrival.
3056  */
3057 static void mdt_thread_info_init(struct ptlrpc_request *req,
3058                                  struct mdt_thread_info *info)
3059 {
3060         int i;
3061
3062         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3063         info->mti_pill = &req->rq_pill;
3064
3065         /* lock handle */
3066         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3067                 mdt_lock_handle_init(&info->mti_lh[i]);
3068
3069         /* mdt device: it can be NULL while CONNECT */
3070         if (req->rq_export) {
3071                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3072                 info->mti_exp = req->rq_export;
3073         } else
3074                 info->mti_mdt = NULL;
3075         info->mti_env = req->rq_svc_thread->t_env;
3076         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3077         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3078         info->mti_mos = NULL;
3079
3080         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3081         info->mti_body = NULL;
3082         info->mti_object = NULL;
3083         info->mti_dlm_req = NULL;
3084         info->mti_has_trans = 0;
3085         info->mti_cross_ref = 0;
3086         info->mti_opdata = 0;
3087         info->mti_big_lmm_used = 0;
3088
3089         /* To not check for split by default. */
3090         info->mti_spec.no_create = 0;
3091         info->mti_spec.sp_rm_entry = 0;
3092 }
3093
3094 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3095 {
3096         int i;
3097
3098         req_capsule_fini(info->mti_pill);
3099         if (info->mti_object != NULL) {
3100                 mdt_object_put(info->mti_env, info->mti_object);
3101                 info->mti_object = NULL;
3102         }
3103         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3104                 mdt_lock_handle_fini(&info->mti_lh[i]);
3105         info->mti_env = NULL;
3106 }
3107
3108 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3109                                        struct obd_device *obd, int *process)
3110 {
3111         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3112         case MDS_CONNECT: /* This will never get here, but for completeness. */
3113         case OST_CONNECT: /* This will never get here, but for completeness. */
3114         case MDS_DISCONNECT:
3115         case OST_DISCONNECT:
3116         case OBD_IDX_READ:
3117                *process = 1;
3118                RETURN(0);
3119
3120         case MDS_CLOSE:
3121         case MDS_DONE_WRITING:
3122         case MDS_SYNC: /* used in unmounting */
3123         case OBD_PING:
3124         case MDS_REINT:
3125         case UPDATE_OBJ:
3126         case SEQ_QUERY:
3127         case FLD_QUERY:
3128         case LDLM_ENQUEUE:
3129                 *process = target_queue_recovery_request(req, obd);
3130                 RETURN(0);
3131
3132         default:
3133                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3134                 *process = -EAGAIN;
3135                 RETURN(0);
3136         }
3137 }
3138
3139 /*
3140  * Handle recovery. Return:
3141  *        +1: continue request processing;
3142  *       -ve: abort immediately with the given error code;
3143  *         0: send reply with error code in req->rq_status;
3144  */
3145 static int mdt_recovery(struct mdt_thread_info *info)
3146 {
3147         struct ptlrpc_request *req = mdt_info_req(info);
3148         struct obd_device *obd;
3149
3150         ENTRY;
3151
3152         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3153         case MDS_CONNECT:
3154         case SEC_CTX_INIT:
3155         case SEC_CTX_INIT_CONT:
3156         case SEC_CTX_FINI:
3157                 {
3158 #if 0
3159                         int rc;
3160
3161                         rc = mdt_handle_idmap(info);
3162                         if (rc)
3163                                 RETURN(rc);
3164                         else
3165 #endif
3166                                 RETURN(+1);
3167                 }
3168         }
3169
3170         if (unlikely(!class_connected_export(req->rq_export))) {
3171                 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3172                        lustre_msg_get_opc(req->rq_reqmsg),
3173                        libcfs_id2str(req->rq_peer));
3174                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3175                  * mds_A will get -ENOTCONN(especially for ping req),
3176                  * which will cause that mds_A deactive timeout, then when
3177                  * mds_A cleanup, the cleanup process will be suspended since
3178                  * deactive timeout is not zero.
3179                  */
3180                 req->rq_status = -ENOTCONN;
3181                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3182                 RETURN(0);
3183         }
3184
3185         /* sanity check: if the xid matches, the request must be marked as a
3186          * resent or replayed */
3187         if (req_xid_is_last(req)) {
3188                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3189                       (MSG_RESENT | MSG_REPLAY))) {
3190                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3191                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3192                                   lustre_msg_get_flags(req->rq_reqmsg));
3193                         LBUG();
3194                         req->rq_status = -ENOTCONN;
3195                         RETURN(-ENOTCONN);
3196                 }
3197         }
3198
3199         /* else: note the opposite is not always true; a RESENT req after a
3200          * failover will usually not match the last_xid, since it was likely
3201          * never committed. A REPLAYed request will almost never match the
3202          * last xid, however it could for a committed, but still retained,
3203          * open. */
3204
3205         obd = req->rq_export->exp_obd;
3206
3207         /* Check for aborted recovery... */
3208         if (unlikely(obd->obd_recovering)) {
3209                 int rc;
3210                 int should_process;
3211                 DEBUG_REQ(D_INFO, req, "Got new replay");
3212                 rc = mdt_filter_recovery_request(req, obd, &should_process);
3213                 if (rc != 0 || !should_process)
3214                         RETURN(rc);
3215                 else if (should_process < 0) {
3216                         req->rq_status = should_process;
3217                         rc = ptlrpc_error(req);
3218                         RETURN(rc);
3219                 }
3220         }
3221         RETURN(+1);
3222 }
3223
3224 static int mdt_msg_check_version(struct lustre_msg *msg)
3225 {
3226         int rc;
3227
3228         switch (lustre_msg_get_opc(msg)) {
3229         case MDS_CONNECT:
3230         case MDS_DISCONNECT:
3231         case OBD_PING:
3232         case SEC_CTX_INIT:
3233         case SEC_CTX_INIT_CONT:
3234         case SEC_CTX_FINI:
3235         case OBD_IDX_READ:
3236                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3237                 if (rc)
3238                         CERROR("bad opc %u version %08x, expecting %08x\n",
3239                                lustre_msg_get_opc(msg),
3240                                lustre_msg_get_version(msg),
3241                                LUSTRE_OBD_VERSION);
3242                 break;
3243         case MDS_GETSTATUS:
3244         case MDS_GETATTR:
3245         case MDS_GETATTR_NAME:
3246         case MDS_STATFS:
3247         case MDS_READPAGE:
3248         case MDS_WRITEPAGE:
3249         case MDS_IS_SUBDIR:
3250         case MDS_REINT:
3251         case MDS_CLOSE:
3252         case MDS_DONE_WRITING:
3253         case MDS_PIN:
3254         case MDS_SYNC:
3255         case MDS_GETXATTR:
3256         case MDS_SETXATTR:
3257         case MDS_SET_INFO:
3258         case MDS_GET_INFO:
3259         case MDS_HSM_PROGRESS:
3260         case MDS_HSM_REQUEST:
3261         case MDS_HSM_CT_REGISTER:
3262         case MDS_HSM_CT_UNREGISTER:
3263         case MDS_HSM_STATE_GET:
3264         case MDS_HSM_STATE_SET:
3265         case MDS_HSM_ACTION:
3266         case MDS_QUOTACHECK:
3267         case MDS_QUOTACTL:
3268         case UPDATE_OBJ:
3269         case MDS_SWAP_LAYOUTS:
3270         case QUOTA_DQACQ:
3271         case QUOTA_DQREL:
3272         case SEQ_QUERY:
3273         case FLD_QUERY:
3274                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3275                 if (rc)
3276                         CERROR("bad opc %u version %08x, expecting %08x\n",
3277                                lustre_msg_get_opc(msg),
3278                                lustre_msg_get_version(msg),
3279                                LUSTRE_MDS_VERSION);
3280                 break;
3281         case LDLM_ENQUEUE:
3282         case LDLM_CONVERT:
3283         case LDLM_BL_CALLBACK:
3284         case LDLM_CP_CALLBACK:
3285                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3286                 if (rc)
3287                         CERROR("bad opc %u version %08x, expecting %08x\n",
3288                                lustre_msg_get_opc(msg),
3289                                lustre_msg_get_version(msg),
3290                                LUSTRE_DLM_VERSION);
3291                 break;
3292         case OBD_LOG_CANCEL:
3293         case LLOG_ORIGIN_HANDLE_CREATE:
3294         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3295         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3296         case LLOG_ORIGIN_HANDLE_CLOSE:
3297         case LLOG_ORIGIN_HANDLE_DESTROY:
3298         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3299         case LLOG_CATINFO:
3300                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3301                 if (rc)
3302                         CERROR("bad opc %u version %08x, expecting %08x\n",
3303                                lustre_msg_get_opc(msg),
3304                                lustre_msg_get_version(msg),
3305                                LUSTRE_LOG_VERSION);
3306                 break;
3307         default:
3308                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3309                 rc = -ENOTSUPP;
3310         }
3311         return rc;
3312 }
3313
3314 static int mdt_handle0(struct ptlrpc_request *req,
3315                        struct mdt_thread_info *info,
3316                        struct mdt_opc_slice *supported)
3317 {
3318         struct mdt_handler *h;
3319         struct lustre_msg  *msg;
3320         int                 rc;
3321
3322         ENTRY;
3323
3324         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3325                 RETURN(0);
3326
3327         LASSERT(current->journal_info == NULL);
3328
3329         msg = req->rq_reqmsg;
3330         rc = mdt_msg_check_version(msg);
3331         if (likely(rc == 0)) {
3332                 rc = mdt_recovery(info);
3333                 if (likely(rc == +1)) {
3334                         h = mdt_handler_find(lustre_msg_get_opc(msg),
3335                                              supported);
3336                         if (likely(h != NULL)) {
3337                                 rc = mdt_req_handle(info, h, req);
3338                         } else {
3339                                 CERROR("The unsupported opc: 0x%x\n",
3340                                        lustre_msg_get_opc(msg) );
3341                                 req->rq_status = -ENOTSUPP;
3342                                 rc = ptlrpc_error(req);
3343                                 RETURN(rc);
3344                         }
3345                 }
3346         } else
3347                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3348         RETURN(rc);
3349 }
3350
3351 /*
3352  * MDT handler function called by ptlrpc service thread when request comes.
3353  *
3354  * XXX common "target" functionality should be factored into separate module
3355  * shared by mdt, ost and stand-alone services like fld.
3356  */
3357 int mdt_handle_common(struct ptlrpc_request *req,
3358                       struct mdt_opc_slice *supported)
3359 {
3360         struct lu_env          *env;
3361         struct mdt_thread_info *info;
3362         int                     rc;
3363         ENTRY;
3364
3365         env = req->rq_svc_thread->t_env;
3366         /* Refill(initilize) the context(mdt_thread_info), in case it is
3367          * not initialized yet. Usually it happens during start up, after
3368          * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3369          * before MDT_thread_info is initialized */
3370         lu_env_refill(env);
3371         LASSERT(env != NULL);
3372         LASSERT(env->le_ses != NULL);
3373         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3374         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3375         LASSERT(info != NULL);
3376
3377         mdt_thread_info_init(req, info);
3378
3379         rc = mdt_handle0(req, info, supported);
3380
3381         mdt_thread_info_fini(info);
3382         RETURN(rc);
3383 }
3384
3385 /*
3386  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3387  * as well.
3388  */
3389 int mdt_recovery_handle(struct ptlrpc_request *req)
3390 {
3391         int rc;
3392         ENTRY;
3393
3394         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3395         case FLD_QUERY:
3396                 rc = mdt_handle_common(req, mdt_fld_handlers);
3397                 break;
3398         case SEQ_QUERY:
3399                 rc = mdt_handle_common(req, mdt_seq_handlers);
3400                 break;
3401         default:
3402                 rc = mdt_handle_common(req, mdt_regular_handlers);
3403                 break;
3404         }
3405
3406         RETURN(rc);
3407 }
3408
3409 enum mdt_it_code {
3410         MDT_IT_OPEN,
3411         MDT_IT_OCREAT,
3412         MDT_IT_CREATE,
3413         MDT_IT_GETATTR,
3414         MDT_IT_READDIR,
3415         MDT_IT_LOOKUP,
3416         MDT_IT_UNLINK,
3417         MDT_IT_TRUNC,
3418         MDT_IT_GETXATTR,
3419         MDT_IT_LAYOUT,
3420         MDT_IT_QUOTA,
3421         MDT_IT_NR
3422 };
3423
3424 static int mdt_intent_getattr(enum mdt_it_code opcode,
3425                               struct mdt_thread_info *info,
3426                               struct ldlm_lock **,
3427                               __u64);
3428 static int mdt_intent_layout(enum mdt_it_code opcode,
3429                              struct mdt_thread_info *info,
3430                              struct ldlm_lock **,
3431                              __u64);
3432 static int mdt_intent_reint(enum mdt_it_code opcode,
3433                             struct mdt_thread_info *info,
3434                             struct ldlm_lock **,
3435                             __u64);
3436
3437 static struct mdt_it_flavor {
3438         const struct req_format *it_fmt;
3439         __u32                    it_flags;
3440         int                    (*it_act)(enum mdt_it_code ,
3441                                          struct mdt_thread_info *,
3442                                          struct ldlm_lock **,
3443                                          __u64);
3444         long                     it_reint;
3445 } mdt_it_flavor[] = {
3446         [MDT_IT_OPEN]     = {
3447                 .it_fmt   = &RQF_LDLM_INTENT,
3448                 /*.it_flags = HABEO_REFERO,*/
3449                 .it_flags = 0,
3450                 .it_act   = mdt_intent_reint,
3451                 .it_reint = REINT_OPEN
3452         },
3453         [MDT_IT_OCREAT]   = {
3454                 .it_fmt   = &RQF_LDLM_INTENT,
3455                 .it_flags = MUTABOR,
3456                 .it_act   = mdt_intent_reint,
3457                 .it_reint = REINT_OPEN
3458         },
3459         [MDT_IT_CREATE]   = {
3460                 .it_fmt   = &RQF_LDLM_INTENT,
3461                 .it_flags = MUTABOR,
3462                 .it_act   = mdt_intent_reint,
3463                 .it_reint = REINT_CREATE
3464         },
3465         [MDT_IT_GETATTR]  = {
3466                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3467                 .it_flags = HABEO_REFERO,
3468                 .it_act   = mdt_intent_getattr
3469         },
3470         [MDT_IT_READDIR]  = {
3471                 .it_fmt   = NULL,
3472                 .it_flags = 0,
3473                 .it_act   = NULL
3474         },
3475         [MDT_IT_LOOKUP]   = {
3476                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3477                 .it_flags = HABEO_REFERO,
3478                 .it_act   = mdt_intent_getattr
3479         },
3480         [MDT_IT_UNLINK]   = {
3481                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3482                 .it_flags = MUTABOR,
3483                 .it_act   = NULL,
3484                 .it_reint = REINT_UNLINK
3485         },
3486         [MDT_IT_TRUNC]    = {
3487                 .it_fmt   = NULL,
3488                 .it_flags = MUTABOR,
3489                 .it_act   = NULL
3490         },
3491         [MDT_IT_GETXATTR] = {
3492                 .it_fmt   = NULL,
3493                 .it_flags = 0,
3494                 .it_act   = NULL
3495         },
3496         [MDT_IT_LAYOUT] = {
3497                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
3498                 .it_flags = 0,
3499                 .it_act   = mdt_intent_layout
3500         }
3501 };
3502
3503 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3504                             struct ldlm_lock **lockp,
3505                             struct ldlm_lock *new_lock,
3506                             struct mdt_lock_handle *lh,
3507                             __u64 flags)
3508 {
3509         struct ptlrpc_request  *req = mdt_info_req(info);
3510         struct ldlm_lock       *lock = *lockp;
3511
3512         /*
3513          * Get new lock only for cases when possible resent did not find any
3514          * lock.
3515          */
3516         if (new_lock == NULL)
3517                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3518
3519         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3520                 lh->mlh_reg_lh.cookie = 0;
3521                 RETURN(0);
3522         }
3523
3524         LASSERTF(new_lock != NULL,
3525                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3526
3527         /*
3528          * If we've already given this lock to a client once, then we should
3529          * have no readers or writers.  Otherwise, we should have one reader
3530          * _or_ writer ref (which will be zeroed below) before returning the
3531          * lock to a client.
3532          */
3533         if (new_lock->l_export == req->rq_export) {
3534                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3535         } else {
3536                 LASSERT(new_lock->l_export == NULL);
3537                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3538         }
3539
3540         *lockp = new_lock;
3541
3542         if (new_lock->l_export == req->rq_export) {
3543                 /*
3544                  * Already gave this to the client, which means that we
3545                  * reconstructed a reply.
3546                  */
3547                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3548                         MSG_RESENT);
3549                 lh->mlh_reg_lh.cookie = 0;
3550                 RETURN(ELDLM_LOCK_REPLACED);
3551         }
3552
3553         /*
3554          * Fixup the lock to be given to the client.
3555          */
3556         lock_res_and_lock(new_lock);
3557         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3558          * possible blocking AST. */
3559         while (new_lock->l_readers > 0) {
3560                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3561                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3562                 new_lock->l_readers--;
3563         }
3564         while (new_lock->l_writers > 0) {
3565                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3566                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3567                 new_lock->l_writers--;
3568         }
3569
3570         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3571         new_lock->l_blocking_ast = lock->l_blocking_ast;
3572         new_lock->l_completion_ast = lock->l_completion_ast;
3573         new_lock->l_remote_handle = lock->l_remote_handle;
3574         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3575
3576         unlock_res_and_lock(new_lock);
3577
3578         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3579                      &new_lock->l_remote_handle,
3580                      &new_lock->l_exp_hash);
3581
3582         LDLM_LOCK_RELEASE(new_lock);
3583         lh->mlh_reg_lh.cookie = 0;
3584
3585         RETURN(ELDLM_LOCK_REPLACED);
3586 }
3587
3588 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3589                                     struct ldlm_lock *new_lock,
3590                                     struct ldlm_lock **old_lock,
3591                                     struct mdt_lock_handle *lh)
3592 {
3593         struct ptlrpc_request  *req = mdt_info_req(info);
3594         struct obd_export      *exp = req->rq_export;
3595         struct lustre_handle    remote_hdl;
3596         struct ldlm_request    *dlmreq;
3597         struct ldlm_lock       *lock;
3598
3599         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3600                 return;
3601
3602         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3603         remote_hdl = dlmreq->lock_handle[0];
3604
3605         /* In the function below, .hs_keycmp resolves to
3606          * ldlm_export_lock_keycmp() */
3607         /* coverity[overrun-buffer-val] */
3608         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3609         if (lock) {
3610                 if (lock != new_lock) {
3611                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3612                         lh->mlh_reg_mode = lock->l_granted_mode;
3613
3614                         LDLM_DEBUG(lock, "Restoring lock cookie");
3615                         DEBUG_REQ(D_DLMTRACE, req,
3616                                   "restoring lock cookie "LPX64,
3617                                   lh->mlh_reg_lh.cookie);
3618                         if (old_lock)
3619                                 *old_lock = LDLM_LOCK_GET(lock);
3620                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3621                         return;
3622                 }
3623
3624                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3625         }
3626
3627         /*
3628          * If the xid matches, then we know this is a resent request, and allow
3629          * it. (It's probably an OPEN, for which we don't send a lock.
3630          */
3631         if (req_xid_is_last(req))
3632                 return;
3633
3634         /*
3635          * This remote handle isn't enqueued, so we never received or processed
3636          * this request.  Clear MSG_RESENT, because it can be handled like any
3637          * normal request now.
3638          */
3639         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3640
3641         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3642                   remote_hdl.cookie);
3643 }
3644
3645 static int mdt_intent_getattr(enum mdt_it_code opcode,
3646                               struct mdt_thread_info *info,
3647                               struct ldlm_lock **lockp,
3648                               __u64 flags)
3649 {
3650         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3651         struct ldlm_lock       *new_lock = NULL;
3652         __u64                   child_bits;
3653         struct ldlm_reply      *ldlm_rep;
3654         struct ptlrpc_request  *req;
3655         struct mdt_body        *reqbody;
3656         struct mdt_body        *repbody;
3657         int                     rc, rc2;
3658         ENTRY;
3659
3660         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3661         LASSERT(reqbody);
3662
3663         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3664         LASSERT(repbody);
3665
3666         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3667         repbody->eadatasize = 0;
3668         repbody->aclsize = 0;
3669
3670         switch (opcode) {
3671         case MDT_IT_LOOKUP:
3672                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3673                 break;
3674         case MDT_IT_GETATTR:
3675                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3676                              MDS_INODELOCK_PERM;
3677                 break;
3678         default:
3679                 CERROR("Unsupported intent (%d)\n", opcode);
3680                 GOTO(out_shrink, rc = -EINVAL);
3681         }
3682
3683         rc = mdt_init_ucred(info, reqbody);
3684         if (rc)
3685                 GOTO(out_shrink, rc);
3686
3687         req = info->mti_pill->rc_req;
3688         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3689         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3690
3691         /* Get lock from request for possible resent case. */
3692         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3693
3694         ldlm_rep->lock_policy_res2 =
3695                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3696
3697         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3698                 ldlm_rep->lock_policy_res2 = 0;
3699         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3700             ldlm_rep->lock_policy_res2) {
3701                 lhc->mlh_reg_lh.cookie = 0ull;
3702                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3703         }
3704
3705         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3706         EXIT;
3707 out_ucred:
3708         mdt_exit_ucred(info);
3709 out_shrink:
3710         mdt_client_compatibility(info);
3711         rc2 = mdt_fix_reply(info);
3712         if (rc == 0)
3713                 rc = rc2;
3714         return rc;
3715 }
3716
3717 static int mdt_intent_layout(enum mdt_it_code opcode,
3718                              struct mdt_thread_info *info,
3719                              struct ldlm_lock **lockp,
3720                              __u64 flags)
3721 {
3722         struct layout_intent *layout;
3723         int rc;
3724         ENTRY;
3725
3726         if (opcode != MDT_IT_LAYOUT) {
3727                 CERROR("%s: Unknown intent (%d)\n",
3728                         info->mti_exp->exp_obd->obd_name, opcode);
3729                 RETURN(-EINVAL);
3730         }
3731
3732         (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3733         req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3734                         ldlm_lvbo_size(*lockp));
3735         rc = req_capsule_server_pack(info->mti_pill);
3736         if (rc != 0)
3737                 RETURN(-EINVAL);
3738
3739         layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3740         LASSERT(layout != NULL);
3741         if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3742                 /* return to normal ldlm handling */
3743                 RETURN(0);
3744
3745         CERROR("%s: Unsupported layout intent (%d)\n",
3746                 info->mti_exp->exp_obd->obd_name, layout->li_opc);
3747         RETURN(-EINVAL);
3748 }
3749
3750 static int mdt_intent_reint(enum mdt_it_code opcode,
3751                             struct mdt_thread_info *info,
3752                             struct ldlm_lock **lockp,
3753                             __u64 flags)
3754 {
3755         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3756         struct ldlm_reply      *rep = NULL;
3757         long                    opc;
3758         int                     rc;
3759
3760         static const struct req_format *intent_fmts[REINT_MAX] = {
3761                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3762                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3763         };
3764
3765         ENTRY;
3766
3767         opc = mdt_reint_opcode(info, intent_fmts);
3768         if (opc < 0)
3769                 RETURN(opc);
3770
3771         if (mdt_it_flavor[opcode].it_reint != opc) {
3772                 CERROR("Reint code %ld doesn't match intent: %d\n",
3773                        opc, opcode);
3774                 RETURN(err_serious(-EPROTO));
3775         }
3776
3777         /* Get lock from request for possible resent case. */
3778         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3779
3780         rc = mdt_reint_internal(info, lhc, opc);
3781
3782         /* Check whether the reply has been packed successfully. */
3783         if (mdt_info_req(info)->rq_repmsg != NULL)
3784                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3785         if (rep == NULL)
3786                 RETURN(err_serious(-EFAULT));
3787
3788         /* MDC expects this in any case */
3789         if (rc != 0)
3790                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3791
3792         /* Cross-ref case, the lock should be returned to the client */
3793         if (rc == -EREMOTE) {
3794                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3795                 rep->lock_policy_res2 = 0;
3796                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3797                 RETURN(rc);
3798         }
3799         rep->lock_policy_res2 = clear_serious(rc);
3800
3801         if (rep->lock_policy_res2 == -ENOENT &&
3802             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3803                 rep->lock_policy_res2 = 0;
3804
3805         if (rc == -ENOTCONN || rc == -ENODEV ||
3806             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3807                 /*
3808                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3809                  * will be returned by rq_status, and client at ptlrpc layer
3810                  * will detect this, then disconnect, reconnect the import
3811                  * immediately, instead of impacting the following the rpc.
3812                  */
3813                 lhc->mlh_reg_lh.cookie = 0ull;
3814                 RETURN(rc);
3815         } else {
3816                 /*
3817                  * For other cases, the error will be returned by intent.
3818                  * and client will retrieve the result from intent.
3819                  */
3820                  /*
3821                   * FIXME: when open lock is finished, that should be
3822                   * checked here.
3823                   */
3824                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3825                         LASSERTF(rc == 0, "Error occurred but lock handle "
3826                                  "is still in use, rc = %d\n", rc);
3827                         rep->lock_policy_res2 = 0;
3828                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3829                         RETURN(rc);
3830                 } else {
3831                         lhc->mlh_reg_lh.cookie = 0ull;
3832                         RETURN(ELDLM_LOCK_ABORTED);
3833                 }
3834         }
3835 }
3836
3837 static int mdt_intent_code(long itcode)
3838 {
3839         int rc;
3840
3841         switch(itcode) {
3842         case IT_OPEN:
3843                 rc = MDT_IT_OPEN;
3844                 break;
3845         case IT_OPEN|IT_CREAT:
3846                 rc = MDT_IT_OCREAT;
3847                 break;
3848         case IT_CREAT:
3849                 rc = MDT_IT_CREATE;
3850                 break;
3851         case IT_READDIR:
3852                 rc = MDT_IT_READDIR;
3853                 break;
3854         case IT_GETATTR:
3855                 rc = MDT_IT_GETATTR;
3856                 break;
3857         case IT_LOOKUP:
3858                 rc = MDT_IT_LOOKUP;
3859                 break;
3860         case IT_UNLINK:
3861                 rc = MDT_IT_UNLINK;
3862                 break;
3863         case IT_TRUNC:
3864                 rc = MDT_IT_TRUNC;
3865                 break;
3866         case IT_GETXATTR:
3867                 rc = MDT_IT_GETXATTR;
3868                 break;
3869         case IT_LAYOUT:
3870                 rc = MDT_IT_LAYOUT;
3871                 break;
3872         case IT_QUOTA_DQACQ:
3873         case IT_QUOTA_CONN:
3874                 rc = MDT_IT_QUOTA;
3875                 break;
3876         default:
3877                 CERROR("Unknown intent opcode: %ld\n", itcode);
3878                 rc = -EINVAL;
3879                 break;
3880         }
3881         return rc;
3882 }
3883
3884 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3885                           struct ldlm_lock **lockp, __u64 flags)
3886 {
3887         struct req_capsule   *pill;
3888         struct mdt_it_flavor *flv;
3889         int opc;
3890         int rc;
3891         ENTRY;
3892
3893         opc = mdt_intent_code(itopc);
3894         if (opc < 0)
3895                 RETURN(-EINVAL);
3896
3897         pill = info->mti_pill;
3898
3899         if (opc == MDT_IT_QUOTA) {
3900                 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3901
3902                 if (qmt == NULL)
3903                         RETURN(-EOPNOTSUPP);
3904
3905                 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3906                 /* pass the request to quota master */
3907                 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3908                                                  mdt_info_req(info), lockp,
3909                                                  flags);
3910                 RETURN(rc);
3911         }
3912
3913         flv  = &mdt_it_flavor[opc];
3914         if (flv->it_fmt != NULL)
3915                 req_capsule_extend(pill, flv->it_fmt);
3916
3917         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3918         if (rc == 0) {
3919                 struct ptlrpc_request *req = mdt_info_req(info);
3920                 if (flv->it_flags & MUTABOR &&
3921                     exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3922                         RETURN(-EROFS);
3923         }
3924         if (rc == 0 && flv->it_act != NULL) {
3925                 /* execute policy */
3926                 rc = flv->it_act(opc, info, lockp, flags);
3927         } else {
3928                 rc = -EOPNOTSUPP;
3929         }
3930         RETURN(rc);
3931 }
3932
3933 static int mdt_intent_policy(struct ldlm_namespace *ns,
3934                              struct ldlm_lock **lockp, void *req_cookie,
3935                              ldlm_mode_t mode, __u64 flags, void *data)
3936 {
3937         struct mdt_thread_info *info;
3938         struct ptlrpc_request  *req  =  req_cookie;
3939         struct ldlm_intent     *it;
3940         struct req_capsule     *pill;
3941         int rc;
3942
3943         ENTRY;
3944
3945         LASSERT(req != NULL);
3946
3947         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3948                                   &mdt_thread_key);
3949         LASSERT(info != NULL);
3950         pill = info->mti_pill;
3951         LASSERT(pill->rc_req == req);
3952
3953         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3954                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3955                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3956                 if (it != NULL) {
3957                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3958                         if (rc == 0)
3959                                 rc = ELDLM_OK;
3960
3961                         /* Lock without inodebits makes no sense and will oops
3962                          * later in ldlm. Let's check it now to see if we have
3963                          * ibits corrupted somewhere in mdt_intent_opc().
3964                          * The case for client miss to set ibits has been
3965                          * processed by others. */
3966                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3967                                         lr_type == LDLM_IBITS,
3968                                      info->mti_dlm_req->lock_desc.\
3969                                         l_policy_data.l_inodebits.bits != 0));
3970                 } else
3971                         rc = err_serious(-EFAULT);
3972         } else {
3973                 /* No intent was provided */
3974                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3975                 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3976                 rc = req_capsule_server_pack(pill);
3977                 if (rc)
3978                         rc = err_serious(rc);
3979         }
3980         RETURN(rc);
3981 }
3982
3983 static int mdt_seq_fini(const struct lu_env *env,
3984                         struct mdt_device *m)
3985 {
3986         return seq_site_fini(env, mdt_seq_site(m));
3987 }
3988
3989 static int mdt_seq_init(const struct lu_env *env,
3990                         const char *uuid,
3991                         struct mdt_device *m)
3992 {
3993         struct seq_server_site *ss;
3994         char *prefix;
3995         int rc;
3996         ENTRY;
3997
3998         ss = mdt_seq_site(m);
3999
4000         /*
4001          * This is sequence-controller node. Init seq-controller server on local
4002          * MDT.
4003          */
4004         if (ss->ss_node_id == 0) {
4005                 LASSERT(ss->ss_control_seq == NULL);
4006
4007                 OBD_ALLOC_PTR(ss->ss_control_seq);
4008                 if (ss->ss_control_seq == NULL)
4009                         RETURN(-ENOMEM);
4010
4011                 rc = seq_server_init(ss->ss_control_seq,
4012                                      m->mdt_bottom, uuid,
4013                                      LUSTRE_SEQ_CONTROLLER,
4014                                      ss,
4015                                      env);
4016
4017                 if (rc)
4018                         GOTO(out_seq_fini, rc);
4019
4020                 OBD_ALLOC_PTR(ss->ss_client_seq);
4021                 if (ss->ss_client_seq == NULL)
4022                         GOTO(out_seq_fini, rc = -ENOMEM);
4023
4024                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4025                 if (prefix == NULL) {
4026                         OBD_FREE_PTR(ss->ss_client_seq);
4027                         GOTO(out_seq_fini, rc = -ENOMEM);
4028                 }
4029
4030                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4031                          uuid);
4032
4033                 /*
4034                  * Init seq-controller client after seq-controller server is
4035                  * ready. Pass ss->ss_control_seq to it for direct talking.
4036                  */
4037                 rc = seq_client_init(ss->ss_client_seq, NULL,
4038                                      LUSTRE_SEQ_METADATA, prefix,
4039                                      ss->ss_control_seq);
4040                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4041
4042                 if (rc)
4043                         GOTO(out_seq_fini, rc);
4044         }
4045
4046         /* Init seq-server on local MDT */
4047         LASSERT(ss->ss_server_seq == NULL);
4048
4049         OBD_ALLOC_PTR(ss->ss_server_seq);
4050         if (ss->ss_server_seq == NULL)
4051                 GOTO(out_seq_fini, rc = -ENOMEM);
4052
4053         rc = seq_server_init(ss->ss_server_seq,
4054                              m->mdt_bottom, uuid,
4055                              LUSTRE_SEQ_SERVER,
4056                              ss,
4057                              env);
4058         if (rc)
4059                 GOTO(out_seq_fini, rc = -ENOMEM);
4060
4061         /* Assign seq-controller client to local seq-server. */
4062         if (ss->ss_node_id == 0) {
4063                 LASSERT(ss->ss_client_seq != NULL);
4064
4065                 rc = seq_server_set_cli(ss->ss_server_seq,
4066                                         ss->ss_client_seq,
4067                                         env);
4068         }
4069
4070         EXIT;
4071 out_seq_fini:
4072         if (rc)
4073                 mdt_seq_fini(env, m);
4074
4075         return rc;
4076 }
4077
4078 /*
4079  * FLD wrappers
4080  */
4081 static int mdt_fld_fini(const struct lu_env *env,
4082                         struct mdt_device *m)
4083 {
4084         struct seq_server_site *ss = mdt_seq_site(m);
4085         ENTRY;
4086
4087         if (ss && ss->ss_server_fld) {
4088                 fld_server_fini(env, ss->ss_server_fld);
4089                 OBD_FREE_PTR(ss->ss_server_fld);
4090                 ss->ss_server_fld = NULL;
4091         }
4092
4093         RETURN(0);
4094 }
4095
4096 static int mdt_fld_init(const struct lu_env *env,
4097                         const char *uuid,
4098                         struct mdt_device *m)
4099 {
4100         struct seq_server_site *ss;
4101         int rc;
4102         ENTRY;
4103
4104         ss = mdt_seq_site(m);
4105
4106         OBD_ALLOC_PTR(ss->ss_server_fld);
4107         if (ss->ss_server_fld == NULL)
4108                 RETURN(rc = -ENOMEM);
4109
4110         rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4111                              ss->ss_node_id, LU_SEQ_RANGE_MDT);
4112         if (rc) {
4113                 OBD_FREE_PTR(ss->ss_server_fld);
4114                 ss->ss_server_fld = NULL;
4115                 RETURN(rc);
4116         }
4117
4118         RETURN(0);
4119 }
4120
4121 static void mdt_stack_pre_fini(const struct lu_env *env,
4122                            struct mdt_device *m, struct lu_device *top)
4123 {
4124         struct obd_device       *obd = mdt2obd_dev(m);
4125         struct lustre_cfg_bufs  *bufs;
4126         struct lustre_cfg       *lcfg;
4127         struct mdt_thread_info  *info;
4128         ENTRY;
4129
4130         LASSERT(top);
4131
4132         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4133         LASSERT(info != NULL);
4134
4135         bufs = &info->mti_u.bufs;
4136
4137         LASSERT(m->mdt_child_exp);
4138         LASSERT(m->mdt_child_exp->exp_obd);
4139         obd = m->mdt_child_exp->exp_obd;
4140
4141         /* process cleanup, pass mdt obd name to get obd umount flags */
4142         /* XXX: this is needed because all layers are referenced by
4143          * objects (some of them are pinned by osd, for example *
4144          * the proper solution should be a model where object used
4145          * by osd only doesn't have mdt/mdd slices -bzzz */
4146         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4147         lustre_cfg_bufs_set_string(bufs, 1, NULL);
4148         lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4149         if (!lcfg) {
4150                 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4151                 return;
4152         }
4153         top->ld_ops->ldo_process_config(env, top, lcfg);
4154         lustre_cfg_free(lcfg);
4155         EXIT;
4156 }
4157
4158 static void mdt_stack_fini(const struct lu_env *env,
4159                            struct mdt_device *m, struct lu_device *top)
4160 {
4161         struct obd_device       *obd = mdt2obd_dev(m);
4162         struct lustre_cfg_bufs  *bufs;
4163         struct lustre_cfg       *lcfg;
4164         struct mdt_thread_info  *info;
4165         char flags[3]="";
4166         ENTRY;
4167
4168         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4169         LASSERT(info != NULL);
4170
4171         lu_dev_del_linkage(top->ld_site, top);
4172
4173         lu_site_purge(env, top->ld_site, -1);
4174
4175         bufs = &info->mti_u.bufs;
4176         /* process cleanup, pass mdt obd name to get obd umount flags */
4177         /* another purpose is to let all layers to release their objects */
4178         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4179         if (obd->obd_force)
4180                 strcat(flags, "F");
4181         if (obd->obd_fail)
4182                 strcat(flags, "A");
4183         lustre_cfg_bufs_set_string(bufs, 1, flags);
4184         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4185         if (!lcfg) {
4186                 CERROR("Cannot alloc lcfg!\n");
4187                 return;
4188         }
4189         LASSERT(top);
4190         top->ld_ops->ldo_process_config(env, top, lcfg);
4191         lustre_cfg_free(lcfg);
4192
4193         lu_site_purge(env, top->ld_site, -1);
4194
4195         m->mdt_child = NULL;
4196         m->mdt_bottom = NULL;
4197
4198         obd_disconnect(m->mdt_child_exp);
4199         m->mdt_child_exp = NULL;
4200
4201         obd_disconnect(m->mdt_bottom_exp);
4202         m->mdt_child_exp = NULL;
4203 }
4204
4205 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4206                                const char *next, struct obd_export **exp)
4207 {
4208         struct obd_connect_data *data = NULL;
4209         struct obd_device       *obd;
4210         int                      rc;
4211         ENTRY;
4212
4213         OBD_ALLOC_PTR(data);
4214         if (data == NULL)
4215                 GOTO(out, rc = -ENOMEM);
4216
4217         obd = class_name2obd(next);
4218         if (obd == NULL) {
4219                 CERROR("%s: can't locate next device: %s\n",
4220                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4221                 GOTO(out, rc = -ENOTCONN);
4222         }
4223
4224         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4225         data->ocd_version = LUSTRE_VERSION_CODE;
4226
4227         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4228         if (rc) {
4229                 CERROR("%s: cannot connect to next dev %s (%d)\n",
4230                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4231                 GOTO(out, rc);
4232         }
4233
4234 out:
4235         if (data)
4236                 OBD_FREE_PTR(data);
4237         RETURN(rc);
4238 }
4239
4240 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4241                           struct lustre_cfg *cfg)
4242 {
4243         char                   *dev = lustre_cfg_string(cfg, 0);
4244         int                     rc, name_size, uuid_size;
4245         char                   *name, *uuid, *p;
4246         struct lustre_cfg_bufs *bufs;
4247         struct lustre_cfg      *lcfg;
4248         struct obd_device      *obd;
4249         struct lustre_profile  *lprof;
4250         struct lu_site         *site;
4251         ENTRY;
4252
4253         /* in 1.8 we had the only device in the stack - MDS.
4254          * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4255          * in 2.3 OSD is instantiated by obd_mount.c, so we need
4256          * to generate names and setup MDT, MDD. MDT will be using
4257          * generated name to connect to MDD. for MDD the next device
4258          * will be LOD with name taken from so called "profile" which
4259          * is generated by mount_option line
4260          *
4261          * 1.8 MGS generates config. commands like this:
4262          *   #06 (104)mount_option 0:  1:lustre-MDT0000  2:lustre-mdtlov
4263          *   #08 (120)setup   0:lustre-MDT0000  1:dev 2:type 3:lustre-MDT0000
4264          * 2.0 MGS generates config. commands like this:
4265          *   #07 (112)mount_option 0:  1:lustre-MDT0000  2:lustre-MDT0000-mdtlov
4266          *   #08 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4267          *                    3:lustre-MDT0000-mdtlov  4:f
4268          *
4269          * we generate MDD name from MDT one, just replacing T with D
4270          *
4271          * after all the preparations, the logical equivalent will be
4272          *   #01 (160)setup   0:lustre-MDD0000  1:lustre-MDD0000_UUID  2:0
4273          *                    3:lustre-MDT0000-mdtlov  4:f
4274          *   #02 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4275          *                    3:lustre-MDD0000  4:f
4276          *
4277          *  notice we build the stack from down to top: MDD first, then MDT */
4278
4279         name_size = MAX_OBD_NAME;
4280         uuid_size = MAX_OBD_NAME;
4281
4282         OBD_ALLOC(name, name_size);
4283         OBD_ALLOC(uuid, uuid_size);
4284         if (name == NULL || uuid == NULL)
4285                 GOTO(cleanup_mem, rc = -ENOMEM);
4286
4287         OBD_ALLOC_PTR(bufs);
4288         if (!bufs)
4289                 GOTO(cleanup_mem, rc = -ENOMEM);
4290
4291         strcpy(name, dev);
4292         p = strstr(name, "-MDT");
4293         if (p == NULL)
4294                 GOTO(cleanup_mem, rc = -ENOMEM);
4295         p[3] = 'D';
4296
4297         snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4298
4299         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4300         if (lprof == NULL || lprof->lp_dt == NULL) {
4301                 CERROR("can't find the profile: %s\n",
4302                        lustre_cfg_string(cfg, 0));
4303                 GOTO(cleanup_mem, rc = -EINVAL);
4304         }
4305
4306         lustre_cfg_bufs_reset(bufs, name);
4307         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4308         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4309         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4310
4311         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4312         if (!lcfg)
4313                 GOTO(free_bufs, rc = -ENOMEM);
4314
4315         rc = class_attach(lcfg);
4316         if (rc)
4317                 GOTO(lcfg_cleanup, rc);
4318
4319         obd = class_name2obd(name);
4320         if (!obd) {
4321                 CERROR("Can not find obd %s (%s in config)\n",
4322                        MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4323                 GOTO(class_detach, rc = -EINVAL);
4324         }
4325
4326         lustre_cfg_free(lcfg);
4327
4328         lustre_cfg_bufs_reset(bufs, name);
4329         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4330         lustre_cfg_bufs_set_string(bufs, 2, dev);
4331         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4332
4333         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4334
4335         rc = class_setup(obd, lcfg);
4336         if (rc)
4337                 GOTO(class_detach, rc);
4338
4339         /* connect to MDD we just setup */
4340         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4341         if (rc)
4342                 RETURN(rc);
4343
4344         site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4345         LASSERT(site);
4346         LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4347         mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4348         site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4349         mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4350
4351
4352         /* now connect to bottom OSD */
4353         snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4354         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4355         if (rc)
4356                 RETURN(rc);
4357         mdt->mdt_bottom =
4358                 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4359
4360
4361         rc = lu_env_refill((struct lu_env *)env);
4362         if (rc != 0)
4363                 CERROR("Failure to refill session: '%d'\n", rc);
4364
4365         lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4366
4367         EXIT;
4368 class_detach:
4369         if (rc)
4370                 class_detach(obd, lcfg);
4371 lcfg_cleanup:
4372         lustre_cfg_free(lcfg);
4373 free_bufs:
4374         OBD_FREE_PTR(bufs);
4375 cleanup_mem:
4376         if (name)
4377                 OBD_FREE(name, name_size);
4378         if (uuid)
4379                 OBD_FREE(uuid, uuid_size);
4380         RETURN(rc);
4381 }
4382
4383 /* setup quota master target on MDT0 */
4384 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4385                           struct lustre_cfg *cfg)
4386 {
4387         struct obd_device       *obd;
4388         char                    *dev = lustre_cfg_string(cfg, 0);
4389         char                    *qmtname, *uuid, *p;
4390         struct lustre_cfg_bufs  *bufs;
4391         struct lustre_cfg       *lcfg;
4392         struct lustre_profile   *lprof;
4393         struct obd_connect_data *data;
4394         int                      rc;
4395         ENTRY;
4396
4397         LASSERT(mdt->mdt_qmt_exp == NULL);
4398         LASSERT(mdt->mdt_qmt_dev == NULL);
4399
4400         /* quota master is on MDT0 only for now */
4401         if (mdt->mdt_seq_site.ss_node_id != 0)
4402                 RETURN(0);
4403
4404         /* MGS generates config commands which look as follows:
4405          *   #01 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4406          *                    3:lustre-MDT0000-mdtlov  4:f
4407          *
4408          * We generate the QMT name from the MDT one, just replacing MD with QM
4409          * after all the preparations, the logical equivalent will be:
4410          *   #01 (160)setup   0:lustre-QMT0000  1:lustre-QMT0000_UUID  2:0
4411          *                    3:lustre-MDT0000-osd  4:f */
4412         OBD_ALLOC(qmtname, MAX_OBD_NAME);
4413         OBD_ALLOC(uuid, UUID_MAX);
4414         OBD_ALLOC_PTR(bufs);
4415         OBD_ALLOC_PTR(data);
4416         if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4417                 GOTO(cleanup_mem, rc = -ENOMEM);
4418
4419         strcpy(qmtname, dev);
4420         p = strstr(qmtname, "-MDT");
4421         if (p == NULL)
4422                 GOTO(cleanup_mem, rc = -ENOMEM);
4423         /* replace MD with QM */
4424         p[1] = 'Q';
4425         p[2] = 'M';
4426
4427         snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4428
4429         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4430         if (lprof == NULL || lprof->lp_dt == NULL) {
4431                 CERROR("can't find profile for %s\n",
4432                        lustre_cfg_string(cfg, 0));
4433                 GOTO(cleanup_mem, rc = -EINVAL);
4434         }
4435
4436         lustre_cfg_bufs_reset(bufs, qmtname);
4437         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4438         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4439         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4440
4441         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4442         if (!lcfg)
4443                 GOTO(cleanup_mem, rc = -ENOMEM);
4444
4445         rc = class_attach(lcfg);
4446         if (rc)
4447                 GOTO(lcfg_cleanup, rc);
4448
4449         obd = class_name2obd(qmtname);
4450         if (!obd) {
4451                 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4452                        lustre_cfg_string(cfg, 0));
4453                 GOTO(class_detach, rc = -EINVAL);
4454         }
4455
4456         lustre_cfg_free(lcfg);
4457
4458         lustre_cfg_bufs_reset(bufs, qmtname);
4459         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4460         lustre_cfg_bufs_set_string(bufs, 2, dev);
4461
4462         /* for quota, the next device should be the OSD device */
4463         lustre_cfg_bufs_set_string(bufs, 3,
4464                                    mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4465
4466         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4467
4468         rc = class_setup(obd, lcfg);
4469         if (rc)
4470                 GOTO(class_detach, rc);
4471
4472         mdt->mdt_qmt_dev = obd->obd_lu_dev;
4473
4474         /* configure local quota objects */
4475         rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4476                                                    &mdt->mdt_md_dev.md_lu_dev,
4477                                                    mdt->mdt_qmt_dev);
4478         if (rc)
4479                 GOTO(class_cleanup, rc);
4480
4481         /* connect to quota master target */
4482         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4483         data->ocd_version = LUSTRE_VERSION_CODE;
4484         rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4485                          data, NULL);
4486         if (rc) {
4487                 CERROR("cannot connect to quota master device %s (%d)\n",
4488                        qmtname, rc);
4489                 GOTO(class_cleanup, rc);
4490         }
4491
4492         EXIT;
4493 class_cleanup:
4494         if (rc) {
4495                 class_manual_cleanup(obd);
4496                 mdt->mdt_qmt_dev = NULL;
4497         }
4498 class_detach:
4499         if (rc)
4500                 class_detach(obd, lcfg);
4501 lcfg_cleanup:
4502         lustre_cfg_free(lcfg);
4503 cleanup_mem:
4504         if (bufs)
4505                 OBD_FREE_PTR(bufs);
4506         if (qmtname)
4507                 OBD_FREE(qmtname, MAX_OBD_NAME);
4508         if (uuid)
4509                 OBD_FREE(uuid, UUID_MAX);
4510         if (data)
4511                 OBD_FREE_PTR(data);
4512         return rc;
4513 }
4514
4515 /* Shutdown quota master target associated with mdt */
4516 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4517 {
4518         ENTRY;
4519
4520         if (mdt->mdt_qmt_exp == NULL)
4521                 RETURN_EXIT;
4522         LASSERT(mdt->mdt_qmt_dev != NULL);
4523
4524         /* the qmt automatically shuts down when the mdt disconnects */
4525         obd_disconnect(mdt->mdt_qmt_exp);
4526         mdt->mdt_qmt_exp = NULL;
4527         mdt->mdt_qmt_dev = NULL;
4528         EXIT;
4529 }
4530
4531 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4532 {
4533         struct md_device  *next = m->mdt_child;
4534         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4535         struct obd_device *obd = mdt2obd_dev(m);
4536         ENTRY;
4537
4538         target_recovery_fini(obd);
4539
4540         ping_evictor_stop();
4541
4542
4543         mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4544         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4545         obd_exports_barrier(obd);
4546         obd_zombie_barrier();
4547
4548         mdt_procfs_fini(m);
4549
4550         tgt_fini(env, &m->mdt_lut);
4551         mdt_fs_cleanup(env, m);
4552         upcall_cache_cleanup(m->mdt_identity_cache);
4553         m->mdt_identity_cache = NULL;
4554
4555         if (m->mdt_namespace != NULL) {
4556                 ldlm_namespace_free(m->mdt_namespace, NULL,
4557                                     d->ld_obd->obd_force);
4558                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4559         }
4560
4561         mdt_quota_fini(env, m);
4562
4563         cfs_free_nidlist(&m->mdt_nosquash_nids);
4564         if (m->mdt_nosquash_str) {
4565                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4566                 m->mdt_nosquash_str = NULL;
4567                 m->mdt_nosquash_strlen = 0;
4568         }
4569
4570         next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
4571                                     0, NULL);
4572         mdt_seq_fini(env, m);
4573         mdt_fld_fini(env, m);
4574         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4575
4576         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4577         cfs_timer_disarm(&m->mdt_ck_timer);
4578         mdt_ck_thread_stop(m);
4579
4580         /*
4581          * Finish the stack
4582          */
4583         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4584
4585         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4586
4587         server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4588
4589         EXIT;
4590 }
4591
4592 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4593 {
4594         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4595         struct sptlrpc_rule_set  tmp_rset;
4596         int                      rc;
4597
4598         sptlrpc_rule_set_init(&tmp_rset);
4599         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4600         if (rc) {
4601                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4602                        obd->obd_name, rc);
4603                 return rc;
4604         }
4605
4606         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4607
4608         write_lock(&m->mdt_sptlrpc_lock);
4609         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4610         m->mdt_sptlrpc_rset = tmp_rset;
4611         write_unlock(&m->mdt_sptlrpc_lock);
4612
4613         return 0;
4614 }
4615
4616 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4617
4618 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4619                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4620 {
4621         struct mdt_thread_info    *info;
4622         struct obd_device         *obd;
4623         const char                *dev = lustre_cfg_string(cfg, 0);
4624         const char                *num = lustre_cfg_string(cfg, 2);
4625         struct lustre_mount_info  *lmi = NULL;
4626         struct lustre_sb_info     *lsi;
4627         struct lu_site            *s;
4628         struct seq_server_site    *ss_site;
4629         const char                *identity_upcall = "NONE";
4630         struct md_device          *next;
4631         int                        rc;
4632         int                        node_id;
4633         mntopt_t                   mntopts;
4634         ENTRY;
4635
4636         md_device_init(&m->mdt_md_dev, ldt);
4637         /*
4638          * Environment (env) might be missing mdt_thread_key values at that
4639          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4640          * mode.
4641          *
4642          * Usually device allocation path doesn't use module key values, but
4643          * mdt has to do a lot of work here, so allocate key value.
4644          */
4645         rc = lu_env_refill((struct lu_env *)env);
4646         if (rc != 0)
4647                 RETURN(rc);
4648
4649         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4650         LASSERT(info != NULL);
4651
4652         obd = class_name2obd(dev);
4653         LASSERT(obd != NULL);
4654
4655         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4656
4657         m->mdt_som_conf = 0;
4658
4659         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4660         lmi = server_get_mount(dev);
4661         if (lmi == NULL) {
4662                 CERROR("Cannot get mount info for %s!\n", dev);
4663                 RETURN(-EFAULT);
4664         } else {
4665                 lsi = s2lsi(lmi->lmi_sb);
4666                 /* CMD is supported only in IAM mode */
4667                 LASSERT(num);
4668                 node_id = simple_strtol(num, NULL, 10);
4669                 obd->u.obt.obt_magic = OBT_MAGIC;
4670         }
4671
4672         rwlock_init(&m->mdt_sptlrpc_lock);
4673         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4674
4675         spin_lock_init(&m->mdt_ioepoch_lock);
4676         m->mdt_opts.mo_compat_resname = 0;
4677         m->mdt_opts.mo_mds_capa = 1;
4678         m->mdt_opts.mo_oss_capa = 1;
4679         m->mdt_capa_timeout = CAPA_TIMEOUT;
4680         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4681         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4682         m->mdt_squash_uid = 0;
4683         m->mdt_squash_gid = 0;
4684         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4685         m->mdt_nosquash_str = NULL;
4686         m->mdt_nosquash_strlen = 0;
4687         init_rwsem(&m->mdt_squash_sem);
4688         spin_lock_init(&m->mdt_osfs_lock);
4689         m->mdt_osfs_age = cfs_time_shift_64(-1000);
4690         m->mdt_enable_remote_dir = 0;
4691         m->mdt_enable_remote_dir_gid = 0;
4692
4693         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4694         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4695         /* set this lu_device to obd, because error handling need it */
4696         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4697
4698         /* init the stack */
4699         rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4700         if (rc) {
4701                 CERROR("Can't init device stack, rc %d\n", rc);
4702                 GOTO(err_lmi, rc);
4703         }
4704
4705         s = m->mdt_md_dev.md_lu_dev.ld_site;
4706         ss_site = &m->mdt_seq_site;
4707         s->ld_seq_site = ss_site;
4708         ss_site->ss_lu = s;
4709
4710         /* set server index */
4711         ss_site->ss_node_id = node_id;
4712
4713         /* failover is the default
4714          * FIXME: we do not failout mds0/mgs, which may cause some problems.
4715          * assumed whose ss_node_id == 0 XXX
4716          * */
4717         obd->obd_replayable = 1;
4718         /* No connection accepted until configurations will finish */
4719         obd->obd_no_conn = 1;
4720
4721         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4722                 char *str = lustre_cfg_string(cfg, 4);
4723                 if (strchr(str, 'n')) {
4724                         CWARN("%s: recovery disabled\n", obd->obd_name);
4725                         obd->obd_replayable = 0;
4726                 }
4727         }
4728
4729         rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4730         if (rc)
4731                 GOTO(err_fini_stack, rc);
4732
4733         rc = mdt_fld_init(env, obd->obd_name, m);
4734         if (rc)
4735                 GOTO(err_lut, rc);
4736
4737         rc = mdt_seq_init(env, obd->obd_name, m);
4738         if (rc)
4739                 GOTO(err_fini_fld, rc);
4740
4741         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4742                  LUSTRE_MDT_NAME"-%p", m);
4743         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4744                                               LDLM_NAMESPACE_SERVER,
4745                                               LDLM_NAMESPACE_GREEDY,
4746                                               LDLM_NS_TYPE_MDT);
4747         if (m->mdt_namespace == NULL)
4748                 GOTO(err_fini_seq, rc = -ENOMEM);
4749
4750         m->mdt_namespace->ns_lvbp = m;
4751         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4752
4753         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4754         /* set obd_namespace for compatibility with old code */
4755         obd->obd_namespace = m->mdt_namespace;
4756
4757         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4758
4759         rc = mdt_ck_thread_start(m);
4760         if (rc)
4761                 GOTO(err_free_ns, rc);
4762
4763         rc = mdt_fs_setup(env, m, obd, lsi);
4764         if (rc)
4765                 GOTO(err_capa, rc);
4766
4767         mdt_adapt_sptlrpc_conf(obd, 1);
4768
4769         next = m->mdt_child;
4770         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4771                                          &mntopts);
4772         if (rc)
4773                 GOTO(err_llog_cleanup, rc);
4774
4775         if (mntopts & MNTOPT_USERXATTR)
4776                 m->mdt_opts.mo_user_xattr = 1;
4777         else
4778                 m->mdt_opts.mo_user_xattr = 0;
4779
4780         if (mntopts & MNTOPT_ACL)
4781                 m->mdt_opts.mo_acl = 1;
4782         else
4783                 m->mdt_opts.mo_acl = 0;
4784
4785         /* XXX: to support suppgid for ACL, we enable identity_upcall
4786          * by default, otherwise, maybe got unexpected -EACCESS. */
4787         if (m->mdt_opts.mo_acl)
4788                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4789
4790         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4791                                                 &mdt_identity_upcall_cache_ops);
4792         if (IS_ERR(m->mdt_identity_cache)) {
4793                 rc = PTR_ERR(m->mdt_identity_cache);
4794                 m->mdt_identity_cache = NULL;
4795                 GOTO(err_llog_cleanup, rc);
4796         }
4797
4798         rc = mdt_procfs_init(m, dev);
4799         if (rc) {
4800                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4801                 GOTO(err_recovery, rc);
4802         }
4803
4804         rc = mdt_quota_init(env, m, cfg);
4805         if (rc)
4806                 GOTO(err_procfs, rc);
4807
4808         m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4809         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4810                            "mdt_ldlm_client", m->mdt_ldlm_client);
4811
4812         ping_evictor_start();
4813
4814         /* recovery will be started upon mdt_prepare()
4815          * when the whole stack is complete and ready
4816          * to serve the requests */
4817
4818         mdt_init_capa_ctxt(env, m);
4819
4820         /* Reduce the initial timeout on an MDS because it doesn't need such
4821          * a long timeout as an OST does. Adaptive timeouts will adjust this
4822          * value appropriately. */
4823         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4824                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4825
4826         RETURN(0);
4827
4828 err_procfs:
4829         mdt_procfs_fini(m);
4830 err_recovery:
4831         target_recovery_fini(obd);
4832         upcall_cache_cleanup(m->mdt_identity_cache);
4833         m->mdt_identity_cache = NULL;
4834 err_llog_cleanup:
4835         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4836         mdt_fs_cleanup(env, m);
4837 err_capa:
4838         cfs_timer_disarm(&m->mdt_ck_timer);
4839         mdt_ck_thread_stop(m);
4840 err_free_ns:
4841         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4842         obd->obd_namespace = m->mdt_namespace = NULL;
4843 err_fini_seq:
4844         mdt_seq_fini(env, m);
4845 err_fini_fld:
4846         mdt_fld_fini(env, m);
4847 err_lut:
4848         tgt_fini(env, &m->mdt_lut);
4849 err_fini_stack:
4850         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4851 err_lmi:
4852         if (lmi)
4853                 server_put_mount(dev, lmi->lmi_mnt);
4854         return (rc);
4855 }
4856
4857 /* For interoperability, the left element is old parameter, the right one
4858  * is the new version of the parameter, if some parameter is deprecated,
4859  * the new version should be set as NULL. */
4860 static struct cfg_interop_param mdt_interop_param[] = {
4861         { "mdt.group_upcall",   NULL },
4862         { "mdt.quota_type",     NULL },
4863         { "mdd.quota_type",     NULL },
4864         { "mdt.rootsquash",     "mdt.root_squash" },
4865         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
4866         { NULL }
4867 };
4868
4869 /* used by MGS to process specific configurations */
4870 static int mdt_process_config(const struct lu_env *env,
4871                               struct lu_device *d, struct lustre_cfg *cfg)
4872 {
4873         struct mdt_device *m = mdt_dev(d);
4874         struct md_device *md_next = m->mdt_child;
4875         struct lu_device *next = md2lu_dev(md_next);
4876         int rc;
4877         ENTRY;
4878
4879         switch (cfg->lcfg_command) {
4880         case LCFG_PARAM: {
4881                 struct lprocfs_static_vars  lvars;
4882                 struct obd_device          *obd = d->ld_obd;
4883
4884                 /* For interoperability */
4885                 struct cfg_interop_param   *ptr = NULL;
4886                 struct lustre_cfg          *old_cfg = NULL;
4887                 char                       *param = NULL;
4888
4889                 param = lustre_cfg_string(cfg, 1);
4890                 if (param == NULL) {
4891                         CERROR("param is empty\n");
4892                         rc = -EINVAL;
4893                         break;
4894                 }
4895
4896                 ptr = class_find_old_param(param, mdt_interop_param);
4897                 if (ptr != NULL) {
4898                         if (ptr->new_param == NULL) {
4899                                 rc = 0;
4900                                 CWARN("For interoperability, skip this %s."
4901                                       " It is obsolete.\n", ptr->old_param);
4902                                 break;
4903                         }
4904
4905                         CWARN("Found old param %s, changed it to %s.\n",
4906                               ptr->old_param, ptr->new_param);
4907
4908                         old_cfg = cfg;
4909                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4910                         if (IS_ERR(cfg)) {
4911                                 rc = PTR_ERR(cfg);
4912                                 break;
4913                         }
4914                 }
4915
4916                 lprocfs_mdt_init_vars(&lvars);
4917                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4918                                               cfg, obd);
4919                 if (rc > 0 || rc == -ENOSYS)
4920                         /* we don't understand; pass it on */
4921                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
4922
4923                 if (old_cfg != NULL)
4924                         lustre_cfg_free(cfg);
4925
4926                 break;
4927         }
4928         default:
4929                 /* others are passed further */
4930                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4931                 break;
4932         }
4933         RETURN(rc);
4934 }
4935
4936 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4937                                           const struct lu_object_header *hdr,
4938                                           struct lu_device *d)
4939 {
4940         struct mdt_object *mo;
4941
4942         ENTRY;
4943
4944         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4945         if (mo != NULL) {
4946                 struct lu_object *o;
4947                 struct lu_object_header *h;
4948
4949                 o = &mo->mot_obj.mo_lu;
4950                 h = &mo->mot_header;
4951                 lu_object_header_init(h);
4952                 lu_object_init(o, h, d);
4953                 lu_object_add_top(h, o);
4954                 o->lo_ops = &mdt_obj_ops;
4955                 mutex_init(&mo->mot_ioepoch_mutex);
4956                 mutex_init(&mo->mot_lov_mutex);
4957                 RETURN(o);
4958         } else
4959                 RETURN(NULL);
4960 }
4961
4962 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4963                            const struct lu_object_conf *unused)
4964 {
4965         struct mdt_device *d = mdt_dev(o->lo_dev);
4966         struct lu_device  *under;
4967         struct lu_object  *below;
4968         int                rc = 0;
4969         ENTRY;
4970
4971         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4972                PFID(lu_object_fid(o)));
4973
4974         under = &d->mdt_child->md_lu_dev;
4975         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4976         if (below != NULL) {
4977                 lu_object_add(o, below);
4978         } else
4979                 rc = -ENOMEM;
4980
4981         RETURN(rc);
4982 }
4983
4984 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4985 {
4986         struct mdt_object *mo = mdt_obj(o);
4987         struct lu_object_header *h;
4988         ENTRY;
4989
4990         h = o->lo_header;
4991         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4992                PFID(lu_object_fid(o)));
4993
4994         lu_object_fini(o);
4995         lu_object_header_fini(h);
4996         OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4997
4998         EXIT;
4999 }
5000
5001 static int mdt_object_print(const struct lu_env *env, void *cookie,
5002                             lu_printer_t p, const struct lu_object *o)
5003 {
5004         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5005         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5006                     "flags="LPX64", epochcount=%d, writecount=%d)",
5007                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
5008                     mdto->mot_ioepoch_count, mdto->mot_writecount);
5009 }
5010
5011 static int mdt_prepare(const struct lu_env *env,
5012                 struct lu_device *pdev,
5013                 struct lu_device *cdev)
5014 {
5015         struct mdt_device *mdt = mdt_dev(cdev);
5016         struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5017         struct obd_device *obd = cdev->ld_obd;
5018         int rc;
5019
5020         ENTRY;
5021
5022         LASSERT(obd);
5023
5024         rc = next->ld_ops->ldo_prepare(env, cdev, next);
5025         if (rc)
5026                 RETURN(rc);
5027
5028         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5029         if (rc)
5030                 RETURN(rc);
5031
5032         rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5033                                                    OBD_IOC_START_LFSCK,
5034                                                    0, NULL);
5035         if (rc != 0)
5036                 CWARN("Fail to auto trigger paused LFSCK.\n");
5037
5038         rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5039                                                   &mdt->mdt_md_root_fid);
5040         if (rc)
5041                 RETURN(rc);
5042
5043         LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5044         target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5045         set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5046         LASSERT(obd->obd_no_conn);
5047         spin_lock(&obd->obd_dev_lock);
5048         obd->obd_no_conn = 0;
5049         spin_unlock(&obd->obd_dev_lock);
5050
5051         if (obd->obd_recovering == 0)
5052                 mdt_postrecov(env, mdt);
5053
5054         RETURN(rc);
5055 }
5056
5057 const struct lu_device_operations mdt_lu_ops = {
5058         .ldo_object_alloc   = mdt_object_alloc,
5059         .ldo_process_config = mdt_process_config,
5060         .ldo_prepare        = mdt_prepare,
5061 };
5062
5063 static const struct lu_object_operations mdt_obj_ops = {
5064         .loo_object_init    = mdt_object_init,
5065         .loo_object_free    = mdt_object_free,
5066         .loo_object_print   = mdt_object_print
5067 };
5068
5069 static int mdt_obd_set_info_async(const struct lu_env *env,
5070                                   struct obd_export *exp,
5071                                   __u32 keylen, void *key,
5072                                   __u32 vallen, void *val,
5073                                   struct ptlrpc_request_set *set)
5074 {
5075         struct obd_device     *obd = exp->exp_obd;
5076         int                    rc;
5077         ENTRY;
5078
5079         LASSERT(obd);
5080
5081         if (KEY_IS(KEY_SPTLRPC_CONF)) {
5082                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5083                 RETURN(rc);
5084         }
5085
5086         RETURN(0);
5087 }
5088
5089 /**
5090  * Match client and server connection feature flags.
5091  *
5092  * Compute the compatibility flags for a connection request based on
5093  * features mutually supported by client and server.
5094  *
5095  * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5096  * must not be updated here, otherwise a partially initialized value may
5097  * be exposed. After the connection request is successfully processed,
5098  * the top-level MDT connect request handler atomically updates the export
5099  * connect flags from the obd_connect_data::ocd_connect_flags field of the
5100  * reply. \see mdt_connect().
5101  *
5102  * \param exp   the obd_export associated with this client/target pair
5103  * \param mdt   the target device for the connection
5104  * \param data  stores data for this connect request
5105  *
5106  * \retval 0       success
5107  * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5108  * \retval -EBADE  client and server feature requirements are incompatible
5109  */
5110 static int mdt_connect_internal(struct obd_export *exp,
5111                                 struct mdt_device *mdt,
5112                                 struct obd_connect_data *data)
5113 {
5114         LASSERT(data != NULL);
5115
5116         data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5117         data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5118
5119         /* If no known bits (which should not happen, probably,
5120            as everybody should support LOOKUP and UPDATE bits at least)
5121            revert to compat mode with plain locks. */
5122         if (!data->ocd_ibits_known &&
5123             data->ocd_connect_flags & OBD_CONNECT_IBITS)
5124                 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5125
5126         if (!mdt->mdt_opts.mo_acl)
5127                 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5128
5129         if (!mdt->mdt_opts.mo_user_xattr)
5130                 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5131
5132         if (!mdt->mdt_som_conf)
5133                 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5134
5135         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5136                 data->ocd_brw_size = min(data->ocd_brw_size,
5137                                          (__u32)MD_MAX_BRW_SIZE);
5138                 if (data->ocd_brw_size == 0) {
5139                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5140                                " ocd_version: %x ocd_grant: %d "
5141                                "ocd_index: %u ocd_brw_size is "
5142                                "unexpectedly zero, network data "
5143                                "corruption? Refusing connection of this"
5144                                " client\n",
5145                                exp->exp_obd->obd_name,
5146                                exp->exp_client_uuid.uuid,
5147                                exp, data->ocd_connect_flags, data->ocd_version,
5148                                data->ocd_grant, data->ocd_index);
5149                         return -EPROTO;
5150                 }
5151         }
5152
5153         /* NB: Disregard the rule against updating
5154          * exp_connect_data.ocd_connect_flags in this case, since
5155          * tgt_client_new() needs to know if this is a lightweight
5156          * connection, and it is safe to expose this flag before
5157          * connection processing completes. */
5158         if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5159                 spin_lock(&exp->exp_lock);
5160                 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5161                 spin_unlock(&exp->exp_lock);
5162         }
5163
5164         data->ocd_version = LUSTRE_VERSION_CODE;
5165         exp->exp_connect_data = *data;
5166         exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5167
5168         if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5169                 CWARN("%s: MDS requires FID support, but client not\n",
5170                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5171                 return -EBADE;
5172         }
5173
5174         if (mdt->mdt_som_conf &&
5175             !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5176                                          OBD_CONNECT_MDS_MDS |
5177                                          OBD_CONNECT_SOM))) {
5178                 CWARN("%s: MDS has SOM enabled, but client does not support "
5179                       "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5180                 return -EBADE;
5181         }
5182
5183         if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
5184                 if (suppress_pings) {
5185                         spin_lock(&exp->exp_obd->obd_dev_lock);
5186                         list_del_init(&exp->exp_obd_chain_timed);
5187                         spin_unlock(&exp->exp_obd->obd_dev_lock);
5188                 } else {
5189                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5190                 }
5191         }
5192
5193         return 0;
5194 }
5195
5196 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5197                                      struct obd_export *exp,
5198                                      struct ptlrpc_request *req)
5199 {
5200         struct sptlrpc_flavor   flvr;
5201         int                     rc = 0;
5202
5203         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5204                 read_lock(&mdt->mdt_sptlrpc_lock);
5205                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5206                                              req->rq_sp_from,
5207                                              req->rq_peer.nid,
5208                                              &flvr);
5209                 read_unlock(&mdt->mdt_sptlrpc_lock);
5210
5211                 spin_lock(&exp->exp_lock);
5212
5213                 exp->exp_sp_peer = req->rq_sp_from;
5214                 exp->exp_flvr = flvr;
5215
5216                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5217                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5218                         CERROR("unauthorized rpc flavor %x from %s, "
5219                                "expect %x\n", req->rq_flvr.sf_rpc,
5220                                libcfs_nid2str(req->rq_peer.nid),
5221                                exp->exp_flvr.sf_rpc);
5222                         rc = -EACCES;
5223                 }
5224
5225                 spin_unlock(&exp->exp_lock);
5226         } else {
5227                 if (exp->exp_sp_peer != req->rq_sp_from) {
5228                         CERROR("RPC source %s doesn't match %s\n",
5229                                sptlrpc_part2name(req->rq_sp_from),
5230                                sptlrpc_part2name(exp->exp_sp_peer));
5231                         rc = -EACCES;
5232                 } else {
5233                         rc = sptlrpc_target_export_check(exp, req);
5234                 }
5235         }
5236
5237         return rc;
5238 }
5239
5240 /* mds_connect copy */
5241 static int mdt_obd_connect(const struct lu_env *env,
5242                            struct obd_export **exp, struct obd_device *obd,
5243                            struct obd_uuid *cluuid,
5244                            struct obd_connect_data *data,
5245                            void *localdata)
5246 {
5247         struct mdt_thread_info *info;
5248         struct obd_export      *lexp;
5249         struct lustre_handle    conn = { 0 };
5250         struct mdt_device      *mdt;
5251         struct ptlrpc_request  *req;
5252         int                     rc;
5253         ENTRY;
5254
5255         LASSERT(env != NULL);
5256         if (!exp || !obd || !cluuid)
5257                 RETURN(-EINVAL);
5258
5259         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5260         req = info->mti_pill->rc_req;
5261         mdt = mdt_dev(obd->obd_lu_dev);
5262
5263         /*
5264          * first, check whether the stack is ready to handle requests
5265          * XXX: probably not very appropriate method is used now
5266          *      at some point we should find a better one
5267          */
5268         if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5269             !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5270                 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5271                 if (rc)
5272                         RETURN(-EAGAIN);
5273                 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5274         }
5275
5276         rc = class_connect(&conn, obd, cluuid);
5277         if (rc)
5278                 RETURN(rc);
5279
5280         lexp = class_conn2export(&conn);
5281         LASSERT(lexp != NULL);
5282
5283         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5284         if (rc)
5285                 GOTO(out, rc);
5286
5287         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5288                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5289
5290         rc = mdt_connect_internal(lexp, mdt, data);
5291         if (rc == 0) {
5292                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5293
5294                 LASSERT(lcd);
5295                 info->mti_exp = lexp;
5296                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5297                 rc = tgt_client_new(env, lexp);
5298                 if (rc == 0)
5299                         mdt_export_stats_init(obd, lexp, localdata);
5300         }
5301
5302 out:
5303         if (rc != 0) {
5304                 class_disconnect(lexp);
5305                 *exp = NULL;
5306         } else {
5307                 *exp = lexp;
5308         }
5309
5310         RETURN(rc);
5311 }
5312
5313 static int mdt_obd_reconnect(const struct lu_env *env,
5314                              struct obd_export *exp, struct obd_device *obd,
5315                              struct obd_uuid *cluuid,
5316                              struct obd_connect_data *data,
5317                              void *localdata)
5318 {
5319         struct mdt_thread_info *info;
5320         struct mdt_device      *mdt;
5321         struct ptlrpc_request  *req;
5322         int                     rc;
5323         ENTRY;
5324
5325         if (exp == NULL || obd == NULL || cluuid == NULL)
5326                 RETURN(-EINVAL);
5327
5328         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5329         req = info->mti_pill->rc_req;
5330         mdt = mdt_dev(obd->obd_lu_dev);
5331
5332         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5333         if (rc)
5334                 RETURN(rc);
5335
5336         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5337         if (rc == 0)
5338                 mdt_export_stats_init(obd, exp, localdata);
5339
5340         RETURN(rc);
5341 }
5342
5343 static int mdt_export_cleanup(struct obd_export *exp)
5344 {
5345         struct mdt_export_data *med = &exp->exp_mdt_data;
5346         struct obd_device      *obd = exp->exp_obd;
5347         struct mdt_device      *mdt;
5348         struct mdt_thread_info *info;
5349         struct lu_env           env;
5350         CFS_LIST_HEAD(closing_list);
5351         struct mdt_file_data *mfd, *n;
5352         int rc = 0;
5353         ENTRY;
5354
5355         spin_lock(&med->med_open_lock);
5356         while (!cfs_list_empty(&med->med_open_head)) {
5357                 cfs_list_t *tmp = med->med_open_head.next;
5358                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5359
5360                 /* Remove mfd handle so it can't be found again.
5361                  * We are consuming the mfd_list reference here. */
5362                 class_handle_unhash(&mfd->mfd_handle);
5363                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5364         }
5365         spin_unlock(&med->med_open_lock);
5366         mdt = mdt_dev(obd->obd_lu_dev);
5367         LASSERT(mdt != NULL);
5368
5369         rc = lu_env_init(&env, LCT_MD_THREAD);
5370         if (rc)
5371                 RETURN(rc);
5372
5373         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5374         LASSERT(info != NULL);
5375         memset(info, 0, sizeof *info);
5376         info->mti_env = &env;
5377         info->mti_mdt = mdt;
5378         info->mti_exp = exp;
5379
5380         if (!cfs_list_empty(&closing_list)) {
5381                 struct md_attr *ma = &info->mti_attr;
5382
5383                 /* Close any open files (which may also cause orphan unlinking). */
5384                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5385                         cfs_list_del_init(&mfd->mfd_list);
5386                         ma->ma_need = ma->ma_valid = 0;
5387                         /* Don't unlink orphan on failover umount, LU-184 */
5388                         if (exp->exp_flags & OBD_OPT_FAILOVER) {
5389                                 ma->ma_valid = MA_FLAGS;
5390                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5391                         }
5392                         mdt_mfd_close(info, mfd);
5393                 }
5394         }
5395         info->mti_mdt = NULL;
5396         /* cleanup client slot early */
5397         /* Do not erase record for recoverable client. */
5398         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5399                 tgt_client_del(&env, exp);
5400         lu_env_fini(&env);
5401
5402         RETURN(rc);
5403 }
5404
5405 static int mdt_obd_disconnect(struct obd_export *exp)
5406 {
5407         int rc;
5408         ENTRY;
5409
5410         LASSERT(exp);
5411         class_export_get(exp);
5412
5413         rc = server_disconnect_export(exp);
5414         if (rc != 0)
5415                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5416
5417         rc = mdt_export_cleanup(exp);
5418         class_export_put(exp);
5419         RETURN(rc);
5420 }
5421
5422 /* FIXME: Can we avoid using these two interfaces? */
5423 static int mdt_init_export(struct obd_export *exp)
5424 {
5425         struct mdt_export_data *med = &exp->exp_mdt_data;
5426         int                     rc;
5427         ENTRY;
5428
5429         CFS_INIT_LIST_HEAD(&med->med_open_head);
5430         spin_lock_init(&med->med_open_lock);
5431         mutex_init(&med->med_idmap_mutex);
5432         med->med_idmap = NULL;
5433         spin_lock(&exp->exp_lock);
5434         exp->exp_connecting = 1;
5435         spin_unlock(&exp->exp_lock);
5436
5437         /* self-export doesn't need client data and ldlm initialization */
5438         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5439                                      &exp->exp_client_uuid)))
5440                 RETURN(0);
5441
5442         rc = tgt_client_alloc(exp);
5443         if (rc)
5444                 GOTO(err, rc);
5445
5446         rc = ldlm_init_export(exp);
5447         if (rc)
5448                 GOTO(err_free, rc);
5449
5450         RETURN(rc);
5451
5452 err_free:
5453         tgt_client_free(exp);
5454 err:
5455         CERROR("%s: Failed to initialize export: rc = %d\n",
5456                exp->exp_obd->obd_name, rc);
5457         return rc;
5458 }
5459
5460 static int mdt_destroy_export(struct obd_export *exp)
5461 {
5462         ENTRY;
5463
5464         if (exp_connect_rmtclient(exp))
5465                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5466
5467         target_destroy_export(exp);
5468         /* destroy can be called from failed obd_setup, so
5469          * checking uuid is safer than obd_self_export */
5470         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5471                                      &exp->exp_client_uuid)))
5472                 RETURN(0);
5473
5474         ldlm_destroy_export(exp);
5475         tgt_client_free(exp);
5476
5477         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5478         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5479
5480         RETURN(0);
5481 }
5482
5483 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5484                             void *val, int vallen)
5485 {
5486         struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5487         struct getinfo_fid2path *fpout, *fpin;
5488         int rc = 0;
5489
5490         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5491         fpout = val;
5492
5493         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5494                 lustre_swab_fid2path(fpin);
5495
5496         memcpy(fpout, fpin, sizeof(*fpin));
5497         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5498                 RETURN(-EINVAL);
5499
5500         rc = mdt_fid2path(info->mti_env, mdt, fpout);
5501         RETURN(rc);
5502 }
5503
5504 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5505                 struct getinfo_fid2path *fp)
5506 {
5507         struct mdt_object *obj;
5508         struct obd_device *obd = mdt2obd_dev(mdt);
5509         int    rc;
5510         ENTRY;
5511
5512         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5513                 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5514
5515         if (!fid_is_sane(&fp->gf_fid))
5516                 RETURN(-EINVAL);
5517
5518         if (!fid_is_client_mdt_visible(&fp->gf_fid)) {
5519                 CWARN("%s: "DFID" is invalid, sequence should be "
5520                         ">= "LPX64"\n", obd->obd_name,
5521                         PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5522                 RETURN(-EINVAL);
5523         }
5524
5525         obj = mdt_object_find(env, mdt, &fp->gf_fid);
5526         if (obj == NULL || IS_ERR(obj)) {
5527                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5528                         PTR_ERR(obj));
5529                 RETURN(-EINVAL);
5530         }
5531
5532         if (mdt_object_remote(obj))
5533                 rc = -EREMOTE;
5534         else if (!mdt_object_exists(obj))
5535                 rc = -ENOENT;
5536
5537         if (rc < 0) {
5538                 mdt_object_put(env, obj);
5539                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5540                         PFID(&fp->gf_fid), rc);
5541                 RETURN(rc);
5542         }
5543
5544         rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5545                         fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5546         mdt_object_put(env, obj);
5547
5548         RETURN(rc);
5549 }
5550
5551 int mdt_get_info(struct mdt_thread_info *info)
5552 {
5553         struct ptlrpc_request *req = mdt_info_req(info);
5554         char *key;
5555         int keylen;
5556         __u32 *vallen;
5557         void *valout;
5558         int rc;
5559         ENTRY;
5560
5561         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5562         if (key == NULL) {
5563                 CDEBUG(D_IOCTL, "No GETINFO key");
5564                 RETURN(-EFAULT);
5565         }
5566         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5567                                       RCL_CLIENT);
5568
5569         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5570         if (vallen == NULL) {
5571                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5572                 RETURN(-EFAULT);
5573         }
5574
5575         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5576                              *vallen);
5577         rc = req_capsule_server_pack(info->mti_pill);
5578         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5579         if (valout == NULL) {
5580                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5581                 RETURN(-EFAULT);
5582         }
5583
5584         if (KEY_IS(KEY_FID2PATH))
5585                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5586         else
5587                 rc = -EINVAL;
5588
5589         lustre_msg_set_status(req->rq_repmsg, rc);
5590
5591         RETURN(rc);
5592 }
5593
5594 /* Pass the ioc down */
5595 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5596                          unsigned int cmd, int len, void *data)
5597 {
5598         struct lu_context ioctl_session;
5599         struct md_device *next = mdt->mdt_child;
5600         int rc;
5601         ENTRY;
5602
5603         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5604         if (rc)
5605                 RETURN(rc);
5606         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5607         lu_context_enter(&ioctl_session);
5608         env->le_ses = &ioctl_session;
5609
5610         LASSERT(next->md_ops->mdo_iocontrol);
5611         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5612
5613         lu_context_exit(&ioctl_session);
5614         lu_context_fini(&ioctl_session);
5615         RETURN(rc);
5616 }
5617
5618 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5619 {
5620         struct obd_ioctl_data *data = karg;
5621         struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5622         __u64 version;
5623         struct mdt_object *obj;
5624         struct mdt_lock_handle  *lh;
5625         int rc;
5626         ENTRY;
5627
5628         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5629         if (!fid_is_sane(fid))
5630                 RETURN(-EINVAL);
5631
5632         lh = &mti->mti_lh[MDT_LH_PARENT];
5633         mdt_lock_reg_init(lh, LCK_CR);
5634
5635         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5636         if (IS_ERR(obj))
5637                 RETURN(PTR_ERR(obj));
5638
5639         if (mdt_object_remote(obj)) {
5640                 rc = -EREMOTE;
5641                 /**
5642                  * before calling version get the correct MDS should be
5643                  * fid, this is error to find remote object here
5644                  */
5645                 CERROR("nonlocal object "DFID"\n", PFID(fid));
5646         } else if (!mdt_object_exists(obj)) {
5647                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5648                 rc = -ENOENT;
5649         } else {
5650                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5651                *(__u64 *)data->ioc_inlbuf2 = version;
5652                 rc = 0;
5653         }
5654         mdt_object_unlock_put(mti, obj, lh, 1);
5655         RETURN(rc);
5656 }
5657
5658 /* ioctls on obd dev */
5659 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5660                          void *karg, void *uarg)
5661 {
5662         struct lu_env      env;
5663         struct obd_device *obd = exp->exp_obd;
5664         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5665         struct dt_device  *dt = mdt->mdt_bottom;
5666         int rc;
5667
5668         ENTRY;
5669         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5670         rc = lu_env_init(&env, LCT_MD_THREAD);
5671         if (rc)
5672                 RETURN(rc);
5673
5674         switch (cmd) {
5675         case OBD_IOC_SYNC:
5676                 rc = mdt_device_sync(&env, mdt);
5677                 break;
5678         case OBD_IOC_SET_READONLY:
5679                 rc = dt->dd_ops->dt_ro(&env, dt);
5680                 break;
5681         case OBD_IOC_ABORT_RECOVERY:
5682                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5683                 target_stop_recovery_thread(obd);
5684                 rc = 0;
5685                 break;
5686         case OBD_IOC_CHANGELOG_REG:
5687         case OBD_IOC_CHANGELOG_DEREG:
5688         case OBD_IOC_CHANGELOG_CLEAR:
5689                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5690                 break;
5691         case OBD_IOC_START_LFSCK:
5692         case OBD_IOC_STOP_LFSCK: {
5693                 struct md_device *next = mdt->mdt_child;
5694                 struct obd_ioctl_data *data = karg;
5695
5696                 if (unlikely(data == NULL)) {
5697                         rc = -EINVAL;
5698                         break;
5699                 }
5700
5701                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5702                                                  data->ioc_inllen1,
5703                                                  data->ioc_inlbuf1);
5704                 break;
5705         }
5706         case OBD_IOC_GET_OBJ_VERSION: {
5707                 struct mdt_thread_info *mti;
5708                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5709                 memset(mti, 0, sizeof *mti);
5710                 mti->mti_env = &env;
5711                 mti->mti_mdt = mdt;
5712                 mti->mti_exp = exp;
5713
5714                 rc = mdt_ioc_version_get(mti, karg);
5715                 break;
5716         }
5717         default:
5718                 CERROR("Not supported cmd = %d for device %s\n",
5719                        cmd, obd->obd_name);
5720                 rc = -EOPNOTSUPP;
5721         }
5722
5723         lu_env_fini(&env);
5724         RETURN(rc);
5725 }
5726
5727 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5728 {
5729         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5730         int rc;
5731         ENTRY;
5732
5733         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5734         RETURN(rc);
5735 }
5736
5737 int mdt_obd_postrecov(struct obd_device *obd)
5738 {
5739         struct lu_env env;
5740         int rc;
5741
5742         rc = lu_env_init(&env, LCT_MD_THREAD);
5743         if (rc)
5744                 RETURN(rc);
5745         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5746         lu_env_fini(&env);
5747         return rc;
5748 }
5749
5750 static struct obd_ops mdt_obd_device_ops = {
5751         .o_owner          = THIS_MODULE,
5752         .o_set_info_async = mdt_obd_set_info_async,
5753         .o_connect        = mdt_obd_connect,
5754         .o_reconnect      = mdt_obd_reconnect,
5755         .o_disconnect     = mdt_obd_disconnect,
5756         .o_init_export    = mdt_init_export,
5757         .o_destroy_export = mdt_destroy_export,
5758         .o_iocontrol      = mdt_iocontrol,
5759         .o_postrecov      = mdt_obd_postrecov,
5760 };
5761
5762 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5763                                          struct lu_device *d)
5764 {
5765         struct mdt_device *m = mdt_dev(d);
5766         ENTRY;
5767
5768         mdt_fini(env, m);
5769         RETURN(NULL);
5770 }
5771
5772 static struct lu_device *mdt_device_free(const struct lu_env *env,
5773                                          struct lu_device *d)
5774 {
5775         struct mdt_device *m = mdt_dev(d);
5776         ENTRY;
5777
5778         md_device_fini(&m->mdt_md_dev);
5779         OBD_FREE_PTR(m);
5780         RETURN(NULL);
5781 }
5782
5783 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5784                                           struct lu_device_type *t,
5785                                           struct lustre_cfg *cfg)
5786 {
5787         struct lu_device  *l;
5788         struct mdt_device *m;
5789
5790         OBD_ALLOC_PTR(m);
5791         if (m != NULL) {
5792                 int rc;
5793
5794                 l = &m->mdt_md_dev.md_lu_dev;
5795                 rc = mdt_init0(env, m, t, cfg);
5796                 if (rc != 0) {
5797                         mdt_device_free(env, l);
5798                         l = ERR_PTR(rc);
5799                         return l;
5800                 }
5801         } else
5802                 l = ERR_PTR(-ENOMEM);
5803         return l;
5804 }
5805
5806 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5807 LU_KEY_INIT(mdt, struct mdt_thread_info);
5808
5809 static void mdt_key_fini(const struct lu_context *ctx,
5810                          struct lu_context_key *key, void* data)
5811 {
5812         struct mdt_thread_info *info = data;
5813
5814         if (info->mti_big_lmm) {
5815                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5816                 info->mti_big_lmm = NULL;
5817                 info->mti_big_lmmsize = 0;
5818         }
5819         OBD_FREE_PTR(info);
5820 }
5821
5822 /* context key: mdt_thread_key */
5823 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5824
5825 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5826 {
5827         return lu_ucred(info->mti_env);
5828 }
5829
5830 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5831 {
5832         return lu_ucred_check(info->mti_env);
5833 }
5834
5835 /**
5836  * Enable/disable COS (Commit On Sharing).
5837  *
5838  * Set/Clear the COS flag in mdt options.
5839  *
5840  * \param mdt mdt device
5841  * \param val 0 disables COS, other values enable COS
5842  */
5843 void mdt_enable_cos(struct mdt_device *mdt, int val)
5844 {
5845         struct lu_env env;
5846         int rc;
5847
5848         mdt->mdt_opts.mo_cos = !!val;
5849         rc = lu_env_init(&env, LCT_LOCAL);
5850         if (unlikely(rc != 0)) {
5851                 CWARN("lu_env initialization failed with rc = %d,"
5852                       "cannot sync\n", rc);
5853                 return;
5854         }
5855         mdt_device_sync(&env, mdt);
5856         lu_env_fini(&env);
5857 }
5858
5859 /**
5860  * Check COS (Commit On Sharing) status.
5861  *
5862  * Return COS flag status.
5863  *
5864  * \param mdt mdt device
5865  */
5866 int mdt_cos_is_enabled(struct mdt_device *mdt)
5867 {
5868         return mdt->mdt_opts.mo_cos != 0;
5869 }
5870
5871 static struct lu_device_type_operations mdt_device_type_ops = {
5872         .ldto_device_alloc = mdt_device_alloc,
5873         .ldto_device_free  = mdt_device_free,
5874         .ldto_device_fini  = mdt_device_fini
5875 };
5876
5877 static struct lu_device_type mdt_device_type = {
5878         .ldt_tags     = LU_DEVICE_MD,
5879         .ldt_name     = LUSTRE_MDT_NAME,
5880         .ldt_ops      = &mdt_device_type_ops,
5881         .ldt_ctx_tags = LCT_MD_THREAD
5882 };
5883
5884 static int __init mdt_mod_init(void)
5885 {
5886         struct lprocfs_static_vars lvars;
5887         int rc;
5888
5889         rc = lu_kmem_init(mdt_caches);
5890         if (rc)
5891                 return rc;
5892
5893         rc = mds_mod_init();
5894         if (rc)
5895                 GOTO(lu_fini, rc);
5896
5897         lprocfs_mdt_init_vars(&lvars);
5898         rc = class_register_type(&mdt_obd_device_ops, NULL,
5899                                  lvars.module_vars, LUSTRE_MDT_NAME,
5900                                  &mdt_device_type);
5901         if (rc)
5902                 GOTO(mds_fini, rc);
5903 lu_fini:
5904         if (rc)
5905                 lu_kmem_fini(mdt_caches);
5906 mds_fini:
5907         if (rc)
5908                 mds_mod_exit();
5909         return rc;
5910 }
5911
5912 static void __exit mdt_mod_exit(void)
5913 {
5914         class_unregister_type(LUSTRE_MDT_NAME);
5915         mds_mod_exit();
5916         lu_kmem_fini(mdt_caches);
5917 }
5918
5919 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5920 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5921 MODULE_LICENSE("GPL");
5922
5923 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);