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