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