Whamcloud - gitweb
LU-14430 mdd: use own buffer for changelog
[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  *
31  * lustre/mdd/mdd_object.c
32  *
33  * Lustre Metadata Server (mdd) routines
34  *
35  * Author: Wang Di <wangdi@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lprocfs_status.h>
45 /* fid_be_cpu(), fid_cpu_to_be(). */
46 #include <lustre_fid.h>
47 #include <lustre_idmap.h>
48 #include <uapi/linux/lustre/lustre_param.h>
49 #include <lustre_mds.h>
50
51 #include "mdd_internal.h"
52
53 static const struct lu_object_operations mdd_lu_obj_ops;
54
55 struct mdd_object_user {
56         struct list_head        mou_list;       /**< linked off mod_users */
57         u64                     mou_open_flags; /**< open mode by client */
58         __u64                   mou_uidgid;     /**< uid_gid on client */
59         int                     mou_opencount;  /**< # opened */
60         ktime_t                 mou_deniednext; /**< time of next access denied
61                                                  * notfication
62                                                  */
63 };
64
65 static int mdd_xattr_get(const struct lu_env *env,
66                          struct md_object *obj, struct lu_buf *buf,
67                          const char *name);
68
69 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
70                                            struct mdd_device *mdd,
71                                            enum changelog_rec_type type,
72                                            enum changelog_rec_flags clf_flags,
73                                            const struct lu_fid *fid,
74                                            const struct lu_fid *pfid,
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                                                       NULL, 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, 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, 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, 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, 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, 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, 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, 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, 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 struct lu_fid *pfid,
888                                            const char *xattr_name,
889                                            struct thandle *handle)
890 {
891         const struct lu_ucred *uc = lu_ucred(env);
892         enum changelog_rec_extra_flags xflags = CLFE_INVALID;
893         struct llog_changelog_rec *rec;
894         struct lu_buf *buf;
895         int reclen;
896         int rc;
897
898         clf_flags = (clf_flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
899
900         if (uc) {
901                 if (uc->uc_jobid[0] != '\0')
902                         clf_flags |= CLF_JOBID;
903                 xflags |= CLFE_UIDGID;
904                 xflags |= CLFE_NID;
905         }
906         if (type == CL_OPEN || type == CL_DN_OPEN)
907                 xflags |= CLFE_OPEN;
908         if (type == CL_SETXATTR || type == CL_GETXATTR)
909                 xflags |= CLFE_XATTR;
910
911         reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
912                                changelog_rec_offset(clf_flags & CLF_SUPPORTED,
913                                                     xflags & CLFE_SUPPORTED));
914         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_chlg_buf, reclen);
915         if (buf->lb_buf == NULL)
916                 RETURN(-ENOMEM);
917         rec = buf->lb_buf;
918
919         rec->cr_hdr.lrh_len = reclen;
920         rec->cr.cr_flags = clf_flags;
921         rec->cr.cr_type = (__u32)type;
922         rec->cr.cr_tfid = *fid;
923         if (pfid)
924                 rec->cr.cr_pfid = *pfid;
925         rec->cr.cr_namelen = 0;
926
927         if (clf_flags & CLF_JOBID)
928                 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
929
930         if (clf_flags & CLF_EXTRA_FLAGS) {
931                 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
932                 if (xflags & CLFE_UIDGID)
933                         mdd_changelog_rec_extra_uidgid(&rec->cr,
934                                                        uc->uc_uid, uc->uc_gid);
935                 if (xflags & CLFE_NID)
936                         mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
937                 if (xflags & CLFE_OPEN)
938                         mdd_changelog_rec_extra_omode(&rec->cr, clf_flags);
939                 if (xflags & CLFE_XATTR) {
940                         if (xattr_name == NULL)
941                                 RETURN(-EINVAL);
942                         mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
943                 }
944         }
945
946         rc = mdd_changelog_store(env, mdd, rec, handle);
947         RETURN(rc);
948 }
949
950
951 /** Store a data change changelog record
952  * If this fails, we must fail the whole transaction; we don't
953  * want the change to commit without the log entry.
954  * \param mdd_obj - mdd_object of change
955  * \param handle - transaction handle
956  * \param pfid - parent FID for CL_MTIME changelogs
957  */
958 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
959                              enum changelog_rec_type type,
960                              enum changelog_rec_flags clf_flags,
961                              struct mdd_object *mdd_obj, struct thandle *handle,
962                              const struct lu_fid *pfid)
963 {
964         int                              rc;
965
966         LASSERT(mdd_obj != NULL);
967         LASSERT(handle != NULL);
968
969         if (!mdd_changelog_enabled(env, mdd, type))
970                 RETURN(0);
971
972         if (mdd_is_volatile_obj(mdd_obj))
973                 RETURN(0);
974
975         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
976             ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
977                 /* Don't need multiple updates in this log */
978                 /* Don't check under lock - no big deal if we get an extra
979                    entry */
980                 RETURN(0);
981         }
982
983         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
984                                              mdd_object_fid(mdd_obj), pfid,
985                                              NULL, handle);
986         if (rc == 0)
987                 mdd_obj->mod_cltime = ktime_get();
988
989         RETURN(rc);
990 }
991
992 int mdd_changelog_data_store_xattr(const struct lu_env *env,
993                                    struct mdd_device *mdd,
994                                    enum changelog_rec_type type,
995                                    enum changelog_rec_flags clf_flags,
996                                    struct mdd_object *mdd_obj,
997                                    const char *xattr_name,
998                                    struct thandle *handle)
999 {
1000         int rc;
1001
1002         LASSERT(mdd_obj != NULL);
1003         LASSERT(handle != NULL);
1004
1005         if (!mdd_changelog_enabled(env, mdd, type))
1006                 RETURN(0);
1007
1008         if (mdd_is_volatile_obj(mdd_obj))
1009                 RETURN(0);
1010
1011         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
1012             ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
1013                 /* Don't need multiple updates in this log */
1014                 /* Don't check under lock - no big deal if we get an extra
1015                  * entry
1016                  */
1017                 RETURN(0);
1018         }
1019
1020         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1021                                              mdd_object_fid(mdd_obj), NULL,
1022                                              xattr_name, handle);
1023         if (rc == 0)
1024                 mdd_obj->mod_cltime = ktime_get();
1025
1026         RETURN(rc);
1027 }
1028
1029 /* only the bottom CLF_FLAGSHIFT bits of @flags are stored in the record,
1030  * except for open flags have a dedicated record to store 32 bits of flags */
1031 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
1032                          enum changelog_rec_flags clf_flags,
1033                          struct md_device *m, const struct lu_fid *fid)
1034 {
1035         struct thandle *handle;
1036         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1037         int rc;
1038         ENTRY;
1039
1040         LASSERT(fid != NULL);
1041
1042         /* We'll check this again below, but we check now before we
1043          * start a transaction. */
1044         if (!mdd_changelog_enabled(env, mdd, type))
1045                 RETURN(0);
1046
1047         handle = mdd_trans_create(env, mdd);
1048         if (IS_ERR(handle))
1049                 RETURN(PTR_ERR(handle));
1050
1051         rc = mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1052         if (rc)
1053                 GOTO(stop, rc);
1054
1055         rc = mdd_trans_start(env, mdd, handle);
1056         if (rc)
1057                 GOTO(stop, rc);
1058
1059         rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1060                                              fid, NULL, NULL, handle);
1061
1062 stop:
1063         rc = mdd_trans_stop(env, mdd, rc, handle);
1064
1065         RETURN(rc);
1066 }
1067
1068 /**
1069  * Save LMA extended attributes with data from \a ma.
1070  *
1071  * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
1072  * not, LMA EA will be first read from disk, modified and write back.
1073  *
1074  */
1075 /* Precedence for choosing record type when multiple
1076  * attributes change: setattr > mtime > ctime > atime
1077  * (ctime changes when mtime does, plus chmod/chown.
1078  * atime and ctime are independent.) */
1079 static int mdd_attr_set_changelog(const struct lu_env *env,
1080                                   struct md_object *obj, struct thandle *handle,
1081                                   const struct lu_fid *pfid, __u64 valid)
1082 {
1083         struct mdd_device *mdd = mdo2mdd(obj);
1084         int bits, type = 0;
1085
1086         bits =  (valid & LA_SIZE)  ? BIT(CL_TRUNC) : 0;
1087         bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? BIT(CL_SETATTR) : 0;
1088         bits |= (valid & LA_MTIME) ? BIT(CL_MTIME) : 0;
1089         bits |= (valid & LA_CTIME) ? BIT(CL_CTIME) : 0;
1090         bits |= (valid & LA_ATIME) ? BIT(CL_ATIME) : 0;
1091         bits = bits & mdd->mdd_cl.mc_mask;
1092         /* This is an implementation limit rather than a protocol limit */
1093         BUILD_BUG_ON(CL_LAST > sizeof(int) * 8);
1094         if (bits == 0)
1095                 return 0;
1096
1097         /* The record type is the lowest non-masked set bit */
1098         type = __ffs(bits);
1099
1100         /* XXX: we only store the low CLF_FLAGMASK bits of la_valid */
1101         return mdd_changelog_data_store(env, mdd, type, valid, md2mdd_obj(obj),
1102                                         handle, pfid);
1103 }
1104
1105 static int mdd_declare_attr_set(const struct lu_env *env,
1106                                 struct mdd_device *mdd,
1107                                 struct mdd_object *obj,
1108                                 const struct lu_attr *attr,
1109                                 struct thandle *handle)
1110 {
1111         int rc;
1112
1113         rc = mdo_declare_attr_set(env, obj, attr, handle);
1114         if (rc)
1115                 return rc;
1116
1117 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1118         if (attr->la_valid & LA_MODE) {
1119                 mdd_read_lock(env, obj, DT_TGT_CHILD);
1120                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
1121                                    XATTR_NAME_ACL_ACCESS);
1122                 mdd_read_unlock(env, obj);
1123                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
1124                         rc = 0;
1125                 else if (rc < 0)
1126                         return rc;
1127
1128                 if (rc != 0) {
1129                         struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
1130                         rc = mdo_declare_xattr_set(env, obj, buf,
1131                                                    XATTR_NAME_ACL_ACCESS, 0,
1132                                                    handle);
1133                         if (rc)
1134                                 return rc;
1135                 }
1136         }
1137 #endif
1138
1139         rc = mdd_declare_changelog_store(env, mdd, CL_SETXATTR, NULL, NULL,
1140                                          handle);
1141         return rc;
1142 }
1143
1144 /*
1145  * LU-3671
1146  * LU-7239
1147  *
1148  * permission changes may require sync operation, to mitigate performance
1149  * impact, only do this for dir and when permission is reduced.
1150  *
1151  * For regular files, version is updated with permission change (see VBR), async
1152  * permission won't cause any issue, while missing permission change on
1153  * directory may affect accessibility of other objects after recovery.
1154  */
1155 static inline bool permission_needs_sync(const struct lu_attr *old,
1156                                          const struct lu_attr *new)
1157 {
1158         if (!S_ISDIR(old->la_mode))
1159                 return false;
1160
1161         if (new->la_valid & LA_UID && old->la_uid != new->la_uid)
1162                 return true;
1163
1164         if (new->la_valid & LA_GID && old->la_gid != new->la_gid)
1165                 return true;
1166
1167         if (new->la_valid & LA_MODE) {
1168                 /* turned on sticky bit */
1169                 if (!(old->la_mode & S_ISVTX) && (new->la_mode & S_ISVTX))
1170                         return true;
1171
1172                 /* set-GID has no impact on what is allowed, not checked */
1173
1174                 /* turned off setuid bit, or one of rwx for someone */
1175                 if (((new->la_mode & old->la_mode) & (0777 | S_ISUID)) !=
1176                      (old->la_mode & (0777 | S_ISUID)))
1177                         return true;
1178         }
1179
1180         return false;
1181 }
1182
1183 static inline __u64 mdd_lmm_dom_size(void *buf)
1184 {
1185         struct lov_mds_md *lmm = buf;
1186         struct lov_comp_md_v1 *comp_v1;
1187         struct lov_mds_md *v1;
1188         __u32 off;
1189
1190         if (lmm == NULL)
1191                 return 0;
1192
1193         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1194                 return 0;
1195
1196         comp_v1 = (struct lov_comp_md_v1 *)lmm;
1197         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1198         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1199
1200         /* DoM entry is the first entry always */
1201         if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) == LOV_PATTERN_MDT)
1202                 return le64_to_cpu(comp_v1->lcm_entries[0].lcme_extent.e_end);
1203
1204         return 0;
1205 }
1206
1207 /* set attr and LOV EA at once, return updated attr */
1208 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
1209                  const struct md_attr *ma)
1210 {
1211         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1212         struct mdd_device *mdd = mdo2mdd(obj);
1213         struct thandle *handle = NULL;
1214         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1215         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1216         const struct lu_attr *la = &ma->ma_attr;
1217         struct lu_ucred  *uc;
1218         bool chrgrp_by_unprivileged_user = false;
1219         int rc;
1220         ENTRY;
1221
1222         /* we do not use ->attr_set() for LOV/HSM EA any more */
1223         LASSERT((ma->ma_valid & MA_LOV) == 0);
1224         LASSERT((ma->ma_valid & MA_HSM) == 0);
1225
1226         rc = mdd_la_get(env, mdd_obj, attr);
1227         if (rc)
1228                 RETURN(rc);
1229
1230         *la_copy = ma->ma_attr;
1231         rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma);
1232         if (rc)
1233                 RETURN(rc);
1234
1235         /* no need to setattr anymore */
1236         if (la_copy->la_valid == 0) {
1237                 CDEBUG(D_INODE,
1238                        "%s: no valid attribute on "DFID", previous was %#llx\n",
1239                        mdd_obj_dev_name(mdd_obj),
1240                        PFID(mdd_object_fid(mdd_obj)), la->la_valid);
1241
1242                 RETURN(0);
1243         }
1244
1245         /* If an unprivileged user changes group of some file,
1246          * the setattr operation will be processed synchronously to
1247          * honor the quota limit of the corresponding group. see LU-5152 */
1248         uc = lu_ucred_check(env);
1249         if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1250             la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1251                 /* LU-10048: disable synchronous chgrp operation for it will
1252                  * cause deadlock between MDT and OST.
1253                 la_copy->la_valid |= LA_FLAGS;
1254                 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1255                  */
1256                 chrgrp_by_unprivileged_user = true;
1257
1258                 /* Flush the possible existing client setattr requests to OSTs
1259                  * to keep the order with the current setattr operation that
1260                  * will be sent directly to OSTs. see LU-5152 */
1261                 /* LU-11303 disable sync as this is too heavyweight.
1262                  * This should be replaced with a sync only for the object
1263                  * being modified here, not the whole filesystem.
1264                 rc = dt_sync(env, mdd->mdd_child);
1265                 if (rc)
1266                         GOTO(out, rc);
1267                  */
1268         }
1269
1270         handle = mdd_trans_create(env, mdd);
1271         if (IS_ERR(handle)) {
1272                 rc = PTR_ERR(handle);
1273                 handle = NULL;
1274
1275                 GOTO(out, rc);
1276         }
1277
1278         rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1279         if (rc)
1280                 GOTO(out, rc);
1281
1282         rc = mdd_trans_start(env, mdd, handle);
1283         if (rc)
1284                 GOTO(out, rc);
1285
1286         if (!chrgrp_by_unprivileged_user && mdd->mdd_sync_permission &&
1287             permission_needs_sync(attr, la))
1288                 handle->th_sync = 1;
1289
1290         if (la->la_valid & (LA_MTIME | LA_CTIME))
1291                 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1292                        la->la_mtime, la->la_ctime);
1293
1294         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
1295
1296         /* LU-10509: setattr of LA_SIZE should be skipped case of DOM,
1297          * otherwise following truncate will do nothing and truncated
1298          * data may be read again. This is a quick fix until LU-11033
1299          * will be resolved.
1300          */
1301         if (la_copy->la_valid & LA_SIZE) {
1302                 struct lu_buf *lov_buf = mdd_buf_get(env, NULL, 0);
1303
1304                 rc = mdd_stripe_get(env, mdd_obj, lov_buf, XATTR_NAME_LOV);
1305                 if (rc) {
1306                         rc = 0;
1307                 } else {
1308                         if (mdd_lmm_dom_size(lov_buf->lb_buf) > 0)
1309                                 la_copy->la_valid &= ~LA_SIZE;
1310                         lu_buf_free(lov_buf);
1311                 }
1312         }
1313
1314         if (la_copy->la_valid) {
1315                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1316
1317                 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1318                         /* rollback to the original gid */
1319                         la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1320                         la_copy->la_gid = attr->la_gid;
1321                         mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1322                 }
1323         }
1324         mdd_write_unlock(env, mdd_obj);
1325
1326 out:
1327         if (rc == 0)
1328                 rc = mdd_attr_set_changelog(env, obj, handle, &ma->ma_pfid,
1329                                             la_copy->la_valid);
1330
1331         if (handle != NULL)
1332                 rc = mdd_trans_stop(env, mdd, rc, handle);
1333
1334         return rc;
1335 }
1336
1337 static int mdd_xattr_sanity_check(const struct lu_env *env,
1338                                   struct mdd_object *obj,
1339                                   const struct lu_attr *attr,
1340                                   const char *name)
1341 {
1342         struct lu_ucred *uc     = lu_ucred_assert(env);
1343         ENTRY;
1344
1345         if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1346                 RETURN(-EPERM);
1347
1348         if (strncmp(XATTR_USER_PREFIX, name,
1349                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1350                 /* For sticky directories, only the owner and privileged user
1351                  * can write attributes. */
1352                 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1353                     (uc->uc_fsuid != attr->la_uid) &&
1354                     !md_capable(uc, CAP_FOWNER))
1355                         RETURN(-EPERM);
1356         } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
1357                    (uc->uc_fsuid != attr->la_uid) &&
1358                    !md_capable(uc, CAP_FOWNER)) {
1359                 RETURN(-EPERM);
1360         }
1361
1362         RETURN(0);
1363 }
1364
1365 /**
1366  * Check if a string begins with a given prefix.
1367  *
1368  * \param str     String to check
1369  * \param prefix  Substring to check at the beginning of \a str
1370  * \return true/false whether the condition is verified.
1371  */
1372 static inline bool has_prefix(const char *str, const char *prefix)
1373 {
1374         return strncmp(prefix, str, strlen(prefix)) == 0;
1375 }
1376
1377 /**
1378  * Indicate the kind of changelog to store (if any) for a xattr set/del.
1379  *
1380  * \param[in]  xattr_name  Full extended attribute name.
1381  *
1382  * \return type of changelog to use, or CL_NONE if no changelog is to be emitted
1383  */
1384 static enum changelog_rec_type
1385 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1386                          const char *xattr_name)
1387 {
1388         /* Layout changes systematically recorded */
1389         if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1390             strcmp(XATTR_LUSTRE_LOV, xattr_name) == 0 ||
1391             allowed_lustre_lov(xattr_name))
1392                 return CL_LAYOUT;
1393
1394         /* HSM information changes systematically recorded */
1395         if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1396                 return CL_HSM;
1397
1398         /* Avoid logging SOM xattr for every file */
1399         if (strcmp(XATTR_NAME_SOM, xattr_name) == 0)
1400                 return CL_NONE;
1401
1402         if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1403             has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1404             has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1405             has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1406             has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1407                 return CL_SETXATTR;
1408
1409         return CL_NONE;
1410 }
1411
1412 static int mdd_declare_xattr_set(const struct lu_env *env,
1413                                  struct mdd_device *mdd,
1414                                  struct mdd_object *obj,
1415                                  const struct lu_buf *buf,
1416                                  const char *name,
1417                                  int fl, struct thandle *handle)
1418 {
1419         enum changelog_rec_type type;
1420         int rc;
1421
1422         rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1423         if (rc)
1424                 return rc;
1425
1426         type = mdd_xattr_changelog_type(env, mdd, name);
1427         if (type < 0)
1428                 return 0; /* no changelog to store */
1429
1430         return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1431 }
1432
1433 /*
1434  * Compare current and future data of HSM EA and add a changelog if needed.
1435  *
1436  * Caller should have write-locked \param obj.
1437  *
1438  * \param buf - Future HSM EA content.
1439  * \retval 0 if no changelog is needed or changelog was added properly.
1440  * \retval -ve errno if there was a problem
1441  */
1442 static int mdd_hsm_update_locked(const struct lu_env *env,
1443                                  struct md_object *obj,
1444                                  const struct lu_buf *buf,
1445                                  struct thandle *handle,
1446                                  enum changelog_rec_flags *clf_flags)
1447 {
1448         struct mdd_thread_info *info = mdd_env_info(env);
1449         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1450         struct lu_buf *current_buf;
1451         struct md_hsm *current_mh;
1452         struct md_hsm *new_mh;
1453         int rc;
1454
1455         ENTRY;
1456         OBD_ALLOC_PTR(current_mh);
1457         if (current_mh == NULL)
1458                 RETURN(-ENOMEM);
1459
1460         /* Read HSM attrs from disk */
1461         current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1462                         min_t(unsigned int,
1463                               mdd_obj2mdd_dev(mdd_obj)->mdd_dt_conf.ddp_max_ea_size,
1464                             XATTR_SIZE_MAX));
1465         rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1466         rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1467         if (rc < 0 && rc != -ENODATA)
1468                 GOTO(free, rc);
1469         else if (rc == -ENODATA)
1470                 current_mh->mh_flags = 0;
1471
1472         /* Map future HSM xattr */
1473         OBD_ALLOC_PTR(new_mh);
1474         if (new_mh == NULL)
1475                 GOTO(free, rc = -ENOMEM);
1476         lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1477
1478         rc = 0;
1479
1480         /* Flags differ, set flags for the changelog that will be added */
1481         if (current_mh->mh_flags != new_mh->mh_flags) {
1482                 hsm_set_cl_event(clf_flags, HE_STATE);
1483                 if (new_mh->mh_flags & HS_DIRTY)
1484                         hsm_set_cl_flags(clf_flags, CLF_HSM_DIRTY);
1485         }
1486
1487         OBD_FREE_PTR(new_mh);
1488         EXIT;
1489 free:
1490         OBD_FREE_PTR(current_mh);
1491         return rc;
1492 }
1493
1494 static int mdd_object_pfid_replace(const struct lu_env *env,
1495                                    struct mdd_object *o)
1496 {
1497         struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1498         struct thandle *handle;
1499         int rc;
1500
1501         handle = mdd_trans_create(env, mdd);
1502         if (IS_ERR(handle))
1503                 RETURN(PTR_ERR(handle));
1504
1505         handle->th_complex = 1;
1506
1507         /* it doesn't need to track the PFID update via llog, because LFSCK
1508          * will repair it even it goes wrong */
1509         rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1510                                    0, handle);
1511         if (rc)
1512                 GOTO(out, rc);
1513
1514         rc = mdd_trans_start(env, mdd, handle);
1515         if (rc != 0)
1516                 GOTO(out, rc);
1517
1518         rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1519         if (rc)
1520                 GOTO(out, rc);
1521
1522 out:
1523         mdd_trans_stop(env, mdd, rc, handle);
1524         return rc;
1525 }
1526
1527
1528 static int mdd_declare_xattr_del(const struct lu_env *env,
1529                                  struct mdd_device *mdd,
1530                                  struct mdd_object *obj,
1531                                  const char *name,
1532                                  struct thandle *handle);
1533
1534 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1535                          const char *name);
1536
1537 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1538                            struct md_object *md_vic)
1539 {
1540         struct mdd_device *mdd = mdo2mdd(md_obj);
1541         struct mdd_object *obj = md2mdd_obj(md_obj);
1542         struct mdd_object *vic = md2mdd_obj(md_vic);
1543         struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1544         struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1545         struct lov_mds_md *lmm;
1546         struct thandle *handle;
1547         int rc;
1548         ENTRY;
1549
1550         rc = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
1551         if (rc == 0) /* same fid */
1552                 RETURN(-EPERM);
1553
1554         handle = mdd_trans_create(env, mdd);
1555         if (IS_ERR(handle))
1556                 RETURN(PTR_ERR(handle));
1557
1558         if (rc > 0) {
1559                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1560                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1561         } else {
1562                 mdd_write_lock(env, vic, DT_TGT_CHILD);
1563                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1564         }
1565
1566         /* get EA of victim file */
1567         memset(buf_vic, 0, sizeof(*buf_vic));
1568         rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
1569         if (rc < 0) {
1570                 if (rc == -ENODATA)
1571                         rc = 0;
1572                 GOTO(out, rc);
1573         }
1574
1575         /* parse the layout of victim file */
1576         lmm = buf_vic->lb_buf;
1577         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1578                 GOTO(out, rc = -EINVAL);
1579
1580         /* save EA of target file for restore */
1581         memset(buf, 0, sizeof(*buf));
1582         rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
1583         if (rc < 0)
1584                 GOTO(out, rc);
1585
1586         /* Get rid of the layout from victim object */
1587         rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1588         if (rc)
1589                 GOTO(out, rc);
1590
1591         rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1592                                    LU_XATTR_MERGE, handle);
1593         if (rc)
1594                 GOTO(out, rc);
1595
1596         rc = mdd_trans_start(env, mdd, handle);
1597         if (rc != 0)
1598                 GOTO(out, rc);
1599
1600         rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1601                            handle);
1602         if (rc)
1603                 GOTO(out, rc);
1604
1605         rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1606         if (rc) /* wtf? */
1607                 GOTO(out_restore, rc);
1608
1609         (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
1610                                        NULL);
1611         (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle,
1612                                        NULL);
1613         EXIT;
1614
1615 out_restore:
1616         if (rc) {
1617                 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1618                                         LU_XATTR_REPLACE, handle);
1619                 if (rc2)
1620                         CERROR("%s: failed rollback of "DFID" layout: file state unknown: rc = %d\n",
1621                                mdd_obj_dev_name(obj),
1622                                PFID(mdd_object_fid(obj)), rc2);
1623         }
1624
1625 out:
1626         mdd_trans_stop(env, mdd, rc, handle);
1627         mdd_write_unlock(env, obj);
1628         mdd_write_unlock(env, vic);
1629         lu_buf_free(buf);
1630         lu_buf_free(buf_vic);
1631
1632         if (!rc)
1633                 (void) mdd_object_pfid_replace(env, obj);
1634
1635         return rc;
1636 }
1637
1638 /**
1639  * Extract the mirror with specified mirror id, and store the splitted
1640  * mirror layout to @buf.
1641  *
1642  * \param[in] comp_v1   mirrored layout
1643  * \param[in] mirror_id the mirror with mirror_id to be extracted
1644  * \param[out] buf      store the layout excluding the extracted mirror,
1645  *                      caller free the buffer we allocated in this function
1646  * \param[out] buf_vic  store the extracted layout, caller free the buffer
1647  *                      we allocated in this function
1648  *
1649  * \retval      0 on success; < 0 if error happens
1650  */
1651 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1652                         struct lu_buf *buf, struct lu_buf *buf_vic)
1653 {
1654         struct lov_comp_md_v1 *comp_rem;
1655         struct lov_comp_md_v1 *comp_vic;
1656         struct lov_comp_md_entry_v1 *entry;
1657         struct lov_comp_md_entry_v1 *entry_rem;
1658         struct lov_comp_md_entry_v1 *entry_vic;
1659         __u16 mirror_cnt;
1660         __u16 comp_cnt, count = 0;
1661         int lmm_size, lmm_size_vic = 0;
1662         int i, j, k;
1663         int offset, offset_rem, offset_vic;
1664
1665         mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1666         /* comp_v1 should contains more than 1 mirror */
1667         if (mirror_cnt <= 1)
1668                 return -EINVAL;
1669         comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1670         lmm_size = le32_to_cpu(comp_v1->lcm_size);
1671
1672         for (i = 0; i < comp_cnt; i++) {
1673                 entry = &comp_v1->lcm_entries[i];
1674                 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1675                         count++;
1676                         lmm_size_vic += sizeof(*entry);
1677                         lmm_size_vic += le32_to_cpu(entry->lcme_size);
1678                 } else if (count > 0) {
1679                         /* find the specified mirror */
1680                         break;
1681                 }
1682         }
1683
1684         if (count == 0)
1685                 return -EINVAL;
1686
1687         lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1688         if (!buf->lb_buf)
1689                 return -ENOMEM;
1690
1691         lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1692         if (!buf_vic->lb_buf) {
1693                 lu_buf_free(buf);
1694                 return -ENOMEM;
1695         }
1696
1697         comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1698         comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1699
1700         memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1701         comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1702         comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1703         comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1704         if (!comp_rem->lcm_mirror_count)
1705                 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1706
1707         memset(comp_vic, 0, sizeof(*comp_v1));
1708         comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1709         comp_vic->lcm_mirror_count = 0;
1710         comp_vic->lcm_entry_count = cpu_to_le32(count);
1711         comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1712         comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1713         comp_vic->lcm_layout_gen = 0;
1714
1715         offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1716         offset_rem = sizeof(*comp_rem) +
1717                      sizeof(*entry_rem) * (comp_cnt - count);
1718         offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1719         for (i = j = k = 0; i < comp_cnt; i++) {
1720                 struct lov_mds_md *lmm, *lmm_dst;
1721                 bool vic = false;
1722
1723                 entry = &comp_v1->lcm_entries[i];
1724                 entry_vic = &comp_vic->lcm_entries[j];
1725                 entry_rem = &comp_rem->lcm_entries[k];
1726
1727                 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1728                         vic = true;
1729
1730                 /* copy component entry */
1731                 if (vic) {
1732                         memcpy(entry_vic, entry, sizeof(*entry));
1733                         entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1734                         entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1735                         j++;
1736                 } else {
1737                         memcpy(entry_rem, entry, sizeof(*entry));
1738                         entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1739                         k++;
1740                 }
1741
1742                 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1743                 if (vic)
1744                         lmm_dst = (struct lov_mds_md *)
1745                                         ((char *)comp_vic + offset_vic);
1746                 else
1747                         lmm_dst = (struct lov_mds_md *)
1748                                         ((char *)comp_rem + offset_rem);
1749
1750                 /* copy component entry blob */
1751                 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1752
1753                 /* blob offset advance */
1754                 offset += le32_to_cpu(entry->lcme_size);
1755                 if (vic)
1756                         offset_vic += le32_to_cpu(entry->lcme_size);
1757                 else
1758                         offset_rem += le32_to_cpu(entry->lcme_size);
1759         }
1760
1761         return 0;
1762 }
1763
1764 static int mdd_dom_data_truncate(const struct lu_env *env,
1765                                  struct mdd_device *mdd, struct mdd_object *mo);
1766
1767 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1768                            struct md_rejig_data *mrd)
1769 {
1770         struct mdd_device *mdd = mdo2mdd(md_obj);
1771         struct mdd_object *obj = md2mdd_obj(md_obj);
1772         struct mdd_object *vic = NULL;
1773         struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1774         struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1775         struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1776         struct lov_comp_md_v1 *lcm;
1777         struct thandle *handle;
1778         int rc;
1779         bool dom_stripe = false;
1780
1781         ENTRY;
1782
1783         /**
1784          * NULL @mrd_obj means mirror deleting, and use NULL vic to indicate
1785          * mirror deleting
1786          */
1787         if (mrd->mrd_obj)
1788                 vic = md2mdd_obj(mrd->mrd_obj);
1789
1790         handle = mdd_trans_create(env, mdd);
1791         if (IS_ERR(handle))
1792                 RETURN(PTR_ERR(handle));
1793
1794         /* get EA of mirrored file */
1795         memset(buf_save, 0, sizeof(*buf));
1796         rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
1797         if (rc < 0)
1798                 GOTO(stop, rc);
1799
1800         lcm = buf_save->lb_buf;
1801         if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1802                 GOTO(stop, rc = -EINVAL);
1803
1804         /**
1805          * Extract the mirror with specified mirror id, and store the splitted
1806          * mirror layout to the victim buffer.
1807          */
1808         memset(buf, 0, sizeof(*buf));
1809         memset(buf_vic, 0, sizeof(*buf_vic));
1810         rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1811         if (rc < 0)
1812                 GOTO(stop, rc);
1813         /**
1814          * @buf stores layout w/o the specified mirror, @buf_vic stores the
1815          * splitted mirror
1816          */
1817
1818         dom_stripe = mdd_lmm_dom_size(buf_vic->lb_buf) > 0;
1819
1820         if (vic) {
1821                 /**
1822                  * non delete mirror split
1823                  *
1824                  * declare obj set remaining layout in @buf, will set obj's
1825                  * in-memory layout
1826                  */
1827                 rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1828                                            LU_XATTR_SPLIT, handle);
1829                 if (rc)
1830                         GOTO(stop, rc);
1831
1832                 /* declare vic set splitted layout in @buf_vic */
1833                 rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic,
1834                                            XATTR_NAME_LOV, LU_XATTR_SPLIT,
1835                                            handle);
1836                 if (rc)
1837                         GOTO(stop, rc);
1838         } else {
1839                 /**
1840                  * declare delete mirror objects in @buf_vic, will change obj's
1841                  * in-memory layout
1842                  */
1843                 rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic,
1844                                            XATTR_NAME_LOV, LU_XATTR_PURGE,
1845                                            handle);
1846                 if (rc)
1847                         GOTO(stop, rc);
1848
1849                 /* declare obj set remaining layout in @buf */
1850                 rc = mdd_declare_xattr_set(env, mdd, obj, buf,
1851                                            XATTR_NAME_LOV, LU_XATTR_SPLIT,
1852                                            handle);
1853                 if (rc)
1854                         GOTO(stop, rc);
1855         }
1856
1857         rc = mdd_trans_start(env, mdd, handle);
1858         if (rc)
1859                 GOTO(stop, rc);
1860
1861         if (vic) {
1862                 /* don't use the same file to save the splitted mirror */
1863                 rc = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
1864                 if (rc == 0)
1865                         GOTO(stop, rc = -EPERM);
1866
1867                 if (rc > 0) {
1868                         mdd_write_lock(env, obj, DT_TGT_CHILD);
1869                         mdd_write_lock(env, vic, DT_TGT_CHILD);
1870                 } else {
1871                         mdd_write_lock(env, vic, DT_TGT_CHILD);
1872                         mdd_write_lock(env, obj, DT_TGT_CHILD);
1873                 }
1874         } else {
1875                 mdd_write_lock(env, obj, DT_TGT_CHILD);
1876         }
1877
1878         /* set obj's layout in @buf */
1879         rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1880                            handle);
1881         if (rc)
1882                 GOTO(unlock, rc);
1883
1884         if (vic) {
1885                 /* set vic's layout in @buf_vic */
1886                 rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV,
1887                                    LU_XATTR_CREATE, handle);
1888                 if (rc)
1889                         GOTO(out_restore, rc);
1890         } else {
1891                 /* delete mirror objects */
1892                 rc = mdo_xattr_set(env, obj, buf_vic, XATTR_NAME_LOV,
1893                                    LU_XATTR_PURGE, handle);
1894                 if (rc)
1895                         GOTO(out_restore, rc);
1896         }
1897
1898         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
1899                                       NULL);
1900         if (rc)
1901                 GOTO(out_restore, rc);
1902
1903         if (vic) {
1904                 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic,
1905                                               handle, NULL);
1906                 if (rc)
1907                         GOTO(out_restore, rc);
1908         }
1909
1910 out_restore:
1911         if (rc) {
1912                 /* restore obj's in-memory and on-disk layout */
1913                 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1914                                         LU_XATTR_REPLACE, handle);
1915                 if (rc2)
1916                         CERROR("%s: failed rollback "DFID
1917                                " layout: file state unknown: rc = %d\n",
1918                                mdd_obj_dev_name(obj),
1919                                PFID(mdd_object_fid(obj)), rc);
1920         }
1921
1922 unlock:
1923         mdd_write_unlock(env, obj);
1924         if (vic)
1925                 mdd_write_unlock(env, vic);
1926 stop:
1927         rc = mdd_trans_stop(env, mdd, rc, handle);
1928
1929         /* Truncate local DOM data if all went well */
1930         if (!rc && dom_stripe)
1931                 mdd_dom_data_truncate(env, mdd, obj);
1932
1933         lu_buf_free(buf_save);
1934         lu_buf_free(buf);
1935         lu_buf_free(buf_vic);
1936
1937         if (!rc)
1938                 (void) mdd_object_pfid_replace(env, obj);
1939
1940         return rc;
1941 }
1942
1943 static int mdd_layout_merge_allowed(const struct lu_env *env,
1944                                     struct md_object *target,
1945                                     struct md_object *victim)
1946 {
1947         struct mdd_object *o1 = md2mdd_obj(target);
1948
1949         /* cannot extend directory's LOVEA */
1950         if (S_ISDIR(mdd_object_type(o1))) {
1951                 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1952                        mdd_obj_dev_name(o1));
1953                 RETURN(-EISDIR);
1954         }
1955
1956         RETURN(0);
1957 }
1958
1959 /**
1960  * The caller should guarantee to update the object ctime
1961  * after xattr_set if needed.
1962  */
1963 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1964                          const struct lu_buf *buf, const char *name,
1965                          int fl)
1966 {
1967         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1968         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1969         struct mdd_device *mdd = mdo2mdd(obj);
1970         struct thandle *handle;
1971         enum changelog_rec_type  cl_type;
1972         enum changelog_rec_flags clf_flags = 0;
1973         int rc;
1974         ENTRY;
1975
1976         rc = mdd_la_get(env, mdd_obj, attr);
1977         if (rc)
1978                 RETURN(rc);
1979
1980         rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1981         if (rc)
1982                 RETURN(rc);
1983
1984         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1985             (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1986                 struct md_rejig_data *mrd = buf->lb_buf;
1987                 struct md_object *victim = mrd->mrd_obj;
1988
1989                 if (buf->lb_len != sizeof(*mrd))
1990                         RETURN(-EINVAL);
1991
1992
1993                 if (fl == LU_XATTR_MERGE) {
1994                         rc = mdd_layout_merge_allowed(env, obj, victim);
1995                         if (rc)
1996                                 RETURN(rc);
1997                         /* merge layout of victim as a mirror of obj's. */
1998                         rc = mdd_xattr_merge(env, obj, victim);
1999                 } else {
2000                         rc = mdd_xattr_split(env, obj, mrd);
2001                 }
2002                 RETURN(rc);
2003         }
2004
2005         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
2006             strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
2007                 struct posix_acl *acl;
2008
2009                 /* user may set empty ACL, which should be treated as removing
2010                  * ACL. */
2011                 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
2012                                            buf->lb_len);
2013                 if (IS_ERR(acl))
2014                         RETURN(PTR_ERR(acl));
2015                 if (acl == NULL) {
2016                         rc = mdd_xattr_del(env, obj, name);
2017                         RETURN(rc);
2018                 }
2019                 posix_acl_release(acl);
2020         }
2021
2022         if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
2023                 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
2024                 RETURN(rc);
2025         }
2026
2027         handle = mdd_trans_create(env, mdd);
2028         if (IS_ERR(handle))
2029                 RETURN(PTR_ERR(handle));
2030
2031         rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
2032         if (rc)
2033                 GOTO(stop, rc);
2034
2035         rc = mdd_trans_start(env, mdd, handle);
2036         if (rc)
2037                 GOTO(stop, rc);
2038
2039         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
2040
2041         if (strcmp(XATTR_NAME_HSM, name) == 0) {
2042                 rc = mdd_hsm_update_locked(env, obj, buf, handle, &clf_flags);
2043                 if (rc) {
2044                         mdd_write_unlock(env, mdd_obj);
2045                         GOTO(stop, rc);
2046                 }
2047         }
2048
2049         rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
2050         mdd_write_unlock(env, mdd_obj);
2051         if (rc)
2052                 GOTO(stop, rc);
2053
2054         cl_type = mdd_xattr_changelog_type(env, mdd, name);
2055         if (cl_type < 0)
2056                 GOTO(stop, rc = 0);
2057
2058         rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, clf_flags,
2059                                             mdd_obj, name, handle);
2060
2061         EXIT;
2062 stop:
2063         return mdd_trans_stop(env, mdd, rc, handle);
2064 }
2065
2066 static int mdd_declare_xattr_del(const struct lu_env *env,
2067                                  struct mdd_device *mdd,
2068                                  struct mdd_object *obj,
2069                                  const char *name,
2070                                  struct thandle *handle)
2071 {
2072         enum changelog_rec_type type;
2073         int rc;
2074
2075         rc = mdo_declare_xattr_del(env, obj, name, handle);
2076         if (rc)
2077                 return rc;
2078
2079         type = mdd_xattr_changelog_type(env, mdd, name);
2080         if (type < 0)
2081                 return 0; /* no changelog to store */
2082
2083         return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
2084 }
2085
2086 /**
2087  * The caller should guarantee to update the object ctime
2088  * after xattr_set if needed.
2089  */
2090 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
2091                          const char *name)
2092 {
2093         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2094         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
2095         struct mdd_device *mdd = mdo2mdd(obj);
2096         struct thandle *handle;
2097         int rc;
2098         ENTRY;
2099
2100         rc = mdd_la_get(env, mdd_obj, attr);
2101         if (rc)
2102                 RETURN(rc);
2103
2104         rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
2105         if (rc)
2106                 RETURN(rc);
2107
2108         handle = mdd_trans_create(env, mdd);
2109         if (IS_ERR(handle))
2110                 RETURN(PTR_ERR(handle));
2111
2112         rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
2113         if (rc)
2114                 GOTO(stop, rc);
2115
2116         rc = mdd_trans_start(env, mdd, handle);
2117         if (rc)
2118                 GOTO(stop, rc);
2119
2120         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
2121         rc = mdo_xattr_del(env, mdd_obj, name, handle);
2122         mdd_write_unlock(env, mdd_obj);
2123         if (rc)
2124                 GOTO(stop, rc);
2125
2126         if (mdd_xattr_changelog_type(env, mdd, name) < 0)
2127                 GOTO(stop, rc = 0);
2128
2129         rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
2130                                             name, handle);
2131
2132         EXIT;
2133 stop:
2134         return mdd_trans_stop(env, mdd, rc, handle);
2135 }
2136
2137 /*
2138  * read lov/lmv EA of an object
2139  * return the lov/lmv EA in an allocated lu_buf
2140  */
2141 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
2142                    struct lu_buf *lmm_buf, const char *name)
2143 {
2144         struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
2145         int rc;
2146
2147         ENTRY;
2148
2149         if (buf->lb_buf == NULL) {
2150                 buf = lu_buf_check_and_alloc(buf, 4096);
2151                 if (buf->lb_buf == NULL)
2152                         RETURN(-ENOMEM);
2153         }
2154
2155 repeat:
2156         rc = mdo_xattr_get(env, obj, buf, name);
2157         if (rc == -ERANGE) {
2158                 /* mti_big_buf is allocated but is too small
2159                  * we need to increase it */
2160                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
2161                                              buf->lb_len * 2);
2162                 if (buf->lb_buf == NULL)
2163                         RETURN(-ENOMEM);
2164                 goto repeat;
2165         } else if (rc < 0) {
2166                 RETURN(rc);
2167         } else if (rc == 0) {
2168                 RETURN(-ENODATA);
2169         }
2170
2171         lu_buf_alloc(lmm_buf, rc);
2172         if (lmm_buf->lb_buf == NULL)
2173                 RETURN(-ENOMEM);
2174
2175         /*
2176          * we don't use lmm_buf directly, because we don't know xattr size, so
2177          * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
2178          */
2179         memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
2180
2181         RETURN(0);
2182 }
2183
2184 static int mdd_xattr_hsm_replace(const struct lu_env *env,
2185                                  struct mdd_object *o, struct lu_buf *buf,
2186                                  struct thandle *handle)
2187 {
2188         struct hsm_attrs *attrs;
2189         enum hsm_states hsm_flags;
2190         enum changelog_rec_flags clf_flags = 0;
2191         int rc;
2192         ENTRY;
2193
2194         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
2195                            handle);
2196         if (rc != 0)
2197                 RETURN(rc);
2198
2199         attrs = buf->lb_buf;
2200         hsm_flags = le32_to_cpu(attrs->hsm_flags);
2201         if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
2202                 RETURN(0);
2203
2204         /* Add a changelog record for release. */
2205         hsm_set_cl_event(&clf_flags, HE_RELEASE);
2206         rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
2207                                       clf_flags, o, handle, NULL);
2208         RETURN(rc);
2209 }
2210
2211 /*
2212  *  check if layout swapping between 2 objects is allowed
2213  *  the rules are:
2214  *  - only normal FIDs or non-system IGIFs
2215  *  - same type of objects
2216  *  - same owner/group (so quotas are still valid) unless this is from HSM
2217  *    release.
2218  */
2219 static int mdd_layout_swap_allowed(const struct lu_env *env,
2220                                    struct mdd_object *o1,
2221                                    const struct lu_attr *attr1,
2222                                    struct mdd_object *o2,
2223                                    const struct lu_attr *attr2,
2224                                    __u64 flags)
2225 {
2226         const struct lu_fid *fid1, *fid2;
2227         ENTRY;
2228
2229         fid1 = mdd_object_fid(o1);
2230         fid2 = mdd_object_fid(o2);
2231
2232         if (!fid_is_norm(fid1) &&
2233             (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
2234                 RETURN(-EBADF);
2235
2236         if (!fid_is_norm(fid2) &&
2237             (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
2238                 RETURN(-EBADF);
2239
2240         if (mdd_object_type(o1) != mdd_object_type(o2)) {
2241                 if (S_ISDIR(mdd_object_type(o1)))
2242                         RETURN(-ENOTDIR);
2243                 if (S_ISREG(mdd_object_type(o1)))
2244                         RETURN(-EISDIR);
2245                 RETURN(-EBADF);
2246         }
2247
2248         if (flags & SWAP_LAYOUTS_MDS_HSM)
2249                 RETURN(0);
2250
2251         if ((attr1->la_uid != attr2->la_uid) ||
2252             (attr1->la_gid != attr2->la_gid))
2253                 RETURN(-EPERM);
2254
2255         RETURN(0);
2256 }
2257
2258 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
2259  *     look into the layout in MDD layer. */
2260 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
2261 {
2262         struct lov_comp_md_v1   *comp_v1;
2263         struct lov_mds_md       *v1;
2264         int                      i, ent_count;
2265         __u32                    off;
2266
2267         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2268                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2269                 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
2270
2271                 if (ent_count == 0)
2272                         return -EINVAL;
2273
2274                 if (get) {
2275                         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
2276                         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
2277                         *oi = v1->lmm_oi;
2278                 } else {
2279                         for (i = 0; i < le32_to_cpu(ent_count); i++) {
2280                                 off = le32_to_cpu(comp_v1->lcm_entries[i].
2281                                                 lcme_offset);
2282                                 v1 = (struct lov_mds_md *)((char *)comp_v1 +
2283                                                 off);
2284                                 v1->lmm_oi = *oi;
2285                         }
2286                 }
2287         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2288                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2289                 if (get)
2290                         *oi = lmm->lmm_oi;
2291                 else
2292                         lmm->lmm_oi = *oi;
2293         } else {
2294                 return -EINVAL;
2295         }
2296         return 0;
2297 }
2298
2299 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2300 {
2301         return mdd_lmm_oi(lmm, oi, true);
2302 }
2303
2304 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2305 {
2306         return mdd_lmm_oi(lmm, oi, false);
2307 }
2308
2309 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
2310 {
2311         struct lov_comp_md_v1 *comp_v1;
2312
2313         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2314                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2315                 if (get)
2316                         *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
2317                 else
2318                         comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
2319         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2320                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2321                 __u16 tmp_gen = *gen;
2322                 if (get)
2323                         *gen = le16_to_cpu(lmm->lmm_layout_gen);
2324                 else
2325                         lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
2326         } else {
2327                 return -EINVAL;
2328         }
2329         return 0;
2330 }
2331
2332 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2333 {
2334         return mdd_lmm_gen(lmm, gen, true);
2335 }
2336
2337 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2338 {
2339         return mdd_lmm_gen(lmm, gen, false);
2340 }
2341
2342 static int mdd_dom_data_truncate(const struct lu_env *env,
2343                                  struct mdd_device *mdd, struct mdd_object *mo)
2344 {
2345         struct thandle *th;
2346         struct dt_object *dom;
2347         int rc;
2348
2349         dom = dt_object_locate(mdd_object_child(mo), mdd->mdd_bottom);
2350         if (!dom)
2351                 GOTO(out, rc = -ENODATA);
2352
2353         th = dt_trans_create(env, mdd->mdd_bottom);
2354         if (IS_ERR(th))
2355                 GOTO(out, rc = PTR_ERR(th));
2356
2357         rc = dt_declare_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2358         if (rc)
2359                 GOTO(stop, rc);
2360
2361         rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
2362         if (rc != 0)
2363                 GOTO(stop, rc);
2364
2365         rc = dt_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2366 stop:
2367         dt_trans_stop(env, mdd->mdd_bottom, th);
2368 out:
2369         /* Ignore failure but report the error */
2370         if (rc)
2371                 CERROR("%s: can't truncate DOM inode "DFID" data: rc = %d\n",
2372                        mdd_obj_dev_name(mo), PFID(mdd_object_fid(mo)), rc);
2373         return rc;
2374 }
2375
2376 /**
2377  * swap layouts between 2 lustre objects
2378  */
2379 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2380                             struct md_object *obj2, __u64 flags)
2381 {
2382         struct mdd_thread_info *info = mdd_env_info(env);
2383         struct mdd_object *fst_o = md2mdd_obj(obj1);
2384         struct mdd_object *snd_o = md2mdd_obj(obj2);
2385         struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2386         struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2387         struct mdd_device *mdd = mdo2mdd(obj1);
2388         struct lov_mds_md *fst_lmm, *snd_lmm;
2389         struct lu_buf *fst_buf = &info->mti_buf[0];
2390         struct lu_buf *snd_buf = &info->mti_buf[1];
2391         struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2392         struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2393         struct ost_id *saved_oi = NULL;
2394         struct thandle *handle;
2395         struct mdd_object *dom_o = NULL;
2396         __u64 domsize_dom, domsize_vlt;
2397         __u32 fst_gen, snd_gen, saved_gen;
2398         int fst_fl;
2399         int rc, rc2;
2400
2401         ENTRY;
2402
2403         BUILD_BUG_ON(ARRAY_SIZE(info->mti_buf) < 4);
2404         memset(info->mti_buf, 0, sizeof(info->mti_buf));
2405
2406         /* we have to sort the 2 obj, so locking will always
2407          * be in the same order, even in case of 2 concurrent swaps */
2408         rc = lu_fid_cmp(mdd_object_fid(fst_o), mdd_object_fid(snd_o));
2409         if (rc == 0) /* same fid ? */
2410                 RETURN(-EPERM);
2411
2412         if (rc < 0)
2413                 swap(fst_o, snd_o);
2414
2415         rc = mdd_la_get(env, fst_o, fst_la);
2416         if (rc != 0)
2417                 RETURN(rc);
2418
2419         rc = mdd_la_get(env, snd_o, snd_la);
2420         if (rc != 0)
2421                 RETURN(rc);
2422
2423         /* check if layout swapping is allowed */
2424         rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2425         if (rc != 0)
2426                 RETURN(rc);
2427
2428         handle = mdd_trans_create(env, mdd);
2429         if (IS_ERR(handle))
2430                 RETURN(PTR_ERR(handle));
2431
2432         /* objects are already sorted */
2433         mdd_write_lock(env, fst_o, DT_TGT_CHILD);
2434         mdd_write_lock(env, snd_o, DT_TGT_CHILD);
2435
2436         rc = mdd_stripe_get(env, fst_o, fst_buf, XATTR_NAME_LOV);
2437         if (rc < 0 && rc != -ENODATA)
2438                 GOTO(stop, rc);
2439
2440         rc = mdd_stripe_get(env, snd_o, snd_buf, XATTR_NAME_LOV);
2441         if (rc < 0 && rc != -ENODATA)
2442                 GOTO(stop, rc);
2443
2444         /* check if file has DoM. DoM file can be migrated only to another
2445          * DoM layout with the same DoM component size or to an non-DOM
2446          * layout. After migration to OSTs layout, local MDT inode data
2447          * should be truncated.
2448          * Objects are sorted by FIDs, considering that original file's FID
2449          * is always smaller the snd_o is always original file we are migrating
2450          * from.
2451          */
2452         domsize_dom = mdd_lmm_dom_size(snd_buf->lb_buf);
2453         domsize_vlt = mdd_lmm_dom_size(fst_buf->lb_buf);
2454
2455         /* Only migration is supported for DoM files, not 'swap_layouts' so
2456          * target file must be volatile and orphan.
2457          */
2458         if (fst_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2459                 dom_o = domsize_dom ? snd_o : NULL;
2460         } else if (snd_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2461                 swap(domsize_dom, domsize_vlt);
2462                 dom_o = domsize_dom ? fst_o : NULL;
2463         } else if (domsize_dom > 0 || domsize_vlt > 0) {
2464                 /* 'lfs swap_layouts' case, neither file should have DoM */
2465                 rc = -EOPNOTSUPP;
2466                 CDEBUG(D_LAYOUT, "cannot swap layouts with DOM component, "
2467                        "use migration instead: rc = %d\n", rc);
2468                 GOTO(stop, rc);
2469         }
2470
2471         if (domsize_vlt > 0 && domsize_dom == 0) {
2472                 rc = -EOPNOTSUPP;
2473                 CDEBUG(D_LAYOUT,
2474                        "%s: cannot swap "DFID" layout: OST to DOM migration not supported: rc = %d\n",
2475                        mdd_obj_dev_name(snd_o),
2476                        PFID(mdd_object_fid(snd_o)), rc);
2477                 GOTO(stop, rc);
2478         } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
2479                 rc = -EOPNOTSUPP;
2480                 CDEBUG(D_LAYOUT,
2481                        "%s: cannot swap "DFID" layout: new layout must have same DoM component size: rc = %d\n",
2482                        mdd_obj_dev_name(fst_o),
2483                        PFID(mdd_object_fid(fst_o)), rc);
2484                 GOTO(stop, rc);
2485         } else if (domsize_vlt > 0) {
2486                 /* Migration with the same DOM component size, no need to
2487                  * truncate local data, it is still being used */
2488                 dom_o = NULL;
2489         }
2490
2491         /* swapping 2 non existant layouts is a success */
2492         if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2493                 GOTO(stop, rc = 0);
2494
2495         /* to help inode migration between MDT, it is better to
2496          * start by the no layout file (if one), so we order the swap */
2497         if (snd_buf->lb_buf == NULL) {
2498                 swap(fst_o, snd_o);
2499                 swap(fst_buf, snd_buf);
2500         }
2501
2502         fst_gen = snd_gen = 0;
2503         /* lmm and generation layout initialization */
2504         if (fst_buf->lb_buf != NULL) {
2505                 fst_lmm = fst_buf->lb_buf;
2506                 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2507                 fst_fl  = LU_XATTR_REPLACE;
2508         } else {
2509                 fst_lmm = NULL;
2510                 fst_gen = 0;
2511                 fst_fl  = LU_XATTR_CREATE;
2512         }
2513
2514         snd_lmm = snd_buf->lb_buf;
2515         mdd_get_lmm_gen(snd_lmm, &snd_gen);
2516
2517         saved_gen = fst_gen;
2518         /* increase the generation layout numbers */
2519         snd_gen++;
2520         fst_gen++;
2521
2522         /*
2523          * XXX The layout generation is used to generate component IDs for
2524          *     the composite file, we have to do some special tweaks to make
2525          *     sure the layout generation is always adequate for that job.
2526          */
2527
2528         /* Skip invalid generation number for composite layout */
2529         if ((snd_gen & LCME_ID_MASK) == 0)
2530                 snd_gen++;
2531         if ((fst_gen & LCME_ID_MASK) == 0)
2532                 fst_gen++;
2533         /* Make sure the generation is greater than all the component IDs */
2534         if (fst_gen < snd_gen)
2535                 fst_gen = snd_gen;
2536         else if (fst_gen > snd_gen)
2537                 snd_gen = fst_gen;
2538
2539         /* set the file specific informations in lmm */
2540         if (fst_lmm != NULL) {
2541                 struct ost_id temp_oi;
2542
2543                 saved_oi = &info->mti_oa.o_oi;
2544                 mdd_get_lmm_oi(fst_lmm, saved_oi);
2545                 mdd_get_lmm_oi(snd_lmm, &temp_oi);
2546                 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2547                 mdd_set_lmm_oi(fst_lmm, &temp_oi);
2548                 mdd_set_lmm_oi(snd_lmm, saved_oi);
2549         } else {
2550                 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2551                     cpu_to_le32(LOV_MAGIC_MAGIC))
2552                         snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2553                 else
2554                         GOTO(stop, rc = -EPROTO);
2555         }
2556         mdd_set_lmm_gen(snd_lmm, &fst_gen);
2557
2558         /* Prepare HSM attribute if it's required */
2559         if (flags & SWAP_LAYOUTS_MDS_HSM) {
2560                 const int buflen = sizeof(struct hsm_attrs);
2561
2562                 lu_buf_alloc(fst_hsm_buf, buflen);
2563                 lu_buf_alloc(snd_hsm_buf, buflen);
2564                 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2565                         GOTO(stop, rc = -ENOMEM);
2566
2567                 /* Read HSM attribute */
2568                 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2569                 if (rc < 0)
2570                         GOTO(stop, rc);
2571
2572                 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2573                 if (rc < 0)
2574                         GOTO(stop, rc);
2575
2576                 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2577                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
2578                                            handle);
2579                 if (rc < 0)
2580                         GOTO(stop, rc);
2581
2582                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2583                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
2584                                            handle);
2585                 if (rc < 0)
2586                         GOTO(stop, rc);
2587         }
2588
2589         /* prepare transaction */
2590         rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2591                                    fst_fl, handle);
2592         if (rc != 0)
2593                 GOTO(stop, rc);
2594
2595         if (fst_buf->lb_buf != NULL)
2596                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2597                                            XATTR_NAME_LOV, LU_XATTR_REPLACE,
2598                                            handle);
2599         else
2600                 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2601                                            handle);
2602         if (rc != 0)
2603                 GOTO(stop, rc);
2604
2605         rc = mdd_trans_start(env, mdd, handle);
2606         if (rc != 0)
2607                 GOTO(stop, rc);
2608
2609         if (flags & SWAP_LAYOUTS_MDS_HSM) {
2610                 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2611                 if (rc < 0)
2612                         GOTO(stop, rc);
2613
2614                 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2615                 if (rc < 0) {
2616                         rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2617                                                     handle);
2618                         if (rc2 < 0)
2619                                 CERROR("%s: HSM error restoring "DFID": rc = %d/%d\n",
2620                                        mdd_obj_dev_name(fst_o),
2621                                        PFID(mdd_object_fid(fst_o)), rc, rc2);
2622                         GOTO(stop, rc);
2623                 }
2624         }
2625
2626         rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2627         if (rc != 0)
2628                 GOTO(stop, rc);
2629
2630         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2631                 rc = -EOPNOTSUPP;
2632         } else {
2633                 if (fst_buf->lb_buf != NULL)
2634                         rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2635                                            LU_XATTR_REPLACE, handle);
2636                 else
2637                         rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2638         }
2639         if (rc != 0)
2640                 GOTO(out_restore, rc);
2641
2642         /* Issue one changelog record per file */
2643         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle,
2644                                       NULL);
2645         if (rc)
2646                 GOTO(stop, rc);
2647
2648         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle,
2649                                       NULL);
2650         if (rc)
2651                 GOTO(stop, rc);
2652         EXIT;
2653
2654 out_restore:
2655         if (rc != 0) {
2656                 int steps = 0;
2657
2658                 /* failure on second file, but first was done, so we have
2659                  * to roll back first. */
2660                 if (fst_buf->lb_buf != NULL) {
2661                         mdd_set_lmm_oi(fst_lmm, saved_oi);
2662                         mdd_set_lmm_gen(fst_lmm, &saved_gen);
2663                         rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2664                                             LU_XATTR_REPLACE, handle);
2665                 } else {
2666                         rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2667                 }
2668                 if (rc2 < 0)
2669                         goto do_lbug;
2670
2671                 ++steps;
2672                 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2673                 if (rc2 < 0)
2674                         goto do_lbug;
2675
2676                 ++steps;
2677                 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2678
2679         do_lbug:
2680                 if (rc2 < 0) {
2681                         /* very bad day */
2682                         CERROR("%s: unable to roll back layout swap of "DFID" and "DFID", steps: %d: rc = %d/%d\n",
2683                                mdd_obj_dev_name(fst_o),
2684                                PFID(mdd_object_fid(snd_o)),
2685                                PFID(mdd_object_fid(fst_o)),
2686                                rc, rc2, steps);
2687                         /* a solution to avoid journal commit is to panic,
2688                          * but it has strong consequences so we use LBUG to
2689                          * allow sysdamin to choose to panic or not
2690                          */
2691                         LBUG();
2692                 }
2693         }
2694
2695 stop:
2696         rc = mdd_trans_stop(env, mdd, rc, handle);
2697
2698         /* Truncate local DOM data if all went well */
2699         if (!rc && dom_o)
2700                 mdd_dom_data_truncate(env, mdd, dom_o);
2701
2702         mdd_write_unlock(env, snd_o);
2703         mdd_write_unlock(env, fst_o);
2704
2705         lu_buf_free(fst_buf);
2706         lu_buf_free(snd_buf);
2707         lu_buf_free(fst_hsm_buf);
2708         lu_buf_free(snd_hsm_buf);
2709
2710         if (!rc) {
2711                 (void) mdd_object_pfid_replace(env, fst_o);
2712                 (void) mdd_object_pfid_replace(env, snd_o);
2713         }
2714         return rc;
2715 }
2716
2717 static int mdd_declare_layout_change(const struct lu_env *env,
2718                                      struct mdd_device *mdd,
2719                                      struct mdd_object *obj,
2720                                      struct md_layout_change *mlc,
2721                                      struct thandle *handle)
2722 {
2723         int rc;
2724
2725         rc = mdo_declare_layout_change(env, obj, mlc, handle);
2726         if (rc)
2727                 return rc;
2728
2729         return mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2730                                            handle);
2731 }
2732
2733 /* For PFL, this is used to instantiate necessary component objects. */
2734 static int
2735 mdd_layout_instantiate_component(const struct lu_env *env,
2736                 struct mdd_object *obj, struct md_layout_change *mlc,
2737                 struct thandle *handle)
2738 {
2739         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2740         int rc;
2741         ENTRY;
2742
2743         if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2744                 RETURN(-ENOTSUPP);
2745
2746         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2747         /**
2748          * It's possible that another layout write intent has already
2749          * instantiated our objects, so a -EALREADY returned, and we need to
2750          * do nothing.
2751          */
2752         if (rc)
2753                 RETURN(rc == -EALREADY ? 0 : rc);
2754
2755         rc = mdd_trans_start(env, mdd, handle);
2756         if (rc)
2757                 RETURN(rc);
2758
2759         mdd_write_lock(env, obj, DT_TGT_CHILD);
2760         rc = mdo_layout_change(env, obj, mlc, handle);
2761         mdd_write_unlock(env, obj);
2762         if (rc)
2763                 RETURN(rc);
2764
2765         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
2766                                       NULL);
2767         RETURN(rc);
2768 }
2769
2770 /**
2771  * Change the FLR layout from RDONLY to WRITE_PENDING.
2772  *
2773  * It picks the primary mirror, and bumps the layout version, and set
2774  * layout version xattr to OST objects in a sync tx. In order to facilitate
2775  * the handling of phantom writers from evicted clients, the clients carry
2776  * layout version of the file with write RPC, so that the OSTs can verify
2777  * if the write RPCs are legitimate, meaning not from evicted clients.
2778  */
2779 static int
2780 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2781                          struct md_layout_change *mlc, struct thandle *handle)
2782 {
2783         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2784         struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2785         struct lustre_som_attrs *som = &mlc->mlc_som;
2786         int fl = 0;
2787         int rc;
2788         ENTRY;
2789
2790         /* Verify acceptable operations */
2791         switch (mlc->mlc_opc) {
2792         case MD_LAYOUT_WRITE:
2793         case MD_LAYOUT_RESYNC:
2794                 /* these are legal operations - this represents the case that
2795                  * a few mirrors were missed in the last resync. */
2796                 break;
2797         case MD_LAYOUT_RESYNC_DONE:
2798         default:
2799                 RETURN(0);
2800         }
2801
2802         som_buf->lb_buf = som;
2803         som_buf->lb_len = sizeof(*som);
2804         rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
2805         if (rc < 0 && rc != -ENODATA)
2806                 RETURN(rc);
2807
2808         if (rc > 0) {
2809                 lustre_som_swab(som);
2810                 if (som->lsa_valid & SOM_FL_STRICT)
2811                         fl = LU_XATTR_REPLACE;
2812         }
2813
2814         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2815         if (rc)
2816                 GOTO(out, rc);
2817
2818         if (fl) {
2819                 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2820                                            XATTR_NAME_SOM, fl, handle);
2821                 if (rc)
2822                         GOTO(out, rc);
2823         }
2824
2825         /* record a changelog for data mover to consume */
2826         rc = mdd_declare_changelog_store(env, mdd, CL_FLRW, NULL, NULL, handle);
2827         if (rc)
2828                 GOTO(out, rc);
2829
2830         rc = mdd_trans_start(env, mdd, handle);
2831         if (rc)
2832                 GOTO(out, rc);
2833
2834         /* it needs a sync tx to make FLR to work properly */
2835         handle->th_sync = 1;
2836
2837         mdd_write_lock(env, obj, DT_TGT_CHILD);
2838         rc = mdo_layout_change(env, obj, mlc, handle);
2839         if (!rc && fl) {
2840                 /* SOM state transition from STRICT to STALE */
2841                 som->lsa_valid = SOM_FL_STALE;
2842                 lustre_som_swab(som);
2843                 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2844                                    fl, handle);
2845         }
2846         mdd_write_unlock(env, obj);
2847         if (rc)
2848                 GOTO(out, rc);
2849
2850         rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle, NULL);
2851         if (rc)
2852                 GOTO(out, rc);
2853
2854         EXIT;
2855
2856 out:
2857         return rc;
2858 }
2859
2860 /**
2861  * Handle mirrored file state transition when it's in WRITE_PENDING.
2862  *
2863  * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2864  * the file is in WRITE_PENDING state. If everything goes fine, the file's
2865  * layout version will be increased, and the file's state will be changed to
2866  * SYNC_PENDING.
2867  */
2868 static int
2869 mdd_layout_update_write_pending(const struct lu_env *env,
2870                 struct mdd_object *obj, struct md_layout_change *mlc,
2871                 struct thandle *handle)
2872 {
2873         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2874         int rc;
2875         ENTRY;
2876
2877         switch (mlc->mlc_opc) {
2878         case MD_LAYOUT_RESYNC:
2879                 /* Upon receiving the resync request, it should
2880                  * instantiate all stale components right away to get ready
2881                  * for mirror copy. In order to avoid layout version change,
2882                  * client should avoid sending LAYOUT_WRITE request at the
2883                  * resync state. */
2884                 break;
2885         case MD_LAYOUT_WRITE:
2886                 /* legal race for concurrent write, the file state has been
2887                  * changed by another client. */
2888                 break;
2889         default:
2890                 RETURN(-EBUSY);
2891         }
2892
2893         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2894         if (rc)
2895                 GOTO(out, rc);
2896
2897         rc = mdd_trans_start(env, mdd, handle);
2898         if (rc)
2899                 GOTO(out, rc);
2900
2901         /* it needs a sync tx to make FLR to work properly */
2902         handle->th_sync = 1;
2903
2904         mdd_write_lock(env, obj, DT_TGT_CHILD);
2905         rc = mdo_layout_change(env, obj, mlc, handle);
2906         mdd_write_unlock(env, obj);
2907         if (rc)
2908                 GOTO(out, rc);
2909
2910         EXIT;
2911
2912 out:
2913         return rc;
2914 }
2915
2916 /**
2917  * Handle the requests when a FLR file's state is in SYNC_PENDING.
2918  *
2919  * Only concurrent write and sync complete requests are possible when the
2920  * file is in SYNC_PENDING. For the latter request, it will pass in the
2921  * mirrors that have been synchronized, then the stale bit will be cleared
2922  * to make the file's state turn into RDONLY.
2923  * For concurrent write reqeust, it just needs to change the file's state
2924  * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2925  * version because the version will be increased in the transition to
2926  * SYNC_PENDING later so that it can deny the write request from potential
2927  * evicted SYNC clients. */
2928 static int
2929 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2930                 struct md_layout_change *mlc, struct thandle *handle)
2931 {
2932         struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2933         struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2934         int fl = 0;
2935         int rc;
2936         ENTRY;
2937
2938         /* operation validation */
2939         switch (mlc->mlc_opc) {
2940         case MD_LAYOUT_RESYNC_DONE:
2941                 /* resync complete. */
2942         case MD_LAYOUT_WRITE:
2943                 /* concurrent write. */
2944                 break;
2945         case MD_LAYOUT_RESYNC:
2946                 /* resync again, most likely the previous run failed.
2947                  * no-op if it's already in SYNC_PENDING state */
2948                 RETURN(0);
2949         default:
2950                 RETURN(-EBUSY);
2951         }
2952
2953         if (mlc->mlc_som.lsa_valid & SOM_FL_STRICT) {
2954                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2955                 if (rc < 0 && rc != -ENODATA)
2956                         RETURN(rc);
2957
2958                 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2959                 lustre_som_swab(&mlc->mlc_som);
2960                 som_buf->lb_buf = &mlc->mlc_som;
2961                 som_buf->lb_len = sizeof(mlc->mlc_som);
2962         }
2963
2964         rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2965         if (rc)
2966                 GOTO(out, rc);
2967
2968         /* record a changelog for the completion of resync */
2969         rc = mdd_declare_changelog_store(env, mdd, CL_RESYNC, NULL, NULL,
2970                                          handle);
2971         if (rc)
2972                 GOTO(out, rc);
2973
2974         /* RESYNC_DONE has piggybacked size and blocks */
2975         if (fl) {
2976                 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2977                                            XATTR_NAME_SOM, fl, handle);
2978                 if (rc)
2979                         GOTO(out, rc);
2980         }
2981
2982         rc = mdd_trans_start(env, mdd, handle);
2983         if (rc)
2984                 GOTO(out, rc);
2985
2986         /* it needs a sync tx to make FLR to work properly */
2987         handle->th_sync = 1;
2988
2989         rc = mdo_layout_change(env, obj, mlc, handle);
2990         if (rc)
2991                 GOTO(out, rc);
2992
2993         if (fl) {
2994                 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2995                                    fl, handle);
2996                 if (rc)
2997                         GOTO(out, rc);
2998         }
2999
3000         rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle,
3001                                       NULL);
3002         if (rc)
3003                 GOTO(out, rc);
3004         EXIT;
3005 out:
3006         return rc;
3007 }
3008
3009 /**
3010  * Layout change callback for object.
3011  *
3012  * This is only used by FLR for now. In the future, it can be exteneded to
3013  * handle all layout change.
3014  */
3015 static int
3016 mdd_layout_change(const struct lu_env *env, struct md_object *o,
3017                   struct md_layout_change *mlc)
3018 {
3019         struct mdd_object       *obj = md2mdd_obj(o);
3020         struct mdd_device       *mdd = mdd_obj2mdd_dev(obj);
3021         struct lu_buf           *buf = mdd_buf_get(env, NULL, 0);
3022         struct lov_comp_md_v1   *lcm;
3023         struct thandle          *handle;
3024         int flr_state;
3025         int rc;
3026
3027         ENTRY;
3028
3029         if (S_ISDIR(mdd_object_type(obj))) {
3030                 switch (mlc->mlc_opc) {
3031                 case MD_LAYOUT_SHRINK:
3032                         rc = mdd_dir_layout_shrink(env, o, mlc);
3033                         break;
3034                 case MD_LAYOUT_SPLIT:
3035                         rc = mdd_dir_layout_split(env, o, mlc);
3036                         break;
3037                 default:
3038                         LBUG();
3039                 }
3040
3041                 RETURN(rc);
3042         }
3043
3044         /* Verify acceptable operations */
3045         switch (mlc->mlc_opc) {
3046         case MD_LAYOUT_WRITE:
3047         case MD_LAYOUT_RESYNC:
3048         case MD_LAYOUT_RESYNC_DONE:
3049                 break;
3050         default:
3051                 RETURN(-ENOTSUPP);
3052         }
3053
3054         handle = mdd_trans_create(env, mdd);
3055         if (IS_ERR(handle))
3056                 RETURN(PTR_ERR(handle));
3057
3058         rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
3059         if (rc < 0) {
3060                 if (rc == -ENODATA)
3061                         rc = -EINVAL;
3062                 GOTO(out, rc);
3063         }
3064
3065         /* analyze the layout to make sure it's a FLR file */
3066         lcm = buf->lb_buf;
3067         if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3068                 GOTO(out, rc = -EINVAL);
3069
3070         flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
3071
3072         /* please refer to HLD of FLR for state transition */
3073         switch (flr_state) {
3074         case LCM_FL_NONE:
3075                 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
3076                 break;
3077         case LCM_FL_WRITE_PENDING:
3078                 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
3079                 break;
3080         case LCM_FL_RDONLY:
3081                 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
3082                 break;
3083         case LCM_FL_SYNC_PENDING:
3084                 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
3085                 break;
3086         default:
3087                 rc = 0;
3088                 break;
3089         }
3090         EXIT;
3091
3092 out:
3093         mdd_trans_stop(env, mdd, rc, handle);
3094         lu_buf_free(buf);
3095         return rc;
3096 }
3097
3098 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
3099                           struct mdd_object *child, const struct lu_attr *attr,
3100                           const struct md_op_spec *spec,
3101                           struct dt_allocation_hint *hint)
3102 {
3103         struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
3104         struct mdd_device *mdd = mdd_obj2mdd_dev(child);
3105         struct dt_object *nc = mdd_object_child(child);
3106
3107         memset(hint, 0, sizeof(*hint));
3108
3109         /* For striped directory, give striping EA to lod_ah_init, which will
3110          * decide the stripe_offset and stripe count by it. */
3111         if (S_ISDIR(attr->la_mode) &&
3112             unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
3113                 hint->dah_eadata = spec->u.sp_ea.eadata;
3114                 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
3115         } else {
3116                 hint->dah_eadata = NULL;
3117                 hint->dah_eadata_len = 0;
3118                 if (spec->sp_cr_flags & MDS_OPEN_APPEND) {
3119                         if (mdd->mdd_append_stripe_count != 0 ||
3120                             mdd->mdd_append_pool[0])
3121                                 CDEBUG(D_INFO,
3122                                        "using O_APPEND file striping\n");
3123                         if (mdd->mdd_append_stripe_count)
3124                                 hint->dah_append_stripes =
3125                                         mdd->mdd_append_stripe_count;
3126                         if (mdd->mdd_append_pool[0])
3127                                 hint->dah_append_pool = mdd->mdd_append_pool;
3128                 } else {
3129                         hint->dah_append_stripes = 0;
3130                 }
3131         }
3132
3133         CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
3134                hint->dah_eadata, hint->dah_eadata_len);
3135         /* @hint will be initialized by underlying device. */
3136         nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
3137 }
3138
3139 static int mdd_accmode(const struct lu_env *env, const struct lu_attr *la,
3140                        u64 open_flags)
3141 {
3142         /* Sadly, NFSD reopens a file repeatedly during operation, so the
3143          * "acc_mode = 0" allowance for newly-created files isn't honoured.
3144          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
3145          * owner can write to a file even if it is marked readonly to hide
3146          * its brokenness. (bug 5781) */
3147         if (open_flags & MDS_OPEN_OWNEROVERRIDE) {
3148                 struct lu_ucred *uc = lu_ucred_check(env);
3149
3150                 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
3151                         return 0;
3152         }
3153
3154         return mds_accmode(open_flags);
3155 }
3156
3157 static int mdd_open_sanity_check(const struct lu_env *env,
3158                                  struct mdd_object *obj,
3159                                  const struct lu_attr *attr, u64 open_flags,
3160                                  int is_replay)
3161 {
3162         unsigned int may_mask;
3163         int rc;
3164         ENTRY;
3165
3166         /* EEXIST check, also opening of *open* orphans is allowed so we can
3167          * open-by-handle unlinked files
3168          */
3169         if (mdd_is_dead_obj(obj) && !is_replay &&
3170             likely(!(mdd_is_orphan_obj(obj) && obj->mod_count > 0)))
3171                 RETURN(-ENOENT);
3172
3173         if (S_ISLNK(attr->la_mode))
3174                 RETURN(-ELOOP);
3175
3176         may_mask = mdd_accmode(env, attr, open_flags);
3177
3178         if (S_ISDIR(attr->la_mode) && (may_mask & MAY_WRITE))
3179                 RETURN(-EISDIR);
3180
3181         if (!(open_flags & MDS_OPEN_CREATED)) {
3182                 rc = mdd_permission_internal(env, obj, attr, may_mask);
3183                 if (rc)
3184                         RETURN(rc);
3185         }
3186
3187         if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
3188             S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
3189                 open_flags &= ~MDS_OPEN_TRUNC;
3190
3191         /* For writing append-only file must open it with append mode. */
3192         if (attr->la_flags & LUSTRE_APPEND_FL) {
3193                 if ((open_flags & MDS_FMODE_WRITE) &&
3194                     !(open_flags & MDS_OPEN_APPEND))
3195                         RETURN(-EPERM);
3196                 if (open_flags & MDS_OPEN_TRUNC)
3197                         RETURN(-EPERM);
3198         }
3199
3200         RETURN(0);
3201 }
3202
3203 static int mdd_open(const struct lu_env *env, struct md_object *obj,
3204                     u64 open_flags, struct md_op_spec *spec)
3205 {
3206         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3207         struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
3208         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3209         struct mdd_object_user *mou = NULL;
3210         const struct lu_ucred *uc = lu_ucred(env);
3211         struct mdd_device *mdd = mdo2mdd(obj);
3212         enum changelog_rec_type type = CL_OPEN;
3213         int rc = 0;
3214         ENTRY;
3215
3216         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3217
3218         rc = mdd_la_get(env, mdd_obj, attr);
3219         if (rc != 0)
3220                 GOTO(out, rc);
3221
3222         rc = mdd_open_sanity_check(env, mdd_obj, attr, open_flags,
3223                                    spec->no_create);
3224         if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & BIT(CL_DN_OPEN)))
3225                 type = CL_DN_OPEN;
3226         else if (rc != 0)
3227                 GOTO(out, rc);
3228         else
3229                 mdd_obj->mod_count++;
3230
3231         if (!mdd_changelog_enabled(env, mdd, type))
3232                 GOTO(out, rc);
3233
3234 find:
3235         /* look for existing opener in list under mdd_write_lock */
3236         mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, open_flags);
3237
3238         if (!mou) {
3239                 int rc2;
3240
3241                 /* add user to list */
3242                 mou = mdd_obj_user_alloc(open_flags, uc->uc_uid, uc->uc_gid);
3243                 if (IS_ERR(mou)) {
3244                         if (rc == 0)
3245                                 rc = PTR_ERR(mou);
3246                         GOTO(out, rc);
3247                 }
3248                 rc2 = mdd_obj_user_add(mdd_obj, mou, type == CL_DN_OPEN);
3249                 if (rc2 != 0) {
3250                         mdd_obj_user_free(mou);
3251                         if (rc2 == -EEXIST)
3252                                 GOTO(find, rc2);
3253                 }
3254         } else {
3255                 if (type == CL_DN_OPEN) {
3256                         if (ktime_before(ktime_get(), mou->mou_deniednext))
3257                                 /* same user denied again same access within
3258                                  * time interval: do not record
3259                                  */
3260                                 GOTO(out, rc);
3261
3262                         /* this user already denied, but some time ago:
3263                          * update denied time
3264                          */
3265                         mou->mou_deniednext =
3266                                 ktime_add(ktime_get(),
3267                                           ktime_set(mdd->mdd_cl.mc_deniednext,
3268                                                     0));
3269                 } else {
3270                         mou->mou_opencount++;
3271                         /* same user opening file again with same flags:
3272                          * don't record
3273                          */
3274                         GOTO(out, rc);
3275                 }
3276         }
3277
3278         /* FYI, only the bottom 32 bits of open_flags are recorded */
3279         mdd_changelog(env, type, open_flags, md_dev, mdd_object_fid(mdd_obj));
3280
3281         EXIT;
3282 out:
3283         mdd_write_unlock(env, mdd_obj);
3284         return rc;
3285 }
3286
3287 static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
3288                              struct md_attr *ma, struct thandle *handle)
3289 {
3290         int rc;
3291
3292         rc = mdd_orphan_declare_delete(env, obj, handle);
3293         if (rc)
3294                 return rc;
3295
3296         return mdo_declare_destroy(env, obj, handle);
3297 }
3298
3299 /*
3300  * No permission check is needed.
3301  */
3302 static int mdd_close(const struct lu_env *env, struct md_object *obj,
3303                      struct md_attr *ma, u64 open_flags)
3304 {
3305         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3306         struct mdd_device *mdd = mdo2mdd(obj);
3307         struct thandle *handle = NULL;
3308         int is_orphan = 0;
3309         int rc;
3310         bool blocked = false;
3311         bool last_close_by_uid = false;
3312         const struct lu_ucred *uc = lu_ucred(env);
3313         ENTRY;
3314
3315         if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
3316                 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3317                 mdd_obj->mod_count--;
3318                 mdd_write_unlock(env, mdd_obj);
3319
3320                 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
3321                         CDEBUG(D_HA, "Object "DFID" is retained in orphan "
3322                                 "list\n", PFID(mdd_object_fid(mdd_obj)));
3323                 RETURN(0);
3324         }
3325
3326         /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
3327          * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
3328         /* check without any lock */
3329         is_orphan = mdd_obj->mod_count == 1 &&
3330                     (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
3331
3332 again:
3333         if (is_orphan) {
3334                 /* mdd_trans_create() maybe failed because of barrier_entry(),
3335                  * under such case, the orphan MDT-object will be left in the
3336                  * orphan list, and when the MDT remount next time, the unused
3337                  * orphans will be destroyed automatically.
3338                  *
3339                  * One exception: the former mdd_finish_unlink may failed to
3340                  * add the orphan MDT-object to the orphan list, then if the
3341                  * mdd_trans_create() failed because of barrier_entry(), the
3342                  * MDT-object will become real orphan that is neither in the
3343                  * namespace nor in the orphan list. Such bad case should be
3344                  * very rare and will be handled by e2fsck/lfsck. */
3345                 handle = mdd_trans_create(env, mdo2mdd(obj));
3346                 if (IS_ERR(handle)) {
3347                         rc = PTR_ERR(handle);
3348                         if (rc != -EINPROGRESS)
3349                                 GOTO(stop, rc);
3350
3351                         handle = NULL;
3352                         blocked = true;
3353                         goto cont;
3354                 }
3355
3356                 rc = mdd_declare_close(env, mdd_obj, ma, handle);
3357                 if (rc)
3358                         GOTO(stop, rc);
3359
3360                 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE, NULL, NULL,
3361                                                  handle);
3362                 if (rc)
3363                         GOTO(stop, rc);
3364
3365                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3366                 if (rc)
3367                         GOTO(stop, rc);
3368         }
3369
3370 cont:
3371         mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3372         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
3373         if (rc != 0) {
3374                 CERROR("%s: failed to get lu_attr of "DFID": rc = %d\n",
3375                        lu_dev_name(mdd2lu_dev(mdd)),
3376                        PFID(mdd_object_fid(mdd_obj)), rc);
3377                 GOTO(out, rc);
3378         }
3379
3380         /* check again with lock */
3381         is_orphan = (mdd_obj->mod_count == 1) &&
3382                     ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
3383                      ma->ma_attr.la_nlink == 0);
3384
3385         if (is_orphan && !handle && !blocked) {
3386                 mdd_write_unlock(env, mdd_obj);
3387                 goto again;
3388         }
3389
3390         mdd_obj->mod_count--; /*release open count */
3391
3392         /* under mdd write lock */
3393         /* If recording, see if we need to remove UID from list. uc is not
3394          * initialized if the client has been evicted. */
3395         if (mdd_changelog_enabled(env, mdd, CL_OPEN) && uc) {
3396                 struct mdd_object_user *mou;
3397
3398                 /* look for UID in list */
3399                 /* If mou is NULL, it probably means logging was enabled after
3400                  * the user had the file open. So the corresponding close
3401                  * will not be logged.
3402                  */
3403                 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid,
3404                                         open_flags);
3405                 if (mou) {
3406                         mou->mou_opencount--;
3407                         if (mou->mou_opencount == 0) {
3408                                 mdd_obj_user_remove(mdd_obj, mou);
3409                                 last_close_by_uid = true;
3410                         }
3411                 }
3412         }
3413
3414         if (!is_orphan || blocked)
3415                 GOTO(out, rc = 0);
3416
3417         /* Orphan object */
3418         /* NB: Object maybe not in orphan list originally, it is rare case for
3419          * mdd_finish_unlink() failure, in that case, the object doesn't have
3420          * ORPHAN_OBJ flag */
3421         if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
3422                 /* remove link to object from orphan index */
3423                 LASSERT(handle != NULL);
3424                 rc = mdd_orphan_delete(env, mdd_obj, handle);
3425                 if (rc != 0) {
3426                         CERROR("%s: unable to delete "DFID" from orphan list: "
3427                                "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3428                                PFID(mdd_object_fid(mdd_obj)), rc);
3429                         /* If object was not deleted from orphan list, do not
3430                          * destroy OSS objects, which will be done when next
3431                          * recovery. */
3432                         GOTO(out, rc);
3433                 }
3434
3435                 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
3436                        "list, OSS objects to be destroyed.\n",
3437                        PFID(mdd_object_fid(mdd_obj)));
3438         }
3439
3440         rc = mdo_destroy(env, mdd_obj, handle);
3441
3442         if (rc != 0) {
3443                 CERROR("%s: unable to delete "DFID" from orphan list: "
3444                        "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3445                        PFID(mdd_object_fid(mdd_obj)), rc);
3446         }
3447         EXIT;
3448
3449 out:
3450         mdd_write_unlock(env, mdd_obj);
3451
3452         if (rc != 0 || blocked ||
3453             !mdd_changelog_enabled(env, mdd, CL_CLOSE))
3454                 GOTO(stop, rc);
3455
3456         /* Record CL_CLOSE in changelog only if file was opened in write mode,
3457          * or if CL_OPEN was recorded and it's last close by user.
3458          * Changelogs mask may change between open and close operations, but
3459          * this is not a big deal if we have a CL_CLOSE entry with no matching
3460          * CL_OPEN. Plus Changelogs mask may not change often.
3461          */
3462         if (((!(mdd->mdd_cl.mc_mask & BIT(CL_OPEN)) &&
3463               (open_flags & (MDS_FMODE_WRITE | MDS_OPEN_APPEND |
3464                              MDS_OPEN_TRUNC))) ||
3465              ((mdd->mdd_cl.mc_mask & BIT(CL_OPEN)) && last_close_by_uid)) &&
3466             !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
3467                 if (handle == NULL) {
3468                         handle = mdd_trans_create(env, mdo2mdd(obj));
3469                         if (IS_ERR(handle))
3470                                 GOTO(stop, rc = PTR_ERR(handle));
3471
3472                         rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE,
3473                                                          NULL, NULL, handle);
3474                         if (rc)
3475                                 GOTO(stop, rc);
3476
3477                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3478                         if (rc)
3479                                 GOTO(stop, rc);
3480                 }
3481
3482                 /* FYI, only the bottom 32 bits of open_flags are recorded */
3483                 mdd_changelog_data_store(env, mdd, CL_CLOSE, open_flags,
3484                                          mdd_obj, handle, NULL);
3485         }
3486
3487 stop:
3488         if (handle != NULL && !IS_ERR(handle))
3489                 rc = mdd_trans_stop(env, mdd, rc, handle);
3490
3491         return rc;
3492 }
3493
3494 /*
3495  * Permission check is done when open,
3496  * no need check again.
3497  */
3498 static int mdd_readpage_sanity_check(const struct lu_env *env,
3499                                      struct mdd_object *obj)
3500 {
3501         struct dt_object *next = mdd_object_child(obj);
3502         int rc;
3503         ENTRY;
3504
3505         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
3506                 rc = 0;
3507         else
3508                 rc = -ENOTDIR;
3509
3510         RETURN(rc);
3511 }
3512
3513 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
3514                               size_t nob, const struct dt_it_ops *iops,
3515                               struct dt_it *it, __u32 attr, void *arg)
3516 {
3517         struct lu_dirpage       *dp = &lp->lp_dir;
3518         void                    *area = dp;
3519         int                      result;
3520         __u64                    hash = 0;
3521         struct lu_dirent        *ent;
3522         struct lu_dirent        *last = NULL;
3523         struct lu_fid            fid;
3524         int                      first = 1;
3525
3526         if (nob < sizeof(*dp))
3527                 return -EINVAL;
3528
3529         memset(area, 0, sizeof (*dp));
3530         area += sizeof (*dp);
3531         nob  -= sizeof (*dp);
3532
3533         ent  = area;
3534         do {
3535                 int    len;
3536                 size_t recsize;
3537
3538                 len = iops->key_size(env, it);
3539
3540                 /* IAM iterator can return record with zero len. */
3541                 if (len == 0)
3542                         goto next;
3543
3544                 hash = iops->store(env, it);
3545                 if (unlikely(first)) {
3546                         first = 0;
3547                         dp->ldp_hash_start = cpu_to_le64(hash);
3548                 }
3549
3550                 /* calculate max space required for lu_dirent */
3551                 recsize = lu_dirent_calc_size(len, attr);
3552
3553                 if (nob >= recsize) {
3554                         result = iops->rec(env, it, (struct dt_rec *)ent, attr);
3555                         if (result == -ESTALE)
3556                                 goto next;
3557                         if (result != 0)
3558                                 goto out;
3559
3560                         /* osd might not able to pack all attributes,
3561                          * so recheck rec length */
3562                         recsize = le16_to_cpu(ent->lde_reclen);
3563
3564                         if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
3565                                 fid_le_to_cpu(&fid, &ent->lde_fid);
3566                                 if (fid_is_dot_lustre(&fid))
3567                                         goto next;
3568                         }
3569                 } else {
3570                         result = (last != NULL) ? 0 :-EINVAL;
3571                         goto out;
3572                 }
3573                 last = ent;
3574                 ent = (void *)ent + recsize;
3575                 nob -= recsize;
3576
3577 next:
3578                 result = iops->next(env, it);
3579                 if (result == -ESTALE)
3580                         goto next;
3581         } while (result == 0);
3582
3583 out:
3584         dp->ldp_hash_end = cpu_to_le64(hash);
3585         if (last != NULL) {
3586                 if (last->lde_hash == dp->ldp_hash_end)
3587                         dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
3588                 last->lde_reclen = 0; /* end mark */
3589         }
3590         if (result > 0)
3591                 /* end of directory */
3592                 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3593         else if (result < 0)
3594                 CWARN("build page failed: %d!\n", result);
3595         return result;
3596 }
3597
3598 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
3599                  const struct lu_rdpg *rdpg)
3600 {
3601         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3602         int rc;
3603         ENTRY;
3604
3605         if (mdd_object_exists(mdd_obj) == 0) {
3606                 CERROR("%s: object "DFID" not found: rc = -2\n",
3607                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
3608                 return -ENOENT;
3609         }
3610
3611         mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
3612         rc = mdd_readpage_sanity_check(env, mdd_obj);
3613         if (rc)
3614                 GOTO(out_unlock, rc);
3615
3616         if (mdd_is_dead_obj(mdd_obj)) {
3617                 struct page *pg;
3618                 struct lu_dirpage *dp;
3619
3620                 /*
3621                  * According to POSIX, please do not return any entry to client:
3622                  * even dot and dotdot should not be returned.
3623                  */
3624                 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
3625                        PFID(mdd_object_fid(mdd_obj)));
3626
3627                 if (rdpg->rp_count <= 0)
3628                         GOTO(out_unlock, rc = -EFAULT);
3629                 LASSERT(rdpg->rp_pages != NULL);
3630
3631                 pg = rdpg->rp_pages[0];
3632                 dp = (struct lu_dirpage *)kmap(pg);
3633                 memset(dp, 0 , sizeof(struct lu_dirpage));
3634                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3635                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
3636                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3637                 kunmap(pg);
3638                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
3639         }
3640
3641         rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
3642                            mdd_dir_page_build, NULL);
3643         if (rc >= 0) {
3644                 struct lu_dirpage       *dp;
3645
3646                 dp = kmap(rdpg->rp_pages[0]);
3647                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3648                 if (rc == 0) {
3649                         /*
3650                          * No pages were processed, mark this for first page
3651                          * and send back.
3652                          */
3653                         dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3654                         dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3655                         rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
3656                 }
3657                 kunmap(rdpg->rp_pages[0]);
3658         }
3659
3660         GOTO(out_unlock, rc);
3661 out_unlock:
3662         mdd_read_unlock(env, mdd_obj);
3663         return rc;
3664 }
3665
3666 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
3667 {
3668         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3669
3670         if (mdd_object_exists(mdd_obj) == 0) {
3671                 int rc = -ENOENT;
3672
3673                 CERROR("%s: object "DFID" not found: rc = %d\n",
3674                        mdd_obj_dev_name(mdd_obj),
3675                        PFID(mdd_object_fid(mdd_obj)), rc);
3676                 return rc;
3677         }
3678         return dt_object_sync(env, mdd_object_child(mdd_obj),
3679                               0, OBD_OBJECT_EOF);
3680 }
3681
3682 static int mdd_object_lock(const struct lu_env *env,
3683                            struct md_object *obj,
3684                            struct lustre_handle *lh,
3685                            struct ldlm_enqueue_info *einfo,
3686                            union ldlm_policy_data *policy)
3687 {
3688         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3689         return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
3690                               einfo, policy);
3691 }
3692
3693 static int mdd_object_unlock(const struct lu_env *env,
3694                              struct md_object *obj,
3695                              struct ldlm_enqueue_info *einfo,
3696                              union ldlm_policy_data *policy)
3697 {
3698         struct mdd_object *mdd_obj = md2mdd_obj(obj);
3699         return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
3700 }
3701
3702 const struct md_object_operations mdd_obj_ops = {
3703         .moo_permission         = mdd_permission,
3704         .moo_attr_get           = mdd_attr_get,
3705         .moo_attr_set           = mdd_attr_set,
3706         .moo_xattr_get          = mdd_xattr_get,
3707         .moo_xattr_set          = mdd_xattr_set,
3708         .moo_xattr_list         = mdd_xattr_list,
3709         .moo_invalidate         = mdd_invalidate,
3710         .moo_xattr_del          = mdd_xattr_del,
3711         .moo_swap_layouts       = mdd_swap_layouts,
3712         .moo_open               = mdd_open,
3713         .moo_close              = mdd_close,
3714         .moo_readpage           = mdd_readpage,
3715         .moo_readlink           = mdd_readlink,
3716         .moo_changelog          = mdd_changelog,
3717         .moo_object_sync        = mdd_object_sync,
3718         .moo_object_lock        = mdd_object_lock,
3719         .moo_object_unlock      = mdd_object_unlock,
3720         .moo_layout_change      = mdd_layout_change,
3721 };