Whamcloud - gitweb
LU-13437 mdt: rename misses remote LOOKUP lock revoke
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_handler.c
33  *
34  * Lustre Metadata Target (mdt) request handler
35  *
36  * Author: Peter Braam <braam@clusterfs.com>
37  * Author: Andreas Dilger <adilger@clusterfs.com>
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Mike Shaver <shaver@clusterfs.com>
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  * Author: Huang Hua <huanghua@clusterfs.com>
42  * Author: Yury Umanets <umka@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
49
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
61 #include <lustre_lmv.h>
62 #include <obd.h>
63 #include <obd_support.h>
64 #include <lustre_barrier.h>
65 #include <obd_cksum.h>
66 #include <llog_swab.h>
67
68 #include "mdt_internal.h"
69
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
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 enum ldlm_mode 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
98 static const struct lu_object_operations mdt_obj_ops;
99
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
102
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
105
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
108
109 static struct lu_kmem_descr mdt_caches[] = {
110         {
111                 .ckd_cache = &mdt_object_kmem,
112                 .ckd_name  = "mdt_obj",
113                 .ckd_size  = sizeof(struct mdt_object)
114         },
115         {
116                 .ckd_cache      = &mdt_hsm_cdt_kmem,
117                 .ckd_name       = "mdt_cdt_restore_handle",
118                 .ckd_size       = sizeof(struct cdt_restore_handle)
119         },
120         {
121                 .ckd_cache      = &mdt_hsm_car_kmem,
122                 .ckd_name       = "mdt_cdt_agent_req",
123                 .ckd_size       = sizeof(struct cdt_agent_req)
124         },
125         {
126                 .ckd_cache = NULL
127         }
128 };
129
130 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
131 {
132         if (!rep)
133                 return 0;
134         return rep->lock_policy_res1 & op_flag;
135 }
136
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138                            struct ldlm_reply *rep, __u64 op_flag)
139 {
140         if (info) {
141                 info->mti_opdata &= ~op_flag;
142                 tgt_opdata_clear(info->mti_env, op_flag);
143         }
144         if (rep)
145                 rep->lock_policy_res1 &= ~op_flag;
146 }
147
148 void mdt_set_disposition(struct mdt_thread_info *info,
149                          struct ldlm_reply *rep, __u64 op_flag)
150 {
151         if (info) {
152                 info->mti_opdata |= op_flag;
153                 tgt_opdata_set(info->mti_env, op_flag);
154         }
155         if (rep)
156                 rep->lock_policy_res1 |= op_flag;
157 }
158
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
160 {
161         lh->mlh_pdo_hash = 0;
162         lh->mlh_reg_mode = lm;
163         lh->mlh_rreg_mode = lm;
164         lh->mlh_type = MDT_REG_LOCK;
165 }
166
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
168                        const struct lu_name *lname)
169 {
170         lh->mlh_reg_mode = lock_mode;
171         lh->mlh_pdo_mode = LCK_MINMODE;
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 = ll_full_name_hash(NULL, 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_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
268                               struct lu_fid *fid)
269 {
270         struct mdt_device *mdt = info->mti_mdt;
271         struct lu_name *lname = &info->mti_name;
272         char *filename = info->mti_filename;
273         struct mdt_object *parent;
274         u32 mode;
275         int rc = 0;
276
277         LASSERT(!info->mti_cross_ref);
278
279         /*
280          * We may want to allow this to mount a completely separate
281          * fileset from the MDT in the future, but keeping it to
282          * ROOT/ only for now avoid potential security issues.
283          */
284         *fid = mdt->mdt_md_root_fid;
285
286         while (rc == 0 && fileset != NULL && *fileset != '\0') {
287                 const char *s1 = fileset;
288                 const char *s2;
289
290                 while (*++s1 == '/')
291                         ;
292                 s2 = s1;
293                 while (*s2 != '/' && *s2 != '\0')
294                         s2++;
295
296                 if (s2 == s1)
297                         break;
298
299                 fileset = s2;
300
301                 lname->ln_namelen = s2 - s1;
302                 if (lname->ln_namelen > NAME_MAX) {
303                         rc = -EINVAL;
304                         break;
305                 }
306
307                 /* reject .. as a path component */
308                 if (lname->ln_namelen == 2 &&
309                     strncmp(s1, "..", 2) == 0) {
310                         rc = -EINVAL;
311                         break;
312                 }
313
314                 strncpy(filename, s1, lname->ln_namelen);
315                 filename[lname->ln_namelen] = '\0';
316                 lname->ln_name = filename;
317
318                 parent = mdt_object_find(info->mti_env, mdt, fid);
319                 if (IS_ERR(parent)) {
320                         rc = PTR_ERR(parent);
321                         break;
322                 }
323                 /* Only got the fid of this obj by name */
324                 fid_zero(fid);
325                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
326                                 fid, &info->mti_spec);
327                 mdt_object_put(info->mti_env, parent);
328         }
329         if (!rc) {
330                 parent = mdt_object_find(info->mti_env, mdt, fid);
331                 if (IS_ERR(parent))
332                         rc = PTR_ERR(parent);
333                 else {
334                         mode = lu_object_attr(&parent->mot_obj);
335                         mdt_object_put(info->mti_env, parent);
336                         if (!S_ISDIR(mode))
337                                 rc = -ENOTDIR;
338                 }
339         }
340
341         return rc;
342 }
343
344 static int mdt_get_root(struct tgt_session_info *tsi)
345 {
346         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
347         struct mdt_device       *mdt = info->mti_mdt;
348         struct mdt_body         *repbody;
349         char                    *fileset = NULL, *buffer = NULL;
350         int                      rc;
351         struct obd_export       *exp = info->mti_exp;
352         char                    *nodemap_fileset;
353
354         ENTRY;
355
356         rc = mdt_check_ucred(info);
357         if (rc)
358                 GOTO(out, rc = err_serious(rc));
359
360         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
361                 GOTO(out, rc = err_serious(-ENOMEM));
362
363         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
364         if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
365                 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
366                 if (fileset == NULL)
367                         GOTO(out, rc = err_serious(-EFAULT));
368         }
369
370         nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
371         if (nodemap_fileset && nodemap_fileset[0]) {
372                 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
373                 if (fileset) {
374                         /* consider fileset from client as a sub-fileset
375                          * of the nodemap one */
376                         OBD_ALLOC(buffer, PATH_MAX + 1);
377                         if (buffer == NULL)
378                                 GOTO(out, rc = err_serious(-ENOMEM));
379                         if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
380                                      nodemap_fileset, fileset) >= PATH_MAX + 1)
381                                 GOTO(out, rc = err_serious(-EINVAL));
382                         fileset = buffer;
383                 } else {
384                         /* enforce fileset as specified in the nodemap */
385                         fileset = nodemap_fileset;
386                 }
387         }
388
389         if (fileset) {
390                 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
391                 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
392                 if (rc < 0)
393                         GOTO(out, rc = err_serious(rc));
394         } else {
395                 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
396         }
397         repbody->mbo_valid |= OBD_MD_FLID;
398
399         EXIT;
400 out:
401         mdt_thread_info_fini(info);
402         if (buffer)
403                 OBD_FREE(buffer, PATH_MAX+1);
404         return rc;
405 }
406
407 static int mdt_statfs(struct tgt_session_info *tsi)
408 {
409         struct ptlrpc_request *req = tgt_ses_req(tsi);
410         struct mdt_thread_info *info = tsi2mdt_info(tsi);
411         struct mdt_device *mdt = info->mti_mdt;
412         struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
413         struct md_device *next = mdt->mdt_child;
414         struct ptlrpc_service_part *svcpt;
415         struct obd_statfs *osfs;
416         struct mdt_body *reqbody = NULL;
417         struct mdt_statfs_cache *msf;
418         ktime_t kstart = ktime_get();
419         int rc;
420
421         ENTRY;
422
423         svcpt = req->rq_rqbd->rqbd_svcpt;
424
425         /* This will trigger a watchdog timeout */
426         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
427                          (MDT_SERVICE_WATCHDOG_FACTOR *
428                           at_get(&svcpt->scp_at_estimate)) + 1);
429
430         rc = mdt_check_ucred(info);
431         if (rc)
432                 GOTO(out, rc = err_serious(rc));
433
434         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
435                 GOTO(out, rc = err_serious(-ENOMEM));
436
437         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
438         if (!osfs)
439                 GOTO(out, rc = -EPROTO);
440
441         if (mdt_is_sum_statfs_client(req->rq_export) &&
442                 lustre_packed_msg_size(req->rq_reqmsg) ==
443                 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
444                                      &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
445                 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
446                 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
447         }
448
449         if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
450                 msf = &mdt->mdt_sum_osfs;
451         else
452                 msf = &mdt->mdt_osfs;
453
454         if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
455                         /** statfs data is too old, get up-to-date one */
456                         if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
457                                 rc = next->md_ops->mdo_statfs(info->mti_env,
458                                                               next, osfs);
459                         else
460                                 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
461                                                osfs);
462                         if (rc)
463                                 GOTO(out, rc);
464                         spin_lock(&mdt->mdt_lock);
465                         msf->msf_osfs = *osfs;
466                         msf->msf_age = ktime_get_seconds();
467                         spin_unlock(&mdt->mdt_lock);
468         } else {
469                         /** use cached statfs data */
470                         spin_lock(&mdt->mdt_lock);
471                         *osfs = msf->msf_osfs;
472                         spin_unlock(&mdt->mdt_lock);
473         }
474
475         /* at least try to account for cached pages.  its still racy and
476          * might be under-reporting if clients haven't announced their
477          * caches with brw recently */
478         CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
479                " pending %llu free %llu avail %llu\n",
480                tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
481                tgd->tgd_tot_pending,
482                osfs->os_bfree << tgd->tgd_blockbits,
483                osfs->os_bavail << tgd->tgd_blockbits);
484
485         osfs->os_bavail -= min_t(u64, osfs->os_bavail,
486                                  ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
487                                    osfs->os_bsize - 1) >> tgd->tgd_blockbits));
488
489         tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
490         CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
491                "%llu objects: %llu free; state %x\n",
492                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
493                osfs->os_files, osfs->os_ffree, osfs->os_state);
494
495         if (!exp_grant_param_supp(tsi->tsi_exp) &&
496             tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
497                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
498                  * should not see a block size > page size, otherwise
499                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
500                  * block size which is the biggest block size known to work
501                  * with all client's page size. */
502                 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
503                 osfs->os_bfree  <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
504                 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
505                 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
506         }
507         if (rc == 0)
508                 mdt_counter_incr(req, LPROC_MDT_STATFS,
509                                  ktime_us_delta(ktime_get(), kstart));
510 out:
511         mdt_thread_info_fini(info);
512         RETURN(rc);
513 }
514
515 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
516 {
517         struct lov_comp_md_v1 *comp_v1;
518         struct lov_mds_md *v1;
519         __u32 off;
520         __u32 dom_stripesize = 0;
521         int i;
522         bool has_ost_stripes = false;
523
524         ENTRY;
525
526         if (is_dom_only)
527                 *is_dom_only = 0;
528
529         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
530                 RETURN(0);
531
532         comp_v1 = (struct lov_comp_md_v1 *)lmm;
533         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
534         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
535
536         /* Fast check for DoM entry with no mirroring, should be the first */
537         if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
538             lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
539                 RETURN(0);
540
541         /* check all entries otherwise */
542         for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
543                 struct lov_comp_md_entry_v1 *lcme;
544
545                 lcme = &comp_v1->lcm_entries[i];
546                 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
547                         continue;
548
549                 off = le32_to_cpu(lcme->lcme_offset);
550                 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
551
552                 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
553                     LOV_PATTERN_MDT)
554                         dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
555                 else
556                         has_ost_stripes = true;
557
558                 if (dom_stripesize && has_ost_stripes)
559                         RETURN(dom_stripesize);
560         }
561         /* DoM-only case exits here */
562         if (is_dom_only && dom_stripesize)
563                 *is_dom_only = 1;
564         RETURN(dom_stripesize);
565 }
566
567 /**
568  * Pack size attributes into the reply.
569  */
570 int mdt_pack_size2body(struct mdt_thread_info *info,
571                         const struct lu_fid *fid, struct lustre_handle *lh)
572 {
573         struct mdt_body *b;
574         struct md_attr *ma = &info->mti_attr;
575         __u32 dom_stripe;
576         bool dom_lock = false;
577
578         ENTRY;
579
580         LASSERT(ma->ma_attr.la_valid & LA_MODE);
581
582         if (!S_ISREG(ma->ma_attr.la_mode) ||
583             !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
584                 RETURN(-ENODATA);
585
586         dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
587         /* no DoM stripe, no size in reply */
588         if (!dom_stripe)
589                 RETURN(-ENOENT);
590
591         if (lustre_handle_is_used(lh)) {
592                 struct ldlm_lock *lock;
593
594                 lock = ldlm_handle2lock(lh);
595                 if (lock != NULL) {
596                         dom_lock = ldlm_has_dom(lock);
597                         LDLM_LOCK_PUT(lock);
598                 }
599         }
600
601         /* no DoM lock, no size in reply */
602         if (!dom_lock)
603                 RETURN(0);
604
605         /* Either DoM lock exists or LMM has only DoM stripe then
606          * return size on body. */
607         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
608
609         mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
610         RETURN(0);
611 }
612
613 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
614 /*
615  * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
616  *
617  * \param       info    thread info object
618  * \param       repbody reply to pack ACLs into
619  * \param       o       mdt object of file to examine
620  * \param       nodemap nodemap of client to reply to
621  * \retval      0       success
622  * \retval      -errno  error getting or parsing ACL from disk
623  */
624 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
625                       struct mdt_object *o, struct lu_nodemap *nodemap)
626 {
627         const struct lu_env     *env = info->mti_env;
628         struct md_object        *next = mdt_object_child(o);
629         struct lu_buf           *buf = &info->mti_buf;
630         struct mdt_device       *mdt = info->mti_mdt;
631         struct req_capsule *pill = info->mti_pill;
632         int rc;
633
634         ENTRY;
635
636         buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
637         buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
638         if (buf->lb_len == 0)
639                 RETURN(0);
640
641 again:
642         rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
643         if (rc < 0) {
644                 if (rc == -ENODATA) {
645                         repbody->mbo_aclsize = 0;
646                         repbody->mbo_valid |= OBD_MD_FLACL;
647                         rc = 0;
648                 } else if (rc == -EOPNOTSUPP) {
649                         rc = 0;
650                 } else {
651                         if (rc == -ERANGE &&
652                             exp_connect_large_acl(info->mti_exp) &&
653                             buf->lb_buf != info->mti_big_acl) {
654                                 if (info->mti_big_acl == NULL) {
655                                         info->mti_big_aclsize =
656                                                         min_t(unsigned int,
657                                                               mdt->mdt_max_ea_size,
658                                                               XATTR_SIZE_MAX);
659                                         OBD_ALLOC_LARGE(info->mti_big_acl,
660                                                         info->mti_big_aclsize);
661                                         if (info->mti_big_acl == NULL) {
662                                                 info->mti_big_aclsize = 0;
663                                                 CERROR("%s: unable to grow "
664                                                        DFID" ACL buffer\n",
665                                                        mdt_obd_name(mdt),
666                                                        PFID(mdt_object_fid(o)));
667                                                 RETURN(-ENOMEM);
668                                         }
669                                 }
670
671                                 CDEBUG(D_INODE, "%s: grow the "DFID
672                                        " ACL buffer to size %d\n",
673                                        mdt_obd_name(mdt),
674                                        PFID(mdt_object_fid(o)),
675                                        info->mti_big_aclsize);
676
677                                 buf->lb_buf = info->mti_big_acl;
678                                 buf->lb_len = info->mti_big_aclsize;
679
680                                 goto again;
681                         }
682
683                         CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
684                                mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
685                 }
686         } else {
687                 int client;
688                 int server;
689                 int acl_buflen;
690                 int lmm_buflen = 0;
691                 int lmmsize = 0;
692
693                 acl_buflen = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
694                 if (acl_buflen >= rc)
695                         goto map;
696
697                 /* If LOV/LMA EA is small, we can reuse part of their buffer */
698                 client = ptlrpc_req_get_repsize(pill->rc_req);
699                 server = lustre_packed_msg_size(pill->rc_req->rq_repmsg);
700                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
701                         lmm_buflen = req_capsule_get_size(pill, &RMF_MDT_MD,
702                                                           RCL_SERVER);
703                         lmmsize = repbody->mbo_eadatasize;
704                 }
705
706                 if (client < server - acl_buflen - lmm_buflen + rc + lmmsize) {
707                         CDEBUG(D_INODE, "%s: client prepared buffer size %d "
708                                "is not big enough with the ACL size %d (%d)\n",
709                                mdt_obd_name(mdt), client, rc,
710                                server - acl_buflen - lmm_buflen + rc + lmmsize);
711                         repbody->mbo_aclsize = 0;
712                         repbody->mbo_valid &= ~OBD_MD_FLACL;
713                         RETURN(-ERANGE);
714                 }
715
716 map:
717                 if (buf->lb_buf == info->mti_big_acl)
718                         info->mti_big_acl_used = 1;
719
720                 rc = nodemap_map_acl(nodemap, buf->lb_buf,
721                                      rc, NODEMAP_FS_TO_CLIENT);
722                 /* if all ACLs mapped out, rc is still >= 0 */
723                 if (rc < 0) {
724                         CERROR("%s: nodemap_map_acl unable to parse "DFID
725                                " ACL: rc = %d\n", mdt_obd_name(mdt),
726                                PFID(mdt_object_fid(o)), rc);
727                         repbody->mbo_aclsize = 0;
728                         repbody->mbo_valid &= ~OBD_MD_FLACL;
729                 } else {
730                         repbody->mbo_aclsize = rc;
731                         repbody->mbo_valid |= OBD_MD_FLACL;
732                         rc = 0;
733                 }
734         }
735
736         RETURN(rc);
737 }
738 #endif
739
740 /* XXX Look into layout in MDT layer. */
741 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
742 {
743         struct lov_comp_md_v1   *comp_v1;
744         struct lov_mds_md       *v1;
745         int                      i;
746
747         if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
748                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
749
750                 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
751                         v1 = (struct lov_mds_md *)((char *)comp_v1 +
752                                 comp_v1->lcm_entries[i].lcme_offset);
753                         /* We don't support partial release for now */
754                         if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
755                                 return false;
756                 }
757                 return true;
758         } else {
759                 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
760                         true : false;
761         }
762 }
763
764 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
765                         const struct lu_attr *attr, const struct lu_fid *fid)
766 {
767         struct md_attr *ma = &info->mti_attr;
768         struct obd_export *exp = info->mti_exp;
769         struct lu_nodemap *nodemap = NULL;
770
771         LASSERT(ma->ma_valid & MA_INODE);
772
773         if (attr->la_valid & LA_ATIME) {
774                 b->mbo_atime = attr->la_atime;
775                 b->mbo_valid |= OBD_MD_FLATIME;
776         }
777         if (attr->la_valid & LA_MTIME) {
778                 b->mbo_mtime = attr->la_mtime;
779                 b->mbo_valid |= OBD_MD_FLMTIME;
780         }
781         if (attr->la_valid & LA_CTIME) {
782                 b->mbo_ctime = attr->la_ctime;
783                 b->mbo_valid |= OBD_MD_FLCTIME;
784         }
785         if (attr->la_valid & LA_BTIME) {
786                 b->mbo_btime = attr->la_btime;
787                 b->mbo_valid |= OBD_MD_FLBTIME;
788         }
789         if (attr->la_valid & LA_FLAGS) {
790                 b->mbo_flags = attr->la_flags;
791                 b->mbo_valid |= OBD_MD_FLFLAGS;
792         }
793         if (attr->la_valid & LA_NLINK) {
794                 b->mbo_nlink = attr->la_nlink;
795                 b->mbo_valid |= OBD_MD_FLNLINK;
796         }
797         if (attr->la_valid & (LA_UID|LA_GID)) {
798                 nodemap = nodemap_get_from_exp(exp);
799                 if (IS_ERR(nodemap))
800                         goto out;
801         }
802         if (attr->la_valid & LA_UID) {
803                 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
804                                             NODEMAP_FS_TO_CLIENT,
805                                             attr->la_uid);
806                 b->mbo_valid |= OBD_MD_FLUID;
807         }
808         if (attr->la_valid & LA_GID) {
809                 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
810                                             NODEMAP_FS_TO_CLIENT,
811                                             attr->la_gid);
812                 b->mbo_valid |= OBD_MD_FLGID;
813         }
814
815         if (attr->la_valid & LA_PROJID) {
816                 /* TODO, nodemap for project id */
817                 b->mbo_projid = attr->la_projid;
818                 b->mbo_valid |= OBD_MD_FLPROJID;
819         }
820
821         b->mbo_mode = attr->la_mode;
822         if (attr->la_valid & LA_MODE)
823                 b->mbo_valid |= OBD_MD_FLMODE;
824         if (attr->la_valid & LA_TYPE)
825                 b->mbo_valid |= OBD_MD_FLTYPE;
826
827         if (fid != NULL) {
828                 b->mbo_fid1 = *fid;
829                 b->mbo_valid |= OBD_MD_FLID;
830                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
831                        PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
832         }
833
834         if (!(attr->la_valid & LA_TYPE))
835                 return;
836
837         b->mbo_rdev   = attr->la_rdev;
838         b->mbo_size   = attr->la_size;
839         b->mbo_blocks = attr->la_blocks;
840
841         if (!S_ISREG(attr->la_mode)) {
842                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
843         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
844                 /* means no objects are allocated on osts. */
845                 LASSERT(!(ma->ma_valid & MA_LOV));
846                 /* just ignore blocks occupied by extend attributes on MDS */
847                 b->mbo_blocks = 0;
848                 /* if no object is allocated on osts, the size on mds is valid.
849                  * b=22272 */
850                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
851         } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
852                 if (mdt_hsm_is_released(ma->ma_lmm)) {
853                         /* A released file stores its size on MDS. */
854                         /* But return 1 block for released file, unless tools
855                          * like tar will consider it fully sparse. (LU-3864)
856                          */
857                         if (unlikely(b->mbo_size == 0))
858                                 b->mbo_blocks = 0;
859                         else
860                                 b->mbo_blocks = 1;
861                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
862                 } else if (info->mti_som_valid) { /* som is valid */
863                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
864                 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
865                         b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
866                         b->mbo_size = ma->ma_som.ms_size;
867                         b->mbo_blocks = ma->ma_som.ms_blocks;
868                 }
869         }
870
871         if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
872                             b->mbo_valid & OBD_MD_FLLAZYSIZE))
873                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
874                        PFID(fid), (unsigned long long)b->mbo_size);
875
876 out:
877         if (!IS_ERR_OR_NULL(nodemap))
878                 nodemap_putref(nodemap);
879 }
880
881 static inline int mdt_body_has_lov(const struct lu_attr *la,
882                                    const struct mdt_body *body)
883 {
884         return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
885                (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
886 }
887
888 void mdt_client_compatibility(struct mdt_thread_info *info)
889 {
890         struct mdt_body       *body;
891         struct ptlrpc_request *req = mdt_info_req(info);
892         struct obd_export     *exp = req->rq_export;
893         struct md_attr        *ma = &info->mti_attr;
894         struct lu_attr        *la = &ma->ma_attr;
895         ENTRY;
896
897         if (exp_connect_layout(exp))
898                 /* the client can deal with 16-bit lmm_stripe_count */
899                 RETURN_EXIT;
900
901         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
902
903         if (!mdt_body_has_lov(la, body))
904                 RETURN_EXIT;
905
906         /* now we have a reply with a lov for a client not compatible with the
907          * layout lock so we have to clean the layout generation number */
908         if (S_ISREG(la->la_mode))
909                 ma->ma_lmm->lmm_layout_gen = 0;
910         EXIT;
911 }
912
913 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
914                                    struct mdt_object *o)
915 {
916         const struct lu_env *env = info->mti_env;
917         int rc, rc2;
918
919         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
920                           XATTR_NAME_LOV);
921
922         if (rc == -ENODATA)
923                 rc = 0;
924
925         if (rc < 0)
926                 goto out;
927
928         /* Is it a directory? Let's check for the LMV as well */
929         if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
930                 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
931                                    XATTR_NAME_LMV);
932
933                 if (rc2 == -ENODATA)
934                         rc2 = mo_xattr_get(env, mdt_object_child(o),
935                                            &LU_BUF_NULL,
936                                            XATTR_NAME_DEFAULT_LMV);
937
938                 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
939                         rc = rc2;
940         }
941
942 out:
943         return rc;
944 }
945
946 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
947                       const char *name)
948 {
949         const struct lu_env *env = info->mti_env;
950         int rc;
951         ENTRY;
952
953         LASSERT(info->mti_big_lmm_used == 0);
954         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
955         if (rc < 0)
956                 RETURN(rc);
957
958         /* big_lmm may need to be grown */
959         if (info->mti_big_lmmsize < rc) {
960                 int size = size_roundup_power2(rc);
961
962                 if (info->mti_big_lmmsize > 0) {
963                         /* free old buffer */
964                         LASSERT(info->mti_big_lmm);
965                         OBD_FREE_LARGE(info->mti_big_lmm,
966                                        info->mti_big_lmmsize);
967                         info->mti_big_lmm = NULL;
968                         info->mti_big_lmmsize = 0;
969                 }
970
971                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
972                 if (info->mti_big_lmm == NULL)
973                         RETURN(-ENOMEM);
974                 info->mti_big_lmmsize = size;
975         }
976         LASSERT(info->mti_big_lmmsize >= rc);
977
978         info->mti_buf.lb_buf = info->mti_big_lmm;
979         info->mti_buf.lb_len = info->mti_big_lmmsize;
980         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
981
982         RETURN(rc);
983 }
984
985 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
986                      struct md_attr *ma, const char *name)
987 {
988         struct md_object *next = mdt_object_child(o);
989         struct lu_buf    *buf = &info->mti_buf;
990         int rc;
991
992         if (strcmp(name, XATTR_NAME_LOV) == 0) {
993                 buf->lb_buf = ma->ma_lmm;
994                 buf->lb_len = ma->ma_lmm_size;
995                 LASSERT(!(ma->ma_valid & MA_LOV));
996         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
997                 buf->lb_buf = ma->ma_lmv;
998                 buf->lb_len = ma->ma_lmv_size;
999                 LASSERT(!(ma->ma_valid & MA_LMV));
1000         } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1001                 buf->lb_buf = ma->ma_default_lmv;
1002                 buf->lb_len = ma->ma_default_lmv_size;
1003                 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1004         } else {
1005                 return -EINVAL;
1006         }
1007
1008         LASSERT(buf->lb_buf);
1009
1010         rc = mo_xattr_get(info->mti_env, next, buf, name);
1011         if (rc > 0) {
1012
1013 got:
1014                 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1015                         if (info->mti_big_lmm_used)
1016                                 ma->ma_lmm = info->mti_big_lmm;
1017
1018                         /* NOT return LOV EA with hole to old client. */
1019                         if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1020                                      LOV_PATTERN_F_HOLE) &&
1021                             !(exp_connect_flags(info->mti_exp) &
1022                               OBD_CONNECT_LFSCK)) {
1023                                 return -EIO;
1024                         } else {
1025                                 ma->ma_lmm_size = rc;
1026                                 ma->ma_valid |= MA_LOV;
1027                         }
1028                 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1029                         if (info->mti_big_lmm_used)
1030                                 ma->ma_lmv = info->mti_big_lmm;
1031
1032                         ma->ma_lmv_size = rc;
1033                         ma->ma_valid |= MA_LMV;
1034                 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1035                         ma->ma_default_lmv_size = rc;
1036                         ma->ma_valid |= MA_LMV_DEF;
1037                 }
1038
1039                 /* Update mdt_max_mdsize so all clients will be aware that */
1040                 if (info->mti_mdt->mdt_max_mdsize < rc)
1041                         info->mti_mdt->mdt_max_mdsize = rc;
1042
1043                 rc = 0;
1044         } else if (rc == -ENODATA) {
1045                 /* no LOV EA */
1046                 rc = 0;
1047         } else if (rc == -ERANGE) {
1048                 /* Default LMV has fixed size, so it must be able to fit
1049                  * in the original buffer */
1050                 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1051                         return rc;
1052                 rc = mdt_big_xattr_get(info, o, name);
1053                 if (rc > 0) {
1054                         info->mti_big_lmm_used = 1;
1055                         goto got;
1056                 }
1057         }
1058
1059         return rc;
1060 }
1061
1062 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1063                    struct md_attr *ma, const char *name)
1064 {
1065         int rc;
1066
1067         if (!info->mti_big_lmm) {
1068                 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1069                 if (!info->mti_big_lmm)
1070                         return -ENOMEM;
1071                 info->mti_big_lmmsize = PAGE_SIZE;
1072         }
1073
1074         if (strcmp(name, XATTR_NAME_LOV) == 0) {
1075                 ma->ma_lmm = info->mti_big_lmm;
1076                 ma->ma_lmm_size = info->mti_big_lmmsize;
1077                 ma->ma_valid &= ~MA_LOV;
1078         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1079                 ma->ma_lmv = info->mti_big_lmm;
1080                 ma->ma_lmv_size = info->mti_big_lmmsize;
1081                 ma->ma_valid &= ~MA_LMV;
1082         } else {
1083                 LBUG();
1084         }
1085
1086         LASSERT(!info->mti_big_lmm_used);
1087         rc = __mdt_stripe_get(info, o, ma, name);
1088         /* since big_lmm is always used here, clear 'used' flag to avoid
1089          * assertion in mdt_big_xattr_get().
1090          */
1091         info->mti_big_lmm_used = 0;
1092
1093         return rc;
1094 }
1095
1096 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1097                       struct lu_fid *pfid)
1098 {
1099         struct lu_buf           *buf = &info->mti_buf;
1100         struct link_ea_header   *leh;
1101         struct link_ea_entry    *lee;
1102         int                      rc;
1103         ENTRY;
1104
1105         buf->lb_buf = info->mti_big_lmm;
1106         buf->lb_len = info->mti_big_lmmsize;
1107         rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1108                           buf, XATTR_NAME_LINK);
1109         /* ignore errors, MA_PFID won't be set and it is
1110          * up to the caller to treat this as an error */
1111         if (rc == -ERANGE || buf->lb_len == 0) {
1112                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1113                 buf->lb_buf = info->mti_big_lmm;
1114                 buf->lb_len = info->mti_big_lmmsize;
1115         }
1116
1117         if (rc < 0)
1118                 RETURN(rc);
1119         if (rc < sizeof(*leh)) {
1120                 CERROR("short LinkEA on "DFID": rc = %d\n",
1121                        PFID(mdt_object_fid(o)), rc);
1122                 RETURN(-ENODATA);
1123         }
1124
1125         leh = (struct link_ea_header *) buf->lb_buf;
1126         lee = (struct link_ea_entry *)(leh + 1);
1127         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1128                 leh->leh_magic = LINK_EA_MAGIC;
1129                 leh->leh_reccount = __swab32(leh->leh_reccount);
1130                 leh->leh_len = __swab64(leh->leh_len);
1131         }
1132         if (leh->leh_magic != LINK_EA_MAGIC)
1133                 RETURN(-EINVAL);
1134         if (leh->leh_reccount == 0)
1135                 RETURN(-ENODATA);
1136
1137         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1138         fid_be_to_cpu(pfid, pfid);
1139
1140         RETURN(0);
1141 }
1142
1143 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1144                            struct lu_fid *pfid, struct lu_name *lname)
1145 {
1146         struct lu_buf *buf = &info->mti_buf;
1147         struct link_ea_header *leh;
1148         struct link_ea_entry *lee;
1149         int reclen;
1150         int rc;
1151
1152         buf->lb_buf = info->mti_xattr_buf;
1153         buf->lb_len = sizeof(info->mti_xattr_buf);
1154         rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1155                           XATTR_NAME_LINK);
1156         if (rc == -ERANGE) {
1157                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1158                 buf->lb_buf = info->mti_big_lmm;
1159                 buf->lb_len = info->mti_big_lmmsize;
1160         }
1161         if (rc < 0)
1162                 return rc;
1163
1164         if (rc < sizeof(*leh)) {
1165                 CERROR("short LinkEA on "DFID": rc = %d\n",
1166                        PFID(mdt_object_fid(o)), rc);
1167                 return -ENODATA;
1168         }
1169
1170         leh = (struct link_ea_header *)buf->lb_buf;
1171         lee = (struct link_ea_entry *)(leh + 1);
1172         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1173                 leh->leh_magic = LINK_EA_MAGIC;
1174                 leh->leh_reccount = __swab32(leh->leh_reccount);
1175                 leh->leh_len = __swab64(leh->leh_len);
1176         }
1177         if (leh->leh_magic != LINK_EA_MAGIC)
1178                 return -EINVAL;
1179
1180         if (leh->leh_reccount == 0)
1181                 return -ENODATA;
1182
1183         linkea_entry_unpack(lee, &reclen, lname, pfid);
1184
1185         return 0;
1186 }
1187
1188 int mdt_attr_get_complex(struct mdt_thread_info *info,
1189                          struct mdt_object *o, struct md_attr *ma)
1190 {
1191         const struct lu_env *env = info->mti_env;
1192         struct md_object    *next = mdt_object_child(o);
1193         struct lu_buf       *buf = &info->mti_buf;
1194         int                  need = ma->ma_need;
1195         int                  rc = 0, rc2;
1196         u32                  mode;
1197         ENTRY;
1198
1199         ma->ma_valid = 0;
1200
1201         if (mdt_object_exists(o) == 0)
1202                 GOTO(out, rc = -ENOENT);
1203         mode = lu_object_attr(&next->mo_lu);
1204
1205         if (need & MA_INODE) {
1206                 ma->ma_need = MA_INODE;
1207                 rc = mo_attr_get(env, next, ma);
1208                 if (rc)
1209                         GOTO(out, rc);
1210
1211                 if (S_ISREG(mode))
1212                         (void) mdt_get_som(info, o, ma);
1213                 ma->ma_valid |= MA_INODE;
1214         }
1215
1216         if (need & MA_PFID) {
1217                 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1218                 if (rc == 0)
1219                         ma->ma_valid |= MA_PFID;
1220                 /* ignore this error, parent fid is not mandatory */
1221                 rc = 0;
1222         }
1223
1224         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1225                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1226                 if (rc)
1227                         GOTO(out, rc);
1228         }
1229
1230         if (need & MA_LMV && S_ISDIR(mode)) {
1231                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1232                 if (rc != 0)
1233                         GOTO(out, rc);
1234         }
1235
1236         if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1237                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1238                 if (rc != 0)
1239                         GOTO(out, rc);
1240         }
1241
1242         /*
1243          * In the handle of MA_INODE, we may already get the SOM attr.
1244          */
1245         if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1246                 rc = mdt_get_som(info, o, ma);
1247                 if (rc != 0)
1248                         GOTO(out, rc);
1249         }
1250
1251         if (need & MA_HSM && S_ISREG(mode)) {
1252                 buf->lb_buf = info->mti_xattr_buf;
1253                 buf->lb_len = sizeof(info->mti_xattr_buf);
1254                 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1255                              sizeof(info->mti_xattr_buf));
1256                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1257                 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1258                 if (rc2 == 0)
1259                         ma->ma_valid |= MA_HSM;
1260                 else if (rc2 < 0 && rc2 != -ENODATA)
1261                         GOTO(out, rc = rc2);
1262         }
1263
1264 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1265         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1266                 buf->lb_buf = ma->ma_acl;
1267                 buf->lb_len = ma->ma_acl_size;
1268                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1269                 if (rc2 > 0) {
1270                         ma->ma_acl_size = rc2;
1271                         ma->ma_valid |= MA_ACL_DEF;
1272                 } else if (rc2 == -ENODATA) {
1273                         /* no ACLs */
1274                         ma->ma_acl_size = 0;
1275                 } else
1276                         GOTO(out, rc = rc2);
1277         }
1278 #endif
1279 out:
1280         ma->ma_need = need;
1281         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1282                rc, ma->ma_valid, ma->ma_lmm);
1283         RETURN(rc);
1284 }
1285
1286 static int mdt_getattr_internal(struct mdt_thread_info *info,
1287                                 struct mdt_object *o, int ma_need)
1288 {
1289         struct mdt_device *mdt = info->mti_mdt;
1290         struct md_object *next = mdt_object_child(o);
1291         const struct mdt_body *reqbody = info->mti_body;
1292         struct ptlrpc_request *req = mdt_info_req(info);
1293         struct md_attr *ma = &info->mti_attr;
1294         struct lu_attr *la = &ma->ma_attr;
1295         struct req_capsule *pill = info->mti_pill;
1296         const struct lu_env *env = info->mti_env;
1297         struct mdt_body *repbody;
1298         struct lu_buf *buffer = &info->mti_buf;
1299         struct obd_export *exp = info->mti_exp;
1300         ktime_t kstart = ktime_get();
1301         int rc;
1302
1303         ENTRY;
1304
1305         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1306                 RETURN(err_serious(-ENOMEM));
1307
1308         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1309
1310         ma->ma_valid = 0;
1311
1312         if (mdt_object_remote(o)) {
1313                 /* This object is located on remote node.*/
1314                 /* Return -ENOTSUPP for old client */
1315                 if (!mdt_is_dne_client(req->rq_export))
1316                         GOTO(out, rc = -ENOTSUPP);
1317
1318                 repbody->mbo_fid1 = *mdt_object_fid(o);
1319                 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1320                 GOTO(out, rc = 0);
1321         }
1322
1323         if (reqbody->mbo_eadatasize > 0) {
1324                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1325                 if (buffer->lb_buf == NULL)
1326                         GOTO(out, rc = -EPROTO);
1327                 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1328                                                       RCL_SERVER);
1329         } else {
1330                 buffer->lb_buf = NULL;
1331                 buffer->lb_len = 0;
1332                 ma_need &= ~(MA_LOV | MA_LMV);
1333                 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1334                        mdt_obd_name(info->mti_mdt),
1335                        req->rq_export->exp_client_uuid.uuid);
1336         }
1337
1338         /* from 2.12.58 intent_getattr pack default LMV in reply */
1339         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1340             ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1341                     (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1342             req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1343                                   RCL_SERVER)) {
1344                 ma->ma_lmv = buffer->lb_buf;
1345                 ma->ma_lmv_size = buffer->lb_len;
1346                 ma->ma_default_lmv = req_capsule_server_get(pill,
1347                                                 &RMF_DEFAULT_MDT_MD);
1348                 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1349                                                 &RMF_DEFAULT_MDT_MD,
1350                                                 RCL_SERVER);
1351                 ma->ma_need = MA_INODE;
1352                 if (ma->ma_lmv_size > 0)
1353                         ma->ma_need |= MA_LMV;
1354                 if (ma->ma_default_lmv_size > 0)
1355                         ma->ma_need |= MA_LMV_DEF;
1356         } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1357                    (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1358                 /* If it is dir and client require MEA, then we got MEA */
1359                 /* Assumption: MDT_MD size is enough for lmv size. */
1360                 ma->ma_lmv = buffer->lb_buf;
1361                 ma->ma_lmv_size = buffer->lb_len;
1362                 ma->ma_need = MA_INODE;
1363                 if (ma->ma_lmv_size > 0) {
1364                         if (reqbody->mbo_valid & OBD_MD_MEA) {
1365                                 ma->ma_need |= MA_LMV;
1366                         } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1367                                 ma->ma_need |= MA_LMV_DEF;
1368                                 ma->ma_default_lmv = buffer->lb_buf;
1369                                 ma->ma_lmv = NULL;
1370                                 ma->ma_default_lmv_size = buffer->lb_len;
1371                                 ma->ma_lmv_size = 0;
1372                         }
1373                 }
1374         } else {
1375                 ma->ma_lmm = buffer->lb_buf;
1376                 ma->ma_lmm_size = buffer->lb_len;
1377                 ma->ma_need = MA_INODE | MA_HSM;
1378                 if (ma->ma_lmm_size > 0) {
1379                         ma->ma_need |= MA_LOV;
1380                         /* Older clients may crash if they getattr overstriped
1381                          * files
1382                          */
1383                         if (!exp_connect_overstriping(exp) &&
1384                             mdt_lmm_is_overstriping(ma->ma_lmm))
1385                                 RETURN(-EOPNOTSUPP);
1386                 }
1387         }
1388
1389         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1390             reqbody->mbo_valid & OBD_MD_FLDIREA  &&
1391             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1392                 /* get default stripe info for this dir. */
1393                 ma->ma_need |= MA_LOV_DEF;
1394         }
1395         ma->ma_need |= ma_need;
1396
1397         rc = mdt_attr_get_complex(info, o, ma);
1398         if (unlikely(rc)) {
1399                 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1400                        "%s: getattr error for "DFID": rc = %d\n",
1401                        mdt_obd_name(info->mti_mdt),
1402                        PFID(mdt_object_fid(o)), rc);
1403                 RETURN(rc);
1404         }
1405
1406         /* if file is released, check if a restore is running */
1407         if (ma->ma_valid & MA_HSM) {
1408                 repbody->mbo_valid |= OBD_MD_TSTATE;
1409                 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1410                     mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1411                         repbody->mbo_t_state = MS_RESTORE;
1412         }
1413
1414         if (unlikely(!(ma->ma_valid & MA_INODE)))
1415                 RETURN(-EFAULT);
1416
1417         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1418
1419         if (mdt_body_has_lov(la, reqbody)) {
1420                 u32 stripe_count = 1;
1421
1422                 if (ma->ma_valid & MA_LOV) {
1423                         LASSERT(ma->ma_lmm_size);
1424                         repbody->mbo_eadatasize = ma->ma_lmm_size;
1425                         if (S_ISDIR(la->la_mode))
1426                                 repbody->mbo_valid |= OBD_MD_FLDIREA;
1427                         else
1428                                 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1429                         mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1430                 }
1431                 if (ma->ma_valid & MA_LMV) {
1432                         struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1433                         u32 magic = le32_to_cpu(lmv->lmv_magic);
1434
1435                         /* Return -ENOTSUPP for old client */
1436                         if (!mdt_is_striped_client(req->rq_export))
1437                                 RETURN(-ENOTSUPP);
1438
1439                         LASSERT(S_ISDIR(la->la_mode));
1440                         mdt_dump_lmv(D_INFO, ma->ma_lmv);
1441                         repbody->mbo_eadatasize = ma->ma_lmv_size;
1442                         repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1443
1444                         stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1445                         if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1446                                 mdt_restripe_migrate_add(info, o);
1447                         else if (magic == LMV_MAGIC_V1 &&
1448                                  lmv_is_restriping(lmv))
1449                                 mdt_restripe_update_add(info, o);
1450                 }
1451                 if (ma->ma_valid & MA_LMV_DEF) {
1452                         /* Return -ENOTSUPP for old client */
1453                         if (!mdt_is_striped_client(req->rq_export))
1454                                 RETURN(-ENOTSUPP);
1455                         LASSERT(S_ISDIR(la->la_mode));
1456                         /*
1457                          * when ll_dir_getstripe() gets default LMV, it
1458                          * checks mbo_eadatasize.
1459                          */
1460                         if (!(ma->ma_valid & MA_LMV))
1461                                 repbody->mbo_eadatasize =
1462                                         ma->ma_default_lmv_size;
1463                         repbody->mbo_valid |= (OBD_MD_FLDIREA |
1464                                                OBD_MD_DEFAULT_MEA);
1465                 }
1466                 CDEBUG(D_VFSTRACE,
1467                        "dirent count %llu stripe count %u MDT count %d\n",
1468                        ma->ma_attr.la_dirent_count, stripe_count,
1469                        atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1470                 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1471                     ma->ma_attr.la_dirent_count >
1472                         mdt->mdt_restriper.mdr_dir_split_count &&
1473                     !fid_is_root(mdt_object_fid(o)) &&
1474                     mdt->mdt_enable_dir_auto_split &&
1475                     !o->mot_restriping &&
1476                     stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1477                         mdt_auto_split_add(info, o);
1478         } else if (S_ISLNK(la->la_mode) &&
1479                    reqbody->mbo_valid & OBD_MD_LINKNAME) {
1480                 buffer->lb_buf = ma->ma_lmm;
1481                 /* eadatasize from client includes NULL-terminator, so
1482                  * there is no need to read it */
1483                 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1484                 rc = mo_readlink(env, next, buffer);
1485                 if (unlikely(rc <= 0)) {
1486                         CERROR("%s: readlink failed for "DFID": rc = %d\n",
1487                                mdt_obd_name(info->mti_mdt),
1488                                PFID(mdt_object_fid(o)), rc);
1489                         rc = -EFAULT;
1490                 } else {
1491                         int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1492
1493                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1494                                 rc -= 2;
1495                         repbody->mbo_valid |= OBD_MD_LINKNAME;
1496                         /* we need to report back size with NULL-terminator
1497                          * because client expects that */
1498                         repbody->mbo_eadatasize = rc + 1;
1499                         if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1500                                 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1501                                        "on "DFID ", expected %d\n",
1502                                        mdt_obd_name(info->mti_mdt),
1503                                        rc, PFID(mdt_object_fid(o)),
1504                                        reqbody->mbo_eadatasize - 1);
1505                         /* NULL terminate */
1506                         ((char *)ma->ma_lmm)[rc] = 0;
1507
1508                         /* If the total CDEBUG() size is larger than a page, it
1509                          * will print a warning to the console, avoid this by
1510                          * printing just the last part of the symlink. */
1511                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1512                                print_limit < rc ? "..." : "", print_limit,
1513                                (char *)ma->ma_lmm + rc - print_limit, rc);
1514                         rc = 0;
1515                 }
1516         }
1517
1518         if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1519                 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1520                 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1521                 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1522                        repbody->mbo_max_mdsize);
1523         }
1524
1525 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1526         if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1527                  (reqbody->mbo_valid & OBD_MD_FLACL)) {
1528                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1529                 if (IS_ERR(nodemap))
1530                         RETURN(PTR_ERR(nodemap));
1531
1532                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1533                 nodemap_putref(nodemap);
1534         }
1535 #endif
1536
1537 out:
1538         if (rc == 0)
1539                 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1540                                  ktime_us_delta(ktime_get(), kstart));
1541
1542         RETURN(rc);
1543 }
1544
1545 static int mdt_getattr(struct tgt_session_info *tsi)
1546 {
1547         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1548         struct mdt_object       *obj = info->mti_object;
1549         struct req_capsule      *pill = info->mti_pill;
1550         struct mdt_body         *reqbody;
1551         struct mdt_body         *repbody;
1552         int rc, rc2;
1553         ENTRY;
1554
1555         if (unlikely(info->mti_object == NULL))
1556                 RETURN(-EPROTO);
1557
1558         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1559         LASSERT(reqbody);
1560         LASSERT(lu_object_assert_exists(&obj->mot_obj));
1561
1562         /* Special case for Data-on-MDT files to get data version */
1563         if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1564                 rc = mdt_data_version_get(tsi);
1565                 GOTO(out, rc);
1566         }
1567
1568         /* Unlike intent case where we need to pre-fill out buffers early on
1569          * in intent policy for ldlm reasons, here we can have a much better
1570          * guess at EA size by just reading it from disk.
1571          * Exceptions are readdir and (missing) directory striping */
1572         /* Readlink */
1573         if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1574                 /* No easy way to know how long is the symlink, but it cannot
1575                  * be more than PATH_MAX, so we allocate +1 */
1576                 rc = PATH_MAX + 1;
1577         /* A special case for fs ROOT: getattr there might fetch
1578          * default EA for entire fs, not just for this dir!
1579          */
1580         } else if (lu_fid_eq(mdt_object_fid(obj),
1581                              &info->mti_mdt->mdt_md_root_fid) &&
1582                    (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1583                    (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1584                                                                  MDS_GETATTR)) {
1585                 /* Should the default strping be bigger, mdt_fix_reply
1586                  * will reallocate */
1587                 rc = DEF_REP_MD_SIZE;
1588         } else {
1589                 /* Read the actual EA size from disk */
1590                 rc = mdt_attr_get_eabuf_size(info, obj);
1591         }
1592
1593         if (rc < 0)
1594                 GOTO(out, rc = err_serious(rc));
1595
1596         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1597
1598         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1599          * by default. If the target object has more ACL entries, then
1600          * enlarge the buffer when necessary. */
1601         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1602                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1603
1604         rc = req_capsule_server_pack(pill);
1605         if (unlikely(rc != 0))
1606                 GOTO(out, rc = err_serious(rc));
1607
1608         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1609         LASSERT(repbody != NULL);
1610         repbody->mbo_eadatasize = 0;
1611         repbody->mbo_aclsize = 0;
1612
1613         rc = mdt_check_ucred(info);
1614         if (unlikely(rc))
1615                 GOTO(out_shrink, rc);
1616
1617         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1618
1619         rc = mdt_getattr_internal(info, obj, 0);
1620         EXIT;
1621 out_shrink:
1622         mdt_client_compatibility(info);
1623         rc2 = mdt_fix_reply(info);
1624         if (rc == 0)
1625                 rc = rc2;
1626 out:
1627         mdt_thread_info_fini(info);
1628         return rc;
1629 }
1630
1631 /**
1632  * Handler of layout intent RPC requiring the layout modification
1633  *
1634  * \param[in]  info     thread environment
1635  * \param[in]  obj      object
1636  * \param[out] lhc      object ldlm lock handle
1637  * \param[in]  layout   layout change descriptor
1638  *
1639  * \retval 0    on success
1640  * \retval < 0  error code
1641  */
1642 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1643                       struct mdt_lock_handle *lhc,
1644                       struct md_layout_change *layout)
1645 {
1646         int rc;
1647
1648         ENTRY;
1649
1650         if (!mdt_object_exists(obj))
1651                 RETURN(-ENOENT);
1652
1653         if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1654                 RETURN(-EINVAL);
1655
1656         rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1657                            MAY_WRITE);
1658         if (rc)
1659                 RETURN(rc);
1660
1661         rc = mdt_check_resent_lock(info, obj, lhc);
1662         if (rc < 0)
1663                 RETURN(rc);
1664
1665         if (rc > 0) {
1666                 /* not resent */
1667                 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1668
1669                 /* take layout lock to prepare layout change */
1670                 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1671                         lockpart |= MDS_INODELOCK_UPDATE;
1672
1673                 mdt_lock_handle_init(lhc);
1674                 mdt_lock_reg_init(lhc, LCK_EX);
1675                 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1676                 if (rc)
1677                         RETURN(rc);
1678         }
1679
1680         mutex_lock(&obj->mot_som_mutex);
1681         rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1682         mutex_unlock(&obj->mot_som_mutex);
1683
1684         if (rc)
1685                 mdt_object_unlock(info, obj, lhc, 1);
1686
1687         RETURN(rc);
1688 }
1689
1690 /**
1691  * Exchange MOF_LOV_CREATED flags between two objects after a
1692  * layout swap. No assumption is made on whether o1 or o2 have
1693  * created objects or not.
1694  *
1695  * \param[in,out] o1    First swap layout object
1696  * \param[in,out] o2    Second swap layout object
1697  */
1698 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1699 {
1700         unsigned int o1_lov_created = o1->mot_lov_created;
1701
1702         mutex_lock(&o1->mot_lov_mutex);
1703         mutex_lock(&o2->mot_lov_mutex);
1704
1705         o1->mot_lov_created = o2->mot_lov_created;
1706         o2->mot_lov_created = o1_lov_created;
1707
1708         mutex_unlock(&o2->mot_lov_mutex);
1709         mutex_unlock(&o1->mot_lov_mutex);
1710 }
1711
1712 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1713 {
1714         struct mdt_thread_info  *info;
1715         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1716         struct obd_export       *exp = req->rq_export;
1717         struct mdt_object       *o1, *o2, *o;
1718         struct mdt_lock_handle  *lh1, *lh2;
1719         struct mdc_swap_layouts *msl;
1720         int                      rc;
1721         ENTRY;
1722
1723         /* client does not support layout lock, so layout swaping
1724          * is disabled.
1725          * FIXME: there is a problem for old clients which don't support
1726          * layout lock yet. If those clients have already opened the file
1727          * they won't be notified at all so that old layout may still be
1728          * used to do IO. This can be fixed after file release is landed by
1729          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1730         if (!exp_connect_layout(exp))
1731                 RETURN(-EOPNOTSUPP);
1732
1733         info = tsi2mdt_info(tsi);
1734         if (unlikely(info->mti_object == NULL))
1735                 RETURN(-EPROTO);
1736
1737         if (info->mti_dlm_req != NULL)
1738                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1739
1740         o1 = info->mti_object;
1741         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1742                                 &info->mti_body->mbo_fid2);
1743         if (IS_ERR(o))
1744                 GOTO(out, rc = PTR_ERR(o));
1745
1746         if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1747                 GOTO(put, rc = -ENOENT);
1748
1749         rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1750         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1751                 GOTO(put, rc);
1752
1753         if (rc < 0)
1754                 swap(o1, o2);
1755
1756         /* permission check. Make sure the calling process having permission
1757          * to write both files. */
1758         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1759                            MAY_WRITE);
1760         if (rc < 0)
1761                 GOTO(put, rc);
1762
1763         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1764                            MAY_WRITE);
1765         if (rc < 0)
1766                 GOTO(put, rc);
1767
1768         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1769         if (msl == NULL)
1770                 GOTO(put, rc = -EPROTO);
1771
1772         lh1 = &info->mti_lh[MDT_LH_NEW];
1773         mdt_lock_reg_init(lh1, LCK_EX);
1774         lh2 = &info->mti_lh[MDT_LH_OLD];
1775         mdt_lock_reg_init(lh2, LCK_EX);
1776
1777         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1778                              MDS_INODELOCK_XATTR);
1779         if (rc < 0)
1780                 GOTO(put, rc);
1781
1782         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1783                              MDS_INODELOCK_XATTR);
1784         if (rc < 0)
1785                 GOTO(unlock1, rc);
1786
1787         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1788                              mdt_object_child(o2), msl->msl_flags);
1789         if (rc < 0)
1790                 GOTO(unlock2, rc);
1791
1792         mdt_swap_lov_flag(o1, o2);
1793
1794 unlock2:
1795         mdt_object_unlock(info, o2, lh2, rc);
1796 unlock1:
1797         mdt_object_unlock(info, o1, lh1, rc);
1798 put:
1799         mdt_object_put(info->mti_env, o);
1800 out:
1801         mdt_thread_info_fini(info);
1802         RETURN(rc);
1803 }
1804
1805 static int mdt_raw_lookup(struct mdt_thread_info *info,
1806                           struct mdt_object *parent,
1807                           const struct lu_name *lname,
1808                           struct ldlm_reply *ldlm_rep)
1809 {
1810         struct lu_fid   *child_fid = &info->mti_tmp_fid1;
1811         int              rc;
1812         ENTRY;
1813
1814         LASSERT(!info->mti_cross_ref);
1815
1816         /* Only got the fid of this obj by name */
1817         fid_zero(child_fid);
1818         rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1819                         lname, child_fid, &info->mti_spec);
1820         if (rc == 0) {
1821                 struct mdt_body *repbody;
1822
1823                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1824                 repbody->mbo_fid1 = *child_fid;
1825                 repbody->mbo_valid = OBD_MD_FLID;
1826                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1827         } else if (rc == -ENOENT) {
1828                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1829         }
1830
1831         RETURN(rc);
1832 }
1833
1834 /*
1835  * UPDATE lock should be taken against parent, and be released before exit;
1836  * child_bits lock should be taken against child, and be returned back:
1837  *            (1)normal request should release the child lock;
1838  *            (2)intent request will grant the lock to client.
1839  */
1840 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1841                                  struct mdt_lock_handle *lhc,
1842                                  __u64 child_bits,
1843                                  struct ldlm_reply *ldlm_rep)
1844 {
1845         struct ptlrpc_request *req = mdt_info_req(info);
1846         struct mdt_body *reqbody = NULL;
1847         struct mdt_object *parent = info->mti_object;
1848         struct mdt_object *child = NULL;
1849         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1850         struct lu_name *lname = NULL;
1851         struct mdt_lock_handle *lhp = NULL;
1852         struct ldlm_lock *lock;
1853         struct req_capsule *pill = info->mti_pill;
1854         __u64 try_bits = 0;
1855         bool is_resent;
1856         int ma_need = 0;
1857         int rc;
1858
1859         ENTRY;
1860
1861         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1862         LASSERT(ergo(is_resent,
1863                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1864
1865         if (parent == NULL)
1866                 RETURN(-ENOENT);
1867
1868         if (info->mti_cross_ref) {
1869                 /* Only getattr on the child. Parent is on another node. */
1870                 mdt_set_disposition(info, ldlm_rep,
1871                                     DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1872                 child = parent;
1873                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1874                        "ldlm_rep = %p\n",
1875                        PFID(mdt_object_fid(child)), ldlm_rep);
1876
1877                 rc = mdt_check_resent_lock(info, child, lhc);
1878                 if (rc < 0) {
1879                         RETURN(rc);
1880                 } else if (rc > 0) {
1881                         mdt_lock_handle_init(lhc);
1882                         mdt_lock_reg_init(lhc, LCK_PR);
1883
1884                         /*
1885                          * Object's name entry is on another MDS, it will
1886                          * request PERM lock only because LOOKUP lock is owned
1887                          * by the MDS where name entry resides.
1888                          *
1889                          * TODO: it should try layout lock too. - Jinshan
1890                          */
1891                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1892                                         MDS_INODELOCK_LAYOUT);
1893                         child_bits |= MDS_INODELOCK_PERM;
1894
1895                         rc = mdt_object_lock(info, child, lhc, child_bits);
1896                         if (rc < 0)
1897                                 RETURN(rc);
1898                 }
1899
1900                 /* Finally, we can get attr for child. */
1901                 if (!mdt_object_exists(child)) {
1902                         LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1903                                         &child->mot_obj,
1904                                         "remote object doesn't exist.");
1905                         mdt_object_unlock(info, child, lhc, 1);
1906                         RETURN(-ENOENT);
1907                 }
1908
1909                 rc = mdt_getattr_internal(info, child, 0);
1910                 if (unlikely(rc != 0)) {
1911                         mdt_object_unlock(info, child, lhc, 1);
1912                         RETURN(rc);
1913                 }
1914
1915                 rc = mdt_pack_secctx_in_reply(info, child);
1916                 if (unlikely(rc))
1917                         mdt_object_unlock(info, child, lhc, 1);
1918                 RETURN(rc);
1919         }
1920
1921         lname = &info->mti_name;
1922         mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1923
1924         if (lu_name_is_valid(lname)) {
1925                 if (mdt_object_remote(parent)) {
1926                         CERROR("%s: parent "DFID" is on remote target\n",
1927                                mdt_obd_name(info->mti_mdt),
1928                                PFID(mdt_object_fid(parent)));
1929                         RETURN(-EPROTO);
1930                 }
1931
1932                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1933                        "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1934                        PNAME(lname), ldlm_rep);
1935         } else {
1936                 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1937                 if (unlikely(reqbody == NULL))
1938                         RETURN(err_serious(-EPROTO));
1939
1940                 *child_fid = reqbody->mbo_fid2;
1941                 if (unlikely(!fid_is_sane(child_fid)))
1942                         RETURN(err_serious(-EINVAL));
1943
1944                 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1945                         mdt_object_get(info->mti_env, parent);
1946                         child = parent;
1947                 } else {
1948                         child = mdt_object_find(info->mti_env, info->mti_mdt,
1949                                                 child_fid);
1950                         if (IS_ERR(child))
1951                                 RETURN(PTR_ERR(child));
1952                 }
1953
1954                 if (mdt_object_remote(child)) {
1955                         CERROR("%s: child "DFID" is on remote target\n",
1956                                mdt_obd_name(info->mti_mdt),
1957                                PFID(mdt_object_fid(child)));
1958                         GOTO(out_child, rc = -EPROTO);
1959                 }
1960
1961                 /* don't fetch LOOKUP lock if it's remote object */
1962                 rc = mdt_is_remote_object(info, parent, child);
1963                 if (rc < 0)
1964                         GOTO(out_child, rc);
1965                 if (rc)
1966                         child_bits &= ~MDS_INODELOCK_LOOKUP;
1967
1968                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1969                        "ldlm_rep = %p\n",
1970                        PFID(mdt_object_fid(parent)),
1971                        PFID(&reqbody->mbo_fid2), ldlm_rep);
1972         }
1973
1974         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1975
1976         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1977                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1978                                 &parent->mot_obj,
1979                                 "Parent doesn't exist!");
1980                 GOTO(out_child, rc = -ESTALE);
1981         }
1982
1983         if (lu_name_is_valid(lname)) {
1984                 /* Always allow to lookup ".." */
1985                 if (unlikely(lname->ln_namelen == 2 &&
1986                              lname->ln_name[0] == '.' &&
1987                              lname->ln_name[1] == '.'))
1988                         info->mti_spec.sp_permitted = 1;
1989
1990                 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1991                         rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1992
1993                         RETURN(rc);
1994                 }
1995
1996                 /* step 1: lock parent only if parent is a directory */
1997                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1998                         lhp = &info->mti_lh[MDT_LH_PARENT];
1999                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
2000                         rc = mdt_object_lock(info, parent, lhp,
2001                                              MDS_INODELOCK_UPDATE);
2002                         if (unlikely(rc != 0))
2003                                 RETURN(rc);
2004                 }
2005
2006                 /* step 2: lookup child's fid by name */
2007                 fid_zero(child_fid);
2008                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2009                                 child_fid, &info->mti_spec);
2010                 if (rc == -ENOENT)
2011                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2012
2013                 if (rc != 0)
2014                         GOTO(unlock_parent, rc);
2015
2016                 child = mdt_object_find(info->mti_env, info->mti_mdt,
2017                                         child_fid);
2018                 if (unlikely(IS_ERR(child)))
2019                         GOTO(unlock_parent, rc = PTR_ERR(child));
2020         }
2021
2022         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2023
2024         /* step 3: lock child regardless if it is local or remote. */
2025         LASSERT(child);
2026
2027         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2028         if (!mdt_object_exists(child)) {
2029                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2030                                 &child->mot_obj,
2031                                 "Object doesn't exist!");
2032                 GOTO(out_child, rc = -ENOENT);
2033         }
2034
2035         rc = mdt_check_resent_lock(info, child, lhc);
2036         if (rc < 0) {
2037                 GOTO(out_child, rc);
2038         } else if (rc > 0) {
2039                 mdt_lock_handle_init(lhc);
2040                 mdt_lock_reg_init(lhc, LCK_PR);
2041
2042                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2043                     !mdt_object_remote(child)) {
2044                         struct md_attr *ma = &info->mti_attr;
2045
2046                         ma->ma_valid = 0;
2047                         ma->ma_need = MA_INODE;
2048                         rc = mdt_attr_get_complex(info, child, ma);
2049                         if (unlikely(rc != 0))
2050                                 GOTO(out_child, rc);
2051
2052                         /* If the file has not been changed for some time, we
2053                          * return not only a LOOKUP lock, but also an UPDATE
2054                          * lock and this might save us RPC on later STAT. For
2055                          * directories, it also let negative dentry cache start
2056                          * working for this dir. */
2057                         if (ma->ma_valid & MA_INODE &&
2058                             ma->ma_attr.la_valid & LA_CTIME &&
2059                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2060                             ma->ma_attr.la_ctime < ktime_get_real_seconds())
2061                                 child_bits |= MDS_INODELOCK_UPDATE;
2062                 }
2063
2064                 /* layout lock must be granted in a best-effort way
2065                  * for IT operations */
2066                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2067                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2068                     !mdt_object_remote(child) && ldlm_rep != NULL) {
2069                         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2070                             exp_connect_layout(info->mti_exp)) {
2071                                 /* try to grant layout lock for regular file. */
2072                                 try_bits = MDS_INODELOCK_LAYOUT;
2073                         }
2074                         /* Acquire DOM lock in advance for data-on-mdt file */
2075                         if (child != parent)
2076                                 try_bits |= MDS_INODELOCK_DOM;
2077                 }
2078
2079                 if (try_bits != 0) {
2080                         /* try layout lock, it may fail to be granted due to
2081                          * contention at LOOKUP or UPDATE */
2082                         rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2083                                                  try_bits, false);
2084                         if (child_bits & MDS_INODELOCK_LAYOUT)
2085                                 ma_need |= MA_LOV;
2086                 } else {
2087                         /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2088                          * client will enqueue the lock to the remote MDT */
2089                         if (mdt_object_remote(child))
2090                                 child_bits &= ~MDS_INODELOCK_UPDATE;
2091                         rc = mdt_object_lock(info, child, lhc, child_bits);
2092                 }
2093                 if (unlikely(rc != 0))
2094                         GOTO(out_child, rc);
2095         }
2096
2097         /* finally, we can get attr for child. */
2098         rc = mdt_getattr_internal(info, child, ma_need);
2099         if (unlikely(rc != 0)) {
2100                 mdt_object_unlock(info, child, lhc, 1);
2101                 GOTO(out_child, rc);
2102         }
2103
2104         rc = mdt_pack_secctx_in_reply(info, child);
2105         if (unlikely(rc)) {
2106                 mdt_object_unlock(info, child, lhc, 1);
2107                 GOTO(out_child, rc);
2108         }
2109
2110         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2111         if (lock) {
2112                 /* Debugging code. */
2113                 LDLM_DEBUG(lock, "Returning lock to client");
2114                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2115                                          &lock->l_resource->lr_name),
2116                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2117                          PLDLMRES(lock->l_resource),
2118                          PFID(mdt_object_fid(child)));
2119
2120                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2121                     !mdt_object_remote(child) && child != parent) {
2122                         mdt_object_put(info->mti_env, child);
2123                         rc = mdt_pack_size2body(info, child_fid,
2124                                                 &lhc->mlh_reg_lh);
2125                         if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2126                                 /* DOM lock was taken in advance but this is
2127                                  * not DoM file. Drop the lock.
2128                                  */
2129                                 lock_res_and_lock(lock);
2130                                 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2131                                 unlock_res_and_lock(lock);
2132                         }
2133                         LDLM_LOCK_PUT(lock);
2134                         GOTO(unlock_parent, rc = 0);
2135                 }
2136                 LDLM_LOCK_PUT(lock);
2137         }
2138
2139         EXIT;
2140 out_child:
2141         if (child)
2142                 mdt_object_put(info->mti_env, child);
2143 unlock_parent:
2144         if (lhp)
2145                 mdt_object_unlock(info, parent, lhp, 1);
2146         return rc;
2147 }
2148
2149 /* normal handler: should release the child lock */
2150 static int mdt_getattr_name(struct tgt_session_info *tsi)
2151 {
2152         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2153         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2154         struct mdt_body        *reqbody;
2155         struct mdt_body        *repbody;
2156         int rc, rc2;
2157         ENTRY;
2158
2159         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2160         LASSERT(reqbody != NULL);
2161         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2162         LASSERT(repbody != NULL);
2163
2164         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2165         repbody->mbo_eadatasize = 0;
2166         repbody->mbo_aclsize = 0;
2167
2168         rc = mdt_init_ucred_intent_getattr(info, reqbody);
2169         if (unlikely(rc))
2170                 GOTO(out_shrink, rc);
2171
2172         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2173         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2174                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2175                 lhc->mlh_reg_lh.cookie = 0;
2176         }
2177         mdt_exit_ucred(info);
2178         EXIT;
2179 out_shrink:
2180         mdt_client_compatibility(info);
2181         rc2 = mdt_fix_reply(info);
2182         if (rc == 0)
2183                 rc = rc2;
2184         mdt_thread_info_fini(info);
2185         return rc;
2186 }
2187
2188 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2189                             const struct lu_fid *pfid,
2190                             const struct lu_name *name,
2191                             struct mdt_object *obj, s64 ctime)
2192 {
2193         struct lu_fid *child_fid = &info->mti_tmp_fid1;
2194         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2195         struct mdt_device *mdt = info->mti_mdt;
2196         struct md_attr *ma = &info->mti_attr;
2197         struct mdt_lock_handle *parent_lh;
2198         struct mdt_lock_handle *child_lh;
2199         struct mdt_object *pobj;
2200         bool cos_incompat = false;
2201         int rc;
2202         ENTRY;
2203
2204         pobj = mdt_object_find(info->mti_env, mdt, pfid);
2205         if (IS_ERR(pobj))
2206                 GOTO(out, rc = PTR_ERR(pobj));
2207
2208         parent_lh = &info->mti_lh[MDT_LH_PARENT];
2209         mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2210         rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2211         if (rc != 0)
2212                 GOTO(put_parent, rc);
2213
2214         if (mdt_object_remote(pobj))
2215                 cos_incompat = true;
2216
2217         rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2218                         name, child_fid, &info->mti_spec);
2219         if (rc != 0)
2220                 GOTO(unlock_parent, rc);
2221
2222         if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2223                 GOTO(unlock_parent, rc = -EREMCHG);
2224
2225         child_lh = &info->mti_lh[MDT_LH_CHILD];
2226         mdt_lock_reg_init(child_lh, LCK_EX);
2227         rc = mdt_reint_striped_lock(info, obj, child_lh,
2228                                     MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2229                                     einfo, cos_incompat);
2230         if (rc != 0)
2231                 GOTO(unlock_parent, rc);
2232
2233         if (atomic_read(&obj->mot_open_count)) {
2234                 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2235                        PFID(mdt_object_fid(obj)));
2236                 GOTO(unlock_child, rc = -EBUSY);
2237         }
2238
2239         ma->ma_need = 0;
2240         ma->ma_valid = MA_INODE;
2241         ma->ma_attr.la_valid = LA_CTIME;
2242         ma->ma_attr.la_ctime = ctime;
2243
2244         mutex_lock(&obj->mot_lov_mutex);
2245
2246         rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2247                         mdt_object_child(obj), name, ma, 0);
2248
2249         mutex_unlock(&obj->mot_lov_mutex);
2250
2251 unlock_child:
2252         mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2253 unlock_parent:
2254         mdt_object_unlock(info, pobj, parent_lh, 1);
2255 put_parent:
2256         mdt_object_put(info->mti_env, pobj);
2257 out:
2258         RETURN(rc);
2259 }
2260
2261 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2262                                         struct mdt_object *obj)
2263 {
2264         struct lu_ucred *uc = lu_ucred(info->mti_env);
2265         struct md_attr *ma = &info->mti_attr;
2266         struct lu_attr *la = &ma->ma_attr;
2267         int rc = 0;
2268         ENTRY;
2269
2270         ma->ma_need = MA_INODE;
2271         rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2272         if (rc)
2273                 GOTO(out, rc);
2274
2275         if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2276                         rc = -EACCES;
2277
2278         if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
2279                 RETURN(0);
2280         if (uc->uc_fsuid == la->la_uid) {
2281                 if ((la->la_mode & S_IWUSR) == 0)
2282                         rc = -EACCES;
2283         } else if (uc->uc_fsgid == la->la_gid) {
2284                 if ((la->la_mode & S_IWGRP) == 0)
2285                         rc = -EACCES;
2286         } else if ((la->la_mode & S_IWOTH) == 0) {
2287                         rc = -EACCES;
2288         }
2289
2290 out:
2291         RETURN(rc);
2292 }
2293
2294 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2295                          s64 ctime)
2296 {
2297         struct mdt_device *mdt = info->mti_mdt;
2298         struct mdt_object *obj = NULL;
2299         struct linkea_data ldata = { NULL };
2300         struct lu_buf *buf = &info->mti_big_buf;
2301         struct lu_name *name = &info->mti_name;
2302         struct lu_fid *pfid = &info->mti_tmp_fid1;
2303         struct link_ea_header *leh;
2304         struct link_ea_entry *lee;
2305         int reclen, count, rc = 0;
2306         ENTRY;
2307
2308         if (!fid_is_sane(fid))
2309                 GOTO(out, rc = -EINVAL);
2310
2311         if (!fid_is_namespace_visible(fid))
2312                 GOTO(out, rc = -EINVAL);
2313
2314         obj = mdt_object_find(info->mti_env, mdt, fid);
2315         if (IS_ERR(obj))
2316                 GOTO(out, rc = PTR_ERR(obj));
2317
2318         if (mdt_object_remote(obj))
2319                 GOTO(out, rc = -EREMOTE);
2320         if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2321                 GOTO(out, rc = -ENOENT);
2322
2323         rc = mdt_rmfid_check_permission(info, obj);
2324         if (rc)
2325                 GOTO(out, rc);
2326
2327         /* take LinkEA */
2328         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2329         if (!buf->lb_buf)
2330                 GOTO(out, rc = -ENOMEM);
2331
2332         ldata.ld_buf = buf;
2333         rc = mdt_links_read(info, obj, &ldata);
2334         if (rc)
2335                 GOTO(out, rc);
2336
2337         leh = buf->lb_buf;
2338         lee = (struct link_ea_entry *)(leh + 1);
2339         for (count = 0; count < leh->leh_reccount; count++) {
2340                 /* remove every hardlink */
2341                 linkea_entry_unpack(lee, &reclen, name, pfid);
2342                 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2343                 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2344                 if (rc)
2345                         break;
2346         }
2347
2348 out:
2349         if (obj && !IS_ERR(obj))
2350                 mdt_object_put(info->mti_env, obj);
2351         if (info->mti_big_buf.lb_buf)
2352                 lu_buf_free(&info->mti_big_buf);
2353
2354         RETURN(rc);
2355 }
2356
2357 static int mdt_rmfid(struct tgt_session_info *tsi)
2358 {
2359         struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2360         struct mdt_body *reqbody;
2361         struct lu_fid *fids, *rfids;
2362         int bufsize, rc;
2363         __u32 *rcs;
2364         int i, nr;
2365         ENTRY;
2366
2367         reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2368         if (reqbody == NULL)
2369                 RETURN(-EPROTO);
2370         bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2371                                        RCL_CLIENT);
2372         nr = bufsize / sizeof(struct lu_fid);
2373         if (nr * sizeof(struct lu_fid) != bufsize)
2374                 RETURN(-EINVAL);
2375         req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2376                              RCL_SERVER, nr * sizeof(__u32));
2377         req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2378                              RCL_SERVER, nr * sizeof(struct lu_fid));
2379         rc = req_capsule_server_pack(tsi->tsi_pill);
2380         if (rc)
2381                 GOTO(out, rc = err_serious(rc));
2382         fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2383         if (fids == NULL)
2384                 RETURN(-EPROTO);
2385         rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2386         LASSERT(rcs);
2387         rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2388         LASSERT(rfids);
2389
2390         mdt_init_ucred(mti, reqbody);
2391         for (i = 0; i < nr; i++) {
2392                 rfids[i] = fids[i];
2393                 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2394         }
2395         mdt_exit_ucred(mti);
2396
2397 out:
2398         RETURN(rc);
2399 }
2400
2401 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2402                          void *karg, void __user *uarg);
2403
2404 static int mdt_set_info(struct tgt_session_info *tsi)
2405 {
2406         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2407         char                    *key;
2408         void                    *val;
2409         int                      keylen, vallen, rc = 0;
2410
2411         ENTRY;
2412
2413         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2414         if (key == NULL) {
2415                 DEBUG_REQ(D_HA, req, "no set_info key");
2416                 RETURN(err_serious(-EFAULT));
2417         }
2418
2419         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2420                                       RCL_CLIENT);
2421
2422         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2423         if (val == NULL) {
2424                 DEBUG_REQ(D_HA, req, "no set_info val");
2425                 RETURN(err_serious(-EFAULT));
2426         }
2427
2428         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2429                                       RCL_CLIENT);
2430
2431         /* Swab any part of val you need to here */
2432         if (KEY_IS(KEY_READ_ONLY)) {
2433                 spin_lock(&req->rq_export->exp_lock);
2434                 if (*(__u32 *)val)
2435                         *exp_connect_flags_ptr(req->rq_export) |=
2436                                 OBD_CONNECT_RDONLY;
2437                 else
2438                         *exp_connect_flags_ptr(req->rq_export) &=
2439                                 ~OBD_CONNECT_RDONLY;
2440                 spin_unlock(&req->rq_export->exp_lock);
2441         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2442                 struct changelog_setinfo *cs = val;
2443
2444                 if (vallen != sizeof(*cs)) {
2445                         CERROR("%s: bad changelog_clear setinfo size %d\n",
2446                                tgt_name(tsi->tsi_tgt), vallen);
2447                         RETURN(-EINVAL);
2448                 }
2449                 if (ptlrpc_req_need_swab(req)) {
2450                         __swab64s(&cs->cs_recno);
2451                         __swab32s(&cs->cs_id);
2452                 }
2453
2454                 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2455                         RETURN(-EACCES);
2456                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2457                                    vallen, val, NULL);
2458         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2459                 if (vallen > 0)
2460                         obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2461         } else {
2462                 RETURN(-EINVAL);
2463         }
2464         RETURN(rc);
2465 }
2466
2467 static int mdt_readpage(struct tgt_session_info *tsi)
2468 {
2469         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
2470         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
2471         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2472         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
2473         struct mdt_body         *repbody;
2474         int                      rc;
2475         int                      i;
2476
2477         ENTRY;
2478
2479         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2480                 RETURN(err_serious(-ENOMEM));
2481
2482         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2483         if (repbody == NULL || reqbody == NULL)
2484                 RETURN(err_serious(-EFAULT));
2485
2486         /*
2487          * prepare @rdpg before calling lower layers and transfer itself. Here
2488          * reqbody->size contains offset of where to start to read and
2489          * reqbody->nlink contains number bytes to read.
2490          */
2491         rdpg->rp_hash = reqbody->mbo_size;
2492         if (rdpg->rp_hash != reqbody->mbo_size) {
2493                 CERROR("Invalid hash: %#llx != %#llx\n",
2494                        rdpg->rp_hash, reqbody->mbo_size);
2495                 RETURN(-EFAULT);
2496         }
2497
2498         rdpg->rp_attrs = reqbody->mbo_mode;
2499         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2500                 rdpg->rp_attrs |= LUDA_64BITHASH;
2501         rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
2502                                 exp_max_brw_size(tsi->tsi_exp));
2503         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2504                           PAGE_SHIFT;
2505         OBD_ALLOC_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2506         if (rdpg->rp_pages == NULL)
2507                 RETURN(-ENOMEM);
2508
2509         for (i = 0; i < rdpg->rp_npages; ++i) {
2510                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2511                 if (rdpg->rp_pages[i] == NULL)
2512                         GOTO(free_rdpg, rc = -ENOMEM);
2513         }
2514
2515         /* call lower layers to fill allocated pages with directory data */
2516         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2517         if (rc < 0)
2518                 GOTO(free_rdpg, rc);
2519
2520         /* send pages to client */
2521         rc = tgt_sendpage(tsi, rdpg, rc);
2522
2523         EXIT;
2524 free_rdpg:
2525
2526         for (i = 0; i < rdpg->rp_npages; i++)
2527                 if (rdpg->rp_pages[i] != NULL)
2528                         __free_page(rdpg->rp_pages[i]);
2529         OBD_FREE_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2530
2531         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2532                 RETURN(0);
2533
2534         return rc;
2535 }
2536
2537 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2538 {
2539         struct lu_ucred *uc = mdt_ucred_check(info);
2540         struct lu_attr *attr = &info->mti_attr.ma_attr;
2541
2542         if (uc == NULL)
2543                 return -EINVAL;
2544
2545         if (op != REINT_SETATTR) {
2546                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2547                         attr->la_uid = uc->uc_fsuid;
2548                 /* for S_ISGID, inherit gid from his parent, such work will be
2549                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2550                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2551                         attr->la_gid = uc->uc_fsgid;
2552         }
2553
2554         return 0;
2555 }
2556
2557 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2558 {
2559         return op == REINT_OPEN &&
2560              !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2561 }
2562
2563 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2564 {
2565         struct req_capsule *pill = info->mti_pill;
2566
2567         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2568                                   RCL_SERVER) &&
2569             req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2570                                   RCL_CLIENT)) {
2571                 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2572                                          RCL_CLIENT) != 0) {
2573                         /* pre-set size in server part with max size */
2574                         req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2575                                              RCL_SERVER,
2576                                              info->mti_mdt->mdt_max_ea_size);
2577                 } else {
2578                         req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2579                                              RCL_SERVER, 0);
2580                 }
2581         }
2582
2583 }
2584
2585 static int mdt_reint_internal(struct mdt_thread_info *info,
2586                               struct mdt_lock_handle *lhc,
2587                               __u32 op)
2588 {
2589         struct req_capsule      *pill = info->mti_pill;
2590         struct mdt_body         *repbody;
2591         int                      rc = 0, rc2;
2592
2593         ENTRY;
2594
2595         rc = mdt_reint_unpack(info, op);
2596         if (rc != 0) {
2597                 CERROR("Can't unpack reint, rc %d\n", rc);
2598                 RETURN(err_serious(rc));
2599         }
2600
2601
2602         /* check if the file system is set to readonly. O_RDONLY open
2603          * is still allowed even the file system is set to readonly mode */
2604         if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2605                 RETURN(err_serious(-EROFS));
2606
2607         /* for replay (no_create) lmm is not needed, client has it already */
2608         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2609                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2610                                      DEF_REP_MD_SIZE);
2611
2612         /* llog cookies are always 0, the field is kept for compatibility */
2613         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2614                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2615
2616         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2617          * by default. If the target object has more ACL entries, then
2618          * enlarge the buffer when necessary. */
2619         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2620                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2621                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2622
2623         mdt_preset_secctx_size(info);
2624
2625         rc = req_capsule_server_pack(pill);
2626         if (rc != 0) {
2627                 CERROR("Can't pack response, rc %d\n", rc);
2628                 RETURN(err_serious(rc));
2629         }
2630
2631         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2632                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2633                 LASSERT(repbody);
2634                 repbody->mbo_eadatasize = 0;
2635                 repbody->mbo_aclsize = 0;
2636         }
2637
2638         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2639
2640         /* for replay no cookkie / lmm need, because client have this already */
2641         if (info->mti_spec.no_create)
2642                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2643                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2644
2645         rc = mdt_init_ucred_reint(info);
2646         if (rc)
2647                 GOTO(out_shrink, rc);
2648
2649         rc = mdt_fix_attr_ucred(info, op);
2650         if (rc != 0)
2651                 GOTO(out_ucred, rc = err_serious(rc));
2652
2653         rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2654         if (rc < 0) {
2655                 GOTO(out_ucred, rc);
2656         } else if (rc == 1) {
2657                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2658                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2659                 GOTO(out_ucred, rc);
2660         }
2661         rc = mdt_reint_rec(info, lhc);
2662         EXIT;
2663 out_ucred:
2664         mdt_exit_ucred(info);
2665 out_shrink:
2666         mdt_client_compatibility(info);
2667
2668         rc2 = mdt_fix_reply(info);
2669         if (rc == 0)
2670                 rc = rc2;
2671
2672         /*
2673          * Data-on-MDT optimization - read data along with OPEN and return it
2674          * in reply when possible.
2675          */
2676         if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2677                 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2678                                           &lhc->mlh_reg_lh);
2679
2680         return rc;
2681 }
2682
2683 static long mdt_reint_opcode(struct ptlrpc_request *req,
2684                              const struct req_format **fmt)
2685 {
2686         struct mdt_device       *mdt;
2687         struct mdt_rec_reint    *rec;
2688         long                     opc;
2689
2690         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2691         if (rec != NULL) {
2692                 opc = rec->rr_opcode;
2693                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2694                 if (opc < REINT_MAX && fmt[opc] != NULL)
2695                         req_capsule_extend(&req->rq_pill, fmt[opc]);
2696                 else {
2697                         mdt = mdt_exp2dev(req->rq_export);
2698                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2699                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
2700                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2701                         opc = err_serious(-EFAULT);
2702                 }
2703         } else {
2704                 opc = err_serious(-EFAULT);
2705         }
2706         return opc;
2707 }
2708
2709 static int mdt_reint(struct tgt_session_info *tsi)
2710 {
2711         long opc;
2712         int  rc;
2713         static const struct req_format *reint_fmts[REINT_MAX] = {
2714                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
2715                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
2716                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
2717                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
2718                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
2719                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
2720                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2721                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
2722                 [REINT_MIGRATE]  = &RQF_MDS_REINT_MIGRATE,
2723                 [REINT_RESYNC]   = &RQF_MDS_REINT_RESYNC,
2724         };
2725
2726         ENTRY;
2727
2728         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2729         if (opc >= 0) {
2730                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2731                 /*
2732                  * No lock possible here from client to pass it to reint code
2733                  * path.
2734                  */
2735                 rc = mdt_reint_internal(info, NULL, opc);
2736                 mdt_thread_info_fini(info);
2737         } else {
2738                 rc = opc;
2739         }
2740
2741         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2742         RETURN(rc);
2743 }
2744
2745 /* this should sync the whole device */
2746 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2747 {
2748         struct dt_device *dt = mdt->mdt_bottom;
2749         int rc;
2750         ENTRY;
2751
2752         rc = dt->dd_ops->dt_sync(env, dt);
2753         RETURN(rc);
2754 }
2755
2756 /* this should sync this object */
2757 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2758                            struct mdt_object *mo)
2759 {
2760         int rc = 0;
2761
2762         ENTRY;
2763
2764         if (!mdt_object_exists(mo)) {
2765                 CWARN("%s: non existing object "DFID": rc = %d\n",
2766                       exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2767                       -ESTALE);
2768                 RETURN(-ESTALE);
2769         }
2770
2771         if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2772                 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2773                 dt_obj_version_t version;
2774
2775                 version = dt_version_get(env, mdt_obj2dt(mo));
2776                 if (version > tgt->lut_obd->obd_last_committed)
2777                         rc = mo_object_sync(env, mdt_object_child(mo));
2778         } else {
2779                 rc = mo_object_sync(env, mdt_object_child(mo));
2780         }
2781
2782         RETURN(rc);
2783 }
2784
2785 static int mdt_sync(struct tgt_session_info *tsi)
2786 {
2787         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2788         struct req_capsule      *pill = tsi->tsi_pill;
2789         struct mdt_body         *body;
2790         ktime_t                  kstart = ktime_get();
2791         int                      rc;
2792
2793         ENTRY;
2794
2795         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2796                 RETURN(err_serious(-ENOMEM));
2797
2798         if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2799                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2800         } else {
2801                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2802
2803                 if (unlikely(info->mti_object == NULL))
2804                         RETURN(-EPROTO);
2805
2806                 /* sync an object */
2807                 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2808                                      info->mti_object);
2809                 if (rc == 0) {
2810                         const struct lu_fid *fid;
2811                         struct lu_attr *la = &info->mti_attr.ma_attr;
2812
2813                         info->mti_attr.ma_need = MA_INODE;
2814                         info->mti_attr.ma_valid = 0;
2815                         rc = mdt_attr_get_complex(info, info->mti_object,
2816                                                   &info->mti_attr);
2817                         if (rc == 0) {
2818                                 body = req_capsule_server_get(pill,
2819                                                               &RMF_MDT_BODY);
2820                                 fid = mdt_object_fid(info->mti_object);
2821                                 mdt_pack_attr2body(info, body, la, fid);
2822                         }
2823                 }
2824                 mdt_thread_info_fini(info);
2825         }
2826         if (rc == 0)
2827                 mdt_counter_incr(req, LPROC_MDT_SYNC,
2828                                  ktime_us_delta(ktime_get(), kstart));
2829
2830         RETURN(rc);
2831 }
2832
2833 static int mdt_data_sync(struct tgt_session_info *tsi)
2834 {
2835         struct mdt_thread_info *info;
2836         struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2837         struct ost_body *body = tsi->tsi_ost_body;
2838         struct ost_body *repbody;
2839         struct mdt_object *mo = NULL;
2840         struct md_attr *ma;
2841         int rc = 0;
2842
2843         ENTRY;
2844
2845         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2846
2847         /* if no fid is specified then do nothing,
2848          * device sync is done via MDS_SYNC */
2849         if (fid_is_zero(&tsi->tsi_fid))
2850                 RETURN(0);
2851
2852         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2853         if (IS_ERR(mo))
2854                 RETURN(PTR_ERR(mo));
2855
2856         rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2857         if (rc)
2858                 GOTO(put, rc);
2859
2860         repbody->oa.o_oi = body->oa.o_oi;
2861         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2862
2863         info = tsi2mdt_info(tsi);
2864         ma = &info->mti_attr;
2865         ma->ma_need = MA_INODE;
2866         ma->ma_valid = 0;
2867         rc = mdt_attr_get_complex(info, mo, ma);
2868         if (rc == 0)
2869                 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2870         else
2871                 rc = 0;
2872         mdt_thread_info_fini(info);
2873
2874         EXIT;
2875 put:
2876         if (mo != NULL)
2877                 mdt_object_put(tsi->tsi_env, mo);
2878         return rc;
2879 }
2880
2881 /*
2882  * Handle quota control requests to consult current usage/limit, but also
2883  * to configure quota enforcement
2884  */
2885 static int mdt_quotactl(struct tgt_session_info *tsi)
2886 {
2887         struct obd_export *exp  = tsi->tsi_exp;
2888         struct req_capsule *pill = tsi->tsi_pill;
2889         struct obd_quotactl *oqctl, *repoqc;
2890         int id, rc;
2891         struct mdt_device *mdt = mdt_exp2dev(exp);
2892         struct lu_device *qmt = mdt->mdt_qmt_dev;
2893         struct lu_nodemap *nodemap;
2894         ENTRY;
2895
2896         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2897         if (!oqctl)
2898                 RETURN(err_serious(-EPROTO));
2899
2900         rc = req_capsule_server_pack(pill);
2901         if (rc)
2902                 RETURN(err_serious(rc));
2903
2904         nodemap = nodemap_get_from_exp(exp);
2905         if (IS_ERR(nodemap))
2906                 RETURN(PTR_ERR(nodemap));
2907
2908         switch (oqctl->qc_cmd) {
2909                 /* master quotactl */
2910         case Q_SETINFO:
2911         case Q_SETQUOTA:
2912         case LUSTRE_Q_SETDEFAULT:
2913         case LUSTRE_Q_SETQUOTAPOOL:
2914         case LUSTRE_Q_SETINFOPOOL:
2915                 if (!nodemap_can_setquota(nodemap))
2916                         GOTO(out_nodemap, rc = -EPERM);
2917                 /* fallthrough */
2918         case Q_GETINFO:
2919         case Q_GETQUOTA:
2920         case LUSTRE_Q_GETDEFAULT:
2921         case LUSTRE_Q_GETQUOTAPOOL:
2922         case LUSTRE_Q_GETINFOPOOL:
2923                 if (qmt == NULL)
2924                         GOTO(out_nodemap, rc = -EOPNOTSUPP);
2925                 /* slave quotactl */
2926                 /* fallthrough */
2927         case Q_GETOINFO:
2928         case Q_GETOQUOTA:
2929                 break;
2930         default:
2931                 rc = -EFAULT;
2932                 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2933                        mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2934                 GOTO(out_nodemap, rc);
2935         }
2936
2937         id = oqctl->qc_id;
2938         switch (oqctl->qc_type) {
2939         case USRQUOTA:
2940                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2941                                     NODEMAP_CLIENT_TO_FS, id);
2942                 break;
2943         case GRPQUOTA:
2944                 id = nodemap_map_id(nodemap, NODEMAP_GID,
2945                                     NODEMAP_CLIENT_TO_FS, id);
2946                 break;
2947         case PRJQUOTA:
2948                 /* todo: check/map project id */
2949                 id = oqctl->qc_id;
2950                 break;
2951         default:
2952                 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2953         }
2954         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2955         if (repoqc == NULL)
2956                 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2957
2958         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2959                 barrier_exit(tsi->tsi_tgt->lut_bottom);
2960
2961         if (oqctl->qc_id != id)
2962                 swap(oqctl->qc_id, id);
2963
2964         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2965                 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2966                         RETURN(-EINPROGRESS);
2967         }
2968
2969         switch (oqctl->qc_cmd) {
2970
2971         case Q_GETINFO:
2972         case Q_SETINFO:
2973         case Q_SETQUOTA:
2974         case Q_GETQUOTA:
2975         case LUSTRE_Q_SETDEFAULT:
2976         case LUSTRE_Q_GETDEFAULT:
2977         case LUSTRE_Q_SETQUOTAPOOL:
2978         case LUSTRE_Q_GETQUOTAPOOL:
2979         case LUSTRE_Q_SETINFOPOOL:
2980         case LUSTRE_Q_GETINFOPOOL:
2981                 /* forward quotactl request to QMT */
2982                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2983                 break;
2984
2985         case Q_GETOINFO:
2986         case Q_GETOQUOTA:
2987                 /* slave quotactl */
2988                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2989                                    oqctl);
2990                 break;
2991
2992         default:
2993                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2994                 GOTO(out_nodemap, rc = -EFAULT);
2995         }
2996
2997         if (oqctl->qc_id != id)
2998                 swap(oqctl->qc_id, id);
2999
3000         QCTL_COPY(repoqc, oqctl);
3001         EXIT;
3002
3003 out_nodemap:
3004         nodemap_putref(nodemap);
3005
3006         return rc;
3007 }
3008
3009 /** clone llog ctxt from child (mdd)
3010  * This allows remote llog (replicator) access.
3011  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3012  * context was originally set up, or we can handle them directly.
3013  * I choose the latter, but that means I need any llog
3014  * contexts set up by child to be accessable by the mdt.  So we clone the
3015  * context into our context list here.
3016  */
3017 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3018                                int idx)
3019 {
3020         struct md_device  *next = mdt->mdt_child;
3021         struct llog_ctxt *ctxt;
3022         int rc;
3023
3024         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3025                 return 0;
3026
3027         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3028         if (rc || ctxt == NULL) {
3029                 return 0;
3030         }
3031
3032         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3033         if (rc)
3034                 CERROR("Can't set mdt ctxt %d\n", rc);
3035
3036         return rc;
3037 }
3038
3039 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3040                                  struct mdt_device *mdt, int idx)
3041 {
3042         struct llog_ctxt *ctxt;
3043
3044         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3045         if (ctxt == NULL)
3046                 return 0;
3047         /* Put once for the get we just did, and once for the clone */
3048         llog_ctxt_put(ctxt);
3049         llog_ctxt_put(ctxt);
3050         return 0;
3051 }
3052
3053 /*
3054  * sec context handlers
3055  */
3056 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3057 {
3058         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3059
3060         return 0;
3061 }
3062
3063 /*
3064  * quota request handlers
3065  */
3066 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3067 {
3068         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
3069         struct lu_device        *qmt = mdt->mdt_qmt_dev;
3070         int                      rc;
3071         ENTRY;
3072
3073         if (qmt == NULL)
3074                 RETURN(err_serious(-EOPNOTSUPP));
3075
3076         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3077         RETURN(rc);
3078 }
3079
3080 struct mdt_object *mdt_object_new(const struct lu_env *env,
3081                                   struct mdt_device *d,
3082                                   const struct lu_fid *f)
3083 {
3084         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3085         struct lu_object *o;
3086         struct mdt_object *m;
3087         ENTRY;
3088
3089         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3090         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3091         if (unlikely(IS_ERR(o)))
3092                 m = (struct mdt_object *)o;
3093         else
3094                 m = mdt_obj(o);
3095         RETURN(m);
3096 }
3097
3098 struct mdt_object *mdt_object_find(const struct lu_env *env,
3099                                    struct mdt_device *d,
3100                                    const struct lu_fid *f)
3101 {
3102         struct lu_object *o;
3103         struct mdt_object *m;
3104         ENTRY;
3105
3106         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3107         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3108         if (unlikely(IS_ERR(o)))
3109                 m = (struct mdt_object *)o;
3110         else
3111                 m = mdt_obj(o);
3112
3113         RETURN(m);
3114 }
3115
3116 /**
3117  * Asyncronous commit for mdt device.
3118  *
3119  * Pass asynchonous commit call down the MDS stack.
3120  *
3121  * \param env environment
3122  * \param mdt the mdt device
3123  */
3124 static void mdt_device_commit_async(const struct lu_env *env,
3125                                     struct mdt_device *mdt)
3126 {
3127         struct dt_device *dt = mdt->mdt_bottom;
3128         int rc;
3129         ENTRY;
3130
3131         rc = dt->dd_ops->dt_commit_async(env, dt);
3132         if (unlikely(rc != 0))
3133                 CWARN("%s: async commit start failed: rc = %d\n",
3134                       mdt_obd_name(mdt), rc);
3135         atomic_inc(&mdt->mdt_async_commit_count);
3136         EXIT;
3137 }
3138
3139 /**
3140  * Mark the lock as "synchonous".
3141  *
3142  * Mark the lock to deffer transaction commit to the unlock time.
3143  *
3144  * \param lock the lock to mark as "synchonous"
3145  *
3146  * \see mdt_is_lock_sync
3147  * \see mdt_save_lock
3148  */
3149 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3150 {
3151         lock->l_ast_data = (void*)1;
3152 }
3153
3154 /**
3155  * Check whehter the lock "synchonous" or not.
3156  *
3157  * \param lock the lock to check
3158  * \retval 1 the lock is "synchonous"
3159  * \retval 0 the lock isn't "synchronous"
3160  *
3161  * \see mdt_set_lock_sync
3162  * \see mdt_save_lock
3163  */
3164 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3165 {
3166         return lock->l_ast_data != NULL;
3167 }
3168
3169 /**
3170  * Blocking AST for mdt locks.
3171  *
3172  * Starts transaction commit if in case of COS lock conflict or
3173  * deffers such a commit to the mdt_save_lock.
3174  *
3175  * \param lock the lock which blocks a request or cancelling lock