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 && 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 return lu_env_info(env, &mdd_thread_key);
249 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
253 buf = &mdd_env_info(env)->mti_buf[0];
259 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
260 const void *area, ssize_t len)
264 buf = &mdd_env_info(env)->mti_buf[0];
265 buf->lb_buf = (void *)area;
270 struct lu_object *mdd_object_alloc(const struct lu_env *env,
271 const struct lu_object_header *hdr,
274 struct mdd_object *mdd_obj;
277 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
281 o = mdd2lu_obj(mdd_obj);
282 lu_object_init(o, NULL, d);
283 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
284 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
285 mdd_obj->mod_count = 0;
286 o->lo_ops = &mdd_lu_obj_ops;
287 INIT_LIST_HEAD(&mdd_obj->mod_users);
292 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
293 const struct lu_object_conf *unused)
295 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
296 struct mdd_object *mdd_obj = lu2mdd_obj(o);
297 struct lu_object *below;
298 struct lu_device *under;
301 mdd_obj->mod_cltime = ktime_set(0, 0);
302 under = &d->mdd_child->dd_lu_dev;
303 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
305 RETURN(PTR_ERR(below));
307 lu_object_add(o, below);
312 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
316 if (lu_object_exists(o)) {
317 struct mdd_object *mdd_obj = lu2mdd_obj(o);
318 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
320 rc = mdd_la_get(env, mdd_obj, attr);
326 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
328 struct mdd_object *mdd = lu2mdd_obj(o);
329 struct mdd_object_user *mou, *tmp2;
332 list_for_each_entry_safe(mou, tmp2, &mdd->mod_users, mou_list) {
333 list_del(&mou->mou_list);
334 mdd_obj_user_free(mou);
338 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
341 static int mdd_object_print(const struct lu_env *env, void *cookie,
342 lu_printer_t p, const struct lu_object *o)
344 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
346 return (*p)(env, cookie,
347 LUSTRE_MDD_NAME"-object@%p(open_count=%d, valid=%x, cltime=%lldns, flags=%lx)",
348 mdd, mdd->mod_count, mdd->mod_valid,
349 ktime_to_ns(mdd->mod_cltime), mdd->mod_flags);
352 static const struct lu_object_operations mdd_lu_obj_ops = {
353 .loo_object_init = mdd_object_init,
354 .loo_object_start = mdd_object_start,
355 .loo_object_free = mdd_object_free,
356 .loo_object_print = mdd_object_print,
359 struct mdd_object *mdd_object_find(const struct lu_env *env,
360 struct mdd_device *d,
361 const struct lu_fid *f)
363 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
367 * No permission check is needed.
369 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
372 struct mdd_object *mdd_obj = md2mdd_obj(obj);
377 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
378 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
379 ma->ma_attr.la_nlink = 0;
385 * No permission check is needed.
387 static int mdd_xattr_get(const struct lu_env *env,
388 struct md_object *obj, struct lu_buf *buf,
391 struct mdd_object *mdd_obj = md2mdd_obj(obj);
392 struct mdd_device *mdd;
397 if (mdd_object_exists(mdd_obj) == 0) {
398 CERROR("%s: object "DFID" not found: rc = -2\n",
399 mdd_obj_dev_name(mdd_obj),
400 PFID(mdd_object_fid(mdd_obj)));
404 /* If the object has been destroyed, then do not get LMVEA, because
405 * it needs to load stripes from the iteration of the master object,
406 * and it will cause problem if master object has been destroyed, see
408 if (unlikely((mdd_obj->mod_flags & DEAD_OBJ) &&
409 !(mdd_obj->mod_flags & ORPHAN_OBJ) &&
410 strcmp(name, XATTR_NAME_LMV) == 0))
413 /* If the object has been delete from the namespace, then
414 * get linkEA should return -ENOENT as well */
415 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
416 strcmp(name, XATTR_NAME_LINK) == 0))
419 mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
420 rc = mdo_xattr_get(env, mdd_obj, buf, name);
421 mdd_read_unlock(env, mdd_obj);
425 /* record only getting user xattrs and acls */
426 if (rc >= 0 && buf->lb_buf &&
427 mdd_changelog_enabled(env, mdd, CL_GETXATTR) &&
428 (has_prefix(name, XATTR_USER_PREFIX) ||
429 has_prefix(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
430 has_prefix(name, XATTR_NAME_POSIX_ACL_DEFAULT))) {
431 struct thandle *handle;
434 LASSERT(mdo2fid(mdd_obj) != NULL);
436 handle = mdd_trans_create(env, mdd);
438 RETURN(PTR_ERR(handle));
440 rc2 = mdd_declare_changelog_store(env, mdd, CL_GETXATTR, NULL,
445 rc2 = mdd_trans_start(env, mdd, handle);
449 rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
450 mdo2fid(mdd_obj), name,
454 rc2 = mdd_trans_stop(env, mdd, rc2, handle);
463 * Permission check is done when open,
464 * no need check again.
466 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
469 struct mdd_object *mdd_obj = md2mdd_obj(obj);
470 struct dt_object *next;
475 if (mdd_object_exists(mdd_obj) == 0) {
476 CERROR("%s: object "DFID" not found: rc = -2\n",
477 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
481 next = mdd_object_child(mdd_obj);
482 LASSERT(next != NULL);
483 LASSERT(next->do_body_ops != NULL);
484 LASSERT(next->do_body_ops->dbo_read != NULL);
485 mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
486 rc = dt_read(env, next, buf, &pos);
487 mdd_read_unlock(env, mdd_obj);
492 * No permission check is needed.
494 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
497 struct mdd_object *mdd_obj = md2mdd_obj(obj);
502 mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
503 rc = mdo_xattr_list(env, mdd_obj, buf);
504 mdd_read_unlock(env, mdd_obj);
506 /* If the buffer is NULL then we are only here to get the
507 * length of the xattr name list. */
508 if (rc < 0 || buf->lb_buf == NULL)
512 * Filter out XATTR_NAME_LINK if this is an orphan object. See
515 if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
516 char *end = (char *)buf->lb_buf + rc;
517 char *p = buf->lb_buf;
520 char *next = p + strlen(p) + 1;
522 if (strcmp(p, XATTR_NAME_LINK) == 0) {
524 memmove(p, next, end - next);
526 CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
527 " of orphan "DFID"\n",
528 PFID(mdd_object_fid(mdd_obj)));
539 int mdd_invalidate(const struct lu_env *env, struct md_object *obj)
541 return mdo_invalidate(env, md2mdd_obj(obj));
544 int mdd_declare_create_object_internal(const struct lu_env *env,
545 struct mdd_object *p,
546 struct mdd_object *c,
547 struct lu_attr *attr,
548 struct thandle *handle,
549 const struct md_op_spec *spec,
550 struct dt_allocation_hint *hint)
552 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
553 const struct dt_index_features *feat = spec->sp_feat;
557 if (feat != &dt_directory_features && feat != NULL) {
558 dof->dof_type = DFT_INDEX;
559 dof->u.dof_idx.di_feat = feat;
561 dof->dof_type = dt_mode_to_dft(attr->la_mode);
562 if (dof->dof_type == DFT_REGULAR) {
563 dof->u.dof_reg.striped =
564 md_should_create(spec->sp_cr_flags);
565 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
566 dof->u.dof_reg.striped = 0;
567 /* is this replay? */
569 dof->u.dof_reg.striped = 0;
573 rc = mdo_declare_create_object(env, c, attr, hint, dof, handle);
578 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
579 struct mdd_object *c, struct lu_attr *attr,
580 struct thandle *handle,
581 const struct md_op_spec *spec,
582 struct dt_allocation_hint *hint)
584 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
588 LASSERT(!mdd_object_exists(c));
590 rc = mdo_create_object(env, c, attr, hint, dof, handle);
595 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
596 const struct lu_attr *attr, struct thandle *handle,
602 rc = mdo_attr_set(env, obj, attr, handle);
603 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
604 if (!rc && (attr->la_valid & LA_MODE) && needacl)
605 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
610 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
611 const struct lu_attr *oattr, struct lu_attr *attr,
612 struct thandle *handle)
617 LASSERT(attr->la_valid & LA_CTIME);
618 LASSERT(oattr != NULL);
620 /* Make sure the ctime is increased only, however, it's not strictly
621 * reliable at here because there is not guarantee to hold lock on
622 * object, so we just bypass some unnecessary cmtime setting first
623 * and OSD has to check it again. */
624 if (attr->la_ctime < oattr->la_ctime)
625 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
626 else if (attr->la_valid == LA_CTIME &&
627 attr->la_ctime == oattr->la_ctime)
628 attr->la_valid &= ~LA_CTIME;
630 if (attr->la_valid != 0)
631 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
636 * This gives the same functionality as the code between
637 * sys_chmod and inode_setattr
638 * chown_common and inode_setattr
639 * utimes and inode_setattr
640 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
642 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
643 const struct lu_attr *oattr, struct lu_attr *la,
644 const unsigned long flags)
653 /* Do not permit change file type */
654 if (la->la_valid & LA_TYPE)
657 /* They should not be processed by setattr */
658 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
661 LASSERT(oattr != NULL);
663 uc = lu_ucred_check(env);
667 if (la->la_valid == LA_CTIME) {
668 if (!(flags & MDS_PERM_BYPASS))
669 /* This is only for set ctime when rename's source is
671 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
673 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
674 la->la_valid &= ~LA_CTIME;
678 if (flags & MDS_CLOSE_UPDATE_TIMES &&
679 la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
680 /* This is an atime/mtime/ctime attribute update for
683 if (la->la_valid & LA_ATIME &&
684 la->la_atime <= (oattr->la_atime +
685 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
686 la->la_valid &= ~LA_ATIME;
687 if (la->la_valid & LA_CTIME && la->la_ctime <= oattr->la_ctime)
688 la->la_valid &= ~LA_CTIME;
689 if (la->la_valid & LA_MTIME && la->la_mtime <= oattr->la_mtime)
690 la->la_valid &= ~LA_MTIME;
694 /* Check if flags change. */
695 if (la->la_valid & LA_FLAGS) {
696 unsigned int oldflags = oattr->la_flags &
697 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
698 unsigned int newflags = la->la_flags &
699 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
701 if ((uc->uc_fsuid != oattr->la_uid) &&
702 !md_capable(uc, CFS_CAP_FOWNER))
705 /* The IMMUTABLE and APPEND_ONLY flags can
706 * only be changed by the relevant capability. */
707 if ((oldflags ^ newflags) &&
708 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
711 if (!S_ISDIR(oattr->la_mode))
712 la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
715 if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
716 (la->la_valid & ~LA_FLAGS) &&
717 !(flags & MDS_PERM_BYPASS))
720 /* Check for setting the obj time. */
721 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
722 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
723 if ((uc->uc_fsuid != oattr->la_uid) &&
724 !md_capable(uc, CFS_CAP_FOWNER)) {
725 rc = mdd_permission_internal(env, obj, oattr,
732 if (la->la_valid & LA_KILL_SUID) {
733 la->la_valid &= ~LA_KILL_SUID;
734 if ((oattr->la_mode & S_ISUID) &&
735 !(la->la_valid & LA_MODE)) {
736 la->la_mode = oattr->la_mode;
737 la->la_valid |= LA_MODE;
739 la->la_mode &= ~S_ISUID;
742 if (la->la_valid & LA_KILL_SGID) {
743 la->la_valid &= ~LA_KILL_SGID;
744 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
745 (S_ISGID | S_IXGRP)) &&
746 !(la->la_valid & LA_MODE)) {
747 la->la_mode = oattr->la_mode;
748 la->la_valid |= LA_MODE;
750 la->la_mode &= ~S_ISGID;
753 /* Make sure a caller can chmod. */
754 if (la->la_valid & LA_MODE) {
755 if (!(flags & MDS_PERM_BYPASS) &&
756 (uc->uc_fsuid != oattr->la_uid) &&
757 !md_capable(uc, CFS_CAP_FOWNER))
760 if (la->la_mode == (umode_t) -1)
761 la->la_mode = oattr->la_mode;
763 la->la_mode = (la->la_mode & S_IALLUGO) |
764 (oattr->la_mode & ~S_IALLUGO);
766 /* Also check the setgid bit! */
767 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
768 la->la_gid : oattr->la_gid) &&
769 !md_capable(uc, CFS_CAP_FSETID))
770 la->la_mode &= ~S_ISGID;
772 la->la_mode = oattr->la_mode;
775 /* Make sure a caller can chown. */
776 if (la->la_valid & LA_UID) {
777 if (la->la_uid == (uid_t) -1)
778 la->la_uid = oattr->la_uid;
779 if (((uc->uc_fsuid != oattr->la_uid) ||
780 (la->la_uid != oattr->la_uid)) &&
781 !md_capable(uc, CFS_CAP_CHOWN))
784 /* If the user or group of a non-directory has been
785 * changed by a non-root user, remove the setuid bit.
786 * 19981026 David C Niemi <niemi@tux.org>
788 * Changed this to apply to all users, including root,
789 * to avoid some races. This is the behavior we had in
790 * 2.0. The check for non-root was definitely wrong
791 * for 2.2 anyway, as it should have been using
792 * CAP_FSETID rather than fsuid -- 19990830 SD. */
793 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
794 !S_ISDIR(oattr->la_mode)) {
795 la->la_mode &= ~S_ISUID;
796 la->la_valid |= LA_MODE;
800 /* Make sure caller can chgrp. */
801 if (la->la_valid & LA_GID) {
802 if (la->la_gid == (gid_t) -1)
803 la->la_gid = oattr->la_gid;
804 if (((uc->uc_fsuid != oattr->la_uid) ||
805 ((la->la_gid != oattr->la_gid) &&
806 !lustre_in_group_p(uc, la->la_gid))) &&
807 !md_capable(uc, CFS_CAP_CHOWN))
810 /* Likewise, if the user or group of a non-directory
811 * has been changed by a non-root user, remove the
812 * setgid bit UNLESS there is no group execute bit
813 * (this would be a file marked for mandatory
814 * locking). 19981026 David C Niemi <niemi@tux.org>
816 * Removed the fsuid check (see the comment above) --
818 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
819 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
820 la->la_mode &= ~S_ISGID;
821 la->la_valid |= LA_MODE;
825 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
826 if (!((flags & MDS_OWNEROVERRIDE) &&
827 (uc->uc_fsuid == oattr->la_uid)) &&
828 !(flags & MDS_PERM_BYPASS)) {
829 rc = mdd_permission_internal(env, obj, oattr,
836 if (la->la_valid & LA_CTIME) {
837 /* The pure setattr, it has the priority over what is
838 * already set, do not drop it if ctime is equal. */
839 if (la->la_ctime < oattr->la_ctime)
840 la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
846 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
847 struct mdd_device *mdd,
848 enum changelog_rec_type type,
849 enum changelog_rec_flags clf_flags,
850 const struct lu_fid *fid,
851 const char *xattr_name,
852 struct thandle *handle)
854 const struct lu_ucred *uc = lu_ucred(env);
855 enum changelog_rec_extra_flags xflags = CLFE_INVALID;
856 struct llog_changelog_rec *rec;
861 clf_flags = (clf_flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
864 if (uc->uc_jobid[0] != '\0')
865 clf_flags |= CLF_JOBID;
866 xflags |= CLFE_UIDGID;
869 if (type == CL_OPEN || type == CL_DN_OPEN)
871 if (type == CL_SETXATTR || type == CL_GETXATTR)
872 xflags |= CLFE_XATTR;
874 reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
875 changelog_rec_offset(clf_flags & CLF_SUPPORTED,
876 xflags & CLFE_SUPPORTED));
877 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
878 if (buf->lb_buf == NULL)
882 rec->cr_hdr.lrh_len = reclen;
883 rec->cr.cr_flags = clf_flags;
884 rec->cr.cr_type = (__u32)type;
885 rec->cr.cr_tfid = *fid;
886 rec->cr.cr_namelen = 0;
888 if (clf_flags & CLF_JOBID)
889 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
891 if (clf_flags & CLF_EXTRA_FLAGS) {
892 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
893 if (xflags & CLFE_UIDGID)
894 mdd_changelog_rec_extra_uidgid(&rec->cr,
895 uc->uc_uid, uc->uc_gid);
896 if (xflags & CLFE_NID)
897 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
898 if (xflags & CLFE_OPEN)
899 mdd_changelog_rec_extra_omode(&rec->cr, clf_flags);
900 if (xflags & CLFE_XATTR) {
901 if (xattr_name == NULL)
903 mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
907 rc = mdd_changelog_store(env, mdd, rec, handle);
912 /** Store a data change changelog record
913 * If this fails, we must fail the whole transaction; we don't
914 * want the change to commit without the log entry.
915 * \param mdd_obj - mdd_object of change
916 * \param handle - transaction handle
918 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
919 enum changelog_rec_type type,
920 enum changelog_rec_flags clf_flags,
921 struct mdd_object *mdd_obj, struct thandle *handle)
925 LASSERT(mdd_obj != NULL);
926 LASSERT(handle != NULL);
928 if (!mdd_changelog_enabled(env, mdd, type))
931 if (mdd_is_volatile_obj(mdd_obj))
934 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
935 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
936 /* Don't need multiple updates in this log */
937 /* Don't check under lock - no big deal if we get an extra
942 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
943 mdo2fid(mdd_obj), NULL, handle);
945 mdd_obj->mod_cltime = ktime_get();
950 int mdd_changelog_data_store_xattr(const struct lu_env *env,
951 struct mdd_device *mdd,
952 enum changelog_rec_type type,
953 enum changelog_rec_flags clf_flags,
954 struct mdd_object *mdd_obj,
955 const char *xattr_name,
956 struct thandle *handle)
960 LASSERT(mdd_obj != NULL);
961 LASSERT(handle != NULL);
963 if (!mdd_changelog_enabled(env, mdd, type))
966 if (mdd_is_volatile_obj(mdd_obj))
969 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
970 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
971 /* Don't need multiple updates in this log */
972 /* Don't check under lock - no big deal if we get an extra
978 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
979 mdo2fid(mdd_obj), xattr_name,
982 mdd_obj->mod_cltime = ktime_get();
987 /* only the bottom CLF_FLAGSHIFT bits of @flags are stored in the record,
988 * except for open flags have a dedicated record to store 32 bits of flags */
989 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
990 enum changelog_rec_flags clf_flags,
991 struct md_device *m, const struct lu_fid *fid)
993 struct thandle *handle;
994 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
998 LASSERT(fid != NULL);
1000 /* We'll check this again below, but we check now before we
1001 * start a transaction. */
1002 if (!mdd_changelog_enabled(env, mdd, type))
1005 handle = mdd_trans_create(env, mdd);
1007 RETURN(PTR_ERR(handle));
1009 rc = mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1013 rc = mdd_trans_start(env, mdd, handle);
1017 rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
1021 rc = mdd_trans_stop(env, mdd, rc, handle);
1027 * Save LMA extended attributes with data from \a ma.
1029 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
1030 * not, LMA EA will be first read from disk, modified and write back.
1033 /* Precedence for choosing record type when multiple
1034 * attributes change: setattr > mtime > ctime > atime
1035 * (ctime changes when mtime does, plus chmod/chown.
1036 * atime and ctime are independent.) */
1037 static int mdd_attr_set_changelog(const struct lu_env *env,
1038 struct md_object *obj, struct thandle *handle,
1041 struct mdd_device *mdd = mdo2mdd(obj);
1044 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
1045 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
1046 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
1047 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
1048 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
1049 bits = bits & mdd->mdd_cl.mc_mask;
1050 /* This is an implementation limit rather than a protocol limit */
1051 BUILD_BUG_ON(CL_LAST > sizeof(int) * 8);
1055 /* The record type is the lowest non-masked set bit */
1058 /* XXX: we only store the low CLF_FLAGMASK bits of la_valid */
1059 return mdd_changelog_data_store(env, mdd, type, valid,
1060 md2mdd_obj(obj), handle);
1063 static int mdd_declare_attr_set(const struct lu_env *env,
1064 struct mdd_device *mdd,
1065 struct mdd_object *obj,
1066 const struct lu_attr *attr,
1067 struct thandle *handle)
1071 rc = mdo_declare_attr_set(env, obj, attr, handle);
1075 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1076 if (attr->la_valid & LA_MODE) {
1077 mdd_read_lock(env, obj, DT_TGT_CHILD);
1078 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
1079 XATTR_NAME_ACL_ACCESS);
1080 mdd_read_unlock(env, obj);
1081 if (rc == -EOPNOTSUPP || rc == -ENODATA)
1087 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
1088 rc = mdo_declare_xattr_set(env, obj, buf,
1089 XATTR_NAME_ACL_ACCESS, 0,
1097 rc = mdd_declare_changelog_store(env, mdd, CL_SETXATTR, NULL, NULL,
1106 * permission changes may require sync operation, to mitigate performance
1107 * impact, only do this for dir and when permission is reduced.
1109 * For regular files, version is updated with permission change (see VBR), async
1110 * permission won't cause any issue, while missing permission change on
1111 * directory may affect accessibility of other objects after recovery.
1113 static inline bool permission_needs_sync(const struct lu_attr *old,
1114 const struct lu_attr *new)
1116 if (!S_ISDIR(old->la_mode))
1119 if (new->la_valid & LA_UID && old->la_uid != new->la_uid)
1122 if (new->la_valid & LA_GID && old->la_gid != new->la_gid)
1125 if (new->la_valid & LA_MODE) {
1126 /* turned on sticky bit */
1127 if (!(old->la_mode & S_ISVTX) && (new->la_mode & S_ISVTX))
1130 /* set-GID has no impact on what is allowed, not checked */
1132 /* turned off setuid bit, or one of rwx for someone */
1133 if (((new->la_mode & old->la_mode) & (0777 | S_ISUID)) !=
1134 (old->la_mode & (0777 | S_ISUID)))
1141 static inline __u64 mdd_lmm_dom_size(void *buf)
1143 struct lov_mds_md *lmm = buf;
1144 struct lov_comp_md_v1 *comp_v1;
1145 struct lov_mds_md *v1;
1151 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1154 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1155 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1156 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1158 /* DoM entry is the first entry always */
1159 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) == LOV_PATTERN_MDT)
1160 return le64_to_cpu(comp_v1->lcm_entries[0].lcme_extent.e_end);
1165 /* set attr and LOV EA at once, return updated attr */
1166 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
1167 const struct md_attr *ma)
1169 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1170 struct mdd_device *mdd = mdo2mdd(obj);
1171 struct thandle *handle = NULL;
1172 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1173 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1174 const struct lu_attr *la = &ma->ma_attr;
1175 struct lu_ucred *uc;
1176 bool chrgrp_by_unprivileged_user = false;
1180 /* we do not use ->attr_set() for LOV/HSM EA any more */
1181 LASSERT((ma->ma_valid & MA_LOV) == 0);
1182 LASSERT((ma->ma_valid & MA_HSM) == 0);
1184 rc = mdd_la_get(env, mdd_obj, attr);
1188 *la_copy = ma->ma_attr;
1189 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
1193 /* no need to setattr anymore */
1194 if (la_copy->la_valid == 0) {
1195 CDEBUG(D_INODE, "%s: no valid attribute on "DFID", previous"
1196 "valid is %#llx\n", mdd2obd_dev(mdd)->obd_name,
1197 PFID(mdo2fid(mdd_obj)), la->la_valid);
1202 /* If an unprivileged user changes group of some file,
1203 * the setattr operation will be processed synchronously to
1204 * honor the quota limit of the corresponding group. see LU-5152 */
1205 uc = lu_ucred_check(env);
1206 if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1207 la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1208 /* LU-10048: disable synchronous chgrp operation for it will
1209 * cause deadlock between MDT and OST.
1210 la_copy->la_valid |= LA_FLAGS;
1211 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1213 chrgrp_by_unprivileged_user = true;
1215 /* Flush the possible existing client setattr requests to OSTs
1216 * to keep the order with the current setattr operation that
1217 * will be sent directly to OSTs. see LU-5152 */
1218 /* LU-11303 disable sync as this is too heavyweight.
1219 * This should be replaced with a sync only for the object
1220 * being modified here, not the whole filesystem.
1221 rc = dt_sync(env, mdd->mdd_child);
1227 handle = mdd_trans_create(env, mdd);
1228 if (IS_ERR(handle)) {
1229 rc = PTR_ERR(handle);
1235 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1239 rc = mdd_trans_start(env, mdd, handle);
1243 if (!chrgrp_by_unprivileged_user && mdd->mdd_sync_permission &&
1244 permission_needs_sync(attr, la))
1245 handle->th_sync = 1;
1247 if (la->la_valid & (LA_MTIME | LA_CTIME))
1248 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1249 la->la_mtime, la->la_ctime);
1251 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
1253 /* LU-10509: setattr of LA_SIZE should be skipped case of DOM,
1254 * otherwise following truncate will do nothing and truncated
1255 * data may be read again. This is a quick fix until LU-11033
1258 if (la_copy->la_valid & LA_SIZE) {
1259 struct lu_buf *lov_buf = mdd_buf_get(env, NULL, 0);
1261 rc = mdd_stripe_get(env, mdd_obj, lov_buf, XATTR_NAME_LOV);
1265 if (mdd_lmm_dom_size(lov_buf->lb_buf) > 0)
1266 la_copy->la_valid &= ~LA_SIZE;
1267 lu_buf_free(lov_buf);
1271 if (la_copy->la_valid) {
1272 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1274 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1275 /* rollback to the original gid */
1276 la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1277 la_copy->la_gid = attr->la_gid;
1278 mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1281 mdd_write_unlock(env, mdd_obj);
1285 rc = mdd_attr_set_changelog(env, obj, handle,
1289 rc = mdd_trans_stop(env, mdd, rc, handle);
1294 static int mdd_xattr_sanity_check(const struct lu_env *env,
1295 struct mdd_object *obj,
1296 const struct lu_attr *attr,
1299 struct lu_ucred *uc = lu_ucred_assert(env);
1302 if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1305 if (strncmp(XATTR_USER_PREFIX, name,
1306 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1307 /* For sticky directories, only the owner and privileged user
1308 * can write attributes. */
1309 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1310 (uc->uc_fsuid != attr->la_uid) &&
1311 !md_capable(uc, CFS_CAP_FOWNER))
1313 } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
1314 (uc->uc_fsuid != attr->la_uid) &&
1315 !md_capable(uc, CFS_CAP_FOWNER)) {
1323 * Check if a string begins with a given prefix.
1325 * \param str String to check
1326 * \param prefix Substring to check at the beginning of \a str
1327 * \return true/false whether the condition is verified.
1329 static inline bool has_prefix(const char *str, const char *prefix)
1331 return strncmp(prefix, str, strlen(prefix)) == 0;
1335 * Indicate the kind of changelog to store (if any) for a xattr set/del.
1337 * \param[in] xattr_name Full extended attribute name.
1339 * \return The type of changelog to use, or -1 if no changelog is to be emitted.
1341 static enum changelog_rec_type
1342 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1343 const char *xattr_name)
1345 /* Layout changes systematically recorded */
1346 if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1347 strncmp(XATTR_LUSTRE_LOV, xattr_name,
1348 strlen(XATTR_LUSTRE_LOV)) == 0)
1351 /* HSM information changes systematically recorded */
1352 if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1355 /* Avoid logging SOM xattr for every file */
1356 if (strcmp(XATTR_NAME_SOM, xattr_name) == 0)
1359 if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1360 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1361 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1362 has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1363 has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1369 static int mdd_declare_xattr_set(const struct lu_env *env,
1370 struct mdd_device *mdd,
1371 struct mdd_object *obj,
1372 const struct lu_buf *buf,
1374 int fl, struct thandle *handle)
1376 enum changelog_rec_type type;
1379 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1383 type = mdd_xattr_changelog_type(env, mdd, name);
1385 return 0; /* no changelog to store */
1387 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1391 * Compare current and future data of HSM EA and add a changelog if needed.
1393 * Caller should have write-locked \param obj.
1395 * \param buf - Future HSM EA content.
1396 * \retval 0 if no changelog is needed or changelog was added properly.
1397 * \retval -ve errno if there was a problem
1399 static int mdd_hsm_update_locked(const struct lu_env *env,
1400 struct md_object *obj,
1401 const struct lu_buf *buf,
1402 struct thandle *handle,
1403 enum changelog_rec_flags *clf_flags)
1405 struct mdd_thread_info *info = mdd_env_info(env);
1406 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1407 struct lu_buf *current_buf;
1408 struct md_hsm *current_mh;
1409 struct md_hsm *new_mh;
1413 OBD_ALLOC_PTR(current_mh);
1414 if (current_mh == NULL)
1417 /* Read HSM attrs from disk */
1418 current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1419 MIN(mdd_obj2mdd_dev(mdd_obj)->mdd_dt_conf.ddp_max_ea_size,
1421 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1422 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1423 if (rc < 0 && rc != -ENODATA)
1425 else if (rc == -ENODATA)
1426 current_mh->mh_flags = 0;
1428 /* Map future HSM xattr */
1429 OBD_ALLOC_PTR(new_mh);
1431 GOTO(free, rc = -ENOMEM);
1432 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1436 /* Flags differ, set flags for the changelog that will be added */
1437 if (current_mh->mh_flags != new_mh->mh_flags) {
1438 hsm_set_cl_event(clf_flags, HE_STATE);
1439 if (new_mh->mh_flags & HS_DIRTY)
1440 hsm_set_cl_flags(clf_flags, CLF_HSM_DIRTY);
1443 OBD_FREE_PTR(new_mh);
1446 OBD_FREE_PTR(current_mh);
1450 static int mdd_object_pfid_replace(const struct lu_env *env,
1451 struct mdd_object *o)
1453 struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1454 struct thandle *handle;
1457 handle = mdd_trans_create(env, mdd);
1459 RETURN(PTR_ERR(handle));
1461 handle->th_complex = 1;
1463 /* it doesn't need to track the PFID update via llog, because LFSCK
1464 * will repair it even it goes wrong */
1465 rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1470 rc = mdd_trans_start(env, mdd, handle);
1474 rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1479 mdd_trans_stop(env, mdd, rc, handle);
1484 static int mdd_declare_xattr_del(const struct lu_env *env,
1485 struct mdd_device *mdd,
1486 struct mdd_object *obj,
1488 struct thandle *handle);
1490 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1493 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1494 struct md_object *md_vic)
1496 struct mdd_device *mdd = mdo2mdd(md_obj);
1497 struct mdd_object *obj = md2mdd_obj(md_obj);
1498 struct mdd_object *vic = md2mdd_obj(md_vic);
1499 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1500 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1501 struct lov_mds_md *lmm;
1502 struct thandle *handle;
1506 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1507 if (rc == 0) /* same fid */
1510 handle = mdd_trans_create(env, mdd);
1512 RETURN(PTR_ERR(handle));
1515 mdd_write_lock(env, obj, DT_TGT_CHILD);
1516 mdd_write_lock(env, vic, DT_TGT_CHILD);
1518 mdd_write_lock(env, vic, DT_TGT_CHILD);
1519 mdd_write_lock(env, obj, DT_TGT_CHILD);
1522 /* get EA of victim file */
1523 memset(buf_vic, 0, sizeof(*buf_vic));
1524 rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
1531 /* parse the layout of victim file */
1532 lmm = buf_vic->lb_buf;
1533 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1534 GOTO(out, rc = -EINVAL);
1536 /* save EA of target file for restore */
1537 memset(buf, 0, sizeof(*buf));
1538 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
1542 /* Get rid of the layout from victim object */
1543 rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1547 rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1548 LU_XATTR_MERGE, handle);
1552 rc = mdd_trans_start(env, mdd, handle);
1556 rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1561 rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1563 GOTO(out_restore, rc);
1565 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1566 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1571 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1572 LU_XATTR_REPLACE, handle);
1574 CERROR("%s: failed to rollback of layout of: "DFID
1575 ": %d, file state unknown\n",
1576 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1580 mdd_trans_stop(env, mdd, rc, handle);
1581 mdd_write_unlock(env, obj);
1582 mdd_write_unlock(env, vic);
1584 lu_buf_free(buf_vic);
1587 (void) mdd_object_pfid_replace(env, obj);
1593 * Extract the mirror with specified mirror id, and store the splitted
1594 * mirror layout to @buf.
1596 * \param[in] comp_v1 mirrored layout
1597 * \param[in] mirror_id the mirror with mirror_id to be extracted
1598 * \param[out] buf store the layout excluding the extracted mirror,
1599 * caller free the buffer we allocated in this function
1600 * \param[out] buf_vic store the extracted layout, caller free the buffer
1601 * we allocated in this function
1603 * \retval 0 on success; < 0 if error happens
1605 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1606 struct lu_buf *buf, struct lu_buf *buf_vic)
1608 struct lov_comp_md_v1 *comp_rem;
1609 struct lov_comp_md_v1 *comp_vic;
1610 struct lov_comp_md_entry_v1 *entry;
1611 struct lov_comp_md_entry_v1 *entry_rem;
1612 struct lov_comp_md_entry_v1 *entry_vic;
1614 __u16 comp_cnt, count = 0;
1615 int lmm_size, lmm_size_vic = 0;
1617 int offset, offset_rem, offset_vic;
1619 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1620 /* comp_v1 should contains more than 1 mirror */
1621 if (mirror_cnt <= 1)
1623 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1624 lmm_size = le32_to_cpu(comp_v1->lcm_size);
1626 for (i = 0; i < comp_cnt; i++) {
1627 entry = &comp_v1->lcm_entries[i];
1628 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1630 lmm_size_vic += sizeof(*entry);
1631 lmm_size_vic += le32_to_cpu(entry->lcme_size);
1632 } else if (count > 0) {
1633 /* find the specified mirror */
1641 lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1645 lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1646 if (!buf_vic->lb_buf) {
1651 comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1652 comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1654 memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1655 comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1656 comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1657 comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1658 if (!comp_rem->lcm_mirror_count)
1659 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1661 memset(comp_vic, 0, sizeof(*comp_v1));
1662 comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1663 comp_vic->lcm_mirror_count = 0;
1664 comp_vic->lcm_entry_count = cpu_to_le32(count);
1665 comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1666 comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1667 comp_vic->lcm_layout_gen = 0;
1669 offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1670 offset_rem = sizeof(*comp_rem) +
1671 sizeof(*entry_rem) * (comp_cnt - count);
1672 offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1673 for (i = j = k = 0; i < comp_cnt; i++) {
1674 struct lov_mds_md *lmm, *lmm_dst;
1677 entry = &comp_v1->lcm_entries[i];
1678 entry_vic = &comp_vic->lcm_entries[j];
1679 entry_rem = &comp_rem->lcm_entries[k];
1681 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1684 /* copy component entry */
1686 memcpy(entry_vic, entry, sizeof(*entry));
1687 entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1688 entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1691 memcpy(entry_rem, entry, sizeof(*entry));
1692 entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1696 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1698 lmm_dst = (struct lov_mds_md *)
1699 ((char *)comp_vic + offset_vic);
1701 lmm_dst = (struct lov_mds_md *)
1702 ((char *)comp_rem + offset_rem);
1704 /* copy component entry blob */
1705 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1707 /* blob offset advance */
1708 offset += le32_to_cpu(entry->lcme_size);
1710 offset_vic += le32_to_cpu(entry->lcme_size);
1712 offset_rem += le32_to_cpu(entry->lcme_size);
1718 static int mdd_dom_data_truncate(const struct lu_env *env,
1719 struct mdd_device *mdd, struct mdd_object *mo);
1721 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1722 struct md_rejig_data *mrd)
1724 struct mdd_device *mdd = mdo2mdd(md_obj);
1725 struct mdd_object *obj = md2mdd_obj(md_obj);
1726 struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
1727 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1728 struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1729 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1730 struct lov_comp_md_v1 *lcm;
1731 struct thandle *handle;
1733 bool dom_stripe = false;
1737 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1738 if (rc == 0) /* same fid */
1741 handle = mdd_trans_create(env, mdd);
1743 RETURN(PTR_ERR(handle));
1746 mdd_write_lock(env, obj, DT_TGT_CHILD);
1747 mdd_write_lock(env, vic, DT_TGT_CHILD);
1749 mdd_write_lock(env, vic, DT_TGT_CHILD);
1750 mdd_write_lock(env, obj, DT_TGT_CHILD);
1753 /* get EA of mirrored file */
1754 memset(buf_save, 0, sizeof(*buf));
1755 rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
1759 lcm = buf_save->lb_buf;
1760 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1761 GOTO(out, rc = -EINVAL);
1764 * Extract the mirror with specified mirror id, and store the splitted
1765 * mirror layout to the victim file.
1767 memset(buf, 0, sizeof(*buf));
1768 memset(buf_vic, 0, sizeof(*buf_vic));
1769 rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1773 dom_stripe = mdd_lmm_dom_size(buf_vic->lb_buf) > 0;
1775 rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1776 LU_XATTR_SPLIT, handle);
1779 rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
1780 LU_XATTR_SPLIT, handle);
1784 rc = mdd_trans_start(env, mdd, handle);
1788 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1793 rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
1796 GOTO(out_restore, rc);
1798 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1802 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1809 /* restore obj's layout */
1810 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1811 LU_XATTR_REPLACE, handle);
1813 CERROR("%s: failed to rollback of layout of: "DFID
1814 ": %d, file state unkonwn.\n",
1815 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1818 rc = mdd_trans_stop(env, mdd, rc, handle);
1820 /* Truncate local DOM data if all went well */
1821 if (!rc && dom_stripe)
1822 mdd_dom_data_truncate(env, mdd, obj);
1824 mdd_write_unlock(env, obj);
1825 mdd_write_unlock(env, vic);
1826 lu_buf_free(buf_save);
1828 lu_buf_free(buf_vic);
1831 (void) mdd_object_pfid_replace(env, obj);
1836 static int mdd_layout_merge_allowed(const struct lu_env *env,
1837 struct md_object *target,
1838 struct md_object *victim)
1840 struct mdd_object *o1 = md2mdd_obj(target);
1842 /* cannot extend directory's LOVEA */
1843 if (S_ISDIR(mdd_object_type(o1))) {
1844 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1845 mdd_obj_dev_name(o1));
1853 * The caller should guarantee to update the object ctime
1854 * after xattr_set if needed.
1856 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1857 const struct lu_buf *buf, const char *name,
1860 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1861 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1862 struct mdd_device *mdd = mdo2mdd(obj);
1863 struct thandle *handle;
1864 enum changelog_rec_type cl_type;
1865 enum changelog_rec_flags clf_flags = 0;
1869 rc = mdd_la_get(env, mdd_obj, attr);
1873 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1877 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1878 (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1879 struct md_rejig_data *mrd = buf->lb_buf;
1880 struct md_object *victim = mrd->mrd_obj;
1882 if (buf->lb_len != sizeof(*mrd))
1885 rc = mdd_layout_merge_allowed(env, obj, victim);
1889 if (fl == LU_XATTR_MERGE)
1890 /* merge layout of victim as a mirror of obj's. */
1891 rc = mdd_xattr_merge(env, obj, victim);
1893 rc = mdd_xattr_split(env, obj, mrd);
1897 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1898 rc = mdd_dir_layout_shrink(env, obj, buf);
1902 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1903 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1904 struct posix_acl *acl;
1906 /* user may set empty ACL, which should be treated as removing
1908 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1911 RETURN(PTR_ERR(acl));
1913 rc = mdd_xattr_del(env, obj, name);
1916 posix_acl_release(acl);
1919 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1920 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1924 handle = mdd_trans_create(env, mdd);
1926 RETURN(PTR_ERR(handle));
1928 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1932 rc = mdd_trans_start(env, mdd, handle);
1936 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
1938 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1939 rc = mdd_hsm_update_locked(env, obj, buf, handle, &clf_flags);
1941 mdd_write_unlock(env, mdd_obj);
1946 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
1947 mdd_write_unlock(env, mdd_obj);
1951 cl_type = mdd_xattr_changelog_type(env, mdd, name);
1955 rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, clf_flags,
1956 mdd_obj, name, handle);
1960 return mdd_trans_stop(env, mdd, rc, handle);
1963 static int mdd_declare_xattr_del(const struct lu_env *env,
1964 struct mdd_device *mdd,
1965 struct mdd_object *obj,
1967 struct thandle *handle)
1969 enum changelog_rec_type type;
1972 rc = mdo_declare_xattr_del(env, obj, name, handle);
1976 type = mdd_xattr_changelog_type(env, mdd, name);
1978 return 0; /* no changelog to store */
1980 return mdd_declare_changelog_store(env, mdd, type, NULL, NULL, handle);
1984 * The caller should guarantee to update the object ctime
1985 * after xattr_set if needed.
1987 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1990 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1991 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1992 struct mdd_device *mdd = mdo2mdd(obj);
1993 struct thandle *handle;
1997 rc = mdd_la_get(env, mdd_obj, attr);
2001 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
2005 handle = mdd_trans_create(env, mdd);
2007 RETURN(PTR_ERR(handle));
2009 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
2013 rc = mdd_trans_start(env, mdd, handle);
2017 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
2018 rc = mdo_xattr_del(env, mdd_obj, name, handle);
2019 mdd_write_unlock(env, mdd_obj);
2023 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
2026 rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
2031 return mdd_trans_stop(env, mdd, rc, handle);
2035 * read lov/lmv EA of an object
2036 * return the lov/lmv EA in an allocated lu_buf
2038 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
2039 struct lu_buf *lmm_buf, const char *name)
2041 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
2046 if (buf->lb_buf == NULL) {
2047 buf = lu_buf_check_and_alloc(buf, 4096);
2048 if (buf->lb_buf == NULL)
2053 rc = mdo_xattr_get(env, obj, buf, name);
2054 if (rc == -ERANGE) {
2055 /* mti_big_buf is allocated but is too small
2056 * we need to increase it */
2057 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
2059 if (buf->lb_buf == NULL)
2062 } else if (rc < 0) {
2064 } else if (rc == 0) {
2068 lu_buf_alloc(lmm_buf, rc);
2069 if (lmm_buf->lb_buf == NULL)
2073 * we don't use lmm_buf directly, because we don't know xattr size, so
2074 * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
2076 memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
2081 static int mdd_xattr_hsm_replace(const struct lu_env *env,
2082 struct mdd_object *o, struct lu_buf *buf,
2083 struct thandle *handle)
2085 struct hsm_attrs *attrs;
2086 enum hsm_states hsm_flags;
2087 enum changelog_rec_flags clf_flags = 0;
2091 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
2096 attrs = buf->lb_buf;
2097 hsm_flags = le32_to_cpu(attrs->hsm_flags);
2098 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
2101 /* Add a changelog record for release. */
2102 hsm_set_cl_event(&clf_flags, HE_RELEASE);
2103 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
2104 clf_flags, o, handle);
2109 * check if layout swapping between 2 objects is allowed
2111 * - only normal FIDs or non-system IGIFs
2112 * - same type of objects
2113 * - same owner/group (so quotas are still valid) unless this is from HSM
2116 static int mdd_layout_swap_allowed(const struct lu_env *env,
2117 struct mdd_object *o1,
2118 const struct lu_attr *attr1,
2119 struct mdd_object *o2,
2120 const struct lu_attr *attr2,
2123 const struct lu_fid *fid1, *fid2;
2129 if (!fid_is_norm(fid1) &&
2130 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
2133 if (!fid_is_norm(fid2) &&
2134 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
2137 if (mdd_object_type(o1) != mdd_object_type(o2)) {
2138 if (S_ISDIR(mdd_object_type(o1)))
2140 if (S_ISREG(mdd_object_type(o1)))
2145 if (flags & SWAP_LAYOUTS_MDS_HSM)
2148 if ((attr1->la_uid != attr2->la_uid) ||
2149 (attr1->la_gid != attr2->la_gid))
2155 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
2156 * look into the layout in MDD layer. */
2157 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
2159 struct lov_comp_md_v1 *comp_v1;
2160 struct lov_mds_md *v1;
2164 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2165 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2166 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
2172 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
2173 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
2176 for (i = 0; i < le32_to_cpu(ent_count); i++) {
2177 off = le32_to_cpu(comp_v1->lcm_entries[i].
2179 v1 = (struct lov_mds_md *)((char *)comp_v1 +
2184 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2185 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2196 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2198 return mdd_lmm_oi(lmm, oi, true);
2201 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
2203 return mdd_lmm_oi(lmm, oi, false);
2206 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
2208 struct lov_comp_md_v1 *comp_v1;
2210 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
2211 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2213 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
2215 comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
2216 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
2217 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
2218 __u16 tmp_gen = *gen;
2220 *gen = le16_to_cpu(lmm->lmm_layout_gen);
2222 lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
2229 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2231 return mdd_lmm_gen(lmm, gen, true);
2234 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
2236 return mdd_lmm_gen(lmm, gen, false);
2239 static int mdd_dom_data_truncate(const struct lu_env *env,
2240 struct mdd_device *mdd, struct mdd_object *mo)
2243 struct dt_object *dom;
2246 dom = dt_object_locate(mdd_object_child(mo), mdd->mdd_bottom);
2248 GOTO(out, rc = -ENODATA);
2250 th = dt_trans_create(env, mdd->mdd_bottom);
2252 GOTO(out, rc = PTR_ERR(th));
2254 rc = dt_declare_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2258 rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
2262 rc = dt_punch(env, dom, 0, OBD_OBJECT_EOF, th);
2264 dt_trans_stop(env, mdd->mdd_bottom, th);
2266 /* Ignore failure but report the error */
2268 CERROR("%s: "DFID" can't truncate DOM inode data, rc = %d\n",
2269 mdd_obj_dev_name(mo), PFID(mdo2fid(mo)), rc);
2274 * swap layouts between 2 lustre objects
2276 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2277 struct md_object *obj2, __u64 flags)
2279 struct mdd_thread_info *info = mdd_env_info(env);
2280 struct mdd_object *fst_o = md2mdd_obj(obj1);
2281 struct mdd_object *snd_o = md2mdd_obj(obj2);
2282 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2283 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2284 struct mdd_device *mdd = mdo2mdd(obj1);
2285 struct lov_mds_md *fst_lmm, *snd_lmm;
2286 struct lu_buf *fst_buf = &info->mti_buf[0];
2287 struct lu_buf *snd_buf = &info->mti_buf[1];
2288 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2289 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2290 struct ost_id *saved_oi = NULL;
2291 struct thandle *handle;
2292 struct mdd_object *dom_o = NULL;
2293 __u64 domsize_dom, domsize_vlt;
2294 __u32 fst_gen, snd_gen, saved_gen;
2300 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
2301 memset(info->mti_buf, 0, sizeof(info->mti_buf));
2303 /* we have to sort the 2 obj, so locking will always
2304 * be in the same order, even in case of 2 concurrent swaps */
2305 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
2306 if (rc == 0) /* same fid ? */
2312 rc = mdd_la_get(env, fst_o, fst_la);
2316 rc = mdd_la_get(env, snd_o, snd_la);
2320 /* check if layout swapping is allowed */
2321 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2325 handle = mdd_trans_create(env, mdd);
2327 RETURN(PTR_ERR(handle));
2329 /* objects are already sorted */
2330 mdd_write_lock(env, fst_o, DT_TGT_CHILD);
2331 mdd_write_lock(env, snd_o, DT_TGT_CHILD);
2333 rc = mdd_stripe_get(env, fst_o, fst_buf, XATTR_NAME_LOV);
2334 if (rc < 0 && rc != -ENODATA)
2337 rc = mdd_stripe_get(env, snd_o, snd_buf, XATTR_NAME_LOV);
2338 if (rc < 0 && rc != -ENODATA)
2341 /* check if file has DoM. DoM file can be migrated only to another
2342 * DoM layout with the same DoM component size or to an non-DOM
2343 * layout. After migration to OSTs layout, local MDT inode data
2344 * should be truncated.
2345 * Objects are sorted by FIDs, considering that original file's FID
2346 * is always smaller the snd_o is always original file we are migrating
2349 domsize_dom = mdd_lmm_dom_size(snd_buf->lb_buf);
2350 domsize_vlt = mdd_lmm_dom_size(fst_buf->lb_buf);
2352 /* Only migration is supported for DoM files, not 'swap_layouts' so
2353 * target file must be volatile and orphan.
2355 if (fst_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2356 dom_o = domsize_dom ? snd_o : NULL;
2357 } else if (snd_o->mod_flags & (ORPHAN_OBJ | VOLATILE_OBJ)) {
2358 swap(domsize_dom, domsize_vlt);
2359 dom_o = domsize_dom ? fst_o : NULL;
2360 } else if (domsize_dom > 0 || domsize_vlt > 0) {
2361 /* 'lfs swap_layouts' case, neither file should have DoM */
2363 CDEBUG(D_LAYOUT, "cannot swap layouts with DOM component, "
2364 "use migration instead: rc = %d\n", rc);
2368 if (domsize_vlt > 0 && domsize_dom == 0) {
2370 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": OST to DOM "
2371 "migration is not supported: rc = %d\n",
2372 PFID(mdo2fid(snd_o)), rc);
2374 } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
2376 CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": new layout "
2377 "must have the same DoM component size: rc = %d\n",
2378 PFID(mdo2fid(fst_o)), rc);
2380 } else if (domsize_vlt > 0) {
2381 /* Migration with the same DOM component size, no need to
2382 * truncate local data, it is still being used */
2386 /* swapping 2 non existant layouts is a success */
2387 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2390 /* to help inode migration between MDT, it is better to
2391 * start by the no layout file (if one), so we order the swap */
2392 if (snd_buf->lb_buf == NULL) {
2394 swap(fst_buf, snd_buf);
2397 fst_gen = snd_gen = 0;
2398 /* lmm and generation layout initialization */
2399 if (fst_buf->lb_buf != NULL) {
2400 fst_lmm = fst_buf->lb_buf;
2401 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2402 fst_fl = LU_XATTR_REPLACE;
2406 fst_fl = LU_XATTR_CREATE;
2409 snd_lmm = snd_buf->lb_buf;
2410 mdd_get_lmm_gen(snd_lmm, &snd_gen);
2412 saved_gen = fst_gen;
2413 /* increase the generation layout numbers */
2418 * XXX The layout generation is used to generate component IDs for
2419 * the composite file, we have to do some special tweaks to make
2420 * sure the layout generation is always adequate for that job.
2423 /* Skip invalid generation number for composite layout */
2424 if ((snd_gen & LCME_ID_MASK) == 0)
2426 if ((fst_gen & LCME_ID_MASK) == 0)
2428 /* Make sure the generation is greater than all the component IDs */
2429 if (fst_gen < snd_gen)
2431 else if (fst_gen > snd_gen)
2434 /* set the file specific informations in lmm */
2435 if (fst_lmm != NULL) {
2436 saved_oi = &info->mti_oa.o_oi;
2437 mdd_get_lmm_oi(fst_lmm, saved_oi);
2438 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2439 mdd_set_lmm_oi(fst_lmm, &snd_lmm->lmm_oi);
2440 mdd_set_lmm_oi(snd_lmm, saved_oi);
2442 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2443 cpu_to_le32(LOV_MAGIC_MAGIC))
2444 snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2446 GOTO(stop, rc = -EPROTO);
2448 mdd_set_lmm_gen(snd_lmm, &fst_gen);
2450 /* Prepare HSM attribute if it's required */
2451 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2452 const int buflen = sizeof(struct hsm_attrs);
2454 lu_buf_alloc(fst_hsm_buf, buflen);
2455 lu_buf_alloc(snd_hsm_buf, buflen);
2456 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2457 GOTO(stop, rc = -ENOMEM);
2459 /* Read HSM attribute */
2460 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2464 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2468 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2469 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2474 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2475 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2481 /* prepare transaction */
2482 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2487 if (fst_buf->lb_buf != NULL)
2488 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2489 XATTR_NAME_LOV, LU_XATTR_REPLACE,
2492 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2497 rc = mdd_trans_start(env, mdd, handle);
2501 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2502 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2506 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2508 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2511 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
2512 mdd_obj_dev_name(fst_o),
2513 PFID(mdo2fid(fst_o)), rc, rc2);
2518 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2522 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2525 if (fst_buf->lb_buf != NULL)
2526 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2527 LU_XATTR_REPLACE, handle);
2529 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2532 GOTO(out_restore, rc);
2534 /* Issue one changelog record per file */
2535 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
2539 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
2548 /* failure on second file, but first was done, so we have
2549 * to roll back first. */
2550 if (fst_buf->lb_buf != NULL) {
2551 mdd_set_lmm_oi(fst_lmm, saved_oi);
2552 mdd_set_lmm_gen(fst_lmm, &saved_gen);
2553 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2554 LU_XATTR_REPLACE, handle);
2556 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2562 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2567 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2572 CERROR("%s: unable to roll back layout swap. FIDs: "
2573 DFID" and "DFID "error: %d/%d, steps: %d\n",
2574 mdd_obj_dev_name(fst_o),
2575 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
2577 /* a solution to avoid journal commit is to panic,
2578 * but it has strong consequences so we use LBUG to
2579 * allow sysdamin to choose to panic or not
2586 rc = mdd_trans_stop(env, mdd, rc, handle);
2588 /* Truncate local DOM data if all went well */
2590 mdd_dom_data_truncate(env, mdd, dom_o);
2592 mdd_write_unlock(env, snd_o);
2593 mdd_write_unlock(env, fst_o);
2595 lu_buf_free(fst_buf);
2596 lu_buf_free(snd_buf);
2597 lu_buf_free(fst_hsm_buf);
2598 lu_buf_free(snd_hsm_buf);
2601 (void) mdd_object_pfid_replace(env, fst_o);
2602 (void) mdd_object_pfid_replace(env, snd_o);
2607 static int mdd_declare_layout_change(const struct lu_env *env,
2608 struct mdd_device *mdd,
2609 struct mdd_object *obj,
2610 struct md_layout_change *mlc,
2611 struct thandle *handle)
2615 rc = mdo_declare_layout_change(env, obj, mlc, handle);
2619 return mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2623 /* For PFL, this is used to instantiate necessary component objects. */
2625 mdd_layout_instantiate_component(const struct lu_env *env,
2626 struct mdd_object *obj, struct md_layout_change *mlc,
2627 struct thandle *handle)
2629 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2633 if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2636 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2638 * It's possible that another layout write intent has already
2639 * instantiated our objects, so a -EALREADY returned, and we need to
2643 RETURN(rc == -EALREADY ? 0 : rc);
2645 rc = mdd_trans_start(env, mdd, handle);
2649 mdd_write_lock(env, obj, DT_TGT_CHILD);
2650 rc = mdo_layout_change(env, obj, mlc, handle);
2651 mdd_write_unlock(env, obj);
2655 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
2660 * Change the FLR layout from RDONLY to WRITE_PENDING.
2662 * It picks the primary mirror, and bumps the layout version, and set
2663 * layout version xattr to OST objects in a sync tx. In order to facilitate
2664 * the handling of phantom writers from evicted clients, the clients carry
2665 * layout version of the file with write RPC, so that the OSTs can verify
2666 * if the write RPCs are legitimate, meaning not from evicted clients.
2669 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2670 struct md_layout_change *mlc, struct thandle *handle)
2672 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2673 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2674 struct lustre_som_attrs *som = &mlc->mlc_som;
2679 /* Verify acceptable operations */
2680 switch (mlc->mlc_opc) {
2681 case MD_LAYOUT_WRITE:
2682 case MD_LAYOUT_RESYNC:
2683 /* these are legal operations - this represents the case that
2684 * a few mirrors were missed in the last resync. */
2686 case MD_LAYOUT_RESYNC_DONE:
2691 som_buf->lb_buf = som;
2692 som_buf->lb_len = sizeof(*som);
2693 rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
2694 if (rc < 0 && rc != -ENODATA)
2698 lustre_som_swab(som);
2699 if (som->lsa_valid & SOM_FL_STRICT)
2700 fl = LU_XATTR_REPLACE;
2703 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2708 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2709 XATTR_NAME_SOM, fl, handle);
2714 /* record a changelog for data mover to consume */
2715 rc = mdd_declare_changelog_store(env, mdd, CL_FLRW, NULL, NULL, handle);
2719 rc = mdd_trans_start(env, mdd, handle);
2723 /* it needs a sync tx to make FLR to work properly */
2724 handle->th_sync = 1;
2726 mdd_write_lock(env, obj, DT_TGT_CHILD);
2727 rc = mdo_layout_change(env, obj, mlc, handle);
2729 /* SOM state transition from STRICT to STALE */
2730 som->lsa_valid = SOM_FL_STALE;
2731 lustre_som_swab(som);
2732 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2735 mdd_write_unlock(env, obj);
2739 rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
2750 * Handle mirrored file state transition when it's in WRITE_PENDING.
2752 * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2753 * the file is in WRITE_PENDING state. If everything goes fine, the file's
2754 * layout version will be increased, and the file's state will be changed to
2758 mdd_layout_update_write_pending(const struct lu_env *env,
2759 struct mdd_object *obj, struct md_layout_change *mlc,
2760 struct thandle *handle)
2762 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2766 switch (mlc->mlc_opc) {
2767 case MD_LAYOUT_RESYNC:
2768 /* Upon receiving the resync request, it should
2769 * instantiate all stale components right away to get ready
2770 * for mirror copy. In order to avoid layout version change,
2771 * client should avoid sending LAYOUT_WRITE request at the
2774 case MD_LAYOUT_WRITE:
2775 /* legal race for concurrent write, the file state has been
2776 * changed by another client. */
2782 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2786 rc = mdd_trans_start(env, mdd, handle);
2790 /* it needs a sync tx to make FLR to work properly */
2791 handle->th_sync = 1;
2793 mdd_write_lock(env, obj, DT_TGT_CHILD);
2794 rc = mdo_layout_change(env, obj, mlc, handle);
2795 mdd_write_unlock(env, obj);
2806 * Handle the requests when a FLR file's state is in SYNC_PENDING.
2808 * Only concurrent write and sync complete requests are possible when the
2809 * file is in SYNC_PENDING. For the latter request, it will pass in the
2810 * mirrors that have been synchronized, then the stale bit will be cleared
2811 * to make the file's state turn into RDONLY.
2812 * For concurrent write reqeust, it just needs to change the file's state
2813 * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2814 * version because the version will be increased in the transition to
2815 * SYNC_PENDING later so that it can deny the write request from potential
2816 * evicted SYNC clients. */
2818 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2819 struct md_layout_change *mlc, struct thandle *handle)
2821 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2822 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2827 /* operation validation */
2828 switch (mlc->mlc_opc) {
2829 case MD_LAYOUT_RESYNC_DONE:
2830 /* resync complete. */
2831 case MD_LAYOUT_WRITE:
2832 /* concurrent write. */
2834 case MD_LAYOUT_RESYNC:
2835 /* resync again, most likely the previous run failed.
2836 * no-op if it's already in SYNC_PENDING state */
2842 if (mlc->mlc_som.lsa_valid & SOM_FL_STRICT) {
2843 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2844 if (rc < 0 && rc != -ENODATA)
2847 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2848 lustre_som_swab(&mlc->mlc_som);
2849 som_buf->lb_buf = &mlc->mlc_som;
2850 som_buf->lb_len = sizeof(mlc->mlc_som);
2853 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2857 /* record a changelog for the completion of resync */
2858 rc = mdd_declare_changelog_store(env, mdd, CL_RESYNC, NULL, NULL,
2863 /* RESYNC_DONE has piggybacked size and blocks */
2865 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2866 XATTR_NAME_SOM, fl, handle);
2871 rc = mdd_trans_start(env, mdd, handle);
2875 /* it needs a sync tx to make FLR to work properly */
2876 handle->th_sync = 1;
2878 rc = mdo_layout_change(env, obj, mlc, handle);
2883 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2889 rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
2898 * Layout change callback for object.
2900 * This is only used by FLR for now. In the future, it can be exteneded to
2901 * handle all layout change.
2904 mdd_layout_change(const struct lu_env *env, struct md_object *o,
2905 struct md_layout_change *mlc)
2907 struct mdd_object *obj = md2mdd_obj(o);
2908 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2909 struct lu_buf *buf = mdd_buf_get(env, NULL, 0);
2910 struct lov_comp_md_v1 *lcm;
2911 struct thandle *handle;
2916 /* Verify acceptable operations */
2917 switch (mlc->mlc_opc) {
2918 case MD_LAYOUT_WRITE:
2919 case MD_LAYOUT_RESYNC:
2920 case MD_LAYOUT_RESYNC_DONE:
2926 handle = mdd_trans_create(env, mdd);
2928 RETURN(PTR_ERR(handle));
2930 rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
2937 /* analyze the layout to make sure it's a FLR file */
2939 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2940 GOTO(out, rc = -EINVAL);
2942 flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
2944 /* please refer to HLD of FLR for state transition */
2945 switch (flr_state) {
2947 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
2949 case LCM_FL_WRITE_PENDING:
2950 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
2953 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
2955 case LCM_FL_SYNC_PENDING:
2956 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
2965 mdd_trans_stop(env, mdd, rc, handle);
2970 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
2971 struct mdd_object *child, const struct lu_attr *attr,
2972 const struct md_op_spec *spec,
2973 struct dt_allocation_hint *hint)
2975 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
2976 struct mdd_device *mdd = mdd_obj2mdd_dev(child);
2977 struct dt_object *nc = mdd_object_child(child);
2979 memset(hint, 0, sizeof(*hint));
2981 /* For striped directory, give striping EA to lod_ah_init, which will
2982 * decide the stripe_offset and stripe count by it. */
2983 if (S_ISDIR(attr->la_mode) &&
2984 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2985 hint->dah_eadata = spec->u.sp_ea.eadata;
2986 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
2988 hint->dah_eadata = NULL;
2989 hint->dah_eadata_len = 0;
2990 if (spec->sp_cr_flags & MDS_OPEN_APPEND) {
2991 if (mdd->mdd_append_stripe_count != 0 ||
2992 mdd->mdd_append_pool[0])
2994 "using O_APPEND file striping\n");
2995 if (mdd->mdd_append_stripe_count)
2996 hint->dah_append_stripes =
2997 mdd->mdd_append_stripe_count;
2998 if (mdd->mdd_append_pool[0])
2999 hint->dah_append_pool = mdd->mdd_append_pool;
3001 hint->dah_append_stripes = 0;
3005 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
3006 hint->dah_eadata, hint->dah_eadata_len);
3007 /* @hint will be initialized by underlying device. */
3008 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
3011 static int accmode(const struct lu_env *env, const struct lu_attr *la,
3014 /* Sadly, NFSD reopens a file repeatedly during operation, so the
3015 * "acc_mode = 0" allowance for newly-created files isn't honoured.
3016 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
3017 * owner can write to a file even if it is marked readonly to hide
3018 * its brokenness. (bug 5781) */
3019 if (open_flags & MDS_OPEN_OWNEROVERRIDE) {
3020 struct lu_ucred *uc = lu_ucred_check(env);
3022 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
3026 return mds_accmode(open_flags);
3029 static int mdd_open_sanity_check(const struct lu_env *env,
3030 struct mdd_object *obj,
3031 const struct lu_attr *attr, u64 open_flags,
3037 /* EEXIST check, also opening of *open* orphans is allowed so we can
3038 * open-by-handle unlinked files
3040 if (mdd_is_dead_obj(obj) && !is_replay &&
3041 likely(!(mdd_is_orphan_obj(obj) && obj->mod_count > 0)))
3044 if (S_ISLNK(attr->la_mode))
3047 mode = accmode(env, attr, open_flags);
3049 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
3052 if (!(open_flags & MDS_OPEN_CREATED)) {
3053 rc = mdd_permission_internal(env, obj, attr, mode);
3058 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
3059 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
3060 open_flags &= ~MDS_OPEN_TRUNC;
3062 /* For writing append-only file must open it with append mode. */
3063 if (attr->la_flags & LUSTRE_APPEND_FL) {
3064 if ((open_flags & MDS_FMODE_WRITE) &&
3065 !(open_flags & MDS_OPEN_APPEND))
3067 if (open_flags & MDS_OPEN_TRUNC)
3074 static int mdd_open(const struct lu_env *env, struct md_object *obj,
3075 u64 open_flags, struct md_op_spec *spec)
3077 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3078 struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
3079 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3080 struct mdd_object_user *mou = NULL;
3081 const struct lu_ucred *uc = lu_ucred(env);
3082 struct mdd_device *mdd = mdo2mdd(obj);
3083 enum changelog_rec_type type = CL_OPEN;
3087 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3089 rc = mdd_la_get(env, mdd_obj, attr);
3093 rc = mdd_open_sanity_check(env, mdd_obj, attr, open_flags,
3095 if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & (1 << CL_DN_OPEN)))
3100 mdd_obj->mod_count++;
3102 if (!mdd_changelog_enabled(env, mdd, type))
3106 /* look for existing opener in list under mdd_write_lock */
3107 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid, open_flags);
3112 /* add user to list */
3113 mou = mdd_obj_user_alloc(open_flags, uc->uc_uid, uc->uc_gid);
3119 rc2 = mdd_obj_user_add(mdd_obj, mou, type == CL_DN_OPEN);
3121 mdd_obj_user_free(mou);
3126 if (type == CL_DN_OPEN) {
3127 if (ktime_before(ktime_get(), mou->mou_deniednext))
3128 /* same user denied again same access within
3129 * time interval: do not record
3133 /* this user already denied, but some time ago:
3134 * update denied time
3136 mou->mou_deniednext =
3137 ktime_add(ktime_get(),
3138 ktime_set(mdd->mdd_cl.mc_deniednext,
3141 mou->mou_opencount++;
3142 /* same user opening file again with same flags:
3149 /* FYI, only the bottom 32 bits of open_flags are recorded */
3150 mdd_changelog(env, type, open_flags, md_dev, mdo2fid(mdd_obj));
3154 mdd_write_unlock(env, mdd_obj);
3158 static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
3159 struct md_attr *ma, struct thandle *handle)
3163 rc = mdd_orphan_declare_delete(env, obj, handle);
3167 return mdo_declare_destroy(env, obj, handle);
3171 * No permission check is needed.
3173 static int mdd_close(const struct lu_env *env, struct md_object *obj,
3174 struct md_attr *ma, u64 open_flags)
3176 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3177 struct mdd_device *mdd = mdo2mdd(obj);
3178 struct thandle *handle = NULL;
3181 bool blocked = false;
3182 bool last_close_by_uid = false;
3183 const struct lu_ucred *uc = lu_ucred(env);
3186 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
3187 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3188 mdd_obj->mod_count--;
3189 mdd_write_unlock(env, mdd_obj);
3191 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
3192 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
3193 "list\n", PFID(mdd_object_fid(mdd_obj)));
3197 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
3198 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
3199 /* check without any lock */
3200 is_orphan = mdd_obj->mod_count == 1 &&
3201 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
3205 /* mdd_trans_create() maybe failed because of barrier_entry(),
3206 * under such case, the orphan MDT-object will be left in the
3207 * orphan list, and when the MDT remount next time, the unused
3208 * orphans will be destroyed automatically.
3210 * One exception: the former mdd_finish_unlink may failed to
3211 * add the orphan MDT-object to the orphan list, then if the
3212 * mdd_trans_create() failed because of barrier_entry(), the
3213 * MDT-object will become real orphan that is neither in the
3214 * namespace nor in the orphan list. Such bad case should be
3215 * very rare and will be handled by e2fsck/lfsck. */
3216 handle = mdd_trans_create(env, mdo2mdd(obj));
3217 if (IS_ERR(handle)) {
3218 rc = PTR_ERR(handle);
3219 if (rc != -EINPROGRESS)
3227 rc = mdd_declare_close(env, mdd_obj, ma, handle);
3231 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE, NULL, NULL,
3236 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3242 mdd_write_lock(env, mdd_obj, DT_TGT_CHILD);
3243 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
3245 CERROR("%s: failed to get lu_attr of "DFID": rc = %d\n",
3246 lu_dev_name(mdd2lu_dev(mdd)),
3247 PFID(mdd_object_fid(mdd_obj)), rc);
3251 /* check again with lock */
3252 is_orphan = (mdd_obj->mod_count == 1) &&
3253 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
3254 ma->ma_attr.la_nlink == 0);
3256 if (is_orphan && !handle && !blocked) {
3257 mdd_write_unlock(env, mdd_obj);
3261 mdd_obj->mod_count--; /*release open count */
3263 /* under mdd write lock */
3264 /* If recording, see if we need to remove UID from list. uc is not
3265 * initialized if the client has been evicted. */
3266 if (mdd_changelog_enabled(env, mdd, CL_OPEN) && uc) {
3267 struct mdd_object_user *mou;
3269 /* look for UID in list */
3270 /* If mou is NULL, it probably means logging was enabled after
3271 * the user had the file open. So the corresponding close
3272 * will not be logged.
3274 mou = mdd_obj_user_find(mdd_obj, uc->uc_uid, uc->uc_gid,
3277 mou->mou_opencount--;
3278 if (mou->mou_opencount == 0) {
3279 mdd_obj_user_remove(mdd_obj, mou);
3280 last_close_by_uid = true;
3285 if (!is_orphan || blocked)
3289 /* NB: Object maybe not in orphan list originally, it is rare case for
3290 * mdd_finish_unlink() failure, in that case, the object doesn't have
3291 * ORPHAN_OBJ flag */
3292 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
3293 /* remove link to object from orphan index */
3294 LASSERT(handle != NULL);
3295 rc = mdd_orphan_delete(env, mdd_obj, handle);
3297 CERROR("%s: unable to delete "DFID" from orphan list: "
3298 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3299 PFID(mdd_object_fid(mdd_obj)), rc);
3300 /* If object was not deleted from orphan list, do not
3301 * destroy OSS objects, which will be done when next
3306 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
3307 "list, OSS objects to be destroyed.\n",
3308 PFID(mdd_object_fid(mdd_obj)));
3311 rc = mdo_destroy(env, mdd_obj, handle);
3314 CERROR("%s: unable to delete "DFID" from orphan list: "
3315 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
3316 PFID(mdd_object_fid(mdd_obj)), rc);
3321 mdd_write_unlock(env, mdd_obj);
3323 if (rc != 0 || blocked ||
3324 !mdd_changelog_enabled(env, mdd, CL_CLOSE))
3327 /* Record CL_CLOSE in changelog only if file was opened in write mode,
3328 * or if CL_OPEN was recorded and it's last close by user.
3329 * Changelogs mask may change between open and close operations, but
3330 * this is not a big deal if we have a CL_CLOSE entry with no matching
3331 * CL_OPEN. Plus Changelogs mask may not change often.
3333 if (((!(mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) &&
3334 (open_flags & (MDS_FMODE_WRITE | MDS_OPEN_APPEND |
3335 MDS_OPEN_TRUNC))) ||
3336 ((mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) && last_close_by_uid)) &&
3337 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
3338 if (handle == NULL) {
3339 handle = mdd_trans_create(env, mdo2mdd(obj));
3341 GOTO(stop, rc = PTR_ERR(handle));
3343 rc = mdd_declare_changelog_store(env, mdd, CL_CLOSE,
3344 NULL, NULL, handle);
3348 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
3353 /* FYI, only the bottom 32 bits of open_flags are recorded */
3354 mdd_changelog_data_store(env, mdd, CL_CLOSE, open_flags,
3359 if (handle != NULL && !IS_ERR(handle))
3360 rc = mdd_trans_stop(env, mdd, rc, handle);
3366 * Permission check is done when open,
3367 * no need check again.
3369 static int mdd_readpage_sanity_check(const struct lu_env *env,
3370 struct mdd_object *obj)
3372 struct dt_object *next = mdd_object_child(obj);
3376 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
3384 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
3385 size_t nob, const struct dt_it_ops *iops,
3386 struct dt_it *it, __u32 attr, void *arg)
3388 struct lu_dirpage *dp = &lp->lp_dir;
3392 struct lu_dirent *ent;
3393 struct lu_dirent *last = NULL;
3397 if (nob < sizeof(*dp))
3400 memset(area, 0, sizeof (*dp));
3401 area += sizeof (*dp);
3402 nob -= sizeof (*dp);
3409 len = iops->key_size(env, it);
3411 /* IAM iterator can return record with zero len. */
3415 hash = iops->store(env, it);
3416 if (unlikely(first)) {
3418 dp->ldp_hash_start = cpu_to_le64(hash);
3421 /* calculate max space required for lu_dirent */
3422 recsize = lu_dirent_calc_size(len, attr);
3424 if (nob >= recsize) {
3425 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
3426 if (result == -ESTALE)
3431 /* osd might not able to pack all attributes,
3432 * so recheck rec length */
3433 recsize = le16_to_cpu(ent->lde_reclen);
3435 if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
3436 fid_le_to_cpu(&fid, &ent->lde_fid);
3437 if (fid_is_dot_lustre(&fid))
3441 result = (last != NULL) ? 0 :-EINVAL;
3445 ent = (void *)ent + recsize;
3449 result = iops->next(env, it);
3450 if (result == -ESTALE)
3452 } while (result == 0);
3455 dp->ldp_hash_end = cpu_to_le64(hash);
3457 if (last->lde_hash == dp->ldp_hash_end)
3458 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
3459 last->lde_reclen = 0; /* end mark */
3462 /* end of directory */
3463 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3464 else if (result < 0)
3465 CWARN("build page failed: %d!\n", result);
3469 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
3470 const struct lu_rdpg *rdpg)
3472 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3476 if (mdd_object_exists(mdd_obj) == 0) {
3477 CERROR("%s: object "DFID" not found: rc = -2\n",
3478 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
3482 mdd_read_lock(env, mdd_obj, DT_TGT_CHILD);
3483 rc = mdd_readpage_sanity_check(env, mdd_obj);
3485 GOTO(out_unlock, rc);
3487 if (mdd_is_dead_obj(mdd_obj)) {
3489 struct lu_dirpage *dp;
3492 * According to POSIX, please do not return any entry to client:
3493 * even dot and dotdot should not be returned.
3495 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
3496 PFID(mdd_object_fid(mdd_obj)));
3498 if (rdpg->rp_count <= 0)
3499 GOTO(out_unlock, rc = -EFAULT);
3500 LASSERT(rdpg->rp_pages != NULL);
3502 pg = rdpg->rp_pages[0];
3503 dp = (struct lu_dirpage *)kmap(pg);
3504 memset(dp, 0 , sizeof(struct lu_dirpage));
3505 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3506 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3507 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3509 GOTO(out_unlock, rc = LU_PAGE_SIZE);
3512 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
3513 mdd_dir_page_build, NULL);
3515 struct lu_dirpage *dp;
3517 dp = kmap(rdpg->rp_pages[0]);
3518 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3521 * No pages were processed, mark this for first page
3524 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3525 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3526 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
3528 kunmap(rdpg->rp_pages[0]);
3531 GOTO(out_unlock, rc);
3533 mdd_read_unlock(env, mdd_obj);
3537 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
3539 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3541 if (mdd_object_exists(mdd_obj) == 0) {
3544 CERROR("%s: object "DFID" not found: rc = %d\n",
3545 mdd_obj_dev_name(mdd_obj),
3546 PFID(mdd_object_fid(mdd_obj)), rc);
3549 return dt_object_sync(env, mdd_object_child(mdd_obj),
3553 static int mdd_object_lock(const struct lu_env *env,
3554 struct md_object *obj,
3555 struct lustre_handle *lh,
3556 struct ldlm_enqueue_info *einfo,
3557 union ldlm_policy_data *policy)
3559 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3560 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
3564 static int mdd_object_unlock(const struct lu_env *env,
3565 struct md_object *obj,
3566 struct ldlm_enqueue_info *einfo,
3567 union ldlm_policy_data *policy)
3569 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3570 return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
3573 const struct md_object_operations mdd_obj_ops = {
3574 .moo_permission = mdd_permission,
3575 .moo_attr_get = mdd_attr_get,
3576 .moo_attr_set = mdd_attr_set,
3577 .moo_xattr_get = mdd_xattr_get,
3578 .moo_xattr_set = mdd_xattr_set,
3579 .moo_xattr_list = mdd_xattr_list,
3580 .moo_invalidate = mdd_invalidate,
3581 .moo_xattr_del = mdd_xattr_del,
3582 .moo_swap_layouts = mdd_swap_layouts,
3583 .moo_open = mdd_open,
3584 .moo_close = mdd_close,
3585 .moo_readpage = mdd_readpage,
3586 .moo_readlink = mdd_readlink,
3587 .moo_changelog = mdd_changelog,
3588 .moo_object_sync = mdd_object_sync,
3589 .moo_object_lock = mdd_object_lock,
3590 .moo_object_unlock = mdd_object_unlock,
3591 .moo_layout_change = mdd_layout_change,