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