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