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 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
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 enum changelog_rec_flags clf_flags,
74 const struct lu_fid *fid,
75 const char *xattr_name,
76 struct thandle *handle);
78 static inline bool has_prefix(const char *str, const char *prefix);
81 static u32 flags_helper(u64 open_flags)
85 if (open_flags & MDS_FMODE_EXEC) {
86 open_mode = MDS_FMODE_EXEC;
88 if (open_flags & MDS_FMODE_READ)
89 open_mode = MDS_FMODE_READ;
91 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
92 open_mode |= MDS_FMODE_WRITE;
98 /** Allocate/init a user and its sub-structures.
103 * \retval mou [OUT] success valid structure
106 static struct mdd_object_user *mdd_obj_user_alloc(u64 open_flags,
107 uid_t uid, gid_t gid)
109 struct mdd_object_user *mou;
113 OBD_SLAB_ALLOC_PTR(mou, mdd_object_kmem);
115 RETURN(ERR_PTR(-ENOMEM));
117 mou->mou_open_flags = open_flags;
118 mou->mou_uidgid = ((__u64)uid << 32) | gid;
119 mou->mou_opencount = 0;
120 mou->mou_deniednext = ktime_set(0, 0);
126 * Free a user and its sub-structures.
128 * \param mou [IN] user to be freed.
130 static void mdd_obj_user_free(struct mdd_object_user *mou)
132 OBD_SLAB_FREE_PTR(mou, mdd_object_kmem);
136 * Find if UID/GID already has this file open
138 * Caller should have write-locked \param mdd_obj.
139 * \param mdd_obj [IN] mdd_obj
140 * \param uid [IN] client uid
141 * \param gid [IN] client gid
142 * \retval user pointer or NULL if not found
145 struct mdd_object_user *mdd_obj_user_find(struct mdd_object *mdd_obj,
146 uid_t uid, gid_t gid,
149 struct mdd_object_user *mou;
154 uidgid = ((__u64)uid << 32) | gid;
155 list_for_each_entry(mou, &mdd_obj->mod_users, mou_list) {
156 if (mou->mou_uidgid == uidgid &&
157 flags_helper(mou->mou_open_flags) ==
158 flags_helper(open_flags))
165 * Add a user to the list of openers for this file
167 * Caller should have write-locked \param mdd_obj.
168 * \param mdd_obj [IN] mdd_obj
169 * \param mou [IN] user
171 * \retval -ve failure
173 static int mdd_obj_user_add(struct mdd_object *mdd_obj,
174 struct mdd_object_user *mou,
177 struct mdd_device *mdd = mdd_obj2mdd_dev(mdd_obj);
178 struct mdd_object_user *tmp;
179 __u32 uid = mou->mou_uidgid >> 32;
180 __u32 gid = mou->mou_uidgid & ((1UL << 32) - 1);
183 tmp = mdd_obj_user_find(mdd_obj, uid, gid, mou->mou_open_flags);
187 list_add_tail(&mou->mou_list, &mdd_obj->mod_users);
190 /* next 'access denied' notification cannot happen before
193 mou->mou_deniednext =
194 ktime_add(ktime_get(),
195 ktime_set(mdd->mdd_cl.mc_deniednext, 0));
197 mou->mou_opencount++;
202 * Remove UID from the list
204 * Caller should have write-locked \param mdd_obj.
205 * \param mdd_obj [IN] mdd_obj
206 * \param uid [IN] user
207 * \retval -ve failure
209 static int mdd_obj_user_remove(struct mdd_object *mdd_obj,
210 struct mdd_object_user *mou)
217 list_del_init(&mou->mou_list);
219 mdd_obj_user_free(mou);
223 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
228 if (mdd_object_exists(obj) == 0)
231 rc = mdo_attr_get(env, obj, la);
232 if (unlikely(rc != 0)) {
234 obj->mod_flags |= DEAD_OBJ;
238 if (la->la_valid & LA_FLAGS &&
239 la->la_flags & LUSTRE_ORPHAN_FL)
240 obj->mod_flags |= ORPHAN_OBJ | DEAD_OBJ;
245 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
247 struct mdd_thread_info *info;
249 lu_env_refill((struct lu_env *)env);
250 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
251 LASSERT(info != NULL);
255 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
259 buf = &mdd_env_info(env)->mti_buf[0];
265 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
266 const void *area, ssize_t len)
270 buf = &mdd_env_info(env)->mti_buf[0];
271 buf->lb_buf = (void *)area;
276 struct lu_object *mdd_object_alloc(const struct lu_env *env,
277 const struct lu_object_header *hdr,
280 struct mdd_object *mdd_obj;
283 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
287 o = mdd2lu_obj(mdd_obj);
288 lu_object_init(o, NULL, d);
289 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
290 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
291 mdd_obj->mod_count = 0;
292 o->lo_ops = &mdd_lu_obj_ops;
293 INIT_LIST_HEAD(&mdd_obj->mod_users);
298 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
299 const struct lu_object_conf *unused)
301 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
302 struct mdd_object *mdd_obj = lu2mdd_obj(o);
303 struct lu_object *below;
304 struct lu_device *under;
307 mdd_obj->mod_cltime = ktime_set(0, 0);
308 under = &d->mdd_child->dd_lu_dev;
309 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
311 RETURN(PTR_ERR(below));
313 lu_object_add(o, below);
318 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
322 if (lu_object_exists(o)) {
323 struct mdd_object *mdd_obj = lu2mdd_obj(o);
324 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
326 rc = mdd_la_get(env, mdd_obj, attr);
332 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
334 struct mdd_object *mdd = lu2mdd_obj(o);
335 struct mdd_object_user *mou, *tmp2;
338 list_for_each_entry_safe(mou, tmp2, &mdd->mod_users, mou_list) {
339 list_del(&mou->mou_list);
340 mdd_obj_user_free(mou);
344 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
347 static int mdd_object_print(const struct lu_env *env, void *cookie,
348 lu_printer_t p, const struct lu_object *o)
350 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
352 return (*p)(env, cookie,
353 LUSTRE_MDD_NAME"-object@%p(open_count=%d, valid=%x, cltime=%lldns, flags=%lx)",
354 mdd, mdd->mod_count, mdd->mod_valid,
355 ktime_to_ns(mdd->mod_cltime), mdd->mod_flags);
358 static const struct lu_object_operations mdd_lu_obj_ops = {
359 .loo_object_init = mdd_object_init,
360 .loo_object_start = mdd_object_start,
361 .loo_object_free = mdd_object_free,
362 .loo_object_print = mdd_object_print,
365 struct mdd_object *mdd_object_find(const struct lu_env *env,
366 struct mdd_device *d,
367 const struct lu_fid *f)
369 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
373 * No permission check is needed.
375 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
378 struct mdd_object *mdd_obj = md2mdd_obj(obj);
383 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
384 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
385 ma->ma_attr.la_nlink = 0;
391 * No permission check is needed.
393 static int mdd_xattr_get(const struct lu_env *env,
394 struct md_object *obj, struct lu_buf *buf,
397 struct mdd_object *mdd_obj = md2mdd_obj(obj);
398 struct mdd_device *mdd;
403 if (mdd_object_exists(mdd_obj) == 0) {
404 CERROR("%s: object "DFID" not found: rc = -2\n",
405 mdd_obj_dev_name(mdd_obj),
406 PFID(mdd_object_fid(mdd_obj)));
410 /* If the object has been destroyed, then do not get LMVEA, because
411 * it needs to load stripes from the iteration of the master object,
412 * and it will cause problem if master object has been destroyed, see
414 if (unlikely((mdd_obj->mod_flags & DEAD_OBJ) &&
415 !(mdd_obj->mod_flags & ORPHAN_OBJ) &&
416 strcmp(name, XATTR_NAME_LMV) == 0))
419 /* If the object has been delete from the namespace, then
420 * get linkEA should return -ENOENT as well */
421 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
422 strcmp(name, XATTR_NAME_LINK) == 0))
425 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
426 rc = mdo_xattr_get(env, mdd_obj, buf, name);
427 mdd_read_unlock(env, mdd_obj);
431 /* record only getting user xattrs and acls */
433 mdd_changelog_enabled(env, mdd, CL_GETXATTR) &&
434 (has_prefix(name, XATTR_USER_PREFIX) ||
435 has_prefix(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
436 has_prefix(name, XATTR_NAME_POSIX_ACL_DEFAULT))) {
437 struct thandle *handle;
440 LASSERT(mdo2fid(mdd_obj) != NULL);
442 handle = mdd_trans_create(env, mdd);
444 RETURN(PTR_ERR(handle));
446 rc2 = mdd_declare_changelog_store(env, mdd, CL_GETXATTR, NULL,
451 rc2 = mdd_trans_start(env, mdd, handle);
455 rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
456 mdo2fid(mdd_obj), name,
460 rc2 = mdd_trans_stop(env, mdd, rc2, handle);
469 * Permission check is done when open,
470 * no need check again.
472 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
475 struct mdd_object *mdd_obj = md2mdd_obj(obj);
476 struct dt_object *next;
481 if (mdd_object_exists(mdd_obj) == 0) {
482 CERROR("%s: object "DFID" not found: rc = -2\n",
483 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
487 next = mdd_object_child(mdd_obj);
488 LASSERT(next != NULL);
489 LASSERT(next->do_body_ops != NULL);
490 LASSERT(next->do_body_ops->dbo_read != NULL);
491 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
492 rc = dt_read(env, next, buf, &pos);
493 mdd_read_unlock(env, mdd_obj);
498 * No permission check is needed.
500 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
503 struct mdd_object *mdd_obj = md2mdd_obj(obj);
508 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
509 rc = mdo_xattr_list(env, mdd_obj, buf);
510 mdd_read_unlock(env, mdd_obj);
512 /* If the buffer is NULL then we are only here to get the
513 * length of the xattr name list. */
514 if (rc < 0 || buf->lb_buf == NULL)
518 * Filter out XATTR_NAME_LINK if this is an orphan object. See
521 if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
522 char *end = (char *)buf->lb_buf + rc;
523 char *p = buf->lb_buf;
526 char *next = p + strlen(p) + 1;
528 if (strcmp(p, XATTR_NAME_LINK) == 0) {
530 memmove(p, next, end - next);
532 CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
533 " of orphan "DFID"\n",
534 PFID(mdd_object_fid(mdd_obj)));
545 int mdd_invalidate(const struct lu_env *env, struct md_object *obj)
547 return mdo_invalidate(env, md2mdd_obj(obj));
550 int mdd_declare_create_object_internal(const struct lu_env *env,
551 struct mdd_object *p,
552 struct mdd_object *c,
553 struct lu_attr *attr,
554 struct thandle *handle,
555 const struct md_op_spec *spec,
556 struct dt_allocation_hint *hint)
558 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
559 const struct dt_index_features *feat = spec->sp_feat;
563 if (feat != &dt_directory_features && feat != NULL) {
564 dof->dof_type = DFT_INDEX;
565 dof->u.dof_idx.di_feat = feat;
567 dof->dof_type = dt_mode_to_dft(attr->la_mode);
568 if (dof->dof_type == DFT_REGULAR) {
569 dof->u.dof_reg.striped =
570 md_should_create(spec->sp_cr_flags);
571 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
572 dof->u.dof_reg.striped = 0;
573 /* is this replay? */
575 dof->u.dof_reg.striped = 0;
579 rc = mdo_declare_create_object(env, c, attr, hint, dof, handle);
584 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
585 struct mdd_object *c, struct lu_attr *attr,
586 struct thandle *handle,
587 const struct md_op_spec *spec,
588 struct dt_allocation_hint *hint)
590 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
594 LASSERT(!mdd_object_exists(c));
596 rc = mdo_create_object(env, c, attr, hint, dof, handle);
601 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
602 const struct lu_attr *attr, struct thandle *handle,
608 rc = mdo_attr_set(env, obj, attr, handle);
609 #ifdef CONFIG_FS_POSIX_ACL
610 if (!rc && (attr->la_valid & LA_MODE) && needacl)
611 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
616 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
617 const struct lu_attr *oattr, struct lu_attr *attr,
618 struct thandle *handle)
623 LASSERT(attr->la_valid & LA_CTIME);
624 LASSERT(oattr != NULL);
626 /* Make sure the ctime is increased only, however, it's not strictly
627 * reliable at here because there is not guarantee to hold lock on
628 * object, so we just bypass some unnecessary cmtime setting first
629 * and OSD has to check it again. */
630 if (attr->la_ctime < oattr->la_ctime)
631 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
632 else if (attr->la_valid == LA_CTIME &&
633 attr->la_ctime == oattr->la_ctime)
634 attr->la_valid &= ~LA_CTIME;
636 if (attr->la_valid != 0)
637 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
642 * This gives the same functionality as the code between
643 * sys_chmod and inode_setattr
644 * chown_common and inode_setattr
645 * utimes and inode_setattr
646 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
648 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
649 const struct lu_attr *oattr, struct lu_attr *la,
650 const unsigned long flags)
659 /* Do not permit change file type */
660 if (la->la_valid & LA_TYPE)
663 /* They should not be processed by setattr */
664 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
667 LASSERT(oattr != NULL);
669 uc = lu_ucred_check(env);
673 if (la->la_valid == LA_CTIME) {
674 if (!(flags & MDS_PERM_BYPASS))
675 /* This is only for set ctime when rename's source is
677 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
679 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
680 la->la_valid &= ~LA_CTIME;
684 if (la->la_valid == LA_ATIME) {
685 /* This is an atime-only attribute update for close RPCs. */
686 if (la->la_atime < (oattr->la_atime +
687 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
688 la->la_valid &= ~LA_ATIME;
692 /* Check if flags change. */
693 if (la->la_valid & LA_FLAGS) {
694 unsigned int oldflags = oattr->la_flags &
695 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
696 unsigned int newflags = la->la_flags &
697 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
699 if ((uc->uc_fsuid != oattr->la_uid) &&
700 !md_capable(uc, CFS_CAP_FOWNER))
703 /* The IMMUTABLE and APPEND_ONLY flags can
704 * only be changed by the relevant capability. */
705 if ((oldflags ^ newflags) &&
706 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
709 if (!S_ISDIR(oattr->la_mode))
710 la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
713 if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
714 (la->la_valid & ~LA_FLAGS) &&
715 !(flags & MDS_PERM_BYPASS))
718 /* Check for setting the obj time. */
719 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
720 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
721 if ((uc->uc_fsuid != oattr->la_uid) &&
722 !md_capable(uc, CFS_CAP_FOWNER)) {
723 rc = mdd_permission_internal(env, obj, oattr,
730 if (la->la_valid & LA_KILL_SUID) {
731 la->la_valid &= ~LA_KILL_SUID;
732 if ((oattr->la_mode & S_ISUID) &&
733 !(la->la_valid & LA_MODE)) {
734 la->la_mode = oattr->la_mode;
735 la->la_valid |= LA_MODE;
737 la->la_mode &= ~S_ISUID;
740 if (la->la_valid & LA_KILL_SGID) {
741 la->la_valid &= ~LA_KILL_SGID;
742 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
743 (S_ISGID | S_IXGRP)) &&
744 !(la->la_valid & LA_MODE)) {
745 la->la_mode = oattr->la_mode;
746 la->la_valid |= LA_MODE;
748 la->la_mode &= ~S_ISGID;
751 /* Make sure a caller can chmod. */
752 if (la->la_valid & LA_MODE) {
753 if (!(flags & MDS_PERM_BYPASS) &&
754 (uc->uc_fsuid != oattr->la_uid) &&
755 !md_capable(uc, CFS_CAP_FOWNER))
758 if (la->la_mode == (umode_t) -1)
759 la->la_mode = oattr->la_mode;
761 la->la_mode = (la->la_mode & S_IALLUGO) |
762 (oattr->la_mode & ~S_IALLUGO);
764 /* Also check the setgid bit! */
765 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
766 la->la_gid : oattr->la_gid) &&
767 !md_capable(uc, CFS_CAP_FSETID))
768 la->la_mode &= ~S_ISGID;
770 la->la_mode = oattr->la_mode;
773 /* Make sure a caller can chown. */
774 if (la->la_valid & LA_UID) {
775 if (la->la_uid == (uid_t) -1)
776 la->la_uid = oattr->la_uid;
777 if (((uc->uc_fsuid != oattr->la_uid) ||
778 (la->la_uid != oattr->la_uid)) &&
779 !md_capable(uc, CFS_CAP_CHOWN))
782 /* If the user or group of a non-directory has been
783 * changed by a non-root user, remove the setuid bit.
784 * 19981026 David C Niemi <niemi@tux.org>
786 * Changed this to apply to all users, including root,
787 * to avoid some races. This is the behavior we had in
788 * 2.0. The check for non-root was definitely wrong
789 * for 2.2 anyway, as it should have been using
790 * CAP_FSETID rather than fsuid -- 19990830 SD. */
791 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
792 !S_ISDIR(oattr->la_mode)) {
793 la->la_mode &= ~S_ISUID;
794 la->la_valid |= LA_MODE;
798 /* Make sure caller can chgrp. */
799 if (la->la_valid & LA_GID) {
800 if (la->la_gid == (gid_t) -1)
801 la->la_gid = oattr->la_gid;
802 if (((uc->uc_fsuid != oattr->la_uid) ||
803 ((la->la_gid != oattr->la_gid) &&
804 !lustre_in_group_p(uc, la->la_gid))) &&
805 !md_capable(uc, CFS_CAP_CHOWN))
808 /* Likewise, if the user or group of a non-directory
809 * has been changed by a non-root user, remove the
810 * setgid bit UNLESS there is no group execute bit
811 * (this would be a file marked for mandatory
812 * locking). 19981026 David C Niemi <niemi@tux.org>
814 * Removed the fsuid check (see the comment above) --
816 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
817 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
818 la->la_mode &= ~S_ISGID;
819 la->la_valid |= LA_MODE;
823 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
824 if (!((flags & MDS_OWNEROVERRIDE) &&
825 (uc->uc_fsuid == oattr->la_uid)) &&
826 !(flags & MDS_PERM_BYPASS)) {
827 rc = mdd_permission_internal(env, obj, oattr,
834 if (la->la_valid & LA_CTIME) {
835 /* The pure setattr, it has the priority over what is
836 * already set, do not drop it if ctime is equal. */
837 if (la->la_ctime < oattr->la_ctime)
838 la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
844 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
845 struct mdd_device *mdd,
846 enum changelog_rec_type type,
847 enum changelog_rec_flags clf_flags,
848 const struct lu_fid *fid,
849 const char *xattr_name,
850 struct thandle *handle)
852 const struct lu_ucred *uc = lu_ucred(env);
853 enum changelog_rec_extra_flags xflags = CLFE_INVALID;
854 struct llog_changelog_rec *rec;
859 clf_flags = (clf_flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
862 if (uc->uc_jobid[0] != '\0')
863 clf_flags |= CLF_JOBID;
864 xflags |= CLFE_UIDGID;
867 if (type == CL_OPEN || type == CL_DN_OPEN)
869 if (type == CL_SETXATTR || type == CL_GETXATTR)
870 xflags |= CLFE_XATTR;
872 reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
873 changelog_rec_offset(clf_flags & CLF_SUPPORTED,
874 xflags & CLFE_SUPPORTED));
875 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
876 if (buf->lb_buf == NULL)
880 rec->cr_hdr.lrh_len = reclen;
881 rec->cr.cr_flags = clf_flags;
882 rec->cr.cr_type = (__u32)type;
883 rec->cr.cr_tfid = *fid;
884 rec->cr.cr_namelen = 0;
886 if (clf_flags & CLF_JOBID)
887 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
889 if (clf_flags & CLF_EXTRA_FLAGS) {
890 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
891 if (xflags & CLFE_UIDGID)
892 mdd_changelog_rec_extra_uidgid(&rec->cr,
893 uc->uc_uid, uc->uc_gid);
894 if (xflags & CLFE_NID)
895 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
896 if (xflags & CLFE_OPEN)
897 mdd_changelog_rec_extra_omode(&rec->cr, clf_flags);
898 if (xflags & CLFE_XATTR) {
899 if (xattr_name == NULL)
901 mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
905 rc = mdd_changelog_store(env, mdd, rec, handle);
910 /** Store a data change changelog record
911 * If this fails, we must fail the whole transaction; we don't
912 * want the change to commit without the log entry.
913 * \param mdd_obj - mdd_object of change
914 * \param handle - transaction handle
916 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
917 enum changelog_rec_type type,
918 enum changelog_rec_flags clf_flags,
919 struct mdd_object *mdd_obj, struct thandle *handle)
923 LASSERT(mdd_obj != NULL);
924 LASSERT(handle != NULL);
926 if (!mdd_changelog_enabled(env, mdd, type))
929 if (mdd_is_volatile_obj(mdd_obj))
932 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
933 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
934 /* Don't need multiple updates in this log */
935 /* Don't check under lock - no big deal if we get an extra
940 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
941 mdo2fid(mdd_obj), NULL, handle);
943 mdd_obj->mod_cltime = ktime_get();
948 int mdd_changelog_data_store_xattr(const struct lu_env *env,
949 struct mdd_device *mdd,
950 enum changelog_rec_type type,
951 enum changelog_rec_flags clf_flags,
952 struct mdd_object *mdd_obj,
953 const char *xattr_name,
954 struct thandle *handle)
958 LASSERT(mdd_obj != NULL);
959 LASSERT(handle != NULL);
961 if (!mdd_changelog_enabled(env, mdd, type))
964 if (mdd_is_volatile_obj(mdd_obj))
967 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
968 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
969 /* Don't need multiple updates in this log */
970 /* Don't check under lock - no big deal if we get an extra
976 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
977 mdo2fid(mdd_obj), xattr_name,
980 mdd_obj->mod_cltime = ktime_get();
985 /* only the bottom CLF_FLAGSHIFT bits of @flags are stored in the record,
986 * except for open flags have a dedicated record to store 32 bits of flags */
987 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
988 enum changelog_rec_flags clf_flags,
989 struct md_device *m, const struct lu_fid *fid)
991 struct thandle *handle;
992 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
996 LASSERT(fid != NULL);
998 /* We'll check this again below, but we check now before we
999 * start a transaction. */
1000 if (!mdd_changelog_enabled(env, mdd, type))
1003 handle = mdd_trans_create(env, mdd);
1005 RETURN(PTR_ERR(handle));
1007 rc = mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1011 rc = mdd_trans_start(env, mdd, handle);
1015 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1019 rc = mdd_trans_stop(env, mdd, rc, handle);
1025 * Save LMA extended attributes with data from \a ma.
1027 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
1028 * not, LMA EA will be first read from disk, modified and write back.
1031 /* Precedence for choosing record type when multiple
1032 * attributes change: setattr > mtime > ctime > atime
1033 * (ctime changes when mtime does, plus chmod/chown.
1034 * atime and ctime are independent.) */
1035 static int mdd_attr_set_changelog(const struct lu_env *env,
1036 struct md_object *obj, struct thandle *handle,
1039 struct mdd_device *mdd = mdo2mdd(obj);
1042 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
1043 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
1044 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
1045 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
1046 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
1047 bits = bits & mdd->mdd_cl.mc_mask;
1048 /* This is an implementation limit rather than a protocol limit */
1049 CLASSERT(CL_LAST <= sizeof(int) * 8);
1053 /* The record type is the lowest non-masked set bit */
1056 /* XXX: we only store the low CLF_FLAGMASK bits of la_valid */
1057 return mdd_changelog_data_store(env, mdd, type, valid,
1058 md2mdd_obj(obj), handle);
1061 static int mdd_declare_attr_set(const struct lu_env *env,
1062 struct mdd_device *mdd,
1063 struct mdd_object *obj,
1064 const struct lu_attr *attr,
1065 struct thandle *handle)
1069 rc = mdo_declare_attr_set(env, obj, attr, handle);
1073 #ifdef CONFIG_FS_POSIX_ACL
1074 if (attr->la_valid & LA_MODE) {
1075 mdd_read_lock(env, obj, MOR_TGT_CHILD);
1076 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
1077 XATTR_NAME_ACL_ACCESS);
1078 mdd_read_unlock(env, obj);
1079 if (rc == -EOPNOTSUPP || rc == -ENODATA)
1085 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
1086 rc = mdo_declare_xattr_set(env, obj, buf,
1087 XATTR_NAME_ACL_ACCESS, 0,
1095 rc = mdd_declare_changelog_store(env, mdd, CL_SETXATTR, NULL, NULL,
1103 * permission changes may require sync operation, to mitigate performance
1104 * impact, only do this for dir and when permission is reduced.
1106 * For regular files, version is updated with permission change (see VBR), async
1107 * permission won't cause any issue, while missing permission change on
1108 * directory may affect accessibility of other objects after recovery.
1110 static inline bool permission_needs_sync(const struct lu_attr *old,
1111 const struct lu_attr *new)
1113 if (!S_ISDIR(old->la_mode))
1116 if (new->la_valid & (LA_UID | LA_GID))
1119 if (new->la_valid & LA_MODE &&
1120 new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
1123 if ((new->la_valid & LA_MODE) &&
1124 ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
1125 (old->la_mode & S_IRWXUGO))
1131 static inline __u64 mdd_lmm_dom_size(void *buf)
1133 struct lov_mds_md *lmm = buf;
1134 struct lov_comp_md_v1 *comp_v1;
1135 struct lov_mds_md *v1;
1141 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1144 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1145 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1146 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1148 /* DoM entry is the first entry always */
1149 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) == LOV_PATTERN_MDT)
1150 return le64_to_cpu(comp_v1->lcm_entries[0].lcme_extent.e_end);
1155 /* set attr and LOV EA at once, return updated attr */
1156 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
1157 const struct md_attr *ma)
1159 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1160 struct mdd_device *mdd = mdo2mdd(obj);
1161 struct thandle *handle = NULL;
1162 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1163 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1164 const struct lu_attr *la = &ma->ma_attr;
1165 struct lu_ucred *uc;
1169 /* we do not use ->attr_set() for LOV/HSM EA any more */
1170 LASSERT((ma->ma_valid & MA_LOV) == 0);
1171 LASSERT((ma->ma_valid & MA_HSM) == 0);
1173 rc = mdd_la_get(env, mdd_obj, attr);
1177 *la_copy = ma->ma_attr;
1178 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
1182 /* no need to setattr anymore */
1183 if (la_copy->la_valid == 0) {
1184 CDEBUG(D_INODE, "%s: no valid attribute on "DFID", previous"
1185 "valid is %#llx\n", mdd2obd_dev(mdd)->obd_name,
1186 PFID(mdo2fid(mdd_obj)), la->la_valid);
1191 /* If an unprivileged user changes group of some file,
1192 * the setattr operation will be processed synchronously to
1193 * honor the quota limit of the corresponding group. see LU-5152 */
1194 uc = lu_ucred_check(env);
1195 if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1196 la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1197 la_copy->la_valid |= LA_FLAGS;
1198 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1200 /* Flush the possible existing sync requests to OSTs to
1201 * keep the order of sync for the current setattr operation
1202 * will be sent directly to OSTs. see LU-5152 */
1203 rc = dt_sync(env, mdd->mdd_child);
1208 handle = mdd_trans_create(env, mdd);
1209 if (IS_ERR(handle)) {
1210 rc = PTR_ERR(handle);
1216 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1220 rc = mdd_trans_start(env, mdd, handle);
1224 if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
1225 handle->th_sync = 1;
1227 if (la->la_valid & (LA_MTIME | LA_CTIME))
1228 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1229 la->la_mtime, la->la_ctime);
1231 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1233 /* LU-10509: setattr of LA_SIZE should be skipped case of DOM,
1234 * otherwise following truncate will do nothing and truncated
1235 * data may be read again. This is a quick fix until LU-11033
1238 if (la_copy->la_valid & LA_SIZE) {
1239 struct lu_buf *lov_buf = mdd_buf_get(env, NULL, 0);
1241 rc = mdd_stripe_get(env, mdd_obj, lov_buf, XATTR_NAME_LOV);
1245 if (mdd_lmm_dom_size(lov_buf->lb_buf) > 0)
1246 la_copy->la_valid &= ~LA_SIZE;
1247 lu_buf_free(lov_buf);
1251 if (la_copy->la_valid) {
1252 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1254 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1255 /* rollback to the original gid */
1256 la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1257 la_copy->la_gid = attr->la_gid;
1258 mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1261 mdd_write_unlock(env, mdd_obj);
1265 rc = mdd_attr_set_changelog(env, obj, handle,
1269 rc = mdd_trans_stop(env, mdd, rc, handle);
1274 static int mdd_xattr_sanity_check(const struct lu_env *env,
1275 struct mdd_object *obj,
1276 const struct lu_attr *attr,
1279 struct lu_ucred *uc = lu_ucred_assert(env);
1282 if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1285 if (strncmp(XATTR_USER_PREFIX, name,
1286 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1287 /* For sticky directories, only the owner and privileged user
1288 * can write attributes. */
1289 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1290 (uc->uc_fsuid != attr->la_uid) &&
1291 !md_capable(uc, CFS_CAP_FOWNER))
1293 } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
1294 (uc->uc_fsuid != attr->la_uid) &&
1295 !md_capable(uc, CFS_CAP_FOWNER)) {
1303 * Check if a string begins with a given prefix.
1305 * \param str String to check
1306 * \param prefix Substring to check at the beginning of \a str
1307 * \return true/false whether the condition is verified.
1309 static inline bool has_prefix(const char *str, const char *prefix)
1311 return strncmp(prefix, str, strlen(prefix)) == 0;
1315 * Indicate the kind of changelog to store (if any) for a xattr set/del.
1317 * \param[in] xattr_name Full extended attribute name.
1319 * \return The type of changelog to use, or -1 if no changelog is to be emitted.
1321 static enum changelog_rec_type
1322 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1323 const char *xattr_name)
1325 /* Layout changes systematically recorded */
1326 if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1327 strncmp(XATTR_LUSTRE_LOV, xattr_name,
1328 strlen(XATTR_LUSTRE_LOV)) == 0)
1331 /* HSM information changes systematically recorded */
1332 if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1335 /* Avoid logging SOM xattr for every file */
1336 if (strcmp(XATTR_NAME_SOM, xattr_name) == 0)
1339 if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1340 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1341 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1342 has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1343 has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1349 static int mdd_declare_xattr_set(const struct lu_env *env,
1350 struct mdd_device *mdd,
1351 struct mdd_object *obj,
1352 const struct lu_buf *buf,
1354 int fl, struct thandle *handle)
1356 enum changelog_rec_type type;
1359 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1363 type = mdd_xattr_changelog_type(env, mdd, name);
1365 return 0; /* no changelog to store */
1367 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1371 * Compare current and future data of HSM EA and add a changelog if needed.
1373 * Caller should have write-locked \param obj.
1375 * \param buf - Future HSM EA content.
1376 * \retval 0 if no changelog is needed or changelog was added properly.
1377 * \retval -ve errno if there was a problem
1379 static int mdd_hsm_update_locked(const struct lu_env *env,
1380 struct md_object *obj,
1381 const struct lu_buf *buf,
1382 struct thandle *handle,
1383 enum changelog_rec_flags *clf_flags)
1385 struct mdd_thread_info *info = mdd_env_info(env);
1386 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1387 struct lu_buf *current_buf;
1388 struct md_hsm *current_mh;
1389 struct md_hsm *new_mh;
1393 OBD_ALLOC_PTR(current_mh);
1394 if (current_mh == NULL)
1397 /* Read HSM attrs from disk */
1398 current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1399 mdo2mdd(obj)->mdd_dt_conf.ddp_max_ea_size);
1400 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1401 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1402 if (rc < 0 && rc != -ENODATA)
1404 else if (rc == -ENODATA)
1405 current_mh->mh_flags = 0;
1407 /* Map future HSM xattr */
1408 OBD_ALLOC_PTR(new_mh);
1410 GOTO(free, rc = -ENOMEM);
1411 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1415 /* Flags differ, set flags for the changelog that will be added */
1416 if (current_mh->mh_flags != new_mh->mh_flags) {
1417 hsm_set_cl_event(clf_flags, HE_STATE);
1418 if (new_mh->mh_flags & HS_DIRTY)
1419 hsm_set_cl_flags(clf_flags, CLF_HSM_DIRTY);
1422 OBD_FREE_PTR(new_mh);
1425 OBD_FREE_PTR(current_mh);
1429 static int mdd_object_pfid_replace(const struct lu_env *env,
1430 struct mdd_object *o)
1432 struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1433 struct thandle *handle;
1436 handle = mdd_trans_create(env, mdd);
1438 RETURN(PTR_ERR(handle));
1440 handle->th_complex = 1;
1442 /* it doesn't need to track the PFID update via llog, because LFSCK
1443 * will repair it even it goes wrong */
1444 rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1449 rc = mdd_trans_start(env, mdd, handle);
1453 rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1458 mdd_trans_stop(env, mdd, rc, handle);
1463 static int mdd_declare_xattr_del(const struct lu_env *env,
1464 struct mdd_device *mdd,
1465 struct mdd_object *obj,
1467 struct thandle *handle);
1469 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1472 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1473 struct md_object *md_vic)
1475 struct mdd_device *mdd = mdo2mdd(md_obj);
1476 struct mdd_object *obj = md2mdd_obj(md_obj);
1477 struct mdd_object *vic = md2mdd_obj(md_vic);
1478 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1479 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1480 struct lov_mds_md *lmm;
1481 struct thandle *handle;
1485 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1486 if (rc == 0) /* same fid */
1489 handle = mdd_trans_create(env, mdd);
1491 RETURN(PTR_ERR(handle));
1494 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1495 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1497 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1498 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1501 /* get EA of victim file */
1502 memset(buf_vic, 0, sizeof(*buf_vic));
1503 rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
1510 /* parse the layout of victim file */
1511 lmm = buf_vic->lb_buf;
1512 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1513 GOTO(out, rc = -EINVAL);
1515 /* save EA of target file for restore */
1516 memset(buf, 0, sizeof(*buf));
1517 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
1521 /* Get rid of the layout from victim object */
1522 rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1526 rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1527 LU_XATTR_MERGE, handle);
1531 rc = mdd_trans_start(env, mdd, handle);
1535 rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1540 rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1542 GOTO(out_restore, rc);
1544 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1545 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1550 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1551 LU_XATTR_REPLACE, handle);
1553 CERROR("%s: failed to rollback of layout of: "DFID
1554 ": %d, file state unknown\n",
1555 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1559 mdd_trans_stop(env, mdd, rc, handle);
1560 mdd_write_unlock(env, obj);
1561 mdd_write_unlock(env, vic);
1563 lu_buf_free(buf_vic);
1566 (void) mdd_object_pfid_replace(env, obj);
1572 * Extract the mirror with specified mirror id, and store the splitted
1573 * mirror layout to @buf.
1575 * \param[in] comp_v1 mirrored layout
1576 * \param[in] mirror_id the mirror with mirror_id to be extracted
1577 * \param[out] buf store the layout excluding the extracted mirror,
1578 * caller free the buffer we allocated in this function
1579 * \param[out] buf_vic store the extracted layout, caller free the buffer
1580 * we allocated in this function
1582 * \retval 0 on success; < 0 if error happens
1584 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1585 struct lu_buf *buf, struct lu_buf *buf_vic)
1587 struct lov_comp_md_v1 *comp_rem;
1588 struct lov_comp_md_v1 *comp_vic;
1589 struct lov_comp_md_entry_v1 *entry;
1590 struct lov_comp_md_entry_v1 *entry_rem;
1591 struct lov_comp_md_entry_v1 *entry_vic;
1593 __u16 comp_cnt, count = 0;
1594 int lmm_size, lmm_size_vic = 0;
1596 int offset, offset_rem, offset_vic;
1598 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1599 /* comp_v1 should contains more than 1 mirror */
1600 if (mirror_cnt <= 1)
1602 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1603 lmm_size = le32_to_cpu(comp_v1->lcm_size);
1605 for (i = 0; i < comp_cnt; i++) {
1606 entry = &comp_v1->lcm_entries[i];
1607 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1609 lmm_size_vic += sizeof(*entry);
1610 lmm_size_vic += le32_to_cpu(entry->lcme_size);
1611 } else if (count > 0) {
1612 /* find the specified mirror */
1620 lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1624 lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1625 if (!buf_vic->lb_buf) {
1630 comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1631 comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1633 memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1634 comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1635 comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1636 comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1637 if (!comp_rem->lcm_mirror_count)
1638 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1640 memset(comp_vic, 0, sizeof(*comp_v1));
1641 comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1642 comp_vic->lcm_mirror_count = 0;
1643 comp_vic->lcm_entry_count = cpu_to_le32(count);
1644 comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1645 comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1646 comp_vic->lcm_layout_gen = 0;
1648 offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1649 offset_rem = sizeof(*comp_rem) +
1650 sizeof(*entry_rem) * (comp_cnt - count);
1651 offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1652 for (i = j = k = 0; i < comp_cnt; i++) {
1653 struct lov_mds_md *lmm, *lmm_dst;
1656 entry = &comp_v1->lcm_entries[i];
1657 entry_vic = &comp_vic->lcm_entries[j];
1658 entry_rem = &comp_rem->lcm_entries[k];
1660 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1663 /* copy component entry */
1665 memcpy(entry_vic, entry, sizeof(*entry));
1666 entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1667 entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1670 memcpy(entry_rem, entry, sizeof(*entry));
1671 entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1675 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1677 lmm_dst = (struct lov_mds_md *)
1678 ((char *)comp_vic + offset_vic);
1680 lmm_dst = (struct lov_mds_md *)
1681 ((char *)comp_rem + offset_rem);
1683 /* copy component entry blob */
1684 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1686 /* blob offset advance */
1687 offset += le32_to_cpu(entry->lcme_size);
1689 offset_vic += le32_to_cpu(entry->lcme_size);
1691 offset_rem += le32_to_cpu(entry->lcme_size);
1697 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1698 struct md_rejig_data *mrd)
1700 struct mdd_device *mdd = mdo2mdd(md_obj);
1701 struct mdd_object *obj = md2mdd_obj(md_obj);
1702 struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
1703 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1704 struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1705 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1706 struct lov_comp_md_v1 *lcm;
1707 struct thandle *handle;
1711 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1712 if (rc == 0) /* same fid */
1715 handle = mdd_trans_create(env, mdd);
1717 RETURN(PTR_ERR(handle));
1720 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1721 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1723 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1724 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1727 /* get EA of mirrored file */
1728 memset(buf_save, 0, sizeof(*buf));
1729 rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
1733 lcm = buf_save->lb_buf;
1734 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1735 GOTO(out, rc = -EINVAL);
1738 * Extract the mirror with specified mirror id, and store the splitted
1739 * mirror layout to the victim file.
1741 memset(buf, 0, sizeof(*buf));
1742 memset(buf_vic, 0, sizeof(*buf_vic));
1743 rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1747 rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1748 LU_XATTR_SPLIT, handle);
1751 rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
1752 LU_XATTR_SPLIT, handle);
1756 rc = mdd_trans_start(env, mdd, handle);
1760 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1765 rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
1768 GOTO(out_restore, rc);
1770 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1774 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1781 /* restore obj's layout */
1782 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1783 LU_XATTR_REPLACE, handle);
1785 CERROR("%s: failed to rollback of layout of: "DFID
1786 ": %d, file state unkonwn.\n",
1787 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1790 mdd_trans_stop(env, mdd, rc, handle);
1791 mdd_write_unlock(env, obj);
1792 mdd_write_unlock(env, vic);
1793 lu_buf_free(buf_save);
1795 lu_buf_free(buf_vic);
1798 (void) mdd_object_pfid_replace(env, obj);
1803 static int mdd_layout_merge_allowed(const struct lu_env *env,
1804 struct md_object *target,
1805 struct md_object *victim)
1807 struct mdd_object *o1 = md2mdd_obj(target);
1809 /* cannot extend directory's LOVEA */
1810 if (S_ISDIR(mdd_object_type(o1))) {
1811 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1812 mdd_obj_dev_name(o1));
1820 * The caller should guarantee to update the object ctime
1821 * after xattr_set if needed.
1823 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1824 const struct lu_buf *buf, const char *name,
1827 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1828 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1829 struct mdd_device *mdd = mdo2mdd(obj);
1830 struct thandle *handle;
1831 enum changelog_rec_type cl_type;
1832 enum changelog_rec_flags clf_flags = 0;
1836 rc = mdd_la_get(env, mdd_obj, attr);
1840 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1844 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1845 (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1846 struct md_rejig_data *mrd = buf->lb_buf;
1847 struct md_object *victim = mrd->mrd_obj;
1849 if (buf->lb_len != sizeof(*mrd))
1852 rc = mdd_layout_merge_allowed(env, obj, victim);
1856 if (fl == LU_XATTR_MERGE)
1857 /* merge layout of victim as a mirror of obj's. */
1858 rc = mdd_xattr_merge(env, obj, victim);
1860 rc = mdd_xattr_split(env, obj, mrd);
1864 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1865 rc = mdd_dir_layout_shrink(env, obj, buf);
1869 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1870 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1871 struct posix_acl *acl;
1873 /* user may set empty ACL, which should be treated as removing
1875 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1878 RETURN(PTR_ERR(acl));
1880 rc = mdd_xattr_del(env, obj, name);
1883 posix_acl_release(acl);
1886 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1887 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1891 handle = mdd_trans_create(env, mdd);
1893 RETURN(PTR_ERR(handle));
1895 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1899 rc = mdd_trans_start(env, mdd, handle);
1903 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1905 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1906 rc = mdd_hsm_update_locked(env, obj, buf, handle, &clf_flags);
1908 mdd_write_unlock(env, mdd_obj);
1913 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
1914 mdd_write_unlock(env, mdd_obj);
1918 cl_type = mdd_xattr_changelog_type(env, mdd, name);
1922 rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, clf_flags,
1923 mdd_obj, name, handle);
1927 return mdd_trans_stop(env, mdd, rc, handle);
1930 static int mdd_declare_xattr_del(const struct lu_env *env,
1931 struct mdd_device *mdd,
1932 struct mdd_object *obj,
1934 struct thandle *handle)
1936 enum changelog_rec_type type;
1939 rc = mdo_declare_xattr_del(env, obj, name, handle);
1943 type = mdd_xattr_changelog_type(env, mdd, name);
1945 return 0; /* no changelog to store */
1947 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1951 * The caller should guarantee to update the object ctime
1952 * after xattr_set if needed.
1954 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1957 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1958 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1959 struct mdd_device *mdd = mdo2mdd(obj);
1960 struct thandle *handle;
1964 rc = mdd_la_get(env, mdd_obj, attr);
1968 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1972 handle = mdd_trans_create(env, mdd);
1974 RETURN(PTR_ERR(handle));
1976 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1980 rc = mdd_trans_start(env, mdd, handle);
1984 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1985 rc = mdo_xattr_del(env, mdd_obj, name, handle);
1986 mdd_write_unlock(env, mdd_obj);
1990 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
1993 rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
1998 return mdd_trans_stop(env, mdd, rc, handle);
2002 * read lov/lmv EA of an object
2003 * return the lov/lmv EA in an allocated lu_buf
2005 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
2006 struct lu_buf *lmm_buf, const char *name)
2008 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
2013 if (buf->lb_buf == NULL) {
2014 buf = lu_buf_check_and_alloc(buf, 4096);
2015 if (buf->lb_buf == NULL)
2020 rc = mdo_xattr_get(env, obj, buf, name);
2021 if (rc == -ERANGE) {
2022 /* mti_big_buf is allocated but is too small
2023 * we need to increase it */
2024 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
2026 if (buf->lb_buf == NULL)
2029 } else if (rc < 0) {
2031 } else if (rc == 0) {
2035 lu_buf_alloc(lmm_buf, rc);
2036 if (lmm_buf->lb_buf == NULL)
2040 * we don't use lmm_buf directly, because we don't know xattr size, so
2041 * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
2043 memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
2048 static int mdd_xattr_hsm_replace(const struct lu_env *env,
2049 struct mdd_object *o, struct lu_buf *buf,
2050 struct thandle *handle)
2052 struct hsm_attrs *attrs;
2053 enum hsm_states hsm_flags;
2054 enum changelog_rec_flags clf_flags = 0;
2058 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
2063 attrs = buf->lb_buf;
2064 hsm_flags = le32_to_cpu(attrs->hsm_flags);
2065 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
2068 /* Add a changelog record for release. */
2069 hsm_set_cl_event(&clf_flags, HE_RELEASE);
2070 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
2071 clf_flags, o, handle);
2076 * check if layout swapping between 2 objects is allowed
2078 * - only normal FIDs or non-system IGIFs
2079 * - same type of objects
2080 * - same owner/group (so quotas are still valid) unless this is from HSM
2083 static int mdd_layout_swap_allowed(const struct lu_env *env,
2084 struct mdd_object *o1,
2085 const struct lu_attr *attr1,
2086 struct mdd_object *o2,
2087 const struct lu_attr *attr2,
2090 const struct lu_fid *fid1, *fid2;
2096 if (!fid_is_norm(fid1) &&
2097 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
2100 if (!fid_is_norm(fid2) &&
2101 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
2104 if (mdd_object_type(o1) != mdd_object_type(o2)) {
2105 if (S_ISDIR(mdd_object_type(o1)))
2107 if (S_ISREG(mdd_object_type(o1)))
2112 if (flags & SWAP_LAYOUTS_MDS_HSM)
2115 if ((attr1->la_uid != attr2->la_uid) ||
2116 (attr1->la_gid != attr2->la_gid))
2122 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
2123 * look into the layout in MDD layer. */
2124 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
2126 struct lov_comp_md_v1 *comp_v1;
2127 struct lov_mds_md *v1;
2131 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2132 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2133 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
2139 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
2140 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
2143 for (i = 0; i < le32_to_cpu(ent_count); i++) {
2144 off = le32_to_cpu(comp_v1->lcm_entries[i].
2146 v1 = (struct lov_mds_md *)((char *)comp_v1 +
2151 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2152 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2163 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2165 return mdd_lmm_oi(lmm, oi, true);
2168 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2170 return mdd_lmm_oi(lmm, oi, false);
2173 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
2175 struct lov_comp_md_v1 *comp_v1;
2177 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2178 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2180 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
2182 comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
2183 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2184 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2185 __u16 tmp_gen = *gen;
2187 *gen = le16_to_cpu(lmm->lmm_layout_gen);
2189 lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
2196 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2198 return mdd_lmm_gen(lmm, gen, true);
2201 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2203 return mdd_lmm_gen(lmm, gen, false);
2206 static int mdd_dom_data_truncate(const struct lu_env *env,
2207 struct mdd_device *mdd, struct mdd_object *mo)
2210 struct dt_object *dom;
2213 dom = dt_object_locate(mdd_object_child(mo), mdd->mdd_bottom);
2215 GOTO(out, rc = -ENODATA);
2217 th = dt_trans_create(env, mdd->mdd_bottom);
2219 GOTO(out, rc = PTR_ERR(th));
2221 rc = dt_declare_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2225 rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
2229 rc = dt_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2231 dt_trans_stop(env, mdd->mdd_bottom, th);
2233 /* Ignore failure but report the error */
2235 CERROR("%s: "DFID" can't truncate DOM inode data, rc = %d\n",
2236 mdd_obj_dev_name(mo), PFID(mdo2fid(mo)), rc);
2241 * swap layouts between 2 lustre objects
2243 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2244 struct md_object *obj2, __u64 flags)
2246 struct mdd_thread_info *info = mdd_env_info(env);
2247 struct mdd_object *fst_o = md2mdd_obj(obj1);
2248 struct mdd_object *snd_o = md2mdd_obj(obj2);
2249 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2250 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2251 struct mdd_device *mdd = mdo2mdd(obj1);
2252 struct lov_mds_md *fst_lmm, *snd_lmm;
2253 struct lu_buf *fst_buf = &info->mti_buf[0];
2254 struct lu_buf *snd_buf = &info->mti_buf[1];
2255 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2256 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2257 struct ost_id *saved_oi = NULL;
2258 struct thandle *handle;
2259 struct mdd_object *dom_o = NULL;
2260 __u64 domsize_dom, domsize_vlt;
2261 __u32 fst_gen, snd_gen, saved_gen;
2267 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
2268 memset(info->mti_buf, 0, sizeof(info->mti_buf));
2270 /* we have to sort the 2 obj, so locking will always
2271 * be in the same order, even in case of 2 concurrent swaps */
2272 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
2273 if (rc == 0) /* same fid ? */
2279 rc = mdd_la_get(env, fst_o, fst_la);
2283 rc = mdd_la_get(env, snd_o, snd_la);
2287 /* check if layout swapping is allowed */
2288 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2292 handle = mdd_trans_create(env, mdd);
2294 RETURN(PTR_ERR(handle));
2296 /* objects are already sorted */
2297 mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
2298 mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
2300 rc = mdd_stripe_get(env, fst_o, fst_buf, XATTR_NAME_LOV);
2301 if (rc < 0 && rc != -ENODATA)
2304 rc = mdd_stripe_get(env, snd_o, snd_buf, XATTR_NAME_LOV);
2305 if (rc < 0 && rc != -ENODATA)
2308 /* check if file has DoM. DoM file can be migrated only to another
2309 * DoM layout with the same DoM component size or to an non-DOM
2310 * layout. After migration to OSTs layout, local MDT inode data
2311 * should be truncated.
2312 * Objects are sorted by FIDs, considering that original file's FID
2313 * is always smaller the snd_o is always original file we are migrating
2316 domsize_dom = mdd_lmm_dom_size(snd_buf->lb_buf);
2317 domsize_vlt = mdd_lmm_dom_size(fst_buf->lb_buf);
2319 /* Only migration is supported for DoM files, not 'swap_layouts' so
2320 * target file must be volatile and orphan.
2322 if (fst_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2323 dom_o = domsize_dom ? snd_o : NULL;
2324 } else if (snd_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2325 swap(domsize_dom, domsize_vlt);
2326 dom_o = domsize_dom ? fst_o : NULL;
2327 } else if (domsize_dom > 0 || domsize_vlt > 0) {
2328 /* 'lfs swap_layouts' case, neither file should have DoM */
2330 CDEBUG(D_LAYOUT, "cannot swap layouts with DOM component, "
2331 "use migration instead: rc = %d\n", rc);
2335 if (domsize_vlt > 0 && domsize_dom == 0) {
2337 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": OST to DOM "
2338 "migration is not supported: rc = %d\n",
2339 PFID(mdo2fid(snd_o)), rc);
2341 } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
2343 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": new layout "
2344 "must have the same DoM component size: rc = %d\n",
2345 PFID(mdo2fid(fst_o)), rc);
2347 } else if (domsize_vlt > 0) {
2348 /* Migration with the same DOM component size, no need to
2349 * truncate local data, it is still being used */
2353 /* swapping 2 non existant layouts is a success */
2354 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2357 /* to help inode migration between MDT, it is better to
2358 * start by the no layout file (if one), so we order the swap */
2359 if (snd_buf->lb_buf == NULL) {
2361 swap(fst_buf, snd_buf);
2364 fst_gen = snd_gen = 0;
2365 /* lmm and generation layout initialization */
2366 if (fst_buf->lb_buf != NULL) {
2367 fst_lmm = fst_buf->lb_buf;
2368 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2369 fst_fl = LU_XATTR_REPLACE;
2373 fst_fl = LU_XATTR_CREATE;
2376 snd_lmm = snd_buf->lb_buf;
2377 mdd_get_lmm_gen(snd_lmm, &snd_gen);
2379 saved_gen = fst_gen;
2380 /* increase the generation layout numbers */
2385 * XXX The layout generation is used to generate component IDs for
2386 * the composite file, we have to do some special tweaks to make
2387 * sure the layout generation is always adequate for that job.
2390 /* Skip invalid generation number for composite layout */
2391 if ((snd_gen & LCME_ID_MASK) == 0)
2393 if ((fst_gen & LCME_ID_MASK) == 0)
2395 /* Make sure the generation is greater than all the component IDs */
2396 if (fst_gen < snd_gen)
2398 else if (fst_gen > snd_gen)
2401 /* set the file specific informations in lmm */
2402 if (fst_lmm != NULL) {
2403 saved_oi = &info->mti_oa.o_oi;
2404 mdd_get_lmm_oi(fst_lmm, saved_oi);
2405 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2406 mdd_set_lmm_oi(fst_lmm, &snd_lmm->lmm_oi);
2407 mdd_set_lmm_oi(snd_lmm, saved_oi);
2409 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2410 cpu_to_le32(LOV_MAGIC_MAGIC))
2411 snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2413 GOTO(stop, rc = -EPROTO);
2415 mdd_set_lmm_gen(snd_lmm, &fst_gen);
2417 /* Prepare HSM attribute if it's required */
2418 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2419 const int buflen = sizeof(struct hsm_attrs);
2421 lu_buf_alloc(fst_hsm_buf, buflen);
2422 lu_buf_alloc(snd_hsm_buf, buflen);
2423 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2424 GOTO(stop, rc = -ENOMEM);
2426 /* Read HSM attribute */
2427 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2431 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2435 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2436 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2441 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2442 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2448 /* prepare transaction */
2449 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2454 if (fst_buf->lb_buf != NULL)
2455 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2456 XATTR_NAME_LOV, LU_XATTR_REPLACE,
2459 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2464 rc = mdd_trans_start(env, mdd, handle);
2468 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2469 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2473 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2475 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2478 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
2479 mdd_obj_dev_name(fst_o),
2480 PFID(mdo2fid(fst_o)), rc, rc2);
2485 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2489 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2492 if (fst_buf->lb_buf != NULL)
2493 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2494 LU_XATTR_REPLACE, handle);
2496 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2499 GOTO(out_restore, rc);
2501 /* Issue one changelog record per file */
2502 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
2506 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
2515 /* failure on second file, but first was done, so we have
2516 * to roll back first. */
2517 if (fst_buf->lb_buf != NULL) {
2518 mdd_set_lmm_oi(fst_lmm, saved_oi);
2519 mdd_set_lmm_gen(fst_lmm, &saved_gen);
2520 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2521 LU_XATTR_REPLACE, handle);
2523 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2529 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2534 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2539 CERROR("%s: unable to roll back layout swap. FIDs: "
2540 DFID" and "DFID "error: %d/%d, steps: %d\n",
2541 mdd_obj_dev_name(fst_o),
2542 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
2544 /* a solution to avoid journal commit is to panic,
2545 * but it has strong consequences so we use LBUG to
2546 * allow sysdamin to choose to panic or not
2553 rc = mdd_trans_stop(env, mdd, rc, handle);
2555 /* Truncate local DOM data if all went well */
2557 mdd_dom_data_truncate(env, mdd, dom_o);
2559 mdd_write_unlock(env, snd_o);
2560 mdd_write_unlock(env, fst_o);
2562 lu_buf_free(fst_buf);
2563 lu_buf_free(snd_buf);
2564 lu_buf_free(fst_hsm_buf);
2565 lu_buf_free(snd_hsm_buf);
2568 (void) mdd_object_pfid_replace(env, fst_o);
2569 (void) mdd_object_pfid_replace(env, snd_o);
2574 static int mdd_declare_layout_change(const struct lu_env *env,
2575 struct mdd_device *mdd,
2576 struct mdd_object *obj,
2577 struct md_layout_change *mlc,
2578 struct thandle *handle)
2582 rc = mdo_declare_layout_change(env, obj, mlc, handle);
2586 return mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2590 /* For PFL, this is used to instantiate necessary component objects. */
2592 mdd_layout_instantiate_component(const struct lu_env *env,
2593 struct mdd_object *obj, struct md_layout_change *mlc,
2594 struct thandle *handle)
2596 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2600 if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2603 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2605 * It's possible that another layout write intent has already
2606 * instantiated our objects, so a -EALREADY returned, and we need to
2610 RETURN(rc == -EALREADY ? 0 : rc);
2612 rc = mdd_trans_start(env, mdd, handle);
2616 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2617 rc = mdo_layout_change(env, obj, mlc, handle);
2618 mdd_write_unlock(env, obj);
2622 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
2627 * Change the FLR layout from RDONLY to WRITE_PENDING.
2629 * It picks the primary mirror, and bumps the layout version, and set
2630 * layout version xattr to OST objects in a sync tx. In order to facilitate
2631 * the handling of phantom writers from evicted clients, the clients carry
2632 * layout version of the file with write RPC, so that the OSTs can verify
2633 * if the write RPCs are legitimate, meaning not from evicted clients.
2636 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2637 struct md_layout_change *mlc, struct thandle *handle)
2639 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2640 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2641 struct lustre_som_attrs *som = &mlc->mlc_som;
2646 /* Verify acceptable operations */
2647 switch (mlc->mlc_opc) {
2648 case MD_LAYOUT_WRITE:
2649 case MD_LAYOUT_RESYNC:
2650 /* these are legal operations - this represents the case that
2651 * a few mirrors were missed in the last resync. */
2653 case MD_LAYOUT_RESYNC_DONE:
2658 som_buf->lb_buf = som;
2659 som_buf->lb_len = sizeof(*som);
2660 rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
2661 if (rc < 0 && rc != -ENODATA)
2665 lustre_som_swab(som);
2666 if (som->lsa_valid & SOM_FL_STRICT)
2667 fl = LU_XATTR_REPLACE;
2670 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2675 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2676 XATTR_NAME_SOM, fl, handle);
2681 /* record a changelog for data mover to consume */
2682 rc = mdd_declare_changelog_store(env, mdd, CL_FLRW, NULL, NULL, handle);
2686 rc = mdd_trans_start(env, mdd, handle);
2690 /* it needs a sync tx to make FLR to work properly */
2691 handle->th_sync = 1;
2693 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2694 rc = mdo_layout_change(env, obj, mlc, handle);
2696 /* SOM state transition from STRICT to STALE */
2697 som->lsa_valid = SOM_FL_STALE;
2698 lustre_som_swab(som);
2699 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2702 mdd_write_unlock(env, obj);
2706 rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
2717 * Handle mirrored file state transition when it's in WRITE_PENDING.
2719 * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2720 * the file is in WRITE_PENDING state. If everything goes fine, the file's
2721 * layout version will be increased, and the file's state will be changed to
2725 mdd_layout_update_write_pending(const struct lu_env *env,
2726 struct mdd_object *obj, struct md_layout_change *mlc,
2727 struct thandle *handle)
2729 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2733 switch (mlc->mlc_opc) {
2734 case MD_LAYOUT_RESYNC:
2735 /* Upon receiving the resync request, it should
2736 * instantiate all stale components right away to get ready
2737 * for mirror copy. In order to avoid layout version change,
2738 * client should avoid sending LAYOUT_WRITE request at the
2741 case MD_LAYOUT_WRITE:
2742 /* legal race for concurrent write, the file state has been
2743 * changed by another client. */
2749 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2753 rc = mdd_trans_start(env, mdd, handle);
2757 /* it needs a sync tx to make FLR to work properly */
2758 handle->th_sync = 1;
2760 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2761 rc = mdo_layout_change(env, obj, mlc, handle);
2762 mdd_write_unlock(env, obj);
2773 * Handle the requests when a FLR file's state is in SYNC_PENDING.
2775 * Only concurrent write and sync complete requests are possible when the
2776 * file is in SYNC_PENDING. For the latter request, it will pass in the
2777 * mirrors that have been synchronized, then the stale bit will be cleared
2778 * to make the file's state turn into RDONLY.
2779 * For concurrent write reqeust, it just needs to change the file's state
2780 * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2781 * version because the version will be increased in the transition to
2782 * SYNC_PENDING later so that it can deny the write request from potential
2783 * evicted SYNC clients. */
2785 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2786 struct md_layout_change *mlc, struct thandle *handle)
2788 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2789 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2794 /* operation validation */
2795 switch (mlc->mlc_opc) {
2796 case MD_LAYOUT_RESYNC_DONE:
2797 /* resync complete. */
2798 case MD_LAYOUT_WRITE:
2799 /* concurrent write. */
2801 case MD_LAYOUT_RESYNC:
2802 /* resync again, most likely the previous run failed.
2803 * no-op if it's already in SYNC_PENDING state */
2809 if (mlc->mlc_som.lsa_valid & SOM_FL_STRICT) {
2810 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2811 if (rc < 0 && rc != -ENODATA)
2814 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2815 lustre_som_swab(&mlc->mlc_som);
2816 som_buf->lb_buf = &mlc->mlc_som;
2817 som_buf->lb_len = sizeof(mlc->mlc_som);
2820 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2824 /* record a changelog for the completion of resync */
2825 rc = mdd_declare_changelog_store(env, mdd, CL_RESYNC, NULL, NULL,
2830 /* RESYNC_DONE has piggybacked size and blocks */
2832 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2833 XATTR_NAME_SOM, fl, handle);
2838 rc = mdd_trans_start(env, mdd, handle);
2842 /* it needs a sync tx to make FLR to work properly */
2843 handle->th_sync = 1;
2845 rc = mdo_layout_change(env, obj, mlc, handle);
2850 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2856 rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
2865 * Layout change callback for object.
2867 * This is only used by FLR for now. In the future, it can be exteneded to
2868 * handle all layout change.
2871 mdd_layout_change(const struct lu_env *env, struct md_object *o,
2872 struct md_layout_change *mlc)
2874 struct mdd_object *obj = md2mdd_obj(o);
2875 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2876 struct lu_buf *buf = mdd_buf_get(env, NULL, 0);
2877 struct lov_comp_md_v1 *lcm;
2878 struct thandle *handle;
2883 /* Verify acceptable operations */
2884 switch (mlc->mlc_opc) {
2885 case MD_LAYOUT_WRITE:
2886 case MD_LAYOUT_RESYNC:
2887 case MD_LAYOUT_RESYNC_DONE:
2893 handle = mdd_trans_create(env, mdd);
2895 RETURN(PTR_ERR(handle));
2897 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
2904 /* analyze the layout to make sure it's a FLR file */
2906 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2907 GOTO(out, rc = -EINVAL);
2909 flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
2911 /* please refer to HLD of FLR for state transition */
2912 switch (flr_state) {
2914 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
2916 case LCM_FL_WRITE_PENDING:
2917 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
2920 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
2922 case LCM_FL_SYNC_PENDING:
2923 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
2932 mdd_trans_stop(env, mdd, rc, handle);
2937 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
2938 struct mdd_object *child, const struct lu_attr *attr,
2939 const struct md_op_spec *spec,
2940 struct dt_allocation_hint *hint)
2942 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
2943 struct dt_object *nc = mdd_object_child(child);
2945 memset(hint, 0, sizeof(*hint));
2947 /* For striped directory, give striping EA to lod_ah_init, which will
2948 * decide the stripe_offset and stripe count by it. */
2949 if (S_ISDIR(attr->la_mode) &&
2950 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2951 hint->dah_eadata = spec->u.sp_ea.eadata;
2952 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
2954 hint->dah_eadata = NULL;
2955 hint->dah_eadata_len = 0;
2958 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
2959 hint->dah_eadata, hint->dah_eadata_len);
2960 /* @hint will be initialized by underlying device. */
2961 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
2964 static int accmode(const struct lu_env *env, const struct lu_attr *la,
2967 /* Sadly, NFSD reopens a file repeatedly during operation, so the
2968 * "acc_mode = 0" allowance for newly-created files isn't honoured.
2969 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
2970 * owner can write to a file even if it is marked readonly to hide
2971 * its brokenness. (bug 5781) */
2972 if (open_flags & MDS_OPEN_OWNEROVERRIDE) {
2973 struct lu_ucred *uc = lu_ucred_check(env);
2975 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
2979 return mds_accmode(open_flags);
2982 static int mdd_open_sanity_check(const struct lu_env *env,
2983 struct mdd_object *obj,
2984 const struct lu_attr *attr, u64 open_flags)
2989 /* EEXIST check, also opening of *open* orphans is allowed so we can
2990 * open-by-handle unlinked files
2992 if (mdd_is_dead_obj(obj) &&
2993 likely(!(mdd_is_orphan_obj(obj) && obj->mod_count > 0)))
2996 if (S_ISLNK(attr->la_mode))
2999 mode = accmode(env, attr, open_flags);
3001 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
3004 if (!(open_flags & MDS_OPEN_CREATED)) {
3005 rc = mdd_permission_internal(env, obj, attr, mode);
3010 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
3011 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
3012 open_flags &= ~MDS_OPEN_TRUNC;
3014 /* For writing append-only file must open it with append mode. */
3015 if (attr->la_flags & LUSTRE_APPEND_FL) {
3016 if ((open_flags & MDS_FMODE_WRITE) &&
3017 !(open_flags & MDS_OPEN_APPEND))
3019 if (open_flags & MDS_OPEN_TRUNC)
3026 static int mdd_open(const struct lu_env *env, struct md_object *obj,
3029 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3030 struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
3031 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3032 struct mdd_object_user *mou = NULL;
3033 const struct lu_ucred *uc = lu_ucred(env);
3034 struct mdd_device *mdd = mdo2mdd(obj);
3035 enum changelog_rec_type type = CL_OPEN;
3038 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3040 rc = mdd_la_get(env, mdd_obj, attr);
3044 rc = mdd_open_sanity_check(env, mdd_obj, attr, open_flags);
3045 if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & (1 << CL_DN_OPEN)))
3050 mdd_obj->mod_count++;
3052 if (!mdd_changelog_enabled(env, mdd, type))
3056 /* look for existing opener in list under mdd_write_lock */
3057 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, open_flags);
3062 /* add user to list */
3063 mou = mdd_obj_user_alloc(open_flags, uc->uc_uid, uc->uc_gid);
3069 rc2 = mdd_obj_user_add(mdd_obj, mou, type == CL_DN_OPEN);
3071 mdd_obj_user_free(mou);
3076 if (type == CL_DN_OPEN) {
3077 if (ktime_before(ktime_get(), mou->mou_deniednext))
3078 /* same user denied again same access within
3079 * time interval: do not record
3083 /* this user already denied, but some time ago:
3084 * update denied time
3086 mou->mou_deniednext =
3087 ktime_add(ktime_get(),
3088 ktime_set(mdd->mdd_cl.mc_deniednext,
3091 mou->mou_opencount++;
3092 /* same user opening file again with same flags:
3099 /* FYI, only the bottom 32 bits of open_flags are recorded */
3100 mdd_changelog(env, type, open_flags, md_dev, mdo2fid(mdd_obj));
3104 mdd_write_unlock(env, mdd_obj);
3108 static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
3109 struct md_attr *ma, struct thandle *handle)
3113 rc = mdd_orphan_declare_delete(env, obj, handle);
3117 return mdo_declare_destroy(env, obj, handle);
3121 * No permission check is needed.
3123 static int mdd_close(const struct lu_env *env, struct md_object *obj,
3124 struct md_attr *ma, u64 open_flags)
3126 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3127 struct mdd_device *mdd = mdo2mdd(obj);
3128 struct thandle *handle = NULL;
3131 bool blocked = false;
3132 int last_close_by_uid = 0;
3133 const struct lu_ucred *uc = lu_ucred(env);
3136 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
3137 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3138 mdd_obj->mod_count--;
3139 mdd_write_unlock(env, mdd_obj);
3141 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
3142 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
3143 "list\n", PFID(mdd_object_fid(mdd_obj)));
3147 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
3148 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
3149 /* check without any lock */
3150 is_orphan = mdd_obj->mod_count == 1 &&
3151 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
3155 /* mdd_trans_create() maybe failed because of barrier_entry(),
3156 * under such case, the orphan MDT-object will be left in the
3157 * orphan list, and when the MDT remount next time, the unused
3158 * orphans will be destroyed automatically.
3160 * One exception: the former mdd_finish_unlink may failed to
3161 * add the orphan MDT-object to the orphan list, then if the
3162 * mdd_trans_create() failed because of barrier_entry(), the
3163 * MDT-object will become real orphan that is neither in the
3164 * namespace nor in the orphan list. Such bad case should be
3165 * very rare and will be handled by e2fsck/lfsck. */
3166 handle = mdd_trans_create(env, mdo2mdd(obj));
3167 if (IS_ERR(handle)) {
3168 rc = PTR_ERR(handle);
3169 if (rc != -EINPROGRESS)
3177 rc = mdd_declare_close(env, mdd_obj, ma, handle);
3181 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE, NULL, NULL,
3186 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3192 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
3193 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
3195 CERROR("%s: failed to get lu_attr of "DFID": rc = %d\n",
3196 lu_dev_name(mdd2lu_dev(mdd)),
3197 PFID(mdd_object_fid(mdd_obj)), rc);
3201 /* check again with lock */
3202 is_orphan = (mdd_obj->mod_count == 1) &&
3203 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
3204 ma->ma_attr.la_nlink == 0);
3206 if (is_orphan && !handle && !blocked) {
3207 mdd_write_unlock(env, mdd_obj);
3211 mdd_obj->mod_count--; /*release open count */
3213 /* under mdd write lock */
3214 /* If recording, see if we need to remove UID from list */
3215 if (mdd_changelog_enabled(env, mdd, CL_OPEN)) {
3216 struct mdd_object_user *mou;
3218 /* look for UID in list */
3219 /* If mou is NULL, it probably means logging was enabled after
3220 * the user had the file open. So the corresponding close
3221 * will not be logged.
3223 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid,
3226 mou->mou_opencount--;
3227 if (mou->mou_opencount == 0) {
3228 mdd_obj_user_remove(mdd_obj, mou);
3229 last_close_by_uid = 1;
3234 if (!is_orphan || blocked)
3238 /* NB: Object maybe not in orphan list originally, it is rare case for
3239 * mdd_finish_unlink() failure, in that case, the object doesn't have
3240 * ORPHAN_OBJ flag */
3241 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
3242 /* remove link to object from orphan index */
3243 LASSERT(handle != NULL);
3244 rc = mdd_orphan_delete(env, mdd_obj, handle);
3246 CERROR("%s: unable to delete "DFID" from orphan list: "
3247 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3248 PFID(mdd_object_fid(mdd_obj)), rc);
3249 /* If object was not deleted from orphan list, do not
3250 * destroy OSS objects, which will be done when next
3255 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
3256 "list, OSS objects to be destroyed.\n",
3257 PFID(mdd_object_fid(mdd_obj)));
3260 rc = mdo_destroy(env, mdd_obj, handle);
3263 CERROR("%s: unable to delete "DFID" from orphan list: "
3264 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3265 PFID(mdd_object_fid(mdd_obj)),&