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