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