4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_object.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/module.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>
52 #include "mdd_internal.h"
54 static const struct lu_object_operations mdd_lu_obj_ops;
56 struct mdd_object_user {
57 struct list_head mou_list; /**< linked off mod_users */
58 int mou_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
66 static int mdd_xattr_get(const struct lu_env *env,
67 struct md_object *obj, struct lu_buf *buf,
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 int flags, const struct lu_fid *fid,
74 const char *xattr_name,
75 struct thandle *handle);
77 static inline bool has_prefix(const char *str, const char *prefix);
80 static unsigned int flags_helper(int flags)
82 unsigned int mflags = 0;
84 if (flags & MDS_FMODE_EXEC) {
85 mflags = MDS_FMODE_EXEC;
87 if (flags & MDS_FMODE_READ)
88 mflags = MDS_FMODE_READ;
90 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
91 mflags |= MDS_FMODE_WRITE;
97 /** Allocate/init a user and its sub-structures.
102 * \retval mou [OUT] success valid structure
105 static struct mdd_object_user *mdd_obj_user_alloc(int flags,
106 uid_t uid, gid_t gid)
108 struct mdd_object_user *mou;
112 OBD_SLAB_ALLOC_PTR(mou, mdd_object_kmem);
114 RETURN(ERR_PTR(-ENOMEM));
116 mou->mou_flags = flags;
117 mou->mou_uidgid = ((__u64)uid << 32) | gid;
118 mou->mou_opencount = 0;
119 mou->mou_deniednext = ktime_set(0, 0);
125 * Free a user and its sub-structures.
127 * \param mou [IN] user to be freed.
129 static void mdd_obj_user_free(struct mdd_object_user *mou)
131 OBD_SLAB_FREE_PTR(mou, mdd_object_kmem);
135 * Find if UID/GID already has this file open
137 * Caller should have write-locked \param mdd_obj.
138 * \param mdd_obj [IN] mdd_obj
139 * \param uid [IN] client uid
140 * \param gid [IN] client gid
141 * \retval user pointer or NULL if not found
144 struct mdd_object_user *mdd_obj_user_find(struct mdd_object *mdd_obj,
145 uid_t uid, gid_t gid, int flags)
147 struct mdd_object_user *mou;
152 uidgid = ((__u64)uid << 32) | gid;
153 list_for_each_entry(mou, &mdd_obj->mod_users, mou_list) {
154 if (mou->mou_uidgid == uidgid &&
155 flags_helper(mou->mou_flags) == flags_helper(flags))
162 * Add a user to the list of openers for this file
164 * Caller should have write-locked \param mdd_obj.
165 * \param mdd_obj [IN] mdd_obj
166 * \param mou [IN] user
168 * \retval -ve failure
170 static int mdd_obj_user_add(struct mdd_object *mdd_obj,
171 struct mdd_object_user *mou,
174 struct mdd_device *mdd = mdd_obj2mdd_dev(mdd_obj);
175 struct mdd_object_user *tmp;
176 __u32 uid = mou->mou_uidgid >> 32;
177 __u32 gid = mou->mou_uidgid & ((1UL << 32) - 1);
181 tmp = mdd_obj_user_find(mdd_obj, uid,
182 gid, mou->mou_flags);
186 list_add_tail(&mou->mou_list, &mdd_obj->mod_users);
189 /* next 'access denied' notification cannot happen before
192 mou->mou_deniednext =
193 ktime_add(ktime_get(),
194 ktime_set(mdd->mdd_cl.mc_deniednext, 0));
196 mou->mou_opencount++;
201 * Remove UID from the list
203 * Caller should have write-locked \param mdd_obj.
204 * \param mdd_obj [IN] mdd_obj
205 * \param uid [IN] user
206 * \retval -ve failure
208 static int mdd_obj_user_remove(struct mdd_object *mdd_obj,
209 struct mdd_object_user *mou)
216 list_del_init(&mou->mou_list);
218 mdd_obj_user_free(mou);
222 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
227 if (mdd_object_exists(obj) == 0)
230 rc = mdo_attr_get(env, obj, la);
231 if (unlikely(rc != 0)) {
233 obj->mod_flags |= DEAD_OBJ;
237 if (la->la_valid & LA_FLAGS &&
238 la->la_flags & LUSTRE_ORPHAN_FL)
239 obj->mod_flags |= ORPHAN_OBJ | DEAD_OBJ;
244 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
246 struct mdd_thread_info *info;
248 lu_env_refill((struct lu_env *)env);
249 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
250 LASSERT(info != NULL);
254 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
258 buf = &mdd_env_info(env)->mti_buf[0];
264 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
265 const void *area, ssize_t len)
269 buf = &mdd_env_info(env)->mti_buf[0];
270 buf->lb_buf = (void *)area;
275 struct lu_object *mdd_object_alloc(const struct lu_env *env,
276 const struct lu_object_header *hdr,
279 struct mdd_object *mdd_obj;
282 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
286 o = mdd2lu_obj(mdd_obj);
287 lu_object_init(o, NULL, d);
288 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
289 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
290 mdd_obj->mod_count = 0;
291 o->lo_ops = &mdd_lu_obj_ops;
292 INIT_LIST_HEAD(&mdd_obj->mod_users);
297 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
298 const struct lu_object_conf *unused)
300 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
301 struct mdd_object *mdd_obj = lu2mdd_obj(o);
302 struct lu_object *below;
303 struct lu_device *under;
306 mdd_obj->mod_cltime = ktime_set(0, 0);
307 under = &d->mdd_child->dd_lu_dev;
308 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
310 RETURN(PTR_ERR(below));
312 lu_object_add(o, below);
317 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
321 if (lu_object_exists(o)) {
322 struct mdd_object *mdd_obj = lu2mdd_obj(o);
323 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
325 rc = mdd_la_get(env, mdd_obj, attr);
331 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
333 struct mdd_object *mdd = lu2mdd_obj(o);
334 struct mdd_object_user *mou, *tmp2;
337 list_for_each_entry_safe(mou, tmp2, &mdd->mod_users, mou_list) {
338 list_del(&mou->mou_list);
339 mdd_obj_user_free(mou);
343 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
346 static int mdd_object_print(const struct lu_env *env, void *cookie,
347 lu_printer_t p, const struct lu_object *o)
349 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
351 return (*p)(env, cookie,
352 LUSTRE_MDD_NAME"-object@%p(open_count=%d, valid=%x, cltime=%lldns, flags=%lx)",
353 mdd, mdd->mod_count, mdd->mod_valid,
354 ktime_to_ns(mdd->mod_cltime), mdd->mod_flags);
357 static const struct lu_object_operations mdd_lu_obj_ops = {
358 .loo_object_init = mdd_object_init,
359 .loo_object_start = mdd_object_start,
360 .loo_object_free = mdd_object_free,
361 .loo_object_print = mdd_object_print,
364 struct mdd_object *mdd_object_find(const struct lu_env *env,
365 struct mdd_device *d,
366 const struct lu_fid *f)
368 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
372 * No permission check is needed.
374 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
377 struct mdd_object *mdd_obj = md2mdd_obj(obj);
382 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
383 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
384 ma->ma_attr.la_nlink = 0;
390 * No permission check is needed.
392 static int mdd_xattr_get(const struct lu_env *env,
393 struct md_object *obj, struct lu_buf *buf,
396 struct mdd_object *mdd_obj = md2mdd_obj(obj);
397 struct mdd_device *mdd;
402 if (mdd_object_exists(mdd_obj) == 0) {
403 CERROR("%s: object "DFID" not found: rc = -2\n",
404 mdd_obj_dev_name(mdd_obj),
405 PFID(mdd_object_fid(mdd_obj)));
409 /* If the object has been destroyed, then do not get LMVEA, because
410 * it needs to load stripes from the iteration of the master object,
411 * and it will cause problem if master object has been destroyed, see
413 if (unlikely((mdd_obj->mod_flags & DEAD_OBJ) &&
414 !(mdd_obj->mod_flags & ORPHAN_OBJ) &&
415 strcmp(name, XATTR_NAME_LMV) == 0))
418 /* If the object has been delete from the namespace, then
419 * get linkEA should return -ENOENT as well */
420 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
421 strcmp(name, XATTR_NAME_LINK) == 0))
424 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
425 rc = mdo_xattr_get(env, mdd_obj, buf, name);
426 mdd_read_unlock(env, mdd_obj);
430 /* record only getting user xattrs and acls */
432 mdd_changelog_enabled(env, mdd, CL_GETXATTR) &&
433 (has_prefix(name, XATTR_USER_PREFIX) ||
434 has_prefix(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
435 has_prefix(name, XATTR_NAME_POSIX_ACL_DEFAULT))) {
436 struct thandle *handle;
439 LASSERT(mdo2fid(mdd_obj) != NULL);
441 handle = mdd_trans_create(env, mdd);
443 RETURN(PTR_ERR(handle));
445 rc2 = mdd_declare_changelog_store(env, mdd, CL_GETXATTR, NULL,
450 rc2 = mdd_trans_start(env, mdd, handle);
454 rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
455 mdo2fid(mdd_obj), name,
459 rc2 = mdd_trans_stop(env, mdd, rc2, handle);
468 * Permission check is done when open,
469 * no need check again.
471 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
474 struct mdd_object *mdd_obj = md2mdd_obj(obj);
475 struct dt_object *next;
480 if (mdd_object_exists(mdd_obj) == 0) {
481 CERROR("%s: object "DFID" not found: rc = -2\n",
482 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
486 next = mdd_object_child(mdd_obj);
487 LASSERT(next != NULL);
488 LASSERT(next->do_body_ops != NULL);
489 LASSERT(next->do_body_ops->dbo_read != NULL);
490 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
491 rc = dt_read(env, next, buf, &pos);
492 mdd_read_unlock(env, mdd_obj);
497 * No permission check is needed.
499 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
502 struct mdd_object *mdd_obj = md2mdd_obj(obj);
507 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
508 rc = mdo_xattr_list(env, mdd_obj, buf);
509 mdd_read_unlock(env, mdd_obj);
511 /* If the buffer is NULL then we are only here to get the
512 * length of the xattr name list. */
513 if (rc < 0 || buf->lb_buf == NULL)
517 * Filter out XATTR_NAME_LINK if this is an orphan object. See
520 if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
521 char *end = (char *)buf->lb_buf + rc;
522 char *p = buf->lb_buf;
525 char *next = p + strlen(p) + 1;
527 if (strcmp(p, XATTR_NAME_LINK) == 0) {
529 memmove(p, next, end - next);
531 CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
532 " of orphan "DFID"\n",
533 PFID(mdd_object_fid(mdd_obj)));
544 int mdd_invalidate(const struct lu_env *env, struct md_object *obj)
546 return mdo_invalidate(env, md2mdd_obj(obj));
549 int mdd_declare_create_object_internal(const struct lu_env *env,
550 struct mdd_object *p,
551 struct mdd_object *c,
552 struct lu_attr *attr,
553 struct thandle *handle,
554 const struct md_op_spec *spec,
555 struct dt_allocation_hint *hint)
557 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
558 const struct dt_index_features *feat = spec->sp_feat;
562 if (feat != &dt_directory_features && feat != NULL) {
563 dof->dof_type = DFT_INDEX;
564 dof->u.dof_idx.di_feat = feat;
566 dof->dof_type = dt_mode_to_dft(attr->la_mode);
567 if (dof->dof_type == DFT_REGULAR) {
568 dof->u.dof_reg.striped =
569 md_should_create(spec->sp_cr_flags);
570 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
571 dof->u.dof_reg.striped = 0;
572 /* is this replay? */
574 dof->u.dof_reg.striped = 0;
578 rc = mdo_declare_create_object(env, c, attr, hint, dof, handle);
583 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
584 struct mdd_object *c, struct lu_attr *attr,
585 struct thandle *handle,
586 const struct md_op_spec *spec,
587 struct dt_allocation_hint *hint)
589 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
593 LASSERT(!mdd_object_exists(c));
595 rc = mdo_create_object(env, c, attr, hint, dof, handle);
600 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
601 const struct lu_attr *attr, struct thandle *handle,
607 rc = mdo_attr_set(env, obj, attr, handle);
608 #ifdef CONFIG_FS_POSIX_ACL
609 if (!rc && (attr->la_valid & LA_MODE) && needacl)
610 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
615 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
616 const struct lu_attr *oattr, struct lu_attr *attr,
617 struct thandle *handle)
622 LASSERT(attr->la_valid & LA_CTIME);
623 LASSERT(oattr != NULL);
625 /* Make sure the ctime is increased only, however, it's not strictly
626 * reliable at here because there is not guarantee to hold lock on
627 * object, so we just bypass some unnecessary cmtime setting first
628 * and OSD has to check it again. */
629 if (attr->la_ctime < oattr->la_ctime)
630 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
631 else if (attr->la_valid == LA_CTIME &&
632 attr->la_ctime == oattr->la_ctime)
633 attr->la_valid &= ~LA_CTIME;
635 if (attr->la_valid != 0)
636 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
641 * This gives the same functionality as the code between
642 * sys_chmod and inode_setattr
643 * chown_common and inode_setattr
644 * utimes and inode_setattr
645 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
647 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
648 const struct lu_attr *oattr, struct lu_attr *la,
649 const unsigned long flags)
658 /* Do not permit change file type */
659 if (la->la_valid & LA_TYPE)
662 /* They should not be processed by setattr */
663 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
666 LASSERT(oattr != NULL);
668 uc = lu_ucred_check(env);
672 if (la->la_valid == LA_CTIME) {
673 if (!(flags & MDS_PERM_BYPASS))
674 /* This is only for set ctime when rename's source is
676 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
678 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
679 la->la_valid &= ~LA_CTIME;
683 if (la->la_valid == LA_ATIME) {
684 /* This is an atime-only attribute update for close RPCs. */
685 if (la->la_atime < (oattr->la_atime +
686 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
687 la->la_valid &= ~LA_ATIME;
691 /* Check if flags change. */
692 if (la->la_valid & LA_FLAGS) {
693 unsigned int oldflags = oattr->la_flags &
694 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
695 unsigned int newflags = la->la_flags &
696 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
698 if ((uc->uc_fsuid != oattr->la_uid) &&
699 !md_capable(uc, CFS_CAP_FOWNER))
702 /* The IMMUTABLE and APPEND_ONLY flags can
703 * only be changed by the relevant capability. */
704 if ((oldflags ^ newflags) &&
705 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
708 if (!S_ISDIR(oattr->la_mode))
709 la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
712 if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
713 (la->la_valid & ~LA_FLAGS) &&
714 !(flags & MDS_PERM_BYPASS))
717 /* Check for setting the obj time. */
718 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
719 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
720 if ((uc->uc_fsuid != oattr->la_uid) &&
721 !md_capable(uc, CFS_CAP_FOWNER)) {
722 rc = mdd_permission_internal(env, obj, oattr,
729 if (la->la_valid & LA_KILL_SUID) {
730 la->la_valid &= ~LA_KILL_SUID;
731 if ((oattr->la_mode & S_ISUID) &&
732 !(la->la_valid & LA_MODE)) {
733 la->la_mode = oattr->la_mode;
734 la->la_valid |= LA_MODE;
736 la->la_mode &= ~S_ISUID;
739 if (la->la_valid & LA_KILL_SGID) {
740 la->la_valid &= ~LA_KILL_SGID;
741 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
742 (S_ISGID | S_IXGRP)) &&
743 !(la->la_valid & LA_MODE)) {
744 la->la_mode = oattr->la_mode;
745 la->la_valid |= LA_MODE;
747 la->la_mode &= ~S_ISGID;
750 /* Make sure a caller can chmod. */
751 if (la->la_valid & LA_MODE) {
752 if (!(flags & MDS_PERM_BYPASS) &&
753 (uc->uc_fsuid != oattr->la_uid) &&
754 !md_capable(uc, CFS_CAP_FOWNER))
757 if (la->la_mode == (umode_t) -1)
758 la->la_mode = oattr->la_mode;
760 la->la_mode = (la->la_mode & S_IALLUGO) |
761 (oattr->la_mode & ~S_IALLUGO);
763 /* Also check the setgid bit! */
764 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
765 la->la_gid : oattr->la_gid) &&
766 !md_capable(uc, CFS_CAP_FSETID))
767 la->la_mode &= ~S_ISGID;
769 la->la_mode = oattr->la_mode;
772 /* Make sure a caller can chown. */
773 if (la->la_valid & LA_UID) {
774 if (la->la_uid == (uid_t) -1)
775 la->la_uid = oattr->la_uid;
776 if (((uc->uc_fsuid != oattr->la_uid) ||
777 (la->la_uid != oattr->la_uid)) &&
778 !md_capable(uc, CFS_CAP_CHOWN))
781 /* If the user or group of a non-directory has been
782 * changed by a non-root user, remove the setuid bit.
783 * 19981026 David C Niemi <niemi@tux.org>
785 * Changed this to apply to all users, including root,
786 * to avoid some races. This is the behavior we had in
787 * 2.0. The check for non-root was definitely wrong
788 * for 2.2 anyway, as it should have been using
789 * CAP_FSETID rather than fsuid -- 19990830 SD. */
790 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
791 !S_ISDIR(oattr->la_mode)) {
792 la->la_mode &= ~S_ISUID;
793 la->la_valid |= LA_MODE;
797 /* Make sure caller can chgrp. */
798 if (la->la_valid & LA_GID) {
799 if (la->la_gid == (gid_t) -1)
800 la->la_gid = oattr->la_gid;
801 if (((uc->uc_fsuid != oattr->la_uid) ||
802 ((la->la_gid != oattr->la_gid) &&
803 !lustre_in_group_p(uc, la->la_gid))) &&
804 !md_capable(uc, CFS_CAP_CHOWN))
807 /* Likewise, if the user or group of a non-directory
808 * has been changed by a non-root user, remove the
809 * setgid bit UNLESS there is no group execute bit
810 * (this would be a file marked for mandatory
811 * locking). 19981026 David C Niemi <niemi@tux.org>
813 * Removed the fsuid check (see the comment above) --
815 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
816 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
817 la->la_mode &= ~S_ISGID;
818 la->la_valid |= LA_MODE;
822 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
823 if (!((flags & MDS_OWNEROVERRIDE) &&
824 (uc->uc_fsuid == oattr->la_uid)) &&
825 !(flags & MDS_PERM_BYPASS)) {
826 rc = mdd_permission_internal(env, obj, oattr,
833 if (la->la_valid & LA_CTIME) {
834 /* The pure setattr, it has the priority over what is
835 * already set, do not drop it if ctime is equal. */
836 if (la->la_ctime < oattr->la_ctime)
837 la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
843 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
844 struct mdd_device *mdd,
845 enum changelog_rec_type type,
846 int flags, const struct lu_fid *fid,
847 const char *xattr_name,
848 struct thandle *handle)
850 const struct lu_ucred *uc = lu_ucred(env);
851 struct llog_changelog_rec *rec;
854 int xflags = CLFE_INVALID;
857 flags = (flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
860 if (uc->uc_jobid[0] != '\0')
862 xflags |= CLFE_UIDGID;
865 if (type == CL_OPEN || type == CL_DN_OPEN)
867 if (type == CL_SETXATTR || type == CL_GETXATTR)
868 xflags |= CLFE_XATTR;
870 reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
871 changelog_rec_offset(flags & CLF_SUPPORTED,
872 xflags & CLFE_SUPPORTED));
873 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
874 if (buf->lb_buf == NULL)
878 rec->cr_hdr.lrh_len = reclen;
879 rec->cr.cr_flags = flags;
880 rec->cr.cr_type = (__u32)type;
881 rec->cr.cr_tfid = *fid;
882 rec->cr.cr_namelen = 0;
884 if (flags & CLF_JOBID)
885 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
887 if (flags & CLF_EXTRA_FLAGS) {
888 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
889 if (xflags & CLFE_UIDGID)
890 mdd_changelog_rec_extra_uidgid(&rec->cr,
891 uc->uc_uid, uc->uc_gid);
892 if (xflags & CLFE_NID)
893 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
894 if (xflags & CLFE_OPEN)
895 mdd_changelog_rec_extra_omode(&rec->cr, flags);
896 if (xflags & CLFE_XATTR) {
897 if (xattr_name == NULL)
899 mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
903 rc = mdd_changelog_store(env, mdd, rec, handle);
908 /** Store a data change changelog record
909 * If this fails, we must fail the whole transaction; we don't
910 * want the change to commit without the log entry.
911 * \param mdd_obj - mdd_object of change
912 * \param handle - transaction handle
914 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
915 enum changelog_rec_type type, int flags,
916 struct mdd_object *mdd_obj, struct thandle *handle)
920 LASSERT(mdd_obj != NULL);
921 LASSERT(handle != NULL);
923 if (!mdd_changelog_enabled(env, mdd, type))
926 if (mdd_is_volatile_obj(mdd_obj))
929 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
930 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
931 /* Don't need multiple updates in this log */
932 /* Don't check under lock - no big deal if we get an extra
937 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
938 mdo2fid(mdd_obj), NULL, handle);
940 mdd_obj->mod_cltime = ktime_get();
945 int mdd_changelog_data_store_xattr(const struct lu_env *env,
946 struct mdd_device *mdd,
947 enum changelog_rec_type type,
948 int flags, struct mdd_object *mdd_obj,
949 const char *xattr_name,
950 struct thandle *handle)
954 LASSERT(mdd_obj != NULL);
955 LASSERT(handle != NULL);
957 if (!mdd_changelog_enabled(env, mdd, type))
960 if (mdd_is_volatile_obj(mdd_obj))
963 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
964 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
965 /* Don't need multiple updates in this log */
966 /* Don't check under lock - no big deal if we get an extra
972 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
973 mdo2fid(mdd_obj), xattr_name,
976 mdd_obj->mod_cltime = ktime_get();
981 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
982 int flags, struct md_device *m, const struct lu_fid *fid)
984 struct thandle *handle;
985 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
989 LASSERT(fid != NULL);
991 /* We'll check this again below, but we check now before we
992 * start a transaction. */
993 if (!mdd_changelog_enabled(env, mdd, type))
996 handle = mdd_trans_create(env, mdd);
998 RETURN(PTR_ERR(handle));
1000 rc = mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1004 rc = mdd_trans_start(env, mdd, handle);
1008 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
1012 rc = mdd_trans_stop(env, mdd, rc, handle);
1018 * Save LMA extended attributes with data from \a ma.
1020 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
1021 * not, LMA EA will be first read from disk, modified and write back.
1024 /* Precedence for choosing record type when multiple
1025 * attributes change: setattr > mtime > ctime > atime
1026 * (ctime changes when mtime does, plus chmod/chown.
1027 * atime and ctime are independent.) */
1028 static int mdd_attr_set_changelog(const struct lu_env *env,
1029 struct md_object *obj, struct thandle *handle,
1032 struct mdd_device *mdd = mdo2mdd(obj);
1035 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
1036 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
1037 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
1038 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
1039 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
1040 bits = bits & mdd->mdd_cl.mc_mask;
1041 /* This is an implementation limit rather than a protocol limit */
1042 CLASSERT(CL_LAST <= sizeof(int) * 8);
1046 /* The record type is the lowest non-masked set bit */
1049 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
1050 return mdd_changelog_data_store(env, mdd, type, (int)valid,
1051 md2mdd_obj(obj), handle);
1054 static int mdd_declare_attr_set(const struct lu_env *env,
1055 struct mdd_device *mdd,
1056 struct mdd_object *obj,
1057 const struct lu_attr *attr,
1058 struct thandle *handle)
1062 rc = mdo_declare_attr_set(env, obj, attr, handle);
1066 #ifdef CONFIG_FS_POSIX_ACL
1067 if (attr->la_valid & LA_MODE) {
1068 mdd_read_lock(env, obj, MOR_TGT_CHILD);
1069 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
1070 XATTR_NAME_ACL_ACCESS);
1071 mdd_read_unlock(env, obj);
1072 if (rc == -EOPNOTSUPP || rc == -ENODATA)
1078 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
1079 rc = mdo_declare_xattr_set(env, obj, buf,
1080 XATTR_NAME_ACL_ACCESS, 0,
1088 rc = mdd_declare_changelog_store(env, mdd, CL_SETXATTR, NULL, NULL,
1096 * permission changes may require sync operation, to mitigate performance
1097 * impact, only do this for dir and when permission is reduced.
1099 * For regular files, version is updated with permission change (see VBR), async
1100 * permission won't cause any issue, while missing permission change on
1101 * directory may affect accessibility of other objects after recovery.
1103 static inline bool permission_needs_sync(const struct lu_attr *old,
1104 const struct lu_attr *new)
1106 if (!S_ISDIR(old->la_mode))
1109 if (new->la_valid & (LA_UID | LA_GID))
1112 if (new->la_valid & LA_MODE &&
1113 new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
1116 if ((new->la_valid & LA_MODE) &&
1117 ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
1118 (old->la_mode & S_IRWXUGO))
1124 static inline __u64 mdd_lmm_dom_size(void *buf)
1126 struct lov_mds_md *lmm = buf;
1127 struct lov_comp_md_v1 *comp_v1;
1128 struct lov_mds_md *v1;
1134 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1137 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1138 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1139 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1141 /* DoM entry is the first entry always */
1142 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) == LOV_PATTERN_MDT)
1143 return le64_to_cpu(comp_v1->lcm_entries[0].lcme_extent.e_end);
1148 /* set attr and LOV EA at once, return updated attr */
1149 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
1150 const struct md_attr *ma)
1152 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1153 struct mdd_device *mdd = mdo2mdd(obj);
1154 struct thandle *handle = NULL;
1155 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1156 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1157 const struct lu_attr *la = &ma->ma_attr;
1158 struct lu_ucred *uc;
1162 /* we do not use ->attr_set() for LOV/HSM EA any more */
1163 LASSERT((ma->ma_valid & MA_LOV) == 0);
1164 LASSERT((ma->ma_valid & MA_HSM) == 0);
1166 rc = mdd_la_get(env, mdd_obj, attr);
1170 *la_copy = ma->ma_attr;
1171 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
1175 /* no need to setattr anymore */
1176 if (la_copy->la_valid == 0) {
1177 CDEBUG(D_INODE, "%s: no valid attribute on "DFID", previous"
1178 "valid is %#llx\n", mdd2obd_dev(mdd)->obd_name,
1179 PFID(mdo2fid(mdd_obj)), la->la_valid);
1184 /* If an unprivileged user changes group of some file,
1185 * the setattr operation will be processed synchronously to
1186 * honor the quota limit of the corresponding group. see LU-5152 */
1187 uc = lu_ucred_check(env);
1188 if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1189 la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1190 la_copy->la_valid |= LA_FLAGS;
1191 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1193 /* Flush the possible existing sync requests to OSTs to
1194 * keep the order of sync for the current setattr operation
1195 * will be sent directly to OSTs. see LU-5152 */
1196 rc = dt_sync(env, mdd->mdd_child);
1201 handle = mdd_trans_create(env, mdd);
1202 if (IS_ERR(handle)) {
1203 rc = PTR_ERR(handle);
1209 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1213 rc = mdd_trans_start(env, mdd, handle);
1217 if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
1218 handle->th_sync = 1;
1220 if (la->la_valid & (LA_MTIME | LA_CTIME))
1221 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1222 la->la_mtime, la->la_ctime);
1224 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1226 /* LU-10509: setattr of LA_SIZE should be skipped case of DOM,
1227 * otherwise following truncate will do nothing and truncated
1228 * data may be read again. This is a quick fix until LU-11033
1231 if (la_copy->la_valid & LA_SIZE) {
1232 struct lu_buf *lov_buf = mdd_buf_get(env, NULL, 0);
1234 rc = mdd_stripe_get(env, mdd_obj, lov_buf, XATTR_NAME_LOV);
1238 if (mdd_lmm_dom_size(lov_buf->lb_buf) > 0)
1239 la_copy->la_valid &= ~LA_SIZE;
1240 lu_buf_free(lov_buf);
1244 if (la_copy->la_valid) {
1245 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1247 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1248 /* rollback to the original gid */
1249 la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1250 la_copy->la_gid = attr->la_gid;
1251 mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1254 mdd_write_unlock(env, mdd_obj);
1258 rc = mdd_attr_set_changelog(env, obj, handle,
1262 rc = mdd_trans_stop(env, mdd, rc, handle);
1267 static int mdd_xattr_sanity_check(const struct lu_env *env,
1268 struct mdd_object *obj,
1269 const struct lu_attr *attr,
1272 struct lu_ucred *uc = lu_ucred_assert(env);
1275 if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1278 if (strncmp(XATTR_USER_PREFIX, name,
1279 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1280 /* For sticky directories, only the owner and privileged user
1281 * can write attributes. */
1282 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1283 (uc->uc_fsuid != attr->la_uid) &&
1284 !md_capable(uc, CFS_CAP_FOWNER))
1286 } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
1287 (uc->uc_fsuid != attr->la_uid) &&
1288 !md_capable(uc, CFS_CAP_FOWNER)) {
1296 * Check if a string begins with a given prefix.
1298 * \param str String to check
1299 * \param prefix Substring to check at the beginning of \a str
1300 * \return true/false whether the condition is verified.
1302 static inline bool has_prefix(const char *str, const char *prefix)
1304 return strncmp(prefix, str, strlen(prefix)) == 0;
1308 * Indicate the kind of changelog to store (if any) for a xattr set/del.
1310 * \param[in] xattr_name Full extended attribute name.
1312 * \return The type of changelog to use, or -1 if no changelog is to be emitted.
1314 static enum changelog_rec_type
1315 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1316 const char *xattr_name)
1318 /* Layout changes systematically recorded */
1319 if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1320 strncmp(XATTR_LUSTRE_LOV, xattr_name,
1321 strlen(XATTR_LUSTRE_LOV)) == 0)
1324 /* HSM information changes systematically recorded */
1325 if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1328 /* Avoid logging SOM xattr for every file */
1329 if (strcmp(XATTR_NAME_SOM, xattr_name) == 0)
1332 if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1333 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1334 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1335 has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1336 has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1342 static int mdd_declare_xattr_set(const struct lu_env *env,
1343 struct mdd_device *mdd,
1344 struct mdd_object *obj,
1345 const struct lu_buf *buf,
1347 int fl, struct thandle *handle)
1349 enum changelog_rec_type type;
1352 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1356 type = mdd_xattr_changelog_type(env, mdd, name);
1358 return 0; /* no changelog to store */
1360 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1364 * Compare current and future data of HSM EA and add a changelog if needed.
1366 * Caller should have write-locked \param obj.
1368 * \param buf - Future HSM EA content.
1369 * \retval 0 if no changelog is needed or changelog was added properly.
1370 * \retval -ve errno if there was a problem
1372 static int mdd_hsm_update_locked(const struct lu_env *env,
1373 struct md_object *obj,
1374 const struct lu_buf *buf,
1375 struct thandle *handle, int *cl_flags)
1377 struct mdd_thread_info *info = mdd_env_info(env);
1378 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1379 struct lu_buf *current_buf;
1380 struct md_hsm *current_mh;
1381 struct md_hsm *new_mh;
1385 OBD_ALLOC_PTR(current_mh);
1386 if (current_mh == NULL)
1389 /* Read HSM attrs from disk */
1390 current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1391 mdo2mdd(obj)->mdd_dt_conf.ddp_max_ea_size);
1392 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1393 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1394 if (rc < 0 && rc != -ENODATA)
1396 else if (rc == -ENODATA)
1397 current_mh->mh_flags = 0;
1399 /* Map future HSM xattr */
1400 OBD_ALLOC_PTR(new_mh);
1402 GOTO(free, rc = -ENOMEM);
1403 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1407 /* Flags differ, set flags for the changelog that will be added */
1408 if (current_mh->mh_flags != new_mh->mh_flags) {
1409 hsm_set_cl_event(cl_flags, HE_STATE);
1410 if (new_mh->mh_flags & HS_DIRTY)
1411 hsm_set_cl_flags(cl_flags, CLF_HSM_DIRTY);
1414 OBD_FREE_PTR(new_mh);
1417 OBD_FREE_PTR(current_mh);
1421 static int mdd_object_pfid_replace(const struct lu_env *env,
1422 struct mdd_object *o)
1424 struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1425 struct thandle *handle;
1428 handle = mdd_trans_create(env, mdd);
1430 RETURN(PTR_ERR(handle));
1432 handle->th_complex = 1;
1434 /* it doesn't need to track the PFID update via llog, because LFSCK
1435 * will repair it even it goes wrong */
1436 rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1441 rc = mdd_trans_start(env, mdd, handle);
1445 rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1450 mdd_trans_stop(env, mdd, rc, handle);
1455 static int mdd_declare_xattr_del(const struct lu_env *env,
1456 struct mdd_device *mdd,
1457 struct mdd_object *obj,
1459 struct thandle *handle);
1461 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1464 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1465 struct md_object *md_vic)
1467 struct mdd_device *mdd = mdo2mdd(md_obj);
1468 struct mdd_object *obj = md2mdd_obj(md_obj);
1469 struct mdd_object *vic = md2mdd_obj(md_vic);
1470 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1471 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1472 struct lov_mds_md *lmm;
1473 struct thandle *handle;
1477 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1478 if (rc == 0) /* same fid */
1481 handle = mdd_trans_create(env, mdd);
1483 RETURN(PTR_ERR(handle));
1486 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1487 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1489 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1490 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1493 /* get EA of victim file */
1494 memset(buf_vic, 0, sizeof(*buf_vic));
1495 rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
1502 /* parse the layout of victim file */
1503 lmm = buf_vic->lb_buf;
1504 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1505 GOTO(out, rc = -EINVAL);
1507 /* save EA of target file for restore */
1508 memset(buf, 0, sizeof(*buf));
1509 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
1513 /* Get rid of the layout from victim object */
1514 rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1518 rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1519 LU_XATTR_MERGE, handle);
1523 rc = mdd_trans_start(env, mdd, handle);
1527 rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1532 rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1534 GOTO(out_restore, rc);
1536 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1537 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1542 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1543 LU_XATTR_REPLACE, handle);
1545 CERROR("%s: failed to rollback of layout of: "DFID
1546 ": %d, file state unknown\n",
1547 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1551 mdd_trans_stop(env, mdd, rc, handle);
1552 mdd_write_unlock(env, obj);
1553 mdd_write_unlock(env, vic);
1555 lu_buf_free(buf_vic);
1558 (void) mdd_object_pfid_replace(env, obj);
1564 * Extract the mirror with specified mirror id, and store the splitted
1565 * mirror layout to @buf.
1567 * \param[in] comp_v1 mirrored layout
1568 * \param[in] mirror_id the mirror with mirror_id to be extracted
1569 * \param[out] buf store the layout excluding the extracted mirror,
1570 * caller free the buffer we allocated in this function
1571 * \param[out] buf_vic store the extracted layout, caller free the buffer
1572 * we allocated in this function
1574 * \retval 0 on success; < 0 if error happens
1576 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1577 struct lu_buf *buf, struct lu_buf *buf_vic)
1579 struct lov_comp_md_v1 *comp_rem;
1580 struct lov_comp_md_v1 *comp_vic;
1581 struct lov_comp_md_entry_v1 *entry;
1582 struct lov_comp_md_entry_v1 *entry_rem;
1583 struct lov_comp_md_entry_v1 *entry_vic;
1585 __u16 comp_cnt, count = 0;
1586 int lmm_size, lmm_size_vic = 0;
1588 int offset, offset_rem, offset_vic;
1590 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1591 /* comp_v1 should contains more than 1 mirror */
1592 if (mirror_cnt <= 1)
1594 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1595 lmm_size = le32_to_cpu(comp_v1->lcm_size);
1597 for (i = 0; i < comp_cnt; i++) {
1598 entry = &comp_v1->lcm_entries[i];
1599 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1601 lmm_size_vic += sizeof(*entry);
1602 lmm_size_vic += le32_to_cpu(entry->lcme_size);
1603 } else if (count > 0) {
1604 /* find the specified mirror */
1612 lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1616 lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1617 if (!buf_vic->lb_buf) {
1622 comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1623 comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1625 memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1626 comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1627 comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1628 comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1629 if (!comp_rem->lcm_mirror_count)
1630 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1632 memset(comp_vic, 0, sizeof(*comp_v1));
1633 comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1634 comp_vic->lcm_mirror_count = 0;
1635 comp_vic->lcm_entry_count = cpu_to_le32(count);
1636 comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1637 comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1638 comp_vic->lcm_layout_gen = 0;
1640 offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1641 offset_rem = sizeof(*comp_rem) +
1642 sizeof(*entry_rem) * (comp_cnt - count);
1643 offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1644 for (i = j = k = 0; i < comp_cnt; i++) {
1645 struct lov_mds_md *lmm, *lmm_dst;
1648 entry = &comp_v1->lcm_entries[i];
1649 entry_vic = &comp_vic->lcm_entries[j];
1650 entry_rem = &comp_rem->lcm_entries[k];
1652 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1655 /* copy component entry */
1657 memcpy(entry_vic, entry, sizeof(*entry));
1658 entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1659 entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1662 memcpy(entry_rem, entry, sizeof(*entry));
1663 entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1667 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1669 lmm_dst = (struct lov_mds_md *)
1670 ((char *)comp_vic + offset_vic);
1672 lmm_dst = (struct lov_mds_md *)
1673 ((char *)comp_rem + offset_rem);
1675 /* copy component entry blob */
1676 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1678 /* blob offset advance */
1679 offset += le32_to_cpu(entry->lcme_size);
1681 offset_vic += le32_to_cpu(entry->lcme_size);
1683 offset_rem += le32_to_cpu(entry->lcme_size);
1689 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1690 struct md_rejig_data *mrd)
1692 struct mdd_device *mdd = mdo2mdd(md_obj);
1693 struct mdd_object *obj = md2mdd_obj(md_obj);
1694 struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
1695 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1696 struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1697 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1698 struct lov_comp_md_v1 *lcm;
1699 struct thandle *handle;
1703 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1704 if (rc == 0) /* same fid */
1707 handle = mdd_trans_create(env, mdd);
1709 RETURN(PTR_ERR(handle));
1712 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1713 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1715 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1716 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1719 /* get EA of mirrored file */
1720 memset(buf_save, 0, sizeof(*buf));
1721 rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
1725 lcm = buf_save->lb_buf;
1726 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1727 GOTO(out, rc = -EINVAL);
1730 * Extract the mirror with specified mirror id, and store the splitted
1731 * mirror layout to the victim file.
1733 memset(buf, 0, sizeof(*buf));
1734 memset(buf_vic, 0, sizeof(*buf_vic));
1735 rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1739 rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1740 LU_XATTR_SPLIT, handle);
1743 rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
1744 LU_XATTR_SPLIT, handle);
1748 rc = mdd_trans_start(env, mdd, handle);
1752 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1757 rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
1760 GOTO(out_restore, rc);
1762 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1766 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1773 /* restore obj's layout */
1774 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1775 LU_XATTR_REPLACE, handle);
1777 CERROR("%s: failed to rollback of layout of: "DFID
1778 ": %d, file state unkonwn.\n",
1779 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1782 mdd_trans_stop(env, mdd, rc, handle);
1783 mdd_write_unlock(env, obj);
1784 mdd_write_unlock(env, vic);
1785 lu_buf_free(buf_save);
1787 lu_buf_free(buf_vic);
1790 (void) mdd_object_pfid_replace(env, obj);
1795 static int mdd_layout_merge_allowed(const struct lu_env *env,
1796 struct md_object *target,
1797 struct md_object *victim)
1799 struct mdd_object *o1 = md2mdd_obj(target);
1801 /* cannot extend directory's LOVEA */
1802 if (S_ISDIR(mdd_object_type(o1))) {
1803 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1804 mdd_obj_dev_name(o1));
1812 * The caller should guarantee to update the object ctime
1813 * after xattr_set if needed.
1815 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1816 const struct lu_buf *buf, const char *name,
1819 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1820 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1821 struct mdd_device *mdd = mdo2mdd(obj);
1822 struct thandle *handle;
1823 enum changelog_rec_type cl_type;
1828 rc = mdd_la_get(env, mdd_obj, attr);
1832 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1836 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1837 (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1838 struct md_rejig_data *mrd = buf->lb_buf;
1839 struct md_object *victim = mrd->mrd_obj;
1841 if (buf->lb_len != sizeof(*mrd))
1844 rc = mdd_layout_merge_allowed(env, obj, victim);
1848 if (fl == LU_XATTR_MERGE)
1849 /* merge layout of victim as a mirror of obj's. */
1850 rc = mdd_xattr_merge(env, obj, victim);
1852 rc = mdd_xattr_split(env, obj, mrd);
1856 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1857 rc = mdd_dir_layout_shrink(env, obj, buf);
1861 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1862 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1863 struct posix_acl *acl;
1865 /* user may set empty ACL, which should be treated as removing
1867 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1870 RETURN(PTR_ERR(acl));
1872 rc = mdd_xattr_del(env, obj, name);
1875 posix_acl_release(acl);
1878 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1879 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1883 handle = mdd_trans_create(env, mdd);
1885 RETURN(PTR_ERR(handle));
1887 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1891 rc = mdd_trans_start(env, mdd, handle);
1895 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1897 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1898 rc = mdd_hsm_update_locked(env, obj, buf, handle, &cl_flags);
1900 mdd_write_unlock(env, mdd_obj);
1905 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
1906 mdd_write_unlock(env, mdd_obj);
1910 cl_type = mdd_xattr_changelog_type(env, mdd, name);
1914 rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, cl_flags,
1915 mdd_obj, name, handle);
1919 return mdd_trans_stop(env, mdd, rc, handle);
1922 static int mdd_declare_xattr_del(const struct lu_env *env,
1923 struct mdd_device *mdd,
1924 struct mdd_object *obj,
1926 struct thandle *handle)
1928 enum changelog_rec_type type;
1931 rc = mdo_declare_xattr_del(env, obj, name, handle);
1935 type = mdd_xattr_changelog_type(env, mdd, name);
1937 return 0; /* no changelog to store */
1939 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1943 * The caller should guarantee to update the object ctime
1944 * after xattr_set if needed.
1946 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1949 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1950 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1951 struct mdd_device *mdd = mdo2mdd(obj);
1952 struct thandle *handle;
1956 rc = mdd_la_get(env, mdd_obj, attr);
1960 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1964 handle = mdd_trans_create(env, mdd);
1966 RETURN(PTR_ERR(handle));
1968 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1972 rc = mdd_trans_start(env, mdd, handle);
1976 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1977 rc = mdo_xattr_del(env, mdd_obj, name, handle);
1978 mdd_write_unlock(env, mdd_obj);
1982 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
1985 rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
1990 return mdd_trans_stop(env, mdd, rc, handle);
1994 * read lov/lmv EA of an object
1995 * return the lov/lmv EA in an allocated lu_buf
1997 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
1998 struct lu_buf *lmm_buf, const char *name)
2000 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
2005 if (buf->lb_buf == NULL) {
2006 buf = lu_buf_check_and_alloc(buf, 4096);
2007 if (buf->lb_buf == NULL)
2012 rc = mdo_xattr_get(env, obj, buf, name);
2013 if (rc == -ERANGE) {
2014 /* mti_big_buf is allocated but is too small
2015 * we need to increase it */
2016 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
2018 if (buf->lb_buf == NULL)
2021 } else if (rc < 0) {
2023 } else if (rc == 0) {
2027 lu_buf_alloc(lmm_buf, rc);
2028 if (lmm_buf->lb_buf == NULL)
2032 * we don't use lmm_buf directly, because we don't know xattr size, so
2033 * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
2035 memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
2040 static int mdd_xattr_hsm_replace(const struct lu_env *env,
2041 struct mdd_object *o, struct lu_buf *buf,
2042 struct thandle *handle)
2044 struct hsm_attrs *attrs;
2050 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
2055 attrs = buf->lb_buf;
2056 hsm_flags = le32_to_cpu(attrs->hsm_flags);
2057 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
2060 /* Add a changelog record for release. */
2061 hsm_set_cl_event(&flags, HE_RELEASE);
2062 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
2068 * check if layout swapping between 2 objects is allowed
2070 * - only normal FIDs or non-system IGIFs
2071 * - same type of objects
2072 * - same owner/group (so quotas are still valid) unless this is from HSM
2075 static int mdd_layout_swap_allowed(const struct lu_env *env,
2076 struct mdd_object *o1,
2077 const struct lu_attr *attr1,
2078 struct mdd_object *o2,
2079 const struct lu_attr *attr2,
2082 const struct lu_fid *fid1, *fid2;
2088 if (!fid_is_norm(fid1) &&
2089 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
2092 if (!fid_is_norm(fid2) &&
2093 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
2096 if (mdd_object_type(o1) != mdd_object_type(o2)) {
2097 if (S_ISDIR(mdd_object_type(o1)))
2099 if (S_ISREG(mdd_object_type(o1)))
2104 if (flags & SWAP_LAYOUTS_MDS_HSM)
2107 if ((attr1->la_uid != attr2->la_uid) ||
2108 (attr1->la_gid != attr2->la_gid))
2114 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
2115 * look into the layout in MDD layer. */
2116 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
2118 struct lov_comp_md_v1 *comp_v1;
2119 struct lov_mds_md *v1;
2123 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2124 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2125 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
2131 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
2132 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
2135 for (i = 0; i < le32_to_cpu(ent_count); i++) {
2136 off = le32_to_cpu(comp_v1->lcm_entries[i].
2138 v1 = (struct lov_mds_md *)((char *)comp_v1 +
2143 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2144 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2155 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2157 return mdd_lmm_oi(lmm, oi, true);
2160 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2162 return mdd_lmm_oi(lmm, oi, false);
2165 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
2167 struct lov_comp_md_v1 *comp_v1;
2169 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2170 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2172 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
2174 comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
2175 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2176 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2177 __u16 tmp_gen = *gen;
2179 *gen = le16_to_cpu(lmm->lmm_layout_gen);
2181 lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
2188 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2190 return mdd_lmm_gen(lmm, gen, true);
2193 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2195 return mdd_lmm_gen(lmm, gen, false);
2198 static int mdd_dom_data_truncate(const struct lu_env *env,
2199 struct mdd_device *mdd, struct mdd_object *mo)
2202 struct dt_object *dom;
2205 dom = dt_object_locate(mdd_object_child(mo), mdd->mdd_bottom);
2207 GOTO(out, rc = -ENODATA);
2209 th = dt_trans_create(env, mdd->mdd_bottom);
2211 GOTO(out, rc = PTR_ERR(th));
2213 rc = dt_declare_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2217 rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
2221 rc = dt_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2223 dt_trans_stop(env, mdd->mdd_bottom, th);
2225 /* Ignore failure but report the error */
2227 CERROR("%s: "DFID" can't truncate DOM inode data, rc = %d\n",
2228 mdd_obj_dev_name(mo), PFID(mdo2fid(mo)), rc);
2233 * swap layouts between 2 lustre objects
2235 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2236 struct md_object *obj2, __u64 flags)
2238 struct mdd_thread_info *info = mdd_env_info(env);
2239 struct mdd_object *fst_o = md2mdd_obj(obj1);
2240 struct mdd_object *snd_o = md2mdd_obj(obj2);
2241 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2242 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2243 struct mdd_device *mdd = mdo2mdd(obj1);
2244 struct lov_mds_md *fst_lmm, *snd_lmm;
2245 struct lu_buf *fst_buf = &info->mti_buf[0];
2246 struct lu_buf *snd_buf = &info->mti_buf[1];
2247 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2248 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2249 struct ost_id *saved_oi = NULL;
2250 struct thandle *handle;
2251 struct mdd_object *dom_o = NULL;
2252 __u64 domsize_dom, domsize_vlt;
2253 __u32 fst_gen, snd_gen, saved_gen;
2259 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
2260 memset(info->mti_buf, 0, sizeof(info->mti_buf));
2262 /* we have to sort the 2 obj, so locking will always
2263 * be in the same order, even in case of 2 concurrent swaps */
2264 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
2265 if (rc == 0) /* same fid ? */
2271 rc = mdd_la_get(env, fst_o, fst_la);
2275 rc = mdd_la_get(env, snd_o, snd_la);
2279 /* check if layout swapping is allowed */
2280 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2284 handle = mdd_trans_create(env, mdd);
2286 RETURN(PTR_ERR(handle));
2288 /* objects are already sorted */
2289 mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
2290 mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
2292 rc = mdd_stripe_get(env, fst_o, fst_buf, XATTR_NAME_LOV);
2293 if (rc < 0 && rc != -ENODATA)
2296 rc = mdd_stripe_get(env, snd_o, snd_buf, XATTR_NAME_LOV);
2297 if (rc < 0 && rc != -ENODATA)
2300 /* check if file has DoM. DoM file can be migrated only to another
2301 * DoM layout with the same DoM component size or to an non-DOM
2302 * layout. After migration to OSTs layout, local MDT inode data
2303 * should be truncated.
2304 * Objects are sorted by FIDs, considering that original file's FID
2305 * is always smaller the snd_o is always original file we are migrating
2308 domsize_dom = mdd_lmm_dom_size(snd_buf->lb_buf);
2309 domsize_vlt = mdd_lmm_dom_size(fst_buf->lb_buf);
2311 /* Only migration is supported for DoM files, not 'swap_layouts' so
2312 * target file must be volatile and orphan.
2314 if (fst_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2315 dom_o = domsize_dom ? snd_o : NULL;
2316 } else if (snd_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2317 swap(domsize_dom, domsize_vlt);
2318 dom_o = domsize_dom ? fst_o : NULL;
2319 } else if (domsize_dom > 0 || domsize_vlt > 0) {
2320 /* 'lfs swap_layouts' case, neither file should have DoM */
2322 CDEBUG(D_LAYOUT, "cannot swap layouts with DOM component, "
2323 "use migration instead: rc = %d\n", rc);
2327 if (domsize_vlt > 0 && domsize_dom == 0) {
2329 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": OST to DOM "
2330 "migration is not supported: rc = %d\n",
2331 PFID(mdo2fid(snd_o)), rc);
2333 } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
2335 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": new layout "
2336 "must have the same DoM component size: rc = %d\n",
2337 PFID(mdo2fid(fst_o)), rc);
2339 } else if (domsize_vlt > 0) {
2340 /* Migration with the same DOM component size, no need to
2341 * truncate local data, it is still being used */
2345 /* swapping 2 non existant layouts is a success */
2346 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2349 /* to help inode migration between MDT, it is better to
2350 * start by the no layout file (if one), so we order the swap */
2351 if (snd_buf->lb_buf == NULL) {
2353 swap(fst_buf, snd_buf);
2356 fst_gen = snd_gen = 0;
2357 /* lmm and generation layout initialization */
2358 if (fst_buf->lb_buf != NULL) {
2359 fst_lmm = fst_buf->lb_buf;
2360 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2361 fst_fl = LU_XATTR_REPLACE;
2365 fst_fl = LU_XATTR_CREATE;
2368 snd_lmm = snd_buf->lb_buf;
2369 mdd_get_lmm_gen(snd_lmm, &snd_gen);
2371 saved_gen = fst_gen;
2372 /* increase the generation layout numbers */
2377 * XXX The layout generation is used to generate component IDs for
2378 * the composite file, we have to do some special tweaks to make
2379 * sure the layout generation is always adequate for that job.
2382 /* Skip invalid generation number for composite layout */
2383 if ((snd_gen & LCME_ID_MASK) == 0)
2385 if ((fst_gen & LCME_ID_MASK) == 0)
2387 /* Make sure the generation is greater than all the component IDs */
2388 if (fst_gen < snd_gen)
2390 else if (fst_gen > snd_gen)
2393 /* set the file specific informations in lmm */
2394 if (fst_lmm != NULL) {
2395 saved_oi = &info->mti_oa.o_oi;
2396 mdd_get_lmm_oi(fst_lmm, saved_oi);
2397 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2398 mdd_set_lmm_oi(fst_lmm, &snd_lmm->lmm_oi);
2399 mdd_set_lmm_oi(snd_lmm, saved_oi);
2401 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2402 cpu_to_le32(LOV_MAGIC_MAGIC))
2403 snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2405 GOTO(stop, rc = -EPROTO);
2407 mdd_set_lmm_gen(snd_lmm, &fst_gen);
2409 /* Prepare HSM attribute if it's required */
2410 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2411 const int buflen = sizeof(struct hsm_attrs);
2413 lu_buf_alloc(fst_hsm_buf, buflen);
2414 lu_buf_alloc(snd_hsm_buf, buflen);
2415 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2416 GOTO(stop, rc = -ENOMEM);
2418 /* Read HSM attribute */
2419 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2423 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2427 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2428 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2433 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2434 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2440 /* prepare transaction */
2441 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2446 if (fst_buf->lb_buf != NULL)
2447 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2448 XATTR_NAME_LOV, LU_XATTR_REPLACE,
2451 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2456 rc = mdd_trans_start(env, mdd, handle);
2460 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2461 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2465 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2467 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2470 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
2471 mdd_obj_dev_name(fst_o),
2472 PFID(mdo2fid(fst_o)), rc, rc2);
2477 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2481 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2484 if (fst_buf->lb_buf != NULL)
2485 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2486 LU_XATTR_REPLACE, handle);
2488 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2491 GOTO(out_restore, rc);
2493 /* Issue one changelog record per file */
2494 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
2498 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
2507 /* failure on second file, but first was done, so we have
2508 * to roll back first. */
2509 if (fst_buf->lb_buf != NULL) {
2510 mdd_set_lmm_oi(fst_lmm, saved_oi);
2511 mdd_set_lmm_gen(fst_lmm, &saved_gen);
2512 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2513 LU_XATTR_REPLACE, handle);
2515 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2521 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2526 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2531 CERROR("%s: unable to roll back layout swap. FIDs: "
2532 DFID" and "DFID "error: %d/%d, steps: %d\n",
2533 mdd_obj_dev_name(fst_o),
2534 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
2536 /* a solution to avoid journal commit is to panic,
2537 * but it has strong consequences so we use LBUG to
2538 * allow sysdamin to choose to panic or not
2545 rc = mdd_trans_stop(env, mdd, rc, handle);
2547 /* Truncate local DOM data if all went well */
2549 mdd_dom_data_truncate(env, mdd, dom_o);
2551 mdd_write_unlock(env, snd_o);
2552 mdd_write_unlock(env, fst_o);
2554 lu_buf_free(fst_buf);
2555 lu_buf_free(snd_buf);
2556 lu_buf_free(fst_hsm_buf);
2557 lu_buf_free(snd_hsm_buf);
2560 (void) mdd_object_pfid_replace(env, fst_o);
2561 (void) mdd_object_pfid_replace(env, snd_o);
2566 static int mdd_declare_layout_change(const struct lu_env *env,
2567 struct mdd_device *mdd,
2568 struct mdd_object *obj,
2569 struct md_layout_change *mlc,
2570 struct thandle *handle)
2574 rc = mdo_declare_layout_change(env, obj, mlc, handle);
2578 return mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2582 /* For PFL, this is used to instantiate necessary component objects. */
2584 mdd_layout_instantiate_component(const struct lu_env *env,
2585 struct mdd_object *obj, struct md_layout_change *mlc,
2586 struct thandle *handle)
2588 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2592 if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2595 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2597 * It's possible that another layout write intent has already
2598 * instantiated our objects, so a -EALREADY returned, and we need to
2602 RETURN(rc == -EALREADY ? 0 : rc);
2604 rc = mdd_trans_start(env, mdd, handle);
2608 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2609 rc = mdo_layout_change(env, obj, mlc, handle);
2610 mdd_write_unlock(env, obj);
2614 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
2619 * Change the FLR layout from RDONLY to WRITE_PENDING.
2621 * It picks the primary mirror, and bumps the layout version, and set
2622 * layout version xattr to OST objects in a sync tx. In order to facilitate
2623 * the handling of phantom writers from evicted clients, the clients carry
2624 * layout version of the file with write RPC, so that the OSTs can verify
2625 * if the write RPCs are legitimate, meaning not from evicted clients.
2628 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2629 struct md_layout_change *mlc, struct thandle *handle)
2631 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2632 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2633 struct lustre_som_attrs *som = &mlc->mlc_som;
2638 /* Verify acceptable operations */
2639 switch (mlc->mlc_opc) {
2640 case MD_LAYOUT_WRITE:
2641 case MD_LAYOUT_RESYNC:
2642 /* these are legal operations - this represents the case that
2643 * a few mirrors were missed in the last resync. */
2645 case MD_LAYOUT_RESYNC_DONE:
2650 som_buf->lb_buf = som;
2651 som_buf->lb_len = sizeof(*som);
2652 rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
2653 if (rc < 0 && rc != -ENODATA)
2657 lustre_som_swab(som);
2658 if (som->lsa_valid & SOM_FL_STRICT)
2659 fl = LU_XATTR_REPLACE;
2662 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2667 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2668 XATTR_NAME_SOM, fl, handle);
2673 /* record a changelog for data mover to consume */
2674 rc = mdd_declare_changelog_store(env, mdd, CL_FLRW, NULL, NULL, handle);
2678 rc = mdd_trans_start(env, mdd, handle);
2682 /* it needs a sync tx to make FLR to work properly */
2683 handle->th_sync = 1;
2685 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2686 rc = mdo_layout_change(env, obj, mlc, handle);
2688 /* SOM state transition from STRICT to STALE */
2689 som->lsa_valid = SOM_FL_STALE;
2690 lustre_som_swab(som);
2691 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2694 mdd_write_unlock(env, obj);
2698 rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
2709 * Handle mirrored file state transition when it's in WRITE_PENDING.
2711 * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2712 * the file is in WRITE_PENDING state. If everything goes fine, the file's
2713 * layout version will be increased, and the file's state will be changed to
2717 mdd_layout_update_write_pending(const struct lu_env *env,
2718 struct mdd_object *obj, struct md_layout_change *mlc,
2719 struct thandle *handle)
2721 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2725 switch (mlc->mlc_opc) {
2726 case MD_LAYOUT_RESYNC:
2727 /* Upon receiving the resync request, it should
2728 * instantiate all stale components right away to get ready
2729 * for mirror copy. In order to avoid layout version change,
2730 * client should avoid sending LAYOUT_WRITE request at the
2733 case MD_LAYOUT_WRITE:
2734 /* legal race for concurrent write, the file state has been
2735 * changed by another client. */
2741 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2745 rc = mdd_trans_start(env, mdd, handle);
2749 /* it needs a sync tx to make FLR to work properly */
2750 handle->th_sync = 1;
2752 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2753 rc = mdo_layout_change(env, obj, mlc, handle);
2754 mdd_write_unlock(env, obj);
2765 * Handle the requests when a FLR file's state is in SYNC_PENDING.
2767 * Only concurrent write and sync complete requests are possible when the
2768 * file is in SYNC_PENDING. For the latter request, it will pass in the
2769 * mirrors that have been synchronized, then the stale bit will be cleared
2770 * to make the file's state turn into RDONLY.
2771 * For concurrent write reqeust, it just needs to change the file's state
2772 * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2773 * version because the version will be increased in the transition to
2774 * SYNC_PENDING later so that it can deny the write request from potential
2775 * evicted SYNC clients. */
2777 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2778 struct md_layout_change *mlc, struct thandle *handle)
2780 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2781 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2786 /* operation validation */
2787 switch (mlc->mlc_opc) {
2788 case MD_LAYOUT_RESYNC_DONE:
2789 /* resync complete. */
2790 case MD_LAYOUT_WRITE:
2791 /* concurrent write. */
2793 case MD_LAYOUT_RESYNC:
2794 /* resync again, most likely the previous run failed.
2795 * no-op if it's already in SYNC_PENDING state */
2801 if (mlc->mlc_som.lsa_valid & SOM_FL_STRICT) {
2802 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2803 if (rc < 0 && rc != -ENODATA)
2806 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2807 lustre_som_swab(&mlc->mlc_som);
2808 som_buf->lb_buf = &mlc->mlc_som;
2809 som_buf->lb_len = sizeof(mlc->mlc_som);
2812 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2816 /* record a changelog for the completion of resync */
2817 rc = mdd_declare_changelog_store(env, mdd, CL_RESYNC, NULL, NULL,
2822 /* RESYNC_DONE has piggybacked size and blocks */
2824 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2825 XATTR_NAME_SOM, fl, handle);
2830 rc = mdd_trans_start(env, mdd, handle);
2834 /* it needs a sync tx to make FLR to work properly */
2835 handle->th_sync = 1;
2837 rc = mdo_layout_change(env, obj, mlc, handle);
2842 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2848 rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
2857 * Layout change callback for object.
2859 * This is only used by FLR for now. In the future, it can be exteneded to
2860 * handle all layout change.
2863 mdd_layout_change(const struct lu_env *env, struct md_object *o,
2864 struct md_layout_change *mlc)
2866 struct mdd_object *obj = md2mdd_obj(o);
2867 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2868 struct lu_buf *buf = mdd_buf_get(env, NULL, 0);
2869 struct lov_comp_md_v1 *lcm;
2870 struct thandle *handle;
2875 /* Verify acceptable operations */
2876 switch (mlc->mlc_opc) {
2877 case MD_LAYOUT_WRITE:
2878 case MD_LAYOUT_RESYNC:
2879 case MD_LAYOUT_RESYNC_DONE:
2885 handle = mdd_trans_create(env, mdd);
2887 RETURN(PTR_ERR(handle));
2889 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
2896 /* analyze the layout to make sure it's a FLR file */
2898 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2899 GOTO(out, rc = -EINVAL);
2901 flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
2903 /* please refer to HLD of FLR for state transition */
2904 switch (flr_state) {
2906 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
2908 case LCM_FL_WRITE_PENDING:
2909 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
2912 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
2914 case LCM_FL_SYNC_PENDING:
2915 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
2924 mdd_trans_stop(env, mdd, rc, handle);
2929 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
2930 struct mdd_object *child, const struct lu_attr *attr,
2931 const struct md_op_spec *spec,
2932 struct dt_allocation_hint *hint)
2934 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
2935 struct dt_object *nc = mdd_object_child(child);
2937 memset(hint, 0, sizeof(*hint));
2939 /* For striped directory, give striping EA to lod_ah_init, which will
2940 * decide the stripe_offset and stripe count by it. */
2941 if (S_ISDIR(attr->la_mode) &&
2942 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2943 hint->dah_eadata = spec->u.sp_ea.eadata;
2944 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
2946 hint->dah_eadata = NULL;
2947 hint->dah_eadata_len = 0;
2950 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
2951 hint->dah_eadata, hint->dah_eadata_len);
2952 /* @hint will be initialized by underlying device. */
2953 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
2957 * do NOT or the MAY_*'s, you'll get the weakest
2959 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
2963 /* Sadly, NFSD reopens a file repeatedly during operation, so the
2964 * "acc_mode = 0" allowance for newly-created files isn't honoured.
2965 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
2966 * owner can write to a file even if it is marked readonly to hide
2967 * its brokenness. (bug 5781) */
2968 if (flags & MDS_OPEN_OWNEROVERRIDE) {
2969 struct lu_ucred *uc = lu_ucred_check(env);
2971 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
2975 if (flags & MDS_FMODE_READ)
2977 if (flags & (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
2979 if (flags & MDS_FMODE_EXEC)
2984 static int mdd_open_sanity_check(const struct lu_env *env,
2985 struct mdd_object *obj,
2986 const struct lu_attr *attr, int flag)
2992 if (mdd_is_dead_obj(obj))
2995 if (S_ISLNK(attr->la_mode))
2998 mode = accmode(env, attr, flag);
3000 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
3003 if (!(flag & MDS_OPEN_CREATED)) {
3004 rc = mdd_permission_internal(env, obj, attr, mode);
3009 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
3010 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
3011 flag &= ~MDS_OPEN_TRUNC;
3013 /* For writing append-only file must open it with append mode. */
3014 if (attr->la_flags & LUSTRE_APPEND_FL) {
3015 if ((flag & MDS_FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
3017 if (flag & MDS_OPEN_TRUNC)
3024 static int mdd_open(const struct lu_env *env, struct md_object *obj,
3027 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3028 struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
3029 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3030 struct mdd_object_user *mou = NULL;
3031 const struct lu_ucred *uc = lu_ucred(env);
3032 struct mdd_device *mdd = mdo2mdd(obj);
3033 enum changelog_rec_type type = CL_OPEN;
3036 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3038 rc = mdd_la_get(env, mdd_obj, attr);
3042 rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
3043 if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & (1 << CL_DN_OPEN)))
3048 mdd_obj->mod_count++;
3050 if (!mdd_changelog_enabled(env, mdd, type))
3054 /* look for existing opener in list under mdd_write_lock */
3055 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, flags);
3060 /* add user to list */
3061 mou = mdd_obj_user_alloc(flags, uc->uc_uid, uc->uc_gid);
3067 rc2 = mdd_obj_user_add(mdd_obj, mou, type == CL_DN_OPEN);
3069 mdd_obj_user_free(mou);
3074 if (type == CL_DN_OPEN) {
3075 if (ktime_before(ktime_get(), mou->mou_deniednext))
3076 /* same user denied again same access within
3077 * time interval: do not record
3081 /* this user already denied, but some time ago:
3082 * update denied time
3084 mou->mou_deniednext =
3085 ktime_add(ktime_get(),
3086 ktime_set(mdd->mdd_cl.mc_deniednext,
3089 mou->mou_opencount++;
3090 /* same user opening file again with same flags:
3097 mdd_changelog(env, type, flags, md_dev, mdo2fid(mdd_obj));
3101 mdd_write_unlock(env, mdd_obj);
3105 static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
3106 struct md_attr *ma, struct thandle *handle)
3110 rc = mdd_orphan_declare_delete(env, obj, handle);
3114 return mdo_declare_destroy(env, obj, handle);
3118 * No permission check is needed.
3120 static int mdd_close(const struct lu_env *env, struct md_object *obj,
3121 struct md_attr *ma, int mode)
3123 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3124 struct mdd_device *mdd = mdo2mdd(obj);
3125 struct thandle *handle = NULL;
3128 bool blocked = false;
3129 int last_close_by_uid = 0;
3130 const struct lu_ucred *uc = lu_ucred(env);
3133 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
3134 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3135 mdd_obj->mod_count--;
3136 mdd_write_unlock(env, mdd_obj);
3138 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
3139 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
3140 "list\n", PFID(mdd_object_fid(mdd_obj)));
3144 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
3145 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
3146 /* check without any lock */
3147 is_orphan = mdd_obj->mod_count == 1 &&
3148 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
3152 /* mdd_trans_create() maybe failed because of barrier_entry(),
3153 * under such case, the orphan MDT-object will be left in the
3154 * orphan list, and when the MDT remount next time, the unused
3155 * orphans will be destroyed automatically.
3157 * One exception: the former mdd_finish_unlink may failed to
3158 * add the orphan MDT-object to the orphan list, then if the
3159 * mdd_trans_create() failed because of barrier_entry(), the
3160 * MDT-object will become real orphan that is neither in the
3161 * namespace nor in the orphan list. Such bad case should be
3162 * very rare and will be handled by e2fsck/lfsck. */
3163 handle = mdd_trans_create(env, mdo2mdd(obj));
3164 if (IS_ERR(handle)) {
3165 rc = PTR_ERR(handle);
3166 if (rc != -EINPROGRESS)
3174 rc = mdd_declare_close(env, mdd_obj, ma, handle);
3178 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE, NULL, NULL,
3183 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3189 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3190 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
3192 CERROR("%s: failed to get lu_attr of "DFID": rc = %d\n",
3193 lu_dev_name(mdd2lu_dev(mdd)),
3194 PFID(mdd_object_fid(mdd_obj)), rc);
3198 /* check again with lock */
3199 is_orphan = (mdd_obj->mod_count == 1) &&
3200 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
3201 ma->ma_attr.la_nlink == 0);
3203 if (is_orphan && !handle && !blocked) {
3204 mdd_write_unlock(env, mdd_obj);
3208 mdd_obj->mod_count--; /*release open count */
3210 /* under mdd write lock */
3211 /* If recording, see if we need to remove UID from list */
3212 if (mdd_changelog_enabled(env, mdd, CL_OPEN)) {
3213 struct mdd_object_user *mou;
3215 /* look for UID in list */
3216 /* If mou is NULL, it probably means logging was enabled after
3217 * the user had the file open. So the corresponding close
3218 * will not be logged.
3220 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, mode);
3222 mou->mou_opencount--;
3223 if (mou->mou_opencount == 0) {
3224 mdd_obj_user_remove(mdd_obj, mou);
3225 last_close_by_uid = 1;
3230 if (!is_orphan || blocked)
3234 /* NB: Object maybe not in orphan list originally, it is rare case for
3235 * mdd_finish_unlink() failure, in that case, the object doesn't have
3236 * ORPHAN_OBJ flag */
3237 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
3238 /* remove link to object from orphan index */
3239 LASSERT(handle != NULL);
3240 rc = mdd_orphan_delete(env, mdd_obj, handle);
3242 CERROR("%s: unable to delete "DFID" from orphan list: "
3243 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3244 PFID(mdd_object_fid(mdd_obj)), rc);
3245 /* If object was not deleted from orphan list, do not
3246 * destroy OSS objects, which will be done when next
3251 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
3252 "list, OSS objects to be destroyed.\n",
3253 PFID(mdd_object_fid(mdd_obj)));
3256 rc = mdo_destroy(env, mdd_obj, handle);
3259 CERROR("%s: unable to delete "DFID" from orphan list: "
3260 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3261 PFID(mdd_object_fid(mdd_obj)), rc);
3266 mdd_write_unlock(env, mdd_obj);
3268 if (rc != 0 || blocked ||
3269 !mdd_changelog_enabled(env, mdd, CL_CLOSE))
3272 /* Record CL_CLOSE in changelog only if file was opened in write mode,
3273 * or if CL_OPEN was recorded and it's last close by user.
3274 * Changelogs mask may change between open and close operations, but
3275 * this is not a big deal if we have a CL_CLOSE entry with no matching
3276 * CL_OPEN. Plus Changelogs mask may not change often.
3278 if (((!(mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) &&
3279 (mode & (MDS_FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC))) ||
3280 ((mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) && last_close_by_uid)) &&
3281 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
3282 if (handle == NULL) {
3283 handle = mdd_trans_create(env, mdo2mdd(obj));
3285 GOTO(stop, rc = PTR_ERR(handle));
3287 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE,
3288 NULL, NULL, handle);
3292 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3297 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
3302 if (handle != NULL && !IS_ERR(handle))
3303 rc = mdd_trans_stop(env, mdd, rc, handle);
3309 * Permission check is done when open,
3310 * no need check again.
3312 static int mdd_readpage_sanity_check(const struct lu_env *env,
3313 struct mdd_object *obj)
3315 struct dt_object *next = mdd_object_child(obj);
3319 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
3327 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
3328 size_t nob, const struct dt_it_ops *iops,
3329 struct dt_it *it, __u32 attr, void *arg)
3331 struct lu_dirpage *dp = &lp->lp_dir;
3335 struct lu_dirent *ent;
3336 struct lu_dirent *last = NULL;
3340 if (nob < sizeof(*dp))
3343 memset(area, 0, sizeof (*dp));
3344 area += sizeof (*dp);
3345 nob -= sizeof (*dp);
3352 len = iops->key_size(env, it);
3354 /* IAM iterator can return record with zero len. */
3358 hash = iops->store(env, it);
3359 if (unlikely(first)) {
3361 dp->ldp_hash_start = cpu_to_le64(hash);
3364 /* calculate max space required for lu_dirent */
3365 recsize = lu_dirent_calc_size(len, attr);
3367 if (nob >= recsize) {
3368 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
3369 if (result == -ESTALE)
3374 /* osd might not able to pack all attributes,
3375 * so recheck rec length */
3376 recsize = le16_to_cpu(ent->lde_reclen);
3378 if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
3379 fid_le_to_cpu(&fid, &ent->lde_fid);
3380 if (fid_is_dot_lustre(&fid))
3384 result = (last != NULL) ? 0 :-EINVAL;
3388 ent = (void *)ent + recsize;
3392 result = iops->next(env, it);
3393 if (result == -ESTALE)
3395 } while (result == 0);
3398 dp->ldp_hash_end = cpu_to_le64(hash);
3400 if (last->lde_hash == dp->ldp_hash_end)
3401 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
3402 last->lde_reclen = 0; /* end mark */
3405 /* end of directory */
3406 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3407 else if (result < 0)
3408 CWARN("build page failed: %d!\n", result);
3412 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
3413 const struct lu_rdpg *rdpg)
3415 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3419 if (mdd_object_exists(mdd_obj) == 0) {
3420 CERROR("%s: object "DFID" not found: rc = -2\n",
3421 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
3425 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
3426 rc = mdd_readpage_sanity_check(env, mdd_obj);
3428 GOTO(out_unlock, rc);
3430 if (mdd_is_dead_obj(mdd_obj)) {
3432 struct lu_dirpage *dp;
3435 * According to POSIX, please do not return any entry to client:
3436 * even dot and dotdot should not be returned.
3438 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
3439 PFID(mdd_object_fid(mdd_obj)));
3441 if (rdpg->rp_count <= 0)
3442 GOTO(out_unlock, rc = -EFAULT);
3443 LASSERT(rdpg->rp_pages != NULL);
3445 pg = rdpg->rp_pages[0];
3446 dp = (struct lu_dirpage *)kmap(pg);
3447 memset(dp, 0 , sizeof(struct lu_dirpage));
3448 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3449 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3450 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3452 GOTO(out_unlock, rc = LU_PAGE_SIZE);
3455 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
3456 mdd_dir_page_build, NULL);
3458 struct lu_dirpage *dp;
3460 dp = kmap(rdpg->rp_pages[0]);
3461 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3464 * No pages were processed, mark this for first page
3467 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3468 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3469 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
3471 kunmap(rdpg->rp_pages[0]);
3474 GOTO(out_unlock, rc);
3476 mdd_read_unlock(env, mdd_obj);
3480 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
3482 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3484 if (mdd_object_exists(mdd_obj) == 0) {
3487 CERROR("%s: object "DFID" not found: rc = %d\n",
3488 mdd_obj_dev_name(mdd_obj),
3489 PFID(mdd_object_fid(mdd_obj)), rc);
3492 return dt_object_sync(env, mdd_object_child(mdd_obj),
3496 static int mdd_object_lock(const struct lu_env *env,
3497 struct md_object *obj,
3498 struct lustre_handle *lh,
3499 struct ldlm_enqueue_info *einfo,
3500 union ldlm_policy_data *policy)
3502 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3503 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
3507 static int mdd_object_unlock(const struct lu_env *env,
3508 struct md_object *obj,
3509 struct ldlm_enqueue_info *einfo,
3510 union ldlm_policy_data *policy)
3512 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3513 return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
3516 const struct md_object_operations mdd_obj_ops = {
3517 .moo_permission = mdd_permission,
3518 .moo_attr_get = mdd_attr_get,
3519 .moo_attr_set = mdd_attr_set,
3520 .moo_xattr_get = mdd_xattr_get,
3521 .moo_xattr_set = mdd_xattr_set,
3522 .moo_xattr_list = mdd_xattr_list,
3523 .moo_invalidate = mdd_invalidate,
3524 .moo_xattr_del = mdd_xattr_del,
3525 .moo_swap_layouts = mdd_swap_layouts,
3526 .moo_open = mdd_open,
3527 .moo_close = mdd_close,
3528 .moo_readpage = mdd_readpage,
3529 .moo_readlink = mdd_readlink,
3530 .moo_changelog = mdd_changelog,
3531 .moo_object_sync = mdd_object_sync,
3532 .moo_object_lock = mdd_object_lock,
3533 .moo_object_unlock = mdd_object_unlock,
3534 .moo_layout_change = mdd_layout_change,