Whamcloud - gitweb
LU-3467 target: unified transaction callbacks
[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 lock is
1317                          * needed here but update is.
1318                          */
1319                         child_bits &= ~MDS_INODELOCK_LOOKUP;
1320                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1321
1322                         rc = mdt_object_lock(info, child, lhc, child_bits,
1323                                              MDT_LOCAL_LOCK);
1324                 }
1325                 if (rc == 0) {
1326                         /* Finally, we can get attr for child. */
1327                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
1328                                          BYPASS_CAPA);
1329                         rc = mdt_getattr_internal(info, child, 0);
1330                         if (unlikely(rc != 0))
1331                                 mdt_object_unlock(info, child, lhc, 1);
1332                 }
1333                 RETURN(rc);
1334         }
1335
1336         if (lname) {
1337                 /* step 1: lock parent only if parent is a directory */
1338                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1339                         lhp = &info->mti_lh[MDT_LH_PARENT];
1340                         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1341                         rc = mdt_object_lock(info, parent, lhp,
1342                                              MDS_INODELOCK_UPDATE,
1343                                              MDT_LOCAL_LOCK);
1344                         if (unlikely(rc != 0))
1345                                 RETURN(rc);
1346                 }
1347
1348                 /* step 2: lookup child's fid by name */
1349                 fid_zero(child_fid);
1350                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1351                                 &info->mti_spec);
1352
1353                 if (rc != 0) {
1354                         if (rc == -ENOENT)
1355                                 mdt_set_disposition(info, ldlm_rep,
1356                                                     DISP_LOOKUP_NEG);
1357                         GOTO(out_parent, rc);
1358                 } else
1359                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1360         } else {
1361                 *child_fid = reqbody->fid2;
1362                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1363         }
1364
1365         /*
1366          *step 3: find the child object by fid & lock it.
1367          *        regardless if it is local or remote.
1368          *
1369          *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1370          *      set parent dir fid the same as child fid in getattr by fid case
1371          *      we should not lu_object_find() the object again, could lead
1372          *      to hung if there is a concurrent unlink destroyed the object.
1373          */
1374         if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1375                 mdt_object_get(info->mti_env, parent);
1376                 child = parent;
1377         } else {
1378                 child = mdt_object_find(info->mti_env, info->mti_mdt,
1379                                         child_fid);
1380         }
1381
1382         if (unlikely(IS_ERR(child)))
1383                 GOTO(out_parent, rc = PTR_ERR(child));
1384         if (is_resent) {
1385                 /* Do not take lock for resent case. */
1386                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1387                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1388                          lhc->mlh_reg_lh.cookie);
1389
1390                 res_id = &lock->l_resource->lr_name;
1391                 if (!fid_res_name_eq(mdt_object_fid(child),
1392                                      &lock->l_resource->lr_name)) {
1393                         LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1394                                                  &lock->l_resource->lr_name),
1395                                  "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1396                                  PLDLMRES(lock->l_resource),
1397                                  PFID(mdt_object_fid(parent)));
1398                         CWARN("Although resent, but still not get child lock"
1399                               "parent:"DFID" child:"DFID"\n",
1400                               PFID(mdt_object_fid(parent)),
1401                               PFID(mdt_object_fid(child)));
1402                         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1403                         LDLM_LOCK_PUT(lock);
1404                         GOTO(relock, 0);
1405                 }
1406                 LDLM_LOCK_PUT(lock);
1407                 rc = 0;
1408         } else {
1409                 bool try_layout = false;
1410
1411 relock:
1412                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1413                 mdt_lock_handle_init(lhc);
1414                 mdt_lock_reg_init(lhc, LCK_PR);
1415
1416                 if (!mdt_object_exists(child)) {
1417                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1418                                         &child->mot_obj,
1419                                         "Object doesn't exist!\n");
1420                         GOTO(out_child, rc = -ENOENT);
1421                 }
1422
1423                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1424                       mdt_object_exists(child) && !mdt_object_remote(child)) {
1425                         struct md_attr *ma = &info->mti_attr;
1426
1427                         ma->ma_valid = 0;
1428                         ma->ma_need = MA_INODE;
1429                         rc = mdt_attr_get_complex(info, child, ma);
1430                         if (unlikely(rc != 0))
1431                                 GOTO(out_child, rc);
1432
1433                         /* If the file has not been changed for some time, we
1434                          * return not only a LOOKUP lock, but also an UPDATE
1435                          * lock and this might save us RPC on later STAT. For
1436                          * directories, it also let negative dentry cache start
1437                          * working for this dir. */
1438                         if (ma->ma_valid & MA_INODE &&
1439                             ma->ma_attr.la_valid & LA_CTIME &&
1440                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1441                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1442                                 child_bits |= MDS_INODELOCK_UPDATE;
1443                 }
1444
1445                 /* layout lock must be granted in a best-effort way
1446                  * for IT operations */
1447                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1448                 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1449                     exp_connect_layout(info->mti_exp) &&
1450                     S_ISREG(lu_object_attr(&child->mot_obj)) &&
1451                     ldlm_rep != NULL) {
1452                         /* try to grant layout lock for regular file. */
1453                         try_layout = true;
1454                 }
1455
1456                 rc = 0;
1457                 if (try_layout) {
1458                         child_bits |= MDS_INODELOCK_LAYOUT;
1459                         /* try layout lock, it may fail to be granted due to
1460                          * contention at LOOKUP or UPDATE */
1461                         if (!mdt_object_lock_try(info, child, lhc, child_bits,
1462                                                  MDT_CROSS_LOCK)) {
1463                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1464                                 LASSERT(child_bits != 0);
1465                                 rc = mdt_object_lock(info, child, lhc,
1466                                                 child_bits, MDT_CROSS_LOCK);
1467                         } else {
1468                                 ma_need |= MA_LOV;
1469                         }
1470                 } else {
1471                         rc = mdt_object_lock(info, child, lhc, child_bits,
1472                                                 MDT_CROSS_LOCK);
1473                 }
1474                 if (unlikely(rc != 0))
1475                         GOTO(out_child, rc);
1476         }
1477
1478         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1479         /* Get MA_SOM attributes if update lock is given. */
1480         if (lock &&
1481             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1482             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1483                 ma_need |= MA_SOM;
1484
1485         /* finally, we can get attr for child. */
1486         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1487         rc = mdt_getattr_internal(info, child, ma_need);
1488         if (unlikely(rc != 0)) {
1489                 mdt_object_unlock(info, child, lhc, 1);
1490         } else if (lock) {
1491                 /* Debugging code. */
1492                 res_id = &lock->l_resource->lr_name;
1493                 LDLM_DEBUG(lock, "Returning lock to client");
1494                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1495                                          &lock->l_resource->lr_name),
1496                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1497                          PLDLMRES(lock->l_resource),
1498                          PFID(mdt_object_fid(child)));
1499                 if (mdt_object_exists(child) && !mdt_object_remote(child))
1500                         mdt_pack_size2body(info, child);
1501         }
1502         if (lock)
1503                 LDLM_LOCK_PUT(lock);
1504
1505         EXIT;
1506 out_child:
1507         mdt_object_put(info->mti_env, child);
1508 out_parent:
1509         if (lhp)
1510                 mdt_object_unlock(info, parent, lhp, 1);
1511         return rc;
1512 }
1513
1514 /* normal handler: should release the child lock */
1515 int mdt_getattr_name(struct tgt_session_info *tsi)
1516 {
1517         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1518         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1519         struct mdt_body        *reqbody;
1520         struct mdt_body        *repbody;
1521         int rc, rc2;
1522         ENTRY;
1523
1524         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1525         LASSERT(reqbody != NULL);
1526         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1527         LASSERT(repbody != NULL);
1528
1529         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1530         repbody->eadatasize = 0;
1531         repbody->aclsize = 0;
1532
1533         rc = mdt_init_ucred(info, reqbody);
1534         if (unlikely(rc))
1535                 GOTO(out_shrink, rc);
1536
1537         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1538         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1539                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1540                 lhc->mlh_reg_lh.cookie = 0;
1541         }
1542         mdt_exit_ucred(info);
1543         EXIT;
1544 out_shrink:
1545         mdt_client_compatibility(info);
1546         rc2 = mdt_fix_reply(info);
1547         if (rc == 0)
1548                 rc = rc2;
1549         mdt_thread_info_fini(info);
1550         return rc;
1551 }
1552
1553 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1554                          void *karg, void *uarg);
1555
1556 int mdt_set_info(struct tgt_session_info *tsi)
1557 {
1558         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1559         char                    *key;
1560         void                    *val;
1561         int                      keylen, vallen, rc = 0;
1562
1563         ENTRY;
1564
1565         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1566         if (key == NULL) {
1567                 DEBUG_REQ(D_HA, req, "no set_info key");
1568                 RETURN(err_serious(-EFAULT));
1569         }
1570
1571         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1572                                       RCL_CLIENT);
1573
1574         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1575         if (val == NULL) {
1576                 DEBUG_REQ(D_HA, req, "no set_info val");
1577                 RETURN(err_serious(-EFAULT));
1578         }
1579
1580         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1581                                       RCL_CLIENT);
1582
1583         /* Swab any part of val you need to here */
1584         if (KEY_IS(KEY_READ_ONLY)) {
1585                 spin_lock(&req->rq_export->exp_lock);
1586                 if (*(__u32 *)val)
1587                         *exp_connect_flags_ptr(req->rq_export) |=
1588                                 OBD_CONNECT_RDONLY;
1589                 else
1590                         *exp_connect_flags_ptr(req->rq_export) &=
1591                                 ~OBD_CONNECT_RDONLY;
1592                 spin_unlock(&req->rq_export->exp_lock);
1593         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1594                 struct changelog_setinfo *cs = val;
1595
1596                 if (vallen != sizeof(*cs)) {
1597                         CERROR("%s: bad changelog_clear setinfo size %d\n",
1598                                tgt_name(tsi->tsi_tgt), vallen);
1599                         RETURN(-EINVAL);
1600                 }
1601                 if (ptlrpc_req_need_swab(req)) {
1602                         __swab64s(&cs->cs_recno);
1603                         __swab32s(&cs->cs_id);
1604                 }
1605
1606                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1607                                    vallen, val, NULL);
1608         } else {
1609                 RETURN(-EINVAL);
1610         }
1611         RETURN(rc);
1612 }
1613
1614 int mdt_readpage(struct tgt_session_info *tsi)
1615 {
1616         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
1617         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
1618         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
1619         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
1620         struct mdt_body         *repbody;
1621         int                      rc;
1622         int                      i;
1623
1624         ENTRY;
1625
1626         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1627                 RETURN(err_serious(-ENOMEM));
1628
1629         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1630         if (repbody == NULL || reqbody == NULL)
1631                 RETURN(err_serious(-EFAULT));
1632
1633         /*
1634          * prepare @rdpg before calling lower layers and transfer itself. Here
1635          * reqbody->size contains offset of where to start to read and
1636          * reqbody->nlink contains number bytes to read.
1637          */
1638         rdpg->rp_hash = reqbody->size;
1639         if (rdpg->rp_hash != reqbody->size) {
1640                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1641                        rdpg->rp_hash, reqbody->size);
1642                 RETURN(-EFAULT);
1643         }
1644
1645         rdpg->rp_attrs = reqbody->mode;
1646         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1647                 rdpg->rp_attrs |= LUDA_64BITHASH;
1648         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1649                                 exp_max_brw_size(tsi->tsi_exp));
1650         rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1651                           PAGE_CACHE_SHIFT;
1652         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1653         if (rdpg->rp_pages == NULL)
1654                 RETURN(-ENOMEM);
1655
1656         for (i = 0; i < rdpg->rp_npages; ++i) {
1657                 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1658                 if (rdpg->rp_pages[i] == NULL)
1659                         GOTO(free_rdpg, rc = -ENOMEM);
1660         }
1661
1662         /* call lower layers to fill allocated pages with directory data */
1663         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1664         if (rc < 0)
1665                 GOTO(free_rdpg, rc);
1666
1667         /* send pages to client */
1668         rc = tgt_sendpage(tsi, rdpg, rc);
1669
1670         EXIT;
1671 free_rdpg:
1672
1673         for (i = 0; i < rdpg->rp_npages; i++)
1674                 if (rdpg->rp_pages[i] != NULL)
1675                         __free_page(rdpg->rp_pages[i]);
1676         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1677
1678         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1679                 RETURN(0);
1680
1681         return rc;
1682 }
1683
1684 static int mdt_reint_internal(struct mdt_thread_info *info,
1685                               struct mdt_lock_handle *lhc,
1686                               __u32 op)
1687 {
1688         struct req_capsule      *pill = info->mti_pill;
1689         struct mdt_body         *repbody;
1690         int                      rc = 0, rc2;
1691
1692         ENTRY;
1693
1694         rc = mdt_reint_unpack(info, op);
1695         if (rc != 0) {
1696                 CERROR("Can't unpack reint, rc %d\n", rc);
1697                 RETURN(err_serious(rc));
1698         }
1699
1700         /* for replay (no_create) lmm is not needed, client has it already */
1701         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1702                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1703                                      info->mti_rr.rr_eadatalen);
1704
1705         /* llog cookies are always 0, the field is kept for compatibility */
1706         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1707                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1708
1709         rc = req_capsule_server_pack(pill);
1710         if (rc != 0) {
1711                 CERROR("Can't pack response, rc %d\n", rc);
1712                 RETURN(err_serious(rc));
1713         }
1714
1715         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1716                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1717                 LASSERT(repbody);
1718                 repbody->eadatasize = 0;
1719                 repbody->aclsize = 0;
1720         }
1721
1722         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1723
1724         /* for replay no cookkie / lmm need, because client have this already */
1725         if (info->mti_spec.no_create)
1726                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1727                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1728
1729         rc = mdt_init_ucred_reint(info);
1730         if (rc)
1731                 GOTO(out_shrink, rc);
1732
1733         rc = mdt_fix_attr_ucred(info, op);
1734         if (rc != 0)
1735                 GOTO(out_ucred, rc = err_serious(rc));
1736
1737         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1738                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1739                 GOTO(out_ucred, rc);
1740         }
1741         rc = mdt_reint_rec(info, lhc);
1742         EXIT;
1743 out_ucred:
1744         mdt_exit_ucred(info);
1745 out_shrink:
1746         mdt_client_compatibility(info);
1747         rc2 = mdt_fix_reply(info);
1748         if (rc == 0)
1749                 rc = rc2;
1750         return rc;
1751 }
1752
1753 static long mdt_reint_opcode(struct ptlrpc_request *req,
1754                              const struct req_format **fmt)
1755 {
1756         struct mdt_device       *mdt;
1757         struct mdt_rec_reint    *rec;
1758         long                     opc;
1759
1760         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1761         if (rec != NULL) {
1762                 opc = rec->rr_opcode;
1763                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1764                 if (opc < REINT_MAX && fmt[opc] != NULL)
1765                         req_capsule_extend(&req->rq_pill, fmt[opc]);
1766                 else {
1767                         mdt = mdt_exp2dev(req->rq_export);
1768                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1769                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
1770                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1771                         opc = err_serious(-EFAULT);
1772                 }
1773         } else {
1774                 opc = err_serious(-EFAULT);
1775         }
1776         return opc;
1777 }
1778
1779 int mdt_reint(struct tgt_session_info *tsi)
1780 {
1781         long opc;
1782         int  rc;
1783         static const struct req_format *reint_fmts[REINT_MAX] = {
1784                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1785                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1786                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1787                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1788                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1789                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1790                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1791                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK
1792         };
1793
1794         ENTRY;
1795
1796         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1797         if (opc >= 0) {
1798                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1799                 /*
1800                  * No lock possible here from client to pass it to reint code
1801                  * path.
1802                  */
1803                 rc = mdt_reint_internal(info, NULL, opc);
1804                 mdt_thread_info_fini(info);
1805         } else {
1806                 rc = opc;
1807         }
1808
1809         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1810         RETURN(rc);
1811 }
1812
1813 /* this should sync the whole device */
1814 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1815 {
1816         struct dt_device *dt = mdt->mdt_bottom;
1817         int rc;
1818         ENTRY;
1819
1820         rc = dt->dd_ops->dt_sync(env, dt);
1821         RETURN(rc);
1822 }
1823
1824 /* this should sync this object */
1825 static int mdt_object_sync(struct mdt_thread_info *info)
1826 {
1827         struct md_object *next;
1828         int rc;
1829         ENTRY;
1830
1831         if (!mdt_object_exists(info->mti_object)) {
1832                 CWARN("Non existing object  "DFID"!\n",
1833                       PFID(mdt_object_fid(info->mti_object)));
1834                 RETURN(-ESTALE);
1835         }
1836         next = mdt_object_child(info->mti_object);
1837         rc = mo_object_sync(info->mti_env, next);
1838
1839         RETURN(rc);
1840 }
1841
1842 int mdt_sync(struct tgt_session_info *tsi)
1843 {
1844         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1845         struct req_capsule      *pill = tsi->tsi_pill;
1846         struct mdt_body         *body;
1847         int                      rc;
1848
1849         ENTRY;
1850
1851         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1852                 RETURN(err_serious(-ENOMEM));
1853
1854         if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1855                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1856         } else {
1857                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1858
1859                 /* sync an object */
1860                 rc = mdt_object_sync(info);
1861                 if (rc == 0) {
1862                         const struct lu_fid *fid;
1863                         struct lu_attr *la = &info->mti_attr.ma_attr;
1864
1865                         info->mti_attr.ma_need = MA_INODE;
1866                         info->mti_attr.ma_valid = 0;
1867                         rc = mdt_attr_get_complex(info, info->mti_object,
1868                                                   &info->mti_attr);
1869                         if (rc == 0) {
1870                                 body = req_capsule_server_get(pill,
1871                                                               &RMF_MDT_BODY);
1872                                 fid = mdt_object_fid(info->mti_object);
1873                                 mdt_pack_attr2body(info, body, la, fid);
1874                         }
1875                 }
1876                 mdt_thread_info_fini(info);
1877         }
1878         if (rc == 0)
1879                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1880
1881         RETURN(rc);
1882 }
1883
1884 /*
1885  * Handle quota control requests to consult current usage/limit, but also
1886  * to configure quota enforcement
1887  */
1888 int mdt_quotactl(struct tgt_session_info *tsi)
1889 {
1890         struct obd_export       *exp  = tsi->tsi_exp;
1891         struct req_capsule      *pill = tsi->tsi_pill;
1892         struct obd_quotactl     *oqctl, *repoqc;
1893         int                      id, rc;
1894         struct mdt_device       *mdt = mdt_exp2dev(exp);
1895         struct lu_device        *qmt = mdt->mdt_qmt_dev;
1896         ENTRY;
1897
1898         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1899         if (oqctl == NULL)
1900                 RETURN(err_serious(-EPROTO));
1901
1902         rc = req_capsule_server_pack(pill);
1903         if (rc)
1904                 RETURN(err_serious(rc));
1905
1906         switch (oqctl->qc_cmd) {
1907         case Q_QUOTACHECK:
1908         case LUSTRE_Q_INVALIDATE:
1909         case LUSTRE_Q_FINVALIDATE:
1910         case Q_QUOTAON:
1911         case Q_QUOTAOFF:
1912         case Q_INITQUOTA:
1913                 /* deprecated, not used any more */
1914                 RETURN(-EOPNOTSUPP);
1915                 /* master quotactl */
1916         case Q_GETINFO:
1917         case Q_SETINFO:
1918         case Q_SETQUOTA:
1919         case Q_GETQUOTA:
1920                 if (qmt == NULL)
1921                         RETURN(-EOPNOTSUPP);
1922                 /* slave quotactl */
1923         case Q_GETOINFO:
1924         case Q_GETOQUOTA:
1925                 break;
1926         default:
1927                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1928                 RETURN(-EFAULT);
1929         }
1930
1931         /* map uid/gid for remote client */
1932         id = oqctl->qc_id;
1933         if (exp_connect_rmtclient(exp)) {
1934                 struct lustre_idmap_table *idmap;
1935
1936                 idmap = exp->exp_mdt_data.med_idmap;
1937
1938                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1939                              oqctl->qc_cmd != Q_GETINFO))
1940                         RETURN(-EPERM);
1941
1942                 if (oqctl->qc_type == USRQUOTA)
1943                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1944                                                      oqctl->qc_id);
1945                 else if (oqctl->qc_type == GRPQUOTA)
1946                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1947                                                      oqctl->qc_id);
1948                 else
1949                         RETURN(-EINVAL);
1950
1951                 if (id == CFS_IDMAP_NOTFOUND) {
1952                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1953                         RETURN(-EACCES);
1954                 }
1955         }
1956
1957         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1958         if (repoqc == NULL)
1959                 RETURN(err_serious(-EFAULT));
1960
1961         if (oqctl->qc_id != id)
1962                 swap(oqctl->qc_id, id);
1963
1964         switch (oqctl->qc_cmd) {
1965
1966         case Q_GETINFO:
1967         case Q_SETINFO:
1968         case Q_SETQUOTA:
1969         case Q_GETQUOTA:
1970                 /* forward quotactl request to QMT */
1971                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
1972                 break;
1973
1974         case Q_GETOINFO:
1975         case Q_GETOQUOTA:
1976                 /* slave quotactl */
1977                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
1978                                    oqctl);
1979                 break;
1980
1981         default:
1982                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1983                 RETURN(-EFAULT);
1984         }
1985
1986         if (oqctl->qc_id != id)
1987                 swap(oqctl->qc_id, id);
1988
1989         *repoqc = *oqctl;
1990         RETURN(rc);
1991 }
1992
1993 /** clone llog ctxt from child (mdd)
1994  * This allows remote llog (replicator) access.
1995  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1996  * context was originally set up, or we can handle them directly.
1997  * I choose the latter, but that means I need any llog
1998  * contexts set up by child to be accessable by the mdt.  So we clone the
1999  * context into our context list here.
2000  */
2001 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2002                                int idx)
2003 {
2004         struct md_device  *next = mdt->mdt_child;
2005         struct llog_ctxt *ctxt;
2006         int rc;
2007
2008         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2009                 return 0;
2010
2011         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2012         if (rc || ctxt == NULL) {
2013                 return 0;
2014         }
2015
2016         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2017         if (rc)
2018                 CERROR("Can't set mdt ctxt %d\n", rc);
2019
2020         return rc;
2021 }
2022
2023 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2024                                  struct mdt_device *mdt, int idx)
2025 {
2026         struct llog_ctxt *ctxt;
2027
2028         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2029         if (ctxt == NULL)
2030                 return 0;
2031         /* Put once for the get we just did, and once for the clone */
2032         llog_ctxt_put(ctxt);
2033         llog_ctxt_put(ctxt);
2034         return 0;
2035 }
2036
2037 /*
2038  * sec context handlers
2039  */
2040 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2041 {
2042         int rc;
2043
2044         rc = mdt_handle_idmap(tsi);
2045         if (unlikely(rc)) {
2046                 struct ptlrpc_request   *req = tgt_ses_req(tsi);
2047                 __u32                    opc;
2048
2049                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2050                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2051                         sptlrpc_svc_ctx_invalidate(req);
2052         }
2053
2054         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2055
2056         return rc;
2057 }
2058
2059 /*
2060  * quota request handlers
2061  */
2062 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2063 {
2064         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
2065         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2066         int                      rc;
2067         ENTRY;
2068
2069         if (qmt == NULL)
2070                 RETURN(err_serious(-EOPNOTSUPP));
2071
2072         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2073         RETURN(rc);
2074 }
2075
2076 struct mdt_object *mdt_object_new(const struct lu_env *env,
2077                                   struct mdt_device *d,
2078                                   const struct lu_fid *f)
2079 {
2080         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2081         struct lu_object *o;
2082         struct mdt_object *m;
2083         ENTRY;
2084
2085         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2086         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2087         if (unlikely(IS_ERR(o)))
2088                 m = (struct mdt_object *)o;
2089         else
2090                 m = mdt_obj(o);
2091         RETURN(m);
2092 }
2093
2094 struct mdt_object *mdt_object_find(const struct lu_env *env,
2095                                    struct mdt_device *d,
2096                                    const struct lu_fid *f)
2097 {
2098         struct lu_object *o;
2099         struct mdt_object *m;
2100         ENTRY;
2101
2102         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2103         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2104         if (unlikely(IS_ERR(o)))
2105                 m = (struct mdt_object *)o;
2106         else
2107                 m = mdt_obj(o);
2108
2109         RETURN(m);
2110 }
2111
2112 /**
2113  * Asyncronous commit for mdt device.
2114  *
2115  * Pass asynchonous commit call down the MDS stack.
2116  *
2117  * \param env environment
2118  * \param mdt the mdt device
2119  */
2120 static void mdt_device_commit_async(const struct lu_env *env,
2121                                     struct mdt_device *mdt)
2122 {
2123         struct dt_device *dt = mdt->mdt_bottom;
2124         int rc;
2125
2126         rc = dt->dd_ops->dt_commit_async(env, dt);
2127         if (unlikely(rc != 0))
2128                 CWARN("async commit start failed with rc = %d", rc);
2129 }
2130
2131 /**
2132  * Mark the lock as "synchonous".
2133  *
2134  * Mark the lock to deffer transaction commit to the unlock time.
2135  *
2136  * \param lock the lock to mark as "synchonous"
2137  *
2138  * \see mdt_is_lock_sync
2139  * \see mdt_save_lock
2140  */
2141 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2142 {
2143         lock->l_ast_data = (void*)1;
2144 }
2145
2146 /**
2147  * Check whehter the lock "synchonous" or not.
2148  *
2149  * \param lock the lock to check
2150  * \retval 1 the lock is "synchonous"
2151  * \retval 0 the lock isn't "synchronous"
2152  *
2153  * \see mdt_set_lock_sync
2154  * \see mdt_save_lock
2155  */
2156 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2157 {
2158         return lock->l_ast_data != NULL;
2159 }
2160
2161 /**
2162  * Blocking AST for mdt locks.
2163  *
2164  * Starts transaction commit if in case of COS lock conflict or
2165  * deffers such a commit to the mdt_save_lock.
2166  *
2167  * \param lock the lock which blocks a request or cancelling lock
2168  * \param desc unused
2169  * \param data unused
2170  * \param flag indicates whether this cancelling or blocking callback
2171  * \retval 0
2172  * \see ldlm_blocking_ast_nocheck
2173  */
2174 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2175                      void *data, int flag)
2176 {
2177         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2178         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2179         int rc;
2180         ENTRY;
2181
2182         if (flag == LDLM_CB_CANCELING)
2183                 RETURN(0);
2184         lock_res_and_lock(lock);
2185         if (lock->l_blocking_ast != mdt_blocking_ast) {
2186                 unlock_res_and_lock(lock);
2187                 RETURN(0);
2188         }
2189         if (mdt_cos_is_enabled(mdt) &&
2190             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2191             lock->l_blocking_lock != NULL &&
2192             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2193                 mdt_set_lock_sync(lock);
2194         }
2195         rc = ldlm_blocking_ast_nocheck(lock);
2196
2197         /* There is no lock conflict if l_blocking_lock == NULL,
2198          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2199          * when the last reference to a local lock was released */
2200         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2201                 struct lu_env env;
2202
2203                 rc = lu_env_init(&env, LCT_LOCAL);
2204                 if (unlikely(rc != 0))
2205                         CWARN("lu_env initialization failed with rc = %d,"
2206                               "cannot start asynchronous commit\n", rc);
2207                 else
2208                         mdt_device_commit_async(&env, mdt);
2209                 lu_env_fini(&env);
2210         }
2211         RETURN(rc);
2212 }
2213
2214 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2215                         void *data, int flag)
2216 {
2217         struct lustre_handle lockh;
2218         int               rc;
2219
2220         switch (flag) {
2221         case LDLM_CB_BLOCKING:
2222                 ldlm_lock2handle(lock, &lockh);
2223                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2224                 if (rc < 0) {
2225                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2226                         RETURN(rc);
2227                 }
2228                 break;
2229         case LDLM_CB_CANCELING:
2230                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2231                 break;
2232         default:
2233                 LBUG();
2234         }
2235         RETURN(0);
2236 }
2237
2238 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2239                            struct mdt_object *o, struct lustre_handle *lh,
2240                            ldlm_mode_t mode, __u64 ibits)
2241 {
2242         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2243         ldlm_policy_data_t *policy = &mti->mti_policy;
2244         int rc = 0;
2245         ENTRY;
2246
2247         LASSERT(mdt_object_remote(o));
2248
2249         LASSERT(ibits & MDS_INODELOCK_UPDATE);
2250
2251         memset(einfo, 0, sizeof(*einfo));
2252         einfo->ei_type = LDLM_IBITS;
2253         einfo->ei_mode = mode;
2254         einfo->ei_cb_bl = mdt_md_blocking_ast;
2255         einfo->ei_cb_cp = ldlm_completion_ast;
2256
2257         memset(policy, 0, sizeof(*policy));
2258         policy->l_inodebits.bits = ibits;
2259
2260         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2261                             policy);
2262         RETURN(rc);
2263 }
2264
2265 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2266                             struct mdt_lock_handle *lh, __u64 ibits,
2267                             bool nonblock, int locality)
2268 {
2269         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2270         ldlm_policy_data_t *policy = &info->mti_policy;
2271         struct ldlm_res_id *res_id = &info->mti_res_id;
2272         __u64 dlmflags;
2273         int rc;
2274         ENTRY;
2275
2276         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2277         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2278         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2279         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2280
2281         if (mdt_object_remote(o)) {
2282                 if (locality == MDT_CROSS_LOCK) {
2283                         ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2284                         ibits |= MDS_INODELOCK_LOOKUP;
2285                 } else {
2286                         LASSERTF(!(ibits &
2287                                   (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2288                                 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2289                                 mdt_obd_name(info->mti_mdt), ibits,
2290                                 PFID(mdt_object_fid(o)));
2291                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2292                 }
2293                 /* No PDO lock on remote object */
2294                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2295         }
2296
2297         if (lh->mlh_type == MDT_PDO_LOCK) {
2298                 /* check for exists after object is locked */
2299                 if (mdt_object_exists(o) == 0) {
2300                         /* Non-existent object shouldn't have PDO lock */
2301                         RETURN(-ESTALE);
2302                 } else {
2303                         /* Non-dir object shouldn't have PDO lock */
2304                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2305                                 RETURN(-ENOTDIR);
2306                 }
2307         }
2308
2309         memset(policy, 0, sizeof(*policy));
2310         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2311
2312         dlmflags = LDLM_FL_ATOMIC_CB;
2313         if (nonblock)
2314                 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2315
2316         /*
2317          * Take PDO lock on whole directory and build correct @res_id for lock
2318          * on part of directory.
2319          */
2320         if (lh->mlh_pdo_hash != 0) {
2321                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2322                 mdt_lock_pdo_mode(info, o, lh);
2323                 if (lh->mlh_pdo_mode != LCK_NL) {
2324                         /*
2325                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2326                          * is never going to be sent to client and we do not
2327                          * want it slowed down due to possible cancels.
2328                          */
2329                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2330                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2331                                           policy, res_id, dlmflags,
2332                                           &info->mti_exp->exp_handle.h_cookie);
2333                         if (unlikely(rc))
2334                                 RETURN(rc);
2335                 }
2336
2337                 /*
2338                  * Finish res_id initializing by name hash marking part of
2339                  * directory which is taking modification.
2340                  */
2341                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2342         }
2343
2344         policy->l_inodebits.bits = ibits;
2345
2346         /*
2347          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2348          * going to be sent to client. If it is - mdt_intent_policy() path will
2349          * fix it up and turn FL_LOCAL flag off.
2350          */
2351         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2352                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2353                           &info->mti_exp->exp_handle.h_cookie);
2354         if (rc)
2355                 mdt_object_unlock(info, o, lh, 1);
2356         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2357                  lh->mlh_pdo_hash != 0 &&
2358                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2359                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2360         }
2361
2362         RETURN(rc);
2363 }
2364
2365 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2366                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2367 {
2368         return mdt_object_lock0(info, o, lh, ibits, false, locality);
2369 }
2370
2371 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2372                         struct mdt_lock_handle *lh, __u64 ibits, int locality)
2373 {
2374         struct mdt_lock_handle tmp = *lh;
2375         int rc;
2376
2377         rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2378         if (rc == 0)
2379                 *lh = tmp;
2380
2381         return rc == 0;
2382 }
2383
2384 /**
2385  * Save a lock within request object.
2386  *
2387  * Keep the lock referenced until whether client ACK or transaction
2388  * commit happens or release the lock immediately depending on input
2389  * parameters. If COS is ON, a write lock is converted to COS lock
2390  * before saving.
2391  *
2392  * \param info thead info object
2393  * \param h lock handle
2394  * \param mode lock mode
2395  * \param decref force immediate lock releasing
2396  */
2397 static
2398 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2399                    ldlm_mode_t mode, int decref)
2400 {
2401         ENTRY;
2402
2403         if (lustre_handle_is_used(h)) {
2404                 if (decref || !info->mti_has_trans ||
2405                     !(mode & (LCK_PW | LCK_EX))){
2406                         mdt_fid_unlock(h, mode);
2407                 } else {
2408                         struct mdt_device *mdt = info->mti_mdt;
2409                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2410                         struct ptlrpc_request *req = mdt_info_req(info);
2411                         int no_ack = 0;
2412
2413                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2414                                  h->cookie);
2415                         /* there is no request if mdt_object_unlock() is called
2416                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2417                         if (likely(req != NULL)) {
2418                                 CDEBUG(D_HA, "request = %p reply state = %p"
2419                                        " transno = "LPD64"\n", req,
2420                                        req->rq_reply_state, req->rq_transno);
2421                                 if (mdt_cos_is_enabled(mdt)) {
2422                                         no_ack = 1;
2423                                         ldlm_lock_downgrade(lock, LCK_COS);
2424                                         mode = LCK_COS;
2425                                 }
2426                                 ptlrpc_save_lock(req, h, mode, no_ack);
2427                         } else {
2428                                 ldlm_lock_decref(h, mode);
2429                         }
2430                         if (mdt_is_lock_sync(lock)) {
2431                                 CDEBUG(D_HA, "found sync-lock,"
2432                                        " async commit started\n");
2433                                 mdt_device_commit_async(info->mti_env,
2434                                                         mdt);
2435                         }
2436                         LDLM_LOCK_PUT(lock);
2437                 }
2438                 h->cookie = 0ull;
2439         }
2440
2441         EXIT;
2442 }
2443
2444 /**
2445  * Unlock mdt object.
2446  *
2447  * Immeditely release the regular lock and the PDO lock or save the
2448  * lock in reqeuest and keep them referenced until client ACK or
2449  * transaction commit.
2450  *
2451  * \param info thread info object
2452  * \param o mdt object
2453  * \param lh mdt lock handle referencing regular and PDO locks
2454  * \param decref force immediate lock releasing
2455  */
2456 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2457                        struct mdt_lock_handle *lh, int decref)
2458 {
2459         ENTRY;
2460
2461         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2462         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2463
2464         if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2465                 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2466
2467         EXIT;
2468 }
2469
2470 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2471                                         const struct lu_fid *f,
2472                                         struct mdt_lock_handle *lh,
2473                                         __u64 ibits)
2474 {
2475         struct mdt_object *o;
2476
2477         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2478         if (!IS_ERR(o)) {
2479                 int rc;
2480
2481                 rc = mdt_object_lock(info, o, lh, ibits,
2482                                      MDT_LOCAL_LOCK);
2483                 if (rc != 0) {
2484                         mdt_object_put(info->mti_env, o);
2485                         o = ERR_PTR(rc);
2486                 }
2487         }
2488         return o;
2489 }
2490
2491 void mdt_object_unlock_put(struct mdt_thread_info * info,
2492                            struct mdt_object * o,
2493                            struct mdt_lock_handle *lh,
2494                            int decref)
2495 {
2496         mdt_object_unlock(info, o, lh, decref);
2497         mdt_object_put(info->mti_env, o);
2498 }
2499
2500 /*
2501  * Generic code handling requests that have struct mdt_body passed in:
2502  *
2503  *  - extract mdt_body from request and save it in @info, if present;
2504  *
2505  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2506  *  @info;
2507  *
2508  *  - if HABEO_CORPUS flag is set for this request type check whether object
2509  *  actually exists on storage (lu_object_exists()).
2510  *
2511  */
2512 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2513 {
2514         const struct mdt_body    *body;
2515         struct mdt_object        *obj;
2516         const struct lu_env      *env;
2517         struct req_capsule       *pill;
2518         int                       rc;
2519         ENTRY;
2520
2521         env = info->mti_env;
2522         pill = info->mti_pill;
2523
2524         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2525         if (body == NULL)
2526                 RETURN(-EFAULT);
2527
2528         if (!(body->valid & OBD_MD_FLID))
2529                 RETURN(0);
2530
2531         if (!fid_is_sane(&body->fid1)) {
2532                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2533                 RETURN(-EINVAL);
2534         }
2535
2536         /*
2537          * Do not get size or any capa fields before we check that request
2538          * contains capa actually. There are some requests which do not, for
2539          * instance MDS_IS_SUBDIR.
2540          */
2541         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2542             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2543                 mdt_set_capainfo(info, 0, &body->fid1,
2544                                  req_capsule_client_get(pill, &RMF_CAPA1));
2545
2546         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2547         if (!IS_ERR(obj)) {
2548                 if ((flags & HABEO_CORPUS) &&
2549                     !mdt_object_exists(obj)) {
2550                         mdt_object_put(env, obj);
2551                         /* for capability renew ENOENT will be handled in
2552                          * mdt_renew_capa */
2553                         if (body->valid & OBD_MD_FLOSSCAPA)
2554                                 rc = 0;
2555                         else
2556                                 rc = -ENOENT;
2557                 } else {
2558                         info->mti_object = obj;
2559                         rc = 0;
2560                 }
2561         } else
2562                 rc = PTR_ERR(obj);
2563
2564         RETURN(rc);
2565 }
2566
2567 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2568 {
2569         struct req_capsule *pill = info->mti_pill;
2570         int rc;
2571         ENTRY;
2572
2573         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2574                 rc = mdt_body_unpack(info, flags);
2575         else
2576                 rc = 0;
2577
2578         if (rc == 0 && (flags & HABEO_REFERO)) {
2579                 /* Pack reply. */
2580                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2581                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2582                                              info->mti_body->eadatasize);
2583                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2584                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2585                                              RCL_SERVER, 0);
2586
2587                 rc = req_capsule_server_pack(pill);
2588         }
2589         RETURN(rc);
2590 }
2591
2592 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2593 {
2594         struct md_device *next = m->mdt_child;
2595
2596         return next->md_ops->mdo_init_capa_ctxt(env, next,
2597                                                 m->mdt_lut.lut_mds_capa,
2598                                                 m->mdt_capa_timeout,
2599                                                 m->mdt_capa_alg,
2600                                                 m->mdt_capa_keys);
2601 }
2602
2603 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2604 {
2605         lh->mlh_type = MDT_NUL_LOCK;
2606         lh->mlh_reg_lh.cookie = 0ull;
2607         lh->mlh_reg_mode = LCK_MINMODE;
2608         lh->mlh_pdo_lh.cookie = 0ull;
2609         lh->mlh_pdo_mode = LCK_MINMODE;
2610         lh->mlh_rreg_lh.cookie = 0ull;
2611         lh->mlh_rreg_mode = LCK_MINMODE;
2612 }
2613
2614 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2615 {
2616         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2617         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2618 }
2619
2620 /*
2621  * Initialize fields of struct mdt_thread_info. Other fields are left in
2622  * uninitialized state, because it's too expensive to zero out whole
2623  * mdt_thread_info (> 1K) on each request arrival.
2624  */
2625 void mdt_thread_info_init(struct ptlrpc_request *req,
2626                           struct mdt_thread_info *info)
2627 {
2628         int i;
2629
2630         info->mti_pill = &req->rq_pill;
2631
2632         /* lock handle */
2633         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2634                 mdt_lock_handle_init(&info->mti_lh[i]);
2635
2636         /* mdt device: it can be NULL while CONNECT */
2637         if (req->rq_export) {
2638                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2639                 info->mti_exp = req->rq_export;
2640         } else
2641                 info->mti_mdt = NULL;
2642         info->mti_env = req->rq_svc_thread->t_env;
2643         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2644
2645         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2646         info->mti_big_buf = LU_BUF_NULL;
2647         info->mti_body = NULL;
2648         info->mti_object = NULL;
2649         info->mti_dlm_req = NULL;
2650         info->mti_has_trans = 0;
2651         info->mti_cross_ref = 0;
2652         info->mti_opdata = 0;
2653         info->mti_big_lmm_used = 0;
2654
2655         /* To not check for split by default. */
2656         info->mti_spec.no_create = 0;
2657         info->mti_spec.sp_rm_entry = 0;
2658 }
2659
2660 void mdt_thread_info_fini(struct mdt_thread_info *info)
2661 {
2662         int i;
2663
2664         if (info->mti_object != NULL) {
2665                 mdt_object_put(info->mti_env, info->mti_object);
2666                 info->mti_object = NULL;
2667         }
2668
2669         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2670                 mdt_lock_handle_fini(&info->mti_lh[i]);
2671         info->mti_env = NULL;
2672         info->mti_pill = NULL;
2673         info->mti_exp = NULL;
2674
2675         if (unlikely(info->mti_big_buf.lb_buf != NULL))
2676                 lu_buf_free(&info->mti_big_buf);
2677 }
2678
2679 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2680 {
2681         struct mdt_thread_info  *mti;
2682         struct lustre_capa      *lc;
2683
2684         mti = mdt_th_info(tsi->tsi_env);
2685         LASSERT(mti != NULL);
2686
2687         mdt_thread_info_init(tgt_ses_req(tsi), mti);
2688         if (tsi->tsi_corpus != NULL) {
2689                 struct req_capsule *pill = tsi->tsi_pill;
2690
2691                 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2692                 lu_object_get(tsi->tsi_corpus);
2693
2694                 /*
2695                  * XXX: must be part of tgt_mdt_body_unpack but moved here
2696                  * due to mdt_set_capainfo().
2697                  */
2698                 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2699                     req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2700                         lc = req_capsule_client_get(pill, &RMF_CAPA1);
2701                         mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2702                 }
2703         }
2704         mti->mti_body = tsi->tsi_mdt_body;
2705         mti->mti_dlm_req = tsi->tsi_dlm_req;
2706
2707         return mti;
2708 }
2709
2710 int mdt_tgt_connect(struct tgt_session_info *tsi)
2711 {
2712         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2713         int                      rc;
2714
2715         ENTRY;
2716
2717         rc = tgt_connect(tsi);
2718         if (rc != 0)
2719                 RETURN(rc);
2720
2721         rc = mdt_init_idmap(tsi);
2722         if (rc != 0)
2723                 GOTO(err, rc);
2724         RETURN(0);
2725 err:
2726         obd_disconnect(class_export_get(req->rq_export));
2727         return rc;
2728 }
2729
2730 enum mdt_it_code {
2731         MDT_IT_OPEN,
2732         MDT_IT_OCREAT,
2733         MDT_IT_CREATE,
2734         MDT_IT_GETATTR,
2735         MDT_IT_READDIR,
2736         MDT_IT_LOOKUP,
2737         MDT_IT_UNLINK,
2738         MDT_IT_TRUNC,
2739         MDT_IT_GETXATTR,
2740         MDT_IT_LAYOUT,
2741         MDT_IT_QUOTA,
2742         MDT_IT_NR
2743 };
2744
2745 static int mdt_intent_getattr(enum mdt_it_code opcode,
2746                               struct mdt_thread_info *info,
2747                               struct ldlm_lock **,
2748                               __u64);
2749
2750 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2751                                 struct mdt_thread_info *info,
2752                                 struct ldlm_lock **lockp,
2753                                 __u64 flags);
2754
2755 static int mdt_intent_layout(enum mdt_it_code opcode,
2756                              struct mdt_thread_info *info,
2757                              struct ldlm_lock **,
2758                              __u64);
2759 static int mdt_intent_reint(enum mdt_it_code opcode,
2760                             struct mdt_thread_info *info,
2761                             struct ldlm_lock **,
2762                             __u64);
2763
2764 static struct mdt_it_flavor {
2765         const struct req_format *it_fmt;
2766         __u32                    it_flags;
2767         int                    (*it_act)(enum mdt_it_code ,
2768                                          struct mdt_thread_info *,
2769                                          struct ldlm_lock **,
2770                                          __u64);
2771         long                     it_reint;
2772 } mdt_it_flavor[] = {
2773         [MDT_IT_OPEN]     = {
2774                 .it_fmt   = &RQF_LDLM_INTENT,
2775                 /*.it_flags = HABEO_REFERO,*/
2776                 .it_flags = 0,
2777                 .it_act   = mdt_intent_reint,
2778                 .it_reint = REINT_OPEN
2779         },
2780         [MDT_IT_OCREAT]   = {
2781                 .it_fmt   = &RQF_LDLM_INTENT,
2782                 /*
2783                  * OCREAT is not a MUTABOR request as if the file
2784                  * already exists.
2785                  * We do the extra check of OBD_CONNECT_RDONLY in
2786                  * mdt_reint_open() when we really need to create
2787                  * the object.
2788                  */
2789                 .it_flags = 0,
2790                 .it_act   = mdt_intent_reint,
2791                 .it_reint = REINT_OPEN
2792         },
2793         [MDT_IT_CREATE]   = {
2794                 .it_fmt   = &RQF_LDLM_INTENT,
2795                 .it_flags = MUTABOR,
2796                 .it_act   = mdt_intent_reint,
2797                 .it_reint = REINT_CREATE
2798         },
2799         [MDT_IT_GETATTR]  = {
2800                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2801                 .it_flags = HABEO_REFERO,
2802                 .it_act   = mdt_intent_getattr
2803         },
2804         [MDT_IT_READDIR]  = {
2805                 .it_fmt   = NULL,
2806                 .it_flags = 0,
2807                 .it_act   = NULL
2808         },
2809         [MDT_IT_LOOKUP]   = {
2810                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2811                 .it_flags = HABEO_REFERO,
2812                 .it_act   = mdt_intent_getattr
2813         },
2814         [MDT_IT_UNLINK]   = {
2815                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2816                 .it_flags = MUTABOR,
2817                 .it_act   = NULL,
2818                 .it_reint = REINT_UNLINK
2819         },
2820         [MDT_IT_TRUNC]    = {
2821                 .it_fmt   = NULL,
2822                 .it_flags = MUTABOR,
2823                 .it_act   = NULL
2824         },
2825         [MDT_IT_GETXATTR] = {
2826                 .it_fmt   = &RQF_LDLM_INTENT_GETXATTR,
2827                 .it_flags = HABEO_CORPUS,
2828                 .it_act   = mdt_intent_getxattr
2829         },
2830         [MDT_IT_LAYOUT] = {
2831                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
2832                 .it_flags = 0,
2833                 .it_act   = mdt_intent_layout
2834         }
2835 };
2836
2837 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2838                             struct ldlm_lock **lockp,
2839                             struct ldlm_lock *new_lock,
2840                             struct mdt_lock_handle *lh,
2841                             __u64 flags)
2842 {
2843         struct ptlrpc_request  *req = mdt_info_req(info);
2844         struct ldlm_lock       *lock = *lockp;
2845
2846         /*
2847          * Get new lock only for cases when possible resent did not find any
2848          * lock.
2849          */
2850         if (new_lock == NULL)
2851                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2852
2853         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2854                 lh->mlh_reg_lh.cookie = 0;
2855                 RETURN(0);
2856         }
2857
2858         LASSERTF(new_lock != NULL,
2859                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2860
2861         /*
2862          * If we've already given this lock to a client once, then we should
2863          * have no readers or writers.  Otherwise, we should have one reader
2864          * _or_ writer ref (which will be zeroed below) before returning the
2865          * lock to a client.
2866          */
2867         if (new_lock->l_export == req->rq_export) {
2868                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2869         } else {
2870                 LASSERT(new_lock->l_export == NULL);
2871                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2872         }
2873
2874         *lockp = new_lock;
2875
2876         if (new_lock->l_export == req->rq_export) {
2877                 /*
2878                  * Already gave this to the client, which means that we
2879                  * reconstructed a reply.
2880                  */
2881                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2882                         MSG_RESENT);
2883                 lh->mlh_reg_lh.cookie = 0;
2884                 RETURN(ELDLM_LOCK_REPLACED);
2885         }
2886
2887         /*
2888          * Fixup the lock to be given to the client.
2889          */
2890         lock_res_and_lock(new_lock);
2891         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2892          * possible blocking AST. */
2893         while (new_lock->l_readers > 0) {
2894                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2895                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2896                 new_lock->l_readers--;
2897         }
2898         while (new_lock->l_writers > 0) {
2899                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2900                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2901                 new_lock->l_writers--;
2902         }
2903
2904         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2905         new_lock->l_blocking_ast = lock->l_blocking_ast;
2906         new_lock->l_completion_ast = lock->l_completion_ast;
2907         new_lock->l_remote_handle = lock->l_remote_handle;
2908         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2909
2910         unlock_res_and_lock(new_lock);
2911
2912         cfs_hash_add(new_lock->l_export->exp_lock_hash,
2913                      &new_lock->l_remote_handle,
2914                      &new_lock->l_exp_hash);
2915
2916         LDLM_LOCK_RELEASE(new_lock);
2917         lh->mlh_reg_lh.cookie = 0;
2918
2919         RETURN(ELDLM_LOCK_REPLACED);
2920 }
2921
2922 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2923                                     struct ldlm_lock *new_lock,
2924                                     struct ldlm_lock **old_lock,
2925                                     struct mdt_lock_handle *lh)
2926 {
2927         struct ptlrpc_request  *req = mdt_info_req(info);
2928         struct obd_export      *exp = req->rq_export;
2929         struct lustre_handle    remote_hdl;
2930         struct ldlm_request    *dlmreq;
2931         struct ldlm_lock       *lock;
2932
2933         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2934                 return;
2935
2936         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2937         remote_hdl = dlmreq->lock_handle[0];
2938
2939         /* In the function below, .hs_keycmp resolves to
2940          * ldlm_export_lock_keycmp() */
2941         /* coverity[overrun-buffer-val] */
2942         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2943         if (lock) {
2944                 if (lock != new_lock) {
2945                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2946                         lh->mlh_reg_mode = lock->l_granted_mode;
2947
2948                         LDLM_DEBUG(lock, "Restoring lock cookie");
2949                         DEBUG_REQ(D_DLMTRACE, req,
2950                                   "restoring lock cookie "LPX64,
2951                                   lh->mlh_reg_lh.cookie);
2952                         if (old_lock)
2953                                 *old_lock = LDLM_LOCK_GET(lock);
2954                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2955                         return;
2956                 }
2957
2958                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2959         }
2960
2961         /*
2962          * If the xid matches, then we know this is a resent request, and allow
2963          * it. (It's probably an OPEN, for which we don't send a lock.
2964          */
2965         if (req_xid_is_last(req))
2966                 return;
2967
2968         /*
2969          * This remote handle isn't enqueued, so we never received or processed
2970          * this request.  Clear MSG_RESENT, because it can be handled like any
2971          * normal request now.
2972          */
2973         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2974
2975         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2976                   remote_hdl.cookie);
2977 }
2978
2979 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2980                                 struct mdt_thread_info *info,
2981                                 struct ldlm_lock **lockp,
2982                                 __u64 flags)
2983 {
2984         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2985         struct ldlm_reply      *ldlm_rep = NULL;
2986         int rc, grc;
2987
2988         /*
2989          * Initialize lhc->mlh_reg_lh either from a previously granted lock
2990          * (for the resend case) or a new lock. Below we will use it to
2991          * replace the original lock.
2992          */
2993         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2994         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2995                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
2996                 rc = mdt_object_lock(info, info->mti_object, lhc,
2997                                         MDS_INODELOCK_XATTR,
2998                                         MDT_LOCAL_LOCK);
2999                 if (rc)
3000                         return rc;
3001         }
3002
3003         grc = mdt_getxattr(info);
3004
3005         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3006
3007         if (mdt_info_req(info)->rq_repmsg != NULL)
3008                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3009         if (ldlm_rep == NULL)
3010                 RETURN(err_serious(-EFAULT));
3011
3012         ldlm_rep->lock_policy_res2 = grc;
3013
3014         return rc;
3015 }
3016
3017 static int mdt_intent_getattr(enum mdt_it_code opcode,
3018                               struct mdt_thread_info *info,
3019                               struct ldlm_lock **lockp,
3020                               __u64 flags)
3021 {
3022         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3023         struct ldlm_lock       *new_lock = NULL;
3024         __u64                   child_bits;
3025         struct ldlm_reply      *ldlm_rep;
3026         struct ptlrpc_request  *req;
3027         struct mdt_body        *reqbody;
3028         struct mdt_body        *repbody;
3029         int                     rc, rc2;
3030         ENTRY;
3031
3032         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3033         LASSERT(reqbody);
3034
3035         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3036         LASSERT(repbody);
3037
3038         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3039         repbody->eadatasize = 0;
3040         repbody->aclsize = 0;
3041
3042         switch (opcode) {
3043         case MDT_IT_LOOKUP:
3044                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3045                 break;
3046         case MDT_IT_GETATTR:
3047                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3048                              MDS_INODELOCK_PERM;
3049                 break;
3050         default:
3051                 CERROR("Unsupported intent (%d)\n", opcode);
3052                 GOTO(out_shrink, rc = -EINVAL);
3053         }
3054
3055         rc = mdt_init_ucred(info, reqbody);
3056         if (rc)
3057                 GOTO(out_shrink, rc);
3058
3059         req = info->mti_pill->rc_req;
3060         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3061         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3062
3063         /* Get lock from request for possible resent case. */
3064         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3065
3066         rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3067         ldlm_rep->lock_policy_res2 = clear_serious(rc);
3068
3069         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3070                 ldlm_rep->lock_policy_res2 = 0;
3071         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3072             ldlm_rep->lock_policy_res2) {
3073                 lhc->mlh_reg_lh.cookie = 0ull;
3074                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3075         }
3076
3077         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3078         EXIT;
3079 out_ucred:
3080         mdt_exit_ucred(info);
3081 out_shrink:
3082         mdt_client_compatibility(info);
3083         rc2 = mdt_fix_reply(info);
3084         if (rc == 0)
3085                 rc = rc2;
3086         return rc;
3087 }
3088
3089 static int mdt_intent_layout(enum mdt_it_code opcode,
3090                              struct mdt_thread_info *info,
3091                              struct ldlm_lock **lockp,
3092                              __u64 flags)
3093 {
3094         struct layout_intent *layout;
3095         struct lu_fid *fid;
3096         struct mdt_object *obj = NULL;
3097         struct md_object *child = NULL;
3098         int rc;
3099         ENTRY;
3100
3101         if (opcode != MDT_IT_LAYOUT) {
3102                 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3103                         opcode);
3104                 RETURN(-EINVAL);
3105         }
3106
3107         fid = &info->mti_tmp_fid2;
3108         fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3109
3110         obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3111         if (IS_ERR(obj))
3112                 RETURN(PTR_ERR(obj));
3113
3114         if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3115                 child = mdt_object_child(obj);
3116
3117                 /* get the length of lsm */
3118                 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3119                                   XATTR_NAME_LOV);
3120
3121                 if (rc > info->mti_mdt->mdt_max_mdsize)
3122                         info->mti_mdt->mdt_max_mdsize = rc;
3123         }
3124
3125         mdt_object_put(info->mti_env, obj);
3126
3127         (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3128         req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3129                         ldlm_lvbo_size(*lockp));
3130         rc = req_capsule_server_pack(info->mti_pill);
3131         if (rc != 0)
3132                 RETURN(-EINVAL);
3133
3134         layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3135         LASSERT(layout != NULL);
3136         if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3137                 /* return to normal ldlm handling */
3138                 RETURN(0);
3139
3140         CERROR("%s: Unsupported layout intent (%d)\n",
3141                 mdt_obd_name(info->mti_mdt), layout->li_opc);
3142         RETURN(-EINVAL);
3143 }
3144
3145 static int mdt_intent_reint(enum mdt_it_code opcode,
3146                             struct mdt_thread_info *info,
3147                             struct ldlm_lock **lockp,
3148                             __u64 flags)
3149 {
3150         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3151         struct ldlm_reply      *rep = NULL;
3152         long                    opc;
3153         int                     rc;
3154
3155         static const struct req_format *intent_fmts[REINT_MAX] = {
3156                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3157                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3158         };
3159
3160         ENTRY;
3161
3162         opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3163         if (opc < 0)
3164                 RETURN(opc);
3165
3166         if (mdt_it_flavor[opcode].it_reint != opc) {
3167                 CERROR("Reint code %ld doesn't match intent: %d\n",
3168                        opc, opcode);
3169                 RETURN(err_serious(-EPROTO));
3170         }
3171
3172         /* Get lock from request for possible resent case. */
3173         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3174
3175         rc = mdt_reint_internal(info, lhc, opc);
3176
3177         /* Check whether the reply has been packed successfully. */
3178         if (mdt_info_req(info)->rq_repmsg != NULL)
3179                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3180         if (rep == NULL)
3181                 RETURN(err_serious(-EFAULT));
3182
3183         /* MDC expects this in any case */
3184         if (rc != 0)
3185                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3186
3187         /* the open lock or the lock for cross-ref object should be
3188          * returned to the client */
3189         if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3190                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3191                 rep->lock_policy_res2 = 0;
3192                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3193                 RETURN(rc);