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