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