Whamcloud - gitweb
LU-9859 libcfs: don't call unshare_fs_struct()
[fs/lustre-release.git] / lustre / mdd / mdd_object.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) 2011, 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/mdd/mdd_object.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <linux/module.h>
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lprocfs_status.h>
46 /* fid_be_cpu(), fid_cpu_to_be(). */
47 #include <lustre_fid.h>
48 #include <lustre_idmap.h>
49 #include <uapi/linux/lustre/lustre_param.h>
50 #include <lustre_mds.h>
51
52 #include "mdd_internal.h"
53
54 static const struct lu_object_operations mdd_lu_obj_ops;
55
56 struct mdd_object_user {
57         struct list_head        mou_list;       /**< linked off mod_users */
58         u64                     mou_open_flags; /**< open mode by client */
59         __u64                   mou_uidgid;     /**< uid_gid on client */
60         int                     mou_opencount;  /**< # opened */
61         ktime_t                 mou_deniednext; /**< time of next access denied
62                                                  * notfication
63                                                  */
64 };
65
66 static int mdd_xattr_get(const struct lu_env *env,
67                          struct md_object *obj, struct lu_buf *buf,
68                          const char *name);
69
70 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
71                                            struct mdd_device *mdd,
72                                            enum changelog_rec_type type,
73                                            enum changelog_rec_flags clf_flags,
74                                            const struct lu_fid *fid,
75                                            const char *xattr_name,
76                                            struct thandle *handle);
77
78 static inline bool has_prefix(const char *str, const char *prefix);
79
80
81 static u32 flags_helper(u64 open_flags)
82 {
83         u32 open_mode = 0;
84
85         if (open_flags & MDS_FMODE_EXEC) {
86                 open_mode = MDS_FMODE_EXEC;
87         } else {
88                 if (open_flags & MDS_FMODE_READ)
89                         open_mode = MDS_FMODE_READ;
90                 if (open_flags &
91                     (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
92                         open_mode |= MDS_FMODE_WRITE;
93         }
94
95         return open_mode;
96 }
97
98 /** Allocate/init a user and its sub-structures.
99  *
100  * \param flags [IN]
101  * \param uid [IN]
102  * \param gid [IN]
103  * \retval mou [OUT] success valid structure
104  * \retval mou [OUT]
105  */
106 static struct mdd_object_user *mdd_obj_user_alloc(u64 open_flags,
107                                                   uid_t uid, gid_t gid)
108 {
109         struct mdd_object_user *mou;
110
111         ENTRY;
112
113         OBD_SLAB_ALLOC_PTR(mou, mdd_object_kmem);
114         if (mou == NULL)
115                 RETURN(ERR_PTR(-ENOMEM));
116
117         mou->mou_open_flags = open_flags;
118         mou->mou_uidgid = ((__u64)uid << 32) | gid;
119         mou->mou_opencount = 0;
120         mou->mou_deniednext = ktime_set(0, 0);
121
122         RETURN(mou);
123 }
124
125 /**
126  * Free a user and its sub-structures.
127  *
128  * \param mou [IN]  user to be freed.
129  */
130 static void mdd_obj_user_free(struct mdd_object_user *mou)
131 {
132         OBD_SLAB_FREE_PTR(mou, mdd_object_kmem);
133 }
134
135 /**
136  * Find if UID/GID already has this file open
137  *
138  * Caller should have write-locked \param mdd_obj.
139  * \param mdd_obj [IN] mdd_obj
140  * \param uid [IN] client uid
141  * \param gid [IN] client gid
142  * \retval user pointer or NULL if not found
143  */
144 static
145 struct mdd_object_user *mdd_obj_user_find(struct mdd_object *mdd_obj,
146                                           uid_t uid, gid_t gid,
147                                           u64 open_flags)
148 {
149         struct mdd_object_user *mou;
150         __u64 uidgid;
151
152         ENTRY;
153
154         uidgid = ((__u64)uid << 32) | gid;
155         list_for_each_entry(mou, &mdd_obj->mod_users, mou_list) {
156                 if (mou->mou_uidgid == uidgid &&
157                     flags_helper(mou->mou_open_flags) ==
158                     flags_helper(open_flags))
159                         RETURN(mou);
160         }
161         RETURN(NULL);
162 }
163
164 /**
165  * Add a user to the list of openers for this file
166  *
167  * Caller should have write-locked \param mdd_obj.
168  * \param mdd_obj [IN] mdd_obj
169  * \param mou [IN] user
170  * \retval 0 success
171  * \retval -ve failure
172  */
173 static int mdd_obj_user_add(struct mdd_object *mdd_obj,
174                             struct mdd_object_user *mou,
175                             bool denied)
176 {
177         struct mdd_device *mdd = mdd_obj2mdd_dev(mdd_obj);
178         struct mdd_object_user *tmp;
179         __u32 uid = mou->mou_uidgid >> 32;
180         __u32 gid = mou->mou_uidgid & ((1UL << 32) - 1);
181
182         ENTRY;
183         tmp = mdd_obj_user_find(mdd_obj, uid, gid, mou->mou_open_flags);
184         if (tmp != NULL)
185                 RETURN(-EEXIST);
186
187         list_add_tail(&mou->mou_list, &mdd_obj->mod_users);
188
189         if (denied)
190                 /* next 'access denied' notification cannot happen before
191                  * mou_deniednext
192                  */
193                 mou->mou_deniednext =
194                         ktime_add(ktime_get(),
195                                   ktime_set(mdd->mdd_cl.mc_deniednext, 0));
196         else
197                 mou->mou_opencount++;
198
199         RETURN(0);
200 }
201 /**
202  * Remove UID from the list
203  *
204  * Caller should have write-locked \param mdd_obj.
205  * \param mdd_obj [IN] mdd_obj
206  * \param uid [IN] user
207  * \retval -ve failure
208  */
209 static int mdd_obj_user_remove(struct mdd_object *mdd_obj,
210                                struct mdd_object_user *mou)
211 {
212         ENTRY;
213
214         if (mou == NULL)
215                 RETURN(-ENOENT);
216
217         list_del_init(&mou->mou_list);
218
219         mdd_obj_user_free(mou);
220
221         RETURN(0);
222 }
223 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
224                struct lu_attr *la)
225 {
226         int rc;
227
228         if (mdd_object_exists(obj) == 0)
229                 return -ENOENT;
230
231         rc = mdo_attr_get(env, obj, la);
232         if (unlikely(rc != 0)) {
233                 if (rc == -ENOENT)
234                         obj->mod_flags |= DEAD_OBJ;
235                 return rc;
236         }
237
238         if (la->la_valid & LA_FLAGS && la->la_flags & LUSTRE_ORPHAN_FL)
239                 obj->mod_flags |= ORPHAN_OBJ | DEAD_OBJ;
240
241         return 0;
242 }
243
244 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
245 {
246         return lu_env_info(env, &mdd_thread_key);
247 }
248
249 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
250 {
251         struct lu_buf *buf;
252
253         buf = &mdd_env_info(env)->mti_buf[0];
254         buf->lb_buf = area;
255         buf->lb_len = len;
256         return buf;
257 }
258
259 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
260                                        const void *area, ssize_t len)
261 {
262         struct lu_buf *buf;
263
264         buf = &mdd_env_info(env)->mti_buf[0];
265         buf->lb_buf = (void *)area;
266         buf->lb_len = len;
267         return buf;
268 }
269
270 struct lu_object *mdd_object_alloc(const struct lu_env *env,
271                                    const struct lu_object_header *hdr,
272                                    struct lu_device *d)
273 {
274         struct mdd_object *mdd_obj;
275         struct lu_object *o;
276
277         OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
278         if (!mdd_obj)
279                 return NULL;
280
281         o = mdd2lu_obj(mdd_obj);
282         lu_object_init(o, NULL, d);
283         mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
284         mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
285         mdd_obj->mod_count = 0;
286         o->lo_ops = &mdd_lu_obj_ops;
287         INIT_LIST_HEAD(&mdd_obj->mod_users);
288
289         return o;
290 }
291
292 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
293                            const struct lu_object_conf *unused)
294 {
295         struct mdd_device *d = lu2mdd_dev(o->lo_dev);
296         struct mdd_object *mdd_obj = lu2mdd_obj(o);
297         struct lu_object  *below;
298         struct lu_device  *under;
299         ENTRY;
300
301         mdd_obj->mod_cltime = ktime_set(0, 0);
302         under = &d->mdd_child->dd_lu_dev;
303         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
304         if (IS_ERR(below))
305                 RETURN(PTR_ERR(below));
306
307         lu_object_add(o, below);
308
309         RETURN(0);
310 }
311
312 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
313 {
314         int rc = 0;
315
316         if (lu_object_exists(o)) {
317                 struct mdd_object *mdd_obj = lu2mdd_obj(o);
318                 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
319
320                 rc = mdd_la_get(env, mdd_obj, attr);
321         }
322
323         return rc;
324 }
325
326 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
327 {
328         struct mdd_object *mdd = lu2mdd_obj(o);
329         struct mdd_object_user *mou, *tmp2;
330
331         /* free user list */
332         list_for_each_entry_safe(mou, tmp2, &mdd->mod_users, mou_list) {
333                 list_del(&mou->mou_list);
334                 mdd_obj_user_free(mou);
335         }
336
337         lu_object_fini(o);
338         /* mdd doesn't contain an lu_object_header, so don't need call_rcu */
339         OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
340 }
341
342 static int mdd_object_print(const struct lu_env *env, void *cookie,
343                             lu_printer_t p, const struct lu_object *o)
344 {
345         struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
346
347         return (*p)(env, cookie,
348                     LUSTRE_MDD_NAME"-object@%p(open_count=%d, valid=%x, cltime=%lldns, flags=%lx)",
349                     mdd, mdd->mod_count, mdd->mod_valid,
350                     ktime_to_ns(mdd->mod_cltime), mdd->mod_flags);
351 }
352
353 static const struct lu_object_operations mdd_lu_obj_ops = {
354         .loo_object_init    = mdd_object_init,
355         .loo_object_start   = mdd_object_start,
356         .loo_object_free    = mdd_object_free,
357         .loo_object_print   = mdd_object_print,
358 };
359
360 struct mdd_object *mdd_object_find(const struct lu_env *env,
361                                    struct mdd_device *d,
362                                    const struct lu_fid *f)
363 {
364         return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
365 }
366
367 /*
368  * No permission check is needed.
369  */
370 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
371                  struct md_attr *ma)
372 {
373         struct mdd_object *mdd_obj = md2mdd_obj(obj);
374         int               rc;
375
376         ENTRY;
377
378         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
379         if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
380                 ma->ma_attr.la_nlink = 0;
381
382         RETURN(rc);
383 }
384
385 /*
386  * No permission check is needed.
387  */
388 static int mdd_xattr_get(const struct lu_env *env,
389                          struct md_object *obj, struct lu_buf *buf,
390                          const char *name)
391 {
392         struct mdd_object *mdd_obj = md2mdd_obj(obj);
393         struct mdd_device *mdd;
394         int rc;
395
396         ENTRY;
397
398         if (mdd_object_exists(mdd_obj) == 0) {
399                 CERROR("%s: object "DFID" not found: rc = -2\n",
400                        mdd_obj_dev_name(mdd_obj),
401                        PFID(mdd_object_fid(mdd_obj)));
402                 return -ENOENT;
403         }
404
405         /* If the object has been destroyed, then do not get LMVEA, because
406          * it needs to load stripes from the iteration of the master object,
407          * and it will cause problem if master object has been destroyed, see
408          * LU-6427 */
409         if (unlikely((mdd_obj->mod_flags & DEAD_OBJ) &&
410                      !(mdd_obj->mod_flags & ORPHAN_OBJ) &&
411                       strcmp(name, XATTR_NAME_LMV) == 0))
412                 RETURN(-ENOENT);
413
414         /* If the object has been delete from the namespace, then
415          * get linkEA should return -ENOENT as well */
416         if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
417                       strcmp(name, XATTR_NAME_LINK) == 0))
418                 RETURN(-ENOENT);
419
420         mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
421         rc = mdo_xattr_get(env, mdd_obj, buf, name);
422         mdd_read_unlock(env, mdd_obj);
423
424         mdd = mdo2mdd(obj);
425
426         /* record only getting user xattrs and acls */
427         if (rc >= 0 && buf->lb_buf &&
428             mdd_changelog_enabled(env, mdd, CL_GETXATTR) &&
429             (has_prefix(name, XATTR_USER_PREFIX) ||
430              has_prefix(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
431              has_prefix(name, XATTR_NAME_POSIX_ACL_DEFAULT))) {
432                 struct thandle *handle;
433                 int rc2;
434
435                 LASSERT(mdd_object_fid(mdd_obj) != NULL);
436
437                 handle = mdd_trans_create(env, mdd);
438                 if (IS_ERR(handle))
439                         RETURN(PTR_ERR(handle));
440
441                 rc2 = mdd_declare_changelog_store(env, mdd, CL_GETXATTR, NULL,
442                                                   NULL, handle);
443                 if (rc2)
444                         GOTO(stop, rc2);
445
446                 rc2 = mdd_trans_start(env, mdd, handle);
447                 if (rc2)
448                         GOTO(stop, rc2);
449
450                 rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
451                                                       mdd_object_fid(mdd_obj),
452                                                       name, handle);
453
454 stop:
455                 rc2 = mdd_trans_stop(env, mdd, rc2, handle);
456                 if (rc2)
457                         rc = rc2;
458         }
459
460         RETURN(rc);
461 }
462
463 /*
464  * Permission check is done when open,
465  * no need check again.
466  */
467 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
468                  struct lu_buf *buf)
469 {
470         struct mdd_object *mdd_obj = md2mdd_obj(obj);
471         struct dt_object  *next;
472         loff_t             pos = 0;
473         int                rc;
474         ENTRY;
475
476         if (mdd_object_exists(mdd_obj) == 0) {
477                 CERROR("%s: object "DFID" not found: rc = -2\n",
478                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
479                 return -ENOENT;
480         }
481
482         next = mdd_object_child(mdd_obj);
483         LASSERT(next != NULL);
484         LASSERT(next->do_body_ops != NULL);
485         LASSERT(next->do_body_ops->dbo_read != NULL);
486         mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
487         rc = dt_read(env, next, buf, &pos);
488         mdd_read_unlock(env, mdd_obj);
489         RETURN(rc);
490 }
491
492 /*
493  * No permission check is needed.
494  */
495 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
496                           struct lu_buf *buf)
497 {
498         struct mdd_object *mdd_obj = md2mdd_obj(obj);
499         int rc;
500
501         ENTRY;
502
503         mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
504         rc = mdo_xattr_list(env, mdd_obj, buf);
505         mdd_read_unlock(env, mdd_obj);
506
507         /* If the buffer is NULL then we are only here to get the
508          * length of the xattr name list. */
509         if (rc < 0 || buf->lb_buf == NULL)
510                 RETURN(rc);
511
512         /*
513          * Filter out XATTR_NAME_LINK if this is an orphan object.  See
514          * mdd_xattr_get().
515          */
516         if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
517                 char   *end = (char *)buf->lb_buf + rc;
518                 char   *p = buf->lb_buf;
519
520                 while (p < end) {
521                         char   *next = p + strlen(p) + 1;
522
523                         if (strcmp(p, XATTR_NAME_LINK) == 0) {
524                                 if (end - next > 0)
525                                         memmove(p, next, end - next);
526                                 rc -= next - p;
527                                 CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
528                                        " of orphan "DFID"\n",
529                                        PFID(mdd_object_fid(mdd_obj)));
530                                 break;
531                         }
532
533                         p = next;
534                 }
535         }
536
537         RETURN(rc);
538 }
539
540 int mdd_invalidate(const struct lu_env *env, struct md_object *obj)
541 {
542         return mdo_invalidate(env, md2mdd_obj(obj));
543 }
544
545 int mdd_declare_create_object_internal(const struct lu_env *env,
546                                        struct mdd_object *p,
547                                        struct mdd_object *c,
548                                        struct lu_attr *attr,
549                                        struct thandle *handle,
550                                        const struct md_op_spec *spec,
551                                        struct dt_allocation_hint *hint)
552 {
553         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
554         const struct dt_index_features *feat = spec->sp_feat;
555         int rc;
556         ENTRY;
557
558         if (feat != &dt_directory_features && feat != NULL) {
559                 dof->dof_type = DFT_INDEX;
560                 dof->u.dof_idx.di_feat = feat;
561         } else {
562                 dof->dof_type = dt_mode_to_dft(attr->la_mode);
563                 if (dof->dof_type == DFT_REGULAR) {
564                         dof->u.dof_reg.striped =
565                                 md_should_create(spec->sp_cr_flags);
566                         if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
567                                 dof->u.dof_reg.striped = 0;
568                         /* is this replay? */
569                         if (spec->no_create)
570                                 dof->u.dof_reg.striped = 0;
571                 }
572         }
573
574         rc = mdo_declare_create_object(env, c, attr, hint, dof, handle);
575
576         RETURN(rc);
577 }
578
579 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
580                                struct mdd_object *c, struct lu_attr *attr,
581                                struct thandle *handle,
582                                const struct md_op_spec *spec,
583                                struct dt_allocation_hint *hint)
584 {
585         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
586         int rc;
587         ENTRY;
588
589         LASSERT(!mdd_object_exists(c));
590
591         rc = mdo_create_object(env, c, attr, hint, dof, handle);
592
593         RETURN(rc);
594 }
595
596 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
597                           const struct lu_attr *attr, struct thandle *handle,
598                           int needacl)
599 {
600         int rc;
601         ENTRY;
602
603         rc = mdo_attr_set(env, obj, attr, handle);
604 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
605         if (!rc && (attr->la_valid & LA_MODE) && needacl)
606                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
607 #endif
608         RETURN(rc);
609 }
610
611 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
612                     const struct lu_attr *oattr, struct lu_attr *attr,
613                     struct thandle *handle)
614 {
615         int rc = 0;
616         ENTRY;
617
618         LASSERT(attr->la_valid & LA_CTIME);
619         LASSERT(oattr != NULL);
620
621         /* Make sure the ctime is increased only, however, it's not strictly
622          * reliable at here because there is not guarantee to hold lock on
623          * object, so we just bypass some unnecessary cmtime setting first
624          * and OSD has to check it again. */
625         if (attr->la_ctime < oattr->la_ctime)
626                 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
627         else if (attr->la_valid == LA_CTIME &&
628                  attr->la_ctime == oattr->la_ctime)
629                 attr->la_valid &= ~LA_CTIME;
630
631         if (attr->la_valid != 0)
632                 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
633         RETURN(rc);
634 }
635
636
637 static bool is_project_state_change(const struct lu_attr *oattr,
638                                     struct lu_attr *la)
639 {
640         if (la->la_valid & LA_PROJID &&
641             oattr->la_projid != la->la_projid)
642                 return true;
643
644         if ((la->la_valid & LA_FLAGS) &&
645             (la->la_flags & LUSTRE_PROJINHERIT_FL) !=
646             (oattr->la_flags & LUSTRE_PROJINHERIT_FL))
647                 return true;
648
649         return false;
650 }
651
652 /*
653  * This gives the same functionality as the code between
654  * sys_chmod and inode_setattr
655  * chown_common and inode_setattr
656  * utimes and inode_setattr
657  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
658  */
659 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
660                         const struct lu_attr *oattr, struct lu_attr *la,
661                         const struct md_attr *ma)
662 {
663         struct lu_ucred  *uc;
664         int               rc = 0;
665         const unsigned long flags = ma->ma_attr_flags;
666
667         ENTRY;
668
669         if (!la->la_valid)
670                 RETURN(0);
671
672         /* Do not permit change file type */
673         if (la->la_valid & LA_TYPE)
674                 RETURN(-EPERM);
675
676         /* They should not be processed by setattr */
677         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
678                 RETURN(-EPERM);
679
680         LASSERT(oattr != NULL);
681
682         uc = lu_ucred_check(env);
683         if (uc == NULL)
684                 RETURN(0);
685
686         if (is_project_state_change(oattr, la)) {
687                 if (!md_capable(uc, CFS_CAP_SYS_RESOURCE) &&
688                     !lustre_in_group_p(uc, ma->ma_enable_chprojid_gid) &&
689                     !(ma->ma_enable_chprojid_gid == -1 &&
690                       mdd_permission_internal(env, obj, oattr, MAY_WRITE)))
691                         RETURN(-EPERM);
692         }
693
694         if (la->la_valid == LA_CTIME) {
695                 if (!(flags & MDS_PERM_BYPASS))
696                         /* This is only for set ctime when rename's source is
697                          * on remote MDS. */
698                         rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
699                                             1, 0);
700                 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
701                         la->la_valid &= ~LA_CTIME;
702                 RETURN(rc);
703         }
704
705         if (flags & MDS_CLOSE_UPDATE_TIMES &&
706             la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
707                 /* This is an atime/mtime/ctime attribute update for
708                  * close RPCs.
709                  */
710                 if (la->la_valid & LA_ATIME &&
711                     la->la_atime <= (oattr->la_atime +
712                                 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
713                         la->la_valid &= ~LA_ATIME;
714                 if (la->la_valid & LA_CTIME && la->la_ctime <= oattr->la_ctime)
715                         la->la_valid &= ~LA_CTIME;
716                 if (la->la_valid & LA_MTIME && la->la_mtime <= oattr->la_mtime)
717                         la->la_valid &= ~LA_MTIME;
718                 RETURN(0);
719         }
720
721         /* Check if flags change. */
722         if (la->la_valid & LA_FLAGS) {
723                 unsigned int oldflags = oattr->la_flags &
724                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
725                 unsigned int newflags = la->la_flags &
726                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
727
728                 if ((uc->uc_fsuid != oattr->la_uid) &&
729                     !md_capable(uc, CFS_CAP_FOWNER))
730                         RETURN(-EPERM);
731
732                 /* The IMMUTABLE and APPEND_ONLY flags can
733                  * only be changed by the relevant capability. */
734                 if ((oldflags ^ newflags) &&
735                     !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
736                         RETURN(-EPERM);
737
738                 if (!S_ISDIR(oattr->la_mode)) {
739                         la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
740                 } else if (la->la_flags & LUSTRE_ENCRYPT_FL) {
741                         /* when trying to add encryption flag on dir,
742                          * make sure it is empty
743                          */
744                         rc = mdd_dir_is_empty(env, obj);
745                         if (rc)
746                                 RETURN(rc);
747                         rc = 0;
748                 }
749         }
750
751         if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
752             (la->la_valid & ~LA_FLAGS) &&
753             !(flags & MDS_PERM_BYPASS))
754                 RETURN(-EPERM);
755
756         /* Check for setting the obj time. */
757         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
758             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
759                 if ((uc->uc_fsuid != oattr->la_uid) &&
760                     !md_capable(uc, CFS_CAP_FOWNER)) {
761                         rc = mdd_permission_internal(env, obj, oattr,
762                                                      MAY_WRITE);
763                         if (rc)
764                                 RETURN(rc);
765                 }
766         }
767
768         if (la->la_valid & LA_KILL_SUID) {
769                 la->la_valid &= ~LA_KILL_SUID;
770                 if ((oattr->la_mode & S_ISUID) &&
771                     !(la->la_valid & LA_MODE)) {
772                         la->la_mode = oattr->la_mode;
773                         la->la_valid |= LA_MODE;
774                 }
775                 la->la_mode &= ~S_ISUID;
776         }
777
778         if (la->la_valid & LA_KILL_SGID) {
779                 la->la_valid &= ~LA_KILL_SGID;
780                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
781                         (S_ISGID | S_IXGRP)) &&
782                     !(la->la_valid & LA_MODE)) {
783                         la->la_mode = oattr->la_mode;
784                         la->la_valid |= LA_MODE;
785                 }
786                 la->la_mode &= ~S_ISGID;
787         }
788
789         /* Make sure a caller can chmod. */
790         if (la->la_valid & LA_MODE) {
791                 if (!(flags & MDS_PERM_BYPASS) &&
792                     (uc->uc_fsuid != oattr->la_uid) &&
793                     !md_capable(uc, CFS_CAP_FOWNER))
794                         RETURN(-EPERM);
795
796                 if (la->la_mode == (umode_t) -1)
797                         la->la_mode = oattr->la_mode;
798                 else
799                         la->la_mode = (la->la_mode & S_IALLUGO) |
800                                         (oattr->la_mode & ~S_IALLUGO);
801
802                 /* Also check the setgid bit! */
803                 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
804                                        la->la_gid : oattr->la_gid) &&
805                     !md_capable(uc, CFS_CAP_FSETID))
806                         la->la_mode &= ~S_ISGID;
807         } else {
808                la->la_mode = oattr->la_mode;
809         }
810
811         /* Make sure a caller can chown. */
812         if (la->la_valid & LA_UID) {
813                 if (la->la_uid == (uid_t) -1)
814                         la->la_uid = oattr->la_uid;
815                 if (((uc->uc_fsuid != oattr->la_uid) ||
816                      (la->la_uid != oattr->la_uid)) &&
817                     !md_capable(uc, CFS_CAP_CHOWN))
818                         RETURN(-EPERM);
819
820                 /* If the user or group of a non-directory has been
821                  * changed by a non-root user, remove the setuid bit.
822                  * 19981026 David C Niemi <niemi@tux.org>
823                  *
824                  * Changed this to apply to all users, including root,
825                  * to avoid some races. This is the behavior we had in
826                  * 2.0. The check for non-root was definitely wrong
827                  * for 2.2 anyway, as it should have been using
828                  * CAP_FSETID rather than fsuid -- 19990830 SD. */
829                 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
830                 !S_ISDIR(oattr->la_mode)) {
831                         la->la_mode &= ~S_ISUID;
832                         la->la_valid |= LA_MODE;
833                 }
834         }
835
836         /* Make sure caller can chgrp. */
837         if (la->la_valid & LA_GID) {
838                 if (la->la_gid == (gid_t) -1)
839                         la->la_gid = oattr->la_gid;
840                 if (((uc->uc_fsuid != oattr->la_uid) ||
841                      ((la->la_gid != oattr->la_gid) &&
842                       !lustre_in_group_p(uc, la->la_gid))) &&
843                     !md_capable(uc, CFS_CAP_CHOWN))
844                         RETURN(-EPERM);
845
846                 /* Likewise, if the user or group of a non-directory
847                  * has been changed by a non-root user, remove the
848                  * setgid bit UNLESS there is no group execute bit
849                  * (this would be a file marked for mandatory
850                  * locking).  19981026 David C Niemi <niemi@tux.org>
851                  *
852                  * Removed the fsuid check (see the comment above) --
853                  * 19990830 SD. */
854                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
855                     (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
856                         la->la_mode &= ~S_ISGID;
857                         la->la_valid |= LA_MODE;
858                 }
859         }
860
861         if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
862                 if (!((flags & MDS_OWNEROVERRIDE) &&
863                       (uc->uc_fsuid == oattr->la_uid)) &&
864                     !(flags & MDS_PERM_BYPASS)) {
865                         rc = mdd_permission_internal(env, obj, oattr,
866                                                      MAY_WRITE);
867                         if (rc != 0)
868                                 RETURN(rc);
869                 }
870         }
871
872         if (la->la_valid & LA_CTIME) {
873                 /* The pure setattr, it has the priority over what is
874                  * already set, do not drop it if ctime is equal. */
875                 if (la->la_ctime < oattr->la_ctime)
876                         la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
877         }
878
879         RETURN(0);
880 }
881
882 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
883                                            struct mdd_device *mdd,
884                                            enum changelog_rec_type type,
885                                            enum changelog_rec_flags clf_flags,
886                                            const struct lu_fid *fid,
887                                            const char *xattr_name,
888                                            struct thandle *handle)
889 {
890         const struct lu_ucred *uc = lu_ucred(env);
891         enum changelog_rec_extra_flags xflags = CLFE_INVALID;
892         struct llog_changelog_rec *rec;
893         struct lu_buf *buf;
894         int reclen;
895         int rc;
896
897         clf_flags = (clf_flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
898
899         if (uc) {
900                 if (uc->uc_jobid[0] != '\0')
901                         clf_flags |= CLF_JOBID;
902                 xflags |= CLFE_UIDGID;
903                 xflags |= CLFE_NID;
904         }
905         if (type == CL_OPEN || type == CL_DN_OPEN)
906                 xflags |= CLFE_OPEN;
907         if (type == CL_SETXATTR || type == CL_GETXATTR)
908                 xflags |= CLFE_XATTR;
909
910         reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
911                                changelog_rec_offset(clf_flags & CLF_SUPPORTED,
912                                                     xflags & CLFE_SUPPORTED));
913         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
914         if (buf->lb_buf == NULL)
915                 RETURN(-ENOMEM);
916         rec = buf->lb_buf;
917
918         rec->cr_hdr.lrh_len = reclen;
919         rec->cr.cr_flags = clf_flags;
920         rec->cr.cr_type = (__u32)type;
921         rec->cr.cr_tfid = *fid;
922         rec->cr.cr_namelen = 0;
923
924         if (clf_flags & CLF_JOBID)
925                 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
926
927         if (clf_flags & CLF_EXTRA_FLAGS) {
928                 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
929                 if (xflags & CLFE_UIDGID)
930                         mdd_changelog_rec_extra_uidgid(&rec->cr,
931                                                        uc->uc_uid, uc->uc_gid);
932                 if (xflags & CLFE_NID)
933                         mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
934                 if (xflags & CLFE_OPEN)
935                         mdd_changelog_rec_extra_omode(&rec->cr, clf_flags);
936                 if (xflags & CLFE_XATTR) {
937                         if (xattr_name == NULL)
938                                 RETURN(-EINVAL);
939                         mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
940                 }
941         }
942
943         rc = mdd_changelog_store(env, mdd, rec, handle);
944         RETURN(rc);
945 }
946
947
948 /** Store a data change changelog record
949  * If this fails, we must fail the whole transaction; we don't
950  * want the change to commit without the log entry.
951  * \param mdd_obj - mdd_object of change
952  * \param handle - transaction handle
953  */
954 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
955                              enum changelog_rec_type type,
956                              enum changelog_rec_flags clf_flags,
957                              struct mdd_object *mdd_obj, struct thandle *handle)
958 {
959         int                              rc;
960
961         LASSERT(mdd_obj != NULL);
962         LASSERT(handle != NULL);
963
964         if (!mdd_changelog_enabled(env, mdd, type))
965                 RETURN(0);
966
967         if (mdd_is_volatile_obj(mdd_obj))
968                 RETURN(0);
969
970         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
971             ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
972                 /* Don't need multiple updates in this log */
973                 /* Don't check under lock - no big deal if we get an extra
974                    entry */
975                 RETURN(0);
976         }
977
978         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
979                                              mdd_object_fid(mdd_obj),
980                                              NULL, handle);
981         if (rc == 0)
982                 mdd_obj->mod_cltime = ktime_get();
983
984         RETURN(rc);
985 }
986
987 int mdd_changelog_data_store_xattr(const struct lu_env *env,
988                                    struct mdd_device *mdd,
989                                    enum changelog_rec_type type,
990                                    enum changelog_rec_flags clf_flags,
991                                    struct mdd_object *mdd_obj,
992                                    const char *xattr_name,
993                                    struct thandle *handle)
994 {
995         int rc;
996
997         LASSERT(mdd_obj != NULL);
998         LASSERT(handle != NULL);
999
1000         if (!mdd_changelog_enabled(env, mdd, type))
1001                 RETURN(0);
1002
1003         if (mdd_is_volatile_obj(mdd_obj))
1004                 RETURN(0);
1005
1006         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
1007             ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
1008                 /* Don't need multiple updates in this log */
1009                 /* Don't check under lock - no big deal if we get an extra
1010                  * entry
1011                  */
1012                 RETURN(0);
1013         }
1014
1015         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1016                                              mdd_object_fid(mdd_obj),
1017                                              xattr_name, handle);
1018         if (rc == 0)
1019                 mdd_obj->mod_cltime = ktime_get();
1020
1021         RETURN(rc);
1022 }
1023
1024 /* only the bottom CLF_FLAGSHIFT bits of @flags are stored in the record,
1025  * except for open flags have a dedicated record to store 32 bits of flags */
1026 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
1027                          enum changelog_rec_flags clf_flags,
1028                          struct md_device *m, const struct lu_fid *fid)
1029 {
1030         struct thandle *handle;
1031         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1032         int rc;
1033         ENTRY;
1034
1035         LASSERT(fid != NULL);
1036
1037         /* We'll check this again below, but we check now before we
1038          * start a transaction. */
1039         if (!mdd_changelog_enabled(env, mdd, type))
1040                 RETURN(0);
1041
1042         handle = mdd_trans_create(env, mdd);
1043         if (IS_ERR(handle))
1044                 RETURN(PTR_ERR(handle));
1045
1046         rc = mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1047         if (rc)
1048                 GOTO(stop, rc);
1049
1050         rc = mdd_trans_start(env, mdd, handle);
1051         if (rc)
1052                 GOTO(stop, rc);
1053
1054         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1055                                              fid, NULL, handle);
1056
1057 stop:
1058         rc = mdd_trans_stop(env, mdd, rc, handle);
1059
1060         RETURN(rc);
1061 }
1062
1063 /**
1064  * Save LMA extended attributes with data from \a ma.
1065  *
1066  * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
1067  * not, LMA EA will be first read from disk, modified and write back.
1068  *
1069  */
1070 /* Precedence for choosing record type when multiple
1071  * attributes change: setattr > mtime > ctime > atime
1072  * (ctime changes when mtime does, plus chmod/chown.
1073  * atime and ctime are independent.) */
1074 static int mdd_attr_set_changelog(const struct lu_env *env,
1075                                   struct md_object *obj, struct thandle *handle,
1076                                   __u64 valid)
1077 {
1078         struct mdd_device *mdd = mdo2mdd(obj);
1079         int bits, type = 0;
1080
1081         bits =  (valid & LA_SIZE)  ? BIT(CL_TRUNC) : 0;
1082         bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? BIT(CL_SETATTR) : 0;
1083         bits |= (valid & LA_MTIME) ? BIT(CL_MTIME) : 0;
1084         bits |= (valid & LA_CTIME) ? BIT(CL_CTIME) : 0;
1085         bits |= (valid & LA_ATIME) ? BIT(CL_ATIME) : 0;
1086         bits = bits & mdd->mdd_cl.mc_mask;
1087         /* This is an implementation limit rather than a protocol limit */
1088         BUILD_BUG_ON(CL_LAST > sizeof(int) * 8);
1089         if (bits == 0)
1090                 return 0;
1091
1092         /* The record type is the lowest non-masked set bit */
1093         type = __ffs(bits);
1094
1095         /* XXX: we only store the low CLF_FLAGMASK bits of la_valid */
1096         return mdd_changelog_data_store(env, mdd, type, valid,
1097                                         md2mdd_obj(obj), handle);
1098 }
1099
1100 static int mdd_declare_attr_set(const struct lu_env *env,
1101                                 struct mdd_device *mdd,
1102                                 struct mdd_object *obj,
1103                                 const struct lu_attr *attr,
1104                                 struct thandle *handle)
1105 {
1106         int rc;
1107
1108         rc = mdo_declare_attr_set(env, obj, attr, handle);
1109         if (rc)
1110                 return rc;
1111
1112 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1113         if (attr->la_valid & LA_MODE) {
1114                 mdd_read_lock(env, obj, DT_TGT_CHILD);
1115                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
1116                                    XATTR_NAME_ACL_ACCESS);
1117                 mdd_read_unlock(env, obj);
1118                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
1119                         rc = 0;
1120                 else if (rc < 0)
1121                         return rc;
1122
1123                 if (rc != 0) {
1124                         struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
1125                         rc = mdo_declare_xattr_set(env, obj, buf,
1126                                                    XATTR_NAME_ACL_ACCESS, 0,
1127                                                    handle);
1128                         if (rc)
1129                                 return rc;
1130                 }
1131         }
1132 #endif
1133
1134         rc = mdd_declare_changelog_store(env, mdd, CL_SETXATTR, NULL, NULL,
1135                                          handle);
1136         return rc;
1137 }
1138
1139 /*
1140  * LU-3671
1141  * LU-7239
1142  *
1143  * permission changes may require sync operation, to mitigate performance
1144  * impact, only do this for dir and when permission is reduced.
1145  *
1146  * For regular files, version is updated with permission change (see VBR), async
1147  * permission won't cause any issue, while missing permission change on
1148  * directory may affect accessibility of other objects after recovery.
1149  */
1150 static inline bool permission_needs_sync(const struct lu_attr *old,
1151                                          const struct lu_attr *new)
1152 {
1153         if (!S_ISDIR(old->la_mode))
1154                 return false;
1155
1156         if (new->la_valid & LA_UID && old->la_uid != new->la_uid)
1157                 return true;
1158
1159         if (new->la_valid & LA_GID && old->la_gid != new->la_gid)
1160                 return true;
1161
1162         if (new->la_valid & LA_MODE) {
1163                 /* turned on sticky bit */
1164                 if (!(old->la_mode & S_ISVTX) && (new->la_mode & S_ISVTX))
1165                         return true;
1166
1167                 /* set-GID has no impact on what is allowed, not checked */
1168
1169                 /* turned off setuid bit, or one of rwx for someone */
1170                 if (((new->la_mode & old->la_mode) & (0777 | S_ISUID)) !=
1171                      (old->la_mode & (0777 | S_ISUID)))
1172                         return true;
1173         }
1174
1175         return false;
1176 }
1177
1178 static inline __u64 mdd_lmm_dom_size(void *buf)
1179 {
1180         struct lov_mds_md *lmm = buf;
1181         struct lov_comp_md_v1 *comp_v1;
1182         struct lov_mds_md *v1;
1183         __u32 off;
1184
1185         if (lmm == NULL)
1186                 return 0;
1187
1188         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1189                 return 0;
1190
1191         comp_v1 = (struct lov_comp_md_v1 *)lmm;
1192         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1193         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1194
1195         /* DoM entry is the first entry always */
1196         if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) == LOV_PATTERN_MDT)
1197                 return le64_to_cpu(comp_v1->lcm_entries[0].lcme_extent.e_end);
1198
1199         return 0;
1200 }
1201
1202 /* set attr and LOV EA at once, return updated attr */
1203 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
1204                  const struct md_attr *ma)
1205 {
1206         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1207         struct mdd_device *mdd = mdo2mdd(obj);
1208         struct thandle *handle = NULL;
1209         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1210         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1211         const struct lu_attr *la = &ma->ma_attr;
1212         struct lu_ucred  *uc;
1213         bool chrgrp_by_unprivileged_user = false;
1214         int rc;
1215         ENTRY;
1216
1217         /* we do not use ->attr_set() for LOV/HSM EA any more */
1218         LASSERT((ma->ma_valid & MA_LOV) == 0);
1219         LASSERT((ma->ma_valid & MA_HSM) == 0);
1220
1221         rc = mdd_la_get(env, mdd_obj, attr);
1222         if (rc)
1223                 RETURN(rc);
1224
1225         *la_copy = ma->ma_attr;
1226         rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma);
1227         if (rc)
1228                 RETURN(rc);
1229
1230         /* no need to setattr anymore */
1231         if (la_copy->la_valid == 0) {
1232                 CDEBUG(D_INODE,
1233                        "%s: no valid attribute on "DFID", previous was %#llx\n",
1234                        mdd_obj_dev_name(mdd_obj),
1235                        PFID(mdd_object_fid(mdd_obj)), la->la_valid);
1236
1237                 RETURN(0);
1238         }
1239
1240         /* If an unprivileged user changes group of some file,
1241          * the setattr operation will be processed synchronously to
1242          * honor the quota limit of the corresponding group. see LU-5152 */
1243         uc = lu_ucred_check(env);
1244         if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1245             la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1246                 /* LU-10048: disable synchronous chgrp operation for it will
1247                  * cause deadlock between MDT and OST.
1248                 la_copy->la_valid |= LA_FLAGS;
1249                 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1250                  */
1251                 chrgrp_by_unprivileged_user = true;
1252
1253                 /* Flush the possible existing client setattr requests to OSTs
1254                  * to keep the order with the current setattr operation that
1255                  * will be sent directly to OSTs. see LU-5152 */
1256                 /* LU-11303 disable sync as this is too heavyweight.
1257                  * This should be replaced with a sync only for the object
1258                  * being modified here, not the whole filesystem.
1259                 rc = dt_sync(env, mdd->mdd_child);
1260                 if (rc)
1261                         GOTO(out, rc);
1262                  */
1263         }
1264
1265         handle = mdd_trans_create(env, mdd);
1266         if (IS_ERR(handle)) {
1267                 rc = PTR_ERR(handle);
1268                 handle = NULL;
1269
1270                 GOTO(out, rc);
1271         }
1272
1273         rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1274         if (rc)
1275                 GOTO(out, rc);
1276
1277         rc = mdd_trans_start(env, mdd, handle);
1278         if (rc)
1279                 GOTO(out, rc);
1280
1281         if (!chrgrp_by_unprivileged_user && mdd->mdd_sync_permission &&
1282             permission_needs_sync(attr, la))
1283                 handle->th_sync = 1;
1284
1285         if (la->la_valid & (LA_MTIME | LA_CTIME))
1286                 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1287                        la->la_mtime, la->la_ctime);
1288
1289         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
1290
1291         /* LU-10509: setattr of LA_SIZE should be skipped case of DOM,
1292          * otherwise following truncate will do nothing and truncated
1293          * data may be read again. This is a quick fix until LU-11033
1294          * will be resolved.
1295          */
1296         if (la_copy->la_valid & LA_SIZE) {
1297                 struct lu_buf *lov_buf = mdd_buf_get(env, NULL, 0);
1298
1299                 rc = mdd_stripe_get(env, mdd_obj, lov_buf, XATTR_NAME_LOV);
1300                 if (rc) {
1301                         rc = 0;
1302                 } else {
1303                         if (mdd_lmm_dom_size(lov_buf->lb_buf) > 0)
1304                                 la_copy->la_valid &= ~LA_SIZE;
1305                         lu_buf_free(lov_buf);
1306                 }
1307         }
1308
1309         if (la_copy->la_valid) {
1310                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1311
1312                 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1313                         /* rollback to the original gid */
1314                         la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1315                         la_copy->la_gid = attr->la_gid;
1316                         mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1317                 }
1318         }
1319         mdd_write_unlock(env, mdd_obj);
1320
1321 out:
1322         if (rc == 0)
1323                 rc = mdd_attr_set_changelog(env, obj, handle,
1324                                             la_copy->la_valid);
1325
1326         if (handle != NULL)
1327                 rc = mdd_trans_stop(env, mdd, rc, handle);
1328
1329         return rc;
1330 }
1331
1332 static int mdd_xattr_sanity_check(const struct lu_env *env,
1333                                   struct mdd_object *obj,
1334                                   const struct lu_attr *attr,
1335                                   const char *name)
1336 {
1337         struct lu_ucred *uc     = lu_ucred_assert(env);
1338         ENTRY;
1339
1340         if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1341                 RETURN(-EPERM);
1342
1343         if (strncmp(XATTR_USER_PREFIX, name,
1344                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1345                 /* For sticky directories, only the owner and privileged user
1346                  * can write attributes. */
1347                 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1348                     (uc->uc_fsuid != attr->la_uid) &&
1349                     !md_capable(uc, CFS_CAP_FOWNER))
1350                         RETURN(-EPERM);
1351         } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
1352                    (uc->uc_fsuid != attr->la_uid) &&
1353                    !md_capable(uc, CFS_CAP_FOWNER)) {
1354                 RETURN(-EPERM);
1355         }
1356
1357         RETURN(0);
1358 }
1359
1360 /**
1361  * Check if a string begins with a given prefix.
1362  *
1363  * \param str     String to check
1364  * \param prefix  Substring to check at the beginning of \a str
1365  * \return true/false whether the condition is verified.
1366  */
1367 static inline bool has_prefix(const char *str, const char *prefix)
1368 {
1369         return strncmp(prefix, str, strlen(prefix)) == 0;
1370 }
1371
1372 /**
1373  * Indicate the kind of changelog to store (if any) for a xattr set/del.
1374  *
1375  * \param[in]  xattr_name  Full extended attribute name.
1376  *
1377  * \return type of changelog to use, or CL_NONE if no changelog is to be emitted
1378  */
1379 static enum changelog_rec_type
1380 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1381                          const char *xattr_name)
1382 {
1383         /* Layout changes systematically recorded */
1384         if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1385             strcmp(XATTR_LUSTRE_LOV, xattr_name) == 0 ||
1386             allowed_lustre_lov(xattr_name))
1387                 return CL_LAYOUT;
1388
1389         /* HSM information changes systematically recorded */
1390         if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1391                 return CL_HSM;
1392
1393         /* Avoid logging SOM xattr for every file */
1394         if (strcmp(XATTR_NAME_SOM, xattr_name) == 0)
1395                 return CL_NONE;
1396
1397         if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1398             has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1399             has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1400             has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1401             has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1402                 return CL_SETXATTR;
1403
1404         return CL_NONE;
1405 }
1406
1407 static int mdd_declare_xattr_set(const struct lu_env *env,
1408                                  struct mdd_device *mdd,
1409                                  struct mdd_object *obj,
1410                                  const struct lu_buf *buf,
1411                                  const char *name,
1412                                  int fl, struct thandle *handle)
1413 {
1414         enum changelog_rec_type type;
1415         int rc;
1416
1417         rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1418         if (rc)
1419                 return rc;
1420
1421         type = mdd_xattr_changelog_type(env, mdd, name);
1422         if (type < 0)
1423                 return 0; /* no changelog to store */
1424
1425         return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1426 }
1427
1428 /*
1429  * Compare current and future data of HSM EA and add a changelog if needed.
1430  *
1431  * Caller should have write-locked \param obj.
1432  *
1433  * \param buf - Future HSM EA content.
1434  * \retval 0 if no changelog is needed or changelog was added properly.
1435  * \retval -ve errno if there was a problem
1436  */
1437 static int mdd_hsm_update_locked(const struct lu_env *env,
1438                                  struct md_object *obj,
1439                                  const struct lu_buf *buf,
1440                                  struct thandle *handle,
1441                                  enum changelog_rec_flags *clf_flags)
1442 {
1443         struct mdd_thread_info *info = mdd_env_info(env);
1444         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1445         struct lu_buf *current_buf;
1446         struct md_hsm *current_mh;
1447         struct md_hsm *new_mh;
1448         int rc;
1449
1450         ENTRY;
1451         OBD_ALLOC_PTR(current_mh);
1452         if (current_mh == NULL)
1453                 RETURN(-ENOMEM);
1454
1455         /* Read HSM attrs from disk */
1456         current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1457                         min_t(unsigned int,
1458                               mdd_obj2mdd_dev(mdd_obj)->mdd_dt_conf.ddp_max_ea_size,
1459                             XATTR_SIZE_MAX));
1460         rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1461         rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1462         if (rc < 0 && rc != -ENODATA)
1463                 GOTO(free, rc);
1464         else if (rc == -ENODATA)
1465                 current_mh->mh_flags = 0;
1466
1467         /* Map future HSM xattr */
1468         OBD_ALLOC_PTR(new_mh);
1469         if (new_mh == NULL)
1470                 GOTO(free, rc = -ENOMEM);
1471         lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1472
1473         rc = 0;
1474
1475         /* Flags differ, set flags for the changelog that will be added */
1476         if (current_mh->mh_flags != new_mh->mh_flags) {
1477                 hsm_set_cl_event(clf_flags, HE_STATE);
1478                 if (new_mh->mh_flags & HS_DIRTY)
1479                         hsm_set_cl_flags(clf_flags, CLF_HSM_DIRTY);
1480         }
1481
1482         OBD_FREE_PTR(new_mh);
1483         EXIT;
1484 free:
1485         OBD_FREE_PTR(current_mh);
1486         return rc;
1487 }
1488
1489 static int mdd_object_pfid_replace(const struct lu_env *env,
1490                                    struct mdd_object *o)
1491 {
1492         struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1493         struct thandle *handle;
1494         int rc;
1495
1496         handle = mdd_trans_create(env, mdd);
1497         if (IS_ERR(handle))
1498                 RETURN(PTR_ERR(handle));
1499
1500         handle->th_complex = 1;
1501
1502         /* it doesn't need to track the PFID update via llog, because LFSCK
1503          * will repair it even it goes wrong */
1504         rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1505                                    0, handle);
1506         if (rc)
1507                 GOTO(out, rc);
1508
1509         rc = mdd_trans_start(env, mdd, handle);
1510         if (rc != 0)
1511                 GOTO(out, rc);
1512
1513         rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1514         if (rc)
1515                 GOTO(out, rc);
1516
1517 out:
1518         mdd_trans_stop(env, mdd, rc, handle);
1519         return rc;
1520 }
1521
1522
1523 static int mdd_declare_xattr_del(const struct lu_env *env,
1524                                  struct mdd_device *mdd,
1525                                  struct mdd_object *obj,
1526                                  const char *name,
1527                                  struct thandle *handle);
1528
1529 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1530                          const char *name);
1531
1532 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1533                            struct md_object *md_vic)
1534 {
1535         struct mdd_device *mdd = mdo2mdd(md_obj);
1536         struct mdd_object *obj = md2mdd_obj(md_obj);
1537         struct mdd_object *vic = md2mdd_obj(md_vic);
1538         struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1539         struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1540         struct lov_mds_md *lmm;
1541         struct thandle *handle;
1542         int rc;
1543         ENTRY;
1544
1545         rc = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
1546         if (rc == 0) /* same fid */
1547                 RETURN(-EPERM);
1548
1549         handle = mdd_trans_create(env, mdd);
1550         if (IS_ERR(handle))
1551                 RETURN(PTR_ERR(handle));
1552
1553         if (rc > 0) {
1554                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1555                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1556         } else {
1557                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1558                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1559         }
1560
1561         /* get EA of victim file */
1562         memset(buf_vic, 0, sizeof(*buf_vic));
1563         rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
1564         if (rc < 0) {
1565                 if (rc == -ENODATA)
1566                         rc = 0;
1567                 GOTO(out, rc);
1568         }
1569
1570         /* parse the layout of victim file */
1571         lmm = buf_vic->lb_buf;
1572         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1573                 GOTO(out, rc = -EINVAL);
1574
1575         /* save EA of target file for restore */
1576         memset(buf, 0, sizeof(*buf));
1577         rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
1578         if (rc < 0)
1579                 GOTO(out, rc);
1580
1581         /* Get rid of the layout from victim object */
1582         rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1583         if (rc)
1584                 GOTO(out, rc);
1585
1586         rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1587                                    LU_XATTR_MERGE, handle);
1588         if (rc)
1589                 GOTO(out, rc);
1590
1591         rc = mdd_trans_start(env, mdd, handle);
1592         if (rc != 0)
1593                 GOTO(out, rc);
1594
1595         rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1596                            handle);
1597         if (rc)
1598                 GOTO(out, rc);
1599
1600         rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1601         if (rc) /* wtf? */
1602                 GOTO(out_restore, rc);
1603
1604         (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1605         (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1606         EXIT;
1607
1608 out_restore:
1609         if (rc) {
1610                 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1611                                         LU_XATTR_REPLACE, handle);
1612                 if (rc2)
1613                         CERROR("%s: failed rollback of "DFID" layout: file state unknown: rc = %d\n",
1614                                mdd_obj_dev_name(obj),
1615                                PFID(mdd_object_fid(obj)), rc2);
1616         }
1617
1618 out:
1619         mdd_trans_stop(env, mdd, rc, handle);
1620         mdd_write_unlock(env, obj);
1621         mdd_write_unlock(env, vic);
1622         lu_buf_free(buf);
1623         lu_buf_free(buf_vic);
1624
1625         if (!rc)
1626                 (void) mdd_object_pfid_replace(env, obj);
1627
1628         return rc;
1629 }
1630
1631 /**
1632  * Extract the mirror with specified mirror id, and store the splitted
1633  * mirror layout to @buf.
1634  *
1635  * \param[in] comp_v1   mirrored layout
1636  * \param[in] mirror_id the mirror with mirror_id to be extracted
1637  * \param[out] buf      store the layout excluding the extracted mirror,
1638  *                      caller free the buffer we allocated in this function
1639  * \param[out] buf_vic  store the extracted layout, caller free the buffer
1640  *                      we allocated in this function
1641  *
1642  * \retval      0 on success; < 0 if error happens
1643  */
1644 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1645                         struct lu_buf *buf, struct lu_buf *buf_vic)
1646 {
1647         struct lov_comp_md_v1 *comp_rem;
1648         struct lov_comp_md_v1 *comp_vic;
1649         struct lov_comp_md_entry_v1 *entry;
1650         struct lov_comp_md_entry_v1 *entry_rem;
1651         struct lov_comp_md_entry_v1 *entry_vic;
1652         __u16 mirror_cnt;
1653         __u16 comp_cnt, count = 0;
1654         int lmm_size, lmm_size_vic = 0;
1655         int i, j, k;
1656         int offset, offset_rem, offset_vic;
1657
1658         mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1659         /* comp_v1 should contains more than 1 mirror */
1660         if (mirror_cnt <= 1)
1661                 return -EINVAL;
1662         comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1663         lmm_size = le32_to_cpu(comp_v1->lcm_size);
1664
1665         for (i = 0; i < comp_cnt; i++) {
1666                 entry = &comp_v1->lcm_entries[i];
1667                 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1668                         count++;
1669                         lmm_size_vic += sizeof(*entry);
1670                         lmm_size_vic += le32_to_cpu(entry->lcme_size);
1671                 } else if (count > 0) {
1672                         /* find the specified mirror */
1673                         break;
1674                 }
1675         }
1676
1677         if (count == 0)
1678                 return -EINVAL;
1679
1680         lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1681         if (!buf->lb_buf)
1682                 return -ENOMEM;
1683
1684         lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1685         if (!buf_vic->lb_buf) {
1686                 lu_buf_free(buf);
1687                 return -ENOMEM;
1688         }
1689
1690         comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1691         comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1692
1693         memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1694         comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1695         comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1696         comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1697         if (!comp_rem->lcm_mirror_count)
1698                 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1699
1700         memset(comp_vic, 0, sizeof(*comp_v1));
1701         comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1702         comp_vic->lcm_mirror_count = 0;
1703         comp_vic->lcm_entry_count = cpu_to_le32(count);
1704         comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1705         comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1706         comp_vic->lcm_layout_gen = 0;
1707
1708         offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1709         offset_rem = sizeof(*comp_rem) +
1710                      sizeof(*entry_rem) * (comp_cnt - count);
1711         offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1712         for (i = j = k = 0; i < comp_cnt; i++) {
1713                 struct lov_mds_md *lmm, *lmm_dst;
1714                 bool vic = false;
1715
1716                 entry = &comp_v1->lcm_entries[i];
1717                 entry_vic = &comp_vic->lcm_entries[j];
1718                 entry_rem = &comp_rem->lcm_entries[k];
1719
1720                 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1721                         vic = true;
1722
1723                 /* copy component entry */
1724                 if (vic) {
1725                         memcpy(entry_vic, entry, sizeof(*entry));
1726                         entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1727                         entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1728                         j++;
1729                 } else {
1730                         memcpy(entry_rem, entry, sizeof(*entry));
1731                         entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1732                         k++;
1733                 }
1734
1735                 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1736                 if (vic)
1737                         lmm_dst = (struct lov_mds_md *)
1738                                         ((char *)comp_vic + offset_vic);
1739                 else
1740                         lmm_dst = (struct lov_mds_md *)
1741                                         ((char *)comp_rem + offset_rem);
1742
1743                 /* copy component entry blob */
1744                 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1745
1746                 /* blob offset advance */
1747                 offset += le32_to_cpu(entry->lcme_size);
1748                 if (vic)
1749                         offset_vic += le32_to_cpu(entry->lcme_size);
1750                 else
1751                         offset_rem += le32_to_cpu(entry->lcme_size);
1752         }
1753
1754         return 0;
1755 }
1756
1757 static int mdd_dom_data_truncate(const struct lu_env *env,
1758                                  struct mdd_device *mdd, struct mdd_object *mo);
1759
1760 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1761                            struct md_rejig_data *mrd)
1762 {
1763         struct mdd_device *mdd = mdo2mdd(md_obj);
1764         struct mdd_object *obj = md2mdd_obj(md_obj);
1765         struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
1766         struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1767         struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1768         struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1769         struct lov_comp_md_v1 *lcm;
1770         struct thandle *handle;
1771         int rc;
1772         bool dom_stripe = false;
1773
1774         ENTRY;
1775
1776         rc = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
1777         if (rc == 0) /* same fid */
1778                 RETURN(-EPERM);
1779
1780         handle = mdd_trans_create(env, mdd);
1781         if (IS_ERR(handle))
1782                 RETURN(PTR_ERR(handle));
1783
1784         if (rc > 0) {
1785                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1786                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1787         } else {
1788                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1789                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1790         }
1791
1792         /* get EA of mirrored file */
1793         memset(buf_save, 0, sizeof(*buf));
1794         rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
1795         if (rc < 0)
1796                 GOTO(out, rc);
1797
1798         lcm = buf_save->lb_buf;
1799         if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1800                 GOTO(out, rc = -EINVAL);
1801
1802         /**
1803          * Extract the mirror with specified mirror id, and store the splitted
1804          * mirror layout to the victim file.
1805          */
1806         memset(buf, 0, sizeof(*buf));
1807         memset(buf_vic, 0, sizeof(*buf_vic));
1808         rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1809         if (rc < 0)
1810                 GOTO(out, rc);
1811
1812         dom_stripe = mdd_lmm_dom_size(buf_vic->lb_buf) > 0;
1813
1814         rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1815                                    LU_XATTR_SPLIT, handle);
1816         if (rc)
1817                 GOTO(out, rc);
1818         rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
1819                                    LU_XATTR_SPLIT, handle);
1820         if (rc)
1821                 GOTO(out, rc);
1822
1823         rc = mdd_trans_start(env, mdd, handle);
1824         if (rc)
1825                 GOTO(out, rc);
1826
1827         rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1828                            handle);
1829         if (rc)
1830                 GOTO(out, rc);
1831
1832         rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
1833                            handle);
1834         if (rc)
1835                 GOTO(out_restore, rc);
1836
1837         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1838         if (rc)
1839                 GOTO(out, rc);
1840
1841         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1842         if (rc)
1843                 GOTO(out, rc);
1844         EXIT;
1845
1846 out_restore:
1847         if (rc) {
1848                 /* restore obj's layout */
1849                 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1850                                         LU_XATTR_REPLACE, handle);
1851                 if (rc2)
1852                         CERROR("%s: failed rollback "DFID" layout: file state unkonwn: rc = %d\n",
1853                                mdd_obj_dev_name(obj),
1854                                PFID(mdd_object_fid(obj)), rc2);
1855         }
1856 out:
1857         rc = mdd_trans_stop(env, mdd, rc, handle);
1858
1859         /* Truncate local DOM data if all went well */
1860         if (!rc && dom_stripe)
1861                 mdd_dom_data_truncate(env, mdd, obj);
1862
1863         mdd_write_unlock(env, obj);
1864         mdd_write_unlock(env, vic);
1865         lu_buf_free(buf_save);
1866         lu_buf_free(buf);
1867         lu_buf_free(buf_vic);
1868
1869         if (!rc)
1870                 (void) mdd_object_pfid_replace(env, obj);
1871
1872         return rc;
1873 }
1874
1875 static int mdd_layout_merge_allowed(const struct lu_env *env,
1876                                     struct md_object *target,
1877                                     struct md_object *victim)
1878 {
1879         struct mdd_object *o1 = md2mdd_obj(target);
1880
1881         /* cannot extend directory's LOVEA */
1882         if (S_ISDIR(mdd_object_type(o1))) {
1883                 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1884                        mdd_obj_dev_name(o1));
1885                 RETURN(-EISDIR);
1886         }
1887
1888         RETURN(0);
1889 }
1890
1891 /**
1892  * The caller should guarantee to update the object ctime
1893  * after xattr_set if needed.
1894  */
1895 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1896                          const struct lu_buf *buf, const char *name,
1897                          int fl)
1898 {
1899         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1900         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1901         struct mdd_device *mdd = mdo2mdd(obj);
1902         struct thandle *handle;
1903         enum changelog_rec_type  cl_type;
1904         enum changelog_rec_flags clf_flags = 0;
1905         int rc;
1906         ENTRY;
1907
1908         rc = mdd_la_get(env, mdd_obj, attr);
1909         if (rc)
1910                 RETURN(rc);
1911
1912         rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1913         if (rc)
1914                 RETURN(rc);
1915
1916         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1917             (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1918                 struct md_rejig_data *mrd = buf->lb_buf;
1919                 struct md_object *victim = mrd->mrd_obj;
1920
1921                 if (buf->lb_len != sizeof(*mrd))
1922                         RETURN(-EINVAL);
1923
1924                 rc = mdd_layout_merge_allowed(env, obj, victim);
1925                 if (rc)
1926                         RETURN(rc);
1927
1928                 if (fl == LU_XATTR_MERGE)
1929                         /* merge layout of victim as a mirror of obj's. */
1930                         rc = mdd_xattr_merge(env, obj, victim);
1931                 else
1932                         rc = mdd_xattr_split(env, obj, mrd);
1933                 RETURN(rc);
1934         }
1935
1936         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1937             strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1938                 struct posix_acl *acl;
1939
1940                 /* user may set empty ACL, which should be treated as removing
1941                  * ACL. */
1942                 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1943                                            buf->lb_len);
1944                 if (IS_ERR(acl))
1945                         RETURN(PTR_ERR(acl));
1946                 if (acl == NULL) {
1947                         rc = mdd_xattr_del(env, obj, name);
1948                         RETURN(rc);
1949                 }
1950                 posix_acl_release(acl);
1951         }
1952
1953         if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1954                 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1955                 RETURN(rc);
1956         }
1957
1958         handle = mdd_trans_create(env, mdd);
1959         if (IS_ERR(handle))
1960                 RETURN(PTR_ERR(handle));
1961
1962         rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1963         if (rc)
1964                 GOTO(stop, rc);
1965
1966         rc = mdd_trans_start(env, mdd, handle);
1967         if (rc)
1968                 GOTO(stop, rc);
1969
1970         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
1971
1972         if (strcmp(XATTR_NAME_HSM, name) == 0) {
1973                 rc = mdd_hsm_update_locked(env, obj, buf, handle, &clf_flags);
1974                 if (rc) {
1975                         mdd_write_unlock(env, mdd_obj);
1976                         GOTO(stop, rc);
1977                 }
1978         }
1979
1980         rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
1981         mdd_write_unlock(env, mdd_obj);
1982         if (rc)
1983                 GOTO(stop, rc);
1984
1985         cl_type = mdd_xattr_changelog_type(env, mdd, name);
1986         if (cl_type < 0)
1987                 GOTO(stop, rc = 0);
1988
1989         rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, clf_flags,
1990                                             mdd_obj, name, handle);
1991
1992         EXIT;
1993 stop:
1994         return mdd_trans_stop(env, mdd, rc, handle);
1995 }
1996
1997 static int mdd_declare_xattr_del(const struct lu_env *env,
1998                                  struct mdd_device *mdd,
1999                                  struct mdd_object *obj,
2000                                  const char *name,
2001                                  struct thandle *handle)
2002 {
2003         enum changelog_rec_type type;
2004         int rc;
2005
2006         rc = mdo_declare_xattr_del(env, obj, name, handle);
2007         if (rc)
2008                 return rc;
2009
2010         type = mdd_xattr_changelog_type(env, mdd, name);
2011         if (type < 0)
2012                 return 0; /* no changelog to store */
2013
2014         return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
2015 }
2016
2017 /**
2018  * The caller should guarantee to update the object ctime
2019  * after xattr_set if needed.
2020  */
2021 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
2022                          const char *name)
2023 {
2024         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2025         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
2026         struct mdd_device *mdd = mdo2mdd(obj);
2027         struct thandle *handle;
2028         int rc;
2029         ENTRY;
2030
2031         rc = mdd_la_get(env, mdd_obj, attr);
2032         if (rc)
2033                 RETURN(rc);
2034
2035         rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
2036         if (rc)
2037                 RETURN(rc);
2038
2039         handle = mdd_trans_create(env, mdd);
2040         if (IS_ERR(handle))
2041                 RETURN(PTR_ERR(handle));
2042
2043         rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
2044         if (rc)
2045                 GOTO(stop, rc);
2046
2047         rc = mdd_trans_start(env, mdd, handle);
2048         if (rc)
2049                 GOTO(stop, rc);
2050
2051         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
2052         rc = mdo_xattr_del(env, mdd_obj, name, handle);
2053         mdd_write_unlock(env, mdd_obj);
2054         if (rc)
2055                 GOTO(stop, rc);
2056
2057         if (mdd_xattr_changelog_type(env, mdd, name) < 0)
2058                 GOTO(stop, rc = 0);
2059
2060         rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
2061                                             name, handle);
2062
2063         EXIT;
2064 stop:
2065         return mdd_trans_stop(env, mdd, rc, handle);
2066 }
2067
2068 /*
2069  * read lov/lmv EA of an object
2070  * return the lov/lmv EA in an allocated lu_buf
2071  */
2072 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
2073                    struct lu_buf *lmm_buf, const char *name)
2074 {
2075         struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
2076         int rc;
2077
2078         ENTRY;
2079
2080         if (buf->lb_buf == NULL) {
2081                 buf = lu_buf_check_and_alloc(buf, 4096);
2082                 if (buf->lb_buf == NULL)
2083                         RETURN(-ENOMEM);
2084         }
2085
2086 repeat:
2087         rc = mdo_xattr_get(env, obj, buf, name);
2088         if (rc == -ERANGE) {
2089                 /* mti_big_buf is allocated but is too small
2090                  * we need to increase it */
2091                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
2092                                              buf->lb_len * 2);
2093                 if (buf->lb_buf == NULL)
2094                         RETURN(-ENOMEM);
2095                 goto repeat;
2096         } else if (rc < 0) {
2097                 RETURN(rc);
2098         } else if (rc == 0) {
2099                 RETURN(-ENODATA);
2100         }
2101
2102         lu_buf_alloc(lmm_buf, rc);
2103         if (lmm_buf->lb_buf == NULL)
2104                 RETURN(-ENOMEM);
2105
2106         /*
2107          * we don't use lmm_buf directly, because we don't know xattr size, so
2108          * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
2109          */
2110         memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
2111
2112         RETURN(0);
2113 }
2114
2115 static int mdd_xattr_hsm_replace(const struct lu_env *env,
2116                                  struct mdd_object *o, struct lu_buf *buf,
2117                                  struct thandle *handle)
2118 {
2119         struct hsm_attrs *attrs;
2120         enum hsm_states hsm_flags;
2121         enum changelog_rec_flags clf_flags = 0;
2122         int rc;
2123         ENTRY;
2124
2125         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
2126                            handle);
2127         if (rc != 0)
2128                 RETURN(rc);
2129
2130         attrs = buf->lb_buf;
2131         hsm_flags = le32_to_cpu(attrs->hsm_flags);
2132         if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
2133                 RETURN(0);
2134
2135         /* Add a changelog record for release. */
2136         hsm_set_cl_event(&clf_flags, HE_RELEASE);
2137         rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
2138                                       clf_flags, o, handle);
2139         RETURN(rc);
2140 }
2141
2142 /*
2143  *  check if layout swapping between 2 objects is allowed
2144  *  the rules are:
2145  *  - only normal FIDs or non-system IGIFs
2146  *  - same type of objects
2147  *  - same owner/group (so quotas are still valid) unless this is from HSM
2148  *    release.
2149  */
2150 static int mdd_layout_swap_allowed(const struct lu_env *env,
2151                                    struct mdd_object *o1,
2152                                    const struct lu_attr *attr1,
2153                                    struct mdd_object *o2,
2154                                    const struct lu_attr *attr2,
2155                                    __u64 flags)
2156 {
2157         const struct lu_fid *fid1, *fid2;
2158         ENTRY;
2159
2160         fid1 = mdd_object_fid(o1);
2161         fid2 = mdd_object_fid(o2);
2162
2163         if (!fid_is_norm(fid1) &&
2164             (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
2165                 RETURN(-EBADF);
2166
2167         if (!fid_is_norm(fid2) &&
2168             (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
2169                 RETURN(-EBADF);
2170
2171         if (mdd_object_type(o1) != mdd_object_type(o2)) {
2172                 if (S_ISDIR(mdd_object_type(o1)))
2173                         RETURN(-ENOTDIR);
2174                 if (S_ISREG(mdd_object_type(o1)))
2175                         RETURN(-EISDIR);
2176                 RETURN(-EBADF);
2177         }
2178
2179         if (flags & SWAP_LAYOUTS_MDS_HSM)
2180                 RETURN(0);
2181
2182         if ((attr1->la_uid != attr2->la_uid) ||
2183             (attr1->la_gid != attr2->la_gid))
2184                 RETURN(-EPERM);
2185
2186         RETURN(0);
2187 }
2188
2189 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
2190  *     look into the layout in MDD layer. */
2191 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
2192 {
2193         struct lov_comp_md_v1   *comp_v1;
2194         struct lov_mds_md       *v1;
2195         int                      i, ent_count;
2196         __u32                    off;
2197
2198         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2199                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2200                 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
2201
2202                 if (ent_count == 0)
2203                         return -EINVAL;
2204
2205                 if (get) {
2206                         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
2207                         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
2208                         *oi = v1->lmm_oi;
2209                 } else {
2210                         for (i = 0; i < le32_to_cpu(ent_count); i++) {
2211                                 off = le32_to_cpu(comp_v1->lcm_entries[i].
2212                                                 lcme_offset);
2213                                 v1 = (struct lov_mds_md *)((char *)comp_v1 +
2214                                                 off);
2215                                 v1->lmm_oi = *oi;
2216                         }
2217                 }
2218         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2219                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2220                 if (get)
2221                         *oi = lmm->lmm_oi;
2222                 else
2223                         lmm->lmm_oi = *oi;
2224         } else {
2225                 return -EINVAL;
2226         }
2227         return 0;
2228 }
2229
2230 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2231 {
2232         return mdd_lmm_oi(lmm, oi, true);
2233 }
2234
2235 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2236 {
2237         return mdd_lmm_oi(lmm, oi, false);
2238 }
2239
2240 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
2241 {
2242         struct lov_comp_md_v1 *comp_v1;
2243
2244         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2245                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2246                 if (get)
2247                         *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
2248                 else
2249                         comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
2250         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2251                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2252                 __u16 tmp_gen = *gen;
2253                 if (get)
2254                         *gen = le16_to_cpu(lmm->lmm_layout_gen);
2255                 else
2256                         lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
2257         } else {
2258                 return -EINVAL;
2259         }
2260         return 0;
2261 }
2262
2263 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2264 {
2265         return mdd_lmm_gen(lmm, gen, true);
2266 }
2267
2268 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2269 {
2270         return mdd_lmm_gen(lmm, gen, false);
2271 }
2272
2273 static int mdd_dom_data_truncate(const struct lu_env *env,
2274                                  struct mdd_device *mdd, struct mdd_object *mo)
2275 {
2276         struct thandle *th;
2277         struct dt_object *dom;
2278         int rc;
2279
2280         dom = dt_object_locate(mdd_object_child(mo), mdd->mdd_bottom);
2281         if (!dom)
2282                 GOTO(out, rc = -ENODATA);
2283
2284         th = dt_trans_create(env, mdd->mdd_bottom);
2285         if (IS_ERR(th))
2286                 GOTO(out, rc = PTR_ERR(th));
2287
2288         rc = dt_declare_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2289         if (rc)
2290                 GOTO(stop, rc);
2291
2292         rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
2293         if (rc != 0)
2294                 GOTO(stop, rc);
2295
2296         rc = dt_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2297 stop:
2298         dt_trans_stop(env, mdd->mdd_bottom, th);
2299 out:
2300         /* Ignore failure but report the error */
2301         if (rc)
2302                 CERROR("%s: can't truncate DOM inode "DFID" data: rc = %d\n",
2303                        mdd_obj_dev_name(mo), PFID(mdd_object_fid(mo)), rc);
2304         return rc;
2305 }
2306
2307 /**
2308  * swap layouts between 2 lustre objects
2309  */
2310 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2311                             struct md_object *obj2, __u64 flags)
2312 {
2313         struct mdd_thread_info *info = mdd_env_info(env);
2314         struct mdd_object *fst_o = md2mdd_obj(obj1);
2315         struct mdd_object *snd_o = md2mdd_obj(obj2);
2316         struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2317         struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2318         struct mdd_device *mdd = mdo2mdd(obj1);
2319         struct lov_mds_md *fst_lmm, *snd_lmm;
2320         struct lu_buf *fst_buf = &info->mti_buf[0];
2321         struct lu_buf *snd_buf = &info->mti_buf[1];
2322         struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2323         struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2324         struct ost_id *saved_oi = NULL;
2325         struct thandle *handle;
2326         struct mdd_object *dom_o = NULL;
2327         __u64 domsize_dom, domsize_vlt;
2328         __u32 fst_gen, snd_gen, saved_gen;
2329         int fst_fl;
2330         int rc, rc2;
2331
2332         ENTRY;
2333
2334         BUILD_BUG_ON(ARRAY_SIZE(info->mti_buf) < 4);
2335         memset(info->mti_buf, 0, sizeof(info->mti_buf));
2336
2337         /* we have to sort the 2 obj, so locking will always
2338          * be in the same order, even in case of 2 concurrent swaps */
2339         rc = lu_fid_cmp(mdd_object_fid(fst_o), mdd_object_fid(snd_o));
2340         if (rc == 0) /* same fid ? */
2341                 RETURN(-EPERM);
2342
2343         if (rc < 0)
2344                 swap(fst_o, snd_o);
2345
2346         rc = mdd_la_get(env, fst_o, fst_la);
2347         if (rc != 0)
2348                 RETURN(rc);
2349
2350         rc = mdd_la_get(env, snd_o, snd_la);
2351         if (rc != 0)
2352                 RETURN(rc);
2353
2354         /* check if layout swapping is allowed */
2355         rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2356         if (rc != 0)
2357                 RETURN(rc);
2358
2359         handle = mdd_trans_create(env, mdd);
2360         if (IS_ERR(handle))
2361                 RETURN(PTR_ERR(handle));
2362
2363         /* objects are already sorted */
2364         mdd_write_lock(env, fst_o, DT_TGT_CHILD);
2365         mdd_write_lock(env, snd_o, DT_TGT_CHILD);
2366
2367         rc = mdd_stripe_get(env, fst_o, fst_buf, XATTR_NAME_LOV);
2368         if (rc < 0 && rc != -ENODATA)
2369                 GOTO(stop, rc);
2370
2371         rc = mdd_stripe_get(env, snd_o, snd_buf, XATTR_NAME_LOV);
2372         if (rc < 0 && rc != -ENODATA)
2373                 GOTO(stop, rc);
2374
2375         /* check if file has DoM. DoM file can be migrated only to another
2376          * DoM layout with the same DoM component size or to an non-DOM
2377          * layout. After migration to OSTs layout, local MDT inode data
2378          * should be truncated.
2379          * Objects are sorted by FIDs, considering that original file's FID
2380          * is always smaller the snd_o is always original file we are migrating
2381          * from.
2382          */
2383         domsize_dom = mdd_lmm_dom_size(snd_buf->lb_buf);
2384         domsize_vlt = mdd_lmm_dom_size(fst_buf->lb_buf);
2385
2386         /* Only migration is supported for DoM files, not 'swap_layouts' so
2387          * target file must be volatile and orphan.
2388          */
2389         if (fst_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2390                 dom_o = domsize_dom ? snd_o : NULL;
2391         } else if (snd_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2392                 swap(domsize_dom, domsize_vlt);
2393                 dom_o = domsize_dom ? fst_o : NULL;
2394         } else if (domsize_dom > 0 || domsize_vlt > 0) {
2395                 /* 'lfs swap_layouts' case, neither file should have DoM */
2396                 rc = -EOPNOTSUPP;
2397                 CDEBUG(D_LAYOUT, "cannot swap layouts with DOM component, "
2398                        "use migration instead: rc = %d\n", rc);
2399                 GOTO(stop, rc);
2400         }
2401
2402         if (domsize_vlt > 0 && domsize_dom == 0) {
2403                 rc = -EOPNOTSUPP;
2404                 CDEBUG(D_LAYOUT,
2405                        "%s: cannot swap "DFID" layout: OST to DOM migration not supported: rc = %d\n",
2406                        mdd_obj_dev_name(snd_o),
2407                        PFID(mdd_object_fid(snd_o)), rc);
2408                 GOTO(stop, rc);
2409         } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
2410                 rc = -EOPNOTSUPP;
2411                 CDEBUG(D_LAYOUT,
2412                        "%s: cannot swap "DFID" layout: new layout must have same DoM component size: rc = %d\n",
2413                        mdd_obj_dev_name(fst_o),
2414                        PFID(mdd_object_fid(fst_o)), rc);
2415                 GOTO(stop, rc);
2416         } else if (domsize_vlt > 0) {
2417                 /* Migration with the same DOM component size, no need to
2418                  * truncate local data, it is still being used */
2419                 dom_o = NULL;
2420         }
2421
2422         /* swapping 2 non existant layouts is a success */
2423         if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2424                 GOTO(stop, rc = 0);
2425
2426         /* to help inode migration between MDT, it is better to
2427          * start by the no layout file (if one), so we order the swap */
2428         if (snd_buf->lb_buf == NULL) {
2429                 swap(fst_o, snd_o);
2430                 swap(fst_buf, snd_buf);
2431         }
2432
2433         fst_gen = snd_gen = 0;
2434         /* lmm and generation layout initialization */
2435         if (fst_buf->lb_buf != NULL) {
2436                 fst_lmm = fst_buf->lb_buf;
2437                 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2438                 fst_fl  = LU_XATTR_REPLACE;
2439         } else {
2440                 fst_lmm = NULL;
2441                 fst_gen = 0;
2442                 fst_fl  = LU_XATTR_CREATE;
2443         }
2444
2445         snd_lmm = snd_buf->lb_buf;
2446         mdd_get_lmm_gen(snd_lmm, &snd_gen);
2447
2448         saved_gen = fst_gen;
2449         /* increase the generation layout numbers */
2450         snd_gen++;
2451         fst_gen++;
2452
2453         /*
2454          * XXX The layout generation is used to generate component IDs for
2455          *     the composite file, we have to do some special tweaks to make
2456          *     sure the layout generation is always adequate for that job.
2457          */
2458
2459         /* Skip invalid generation number for composite layout */
2460         if ((snd_gen & LCME_ID_MASK) == 0)
2461                 snd_gen++;
2462         if ((fst_gen & LCME_ID_MASK) == 0)
2463                 fst_gen++;
2464         /* Make sure the generation is greater than all the component IDs */
2465         if (fst_gen < snd_gen)
2466                 fst_gen = snd_gen;
2467         else if (fst_gen > snd_gen)
2468                 snd_gen = fst_gen;
2469
2470         /* set the file specific informations in lmm */
2471         if (fst_lmm != NULL) {
2472                 struct ost_id temp_oi;
2473
2474                 saved_oi = &info->mti_oa.o_oi;
2475                 mdd_get_lmm_oi(fst_lmm, saved_oi);
2476                 mdd_get_lmm_oi(snd_lmm, &temp_oi);
2477                 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2478                 mdd_set_lmm_oi(fst_lmm, &temp_oi);
2479                 mdd_set_lmm_oi(snd_lmm, saved_oi);
2480         } else {
2481                 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2482                     cpu_to_le32(LOV_MAGIC_MAGIC))
2483                         snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2484                 else
2485                         GOTO(stop, rc = -EPROTO);
2486         }
2487         mdd_set_lmm_gen(snd_lmm, &fst_gen);
2488
2489         /* Prepare HSM attribute if it's required */
2490         if (flags & SWAP_LAYOUTS_MDS_HSM) {
2491                 const int buflen = sizeof(struct hsm_attrs);
2492
2493                 lu_buf_alloc(fst_hsm_buf, buflen);
2494                 lu_buf_alloc(snd_hsm_buf, buflen);
2495                 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2496                         GOTO(stop, rc = -ENOMEM);
2497
2498                 /* Read HSM attribute */
2499                 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2500                 if (rc < 0)
2501                         GOTO(stop, rc);
2502
2503                 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2504                 if (rc < 0)
2505                         GOTO(stop, rc);
2506
2507                 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2508                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
2509                                            handle);
2510                 if (rc < 0)
2511                         GOTO(stop, rc);
2512
2513                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2514                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
2515                                            handle);
2516                 if (rc < 0)
2517                         GOTO(stop, rc);
2518         }
2519
2520         /* prepare transaction */
2521         rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2522                                    fst_fl, handle);
2523         if (rc != 0)
2524                 GOTO(stop, rc);
2525
2526         if (fst_buf->lb_buf != NULL)
2527                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2528                                            XATTR_NAME_LOV, LU_XATTR_REPLACE,
2529                                            handle);
2530         else
2531                 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2532                                            handle);
2533         if (rc != 0)
2534                 GOTO(stop, rc);
2535
2536         rc = mdd_trans_start(env, mdd, handle);
2537         if (rc != 0)
2538                 GOTO(stop, rc);
2539
2540         if (flags & SWAP_LAYOUTS_MDS_HSM) {
2541                 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2542                 if (rc < 0)
2543                         GOTO(stop, rc);
2544
2545                 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2546                 if (rc < 0) {
2547                         rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2548                                                     handle);
2549                         if (rc2 < 0)
2550                                 CERROR("%s: HSM error restoring "DFID": rc = %d/%d\n",
2551                                        mdd_obj_dev_name(fst_o),
2552                                        PFID(mdd_object_fid(fst_o)), rc, rc2);
2553                         GOTO(stop, rc);
2554                 }
2555         }
2556
2557         rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2558         if (rc != 0)
2559                 GOTO(stop, rc);
2560
2561         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2562                 rc = -EOPNOTSUPP;
2563         } else {
2564                 if (fst_buf->lb_buf != NULL)
2565                         rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2566                                            LU_XATTR_REPLACE, handle);
2567                 else
2568                         rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2569         }
2570         if (rc != 0)
2571                 GOTO(out_restore, rc);
2572
2573         /* Issue one changelog record per file */
2574         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
2575         if (rc)
2576                 GOTO(stop, rc);
2577
2578         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
2579         if (rc)
2580                 GOTO(stop, rc);
2581         EXIT;
2582
2583 out_restore:
2584         if (rc != 0) {
2585                 int steps = 0;
2586
2587                 /* failure on second file, but first was done, so we have
2588                  * to roll back first. */
2589                 if (fst_buf->lb_buf != NULL) {
2590                         mdd_set_lmm_oi(fst_lmm, saved_oi);
2591                         mdd_set_lmm_gen(fst_lmm, &saved_gen);
2592                         rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2593                                             LU_XATTR_REPLACE, handle);
2594                 } else {
2595                         rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2596                 }
2597                 if (rc2 < 0)
2598                         goto do_lbug;
2599
2600                 ++steps;
2601                 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2602                 if (rc2 < 0)
2603                         goto do_lbug;
2604
2605                 ++steps;
2606                 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2607
2608         do_lbug:
2609                 if (rc2 < 0) {
2610                         /* very bad day */
2611                         CERROR("%s: unable to roll back layout swap of "DFID" and "DFID", steps: %d: rc = %d/%d\n",
2612                                mdd_obj_dev_name(fst_o),
2613                                PFID(mdd_object_fid(snd_o)),
2614                                PFID(mdd_object_fid(fst_o)),
2615                                rc, rc2, steps);
2616                         /* a solution to avoid journal commit is to panic,
2617                          * but it has strong consequences so we use LBUG to
2618                          * allow sysdamin to choose to panic or not
2619                          */
2620                         LBUG();
2621                 }
2622         }
2623
2624 stop:
2625         rc = mdd_trans_stop(env, mdd, rc, handle);
2626
2627         /* Truncate local DOM data if all went well */
2628         if (!rc && dom_o)
2629                 mdd_dom_data_truncate(env, mdd, dom_o);
2630
2631         mdd_write_unlock(env, snd_o);
2632         mdd_write_unlock(env, fst_o);
2633
2634         lu_buf_free(fst_buf);
2635         lu_buf_free(snd_buf);
2636         lu_buf_free(fst_hsm_buf);
2637         lu_buf_free(snd_hsm_buf);
2638
2639         if (!rc) {
2640                 (void) mdd_object_pfid_replace(env, fst_o);
2641                 (void) mdd_object_pfid_replace(env, snd_o);
2642         }
2643         return rc;
2644 }
2645
2646 static int mdd_declare_layout_change(const struct lu_env *env,
2647                                      struct mdd_device *mdd,
2648                                      struct mdd_object *obj,
2649                                      struct md_layout_change *mlc,
2650                                      struct thandle *handle)
2651 {
2652         int rc;
2653
2654         rc = mdo_declare_layout_change(env, obj, mlc, handle);
2655         if (rc)
2656                 return rc;
2657
2658         return mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2659                                            handle);
2660 }
2661
2662 /* For PFL, this is used to instantiate necessary component objects. */
2663 static int
2664 mdd_layout_instantiate_component(const struct lu_env *env,
2665                 struct mdd_object *obj, struct md_layout_change *mlc,
2666                 struct thandle *handle)
2667 {
2668         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2669         int rc;
2670         ENTRY;
2671
2672         if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2673                 RETURN(-ENOTSUPP);
2674
2675         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2676         /**
2677          * It's possible that another layout write intent has already
2678          * instantiated our objects, so a -EALREADY returned, and we need to
2679          * do nothing.
2680          */
2681         if (rc)
2682                 RETURN(rc == -EALREADY ? 0 : rc);
2683
2684         rc = mdd_trans_start(env, mdd, handle);
2685         if (rc)
2686                 RETURN(rc);
2687
2688         mdd_write_lock(env, obj, DT_TGT_CHILD);
2689         rc = mdo_layout_change(env, obj, mlc, handle);
2690         mdd_write_unlock(env, obj);
2691         if (rc)
2692                 RETURN(rc);
2693
2694         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
2695         RETURN(rc);
2696 }
2697
2698 /**
2699  * Change the FLR layout from RDONLY to WRITE_PENDING.
2700  *
2701  * It picks the primary mirror, and bumps the layout version, and set
2702  * layout version xattr to OST objects in a sync tx. In order to facilitate
2703  * the handling of phantom writers from evicted clients, the clients carry
2704  * layout version of the file with write RPC, so that the OSTs can verify
2705  * if the write RPCs are legitimate, meaning not from evicted clients.
2706  */
2707 static int
2708 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2709                          struct md_layout_change *mlc, struct thandle *handle)
2710 {
2711         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2712         struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2713         struct lustre_som_attrs *som = &mlc->mlc_som;
2714         int fl = 0;
2715         int rc;
2716         ENTRY;
2717
2718         /* Verify acceptable operations */
2719         switch (mlc->mlc_opc) {
2720         case MD_LAYOUT_WRITE:
2721         case MD_LAYOUT_RESYNC:
2722                 /* these are legal operations - this represents the case that
2723                  * a few mirrors were missed in the last resync. */
2724                 break;
2725         case MD_LAYOUT_RESYNC_DONE:
2726         default:
2727                 RETURN(0);
2728         }
2729
2730         som_buf->lb_buf = som;
2731         som_buf->lb_len = sizeof(*som);
2732         rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
2733         if (rc < 0 && rc != -ENODATA)
2734                 RETURN(rc);
2735
2736         if (rc > 0) {
2737                 lustre_som_swab(som);
2738                 if (som->lsa_valid & SOM_FL_STRICT)
2739                         fl = LU_XATTR_REPLACE;
2740         }
2741
2742         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2743         if (rc)
2744                 GOTO(out, rc);
2745
2746         if (fl) {
2747                 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2748                                            XATTR_NAME_SOM, fl, handle);
2749                 if (rc)
2750                         GOTO(out, rc);
2751         }
2752
2753         /* record a changelog for data mover to consume */
2754         rc = mdd_declare_changelog_store(env, mdd, CL_FLRW, NULL, NULL, handle);
2755         if (rc)
2756                 GOTO(out, rc);
2757
2758         rc = mdd_trans_start(env, mdd, handle);
2759         if (rc)
2760                 GOTO(out, rc);
2761
2762         /* it needs a sync tx to make FLR to work properly */
2763         handle->th_sync = 1;
2764
2765         mdd_write_lock(env, obj, DT_TGT_CHILD);
2766         rc = mdo_layout_change(env, obj, mlc, handle);
2767         if (!rc && fl) {
2768                 /* SOM state transition from STRICT to STALE */
2769                 som->lsa_valid = SOM_FL_STALE;
2770                 lustre_som_swab(som);
2771                 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2772                                    fl, handle);
2773         }
2774         mdd_write_unlock(env, obj);
2775         if (rc)
2776                 GOTO(out, rc);
2777
2778         rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
2779         if (rc)
2780                 GOTO(out, rc);
2781
2782         EXIT;
2783
2784 out:
2785         return rc;
2786 }
2787
2788 /**
2789  * Handle mirrored file state transition when it's in WRITE_PENDING.
2790  *
2791  * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2792  * the file is in WRITE_PENDING state. If everything goes fine, the file's
2793  * layout version will be increased, and the file's state will be changed to
2794  * SYNC_PENDING.
2795  */
2796 static int
2797 mdd_layout_update_write_pending(const struct lu_env *env,
2798                 struct mdd_object *obj, struct md_layout_change *mlc,
2799                 struct thandle *handle)
2800 {
2801         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2802         int rc;
2803         ENTRY;
2804
2805         switch (mlc->mlc_opc) {
2806         case MD_LAYOUT_RESYNC:
2807                 /* Upon receiving the resync request, it should
2808                  * instantiate all stale components right away to get ready
2809                  * for mirror copy. In order to avoid layout version change,
2810                  * client should avoid sending LAYOUT_WRITE request at the
2811                  * resync state. */
2812                 break;
2813         case MD_LAYOUT_WRITE:
2814                 /* legal race for concurrent write, the file state has been
2815                  * changed by another client. */
2816                 break;
2817         default:
2818                 RETURN(-EBUSY);
2819         }
2820
2821         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2822         if (rc)
2823                 GOTO(out, rc);
2824
2825         rc = mdd_trans_start(env, mdd, handle);
2826         if (rc)
2827                 GOTO(out, rc);
2828
2829         /* it needs a sync tx to make FLR to work properly */
2830         handle->th_sync = 1;
2831
2832         mdd_write_lock(env, obj, DT_TGT_CHILD);
2833         rc = mdo_layout_change(env, obj, mlc, handle);
2834         mdd_write_unlock(env, obj);
2835         if (rc)
2836                 GOTO(out, rc);
2837
2838         EXIT;
2839
2840 out:
2841         return rc;
2842 }
2843
2844 /**
2845  * Handle the requests when a FLR file's state is in SYNC_PENDING.
2846  *
2847  * Only concurrent write and sync complete requests are possible when the
2848  * file is in SYNC_PENDING. For the latter request, it will pass in the
2849  * mirrors that have been synchronized, then the stale bit will be cleared
2850  * to make the file's state turn into RDONLY.
2851  * For concurrent write reqeust, it just needs to change the file's state
2852  * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2853  * version because the version will be increased in the transition to
2854  * SYNC_PENDING later so that it can deny the write request from potential
2855  * evicted SYNC clients. */
2856 static int
2857 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2858                 struct md_layout_change *mlc, struct thandle *handle)
2859 {
2860         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2861         struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2862         int fl = 0;
2863         int rc;
2864         ENTRY;
2865
2866         /* operation validation */
2867         switch (mlc->mlc_opc) {
2868         case MD_LAYOUT_RESYNC_DONE:
2869                 /* resync complete. */
2870         case MD_LAYOUT_WRITE:
2871                 /* concurrent write. */
2872                 break;
2873         case MD_LAYOUT_RESYNC:
2874                 /* resync again, most likely the previous run failed.
2875                  * no-op if it's already in SYNC_PENDING state */
2876                 RETURN(0);
2877         default:
2878                 RETURN(-EBUSY);
2879         }
2880
2881         if (mlc->mlc_som.lsa_valid & SOM_FL_STRICT) {
2882                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2883                 if (rc < 0 && rc != -ENODATA)
2884                         RETURN(rc);
2885
2886                 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2887                 lustre_som_swab(&mlc->mlc_som);
2888                 som_buf->lb_buf = &mlc->mlc_som;
2889                 som_buf->lb_len = sizeof(mlc->mlc_som);
2890         }
2891
2892         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2893         if (rc)
2894                 GOTO(out, rc);
2895
2896         /* record a changelog for the completion of resync */
2897         rc = mdd_declare_changelog_store(env, mdd, CL_RESYNC, NULL, NULL,
2898                                          handle);
2899         if (rc)
2900                 GOTO(out, rc);
2901
2902         /* RESYNC_DONE has piggybacked size and blocks */
2903         if (fl) {
2904                 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2905                                            XATTR_NAME_SOM, fl, handle);
2906                 if (rc)
2907                         GOTO(out, rc);
2908         }
2909
2910         rc = mdd_trans_start(env, mdd, handle);
2911         if (rc)
2912                 GOTO(out, rc);
2913
2914         /* it needs a sync tx to make FLR to work properly */
2915         handle->th_sync = 1;
2916
2917         rc = mdo_layout_change(env, obj, mlc, handle);
2918         if (rc)
2919                 GOTO(out, rc);
2920
2921         if (fl) {
2922                 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2923                                    fl, handle);
2924                 if (rc)
2925                         GOTO(out, rc);
2926         }
2927
2928         rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
2929         if (rc)
2930                 GOTO(out, rc);
2931         EXIT;
2932 out:
2933         return rc;
2934 }
2935
2936 /**
2937  * Layout change callback for object.
2938  *
2939  * This is only used by FLR for now. In the future, it can be exteneded to
2940  * handle all layout change.
2941  */
2942 static int
2943 mdd_layout_change(const struct lu_env *env, struct md_object *o,
2944                   struct md_layout_change *mlc)
2945 {
2946         struct mdd_object       *obj = md2mdd_obj(o);
2947         struct mdd_device       *mdd = mdd_obj2mdd_dev(obj);
2948         struct lu_buf           *buf = mdd_buf_get(env, NULL, 0);
2949         struct lov_comp_md_v1   *lcm;
2950         struct thandle          *handle;
2951         int flr_state;
2952         int rc;
2953
2954         ENTRY;
2955
2956         if (S_ISDIR(mdd_object_type(obj))) {
2957                 switch (mlc->mlc_opc) {
2958                 case MD_LAYOUT_SHRINK:
2959                         rc = mdd_dir_layout_shrink(env, o, mlc);
2960                         break;
2961                 case MD_LAYOUT_SPLIT:
2962                         rc = mdd_dir_layout_split(env, o, mlc);
2963                         break;
2964                 default:
2965                         LBUG();
2966                 }
2967
2968                 RETURN(rc);
2969         }
2970
2971         /* Verify acceptable operations */
2972         switch (mlc->mlc_opc) {
2973         case MD_LAYOUT_WRITE:
2974         case MD_LAYOUT_RESYNC:
2975         case MD_LAYOUT_RESYNC_DONE:
2976                 break;
2977         default:
2978                 RETURN(-ENOTSUPP);
2979         }
2980
2981         handle = mdd_trans_create(env, mdd);
2982         if (IS_ERR(handle))
2983                 RETURN(PTR_ERR(handle));
2984
2985         rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
2986         if (rc < 0) {
2987                 if (rc == -ENODATA)
2988                         rc = -EINVAL;
2989                 GOTO(out, rc);
2990         }
2991
2992         /* analyze the layout to make sure it's a FLR file */
2993         lcm = buf->lb_buf;
2994         if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2995                 GOTO(out, rc = -EINVAL);
2996
2997         flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
2998
2999         /* please refer to HLD of FLR for state transition */
3000         switch (flr_state) {
3001         case LCM_FL_NONE:
3002                 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
3003                 break;
3004         case LCM_FL_WRITE_PENDING:
3005                 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
3006                 break;
3007         case LCM_FL_RDONLY:
3008                 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
3009                 break;
3010         case LCM_FL_SYNC_PENDING:
3011                 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
3012                 break;
3013         default:
3014                 rc = 0;
3015                 break;
3016         }
3017         EXIT;
3018
3019 out:
3020         mdd_trans_stop(env, mdd, rc, handle);
3021         lu_buf_free(buf);
3022         return rc;
3023 }
3024
3025 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
3026                           struct mdd_object *child, const struct lu_attr *attr,
3027                           const struct md_op_spec *spec,
3028                           struct dt_allocation_hint *hint)
3029 {
3030         struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
3031         struct mdd_device *mdd = mdd_obj2mdd_dev(child);
3032         struct dt_object *nc = mdd_object_child(child);
3033
3034         memset(hint, 0, sizeof(*hint));
3035
3036         /* For striped directory, give striping EA to lod_ah_init, which will
3037          * decide the stripe_offset and stripe count by it. */
3038         if (S_ISDIR(attr->la_mode) &&
3039             unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
3040                 hint->dah_eadata = spec->u.sp_ea.eadata;
3041                 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
3042         } else {
3043                 hint->dah_eadata = NULL;
3044                 hint->dah_eadata_len = 0;
3045                 if (spec->sp_cr_flags & MDS_OPEN_APPEND) {
3046                         if (mdd->mdd_append_stripe_count != 0 ||
3047                             mdd->mdd_append_pool[0])
3048                                 CDEBUG(D_INFO,
3049                                        "using O_APPEND file striping\n");
3050                         if (mdd->mdd_append_stripe_count)
3051                                 hint->dah_append_stripes =
3052                                         mdd->mdd_append_stripe_count;
3053                         if (mdd->mdd_append_pool[0])
3054                                 hint->dah_append_pool = mdd->mdd_append_pool;
3055                 } else {
3056                         hint->dah_append_stripes = 0;
3057                 }
3058         }
3059
3060         CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
3061                hint->dah_eadata, hint->dah_eadata_len);
3062         /* @hint will be initialized by underlying device. */
3063         nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
3064 }
3065
3066 static int accmode(const struct lu_env *env, const struct lu_attr *la,
3067                    u64 open_flags)
3068 {
3069         /* Sadly, NFSD reopens a file repeatedly during operation, so the
3070          * "acc_mode = 0" allowance for newly-created files isn't honoured.
3071          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
3072          * owner can write to a file even if it is marked readonly to hide
3073          * its brokenness. (bug 5781) */
3074         if (open_flags & MDS_OPEN_OWNEROVERRIDE) {
3075                 struct lu_ucred *uc = lu_ucred_check(env);
3076
3077                 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
3078                         return 0;
3079         }
3080
3081         return mds_accmode(open_flags);
3082 }
3083
3084 static int mdd_open_sanity_check(const struct lu_env *env,
3085                                  struct mdd_object *obj,
3086                                  const struct lu_attr *attr, u64 open_flags,
3087                                  int is_replay)
3088 {
3089         int mode, rc;
3090         ENTRY;
3091
3092         /* EEXIST check, also opening of *open* orphans is allowed so we can
3093          * open-by-handle unlinked files
3094          */
3095         if (mdd_is_dead_obj(obj) && !is_replay &&
3096             likely(!(mdd_is_orphan_obj(obj) && obj->mod_count > 0)))
3097                 RETURN(-ENOENT);
3098
3099         if (S_ISLNK(attr->la_mode))
3100                 RETURN(-ELOOP);
3101
3102         mode = accmode(env, attr, open_flags);
3103
3104         if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
3105                 RETURN(-EISDIR);
3106
3107         if (!(open_flags & MDS_OPEN_CREATED)) {
3108                 rc = mdd_permission_internal(env, obj, attr, mode);
3109                 if (rc)
3110                         RETURN(rc);
3111         }
3112
3113         if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
3114             S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
3115                 open_flags &= ~MDS_OPEN_TRUNC;
3116
3117         /* For writing append-only file must open it with append mode. */
3118         if (attr->la_flags & LUSTRE_APPEND_FL) {
3119                 if ((open_flags & MDS_FMODE_WRITE) &&
3120                     !(open_flags & MDS_OPEN_APPEND))
3121                         RETURN(-EPERM);
3122                 if (open_flags & MDS_OPEN_TRUNC)
3123                         RETURN(-EPERM);
3124         }
3125
3126         RETURN(0);
3127 }
3128
3129 static int mdd_open(const struct lu_env *env, struct md_object *obj,
3130                     u64 open_flags, struct md_op_spec *spec)
3131 {
3132         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3133         struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
3134         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3135         struct mdd_object_user *mou = NULL;
3136         const struct lu_ucred *uc = lu_ucred(env);
3137         struct mdd_device *mdd = mdo2mdd(obj);
3138         enum changelog_rec_type type = CL_OPEN;
3139         int rc = 0;
3140         ENTRY;
3141
3142         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3143
3144         rc = mdd_la_get(env, mdd_obj, attr);
3145         if (rc != 0)
3146                 GOTO(out, rc);
3147
3148         rc = mdd_open_sanity_check(env, mdd_obj, attr, open_flags,
3149                                    spec->no_create);
3150         if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & BIT(CL_DN_OPEN)))
3151                 type = CL_DN_OPEN;
3152         else if (rc != 0)
3153                 GOTO(out, rc);
3154         else
3155                 mdd_obj->mod_count++;
3156
3157         if (!mdd_changelog_enabled(env, mdd, type))
3158                 GOTO(out, rc);
3159
3160 find:
3161         /* look for existing opener in list under mdd_write_lock */
3162         mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, open_flags);
3163
3164         if (!mou) {
3165                 int rc2;
3166
3167                 /* add user to list */
3168                 mou = mdd_obj_user_alloc(open_flags, uc->uc_uid, uc->uc_gid);
3169                 if (IS_ERR(mou)) {
3170                         if (rc == 0)
3171                                 rc = PTR_ERR(mou);
3172                         GOTO(out, rc);
3173                 }
3174                 rc2 = mdd_obj_user_add(mdd_obj, mou, type == CL_DN_OPEN);
3175                 if (rc2 != 0) {
3176                         mdd_obj_user_free(mou);
3177                         if (rc2 == -EEXIST)
3178                                 GOTO(find, rc2);
3179                 }
3180         } else {
3181                 if (type == CL_DN_OPEN) {
3182                         if (ktime_before(ktime_get(), mou->mou_deniednext))
3183                                 /* same user denied again same access within
3184                                  * time interval: do not record
3185                                  */
3186                                 GOTO(out, rc);
3187
3188                         /* this user already denied, but some time ago:
3189                          * update denied time
3190                          */
3191                         mou->mou_deniednext =
3192                                 ktime_add(ktime_get(),
3193                                           ktime_set(mdd->mdd_cl.mc_deniednext,
3194                                                     0));
3195                 } else {
3196                         mou->mou_opencount++;
3197                         /* same user opening file again with same flags:
3198                          * don't record
3199                          */
3200                         GOTO(out, rc);
3201                 }
3202         }
3203
3204         /* FYI, only the bottom 32 bits of open_flags are recorded */
3205         mdd_changelog(env, type, open_flags, md_dev, mdd_object_fid(mdd_obj));
3206
3207         EXIT;
3208 out:
3209         mdd_write_unlock(env, mdd_obj);
3210         return rc;
3211 }
3212
3213 static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
3214                              struct md_attr *ma, struct thandle *handle)
3215 {
3216         int rc;
3217
3218         rc = mdd_orphan_declare_delete(env, obj, handle);
3219         if (rc)
3220                 return rc;
3221
3222         return mdo_declare_destroy(env, obj, handle);
3223 }
3224
3225 /*
3226  * No permission check is needed.
3227  */
3228 static int mdd_close(const struct lu_env *env, struct md_object *obj,
3229                      struct md_attr *ma, u64 open_flags)
3230 {
3231         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3232         struct mdd_device *mdd = mdo2mdd(obj);
3233         struct thandle *handle = NULL;
3234         int is_orphan = 0;
3235         int rc;
3236         bool blocked = false;
3237         bool last_close_by_uid = false;
3238         const struct lu_ucred *uc = lu_ucred(env);
3239         ENTRY;
3240
3241         if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
3242                 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3243                 mdd_obj->mod_count--;
3244                 mdd_write_unlock(env, mdd_obj);
3245
3246                 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
3247                         CDEBUG(D_HA, "Object "DFID" is retained in orphan "
3248                                 "list\n", PFID(mdd_object_fid(mdd_obj)));
3249                 RETURN(0);
3250         }
3251
3252         /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
3253          * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
3254         /* check without any lock */
3255         is_orphan = mdd_obj->mod_count == 1 &&
3256                     (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
3257
3258 again:
3259         if (is_orphan) {
3260                 /* mdd_trans_create() maybe failed because of barrier_entry(),
3261                  * under such case, the orphan MDT-object will be left in the
3262                  * orphan list, and when the MDT remount next time, the unused
3263                  * orphans will be destroyed automatically.
3264                  *
3265                  * One exception: the former mdd_finish_unlink may failed to
3266                  * add the orphan MDT-object to the orphan list, then if the
3267                  * mdd_trans_create() failed because of barrier_entry(), the
3268                  * MDT-object will become real orphan that is neither in the
3269                  * namespace nor in the orphan list. Such bad case should be
3270                  * very rare and will be handled by e2fsck/lfsck. */
3271                 handle = mdd_trans_create(env, mdo2mdd(obj));
3272                 if (IS_ERR(handle)) {
3273                         rc = PTR_ERR(handle);
3274                         if (rc != -EINPROGRESS)
3275                                 GOTO(stop, rc);
3276
3277                         handle = NULL;
3278                         blocked = true;
3279                         goto cont;
3280                 }
3281
3282                 rc = mdd_declare_close(env, mdd_obj, ma, handle);
3283                 if (rc)
3284                         GOTO(stop, rc);
3285
3286                 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE, NULL, NULL,
3287                                                  handle);
3288                 if (rc)
3289                         GOTO(stop, rc);
3290
3291                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3292                 if (rc)
3293                         GOTO(stop, rc);
3294         }
3295
3296 cont:
3297         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3298         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
3299         if (rc != 0) {
3300                 CERROR("%s: failed to get lu_attr of "DFID": rc = %d\n",
3301                        lu_dev_name(mdd2lu_dev(mdd)),
3302                        PFID(mdd_object_fid(mdd_obj)), rc);
3303                 GOTO(out, rc);
3304         }
3305
3306         /* check again with lock */
3307         is_orphan = (mdd_obj->mod_count == 1) &&
3308                     ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
3309                      ma->ma_attr.la_nlink == 0);
3310
3311         if (is_orphan && !handle && !blocked) {
3312                 mdd_write_unlock(env, mdd_obj);
3313                 goto again;
3314         }
3315
3316         mdd_obj->mod_count--; /*release open count */
3317
3318         /* under mdd write lock */
3319         /* If recording, see if we need to remove UID from list. uc is not
3320          * initialized if the client has been evicted. */
3321         if (mdd_changelog_enabled(env, mdd, CL_OPEN) && uc) {
3322                 struct mdd_object_user *mou;
3323
3324                 /* look for UID in list */
3325                 /* If mou is NULL, it probably means logging was enabled after
3326                  * the user had the file open. So the corresponding close
3327                  * will not be logged.
3328                  */
3329                 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid,
3330                                         open_flags);
3331                 if (mou) {
3332                         mou->mou_opencount--;
3333                         if (mou->mou_opencount == 0) {
3334                                 mdd_obj_user_remove(mdd_obj, mou);
3335                                 last_close_by_uid = true;
3336                         }
3337                 }
3338         }
3339
3340         if (!is_orphan || blocked)
3341                 GOTO(out, rc = 0);
3342
3343         /* Orphan object */
3344         /* NB: Object maybe not in orphan list originally, it is rare case for
3345          * mdd_finish_unlink() failure, in that case, the object doesn't have
3346          * ORPHAN_OBJ flag */
3347         if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
3348                 /* remove link to object from orphan index */
3349                 LASSERT(handle != NULL);
3350                 rc = mdd_orphan_delete(env, mdd_obj, handle);
3351                 if (rc != 0) {
3352                         CERROR("%s: unable to delete "DFID" from orphan list: "
3353                                "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3354                                PFID(mdd_object_fid(mdd_obj)), rc);
3355                         /* If object was not deleted from orphan list, do not
3356                          * destroy OSS objects, which will be done when next
3357                          * recovery. */
3358                         GOTO(out, rc);
3359                 }
3360
3361                 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
3362                        "list, OSS objects to be destroyed.\n",
3363                        PFID(mdd_object_fid(mdd_obj)));
3364         }
3365
3366         rc = mdo_destroy(env, mdd_obj, handle);
3367
3368         if (rc != 0) {
3369                 CERROR("%s: unable to delete "DFID" from orphan list: "
3370                        "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3371                        PFID(mdd_object_fid(mdd_obj)), rc);
3372         }
3373         EXIT;
3374
3375 out:
3376         mdd_write_unlock(env, mdd_obj);
3377
3378         if (rc != 0 || blocked ||
3379             !mdd_changelog_enabled(env, mdd, CL_CLOSE))
3380                 GOTO(stop, rc);
3381
3382         /* Record CL_CLOSE in changelog only if file was opened in write mode,
3383          * or if CL_OPEN was recorded and it's last close by user.
3384          * Changelogs mask may change between open and close operations, but
3385          * this is not a big deal if we have a CL_CLOSE entry with no matching
3386          * CL_OPEN. Plus Changelogs mask may not change often.
3387          */
3388         if (((!(mdd->mdd_cl.mc_mask & BIT(CL_OPEN)) &&
3389               (open_flags & (MDS_FMODE_WRITE | MDS_OPEN_APPEND |
3390                              MDS_OPEN_TRUNC))) ||
3391              ((mdd->mdd_cl.mc_mask & BIT(CL_OPEN)) && last_close_by_uid)) &&
3392             !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
3393                 if (handle == NULL) {
3394                         handle = mdd_trans_create(env, mdo2mdd(obj));
3395                         if (IS_ERR(handle))
3396                                 GOTO(stop, rc = PTR_ERR(handle));
3397
3398                         rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE,
3399                                                          NULL, NULL, handle);
3400                         if (rc)
3401                                 GOTO(stop, rc);
3402
3403                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3404                         if (rc)
3405                                 GOTO(stop, rc);
3406                 }
3407
3408                 /* FYI, only the bottom 32 bits of open_flags are recorded */
3409                 mdd_changelog_data_store(env, mdd, CL_CLOSE, open_flags,
3410                                          mdd_obj, handle);
3411         }
3412
3413 stop:
3414         if (handle != NULL && !IS_ERR(handle))
3415                 rc = mdd_trans_stop(env, mdd, rc, handle);
3416
3417         return rc;
3418 }
3419
3420 /*
3421  * Permission check is done when open,
3422  * no need check again.
3423  */
3424 static int mdd_readpage_sanity_check(const struct lu_env *env,
3425                                      struct mdd_object *obj)
3426 {
3427         struct dt_object *next = mdd_object_child(obj);
3428         int rc;
3429         ENTRY;
3430
3431         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
3432                 rc = 0;
3433         else
3434                 rc = -ENOTDIR;
3435
3436         RETURN(rc);
3437 }
3438
3439 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
3440                               size_t nob, const struct dt_it_ops *iops,
3441                               struct dt_it *it, __u32 attr, void *arg)
3442 {
3443         struct lu_dirpage       *dp = &lp->lp_dir;
3444         void                    *area = dp;
3445         int                      result;
3446         __u64                    hash = 0;
3447         struct lu_dirent        *ent;
3448         struct lu_dirent        *last = NULL;
3449         struct lu_fid            fid;
3450         int                      first = 1;
3451
3452         if (nob < sizeof(*dp))
3453                 return -EINVAL;
3454
3455         memset(area, 0, sizeof (*dp));
3456         area += sizeof (*dp);
3457         nob  -= sizeof (*dp);
3458
3459         ent  = area;
3460         do {
3461                 int    len;
3462                 size_t recsize;
3463
3464                 len = iops->key_size(env, it);
3465
3466                 /* IAM iterator can return record with zero len. */
3467                 if (len == 0)
3468                         goto next;
3469
3470                 hash = iops->store(env, it);
3471                 if (unlikely(first)) {
3472                         first = 0;
3473                         dp->ldp_hash_start = cpu_to_le64(hash);
3474                 }
3475
3476                 /* calculate max space required for lu_dirent */
3477                 recsize = lu_dirent_calc_size(len, attr);
3478
3479                 if (nob >= recsize) {
3480                         result = iops->rec(env, it, (struct dt_rec *)ent, attr);
3481                         if (result == -ESTALE)
3482                                 goto next;
3483                         if (result != 0)
3484                                 goto out;
3485
3486                         /* osd might not able to pack all attributes,
3487                          * so recheck rec length */
3488                         recsize = le16_to_cpu(ent->lde_reclen);
3489
3490                         if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
3491                                 fid_le_to_cpu(&fid, &ent->lde_fid);
3492                                 if (fid_is_dot_lustre(&fid))
3493                                         goto next;
3494                         }
3495                 } else {
3496                         result = (last != NULL) ? 0 :-EINVAL;
3497                         goto out;
3498                 }
3499                 last = ent;
3500                 ent = (void *)ent + recsize;
3501                 nob -= recsize;
3502
3503 next:
3504                 result = iops->next(env, it);
3505                 if (result == -ESTALE)
3506                         goto next;
3507         } while (result == 0);
3508
3509 out:
3510         dp->ldp_hash_end = cpu_to_le64(hash);
3511         if (last != NULL) {
3512                 if (last->lde_hash == dp->ldp_hash_end)
3513                         dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
3514                 last->lde_reclen = 0; /* end mark */
3515         }
3516         if (result > 0)
3517                 /* end of directory */
3518                 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3519         else if (result < 0)
3520                 CWARN("build page failed: %d!\n", result);
3521         return result;
3522 }
3523
3524 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
3525                  const struct lu_rdpg *rdpg)
3526 {
3527         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3528         int rc;
3529         ENTRY;
3530
3531         if (mdd_object_exists(mdd_obj) == 0) {
3532                 CERROR("%s: object "DFID" not found: rc = -2\n",
3533                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
3534                 return -ENOENT;
3535         }
3536
3537         mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
3538         rc = mdd_readpage_sanity_check(env, mdd_obj);
3539         if (rc)
3540                 GOTO(out_unlock, rc);
3541
3542         if (mdd_is_dead_obj(mdd_obj)) {
3543                 struct page *pg;
3544                 struct lu_dirpage *dp;
3545
3546                 /*
3547                  * According to POSIX, please do not return any entry to client:
3548                  * even dot and dotdot should not be returned.
3549                  */
3550                 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
3551                        PFID(mdd_object_fid(mdd_obj)));
3552
3553                 if (rdpg->rp_count <= 0)
3554                         GOTO(out_unlock, rc = -EFAULT);
3555                 LASSERT(rdpg->rp_pages != NULL);
3556
3557                 pg = rdpg->rp_pages[0];
3558                 dp = (struct lu_dirpage *)kmap(pg);
3559                 memset(dp, 0 , sizeof(struct lu_dirpage));
3560                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3561                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
3562                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3563                 kunmap(pg);
3564                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
3565         }
3566
3567         rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
3568                            mdd_dir_page_build, NULL);
3569         if (rc >= 0) {
3570                 struct lu_dirpage       *dp;
3571
3572                 dp = kmap(rdpg->rp_pages[0]);
3573                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3574                 if (rc == 0) {
3575                         /*
3576                          * No pages were processed, mark this for first page
3577                          * and send back.
3578                          */
3579                         dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3580                         dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3581                         rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
3582                 }
3583                 kunmap(rdpg->rp_pages[0]);
3584         }
3585
3586         GOTO(out_unlock, rc);
3587 out_unlock:
3588         mdd_read_unlock(env, mdd_obj);
3589         return rc;
3590 }
3591
3592 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
3593 {
3594         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3595
3596         if (mdd_object_exists(mdd_obj) == 0) {
3597                 int rc = -ENOENT;
3598
3599                 CERROR("%s: object "DFID" not found: rc = %d\n",
3600                        mdd_obj_dev_name(mdd_obj),
3601                        PFID(mdd_object_fid(mdd_obj)), rc);
3602                 return rc;
3603         }
3604         return dt_object_sync(env, mdd_object_child(mdd_obj),
3605                               0, OBD_OBJECT_EOF);
3606 }
3607
3608 static int mdd_object_lock(const struct lu_env *env,
3609                            struct md_object *obj,
3610                            struct lustre_handle *lh,
3611                            struct ldlm_enqueue_info *einfo,
3612                            union ldlm_policy_data *policy)
3613 {
3614         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3615         return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
3616                               einfo, policy);
3617 }
3618
3619 static int mdd_object_unlock(const struct lu_env *env,
3620                              struct md_object *obj,
3621                              struct ldlm_enqueue_info *einfo,
3622                              union ldlm_policy_data *policy)
3623 {
3624         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3625         return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
3626 }
3627
3628 const struct md_object_operations mdd_obj_ops = {
3629         .moo_permission         = mdd_permission,
3630         .moo_attr_get           = mdd_attr_get,
3631         .moo_attr_set           = mdd_attr_set,
3632         .moo_xattr_get          = mdd_xattr_get,
3633         .moo_xattr_set          = mdd_xattr_set,
3634         .moo_xattr_list         = mdd_xattr_list,
3635         .moo_invalidate         = mdd_invalidate,
3636         .moo_xattr_del          = mdd_xattr_del,
3637         .moo_swap_layouts       = mdd_swap_layouts,
3638         .moo_open               = mdd_open,
3639         .moo_close              = mdd_close,
3640         .moo_readpage           = mdd_readpage,
3641         .moo_readlink           = mdd_readlink,
3642         .moo_changelog          = mdd_changelog,
3643         .moo_object_sync        = mdd_object_sync,
3644         .moo_object_lock        = mdd_object_lock,
3645         .moo_object_unlock      = mdd_object_unlock,
3646         .moo_layout_change      = mdd_layout_change,
3647 };