Whamcloud - gitweb
fd66ec58fad148d65d03ff27e4ba39d076566007
[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_lfsck.h>
71
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73         [LCK_MINMODE] = MDL_MINMODE,
74         [LCK_EX]      = MDL_EX,
75         [LCK_PW]      = MDL_PW,
76         [LCK_PR]      = MDL_PR,
77         [LCK_CW]      = MDL_CW,
78         [LCK_CR]      = MDL_CR,
79         [LCK_NL]      = MDL_NL,
80         [LCK_GROUP]   = MDL_GROUP
81 };
82
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84         [MDL_MINMODE] = LCK_MINMODE,
85         [MDL_EX]      = LCK_EX,
86         [MDL_PW]      = LCK_PW,
87         [MDL_PR]      = LCK_PR,
88         [MDL_CW]      = LCK_CW,
89         [MDL_CR]      = LCK_CR,
90         [MDL_NL]      = LCK_NL,
91         [MDL_GROUP]   = LCK_GROUP
92 };
93
94 static struct mdt_device *mdt_dev(struct lu_device *d);
95 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
96
97 static const struct lu_object_operations mdt_obj_ops;
98
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
101
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
104
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
107
108 static struct lu_kmem_descr mdt_caches[] = {
109         {
110                 .ckd_cache = &mdt_object_kmem,
111                 .ckd_name  = "mdt_obj",
112                 .ckd_size  = sizeof(struct mdt_object)
113         },
114         {
115                 .ckd_cache      = &mdt_hsm_cdt_kmem,
116                 .ckd_name       = "mdt_cdt_restore_handle",
117                 .ckd_size       = sizeof(struct cdt_restore_handle)
118         },
119         {
120                 .ckd_cache      = &mdt_hsm_car_kmem,
121                 .ckd_name       = "mdt_cdt_agent_req",
122                 .ckd_size       = sizeof(struct cdt_agent_req)
123         },
124         {
125                 .ckd_cache = NULL
126         }
127 };
128
129 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
130 {
131         if (!rep)
132                 return 0;
133         return (rep->lock_policy_res1 & flag);
134 }
135
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137                            struct ldlm_reply *rep, int flag)
138 {
139         if (info) {
140                 info->mti_opdata &= ~flag;
141                 tgt_opdata_clear(info->mti_env, flag);
142         }
143         if (rep)
144                 rep->lock_policy_res1 &= ~flag;
145 }
146
147 void mdt_set_disposition(struct mdt_thread_info *info,
148                          struct ldlm_reply *rep, int flag)
149 {
150         if (info) {
151                 info->mti_opdata |= flag;
152                 tgt_opdata_set(info->mti_env, flag);
153         }
154         if (rep)
155                 rep->lock_policy_res1 |= flag;
156 }
157
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
159 {
160         lh->mlh_pdo_hash = 0;
161         lh->mlh_reg_mode = lm;
162         lh->mlh_rreg_mode = lm;
163         lh->mlh_type = MDT_REG_LOCK;
164 }
165
166 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
167                        const struct lu_name *lname)
168 {
169         lh->mlh_reg_mode = lock_mode;
170         lh->mlh_rreg_mode = lock_mode;
171         lh->mlh_type = MDT_PDO_LOCK;
172
173         if (lu_name_is_valid(lname)) {
174                 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
175                                                   lname->ln_namelen);
176                 /* XXX Workaround for LU-2856
177                  *
178                  * Zero is a valid return value of full_name_hash, but
179                  * several users of mlh_pdo_hash assume a non-zero
180                  * hash value. We therefore map zero onto an
181                  * arbitrary, but consistent value (1) to avoid
182                  * problems further down the road. */
183                 if (unlikely(lh->mlh_pdo_hash == 0))
184                         lh->mlh_pdo_hash = 1;
185         } else {
186                 lh->mlh_pdo_hash = 0;
187         }
188 }
189
190 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
191                               struct mdt_lock_handle *lh)
192 {
193         mdl_mode_t mode;
194         ENTRY;
195
196         /*
197          * Any dir access needs couple of locks:
198          *
199          * 1) on part of dir we gonna take lookup/modify;
200          *
201          * 2) on whole dir to protect it from concurrent splitting and/or to
202          * flush client's cache for readdir().
203          *
204          * so, for a given mode and object this routine decides what lock mode
205          * to use for lock #2:
206          *
207          * 1) if caller's gonna lookup in dir then we need to protect dir from
208          * being splitted only - LCK_CR
209          *
210          * 2) if caller's gonna modify dir then we need to protect dir from
211          * being splitted and to flush cache - LCK_CW
212          *
213          * 3) if caller's gonna modify dir and that dir seems ready for
214          * splitting then we need to protect it from any type of access
215          * (lookup/modify/split) - LCK_EX --bzzz
216          */
217
218         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
219         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
220
221         /*
222          * Ask underlaying level its opinion about preferable PDO lock mode
223          * having access type passed as regular lock mode:
224          *
225          * - MDL_MINMODE means that lower layer does not want to specify lock
226          * mode;
227          *
228          * - MDL_NL means that no PDO lock should be taken. This is used in some
229          * cases. Say, for non-splittable directories no need to use PDO locks
230          * at all.
231          */
232         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
233                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
234
235         if (mode != MDL_MINMODE) {
236                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
237         } else {
238                 /*
239                  * Lower layer does not want to specify locking mode. We do it
240                  * our selves. No special protection is needed, just flush
241                  * client's cache on modification and allow concurrent
242                  * mondification.
243                  */
244                 switch (lh->mlh_reg_mode) {
245                 case LCK_EX:
246                         lh->mlh_pdo_mode = LCK_EX;
247                         break;
248                 case LCK_PR:
249                         lh->mlh_pdo_mode = LCK_CR;
250                         break;
251                 case LCK_PW:
252                         lh->mlh_pdo_mode = LCK_CW;
253                         break;
254                 default:
255                         CERROR("Not expected lock type (0x%x)\n",
256                                (int)lh->mlh_reg_mode);
257                         LBUG();
258                 }
259         }
260
261         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
262         EXIT;
263 }
264
265 int mdt_getstatus(struct tgt_session_info *tsi)
266 {
267         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
268         struct mdt_device       *mdt = info->mti_mdt;
269         struct mdt_body         *repbody;
270         int                      rc;
271
272         ENTRY;
273
274         rc = mdt_check_ucred(info);
275         if (rc)
276                 GOTO(out, rc = err_serious(rc));
277
278         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
279                 GOTO(out, rc = err_serious(-ENOMEM));
280
281         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
282         repbody->fid1 = mdt->mdt_md_root_fid;
283         repbody->valid |= OBD_MD_FLID;
284
285         if (tsi->tsi_tgt->lut_mds_capa &&
286             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
287                 struct mdt_object       *root;
288                 struct lustre_capa      *capa;
289
290                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
291                 if (IS_ERR(root))
292                         GOTO(out, rc = PTR_ERR(root));
293
294                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
295                 LASSERT(capa);
296                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
297                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
298                                  0);
299                 mdt_object_put(info->mti_env, root);
300                 if (rc == 0)
301                         repbody->valid |= OBD_MD_FLMDSCAPA;
302         }
303         EXIT;
304 out:
305         mdt_thread_info_fini(info);
306         return rc;
307 }
308
309 int mdt_statfs(struct tgt_session_info *tsi)
310 {
311         struct ptlrpc_request           *req = tgt_ses_req(tsi);
312         struct mdt_thread_info          *info = tsi2mdt_info(tsi);
313         struct md_device                *next = info->mti_mdt->mdt_child;
314         struct ptlrpc_service_part      *svcpt;
315         struct obd_statfs               *osfs;
316         int                             rc;
317
318         ENTRY;
319
320         svcpt = req->rq_rqbd->rqbd_svcpt;
321
322         /* This will trigger a watchdog timeout */
323         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
324                          (MDT_SERVICE_WATCHDOG_FACTOR *
325                           at_get(&svcpt->scp_at_estimate)) + 1);
326
327         rc = mdt_check_ucred(info);
328         if (rc)
329                 GOTO(out, rc = err_serious(rc));
330
331         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
332                 GOTO(out, rc = err_serious(-ENOMEM));
333
334         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
335         if (!osfs)
336                 GOTO(out, rc = -EPROTO);
337
338         /** statfs information are cached in the mdt_device */
339         if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
340                                cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
341                 /** statfs data is too old, get up-to-date one */
342                 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
343                 if (rc)
344                         GOTO(out, rc);
345                 spin_lock(&info->mti_mdt->mdt_osfs_lock);
346                 info->mti_mdt->mdt_osfs = *osfs;
347                 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
348                 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
349         } else {
350                 /** use cached statfs data */
351                 spin_lock(&info->mti_mdt->mdt_osfs_lock);
352                 *osfs = info->mti_mdt->mdt_osfs;
353                 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
354         }
355
356         if (rc == 0)
357                 mdt_counter_incr(req, LPROC_MDT_STATFS);
358 out:
359         mdt_thread_info_fini(info);
360         RETURN(rc);
361 }
362
363 /**
364  * Pack SOM attributes into the reply.
365  * Call under a DLM UPDATE lock.
366  */
367 static void mdt_pack_size2body(struct mdt_thread_info *info,
368                                struct mdt_object *mo)
369 {
370         struct mdt_body *b;
371         struct md_attr *ma = &info->mti_attr;
372
373         LASSERT(ma->ma_attr.la_valid & LA_MODE);
374         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
375
376         /* Check if Size-on-MDS is supported, if this is a regular file,
377          * if SOM is enabled on the object and if SOM cache exists and valid.
378          * Otherwise do not pack Size-on-MDS attributes to the reply. */
379         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
380             !S_ISREG(ma->ma_attr.la_mode) ||
381             !mdt_object_is_som_enabled(mo) ||
382             !(ma->ma_valid & MA_SOM))
383                 return;
384
385         b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
386         b->size = ma->ma_som->msd_size;
387         b->blocks = ma->ma_som->msd_blocks;
388 }
389
390 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
391                         const struct lu_attr *attr, const struct lu_fid *fid)
392 {
393         struct md_attr *ma = &info->mti_attr;
394
395         LASSERT(ma->ma_valid & MA_INODE);
396
397         b->atime      = attr->la_atime;
398         b->mtime      = attr->la_mtime;
399         b->ctime      = attr->la_ctime;
400         b->mode       = attr->la_mode;
401         b->size       = attr->la_size;
402         b->blocks     = attr->la_blocks;
403         b->uid        = attr->la_uid;
404         b->gid        = attr->la_gid;
405         b->flags      = attr->la_flags;
406         b->nlink      = attr->la_nlink;
407         b->rdev       = attr->la_rdev;
408
409         /*XXX should pack the reply body according to lu_valid*/
410         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
411                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
412                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
413                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
414
415         if (!S_ISREG(attr->la_mode)) {
416                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
417         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
418                 /* means no objects are allocated on osts. */
419                 LASSERT(!(ma->ma_valid & MA_LOV));
420                 /* just ignore blocks occupied by extend attributes on MDS */
421                 b->blocks = 0;
422                 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
423                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
424         } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
425                    ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
426                 /* A released file stores its size on MDS. */
427                 /* But return 1 block for released file, unless tools like tar
428                  * will consider it fully sparse. (LU-3864)
429                  */
430                 if (unlikely(b->size == 0))
431                         b->blocks = 0;
432                 else
433                         b->blocks = 1;
434                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
435         }
436
437         if (fid) {
438                 b->fid1 = *fid;
439                 b->valid |= OBD_MD_FLID;
440                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
441                                 PFID(fid), b->nlink, b->mode, b->size);
442         }
443
444         if (info)
445                 mdt_body_reverse_idmap(info, b);
446
447         if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
448                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
449                        PFID(fid), (unsigned long long)b->size);
450 }
451
452 static inline int mdt_body_has_lov(const struct lu_attr *la,
453                                    const struct mdt_body *body)
454 {
455         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
456                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
457 }
458
459 void mdt_client_compatibility(struct mdt_thread_info *info)
460 {
461         struct mdt_body       *body;
462         struct ptlrpc_request *req = mdt_info_req(info);
463         struct obd_export     *exp = req->rq_export;
464         struct md_attr        *ma = &info->mti_attr;
465         struct lu_attr        *la = &ma->ma_attr;
466         ENTRY;
467
468         if (exp_connect_layout(exp))
469                 /* the client can deal with 16-bit lmm_stripe_count */
470                 RETURN_EXIT;
471
472         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
473
474         if (!mdt_body_has_lov(la, body))
475                 RETURN_EXIT;
476
477         /* now we have a reply with a lov for a client not compatible with the
478          * layout lock so we have to clean the layout generation number */
479         if (S_ISREG(la->la_mode))
480                 ma->ma_lmm->lmm_layout_gen = 0;
481         EXIT;
482 }
483
484 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
485                              const char *name)
486 {
487         const struct lu_env *env = info->mti_env;
488         int rc;
489         ENTRY;
490
491         LASSERT(info->mti_big_lmm_used == 0);
492         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
493         if (rc < 0)
494                 RETURN(rc);
495
496         /* big_lmm may need to be grown */
497         if (info->mti_big_lmmsize < rc) {
498                 int size = size_roundup_power2(rc);
499
500                 if (info->mti_big_lmmsize > 0) {
501                         /* free old buffer */
502                         LASSERT(info->mti_big_lmm);
503                         OBD_FREE_LARGE(info->mti_big_lmm,
504                                        info->mti_big_lmmsize);
505                         info->mti_big_lmm = NULL;
506                         info->mti_big_lmmsize = 0;
507                 }
508
509                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
510                 if (info->mti_big_lmm == NULL)
511                         RETURN(-ENOMEM);
512                 info->mti_big_lmmsize = size;
513         }
514         LASSERT(info->mti_big_lmmsize >= rc);
515
516         info->mti_buf.lb_buf = info->mti_big_lmm;
517         info->mti_buf.lb_len = info->mti_big_lmmsize;
518         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
519
520         RETURN(rc);
521 }
522
523 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
524                    struct md_attr *ma, const char *name)
525 {
526         struct md_object *next = mdt_object_child(o);
527         struct lu_buf    *buf = &info->mti_buf;
528         int rc;
529
530         if (strcmp(name, XATTR_NAME_LOV) == 0) {
531                 buf->lb_buf = ma->ma_lmm;
532                 buf->lb_len = ma->ma_lmm_size;
533                 LASSERT(!(ma->ma_valid & MA_LOV));
534         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
535                 buf->lb_buf = ma->ma_lmv;
536                 buf->lb_len = ma->ma_lmv_size;
537                 LASSERT(!(ma->ma_valid & MA_LMV));
538         } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
539                 buf->lb_buf = ma->ma_lmv;
540                 buf->lb_len = ma->ma_lmv_size;
541                 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
542         } else {
543                 return -EINVAL;
544         }
545
546         rc = mo_xattr_get(info->mti_env, next, buf, name);
547         if (rc > 0) {
548                 if (strcmp(name, XATTR_NAME_LOV) == 0) {
549                         ma->ma_lmm_size = rc;
550                         ma->ma_valid |= MA_LOV;
551                 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
552                         ma->ma_lmv_size = rc;
553                         ma->ma_valid |= MA_LMV;
554                 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
555                         ma->ma_lmv_size = rc;
556                         ma->ma_valid |= MA_LMV_DEF;
557                 }
558
559                 rc = 0;
560         } else if (rc == -ENODATA) {
561                 /* no LOV EA */
562                 rc = 0;
563         } else if (rc == -ERANGE) {
564                 /* Default LMV has fixed size, so it must be able to fit
565                  * in the original buffer */
566                 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
567                         return rc;
568                 rc = mdt_big_xattr_get(info, o, name);
569                 if (rc > 0) {
570                         info->mti_big_lmm_used = 1;
571                         if (!strcmp(name, XATTR_NAME_LOV)) {
572                                 ma->ma_valid |= MA_LOV;
573                                 ma->ma_lmm = info->mti_big_lmm;
574                                 ma->ma_lmm_size = rc;
575                         } else if (!strcmp(name, XATTR_NAME_LMV)) {
576                                 ma->ma_valid |= MA_LMV;
577                                 ma->ma_lmv = info->mti_big_lmm;
578                                 ma->ma_lmv_size = rc;
579                         } else {
580                                 return -EINVAL;
581                         }
582
583                         /* update mdt_max_mdsize so all clients
584                          * will be aware about that */
585                         if (info->mti_mdt->mdt_max_mdsize < rc)
586                                 info->mti_mdt->mdt_max_mdsize = rc;
587                         rc = 0;
588                 }
589         }
590
591         return rc;
592 }
593
594 int mdt_attr_get_pfid(struct mdt_thread_info *info,
595                       struct mdt_object *o, struct lu_fid *pfid)
596 {
597         struct lu_buf           *buf = &info->mti_buf;
598         struct link_ea_header   *leh;
599         struct link_ea_entry    *lee;
600         int                      rc;
601         ENTRY;
602
603         buf->lb_buf = info->mti_big_lmm;
604         buf->lb_len = info->mti_big_lmmsize;
605         rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
606                           buf, XATTR_NAME_LINK);
607         /* ignore errors, MA_PFID won't be set and it is
608          * up to the caller to treat this as an error */
609         if (rc == -ERANGE || buf->lb_len == 0) {
610                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
611                 buf->lb_buf = info->mti_big_lmm;
612                 buf->lb_len = info->mti_big_lmmsize;
613         }
614
615         if (rc < 0)
616                 RETURN(rc);
617         if (rc < sizeof(*leh)) {
618                 CERROR("short LinkEA on "DFID": rc = %d\n",
619                        PFID(mdt_object_fid(o)), rc);
620                 RETURN(-ENODATA);
621         }
622
623         leh = (struct link_ea_header *) buf->lb_buf;
624         lee = (struct link_ea_entry *)(leh + 1);
625         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
626                 leh->leh_magic = LINK_EA_MAGIC;
627                 leh->leh_reccount = __swab32(leh->leh_reccount);
628                 leh->leh_len = __swab64(leh->leh_len);
629         }
630         if (leh->leh_magic != LINK_EA_MAGIC)
631                 RETURN(-EINVAL);
632         if (leh->leh_reccount == 0)
633                 RETURN(-ENODATA);
634
635         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
636         fid_be_to_cpu(pfid, pfid);
637
638         RETURN(0);
639 }
640
641 int mdt_attr_get_complex(struct mdt_thread_info *info,
642                          struct mdt_object *o, struct md_attr *ma)
643 {
644         const struct lu_env *env = info->mti_env;
645         struct md_object    *next = mdt_object_child(o);
646         struct lu_buf       *buf = &info->mti_buf;
647         u32                  mode = lu_object_attr(&next->mo_lu);
648         int                  need = ma->ma_need;
649         int                  rc = 0, rc2;
650         ENTRY;
651
652         ma->ma_valid = 0;
653
654         if (need & MA_INODE) {
655                 ma->ma_need = MA_INODE;
656                 rc = mo_attr_get(env, next, ma);
657                 if (rc)
658                         GOTO(out, rc);
659                 ma->ma_valid |= MA_INODE;
660         }
661
662         if (need & MA_PFID) {
663                 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
664                 if (rc == 0)
665                         ma->ma_valid |= MA_PFID;
666                 /* ignore this error, parent fid is not mandatory */
667                 rc = 0;
668         }
669
670         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
671                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
672                 if (rc)
673                         GOTO(out, rc);
674         }
675
676         if (need & MA_LMV && S_ISDIR(mode)) {
677                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
678                 if (rc != 0)
679                         GOTO(out, rc);
680         }
681
682         if (need & MA_LMV_DEF && S_ISDIR(mode)) {
683                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
684                 if (rc != 0)
685                         GOTO(out, rc);
686         }
687
688         if (need & MA_SOM && S_ISREG(mode)) {
689                 buf->lb_buf = info->mti_xattr_buf;
690                 buf->lb_len = sizeof(info->mti_xattr_buf);
691                 CLASSERT(sizeof(struct som_attrs) <=
692                          sizeof(info->mti_xattr_buf));
693                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
694                 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
695                 if (rc2 == 0)
696                         ma->ma_valid |= MA_SOM;
697                 else if (rc2 < 0 && rc2 != -ENODATA)
698                         GOTO(out, rc = rc2);
699         }
700
701         if (need & MA_HSM && S_ISREG(mode)) {
702                 buf->lb_buf = info->mti_xattr_buf;
703                 buf->lb_len = sizeof(info->mti_xattr_buf);
704                 CLASSERT(sizeof(struct hsm_attrs) <=
705                          sizeof(info->mti_xattr_buf));
706                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
707                 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
708                 if (rc2 == 0)
709                         ma->ma_valid |= MA_HSM;
710                 else if (rc2 < 0 && rc2 != -ENODATA)
711                         GOTO(out, rc = rc2);
712         }
713
714 #ifdef CONFIG_FS_POSIX_ACL
715         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
716                 buf->lb_buf = ma->ma_acl;
717                 buf->lb_len = ma->ma_acl_size;
718                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
719                 if (rc2 > 0) {
720                         ma->ma_acl_size = rc2;
721                         ma->ma_valid |= MA_ACL_DEF;
722                 } else if (rc2 == -ENODATA) {
723                         /* no ACLs */
724                         ma->ma_acl_size = 0;
725                 } else
726                         GOTO(out, rc = rc2);
727         }
728 #endif
729 out:
730         ma->ma_need = need;
731         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
732                rc, ma->ma_valid, ma->ma_lmm);
733         RETURN(rc);
734 }
735
736 static int mdt_getattr_internal(struct mdt_thread_info *info,
737                                 struct mdt_object *o, int ma_need)
738 {
739         struct md_object        *next = mdt_object_child(o);
740         const struct mdt_body   *reqbody = info->mti_body;
741         struct ptlrpc_request   *req = mdt_info_req(info);
742         struct md_attr          *ma = &info->mti_attr;
743         struct lu_attr          *la = &ma->ma_attr;
744         struct req_capsule      *pill = info->mti_pill;
745         const struct lu_env     *env = info->mti_env;
746         struct mdt_body         *repbody;
747         struct lu_buf           *buffer = &info->mti_buf;
748         int                     rc;
749         int                     is_root;
750         ENTRY;
751
752         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
753                 RETURN(err_serious(-ENOMEM));
754
755         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
756
757         ma->ma_valid = 0;
758
759         if (mdt_object_remote(o)) {
760                 /* This object is located on remote node.*/
761                 /* Return -EIO for old client */
762                 if (!mdt_is_dne_client(req->rq_export))
763                         GOTO(out, rc = -EIO);
764
765                 repbody->fid1 = *mdt_object_fid(o);
766                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
767                 GOTO(out, rc = 0);
768         }
769
770         buffer->lb_len = reqbody->eadatasize;
771         if (buffer->lb_len > 0) {
772                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
773                 if (buffer->lb_buf == NULL)
774                         GOTO(out, rc = -EPROTO);
775         } else {
776                 buffer->lb_buf = NULL;
777                 ma_need &= ~(MA_LOV | MA_LMV);
778                 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
779                        mdt_obd_name(info->mti_mdt),
780                        req->rq_export->exp_client_uuid.uuid);
781         }
782
783         /* If it is dir object and client require MEA, then we got MEA */
784         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
785             (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
786                 /* Assumption: MDT_MD size is enough for lmv size. */
787                 ma->ma_lmv = buffer->lb_buf;
788                 ma->ma_lmv_size = buffer->lb_len;
789                 ma->ma_need = MA_INODE;
790                 if (ma->ma_lmv_size > 0) {
791                         if (reqbody->valid & OBD_MD_MEA)
792                                 ma->ma_need |= MA_LMV;
793                         else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
794                                 ma->ma_need |= MA_LMV_DEF;
795                 }
796         } else {
797                 ma->ma_lmm = buffer->lb_buf;
798                 ma->ma_lmm_size = buffer->lb_len;
799                 ma->ma_need = MA_INODE | MA_HSM;
800                 if (ma->ma_lmm_size > 0)
801                         ma->ma_need |= MA_LOV;
802         }
803
804         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
805             reqbody->valid & OBD_MD_FLDIREA  &&
806             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
807                 /* get default stripe info for this dir. */
808                 ma->ma_need |= MA_LOV_DEF;
809         }
810         ma->ma_need |= ma_need;
811         if (ma->ma_need & MA_SOM)
812                 ma->ma_som = &info->mti_u.som.data;
813
814         rc = mdt_attr_get_complex(info, o, ma);
815         if (unlikely(rc)) {
816                 CERROR("%s: getattr error for "DFID": rc = %d\n",
817                        mdt_obd_name(info->mti_mdt),
818                        PFID(mdt_object_fid(o)), rc);
819                 RETURN(rc);
820         }
821
822         /* if file is released, check if a restore is running */
823         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
824             mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
825                 repbody->t_state = MS_RESTORE;
826                 repbody->valid |= OBD_MD_TSTATE;
827         }
828
829         is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
830
831         /* the Lustre protocol supposes to return default striping
832          * on the user-visible root if explicitly requested */
833         if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
834             (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
835                 struct lu_fid      rootfid;
836                 struct mdt_object *root;
837                 struct mdt_device *mdt = info->mti_mdt;
838
839                 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
840                 if (rc)
841                         RETURN(rc);
842                 root = mdt_object_find(env, mdt, &rootfid);
843                 if (IS_ERR(root))
844                         RETURN(PTR_ERR(root));
845                 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
846                 mdt_object_put(info->mti_env, root);
847                 if (unlikely(rc)) {
848                         CERROR("%s: getattr error for "DFID": rc = %d\n",
849                                mdt_obd_name(info->mti_mdt),
850                                PFID(mdt_object_fid(o)), rc);
851                         RETURN(rc);
852                 }
853         }
854
855         if (likely(ma->ma_valid & MA_INODE))
856                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
857         else
858                 RETURN(-EFAULT);
859
860         if (mdt_body_has_lov(la, reqbody)) {
861                 if (ma->ma_valid & MA_LOV) {
862                         LASSERT(ma->ma_lmm_size);
863                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
864                         repbody->eadatasize = ma->ma_lmm_size;
865                         if (S_ISDIR(la->la_mode))
866                                 repbody->valid |= OBD_MD_FLDIREA;
867                         else
868                                 repbody->valid |= OBD_MD_FLEASIZE;
869                 }
870                 if (ma->ma_valid & MA_LMV) {
871                         LASSERT(S_ISDIR(la->la_mode));
872                         mdt_dump_lmv(D_INFO, ma->ma_lmv);
873                         repbody->eadatasize = ma->ma_lmv_size;
874                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
875                 }
876                 if (ma->ma_valid & MA_LMV_DEF) {
877                         LASSERT(S_ISDIR(la->la_mode));
878                         repbody->eadatasize = ma->ma_lmv_size;
879                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
880                 }
881         } else if (S_ISLNK(la->la_mode) &&
882                    reqbody->valid & OBD_MD_LINKNAME) {
883                 buffer->lb_buf = ma->ma_lmm;
884                 /* eadatasize from client includes NULL-terminator, so
885                  * there is no need to read it */
886                 buffer->lb_len = reqbody->eadatasize - 1;
887                 rc = mo_readlink(env, next, buffer);
888                 if (unlikely(rc <= 0)) {
889                         CERROR("%s: readlink failed for "DFID": rc = %d\n",
890                                mdt_obd_name(info->mti_mdt),
891                                PFID(mdt_object_fid(o)), rc);
892                         rc = -EFAULT;
893                 } else {
894                         int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
895
896                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
897                                 rc -= 2;
898                         repbody->valid |= OBD_MD_LINKNAME;
899                         /* we need to report back size with NULL-terminator
900                          * because client expects that */
901                         repbody->eadatasize = rc + 1;
902                         if (repbody->eadatasize != reqbody->eadatasize)
903                                 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
904                                        "on "DFID ", expected %d\n",
905                                        mdt_obd_name(info->mti_mdt),
906                                        rc, PFID(mdt_object_fid(o)),
907                                        reqbody->eadatasize - 1);
908                         /* NULL terminate */
909                         ((char *)ma->ma_lmm)[rc] = 0;
910
911                         /* If the total CDEBUG() size is larger than a page, it
912                          * will print a warning to the console, avoid this by
913                          * printing just the last part of the symlink. */
914                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
915                                print_limit < rc ? "..." : "", print_limit,
916                                (char *)ma->ma_lmm + rc - print_limit, rc);
917                         rc = 0;
918                 }
919         }
920
921         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
922                 repbody->max_cookiesize = 0;
923                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
924                 repbody->valid |= OBD_MD_FLMODEASIZE;
925                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
926                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
927                        repbody->max_cookiesize);
928         }
929
930         if (exp_connect_rmtclient(info->mti_exp) &&
931             reqbody->valid & OBD_MD_FLRMTPERM) {
932                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
933
934                 /* mdt_getattr_lock only */
935                 rc = mdt_pack_remote_perm(info, o, buf);
936                 if (rc) {
937                         repbody->valid &= ~OBD_MD_FLRMTPERM;
938                         repbody->aclsize = 0;
939                         RETURN(rc);
940                 } else {
941                         repbody->valid |= OBD_MD_FLRMTPERM;
942                         repbody->aclsize = sizeof(struct mdt_remote_perm);
943                 }
944         }
945 #ifdef CONFIG_FS_POSIX_ACL
946         else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
947                  (reqbody->valid & OBD_MD_FLACL)) {
948                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
949                 buffer->lb_len = req_capsule_get_size(pill,
950                                                       &RMF_ACL, RCL_SERVER);
951                 if (buffer->lb_len > 0) {
952                         rc = mo_xattr_get(env, next, buffer,
953                                           XATTR_NAME_ACL_ACCESS);
954                         if (rc < 0) {
955                                 if (rc == -ENODATA) {
956                                         repbody->aclsize = 0;
957                                         repbody->valid |= OBD_MD_FLACL;
958                                         rc = 0;
959                                 } else if (rc == -EOPNOTSUPP) {
960                                         rc = 0;
961                                 } else {
962                                         CERROR("%s: unable to read "DFID
963                                                " ACL: rc = %d\n",
964                                                mdt_obd_name(info->mti_mdt),
965                                                PFID(mdt_object_fid(o)), rc);
966                                 }
967                         } else {
968                                 repbody->aclsize = rc;
969                                 repbody->valid |= OBD_MD_FLACL;
970                                 rc = 0;
971                         }
972                 }
973         }
974 #endif
975
976         if (reqbody->valid & OBD_MD_FLMDSCAPA &&
977             info->mti_mdt->mdt_lut.lut_mds_capa &&
978             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
979                 struct lustre_capa *capa;
980
981                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
982                 LASSERT(capa);
983                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
984                 rc = mo_capa_get(env, next, capa, 0);
985                 if (rc)
986                         RETURN(rc);
987                 repbody->valid |= OBD_MD_FLMDSCAPA;
988         }
989
990 out:
991         if (rc == 0)
992                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
993
994         RETURN(rc);
995 }
996
997 static int mdt_renew_capa(struct mdt_thread_info *info)
998 {
999         struct mdt_object  *obj = info->mti_object;
1000         struct mdt_body    *body;
1001         struct lustre_capa *capa, *c;
1002         int rc;
1003         ENTRY;
1004
1005         /* if object doesn't exist, or server has disabled capability,
1006          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1007          * flag not set.
1008          */
1009         if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1010             !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1011                 RETURN(0);
1012
1013         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1014         LASSERT(body != NULL);
1015
1016         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1017         LASSERT(c);
1018
1019         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1020         LASSERT(capa);
1021
1022         *capa = *c;
1023         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1024         if (rc == 0)
1025                 body->valid |= OBD_MD_FLOSSCAPA;
1026         RETURN(rc);
1027 }
1028
1029 int mdt_getattr(struct tgt_session_info *tsi)
1030 {
1031         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1032         struct mdt_object       *obj = info->mti_object;
1033         struct req_capsule      *pill = info->mti_pill;
1034         struct mdt_body         *reqbody;
1035         struct mdt_body         *repbody;
1036         mode_t                   mode;
1037         int rc, rc2;
1038         ENTRY;
1039
1040         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1041         LASSERT(reqbody);
1042
1043         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1044                 rc = req_capsule_server_pack(pill);
1045                 if (unlikely(rc))
1046                         RETURN(err_serious(rc));
1047                 rc = mdt_renew_capa(info);
1048                 GOTO(out_shrink, rc);
1049         }
1050
1051         LASSERT(obj != NULL);
1052         LASSERT(lu_object_assert_exists(&obj->mot_obj));
1053
1054         mode = lu_object_attr(&obj->mot_obj);
1055
1056         /* old clients may not report needed easize, use max value then */
1057         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1058                              reqbody->eadatasize == 0 ?
1059                              info->mti_mdt->mdt_max_mdsize :
1060                              reqbody->eadatasize);
1061
1062         rc = req_capsule_server_pack(pill);
1063         if (unlikely(rc != 0))
1064                 GOTO(out, rc = err_serious(rc));
1065
1066         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1067         LASSERT(repbody != NULL);
1068         repbody->eadatasize = 0;
1069         repbody->aclsize = 0;
1070
1071         if (reqbody->valid & OBD_MD_FLRMTPERM)
1072                 rc = mdt_init_ucred(info, reqbody);
1073         else
1074                 rc = mdt_check_ucred(info);
1075         if (unlikely(rc))
1076                 GOTO(out_shrink, rc);
1077
1078         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1079
1080         /*
1081          * Don't check capability at all, because rename might getattr for
1082          * remote obj, and at that time no capability is available.
1083          */
1084         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1085         rc = mdt_getattr_internal(info, obj, 0);
1086         if (reqbody->valid & OBD_MD_FLRMTPERM)
1087                 mdt_exit_ucred(info);
1088         EXIT;
1089 out_shrink:
1090         mdt_client_compatibility(info);
1091         rc2 = mdt_fix_reply(info);
1092         if (rc == 0)
1093                 rc = rc2;
1094 out:
1095         mdt_thread_info_fini(info);
1096         return rc;
1097 }
1098
1099 int mdt_is_subdir(struct tgt_session_info *tsi)
1100 {
1101         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1102         struct mdt_object     *o = info->mti_object;
1103         struct req_capsule    *pill = info->mti_pill;
1104         const struct mdt_body *body = info->mti_body;
1105         struct mdt_body       *repbody;
1106         int                    rc;
1107         ENTRY;
1108
1109         LASSERT(o != NULL);
1110
1111         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1112
1113         /*
1114          * We save last checked parent fid to @repbody->fid1 for remote
1115          * directory case.
1116          */
1117         LASSERT(fid_is_sane(&body->fid2));
1118         LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1119         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1120                            &body->fid2, &repbody->fid1);
1121         if (rc == 0 || rc == -EREMOTE)
1122                 repbody->valid |= OBD_MD_FLID;
1123
1124         mdt_thread_info_fini(info);
1125         RETURN(rc);
1126 }
1127
1128 int mdt_swap_layouts(struct tgt_session_info *tsi)
1129 {
1130         struct mdt_thread_info  *info;
1131         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1132         struct obd_export       *exp = req->rq_export;
1133         struct mdt_object       *o1, *o2, *o;
1134         struct mdt_lock_handle  *lh1, *lh2;
1135         struct mdc_swap_layouts *msl;
1136         int                      rc;
1137         ENTRY;
1138
1139         /* client does not support layout lock, so layout swaping
1140          * is disabled.
1141          * FIXME: there is a problem for old clients which don't support
1142          * layout lock yet. If those clients have already opened the file
1143          * they won't be notified at all so that old layout may still be
1144          * used to do IO. This can be fixed after file release is landed by
1145          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1146         if (!exp_connect_layout(exp))
1147                 RETURN(-EOPNOTSUPP);
1148
1149         info = tsi2mdt_info(tsi);
1150         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1151                 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1152                                  req_capsule_client_get(info->mti_pill,
1153                                                         &RMF_CAPA1));
1154
1155         if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1156                 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1157                                  req_capsule_client_get(info->mti_pill,
1158                                                         &RMF_CAPA2));
1159
1160         o1 = info->mti_object;
1161         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1162                                 &info->mti_body->fid2);
1163         if (IS_ERR(o))
1164                 GOTO(out, rc = PTR_ERR(o));
1165
1166         if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1167                 GOTO(put, rc = -ENOENT);
1168
1169         rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1170         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1171                 GOTO(put, rc);
1172
1173         if (rc < 0)
1174                 swap(o1, o2);
1175
1176         /* permission check. Make sure the calling process having permission
1177          * to write both files. */
1178         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1179                                 MAY_WRITE);
1180         if (rc < 0)
1181                 GOTO(put, rc);
1182
1183         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1184                                 MAY_WRITE);
1185         if (rc < 0)
1186                 GOTO(put, rc);
1187
1188         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1189         if (msl == NULL)
1190                 GOTO(put, rc = -EPROTO);
1191
1192         lh1 = &info->mti_lh[MDT_LH_NEW];
1193         mdt_lock_reg_init(lh1, LCK_EX);
1194         lh2 = &info->mti_lh[MDT_LH_OLD];
1195         mdt_lock_reg_init(lh2, LCK_EX);
1196
1197         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1198                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1199         if (rc < 0)
1200                 GOTO(put, rc);
1201
1202         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1203                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1204         if (rc < 0)
1205                 GOTO(unlock1, rc);
1206
1207         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1208                              mdt_object_child(o2), msl->msl_flags);
1209         GOTO(unlock2, rc);
1210 unlock2:
1211         mdt_object_unlock(info, o2, lh2, rc);
1212 unlock1:
1213         mdt_object_unlock(info, o1, lh1, rc);
1214 put:
1215         mdt_object_put(info->mti_env, o);
1216 out:
1217         mdt_thread_info_fini(info);
1218         RETURN(rc);
1219 }
1220
1221 static int mdt_raw_lookup(struct mdt_thread_info *info,
1222                           struct mdt_object *parent,
1223                           const struct lu_name *lname,
1224                           struct ldlm_reply *ldlm_rep)
1225 {
1226         struct md_object *next = mdt_object_child(info->mti_object);
1227         const struct mdt_body *reqbody = info->mti_body;
1228         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1229         struct mdt_body *repbody;
1230         int rc;
1231         ENTRY;
1232
1233         if (reqbody->valid != OBD_MD_FLID)
1234                 RETURN(0);
1235
1236         LASSERT(!info->mti_cross_ref);
1237
1238         /* Only got the fid of this obj by name */
1239         fid_zero(child_fid);
1240         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1241                         &info->mti_spec);
1242 #if 0
1243         /* XXX is raw_lookup possible as intent operation? */
1244         if (rc != 0) {
1245                 if (rc == -ENOENT)
1246                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1247                 RETURN(rc);
1248         } else
1249                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1250
1251         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1252 #endif
1253         if (rc == 0) {
1254                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1255                 repbody->fid1 = *child_fid;
1256                 repbody->valid = OBD_MD_FLID;
1257         }
1258         RETURN(1);
1259 }
1260
1261 /*
1262  * UPDATE lock should be taken against parent, and be release before exit;
1263  * child_bits lock should be taken against child, and be returned back:
1264  *            (1)normal request should release the child lock;
1265  *            (2)intent request will grant the lock to client.
1266  */
1267 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1268                                  struct mdt_lock_handle *lhc,
1269                                  __u64 child_bits,
1270                                  struct ldlm_reply *ldlm_rep)
1271 {
1272         struct ptlrpc_request  *req       = mdt_info_req(info);
1273         struct mdt_body        *reqbody   = NULL;
1274         struct mdt_object      *parent    = info->mti_object;
1275         struct mdt_object      *child;
1276         struct md_object       *next      = mdt_object_child(parent);
1277         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
1278         struct lu_name         *lname     = NULL;
1279         struct mdt_lock_handle *lhp       = NULL;
1280         struct ldlm_lock       *lock;
1281         struct ldlm_res_id     *res_id;
1282         int                     is_resent;
1283         int                     ma_need = 0;
1284         int                     rc;
1285
1286         ENTRY;
1287
1288         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1289         LASSERT(ergo(is_resent,
1290                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1291
1292         LASSERT(parent != NULL);
1293
1294         lname = &info->mti_name;
1295         mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1296
1297         if (!info->mti_cross_ref) {
1298                 /*
1299                  * XXX: Check for anonymous name is for getattr by fid
1300                  * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1301                  * that is the name must contain at least one character and
1302                  * the terminating '\0'.
1303                  */
1304                 if (!lu_name_is_valid(lname)) {
1305                         reqbody = req_capsule_client_get(info->mti_pill,
1306                                                          &RMF_MDT_BODY);
1307                         if (unlikely(reqbody == NULL))
1308                                 RETURN(err_serious(-EFAULT));
1309
1310                         if (unlikely(!fid_is_sane(&reqbody->fid2)))
1311                                 RETURN(err_serious(-EINVAL));
1312
1313                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1314                                "ldlm_rep = %p\n",
1315                                PFID(mdt_object_fid(parent)),
1316                                PFID(&reqbody->fid2), ldlm_rep);
1317                 } else {
1318                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1319                                "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1320                                PNAME(lname), ldlm_rep);
1321                 }
1322         }
1323
1324         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1325
1326         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1327                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1328                                 &parent->mot_obj,
1329                                 "Parent doesn't exist!\n");
1330                 RETURN(-ESTALE);
1331         } else if (!info->mti_cross_ref) {
1332                 LASSERTF(!mdt_object_remote(parent),
1333                          "Parent "DFID" is on remote server\n",
1334                          PFID(mdt_object_fid(parent)));
1335         }
1336
1337         if (lu_name_is_valid(lname)) {
1338                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1339                 if (rc != 0) {
1340                         if (rc > 0)
1341                                 rc = 0;
1342                         RETURN(rc);
1343                 }
1344         }
1345
1346         if (info->mti_cross_ref) {
1347                 /* Only getattr on the child. Parent is on another node. */
1348                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1349                 child = parent;
1350                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1351                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1352
1353                 if (is_resent) {
1354                         /* Do not take lock for resent case. */
1355                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1356                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1357                                  lhc->mlh_reg_lh.cookie);
1358                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
1359                                                 &lock->l_resource->lr_name));
1360                         LDLM_LOCK_PUT(lock);
1361                         rc = 0;
1362                 } else {
1363                         mdt_lock_handle_init(lhc);
1364                         mdt_lock_reg_init(lhc, LCK_PR);
1365
1366                         /*
1367                          * Object's name is on another MDS, no lookup or layout
1368                          * lock is needed here but update lock is.
1369                          */
1370                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1371                                         MDS_INODELOCK_LAYOUT);
1372                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1373
1374                         rc = mdt_object_lock(info, child, lhc, child_bits,
1375                                              MDT_LOCAL_LOCK);
1376                 }
1377                 if (rc == 0) {
1378                         /* Finally, we can get attr for child. */
1379                         if (!mdt_object_exists(child)) {
1380                                 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1381                                                 &child->mot_obj,
1382                                              "remote object doesn't exist.\n");
1383                                 mdt_object_unlock(info, child, lhc, 1);
1384                                 RETURN(-ENOENT);
1385                         }
1386
1387                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
1388                                          BYPASS_CAPA);
1389                         rc = mdt_getattr_internal(info, child, 0);
1390                         if (unlikely(rc != 0))
1391                                 mdt_object_unlock(info, child, lhc, 1);
1392                 }
1393                 RETURN(rc);
1394         }
1395
1396         if (lu_name_is_valid(lname)) {
1397                 /* step 1: lock parent only if parent is a directory */
1398                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1399                         lhp = &info->mti_lh[MDT_LH_PARENT];
1400                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
1401                         rc = mdt_object_lock(info, parent, lhp,
1402                                              MDS_INODELOCK_UPDATE,
1403                                              MDT_LOCAL_LOCK);
1404                         if (unlikely(rc != 0))
1405                                 RETURN(rc);
1406                 }
1407
1408                 /* step 2: lookup child's fid by name */
1409                 fid_zero(child_fid);
1410                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1411                                 &info->mti_spec);
1412
1413                 if (rc != 0) {
1414                         if (rc == -ENOENT)
1415                                 mdt_set_disposition(info, ldlm_rep,
1416                                                     DISP_LOOKUP_NEG);
1417                         GOTO(out_parent, rc);
1418                 } else
1419                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1420         } else {
1421                 *child_fid = reqbody->fid2;
1422                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1423         }
1424
1425         /*
1426          *step 3: find the child object by fid & lock it.
1427          *        regardless if it is local or remote.
1428          *
1429          *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1430          *      set parent dir fid the same as child fid in getattr by fid case
1431          *      we should not lu_object_find() the object again, could lead
1432          *      to hung if there is a concurrent unlink destroyed the object.
1433          */
1434         if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1435                 mdt_object_get(info->mti_env, parent);
1436                 child = parent;
1437         } else {
1438                 child = mdt_object_find(info->mti_env, info->mti_mdt,
1439                                         child_fid);
1440         }
1441
1442         if (unlikely(IS_ERR(child)))
1443                 GOTO(out_parent, rc = PTR_ERR(child));
1444         if (is_resent) {
1445                 /* Do not take lock for resent case. */
1446                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1447                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1448                          lhc->mlh_reg_lh.cookie);
1449
1450                 res_id = &lock->l_resource->lr_name;
1451                 if (!fid_res_name_eq(mdt_object_fid(child),
1452                                      &lock->l_resource->lr_name)) {
1453                         LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1454                                                  &lock->l_resource->lr_name),
1455                                  "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1456                                  PLDLMRES(lock->l_resource),
1457                                  PFID(mdt_object_fid(parent)));
1458                         CWARN("Although resent, but still not get child lock"
1459                               "parent:"DFID" child:"DFID"\n",
1460                               PFID(mdt_object_fid(parent)),
1461                               PFID(mdt_object_fid(child)));
1462                         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1463                         LDLM_LOCK_PUT(lock);
1464                         GOTO(relock, 0);
1465                 }
1466                 LDLM_LOCK_PUT(lock);
1467                 rc = 0;
1468         } else {
1469                 bool try_layout = false;
1470
1471 relock:
1472                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1473                 mdt_lock_handle_init(lhc);
1474                 mdt_lock_reg_init(lhc, LCK_PR);
1475
1476                 if (!mdt_object_exists(child)) {
1477                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1478                                         &child->mot_obj,
1479                                         "Object doesn't exist!\n");
1480                         GOTO(out_child, rc = -ENOENT);
1481                 }
1482
1483                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1484                       mdt_object_exists(child) && !mdt_object_remote(child)) {
1485                         struct md_attr *ma = &info->mti_attr;
1486
1487                         ma->ma_valid = 0;
1488                         ma->ma_need = MA_INODE;
1489                         rc = mdt_attr_get_complex(info, child, ma);
1490                         if (unlikely(rc != 0))
1491                                 GOTO(out_child, rc);
1492
1493                         /* If the file has not been changed for some time, we
1494                          * return not only a LOOKUP lock, but also an UPDATE
1495                          * lock and this might save us RPC on later STAT. For
1496                          * directories, it also let negative dentry cache start
1497                          * working for this dir. */
1498                         if (ma->ma_valid & MA_INODE &&
1499                             ma->ma_attr.la_valid & LA_CTIME &&
1500                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1501                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1502                                 child_bits |= MDS_INODELOCK_UPDATE;
1503                 }
1504
1505                 /* layout lock must be granted in a best-effort way
1506                  * for IT operations */
1507                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1508                 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1509                     exp_connect_layout(info->mti_exp) &&
1510                     S_ISREG(lu_object_attr(&child->mot_obj)) &&
1511                     !mdt_object_remote(child) && ldlm_rep != NULL) {
1512                         /* try to grant layout lock for regular file. */
1513                         try_layout = true;
1514                 }
1515
1516                 rc = 0;
1517                 if (try_layout) {
1518                         child_bits |= MDS_INODELOCK_LAYOUT;
1519                         /* try layout lock, it may fail to be granted due to
1520                          * contention at LOOKUP or UPDATE */
1521                         if (!mdt_object_lock_try(info, child, lhc, child_bits,
1522                                                  MDT_CROSS_LOCK)) {
1523                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1524                                 LASSERT(child_bits != 0);
1525                                 rc = mdt_object_lock(info, child, lhc,
1526                                                 child_bits, MDT_CROSS_LOCK);
1527                         } else {
1528                                 ma_need |= MA_LOV;
1529                         }
1530                 } else {
1531                         rc = mdt_object_lock(info, child, lhc, child_bits,
1532                                                 MDT_CROSS_LOCK);
1533                 }
1534                 if (unlikely(rc != 0))
1535                         GOTO(out_child, rc);
1536         }
1537
1538         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1539         /* Get MA_SOM attributes if update lock is given. */
1540         if (lock &&
1541             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1542             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1543                 ma_need |= MA_SOM;
1544
1545         /* finally, we can get attr for child. */
1546         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1547         rc = mdt_getattr_internal(info, child, ma_need);
1548         if (unlikely(rc != 0)) {
1549                 mdt_object_unlock(info, child, lhc, 1);
1550         } else if (lock) {
1551                 /* Debugging code. */
1552                 res_id = &lock->l_resource->lr_name;
1553                 LDLM_DEBUG(lock, "Returning lock to client");
1554                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1555                                          &lock->l_resource->lr_name),
1556                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1557                          PLDLMRES(lock->l_resource),
1558                          PFID(mdt_object_fid(child)));
1559                 if (mdt_object_exists(child) && !mdt_object_remote(child))
1560                         mdt_pack_size2body(info, child);
1561         }
1562         if (lock)
1563                 LDLM_LOCK_PUT(lock);
1564
1565         EXIT;
1566 out_child:
1567         mdt_object_put(info->mti_env, child);
1568 out_parent:
1569         if (lhp)
1570                 mdt_object_unlock(info, parent, lhp, 1);
1571         return rc;
1572 }
1573
1574 /* normal handler: should release the child lock */
1575 int mdt_getattr_name(struct tgt_session_info *tsi)
1576 {
1577         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1578         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1579         struct mdt_body        *reqbody;
1580         struct mdt_body        *repbody;
1581         int rc, rc2;
1582         ENTRY;
1583
1584         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1585         LASSERT(reqbody != NULL);
1586         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1587         LASSERT(repbody != NULL);
1588
1589         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1590         repbody->eadatasize = 0;
1591         repbody->aclsize = 0;
1592
1593         rc = mdt_init_ucred(info, reqbody);
1594         if (unlikely(rc))
1595                 GOTO(out_shrink, rc);
1596
1597         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1598         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1599                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1600                 lhc->mlh_reg_lh.cookie = 0;
1601         }
1602         mdt_exit_ucred(info);
1603         EXIT;
1604 out_shrink:
1605         mdt_client_compatibility(info);
1606         rc2 = mdt_fix_reply(info);
1607         if (rc == 0)
1608                 rc = rc2;
1609         mdt_thread_info_fini(info);
1610         return rc;
1611 }
1612
1613 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1614                          void *karg, void *uarg);
1615
1616 int mdt_set_info(struct tgt_session_info *tsi)
1617 {
1618         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1619         char                    *key;
1620         void                    *val;
1621         int                      keylen, vallen, rc = 0;
1622
1623         ENTRY;
1624
1625         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1626         if (key == NULL) {
1627                 DEBUG_REQ(D_HA, req, "no set_info key");
1628                 RETURN(err_serious(-EFAULT));
1629         }
1630
1631         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1632                                       RCL_CLIENT);
1633
1634         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1635         if (val == NULL) {
1636                 DEBUG_REQ(D_HA, req, "no set_info val");
1637                 RETURN(err_serious(-EFAULT));
1638         }
1639
1640         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1641                                       RCL_CLIENT);
1642
1643         /* Swab any part of val you need to here */
1644         if (KEY_IS(KEY_READ_ONLY)) {
1645                 spin_lock(&req->rq_export->exp_lock);
1646                 if (*(__u32 *)val)
1647                         *exp_connect_flags_ptr(req->rq_export) |=
1648                                 OBD_CONNECT_RDONLY;
1649                 else
1650                         *exp_connect_flags_ptr(req->rq_export) &=
1651                                 ~OBD_CONNECT_RDONLY;
1652                 spin_unlock(&req->rq_export->exp_lock);
1653         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1654                 struct changelog_setinfo *cs = val;
1655
1656                 if (vallen != sizeof(*cs)) {
1657                         CERROR("%s: bad changelog_clear setinfo size %d\n",
1658                                tgt_name(tsi->tsi_tgt), vallen);
1659                         RETURN(-EINVAL);
1660                 }
1661                 if (ptlrpc_req_need_swab(req)) {
1662                         __swab64s(&cs->cs_recno);
1663                         __swab32s(&cs->cs_id);
1664                 }
1665
1666                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1667                                    vallen, val, NULL);
1668         } else {
1669                 RETURN(-EINVAL);
1670         }
1671         RETURN(rc);
1672 }
1673
1674 int mdt_readpage(struct tgt_session_info *tsi)
1675 {
1676         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
1677         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
1678         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
1679         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
1680         struct mdt_body         *repbody;
1681         int                      rc;
1682         int                      i;
1683
1684         ENTRY;
1685
1686         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1687                 RETURN(err_serious(-ENOMEM));
1688
1689         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1690         if (repbody == NULL || reqbody == NULL)
1691                 RETURN(err_serious(-EFAULT));
1692
1693         /*
1694          * prepare @rdpg before calling lower layers and transfer itself. Here
1695          * reqbody->size contains offset of where to start to read and
1696          * reqbody->nlink contains number bytes to read.
1697          */
1698         rdpg->rp_hash = reqbody->size;
1699         if (rdpg->rp_hash != reqbody->size) {
1700                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1701                        rdpg->rp_hash, reqbody->size);
1702                 RETURN(-EFAULT);
1703         }
1704
1705         rdpg->rp_attrs = reqbody->mode;
1706         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1707                 rdpg->rp_attrs |= LUDA_64BITHASH;
1708         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1709                                 exp_max_brw_size(tsi->tsi_exp));
1710         rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1711                           PAGE_CACHE_SHIFT;
1712         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1713         if (rdpg->rp_pages == NULL)
1714                 RETURN(-ENOMEM);
1715
1716         for (i = 0; i < rdpg->rp_npages; ++i) {
1717                 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1718                 if (rdpg->rp_pages[i] == NULL)
1719                         GOTO(free_rdpg, rc = -ENOMEM);
1720         }
1721
1722         /* call lower layers to fill allocated pages with directory data */
1723         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1724         if (rc < 0)
1725                 GOTO(free_rdpg, rc);
1726
1727         /* send pages to client */
1728         rc = tgt_sendpage(tsi, rdpg, rc);
1729
1730         EXIT;
1731 free_rdpg:
1732
1733         for (i = 0; i < rdpg->rp_npages; i++)
1734                 if (rdpg->rp_pages[i] != NULL)
1735                         __free_page(rdpg->rp_pages[i]);
1736         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1737
1738         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1739                 RETURN(0);
1740
1741         return rc;
1742 }
1743
1744 static int mdt_reint_internal(struct mdt_thread_info *info,
1745                               struct mdt_lock_handle *lhc,
1746                               __u32 op)
1747 {
1748         struct req_capsule      *pill = info->mti_pill;
1749         struct mdt_body         *repbody;
1750         int                      rc = 0, rc2;
1751
1752         ENTRY;
1753
1754         rc = mdt_reint_unpack(info, op);
1755         if (rc != 0) {
1756                 CERROR("Can't unpack reint, rc %d\n", rc);
1757                 RETURN(err_serious(rc));
1758         }
1759
1760         /* for replay (no_create) lmm is not needed, client has it already */
1761         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1762                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1763                                      info->mti_rr.rr_eadatalen);
1764
1765         /* llog cookies are always 0, the field is kept for compatibility */
1766         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1767                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1768
1769         rc = req_capsule_server_pack(pill);
1770         if (rc != 0) {
1771                 CERROR("Can't pack response, rc %d\n", rc);
1772                 RETURN(err_serious(rc));
1773         }
1774
1775         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1776                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1777                 LASSERT(repbody);
1778                 repbody->eadatasize = 0;
1779                 repbody->aclsize = 0;
1780         }
1781
1782         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1783
1784         /* for replay no cookkie / lmm need, because client have this already */
1785         if (info->mti_spec.no_create)
1786                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1787                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1788
1789         rc = mdt_init_ucred_reint(info);
1790         if (rc)
1791                 GOTO(out_shrink, rc);
1792
1793         rc = mdt_fix_attr_ucred(info, op);
1794         if (rc != 0)
1795                 GOTO(out_ucred, rc = err_serious(rc));
1796
1797         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1798                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1799                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1800                 GOTO(out_ucred, rc);
1801         }
1802         rc = mdt_reint_rec(info, lhc);
1803         EXIT;
1804 out_ucred:
1805         mdt_exit_ucred(info);
1806 out_shrink:
1807         mdt_client_compatibility(info);
1808         rc2 = mdt_fix_reply(info);
1809         if (rc == 0)
1810                 rc = rc2;
1811         return rc;
1812 }
1813
1814 static long mdt_reint_opcode(struct ptlrpc_request *req,
1815                              const struct req_format **fmt)
1816 {
1817         struct mdt_device       *mdt;
1818         struct mdt_rec_reint    *rec;
1819         long                     opc;
1820
1821         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1822         if (rec != NULL) {
1823                 opc = rec->rr_opcode;
1824                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1825                 if (opc < REINT_MAX && fmt[opc] != NULL)
1826                         req_capsule_extend(&req->rq_pill, fmt[opc]);
1827                 else {
1828                         mdt = mdt_exp2dev(req->rq_export);
1829                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1830                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
1831                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1832                         opc = err_serious(-EFAULT);
1833                 }
1834         } else {
1835                 opc = err_serious(-EFAULT);
1836         }
1837         return opc;
1838 }
1839
1840 int mdt_reint(struct tgt_session_info *tsi)
1841 {
1842         long opc;
1843         int  rc;
1844         static const struct req_format *reint_fmts[REINT_MAX] = {
1845                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1846                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1847                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1848                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1849                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1850                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1851                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1852                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
1853                 [REINT_MIGRATE]  = &RQF_MDS_REINT_RENAME
1854         };
1855
1856         ENTRY;
1857
1858         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1859         if (opc >= 0) {
1860                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1861                 /*
1862                  * No lock possible here from client to pass it to reint code
1863                  * path.
1864                  */
1865                 rc = mdt_reint_internal(info, NULL, opc);
1866                 mdt_thread_info_fini(info);
1867         } else {
1868                 rc = opc;
1869         }
1870
1871         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1872         RETURN(rc);
1873 }
1874
1875 /* this should sync the whole device */
1876 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1877 {
1878         struct dt_device *dt = mdt->mdt_bottom;
1879         int rc;
1880         ENTRY;
1881
1882         rc = dt->dd_ops->dt_sync(env, dt);
1883         RETURN(rc);
1884 }
1885
1886 /* this should sync this object */
1887 static int mdt_object_sync(struct mdt_thread_info *info)
1888 {
1889         struct md_object *next;
1890         int rc;
1891         ENTRY;
1892
1893         if (!mdt_object_exists(info->mti_object)) {
1894                 CWARN("Non existing object  "DFID"!\n",
1895                       PFID(mdt_object_fid(info->mti_object)));
1896                 RETURN(-ESTALE);
1897         }
1898         next = mdt_object_child(info->mti_object);
1899         rc = mo_object_sync(info->mti_env, next);
1900
1901         RETURN(rc);
1902 }
1903
1904 int mdt_sync(struct tgt_session_info *tsi)
1905 {
1906         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1907         struct req_capsule      *pill = tsi->tsi_pill;
1908         struct mdt_body         *body;
1909         int                      rc;
1910
1911         ENTRY;
1912
1913         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1914                 RETURN(err_serious(-ENOMEM));
1915
1916         if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1917                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1918         } else {
1919                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1920
1921                 /* sync an object */
1922                 rc = mdt_object_sync(info);
1923                 if (rc == 0) {
1924                         const struct lu_fid *fid;
1925                         struct lu_attr *la = &info->mti_attr.ma_attr;
1926
1927                         info->mti_attr.ma_need = MA_INODE;
1928                         info->mti_attr.ma_valid = 0;
1929                         rc = mdt_attr_get_complex(info, info->mti_object,
1930                                                   &info->mti_attr);
1931                         if (rc == 0) {
1932                                 body = req_capsule_server_get(pill,
1933                                                               &RMF_MDT_BODY);
1934                                 fid = mdt_object_fid(info->mti_object);
1935                                 mdt_pack_attr2body(info, body, la, fid);
1936                         }
1937                 }
1938                 mdt_thread_info_fini(info);
1939         }
1940         if (rc == 0)
1941                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1942
1943         RETURN(rc);
1944 }
1945
1946 /*
1947  * Handle quota control requests to consult current usage/limit, but also
1948  * to configure quota enforcement
1949  */
1950 int mdt_quotactl(struct tgt_session_info *tsi)
1951 {
1952         struct obd_export       *exp  = tsi->tsi_exp;
1953         struct req_capsule      *pill = tsi->tsi_pill;
1954         struct obd_quotactl     *oqctl, *repoqc;
1955         int                      id, rc;
1956         struct mdt_device       *mdt = mdt_exp2dev(exp);
1957         struct lu_device        *qmt = mdt->mdt_qmt_dev;
1958         ENTRY;
1959
1960         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1961         if (oqctl == NULL)
1962                 RETURN(err_serious(-EPROTO));
1963
1964         rc = req_capsule_server_pack(pill);
1965         if (rc)
1966                 RETURN(err_serious(rc));
1967
1968         switch (oqctl->qc_cmd) {
1969         case Q_QUOTACHECK:
1970         case LUSTRE_Q_INVALIDATE:
1971         case LUSTRE_Q_FINVALIDATE:
1972         case Q_QUOTAON:
1973         case Q_QUOTAOFF:
1974         case Q_INITQUOTA:
1975                 /* deprecated, not used any more */
1976                 RETURN(-EOPNOTSUPP);
1977                 /* master quotactl */
1978         case Q_GETINFO:
1979         case Q_SETINFO:
1980         case Q_SETQUOTA:
1981         case Q_GETQUOTA:
1982                 if (qmt == NULL)
1983                         RETURN(-EOPNOTSUPP);
1984                 /* slave quotactl */
1985         case Q_GETOINFO:
1986         case Q_GETOQUOTA:
1987                 break;
1988         default:
1989                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1990                 RETURN(-EFAULT);
1991         }
1992
1993         /* map uid/gid for remote client */
1994         id = oqctl->qc_id;
1995         if (exp_connect_rmtclient(exp)) {
1996                 struct lustre_idmap_table *idmap;
1997
1998                 idmap = exp->exp_mdt_data.med_idmap;
1999
2000                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2001                              oqctl->qc_cmd != Q_GETINFO))
2002                         RETURN(-EPERM);
2003
2004                 if (oqctl->qc_type == USRQUOTA)
2005                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2006                                                      oqctl->qc_id);
2007                 else if (oqctl->qc_type == GRPQUOTA)
2008                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2009                                                      oqctl->qc_id);
2010                 else
2011                         RETURN(-EINVAL);
2012
2013                 if (id == CFS_IDMAP_NOTFOUND) {
2014                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2015                         RETURN(-EACCES);
2016                 }
2017         }
2018
2019         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2020         if (repoqc == NULL)
2021                 RETURN(err_serious(-EFAULT));
2022
2023         if (oqctl->qc_id != id)
2024                 swap(oqctl->qc_id, id);
2025
2026         switch (oqctl->qc_cmd) {
2027
2028         case Q_GETINFO:
2029         case Q_SETINFO:
2030         case Q_SETQUOTA:
2031         case Q_GETQUOTA:
2032                 /* forward quotactl request to QMT */
2033                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2034                 break;
2035
2036         case Q_GETOINFO:
2037         case Q_GETOQUOTA:
2038                 /* slave quotactl */
2039                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2040                                    oqctl);
2041                 break;
2042
2043         default:
2044                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2045                 RETURN(-EFAULT);
2046         }
2047
2048         if (oqctl->qc_id != id)
2049                 swap(oqctl->qc_id, id);
2050
2051         *repoqc = *oqctl;
2052         RETURN(rc);
2053 }
2054
2055 /** clone llog ctxt from child (mdd)
2056  * This allows remote llog (replicator) access.
2057  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2058  * context was originally set up, or we can handle them directly.
2059  * I choose the latter, but that means I need any llog
2060  * contexts set up by child to be accessable by the mdt.  So we clone the
2061  * context into our context list here.
2062  */
2063 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2064                                int idx)
2065 {
2066         struct md_device  *next = mdt->mdt_child;
2067         struct llog_ctxt *ctxt;
2068         int rc;
2069
2070         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2071                 return 0;
2072
2073         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2074         if (rc || ctxt == NULL) {
2075                 return 0;
2076         }
2077
2078         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2079         if (rc)
2080                 CERROR("Can't set mdt ctxt %d\n", rc);
2081
2082         return rc;
2083 }
2084
2085 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2086                                  struct mdt_device *mdt, int idx)
2087 {
2088         struct llog_ctxt *ctxt;
2089
2090         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2091         if (ctxt == NULL)
2092                 return 0;
2093         /* Put once for the get we just did, and once for the clone */
2094         llog_ctxt_put(ctxt);
2095         llog_ctxt_put(ctxt);
2096         return 0;
2097 }
2098
2099 /*
2100  * sec context handlers
2101  */
2102 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2103 {
2104         int rc;
2105
2106         rc = mdt_handle_idmap(tsi);
2107         if (unlikely(rc)) {
2108                 struct ptlrpc_request   *req = tgt_ses_req(tsi);
2109                 __u32                    opc;
2110
2111                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2112                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2113                         sptlrpc_svc_ctx_invalidate(req);
2114         }
2115
2116         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2117
2118         return rc;
2119 }
2120
2121 /*
2122  * quota request handlers
2123  */
2124 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2125 {
2126         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
2127         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2128         int                      rc;
2129         ENTRY;
2130
2131         if (qmt == NULL)
2132                 RETURN(err_serious(-EOPNOTSUPP));
2133
2134         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2135         RETURN(rc);
2136 }
2137
2138 struct mdt_object *mdt_object_new(const struct lu_env *env,
2139                                   struct mdt_device *d,
2140                                   const struct lu_fid *f)
2141 {
2142         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2143         struct lu_object *o;
2144         struct mdt_object *m;
2145         ENTRY;
2146
2147         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2148         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2149         if (unlikely(IS_ERR(o)))
2150                 m = (struct mdt_object *)o;
2151         else
2152                 m = mdt_obj(o);
2153         RETURN(m);
2154 }
2155
2156 struct mdt_object *mdt_object_find(const struct lu_env *env,
2157                                    struct mdt_device *d,
2158                                    const struct lu_fid *f)
2159 {
2160         struct lu_object *o;
2161         struct mdt_object *m;
2162         ENTRY;
2163
2164         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2165         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2166         if (unlikely(IS_ERR(o)))
2167                 m = (struct mdt_object *)o;
2168         else
2169                 m = mdt_obj(o);
2170
2171         RETURN(m);
2172 }
2173
2174 /**
2175  * Asyncronous commit for mdt device.
2176  *
2177  * Pass asynchonous commit call down the MDS stack.
2178  *
2179  * \param env environment
2180  * \param mdt the mdt device
2181  */
2182 static void mdt_device_commit_async(const struct lu_env *env,
2183                                     struct mdt_device *mdt)
2184 {
2185         struct dt_device *dt = mdt->mdt_bottom;
2186         int rc;
2187
2188         rc = dt->dd_ops->dt_commit_async(env, dt);
2189         if (unlikely(rc != 0))
2190                 CWARN("async commit start failed with rc = %d", rc);
2191 }
2192
2193 /**
2194  * Mark the lock as "synchonous".
2195  *
2196  * Mark the lock to deffer transaction commit to the unlock time.
2197  *
2198  * \param lock the lock to mark as "synchonous"
2199  *
2200  * \see mdt_is_lock_sync
2201  * \see mdt_save_lock
2202  */
2203 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2204 {
2205         lock->l_ast_data = (void*)1;
2206 }
2207
2208 /**
2209  * Check whehter the lock "synchonous" or not.
2210  *
2211  * \param lock the lock to check
2212  * \retval 1 the lock is "synchonous"
2213  * \retval 0 the lock isn't "synchronous"
2214  *
2215  * \see mdt_set_lock_sync
2216  * \see mdt_save_lock
2217  */
2218 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2219 {
2220         return lock->l_ast_data != NULL;
2221 }
2222
2223 /**
2224  * Blocking AST for mdt locks.
2225  *
2226  * Starts transaction commit if in case of COS lock conflict or
2227  * deffers such a commit to the mdt_save_lock.
2228  *
2229  * \param lock the lock which blocks a request or cancelling lock
2230  * \param desc unused
2231  * \param data unused
2232  * \param flag indicates whether this cancelling or blocking callback
2233  * \retval 0
2234  * \see ldlm_blocking_ast_nocheck
2235  */
2236 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2237                      void *data, int flag)
2238 {
2239         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2240         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2241         int rc;
2242         ENTRY;
2243
2244         if (flag == LDLM_CB_CANCELING)
2245                 RETURN(0);
2246         lock_res_and_lock(lock);
2247         if (lock->l_blocking_ast != mdt_blocking_ast) {
2248                 unlock_res_and_lock(lock);
2249                 RETURN(0);
2250         }
2251         if (mdt_cos_is_enabled(mdt) &&
2252             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2253             lock->l_blocking_lock != NULL &&
2254             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2255                 mdt_set_lock_sync(lock);
2256         }
2257         rc = ldlm_blocking_ast_nocheck(lock);
2258
2259         /* There is no lock conflict if l_blocking_lock == NULL,
2260          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2261          * when the last reference to a local lock was released */
2262         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2263                 struct lu_env env;
2264
2265                 rc = lu_env_init(&env, LCT_LOCAL);
2266                 if (unlikely(rc != 0))
2267                         CWARN("lu_env initialization failed with rc = %d,"
2268                               "cannot start asynchronous commit\n", rc);
2269                 else
2270                         mdt_device_commit_async(&env, mdt);
2271                 lu_env_fini(&env);
2272         }
2273         RETURN(rc);
2274 }
2275
2276 /* Used for cross-MDT lock */
2277 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2278                             void *data, int flag)
2279 {
2280         struct lustre_handle lockh;
2281         int               rc;
2282
2283         switch (flag) {
2284         case LDLM_CB_BLOCKING:
2285                 ldlm_lock2handle(lock, &lockh);
2286                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2287                 if (rc < 0) {
2288                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2289                         RETURN(rc);
2290                 }
2291                 break;
2292         case LDLM_CB_CANCELING:
2293                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2294                 break;
2295         default:
2296                 LBUG();
2297         }
2298         RETURN(0);
2299 }
2300
2301 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2302                            struct mdt_object *o, struct lustre_handle *lh,
2303                            ldlm_mode_t mode, __u64 ibits)
2304 {
2305         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2306         ldlm_policy_data_t *policy = &mti->mti_policy;
2307         int rc = 0;
2308         ENTRY;
2309
2310         LASSERT(mdt_object_remote(o));
2311
2312         LASSERT(ibits & MDS_INODELOCK_UPDATE);
2313
2314         memset(einfo, 0, sizeof(*einfo));
2315         einfo->ei_type = LDLM_IBITS;
2316         einfo->ei_mode = mode;
2317         einfo->ei_cb_bl = mdt_remote_blocking_ast;
2318         einfo->ei_cb_cp = ldlm_completion_ast;
2319         einfo->ei_enq_slave = 0;
2320
2321         memset(policy, 0, sizeof(*policy));
2322         policy->l_inodebits.bits = ibits;
2323
2324         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2325                             policy);
2326         RETURN(rc);
2327 }
2328
2329 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2330                             struct mdt_lock_handle *lh, __u64 ibits,
2331                             bool nonblock, int locality)
2332 {
2333         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2334         ldlm_policy_data_t *policy = &info->mti_policy;
2335         struct ldlm_res_id *res_id = &info->mti_res_id;
2336         __u64 dlmflags;
2337         int rc;
2338         ENTRY;
2339
2340         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2341         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2342         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2343         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2344
2345         if (mdt_object_remote(o)) {
2346                 if (locality == MDT_CROSS_LOCK) {
2347                         ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2348                                    MDS_INODELOCK_LAYOUT);
2349                         ibits |= MDS_INODELOCK_LOOKUP;
2350                 } else {
2351                         LASSERTF(!(ibits &
2352                                  (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2353                                   MDS_INODELOCK_LAYOUT)),
2354                                 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2355                                 mdt_obd_name(info->mti_mdt), ibits,
2356                                 PFID(mdt_object_fid(o)));
2357                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2358                 }
2359                 /* No PDO lock on remote object */
2360                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2361         }
2362
2363         if (lh->mlh_type == MDT_PDO_LOCK) {
2364                 /* check for exists after object is locked */
2365                 if (mdt_object_exists(o) == 0) {
2366                         /* Non-existent object shouldn't have PDO lock */
2367                         RETURN(-ESTALE);
2368                 } else {
2369                         /* Non-dir object shouldn't have PDO lock */
2370                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2371                                 RETURN(-ENOTDIR);
2372                 }
2373         }
2374
2375         memset(policy, 0, sizeof(*policy));
2376         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2377
2378         dlmflags = LDLM_FL_ATOMIC_CB;
2379         if (nonblock)
2380                 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2381
2382         /*
2383          * Take PDO lock on whole directory and build correct @res_id for lock
2384          * on part of directory.
2385          */
2386         if (lh->mlh_pdo_hash != 0) {
2387                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2388                 mdt_lock_pdo_mode(info, o, lh);
2389                 if (lh->mlh_pdo_mode != LCK_NL) {
2390                         /*
2391                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2392                          * is never going to be sent to client and we do not
2393                          * want it slowed down due to possible cancels.
2394                          */
2395                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2396                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2397                                           policy, res_id, dlmflags,
2398                                           &info->mti_exp->exp_handle.h_cookie);
2399                         if (unlikely(rc))
2400                                 RETURN(rc);
2401                 }
2402
2403                 /*
2404                  * Finish res_id initializing by name hash marking part of
2405                  * directory which is taking modification.
2406                  */
2407                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2408         }
2409
2410         policy->l_inodebits.bits = ibits;
2411
2412         /*
2413          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2414          * going to be sent to client. If it is - mdt_intent_policy() path will
2415          * fix it up and turn FL_LOCAL flag off.
2416          */
2417         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2418                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2419                           &info->mti_exp->exp_handle.h_cookie);
2420         if (rc)
2421                 mdt_object_unlock(info, o, lh, 1);
2422         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2423                  lh->mlh_pdo_hash != 0 &&
2424                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2425                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2426         }
2427
2428         RETURN(rc);
2429 }
2430
2431 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2432                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2433 {
2434         return mdt_object_lock0(info, o, lh, ibits, false, locality);
2435 }
2436
2437 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2438                         struct mdt_lock_handle *lh, __u64 ibits, int locality)
2439 {
2440         struct mdt_lock_handle tmp = *lh;
2441         int rc;
2442
2443         rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2444         if (rc == 0)
2445                 *lh = tmp;
2446
2447         return rc == 0;
2448 }
2449
2450 /**
2451  * Save a lock within request object.
2452  *
2453  * Keep the lock referenced until whether client ACK or transaction
2454  * commit happens or release the lock immediately depending on input
2455  * parameters. If COS is ON, a write lock is converted to COS lock
2456  * before saving.
2457  *
2458  * \param info thead info object
2459  * \param h lock handle
2460  * \param mode lock mode
2461  * \param decref force immediate lock releasing
2462  */
2463 static
2464 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2465                    ldlm_mode_t mode, int decref)
2466 {
2467         ENTRY;
2468
2469         if (lustre_handle_is_used(h)) {
2470                 if (decref || !info->mti_has_trans ||
2471                     !(mode & (LCK_PW | LCK_EX))){
2472                         mdt_fid_unlock(h, mode);
2473                 } else {
2474                         struct mdt_device *mdt = info->mti_mdt;
2475                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2476                         struct ptlrpc_request *req = mdt_info_req(info);
2477                         int no_ack = 0;
2478
2479                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2480                                  h->cookie);
2481                         /* there is no request if mdt_object_unlock() is called
2482                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2483                         if (likely(req != NULL)) {
2484                                 CDEBUG(D_HA, "request = %p reply state = %p"
2485                                        " transno = "LPD64"\n", req,
2486                                        req->rq_reply_state, req->rq_transno);
2487                                 if (mdt_cos_is_enabled(mdt)) {
2488                                         no_ack = 1;
2489                                         ldlm_lock_downgrade(lock, LCK_COS);
2490                                         mode = LCK_COS;
2491                                 }
2492                                 ptlrpc_save_lock(req, h, mode, no_ack);
2493                         } else {
2494                                 ldlm_lock_decref(h, mode);
2495                         }
2496                         if (mdt_is_lock_sync(lock)) {
2497                                 CDEBUG(D_HA, "found sync-lock,"
2498                                        " async commit started\n");
2499                                 mdt_device_commit_async(info->mti_env,
2500                                                         mdt);
2501                         }
2502                         LDLM_LOCK_PUT(lock);
2503                 }
2504                 h->cookie = 0ull;
2505         }
2506
2507         EXIT;
2508 }
2509
2510 /**
2511  * Unlock mdt object.
2512  *
2513  * Immeditely release the regular lock and the PDO lock or save the
2514  * lock in reqeuest and keep them referenced until client ACK or
2515  * transaction commit.
2516  *
2517  * \param info thread info object
2518  * \param o mdt object
2519  * \param lh mdt lock handle referencing regular and PDO locks
2520  * \param decref force immediate lock releasing
2521  */
2522 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2523                        struct mdt_lock_handle *lh, int decref)
2524 {
2525         ENTRY;
2526
2527         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2528         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2529
2530         if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2531                 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2532
2533         EXIT;
2534 }
2535
2536 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2537                                         const struct lu_fid *f,
2538                                         struct mdt_lock_handle *lh,
2539                                         __u64 ibits)
2540 {
2541         struct mdt_object *o;
2542
2543         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2544         if (!IS_ERR(o)) {
2545                 int rc;
2546
2547                 rc = mdt_object_lock(info, o, lh, ibits,
2548                                      MDT_LOCAL_LOCK);
2549                 if (rc != 0) {
2550                         mdt_object_put(info->mti_env, o);
2551                         o = ERR_PTR(rc);
2552                 }
2553         }
2554         return o;
2555 }
2556
2557 void mdt_object_unlock_put(struct mdt_thread_info * info,
2558                            struct mdt_object * o,
2559                            struct mdt_lock_handle *lh,
2560                            int decref)
2561 {
2562         mdt_object_unlock(info, o, lh, decref);
2563         mdt_object_put(info->mti_env, o);
2564 }
2565
2566 /*
2567  * Generic code handling requests that have struct mdt_body passed in:
2568  *
2569  *  - extract mdt_body from request and save it in @info, if present;
2570  *
2571  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2572  *  @info;
2573  *
2574  *  - if HABEO_CORPUS flag is set for this request type check whether object
2575  *  actually exists on storage (lu_object_exists()).
2576  *
2577  */
2578 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2579 {
2580         const struct mdt_body    *body;
2581         struct mdt_object        *obj;
2582         const struct lu_env      *env;
2583         struct req_capsule       *pill;
2584         int                       rc;
2585         ENTRY;
2586
2587         env = info->mti_env;
2588         pill = info->mti_pill;
2589
2590         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2591         if (body == NULL)
2592                 RETURN(-EFAULT);
2593
2594         if (!(body->valid & OBD_MD_FLID))
2595                 RETURN(0);
2596
2597         if (!fid_is_sane(&body->fid1)) {
2598                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2599                 RETURN(-EINVAL);
2600         }
2601
2602         /*
2603          * Do not get size or any capa fields before we check that request
2604          * contains capa actually. There are some requests which do not, for
2605          * instance MDS_IS_SUBDIR.
2606          */
2607         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2608             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2609                 mdt_set_capainfo(info, 0, &body->fid1,
2610                                  req_capsule_client_get(pill, &RMF_CAPA1));
2611
2612         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2613         if (!IS_ERR(obj)) {
2614                 if ((flags & HABEO_CORPUS) &&
2615                     !mdt_object_exists(obj)) {
2616                         mdt_object_put(env, obj);
2617                         /* for capability renew ENOENT will be handled in
2618                          * mdt_renew_capa */
2619                         if (body->valid & OBD_MD_FLOSSCAPA)
2620                                 rc = 0;
2621                         else
2622                                 rc = -ENOENT;
2623                 } else {
2624                         info->mti_object = obj;
2625                         rc = 0;
2626                 }
2627         } else
2628                 rc = PTR_ERR(obj);
2629
2630         RETURN(rc);
2631 }
2632
2633 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2634 {
2635         struct req_capsule *pill = info->mti_pill;
2636         int rc;
2637         ENTRY;
2638
2639         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2640                 rc = mdt_body_unpack(info, flags);
2641         else
2642                 rc = 0;
2643
2644         if (rc == 0 && (flags & HABEO_REFERO)) {
2645                 /* Pack reply. */
2646                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2647                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2648                                              info->mti_body->eadatasize);
2649                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2650                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2651                                              RCL_SERVER, 0);
2652
2653                 rc = req_capsule_server_pack(pill);
2654         }
2655         RETURN(rc);
2656 }
2657
2658 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2659 {
2660         struct md_device *next = m->mdt_child;
2661
2662         return next->md_ops->mdo_init_capa_ctxt(env, next,
2663                                                 m->mdt_lut.lut_mds_capa,
2664                                                 m->mdt_capa_timeout,
2665                                                 m->mdt_capa_alg,
2666                                                 m->mdt_capa_keys);
2667 }
2668
2669 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2670 {
2671         lh->mlh_type = MDT_NUL_LOCK;
2672         lh->mlh_reg_lh.cookie = 0ull;
2673         lh->mlh_reg_mode = LCK_MINMODE;
2674         lh->mlh_pdo_lh.cookie = 0ull;
2675         lh->mlh_pdo_mode = LCK_MINMODE;
2676         lh->mlh_rreg_lh.cookie = 0ull;
2677         lh->mlh_rreg_mode = LCK_MINMODE;
2678 }
2679
2680 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2681 {
2682         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2683         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2684 }
2685
2686 /*
2687  * Initialize fields of struct mdt_thread_info. Other fields are left in
2688  * uninitialized state, because it's too expensive to zero out whole
2689  * mdt_thread_info (> 1K) on each request arrival.
2690  */
2691 void mdt_thread_info_init(struct ptlrpc_request *req,
2692                           struct mdt_thread_info *info)
2693 {
2694         int i;
2695
2696         info->mti_pill = &req->rq_pill;
2697
2698         /* lock handle */
2699         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2700                 mdt_lock_handle_init(&info->mti_lh[i]);
2701
2702         /* mdt device: it can be NULL while CONNECT */
2703         if (req->rq_export) {
2704                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2705                 info->mti_exp = req->rq_export;
2706         } else
2707                 info->mti_mdt = NULL;
2708         info->mti_env = req->rq_svc_thread->t_env;
2709         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2710
2711         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2712         info->mti_big_buf = LU_BUF_NULL;
2713         info->mti_body = NULL;
2714         info->mti_object = NULL;
2715         info->mti_dlm_req = NULL;
2716         info->mti_has_trans = 0;
2717         info->mti_cross_ref = 0;
2718         info->mti_opdata = 0;
2719         info->mti_big_lmm_used = 0;
2720
2721         info->mti_spec.no_create = 0;
2722         info->mti_spec.sp_rm_entry = 0;
2723
2724         info->mti_spec.u.sp_ea.eadata = NULL;
2725         info->mti_spec.u.sp_ea.eadatalen = 0;
2726 }
2727
2728 void mdt_thread_info_fini(struct mdt_thread_info *info)
2729 {
2730         int i;
2731
2732         if (info->mti_object != NULL) {
2733                 mdt_object_put(info->mti_env, info->mti_object);
2734                 info->mti_object = NULL;
2735         }
2736
2737         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2738                 mdt_lock_handle_fini(&info->mti_lh[i]);
2739         info->mti_env = NULL;
2740         info->mti_pill = NULL;
2741         info->mti_exp = NULL;
2742
2743         if (unlikely(info->mti_big_buf.lb_buf != NULL))
2744                 lu_buf_free(&info->mti_big_buf);
2745 }
2746
2747 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2748 {
2749         struct mdt_thread_info  *mti;
2750         struct lustre_capa      *lc;
2751
2752         mti = mdt_th_info(tsi->tsi_env);
2753         LASSERT(mti != NULL);
2754
2755         mdt_thread_info_init(tgt_ses_req(tsi), mti);
2756         if (tsi->tsi_corpus != NULL) {
2757                 struct req_capsule *pill = tsi->tsi_pill;
2758
2759                 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2760                 lu_object_get(tsi->tsi_corpus);
2761
2762                 /*
2763                  * XXX: must be part of tgt_mdt_body_unpack but moved here
2764                  * due to mdt_set_capainfo().
2765                  */
2766                 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2767                     req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2768                         lc = req_capsule_client_get(pill, &RMF_CAPA1);
2769                         mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2770                 }
2771         }
2772         mti->mti_body = tsi->tsi_mdt_body;
2773         mti->mti_dlm_req = tsi->tsi_dlm_req;
2774
2775         return mti;
2776 }
2777
2778 int mdt_tgt_connect(struct tgt_session_info *tsi)
2779 {
2780         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2781         int                      rc;
2782
2783         ENTRY;
2784
2785         rc = tgt_connect(tsi);
2786         if (rc != 0)
2787                 RETURN(rc);
2788
2789         rc = mdt_init_idmap(tsi);
2790         if (rc != 0)
2791                 GOTO(err, rc);
2792         RETURN(0);
2793 err:
2794         obd_disconnect(class_export_get(req->rq_export));
2795         return rc;
2796 }
2797
2798 enum mdt_it_code {
2799         MDT_IT_OPEN,
2800         MDT_IT_OCREAT,
2801         MDT_IT_CREATE,
2802         MDT_IT_GETATTR,
2803         MDT_IT_READDIR,
2804         MDT_IT_LOOKUP,
2805         MDT_IT_UNLINK,
2806         MDT_IT_TRUNC,
2807         MDT_IT_GETXATTR,
2808         MDT_IT_LAYOUT,
2809         MDT_IT_QUOTA,
2810         MDT_IT_NR
2811 };
2812
2813 static int mdt_intent_getattr(enum mdt_it_code opcode,
2814                               struct mdt_thread_info *info,
2815                               struct ldlm_lock **,
2816                               __u64);
2817
2818 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2819                                 struct mdt_thread_info *info,
2820                                 struct ldlm_lock **lockp,
2821                                 __u64 flags);
2822
2823 static int mdt_intent_layout(enum mdt_it_code opcode,
2824                              struct mdt_thread_info *info,
2825                              struct ldlm_lock **,
2826                              __u64);
2827 static int mdt_intent_reint(enum mdt_it_code opcode,
2828                             struct mdt_thread_info *info,
2829                             struct ldlm_lock **,
2830                             __u64);
2831
2832 static struct mdt_it_flavor {
2833         const struct req_format *it_fmt;
2834         __u32                    it_flags;
2835         int                    (*it_act)(enum mdt_it_code ,
2836                                          struct mdt_thread_info *,
2837                                          struct ldlm_lock **,
2838                                          __u64);
2839         long                     it_reint;
2840 } mdt_it_flavor[] = {
2841         [MDT_IT_OPEN]     = {
2842                 .it_fmt   = &RQF_LDLM_INTENT,
2843                 /*.it_flags = HABEO_REFERO,*/
2844                 .it_flags = 0,
2845                 .it_act   = mdt_intent_reint,
2846                 .it_reint = REINT_OPEN
2847         },
2848         [MDT_IT_OCREAT]   = {
2849                 .it_fmt   = &RQF_LDLM_INTENT,
2850                 /*
2851                  * OCREAT is not a MUTABOR request as if the file
2852                  * already exists.
2853                  * We do the extra check of OBD_CONNECT_RDONLY in
2854                  * mdt_reint_open() when we really need to create
2855                  * the object.
2856                  */
2857                 .it_flags = 0,
2858                 .it_act   = mdt_intent_reint,
2859                 .it_reint = REINT_OPEN
2860         },
2861         [MDT_IT_CREATE]   = {
2862                 .it_fmt   = &RQF_LDLM_INTENT,
2863                 .it_flags = MUTABOR,
2864                 .it_act   = mdt_intent_reint,
2865                 .it_reint = REINT_CREATE
2866         },
2867         [MDT_IT_GETATTR]  = {
2868                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2869                 .it_flags = HABEO_REFERO,
2870                 .it_act   = mdt_intent_getattr
2871         },
2872         [MDT_IT_READDIR]  = {
2873                 .it_fmt   = NULL,
2874                 .it_flags = 0,
2875                 .it_act   = NULL
2876         },
2877         [MDT_IT_LOOKUP]   = {
2878                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2879                 .it_flags = HABEO_REFERO,
2880                 .it_act   = mdt_intent_getattr
2881         },
2882         [MDT_IT_UNLINK]   = {
2883                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2884                 .it_flags = MUTABOR,
2885                 .it_act   = NULL,
2886                 .it_reint = REINT_UNLINK
2887         },
2888         [MDT_IT_TRUNC]    = {
2889                 .it_fmt   = NULL,
2890                 .it_flags = MUTABOR,
2891                 .it_act   = NULL
2892         },
2893         [MDT_IT_GETXATTR] = {
2894                 .it_fmt   = &RQF_LDLM_INTENT_GETXATTR,
2895                 .it_flags = HABEO_CORPUS,
2896                 .it_act   = mdt_intent_getxattr
2897         },
2898         [MDT_IT_LAYOUT] = {
2899                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
2900                 .it_flags = 0,
2901                 .it_act   = mdt_intent_layout
2902         }
2903 };
2904
2905 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2906                             struct ldlm_lock **lockp,
2907                             struct ldlm_lock *new_lock,
2908                             struct mdt_lock_handle *lh,
2909                             __u64 flags)
2910 {
2911         struct ptlrpc_request  *req = mdt_info_req(info);
2912         struct ldlm_lock       *lock = *lockp;
2913
2914         /*
2915          * Get new lock only for cases when possible resent did not find any
2916          * lock.
2917          */
2918         if (new_lock == NULL)
2919                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2920
2921         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2922                 lh->mlh_reg_lh.cookie = 0;
2923                 RETURN(0);
2924         }
2925
2926         LASSERTF(new_lock != NULL,
2927                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2928
2929         /*
2930          * If we've already given this lock to a client once, then we should
2931          * have no readers or writers.  Otherwise, we should have one reader
2932          * _or_ writer ref (which will be zeroed below) before returning the
2933          * lock to a client.
2934          */
2935         if (new_lock->l_export == req->rq_export) {
2936                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2937         } else {
2938                 LASSERT(new_lock->l_export == NULL);
2939                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2940         }
2941
2942         *lockp = new_lock;
2943
2944         if (new_lock->l_export == req->rq_export) {
2945                 /*
2946                  * Already gave this to the client, which means that we
2947                  * reconstructed a reply.
2948                  */
2949                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2950                         MSG_RESENT);
2951                 lh->mlh_reg_lh.cookie = 0;
2952                 RETURN(ELDLM_LOCK_REPLACED);
2953         }
2954
2955         /*
2956          * Fixup the lock to be given to the client.
2957          */
2958         lock_res_and_lock(new_lock);
2959         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2960          * possible blocking AST. */
2961         while (new_lock->l_readers > 0) {
2962                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2963                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2964                 new_lock->l_readers--;
2965         }
2966         while (new_lock->l_writers > 0) {
2967                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2968                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2969                 new_lock->l_writers--;
2970         }
2971
2972         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2973         new_lock->l_blocking_ast = lock->l_blocking_ast;
2974         new_lock->l_completion_ast = lock->l_completion_ast;
2975         new_lock->l_remote_handle = lock->l_remote_handle;
2976         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2977
2978         unlock_res_and_lock(new_lock);
2979
2980         cfs_hash_add(new_lock->l_export->exp_lock_hash,
2981                      &new_lock->l_remote_handle,
2982                      &new_lock->l_exp_hash);
2983
2984         LDLM_LOCK_RELEASE(new_lock);
2985         lh->mlh_reg_lh.cookie = 0;
2986
2987         RETURN(ELDLM_LOCK_REPLACED);
2988 }
2989
2990 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2991                                     struct ldlm_lock *new_lock,
2992                                     struct ldlm_lock **old_lock,
2993                                     struct mdt_lock_handle *lh,
2994                                     enum mdt_it_code opcode)
2995 {
2996         struct ptlrpc_request  *req = mdt_info_req(info);
2997         struct obd_export      *exp = req->rq_export;
2998         struct lustre_handle    remote_hdl;
2999         struct ldlm_request    *dlmreq;
3000         struct ldlm_lock       *lock;
3001
3002         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3003                 return;
3004
3005         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3006         remote_hdl = dlmreq->lock_handle[0];
3007         /* If the client does not require open lock, it does not need to
3008          * search lock in exp_lock_hash, since the server thread will
3009          * make sure the lock will be released, and the resend request
3010          * can always re-enqueue the lock */
3011         if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3012             info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3013                 /* In the function below, .hs_keycmp resolves to
3014                  * ldlm_export_lock_keycmp() */
3015                 /* coverity[overrun-buffer-val] */
3016                 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3017                 if (lock) {
3018                         lock_res_and_lock(lock);
3019                         if (lock != new_lock) {
3020                                 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3021                                 lh->mlh_reg_mode = lock->l_granted_mode;
3022
3023                                 LDLM_DEBUG(lock, "Restoring lock cookie");
3024                                 DEBUG_REQ(D_DLMTRACE, req,
3025                                           "restoring lock cookie "LPX64,
3026                                           lh->mlh_reg_lh.cookie);
3027                                 if (old_lock)
3028                                         *old_lock = LDLM_LOCK_GET(lock);
3029                                 cfs_hash_put(exp->exp_lock_hash,
3030                                              &lock->l_exp_hash);
3031                                 unlock_res_and_lock(lock);
3032                                 return;
3033                         }
3034                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3035                         unlock_res_and_lock(lock);
3036                 }
3037         }
3038         /*
3039          * If the xid matches, then we know this is a resent request, and allow
3040          * it. (It's probably an OPEN, for which we don't send a lock.
3041          */
3042         if (req_xid_is_last(req))
3043                 return;
3044
3045         /*
3046          * This remote handle isn't enqueued, so we never received or processed
3047          * this request.  Clear MSG_RESENT, because it can be handled like any
3048          * normal request now.
3049          */
3050         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3051
3052         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3053                   remote_hdl.cookie);
3054 }
3055
3056 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3057                                 struct mdt_thread_info *info,
3058                                 struct ldlm_lock **lockp,
3059                                 __u64 flags)
3060 {
3061         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3062         struct ldlm_reply      *ldlm_rep = NULL;
3063         int rc, grc;
3064
3065         /*
3066          * Initialize lhc->mlh_reg_lh either from a previously granted lock
3067          * (for the resend case) or a new lock. Below we will use it to
3068          * replace the original lock.
3069          */
3070         mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3071         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3072                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3073                 rc = mdt_object_lock(info, info->mti_object, lhc,
3074                                         MDS_INODELOCK_XATTR,
3075                                         MDT_LOCAL_LOCK);
3076                 if (rc)
3077                         return rc;
3078         }
3079
3080         grc = mdt_getxattr(info);
3081
3082         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3083
3084         if (mdt_info_req(info)->rq_repmsg != NULL)
3085                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3086         if (ldlm_rep == NULL)
3087                 RETURN(err_serious(-EFAULT));
3088
3089         ldlm_rep->lock_policy_res2 = grc;
3090
3091         return rc;
3092 }
3093
3094 static int mdt_intent_getattr(enum mdt_it_code opcode,
3095                               struct mdt_thread_info *info,
3096                               struct ldlm_lock **lockp,
3097                               __u64 flags)
3098 {
3099         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3100         struct ldlm_lock       *new_lock = NULL;
3101         __u64                   child_bits;
3102         struct ldlm_reply      *ldlm_rep;
3103         struct ptlrpc_request  *req;
3104         struct mdt_body        *reqbody;
3105         struct mdt_body        *repbody;
3106         int                     rc, rc2;
3107         ENTRY;
3108
3109         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3110         LASSERT(reqbody);
3111
3112         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3113         LASSERT(repbody);
3114
3115         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3116         repbody->eadatasize = 0;
3117         repbody->aclsize = 0;
3118
3119         switch (opcode) {
3120         case MDT_IT_LOOKUP:
3121                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3122                 break;
3123         case MDT_IT_GETATTR:
3124                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3125                              MDS_INODELOCK_PERM;
3126                 break;
3127         default:
3128                 CERROR("Unsupported intent (%d)\n", opcode);
3129                 GOTO(out_shrink, rc = -EINVAL);
3130         }
3131
3132         rc = mdt_init_ucred(info, reqbody);
3133         if (rc)
3134                 GOTO(out_shrink, rc);
3135
3136         req = info->mti_pill->rc_req;
3137         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3138         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3139
3140         /* Get lock from request for possible resent case. */
3141         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3142
3143         rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3144         ldlm_rep->lock_policy_res2 = clear_serious(rc);
3145
3146         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3147                 ldlm_rep->lock_policy_res2 = 0;
3148         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3149             ldlm_rep->lock_policy_res2) {
3150                 lhc->mlh_reg_lh.cookie = 0ull;
3151                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3152         }
3153
3154         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3155         EXIT;
3156 out_ucred:
3157         mdt_exit_ucred(info);
3158 out_shrink:
3159         mdt_client_compatibility(info);
3160         rc2 = mdt_fix_reply(info);
3161         if (rc == 0)
3162                 rc = rc2;
3163         return rc;
3164 }
3165
3166 static int mdt_intent_layout(enum mdt_it_code opcode,
3167                              struct mdt_thread_info *info,
3168                              struct ldlm_lock **lockp,
3169                              __u64 flags)
3170 {
3171         struct layout_intent *layout;
3172         struct lu_fid *fid;
3173         struct mdt_object *obj = NULL;
3174         struct md_object *child = NULL;
3175         int rc;
3176         ENTRY;
3177
3178         if (opcode != MDT_IT_LAYOUT) {
3179                 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3180                         opcode);
3181                 RETURN(-EINVAL);
3182         }
3183
3184         fid = &info->mti_tmp_fid2;
3185         fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3186
3187         obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3188         if (IS_ERR(obj))
3189                 RETURN(PTR_ERR(obj));
3190
3191         if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3192                 child = mdt_object_child(obj);
3193
3194                 /* get the length of lsm */
3195                 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3196                                   XATTR_NAME_LOV);
3197
3198                 if (rc > info->mti_mdt->mdt_max_mdsize)
3199                         info->mti_mdt->mdt_max_mdsize = rc;
3200         }
3201
3202         mdt_object_put(info->mti_env, obj);
3203
3204         (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3205         req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3206                         ldlm_lvbo_size(*lockp));
3207         rc = req_capsule_server_pack(info->mti_pill);
3208         if (rc != 0)
3209                 RETURN(-EINVAL);
3210
3211         layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3212         LASSERT(layout != NULL);
3213         if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3214                 /* return to normal ldlm handling */
3215                 RETURN(0);
3216
3217         CERROR("%s: Unsupported layout intent (%d)\n",
3218                 mdt_obd_name(info->mti_mdt), layout->li_opc);
3219         RETURN(-EINVAL);
3220 }
3221
3222 static int mdt_intent_reint(enum mdt_it_code opcode,
3223                             struct mdt_thread_info *info,
3224                             struct ldlm_lock **lockp,
3225                             __u64 flags)
3226 {
3227         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3228         struct ldlm_reply      *rep = NULL;
3229         long                    opc;
3230         int                     rc;
3231
3232         static const struct req_format *intent_fmts[REINT_MAX] = {
3233                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3234                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3235         };
3236
3237         ENTRY;
3238
3239         opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3240         if (opc < 0)
3241                 RETURN(opc);
3242
3243         if (mdt_it_flavor[opcode].it_reint != opc) {
3244                 CERROR("Reint code %ld doesn't match intent: %d\n",
3245                        opc, opcode);
3246                 RETURN(err_serious(-EPROTO));
3247         }
3248
3249         /* Get lock from request for possible resent case. */
3250         mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3251
3252         rc = mdt_reint_internal(info, lhc, opc);
3253
3254         /* Check whether the reply has been packed successfully. */
3255         if (mdt_info_req(info)->rq_repmsg != NULL)
3256                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3257         if (rep == NULL)
3258                 RETURN(err_serious(-EFAULT));
3259
3260         /* MDC expects this in any case */
3261         if (rc != 0)
3262                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3263
3264         /* the open lock or the lock for cross-ref object should be
3265          * returned to the client */
3266         if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3267                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3268                 rep->lock_policy_res2 = 0;
3269                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3270                 RETURN(rc);
3271         }
3272
3273         rep->lock_policy_res2 = clear_serious(rc);
3274
3275         if (rep->lock_policy_res2 == -ENOENT &&
3276             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3277                 rep->lock_policy_res2 = 0;
3278
3279         if (rc == -ENOTCONN || rc == -ENODEV ||
3280             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3281                 /*
3282                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3283                  * will be returned by rq_status, and client at ptlrpc layer
3284                  * will detect this, then disconnect, reconnect the import
3285                  * immediately, instead of impacting the following the rpc.
3286                  */
3287                 lhc->mlh_reg_lh.cookie = 0ull;
3288                 RETURN(rc);
3289         } else {
3290                 /*
3291                  * For other cases, the error will be returned by intent.
3292                  * and client will retrieve the result from intent.
3293                  */
3294                  /*
3295                   * FIXME: when open lock is finished, that should be
3296                   * checked here.
3297                   */
3298                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3299                         LASSERTF(rc == 0, "Error occurred but lock handle "
3300                                  "is still in use, rc = %d\n", rc);
3301                         rep->lock_policy_res2 = 0;
3302                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3303                         RETURN(rc);
3304                 } else {
3305                         lhc->mlh_reg_lh.cookie = 0ull;
3306                         RETURN(ELDLM_LOCK_ABORTED);
3307                 }
3308         }
3309 }
3310
3311 static int mdt_intent_code(long itcode)
3312 {
3313         int rc;
3314
3315         switch(itcode) {
3316         case IT_OPEN:
3317                 rc = MDT_IT_OPEN;
3318                 break;
3319         case IT_OPEN|IT_CREAT:
3320                 rc = MDT_IT_OCREAT;
3321                 break;
3322         case IT_CREAT:
3323                 rc = MDT_IT_CREATE;
3324                 break;
3325         case IT_READDIR:
3326                 rc = MDT_IT_READDIR;
3327                 break;
3328         case IT_GETATTR:
3329                 rc = MDT_IT_GETATTR;
3330                 break;
3331         case IT_LOOKUP:
3332                 rc = MDT_IT_LOOKUP;
3333                 break;
3334         case IT_UNLINK:
3335                 rc = MDT_IT_UNLINK;
3336                 break;
3337         case IT_TRUNC:
3338                 rc = MDT_IT_TRUNC;
3339                 break;
3340         case IT_GETXATTR:
3341                 rc = MDT_IT_GETXATTR;
3342                 break;
3343         case IT_LAYOUT:
3344                 rc = MDT_IT_LAYOUT;
3345                 break;
3346         case IT_QUOTA_DQACQ:
3347         case IT_QUOTA_CONN:
3348                 rc = MDT_IT_QUOTA;
3349                 break;
3350         default:
3351                 CERROR("Unknown intent opcode: %ld\n", itcode);
3352                 rc = -EINVAL;
3353                 break;
3354         }
3355         return rc;
3356 }
3357
3358 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3359                           struct ldlm_lock **lockp, __u64 flags)
3360 {
3361         struct req_capsule   *pill;
3362         struct mdt_it_flavor *flv;
3363         int opc;
3364         int rc;
3365         ENTRY;
3366
3367         opc = mdt_intent_code(itopc);
3368         if (opc < 0)
3369                 RETURN(-EINVAL);
3370
3371         pill = info->mti_pill;
3372
3373         if (opc == MDT_IT_QUOTA) {
3374                 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3375
3376                 if (qmt == NULL)
3377                         RETURN(-EOPNOTSUPP);
3378
3379                 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3380                 /* pass the request to quota master */
3381                 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3382                                                  mdt_info_req(info), lockp,
3383                                                  flags);
3384                 RETURN(rc);
3385         }
3386
3387         flv  = &mdt_it_flavor[opc];
3388         if (flv->it_fmt != NULL)
3389                 req_capsule_extend(pill, flv->it_fmt);
3390
3391         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3392         if (rc == 0) {
3393                 struct ptlrpc_request *req = mdt_info_req(info);
3394                 if (flv->it_flags & MUTABOR &&
3395                     exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3396                         RETURN(-EROFS);
3397         }
3398         if (rc == 0 && flv->it_act != NULL) {
3399                 struct ldlm_reply *rep;
3400
3401                 /* execute policy */
3402                 rc = flv->it_act(opc, info, lockp, flags);
3403
3404                 /* Check whether the reply has been packed successfully. */
3405                 if (mdt_info_req(info)->rq_repmsg != NULL) {
3406                         rep = req_capsule_server_get(info->mti_pill,
3407                                                      &RMF_DLM_REP);
3408                         rep->lock_policy_res2 =
3409                                 ptlrpc_status_hton(rep->lock_policy_res2);
3410                 }
3411         } else {
3412                 rc = -EPROTO;
3413         }
3414         RETURN(rc);
3415 }
3416
3417 static int mdt_intent_policy(struct ldlm_namespace *ns,
3418                              struct ldlm_lock **lockp, void *req_cookie,
3419                              ldlm_mode_t mode, __u64 flags, void *data)
3420 {
3421         struct tgt_session_info *tsi;
3422         struct mdt_thread_info  *info;
3423         struct ptlrpc_request   *req  =  req_cookie;
3424         struct ldlm_intent      *it;
3425         struct req_capsule      *pill;
3426         int rc;
3427
3428         ENTRY;
3429
3430         LASSERT(req != NULL);
3431
3432         tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3433
3434         info = tsi2mdt_info(tsi);
3435         LASSERT(info != NULL);
3436         pill = info->mti_pill;
3437         LASSERT(pill->rc_req == req);
3438
3439         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3440                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3441                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3442                 if (it != NULL) {
3443                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3444                         if (rc == 0)
3445                                 rc = ELDLM_OK;
3446
3447                         /* Lock without inodebits makes no sense and will oops
3448                          * later in ldlm. Let's check it now to see if we have
3449                          * ibits corrupted somewhere in mdt_intent_opc().
3450                          * The case for client miss to set ibits has been
3451                          * processed by others. */
3452                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3453                                         lr_type == LDLM_IBITS,
3454                                      info->mti_dlm_req->lock_desc.\
3455                                         l_policy_data.l_inodebits.bits != 0));
3456                 } else
3457                         rc = err_serious(-EFAULT);
3458         } else {
3459                 /* No intent was provided */
3460                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3461                 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3462                 rc = req_capsule_server_pack(pill);
3463                 if (rc)
3464                         rc = err_serious(rc);
3465         }
3466         mdt_thread_info_fini(info);
3467         RETURN(rc);
3468 }
3469
3470 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3471 {
3472         struct seq_server_site  *ss = mdt_seq_site(mdt);
3473
3474         if (ss->ss_node_id == 0)
3475                 return;
3476
3477         if (ss->ss_client_seq != NULL) {
3478                 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3479                 ss->ss_client_seq->lcs_exp = NULL;
3480         }
3481
3482         if (ss->ss_server_fld != NULL) {
3483                 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3484                 ss->ss_server_fld->lsf_control_exp = NULL;
3485         }
3486 }
3487
3488 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3489 {
3490         struct seq_server_site *ss = mdt_seq_site(mdt);
3491
3492         if (ss == NULL)
3493                 return;
3494
3495         if (ss->ss_server_seq == NULL)
3496                 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3497
3498         return;
3499 }
3500
3501 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3502 {
3503         mdt_seq_fini_cli(mdt);
3504         mdt_deregister_seq_exp(mdt);
3505
3506         return seq_site_fini(env, mdt_seq_site(mdt));
3507 }
3508
3509 /**
3510  * It will retrieve its FLDB entries from MDT0, and it only happens
3511  * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3512  * and it needs to refresh FLDB from the MDT0.
3513  **/
3514 static int mdt_register_lwp_callback(void *data)
3515 {
3516         struct lu_env           env;
3517         struct mdt_device       *mdt = data;
3518         struct lu_server_fld    *fld = mdt_seq_site(mdt)->ss_server_fld;
3519         int                     rc;
3520         ENTRY;
3521
3522         LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3523
3524         if (!likely(fld->lsf_new))
3525                 RETURN(0);
3526
3527         rc = lu_env_init(&env, LCT_MD_THREAD);
3528         if (rc) {
3529                 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3530                 RETURN(rc);
3531         }
3532
3533         rc = fld_update_from_controller(&env, fld);
3534         if (rc != 0) {
3535                 CERROR("%s: cannot update controller: rc = %d\n",
3536                        mdt_obd_name(mdt), rc);
3537                 GOTO(out, rc);
3538         }
3539 out:
3540         lu_env_fini(&env);
3541         RETURN(rc);
3542 }
3543
3544 static int mdt_register_seq_exp(struct mdt_device *mdt)
3545 {
3546         struct seq_server_site  *ss = mdt_seq_site(mdt);
3547         char                    *lwp_name = NULL;
3548         int                     rc;
3549
3550         if (ss->ss_node_id == 0)
3551                 return 0;
3552
3553         OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3554         if (lwp_name == NULL)
3555                 GOTO(out_free, rc = -ENOMEM);
3556
3557         rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3558         if (rc != 0)
3559                 GOTO(out_free, rc);
3560
3561         rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3562                                       NULL, NULL);
3563         if (rc != 0)
3564                 GOTO(out_free, rc);
3565
3566         rc = lustre_register_lwp_item(lwp_name,
3567                                       &ss->ss_server_fld->lsf_control_exp,
3568                                       mdt_register_lwp_callback, mdt);
3569         if (rc != 0) {
3570                 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3571                 ss->ss_client_seq->lcs_exp = NULL;
3572                 GOTO(out_free, rc);
3573         }
3574 out_free:
3575         if (lwp_name != NULL)
3576                 OBD_FREE(lwp_name, MAX_OBD_NAME);
3577
3578         return rc;
3579 }
3580
3581 /*
3582  * Init client sequence manager which is used by local MDS to talk to sequence
3583  * controller on remote node.
3584  */
3585 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3586 {
3587         struct seq_server_site  *ss = mdt_seq_site(mdt);
3588         int                     rc;
3589         char                    *prefix;
3590         ENTRY;
3591
3592         /* check if this is adding the first MDC and controller is not yet
3593          * initialized. */
3594         OBD_ALLOC_PTR(ss->ss_client_seq);
3595         if (ss->ss_client_seq == NULL)
3596                 RETURN(-ENOMEM);
3597
3598         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3599         if (prefix == NULL) {
3600                 OBD_FREE_PTR(ss->ss_client_seq);
3601                 ss->ss_client_seq = NULL;
3602                 RETURN(-ENOMEM);
3603         }
3604
3605         /* Note: seq_client_fini will be called in seq_site_fini */
3606         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3607         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3608                              prefix, ss->ss_node_id == 0 ?  ss->ss_control_seq :
3609                                                             NULL);
3610         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3611         if (rc != 0) {
3612                 OBD_FREE_PTR(ss->ss_client_seq);
3613                 ss->ss_client_seq = NULL;
3614                 RETURN(rc);
3615         }
3616
3617         rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3618
3619         RETURN(rc);
3620 }
3621
3622 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3623 {
3624         struct seq_server_site  *ss;
3625         int                     rc;
3626         ENTRY;
3627
3628         ss = mdt_seq_site(mdt);
3629         /* init sequence controller server(MDT0) */
3630         if (ss->ss_node_id == 0) {
3631                 OBD_ALLOC_PTR(ss->ss_control_seq);
3632                 if (ss->ss_control_seq == NULL)
3633                         RETURN(-ENOMEM);
3634
3635                 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3636                                      mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3637                                      ss);
3638                 if (rc)
3639                         GOTO(out_seq_fini, rc);
3640         }
3641
3642         /* Init normal sequence server */
3643         OBD_ALLOC_PTR(ss->ss_server_seq);
3644         if (ss->ss_server_seq == NULL)
3645                 GOTO(out_seq_fini, rc = -ENOMEM);
3646
3647         rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3648                              mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3649         if (rc)
3650                 GOTO(out_seq_fini, rc);
3651
3652         /* init seq client for seq server to talk to seq controller(MDT0) */
3653         rc = mdt_seq_init_cli(env, mdt);
3654         if (rc != 0)
3655                 GOTO(out_seq_fini, rc);
3656
3657         if (ss->ss_node_id != 0)
3658                 /* register controler export through lwp */
3659                 rc = mdt_register_seq_exp(mdt);
3660
3661         EXIT;
3662 out_seq_fini:
3663         if (rc)
3664                 mdt_seq_fini(env, mdt);
3665
3666         return rc;
3667 }
3668
3669 /*
3670  * FLD wrappers
3671  */
3672 static int mdt_fld_fini(const struct lu_env *env,
3673                         struct mdt_device *m)
3674 {
3675         struct seq_server_site *ss = mdt_seq_site(m);
3676         ENTRY;
3677
3678         if (ss && ss->ss_server_fld) {
3679                 fld_server_fini(env, ss->ss_server_fld);
3680                 OBD_FREE_PTR(ss->ss_server_fld);
3681                 ss->ss_server_fld = NULL;
3682         }
3683
3684         RETURN(0);
3685 }
3686
3687 static int mdt_fld_init(const struct lu_env *env,
3688       &nb