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 static int mdd_xattr_get(const struct lu_env *env,
57 struct md_object *obj, struct lu_buf *buf,
60 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
61 struct mdd_device *mdd,
62 enum changelog_rec_type type,
63 int flags, const struct lu_fid *fid,
64 const char *xattr_name,
65 struct thandle *handle);
67 static inline bool has_prefix(const char *str, const char *prefix);
69 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
74 if (mdd_object_exists(obj) == 0)
77 rc = mdo_attr_get(env, obj, la);
78 if (unlikely(rc != 0)) {
80 obj->mod_flags |= DEAD_OBJ;
84 if (la->la_valid & LA_FLAGS &&
85 la->la_flags & LUSTRE_ORPHAN_FL)
86 obj->mod_flags |= ORPHAN_OBJ | DEAD_OBJ;
91 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
93 struct mdd_thread_info *info;
95 lu_env_refill((struct lu_env *)env);
96 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
97 LASSERT(info != NULL);
101 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
105 buf = &mdd_env_info(env)->mti_buf[0];
111 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
112 const void *area, ssize_t len)
116 buf = &mdd_env_info(env)->mti_buf[0];
117 buf->lb_buf = (void *)area;
122 struct lu_object *mdd_object_alloc(const struct lu_env *env,
123 const struct lu_object_header *hdr,
126 struct mdd_object *mdd_obj;
128 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
129 if (mdd_obj != NULL) {
132 o = mdd2lu_obj(mdd_obj);
133 lu_object_init(o, NULL, d);
134 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
135 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
136 mdd_obj->mod_count = 0;
137 o->lo_ops = &mdd_lu_obj_ops;
144 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
145 const struct lu_object_conf *unused)
147 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
148 struct mdd_object *mdd_obj = lu2mdd_obj(o);
149 struct lu_object *below;
150 struct lu_device *under;
153 mdd_obj->mod_cltime = ktime_set(0, 0);
154 under = &d->mdd_child->dd_lu_dev;
155 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
157 RETURN(PTR_ERR(below));
159 lu_object_add(o, below);
164 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
168 if (lu_object_exists(o)) {
169 struct mdd_object *mdd_obj = lu2mdd_obj(o);
170 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
172 rc = mdd_la_get(env, mdd_obj, attr);
178 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
180 struct mdd_object *mdd = lu2mdd_obj(o);
183 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
186 static int mdd_object_print(const struct lu_env *env, void *cookie,
187 lu_printer_t p, const struct lu_object *o)
189 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
191 return (*p)(env, cookie,
192 LUSTRE_MDD_NAME"-object@%p(open_count=%d, valid=%x, cltime=%lldns, flags=%lx)",
193 mdd, mdd->mod_count, mdd->mod_valid,
194 ktime_to_ns(mdd->mod_cltime), mdd->mod_flags);
197 static const struct lu_object_operations mdd_lu_obj_ops = {
198 .loo_object_init = mdd_object_init,
199 .loo_object_start = mdd_object_start,
200 .loo_object_free = mdd_object_free,
201 .loo_object_print = mdd_object_print,
204 struct mdd_object *mdd_object_find(const struct lu_env *env,
205 struct mdd_device *d,
206 const struct lu_fid *f)
208 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
212 * No permission check is needed.
214 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
217 struct mdd_object *mdd_obj = md2mdd_obj(obj);
222 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
223 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
224 ma->ma_attr.la_nlink = 0;
230 * No permission check is needed.
232 static int mdd_xattr_get(const struct lu_env *env,
233 struct md_object *obj, struct lu_buf *buf,
236 struct mdd_object *mdd_obj = md2mdd_obj(obj);
237 struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
242 if (mdd_object_exists(mdd_obj) == 0) {
243 CERROR("%s: object "DFID" not found: rc = -2\n",
244 mdd_obj_dev_name(mdd_obj),
245 PFID(mdd_object_fid(mdd_obj)));
249 /* If the object has been destroyed, then do not get LMVEA, because
250 * it needs to load stripes from the iteration of the master object,
251 * and it will cause problem if master object has been destroyed, see
253 if (unlikely((mdd_obj->mod_flags & DEAD_OBJ) &&
254 !(mdd_obj->mod_flags & ORPHAN_OBJ) &&
255 strcmp(name, XATTR_NAME_LMV) == 0))
258 /* If the object has been delete from the namespace, then
259 * get linkEA should return -ENOENT as well */
260 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
261 strcmp(name, XATTR_NAME_LINK) == 0))
264 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
265 rc = mdo_xattr_get(env, mdd_obj, buf, name);
266 mdd_read_unlock(env, mdd_obj);
268 /* record only getting user xattrs and acls */
270 (has_prefix(name, XATTR_USER_PREFIX) ||
271 has_prefix(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
272 has_prefix(name, XATTR_NAME_POSIX_ACL_DEFAULT))) {
273 struct thandle *handle;
274 struct mdd_device *mdd = lu2mdd_dev(&md_dev->md_lu_dev);
278 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
280 if (!(mdd->mdd_cl.mc_mask & (1 << CL_GETXATTR)))
283 LASSERT(mdo2fid(mdd_obj) != NULL);
285 handle = mdd_trans_create(env, mdd);
287 RETURN(PTR_ERR(handle));
289 rc2 = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
293 rc2 = mdd_trans_start(env, mdd, handle);
297 rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
298 mdo2fid(mdd_obj), name,
302 rc2 = mdd_trans_stop(env, mdd, rc2, handle);
311 * Permission check is done when open,
312 * no need check again.
314 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
317 struct mdd_object *mdd_obj = md2mdd_obj(obj);
318 struct dt_object *next;
323 if (mdd_object_exists(mdd_obj) == 0) {
324 CERROR("%s: object "DFID" not found: rc = -2\n",
325 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
329 next = mdd_object_child(mdd_obj);
330 LASSERT(next != NULL);
331 LASSERT(next->do_body_ops != NULL);
332 LASSERT(next->do_body_ops->dbo_read != NULL);
333 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
334 rc = dt_read(env, next, buf, &pos);
335 mdd_read_unlock(env, mdd_obj);
340 * No permission check is needed.
342 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
345 struct mdd_object *mdd_obj = md2mdd_obj(obj);
350 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
351 rc = mdo_xattr_list(env, mdd_obj, buf);
352 mdd_read_unlock(env, mdd_obj);
354 /* If the buffer is NULL then we are only here to get the
355 * length of the xattr name list. */
356 if (rc < 0 || buf->lb_buf == NULL)
360 * Filter out XATTR_NAME_LINK if this is an orphan object. See
363 if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
364 char *end = (char *)buf->lb_buf + rc;
365 char *p = buf->lb_buf;
368 char *next = p + strlen(p) + 1;
370 if (strcmp(p, XATTR_NAME_LINK) == 0) {
372 memmove(p, next, end - next);
374 CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
375 " of orphan "DFID"\n",
376 PFID(mdd_object_fid(mdd_obj)));
387 int mdd_invalidate(const struct lu_env *env, struct md_object *obj)
389 return mdo_invalidate(env, md2mdd_obj(obj));
392 int mdd_declare_create_object_internal(const struct lu_env *env,
393 struct mdd_object *p,
394 struct mdd_object *c,
395 struct lu_attr *attr,
396 struct thandle *handle,
397 const struct md_op_spec *spec,
398 struct dt_allocation_hint *hint)
400 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
401 const struct dt_index_features *feat = spec->sp_feat;
405 if (feat != &dt_directory_features && feat != NULL) {
406 dof->dof_type = DFT_INDEX;
407 dof->u.dof_idx.di_feat = feat;
409 dof->dof_type = dt_mode_to_dft(attr->la_mode);
410 if (dof->dof_type == DFT_REGULAR) {
411 dof->u.dof_reg.striped =
412 md_should_create(spec->sp_cr_flags);
413 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
414 dof->u.dof_reg.striped = 0;
415 /* is this replay? */
417 dof->u.dof_reg.striped = 0;
421 rc = mdo_declare_create_object(env, c, attr, hint, dof, handle);
426 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
427 struct mdd_object *c, struct lu_attr *attr,
428 struct thandle *handle,
429 const struct md_op_spec *spec,
430 struct dt_allocation_hint *hint)
432 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
436 LASSERT(!mdd_object_exists(c));
438 rc = mdo_create_object(env, c, attr, hint, dof, handle);
443 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
444 const struct lu_attr *attr, struct thandle *handle,
450 rc = mdo_attr_set(env, obj, attr, handle);
451 #ifdef CONFIG_FS_POSIX_ACL
452 if (!rc && (attr->la_valid & LA_MODE) && needacl)
453 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
458 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
459 const struct lu_attr *oattr, struct lu_attr *attr,
460 struct thandle *handle)
465 LASSERT(attr->la_valid & LA_CTIME);
466 LASSERT(oattr != NULL);
468 /* Make sure the ctime is increased only, however, it's not strictly
469 * reliable at here because there is not guarantee to hold lock on
470 * object, so we just bypass some unnecessary cmtime setting first
471 * and OSD has to check it again. */
472 if (attr->la_ctime < oattr->la_ctime)
473 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
474 else if (attr->la_valid == LA_CTIME &&
475 attr->la_ctime == oattr->la_ctime)
476 attr->la_valid &= ~LA_CTIME;
478 if (attr->la_valid != 0)
479 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
484 * This gives the same functionality as the code between
485 * sys_chmod and inode_setattr
486 * chown_common and inode_setattr
487 * utimes and inode_setattr
488 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
490 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
491 const struct lu_attr *oattr, struct lu_attr *la,
492 const unsigned long flags)
501 /* Do not permit change file type */
502 if (la->la_valid & LA_TYPE)
505 /* They should not be processed by setattr */
506 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
509 LASSERT(oattr != NULL);
511 uc = lu_ucred_check(env);
515 if (la->la_valid == LA_CTIME) {
516 if (!(flags & MDS_PERM_BYPASS))
517 /* This is only for set ctime when rename's source is
519 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
521 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
522 la->la_valid &= ~LA_CTIME;
526 if (la->la_valid == LA_ATIME) {
527 /* This is an atime-only attribute update for close RPCs. */
528 if (la->la_atime < (oattr->la_atime +
529 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
530 la->la_valid &= ~LA_ATIME;
534 /* Check if flags change. */
535 if (la->la_valid & LA_FLAGS) {
536 unsigned int oldflags = oattr->la_flags &
537 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
538 unsigned int newflags = la->la_flags &
539 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
541 if ((uc->uc_fsuid != oattr->la_uid) &&
542 !md_capable(uc, CFS_CAP_FOWNER))
545 /* The IMMUTABLE and APPEND_ONLY flags can
546 * only be changed by the relevant capability. */
547 if ((oldflags ^ newflags) &&
548 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
551 if (!S_ISDIR(oattr->la_mode))
552 la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
555 if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
556 (la->la_valid & ~LA_FLAGS) &&
557 !(flags & MDS_PERM_BYPASS))
560 /* Check for setting the obj time. */
561 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
562 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
563 if ((uc->uc_fsuid != oattr->la_uid) &&
564 !md_capable(uc, CFS_CAP_FOWNER)) {
565 rc = mdd_permission_internal(env, obj, oattr,
572 if (la->la_valid & LA_KILL_SUID) {
573 la->la_valid &= ~LA_KILL_SUID;
574 if ((oattr->la_mode & S_ISUID) &&
575 !(la->la_valid & LA_MODE)) {
576 la->la_mode = oattr->la_mode;
577 la->la_valid |= LA_MODE;
579 la->la_mode &= ~S_ISUID;
582 if (la->la_valid & LA_KILL_SGID) {
583 la->la_valid &= ~LA_KILL_SGID;
584 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
585 (S_ISGID | S_IXGRP)) &&
586 !(la->la_valid & LA_MODE)) {
587 la->la_mode = oattr->la_mode;
588 la->la_valid |= LA_MODE;
590 la->la_mode &= ~S_ISGID;
593 /* Make sure a caller can chmod. */
594 if (la->la_valid & LA_MODE) {
595 if (!(flags & MDS_PERM_BYPASS) &&
596 (uc->uc_fsuid != oattr->la_uid) &&
597 !md_capable(uc, CFS_CAP_FOWNER))
600 if (la->la_mode == (umode_t) -1)
601 la->la_mode = oattr->la_mode;
603 la->la_mode = (la->la_mode & S_IALLUGO) |
604 (oattr->la_mode & ~S_IALLUGO);
606 /* Also check the setgid bit! */
607 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
608 la->la_gid : oattr->la_gid) &&
609 !md_capable(uc, CFS_CAP_FSETID))
610 la->la_mode &= ~S_ISGID;
612 la->la_mode = oattr->la_mode;
615 /* Make sure a caller can chown. */
616 if (la->la_valid & LA_UID) {
617 if (la->la_uid == (uid_t) -1)
618 la->la_uid = oattr->la_uid;
619 if (((uc->uc_fsuid != oattr->la_uid) ||
620 (la->la_uid != oattr->la_uid)) &&
621 !md_capable(uc, CFS_CAP_CHOWN))
624 /* If the user or group of a non-directory has been
625 * changed by a non-root user, remove the setuid bit.
626 * 19981026 David C Niemi <niemi@tux.org>
628 * Changed this to apply to all users, including root,
629 * to avoid some races. This is the behavior we had in
630 * 2.0. The check for non-root was definitely wrong
631 * for 2.2 anyway, as it should have been using
632 * CAP_FSETID rather than fsuid -- 19990830 SD. */
633 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
634 !S_ISDIR(oattr->la_mode)) {
635 la->la_mode &= ~S_ISUID;
636 la->la_valid |= LA_MODE;
640 /* Make sure caller can chgrp. */
641 if (la->la_valid & LA_GID) {
642 if (la->la_gid == (gid_t) -1)
643 la->la_gid = oattr->la_gid;
644 if (((uc->uc_fsuid != oattr->la_uid) ||
645 ((la->la_gid != oattr->la_gid) &&
646 !lustre_in_group_p(uc, la->la_gid))) &&
647 !md_capable(uc, CFS_CAP_CHOWN))
650 /* Likewise, if the user or group of a non-directory
651 * has been changed by a non-root user, remove the
652 * setgid bit UNLESS there is no group execute bit
653 * (this would be a file marked for mandatory
654 * locking). 19981026 David C Niemi <niemi@tux.org>
656 * Removed the fsuid check (see the comment above) --
658 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
659 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
660 la->la_mode &= ~S_ISGID;
661 la->la_valid |= LA_MODE;
665 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
666 if (!((flags & MDS_OWNEROVERRIDE) &&
667 (uc->uc_fsuid == oattr->la_uid)) &&
668 !(flags & MDS_PERM_BYPASS)) {
669 rc = mdd_permission_internal(env, obj, oattr,
676 if (la->la_valid & LA_CTIME) {
677 /* The pure setattr, it has the priority over what is
678 * already set, do not drop it if ctime is equal. */
679 if (la->la_ctime < oattr->la_ctime)
680 la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
686 static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
687 struct mdd_device *mdd,
688 enum changelog_rec_type type,
689 int flags, const struct lu_fid *fid,
690 const char *xattr_name,
691 struct thandle *handle)
693 const struct lu_ucred *uc = lu_ucred(env);
694 struct llog_changelog_rec *rec;
697 int xflags = CLFE_INVALID;
700 flags = (flags & CLF_FLAGMASK) | CLF_VERSION | CLF_EXTRA_FLAGS;
703 if (uc->uc_jobid[0] != '\0')
705 xflags |= CLFE_UIDGID;
710 if (type == CL_SETXATTR || type == CL_GETXATTR)
711 xflags |= CLFE_XATTR;
713 reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
714 changelog_rec_offset(flags & CLF_SUPPORTED,
715 xflags & CLFE_SUPPORTED));
716 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
717 if (buf->lb_buf == NULL)
721 rec->cr_hdr.lrh_len = reclen;
722 rec->cr.cr_flags = flags;
723 rec->cr.cr_type = (__u32)type;
724 rec->cr.cr_tfid = *fid;
725 rec->cr.cr_namelen = 0;
727 if (flags & CLF_JOBID)
728 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
730 if (flags & CLF_EXTRA_FLAGS) {
731 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
732 if (xflags & CLFE_UIDGID)
733 mdd_changelog_rec_extra_uidgid(&rec->cr,
734 uc->uc_uid, uc->uc_gid);
735 if (xflags & CLFE_NID)
736 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
737 if (xflags & CLFE_OPEN)
738 mdd_changelog_rec_extra_omode(&rec->cr, flags);
739 if (xflags & CLFE_XATTR) {
740 if (xattr_name == NULL)
742 mdd_changelog_rec_extra_xattr(&rec->cr, xattr_name);
746 rc = mdd_changelog_store(env, mdd, rec, handle);
751 /** Store a data change changelog record
752 * If this fails, we must fail the whole transaction; we don't
753 * want the change to commit without the log entry.
754 * \param mdd_obj - mdd_object of change
755 * \param handle - transaction handle
757 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
758 enum changelog_rec_type type, int flags,
759 struct mdd_object *mdd_obj, struct thandle *handle)
763 LASSERT(mdd_obj != NULL);
764 LASSERT(handle != NULL);
767 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
769 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
772 if (mdd_is_volatile_obj(mdd_obj))
775 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
776 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
777 /* Don't need multiple updates in this log */
778 /* Don't check under lock - no big deal if we get an extra
783 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
784 mdo2fid(mdd_obj), NULL, handle);
786 mdd_obj->mod_cltime = ktime_get();
791 static int mdd_changelog_data_store_xattr(const struct lu_env *env,
792 struct mdd_device *mdd,
793 enum changelog_rec_type type,
794 int flags, struct mdd_object *mdd_obj,
795 const char *xattr_name,
796 struct thandle *handle)
800 LASSERT(mdd_obj != NULL);
801 LASSERT(handle != NULL);
804 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
806 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
809 if (mdd_is_volatile_obj(mdd_obj))
812 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
813 ktime_before(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
814 /* Don't need multiple updates in this log */
815 /* Don't check under lock - no big deal if we get an extra
821 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
822 mdo2fid(mdd_obj), xattr_name,
825 mdd_obj->mod_cltime = ktime_get();
830 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
831 int flags, struct md_device *m, const struct lu_fid *fid)
833 struct thandle *handle;
834 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
839 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
841 if (!(mdd->mdd_cl.mc_mask & (1 << type)))
844 LASSERT(fid != NULL);
846 handle = mdd_trans_create(env, mdd);
848 RETURN(PTR_ERR(handle));
850 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
854 rc = mdd_trans_start(env, mdd, handle);
858 rc = mdd_changelog_data_store_by_fid(env, mdd, type, flags,
862 rc = mdd_trans_stop(env, mdd, rc, handle);
868 * Save LMA extended attributes with data from \a ma.
870 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
871 * not, LMA EA will be first read from disk, modified and write back.
874 /* Precedence for choosing record type when multiple
875 * attributes change: setattr > mtime > ctime > atime
876 * (ctime changes when mtime does, plus chmod/chown.
877 * atime and ctime are independent.) */
878 static int mdd_attr_set_changelog(const struct lu_env *env,
879 struct md_object *obj, struct thandle *handle,
882 struct mdd_device *mdd = mdo2mdd(obj);
885 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
886 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
887 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
888 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
889 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
890 bits = bits & mdd->mdd_cl.mc_mask;
891 /* This is an implementation limit rather than a protocol limit */
892 CLASSERT(CL_LAST <= sizeof(int) * 8);
896 /* The record type is the lowest non-masked set bit */
899 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
900 return mdd_changelog_data_store(env, mdd, type, (int)valid,
901 md2mdd_obj(obj), handle);
904 static int mdd_declare_attr_set(const struct lu_env *env,
905 struct mdd_device *mdd,
906 struct mdd_object *obj,
907 const struct lu_attr *attr,
908 struct thandle *handle)
912 rc = mdo_declare_attr_set(env, obj, attr, handle);
916 #ifdef CONFIG_FS_POSIX_ACL
917 if (attr->la_valid & LA_MODE) {
918 mdd_read_lock(env, obj, MOR_TGT_CHILD);
919 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
920 XATTR_NAME_ACL_ACCESS);
921 mdd_read_unlock(env, obj);
922 if (rc == -EOPNOTSUPP || rc == -ENODATA)
928 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
929 rc = mdo_declare_xattr_set(env, obj, buf,
930 XATTR_NAME_ACL_ACCESS, 0,
938 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
945 * permission changes may require sync operation, to mitigate performance
946 * impact, only do this for dir and when permission is reduced.
948 * For regular files, version is updated with permission change (see VBR), async
949 * permission won't cause any issue, while missing permission change on
950 * directory may affect accessibility of other objects after recovery.
952 static inline bool permission_needs_sync(const struct lu_attr *old,
953 const struct lu_attr *new)
955 if (!S_ISDIR(old->la_mode))
958 if (new->la_valid & (LA_UID | LA_GID))
961 if (new->la_valid & LA_MODE &&
962 new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
965 if ((new->la_valid & LA_MODE) &&
966 ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
967 (old->la_mode & S_IRWXUGO))
973 /* set attr and LOV EA at once, return updated attr */
974 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
975 const struct md_attr *ma)
977 struct mdd_object *mdd_obj = md2mdd_obj(obj);
978 struct mdd_device *mdd = mdo2mdd(obj);
979 struct thandle *handle = NULL;
980 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
981 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
982 const struct lu_attr *la = &ma->ma_attr;
987 /* we do not use ->attr_set() for LOV/HSM EA any more */
988 LASSERT((ma->ma_valid & MA_LOV) == 0);
989 LASSERT((ma->ma_valid & MA_HSM) == 0);
991 rc = mdd_la_get(env, mdd_obj, attr);
995 *la_copy = ma->ma_attr;
996 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
1000 /* no need to setattr anymore */
1001 if (la_copy->la_valid == 0) {
1002 CDEBUG(D_INODE, "%s: no valid attribute on "DFID", previous"
1003 "valid is %#llx\n", mdd2obd_dev(mdd)->obd_name,
1004 PFID(mdo2fid(mdd_obj)), la->la_valid);
1009 /* If an unprivileged user changes group of some file,
1010 * the setattr operation will be processed synchronously to
1011 * honor the quota limit of the corresponding group. see LU-5152 */
1012 uc = lu_ucred_check(env);
1013 if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
1014 la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
1015 la_copy->la_valid |= LA_FLAGS;
1016 la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
1018 /* Flush the possible existing sync requests to OSTs to
1019 * keep the order of sync for the current setattr operation
1020 * will be sent directly to OSTs. see LU-5152 */
1021 rc = dt_sync(env, mdd->mdd_child);
1026 handle = mdd_trans_create(env, mdd);
1027 if (IS_ERR(handle)) {
1028 rc = PTR_ERR(handle);
1034 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la_copy, handle);
1038 rc = mdd_trans_start(env, mdd, handle);
1042 if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
1043 handle->th_sync = 1;
1045 if (la->la_valid & (LA_MTIME | LA_CTIME))
1046 CDEBUG(D_INODE, "setting mtime %llu, ctime %llu\n",
1047 la->la_mtime, la->la_ctime);
1049 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1050 if (la_copy->la_valid) {
1051 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1053 if (rc == -EDQUOT && la_copy->la_flags & LUSTRE_SET_SYNC_FL) {
1054 /* rollback to the original gid */
1055 la_copy->la_flags &= ~LUSTRE_SET_SYNC_FL;
1056 la_copy->la_gid = attr->la_gid;
1057 mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
1060 mdd_write_unlock(env, mdd_obj);
1064 rc = mdd_attr_set_changelog(env, obj, handle,
1068 rc = mdd_trans_stop(env, mdd, rc, handle);
1073 static int mdd_xattr_sanity_check(const struct lu_env *env,
1074 struct mdd_object *obj,
1075 const struct lu_attr *attr,
1078 struct lu_ucred *uc = lu_ucred_assert(env);
1081 if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
1084 if (strncmp(XATTR_USER_PREFIX, name,
1085 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
1086 /* For sticky directories, only the owner and privileged user
1087 * can write attributes. */
1088 if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
1089 (uc->uc_fsuid != attr->la_uid) &&
1090 !md_capable(uc, CFS_CAP_FOWNER))
1093 if ((uc->uc_fsuid != attr->la_uid) &&
1094 !md_capable(uc, CFS_CAP_FOWNER))
1102 * Check if a string begins with a given prefix.
1104 * \param str String to check
1105 * \param prefix Substring to check at the beginning of \a str
1106 * \return true/false whether the condition is verified.
1108 static inline bool has_prefix(const char *str, const char *prefix)
1110 return strncmp(prefix, str, strlen(prefix)) == 0;
1114 * Indicate the kind of changelog to store (if any) for a xattr set/del.
1116 * \param[in] xattr_name Full extended attribute name.
1118 * \return The type of changelog to use, or -1 if no changelog is to be emitted.
1120 static enum changelog_rec_type
1121 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
1122 const char *xattr_name)
1124 /* Layout changes systematically recorded */
1125 if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
1126 strncmp(XATTR_LUSTRE_LOV, xattr_name,
1127 strlen(XATTR_LUSTRE_LOV)) == 0)
1130 /* HSM information changes systematically recorded */
1131 if (strcmp(XATTR_NAME_HSM, xattr_name) == 0)
1134 if (has_prefix(xattr_name, XATTR_USER_PREFIX) ||
1135 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_ACCESS) ||
1136 has_prefix(xattr_name, XATTR_NAME_POSIX_ACL_DEFAULT) ||
1137 has_prefix(xattr_name, XATTR_TRUSTED_PREFIX) ||
1138 has_prefix(xattr_name, XATTR_SECURITY_PREFIX))
1144 static int mdd_declare_xattr_set(const struct lu_env *env,
1145 struct mdd_device *mdd,
1146 struct mdd_object *obj,
1147 const struct lu_buf *buf,
1149 int fl, struct thandle *handle)
1153 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
1157 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
1158 return 0; /* no changelog to store */
1160 return mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1164 * Compare current and future data of HSM EA and add a changelog if needed.
1166 * Caller should have write-locked \param obj.
1168 * \param buf - Future HSM EA content.
1169 * \retval 0 if no changelog is needed or changelog was added properly.
1170 * \retval -ve errno if there was a problem
1172 static int mdd_hsm_update_locked(const struct lu_env *env,
1173 struct md_object *obj,
1174 const struct lu_buf *buf,
1175 struct thandle *handle, int *cl_flags)
1177 struct mdd_thread_info *info = mdd_env_info(env);
1178 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1179 struct lu_buf *current_buf;
1180 struct md_hsm *current_mh;
1181 struct md_hsm *new_mh;
1185 OBD_ALLOC_PTR(current_mh);
1186 if (current_mh == NULL)
1189 /* Read HSM attrs from disk */
1190 current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
1191 mdo2mdd(obj)->mdd_dt_conf.ddp_max_ea_size);
1192 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
1193 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
1194 if (rc < 0 && rc != -ENODATA)
1196 else if (rc == -ENODATA)
1197 current_mh->mh_flags = 0;
1199 /* Map future HSM xattr */
1200 OBD_ALLOC_PTR(new_mh);
1202 GOTO(free, rc = -ENOMEM);
1203 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1207 /* Flags differ, set flags for the changelog that will be added */
1208 if (current_mh->mh_flags != new_mh->mh_flags) {
1209 hsm_set_cl_event(cl_flags, HE_STATE);
1210 if (new_mh->mh_flags & HS_DIRTY)
1211 hsm_set_cl_flags(cl_flags, CLF_HSM_DIRTY);
1214 OBD_FREE_PTR(new_mh);
1217 OBD_FREE_PTR(current_mh);
1221 static int mdd_object_pfid_replace(const struct lu_env *env,
1222 struct mdd_object *o)
1224 struct mdd_device *mdd = mdo2mdd(&o->mod_obj);
1225 struct thandle *handle;
1228 handle = mdd_trans_create(env, mdd);
1230 RETURN(PTR_ERR(handle));
1232 handle->th_complex = 1;
1234 /* it doesn't need to track the PFID update via llog, because LFSCK
1235 * will repair it even it goes wrong */
1236 rc = mdd_declare_xattr_set(env, mdd, o, NULL, XATTR_NAME_FID,
1241 rc = mdd_trans_start(env, mdd, handle);
1245 rc = mdo_xattr_set(env, o, NULL, XATTR_NAME_FID, 0, handle);
1250 mdd_trans_stop(env, mdd, rc, handle);
1255 static int mdd_declare_xattr_del(const struct lu_env *env,
1256 struct mdd_device *mdd,
1257 struct mdd_object *obj,
1259 struct thandle *handle);
1261 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1264 static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
1265 struct md_object *md_vic)
1267 struct mdd_device *mdd = mdo2mdd(md_obj);
1268 struct mdd_object *obj = md2mdd_obj(md_obj);
1269 struct mdd_object *vic = md2mdd_obj(md_vic);
1270 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1271 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
1272 struct lov_mds_md *lmm;
1273 struct thandle *handle;
1277 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1278 if (rc == 0) /* same fid */
1281 handle = mdd_trans_create(env, mdd);
1283 RETURN(PTR_ERR(handle));
1286 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1287 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1289 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1290 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1293 /* get EA of victim file */
1294 memset(buf_vic, 0, sizeof(*buf_vic));
1295 rc = mdd_get_lov_ea(env, vic, buf_vic);
1302 /* parse the layout of victim file */
1303 lmm = buf_vic->lb_buf;
1304 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
1305 GOTO(out, rc = -EINVAL);
1307 /* save EA of target file for restore */
1308 memset(buf, 0, sizeof(*buf));
1309 rc = mdd_get_lov_ea(env, obj, buf);
1313 /* Get rid of the layout from victim object */
1314 rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
1318 rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
1319 LU_XATTR_MERGE, handle);
1323 rc = mdd_trans_start(env, mdd, handle);
1327 rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
1332 rc = mdo_xattr_del(env, vic, XATTR_NAME_LOV, handle);
1334 GOTO(out_restore, rc);
1336 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1337 (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1342 int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1343 LU_XATTR_REPLACE, handle);
1345 CERROR("%s: failed to rollback of layout of: "DFID
1346 ": %d, file state unknown\n",
1347 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1351 mdd_trans_stop(env, mdd, rc, handle);
1352 mdd_write_unlock(env, obj);
1353 mdd_write_unlock(env, vic);
1355 lu_buf_free(buf_vic);
1358 (void) mdd_object_pfid_replace(env, obj);
1364 * Extract the mirror with specified mirror id, and store the splitted
1365 * mirror layout to @buf.
1367 * \param[in] comp_v1 mirrored layout
1368 * \param[in] mirror_id the mirror with mirror_id to be extracted
1369 * \param[out] buf store the layout excluding the extracted mirror,
1370 * caller free the buffer we allocated in this function
1371 * \param[out] buf_vic store the extracted layout, caller free the buffer
1372 * we allocated in this function
1374 * \retval 0 on success; < 0 if error happens
1376 static int mdd_split_ea(struct lov_comp_md_v1 *comp_v1, __u16 mirror_id,
1377 struct lu_buf *buf, struct lu_buf *buf_vic)
1379 struct lov_comp_md_v1 *comp_rem;
1380 struct lov_comp_md_v1 *comp_vic;
1381 struct lov_comp_md_entry_v1 *entry;
1382 struct lov_comp_md_entry_v1 *entry_rem;
1383 struct lov_comp_md_entry_v1 *entry_vic;
1385 __u16 comp_cnt, count = 0;
1386 int lmm_size, lmm_size_vic = 0;
1388 int offset, offset_rem, offset_vic;
1390 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1391 /* comp_v1 should contains more than 1 mirror */
1392 if (mirror_cnt <= 1)
1394 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1395 lmm_size = le32_to_cpu(comp_v1->lcm_size);
1397 for (i = 0; i < comp_cnt; i++) {
1398 entry = &comp_v1->lcm_entries[i];
1399 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id) {
1401 lmm_size_vic += sizeof(*entry);
1402 lmm_size_vic += le32_to_cpu(entry->lcme_size);
1403 } else if (count > 0) {
1404 /* find the specified mirror */
1412 lu_buf_alloc(buf, lmm_size - lmm_size_vic);
1416 lu_buf_alloc(buf_vic, sizeof(*comp_vic) + lmm_size_vic);
1417 if (!buf_vic->lb_buf) {
1422 comp_rem = (struct lov_comp_md_v1 *)buf->lb_buf;
1423 comp_vic = (struct lov_comp_md_v1 *)buf_vic->lb_buf;
1425 memcpy(comp_rem, comp_v1, sizeof(*comp_v1));
1426 comp_rem->lcm_mirror_count = cpu_to_le16(mirror_cnt - 2);
1427 comp_rem->lcm_entry_count = cpu_to_le32(comp_cnt - count);
1428 comp_rem->lcm_size = cpu_to_le32(lmm_size - lmm_size_vic);
1429 if (!comp_rem->lcm_mirror_count)
1430 comp_rem->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1432 memset(comp_vic, 0, sizeof(*comp_v1));
1433 comp_vic->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1434 comp_vic->lcm_mirror_count = 0;
1435 comp_vic->lcm_entry_count = cpu_to_le32(count);
1436 comp_vic->lcm_size = cpu_to_le32(lmm_size_vic + sizeof(*comp_vic));
1437 comp_vic->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1438 comp_vic->lcm_layout_gen = 0;
1440 offset = sizeof(*comp_v1) + sizeof(*entry) * comp_cnt;
1441 offset_rem = sizeof(*comp_rem) +
1442 sizeof(*entry_rem) * (comp_cnt - count);
1443 offset_vic = sizeof(*comp_vic) + sizeof(*entry_vic) * count;
1444 for (i = j = k = 0; i < comp_cnt; i++) {
1445 struct lov_mds_md *lmm, *lmm_dst;
1448 entry = &comp_v1->lcm_entries[i];
1449 entry_vic = &comp_vic->lcm_entries[j];
1450 entry_rem = &comp_rem->lcm_entries[k];
1452 if (mirror_id_of(le32_to_cpu(entry->lcme_id)) == mirror_id)
1455 /* copy component entry */
1457 memcpy(entry_vic, entry, sizeof(*entry));
1458 entry_vic->lcme_flags &= cpu_to_le32(LCME_FL_INIT);
1459 entry_vic->lcme_offset = cpu_to_le32(offset_vic);
1462 memcpy(entry_rem, entry, sizeof(*entry));
1463 entry_rem->lcme_offset = cpu_to_le32(offset_rem);
1467 lmm = (struct lov_mds_md *)((char *)comp_v1 + offset);
1469 lmm_dst = (struct lov_mds_md *)
1470 ((char *)comp_vic + offset_vic);
1472 lmm_dst = (struct lov_mds_md *)
1473 ((char *)comp_rem + offset_rem);
1475 /* copy component entry blob */
1476 memcpy(lmm_dst, lmm, le32_to_cpu(entry->lcme_size));
1478 /* blob offset advance */
1479 offset += le32_to_cpu(entry->lcme_size);
1481 offset_vic += le32_to_cpu(entry->lcme_size);
1483 offset_rem += le32_to_cpu(entry->lcme_size);
1489 static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
1490 struct md_rejig_data *mrd)
1492 struct mdd_device *mdd = mdo2mdd(md_obj);
1493 struct mdd_object *obj = md2mdd_obj(md_obj);
1494 struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
1495 struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
1496 struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
1497 struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
1498 struct lov_comp_md_v1 *lcm;
1499 struct thandle *handle;
1503 rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
1504 if (rc == 0) /* same fid */
1507 handle = mdd_trans_create(env, mdd);
1509 RETURN(PTR_ERR(handle));
1512 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1513 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1515 mdd_write_lock(env, vic, MOR_TGT_CHILD);
1516 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1519 /* get EA of mirrored file */
1520 memset(buf_save, 0, sizeof(*buf));
1521 rc = mdd_get_lov_ea(env, obj, buf_save);
1525 lcm = buf_save->lb_buf;
1526 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1527 GOTO(out, rc = -EINVAL);
1530 * Extract the mirror with specified mirror id, and store the splitted
1531 * mirror layout to the victim file.
1533 memset(buf, 0, sizeof(*buf));
1534 memset(buf_vic, 0, sizeof(*buf_vic));
1535 rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
1539 rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
1540 LU_XATTR_SPLIT, handle);
1543 rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
1544 LU_XATTR_SPLIT, handle);
1548 rc = mdd_trans_start(env, mdd, handle);
1552 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
1557 rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
1560 GOTO(out_restore, rc);
1562 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
1566 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
1573 /* restore obj's layout */
1574 int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
1575 LU_XATTR_REPLACE, handle);
1577 CERROR("%s: failed to rollback of layout of: "DFID
1578 ": %d, file state unkonwn.\n",
1579 mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
1582 mdd_trans_stop(env, mdd, rc, handle);
1583 mdd_write_unlock(env, obj);
1584 mdd_write_unlock(env, vic);
1585 lu_buf_free(buf_save);
1587 lu_buf_free(buf_vic);
1590 (void) mdd_object_pfid_replace(env, obj);
1595 static int mdd_layout_merge_allowed(const struct lu_env *env,
1596 struct md_object *target,
1597 struct md_object *victim)
1599 struct mdd_object *o1 = md2mdd_obj(target);
1601 /* cannot extend directory's LOVEA */
1602 if (S_ISDIR(mdd_object_type(o1))) {
1603 CERROR("%s: Don't extend directory's LOVEA, just set it.\n",
1604 mdd_obj_dev_name(o1));
1612 * The caller should guarantee to update the object ctime
1613 * after xattr_set if needed.
1615 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1616 const struct lu_buf *buf, const char *name,
1619 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1620 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1621 struct mdd_device *mdd = mdo2mdd(obj);
1622 struct thandle *handle;
1623 enum changelog_rec_type cl_type;
1628 rc = mdd_la_get(env, mdd_obj, attr);
1632 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1636 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 &&
1637 (fl == LU_XATTR_MERGE || fl == LU_XATTR_SPLIT)) {
1638 struct md_rejig_data *mrd = buf->lb_buf;
1639 struct md_object *victim = mrd->mrd_obj;
1641 if (buf->lb_len != sizeof(*mrd))
1644 rc = mdd_layout_merge_allowed(env, obj, victim);
1648 if (fl == LU_XATTR_MERGE)
1649 /* merge layout of victim as a mirror of obj's. */
1650 rc = mdd_xattr_merge(env, obj, victim);
1652 rc = mdd_xattr_split(env, obj, mrd);
1656 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1657 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1658 struct posix_acl *acl;
1660 /* user may set empty ACL, which should be treated as removing
1662 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1665 RETURN(PTR_ERR(acl));
1667 rc = mdd_xattr_del(env, obj, name);
1670 posix_acl_release(acl);
1673 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1674 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1678 handle = mdd_trans_create(env, mdd);
1680 RETURN(PTR_ERR(handle));
1682 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1686 rc = mdd_trans_start(env, mdd, handle);
1690 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1692 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1693 rc = mdd_hsm_update_locked(env, obj, buf, handle, &cl_flags);
1695 mdd_write_unlock(env, mdd_obj);
1700 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
1701 mdd_write_unlock(env, mdd_obj);
1705 cl_type = mdd_xattr_changelog_type(env, mdd, name);
1709 rc = mdd_changelog_data_store_xattr(env, mdd, cl_type, cl_flags,
1710 mdd_obj, name, handle);
1714 return mdd_trans_stop(env, mdd, rc, handle);
1717 static int mdd_declare_xattr_del(const struct lu_env *env,
1718 struct mdd_device *mdd,
1719 struct mdd_object *obj,
1721 struct thandle *handle)
1725 rc = mdo_declare_xattr_del(env, obj, name, handle);
1729 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
1730 return 0; /* no changelog to store */
1732 return mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1736 * The caller should guarantee to update the object ctime
1737 * after xattr_set if needed.
1739 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1742 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1743 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1744 struct mdd_device *mdd = mdo2mdd(obj);
1745 struct thandle *handle;
1749 rc = mdd_la_get(env, mdd_obj, attr);
1753 rc = mdd_xattr_sanity_check(env, mdd_obj, attr, name);
1757 handle = mdd_trans_create(env, mdd);
1759 RETURN(PTR_ERR(handle));
1761 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1765 rc = mdd_trans_start(env, mdd, handle);
1769 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1770 rc = mdo_xattr_del(env, mdd_obj, name, handle);
1771 mdd_write_unlock(env, mdd_obj);
1775 if (mdd_xattr_changelog_type(env, mdd, name) < 0)
1778 rc = mdd_changelog_data_store_xattr(env, mdd, CL_SETXATTR, 0, mdd_obj,
1783 return mdd_trans_stop(env, mdd, rc, handle);
1787 * read lov EA of an object
1788 * return the lov EA in an allocated lu_buf
1790 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1791 struct lu_buf *lmm_buf)
1793 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
1798 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV);
1800 if (rc == -ERANGE) {
1801 /* mti_big_buf is allocated but is too small
1802 * we need to increase it */
1803 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1805 if (buf->lb_buf == NULL)
1806 GOTO(out, rc = -ENOMEM);
1817 if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1818 /* mti_big_buf was not allocated, so we have to
1819 * allocate it based on the ea size */
1820 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1822 if (buf->lb_buf == NULL)
1823 GOTO(out, rc = -ENOMEM);
1827 lu_buf_alloc(lmm_buf, bufsize);
1828 if (lmm_buf->lb_buf == NULL)
1829 GOTO(out, rc = -ENOMEM);
1831 memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1837 lu_buf_free(lmm_buf);
1841 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1842 struct mdd_object *o, struct lu_buf *buf,
1843 struct thandle *handle)
1845 struct hsm_attrs *attrs;
1851 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1856 attrs = buf->lb_buf;
1857 hsm_flags = le32_to_cpu(attrs->hsm_flags);
1858 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1861 /* Add a changelog record for release. */
1862 hsm_set_cl_event(&flags, HE_RELEASE);
1863 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1869 * check if layout swapping between 2 objects is allowed
1871 * - only normal FIDs or non-system IGIFs
1872 * - same type of objects
1873 * - same owner/group (so quotas are still valid) unless this is from HSM
1876 static int mdd_layout_swap_allowed(const struct lu_env *env,
1877 struct mdd_object *o1,
1878 const struct lu_attr *attr1,
1879 struct mdd_object *o2,
1880 const struct lu_attr *attr2,
1883 const struct lu_fid *fid1, *fid2;
1889 if (!fid_is_norm(fid1) &&
1890 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1893 if (!fid_is_norm(fid2) &&
1894 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1897 if (mdd_object_type(o1) != mdd_object_type(o2)) {
1898 if (S_ISDIR(mdd_object_type(o1)))
1900 if (S_ISREG(mdd_object_type(o1)))
1905 if (flags & SWAP_LAYOUTS_MDS_HSM)
1908 if ((attr1->la_uid != attr2->la_uid) ||
1909 (attr1->la_gid != attr2->la_gid))
1915 /* XXX To set the proper lmm_oi & lmm_layout_gen when swap layouts, we have to
1916 * look into the layout in MDD layer. */
1917 static int mdd_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi, bool get)
1919 struct lov_comp_md_v1 *comp_v1;
1920 struct lov_mds_md *v1;
1924 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
1925 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1926 ent_count = le16_to_cpu(comp_v1->lcm_entry_count);
1932 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
1933 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1936 for (i = 0; i < le32_to_cpu(ent_count); i++) {
1937 off = le32_to_cpu(comp_v1->lcm_entries[i].
1939 v1 = (struct lov_mds_md *)((char *)comp_v1 +
1944 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1945 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1956 static inline int mdd_get_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
1958 return mdd_lmm_oi(lmm, oi, true);
1961 static inline int mdd_set_lmm_oi(struct lov_mds_md *lmm, struct ost_id *oi)
1963 return mdd_lmm_oi(lmm, oi, false);
1966 static int mdd_lmm_gen(struct lov_mds_md *lmm, __u32 *gen, bool get)
1968 struct lov_comp_md_v1 *comp_v1;
1970 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
1971 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1973 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
1975 comp_v1->lcm_layout_gen = cpu_to_le32(*gen);
1976 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1977 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1978 __u16 tmp_gen = *gen;
1980 *gen = le16_to_cpu(lmm->lmm_layout_gen);
1982 lmm->lmm_layout_gen = cpu_to_le16(tmp_gen);
1989 static inline int mdd_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1991 return mdd_lmm_gen(lmm, gen, true);
1994 static inline int mdd_set_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1996 return mdd_lmm_gen(lmm, gen, false);
2000 * swap layouts between 2 lustre objects
2002 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
2003 struct md_object *obj2, __u64 flags)
2005 struct mdd_thread_info *info = mdd_env_info(env);
2006 struct mdd_object *fst_o = md2mdd_obj(obj1);
2007 struct mdd_object *snd_o = md2mdd_obj(obj2);
2008 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
2009 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
2010 struct mdd_device *mdd = mdo2mdd(obj1);
2011 struct lov_mds_md *fst_lmm, *snd_lmm;
2012 struct lu_buf *fst_buf = &info->mti_buf[0];
2013 struct lu_buf *snd_buf = &info->mti_buf[1];
2014 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
2015 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
2016 struct ost_id *saved_oi = NULL;
2017 struct thandle *handle;
2018 __u32 fst_gen, snd_gen, saved_gen;
2024 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
2025 memset(info->mti_buf, 0, sizeof(info->mti_buf));
2027 /* we have to sort the 2 obj, so locking will always
2028 * be in the same order, even in case of 2 concurrent swaps */
2029 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
2030 if (rc == 0) /* same fid ? */
2036 rc = mdd_la_get(env, fst_o, fst_la);
2040 rc = mdd_la_get(env, snd_o, snd_la);
2044 /* check if layout swapping is allowed */
2045 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la, flags);
2049 handle = mdd_trans_create(env, mdd);
2051 RETURN(PTR_ERR(handle));
2053 /* objects are already sorted */
2054 mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
2055 mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
2057 rc = mdd_get_lov_ea(env, fst_o, fst_buf);
2058 if (rc < 0 && rc != -ENODATA)
2061 rc = mdd_get_lov_ea(env, snd_o, snd_buf);
2062 if (rc < 0 && rc != -ENODATA)
2065 /* swapping 2 non existant layouts is a success */
2066 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
2069 /* to help inode migration between MDT, it is better to
2070 * start by the no layout file (if one), so we order the swap */
2071 if (snd_buf->lb_buf == NULL) {
2073 swap(fst_buf, snd_buf);
2076 fst_gen = snd_gen = 0;
2077 /* lmm and generation layout initialization */
2078 if (fst_buf->lb_buf != NULL) {
2079 fst_lmm = fst_buf->lb_buf;
2080 mdd_get_lmm_gen(fst_lmm, &fst_gen);
2081 fst_fl = LU_XATTR_REPLACE;
2085 fst_fl = LU_XATTR_CREATE;
2088 snd_lmm = snd_buf->lb_buf;
2089 mdd_get_lmm_gen(snd_lmm, &snd_gen);
2091 saved_gen = fst_gen;
2092 /* increase the generation layout numbers */
2097 * XXX The layout generation is used to generate component IDs for
2098 * the composite file, we have to do some special tweaks to make
2099 * sure the layout generation is always adequate for that job.
2102 /* Skip invalid generation number for composite layout */
2103 if ((snd_gen & LCME_ID_MASK) == 0)
2105 if ((fst_gen & LCME_ID_MASK) == 0)
2107 /* Make sure the generation is greater than all the component IDs */
2108 if (fst_gen < snd_gen)
2110 else if (fst_gen > snd_gen)
2113 /* set the file specific informations in lmm */
2114 if (fst_lmm != NULL) {
2115 saved_oi = &info->mti_oa.o_oi;
2116 mdd_get_lmm_oi(fst_lmm, saved_oi);
2117 mdd_set_lmm_gen(fst_lmm, &snd_gen);
2118 mdd_set_lmm_oi(fst_lmm, &snd_lmm->lmm_oi);
2119 mdd_set_lmm_oi(snd_lmm, saved_oi);
2121 if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
2122 cpu_to_le32(LOV_MAGIC_MAGIC))
2123 snd_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
2125 GOTO(stop, rc = -EPROTO);
2127 mdd_set_lmm_gen(snd_lmm, &fst_gen);
2129 /* Prepare HSM attribute if it's required */
2130 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2131 const int buflen = sizeof(struct hsm_attrs);
2133 lu_buf_alloc(fst_hsm_buf, buflen);
2134 lu_buf_alloc(snd_hsm_buf, buflen);
2135 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
2136 GOTO(stop, rc = -ENOMEM);
2138 /* Read HSM attribute */
2139 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
2143 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
2147 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
2148 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2153 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
2154 XATTR_NAME_HSM, LU_XATTR_REPLACE,
2160 /* prepare transaction */
2161 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
2166 if (fst_buf->lb_buf != NULL)
2167 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
2168 XATTR_NAME_LOV, LU_XATTR_REPLACE,
2171 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
2176 rc = mdd_trans_start(env, mdd, handle);
2180 if (flags & SWAP_LAYOUTS_MDS_HSM) {
2181 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
2185 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
2187 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
2190 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
2191 mdd_obj_dev_name(fst_o),
2192 PFID(mdo2fid(fst_o)), rc, rc2);
2197 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
2201 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
2204 if (fst_buf->lb_buf != NULL)
2205 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
2206 LU_XATTR_REPLACE, handle);
2208 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
2211 GOTO(out_restore, rc);
2213 /* Issue one changelog record per file */
2214 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
2218 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
2227 /* failure on second file, but first was done, so we have
2228 * to roll back first. */
2229 if (fst_buf->lb_buf != NULL) {
2230 mdd_set_lmm_oi(fst_lmm, saved_oi);
2231 mdd_set_lmm_gen(fst_lmm, &saved_gen);
2232 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
2233 LU_XATTR_REPLACE, handle);
2235 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
2241 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
2246 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
2251 CERROR("%s: unable to roll back layout swap. FIDs: "
2252 DFID" and "DFID "error: %d/%d, steps: %d\n",
2253 mdd_obj_dev_name(fst_o),
2254 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
2256 /* a solution to avoid journal commit is to panic,
2257 * but it has strong consequences so we use LBUG to
2258 * allow sysdamin to choose to panic or not
2265 rc = mdd_trans_stop(env, mdd, rc, handle);
2267 mdd_write_unlock(env, snd_o);
2268 mdd_write_unlock(env, fst_o);
2270 lu_buf_free(fst_buf);
2271 lu_buf_free(snd_buf);
2272 lu_buf_free(fst_hsm_buf);
2273 lu_buf_free(snd_hsm_buf);
2276 (void) mdd_object_pfid_replace(env, fst_o);
2277 (void) mdd_object_pfid_replace(env, snd_o);
2282 static int mdd_declare_layout_change(const struct lu_env *env,
2283 struct mdd_device *mdd,
2284 struct mdd_object *obj,
2285 struct md_layout_change *mlc,
2286 struct thandle *handle)
2290 rc = mdo_declare_layout_change(env, obj, mlc, handle);
2294 return mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
2297 /* For PFL, this is used to instantiate necessary component objects. */
2299 mdd_layout_instantiate_component(const struct lu_env *env,
2300 struct mdd_object *obj, struct md_layout_change *mlc,
2301 struct thandle *handle)
2303 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2307 if (mlc->mlc_opc != MD_LAYOUT_WRITE)
2310 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2312 * It's possible that another layout write intent has already
2313 * instantiated our objects, so a -EALREADY returned, and we need to
2317 RETURN(rc == -EALREADY ? 0 : rc);
2319 rc = mdd_trans_start(env, mdd, handle);
2323 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2324 rc = mdo_layout_change(env, obj, mlc, handle);
2325 mdd_write_unlock(env, obj);
2329 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
2334 * Change the FLR layout from RDONLY to WRITE_PENDING.
2336 * It picks the primary mirror, and bumps the layout version, and set
2337 * layout version xattr to OST objects in a sync tx. In order to facilitate
2338 * the handling of phantom writers from evicted clients, the clients carry
2339 * layout version of the file with write RPC, so that the OSTs can verify
2340 * if the write RPCs are legitimate, meaning not from evicted clients.
2343 mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
2344 struct md_layout_change *mlc, struct thandle *handle)
2346 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2350 /* Verify acceptable operations */
2351 switch (mlc->mlc_opc) {
2352 case MD_LAYOUT_WRITE:
2354 case MD_LAYOUT_RESYNC:
2355 /* these are legal operations - this represents the case that
2356 * a few mirrors were missed in the last resync.
2357 * XXX: it will be supported later */
2358 case MD_LAYOUT_RESYNC_DONE:
2363 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2367 rc = mdd_declare_xattr_del(env, mdd, obj, XATTR_NAME_SOM, handle);
2371 /* record a changelog for data mover to consume */
2372 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
2376 rc = mdd_trans_start(env, mdd, handle);
2380 /* it needs a sync tx to make FLR to work properly */
2381 handle->th_sync = 1;
2383 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2384 rc = mdo_layout_change(env, obj, mlc, handle);
2386 rc = mdo_xattr_del(env, obj, XATTR_NAME_SOM, handle);
2390 mdd_write_unlock(env, obj);
2394 rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
2405 * Handle mirrored file state transition when it's in WRITE_PENDING.
2407 * Only MD_LAYOUT_RESYNC, which represents start of resync, is allowed when
2408 * the file is in WRITE_PENDING state. If everything goes fine, the file's
2409 * layout version will be increased, and the file's state will be changed to
2413 mdd_layout_update_write_pending(const struct lu_env *env,
2414 struct mdd_object *obj, struct md_layout_change *mlc,
2415 struct thandle *handle)
2417 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2421 switch (mlc->mlc_opc) {
2422 case MD_LAYOUT_RESYNC:
2423 /* Upon receiving the resync request, it should
2424 * instantiate all stale components right away to get ready
2425 * for mirror copy. In order to avoid layout version change,
2426 * client should avoid sending LAYOUT_WRITE request at the
2429 case MD_LAYOUT_WRITE:
2430 /* legal race for concurrent write, the file state has been
2431 * changed by another client. */
2437 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2441 rc = mdd_trans_start(env, mdd, handle);
2445 /* it needs a sync tx to make FLR to work properly */
2446 handle->th_sync = 1;
2448 mdd_write_lock(env, obj, MOR_TGT_CHILD);
2449 rc = mdo_layout_change(env, obj, mlc, handle);
2450 mdd_write_unlock(env, obj);
2461 * Handle the requests when a FLR file's state is in SYNC_PENDING.
2463 * Only concurrent write and sync complete requests are possible when the
2464 * file is in SYNC_PENDING. For the latter request, it will pass in the
2465 * mirrors that have been synchronized, then the stale bit will be cleared
2466 * to make the file's state turn into RDONLY.
2467 * For concurrent write reqeust, it just needs to change the file's state
2468 * to WRITE_PENDING in a sync tx. It doesn't have to change the layout
2469 * version because the version will be increased in the transition to
2470 * SYNC_PENDING later so that it can deny the write request from potential
2471 * evicted SYNC clients. */
2473 mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
2474 struct md_layout_change *mlc, struct thandle *handle)
2476 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2477 struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
2482 /* operation validation */
2483 switch (mlc->mlc_opc) {
2484 case MD_LAYOUT_RESYNC_DONE:
2485 /* resync complete. */
2486 case MD_LAYOUT_WRITE:
2487 /* concurrent write. */
2489 case MD_LAYOUT_RESYNC:
2490 /* resync again, most likely the previous run failed.
2491 * no-op if it's already in SYNC_PENDING state */
2497 if (mlc->mlc_som.lsa_valid & LSOM_FL_VALID) {
2498 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_SOM);
2499 if (rc && rc != -ENODATA)
2502 fl = rc == -ENODATA ? LU_XATTR_CREATE : LU_XATTR_REPLACE;
2503 som_buf->lb_buf = &mlc->mlc_som;
2504 som_buf->lb_len = sizeof(mlc->mlc_som);
2507 rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
2511 /* record a changelog for the completion of resync */
2512 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
2516 /* RESYNC_DONE has piggybacked size and blocks */
2518 rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
2519 XATTR_NAME_SOM, fl, handle);
2524 rc = mdd_trans_start(env, mdd, handle);
2528 /* it needs a sync tx to make FLR to work properly */
2529 handle->th_sync = 1;
2531 rc = mdo_layout_change(env, obj, mlc, handle);
2536 rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
2542 rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
2551 * Layout change callback for object.
2553 * This is only used by FLR for now. In the future, it can be exteneded to
2554 * handle all layout change.
2557 mdd_layout_change(const struct lu_env *env, struct md_object *o,
2558 struct md_layout_change *mlc)
2560 struct mdd_object *obj = md2mdd_obj(o);
2561 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
2562 struct lu_buf *buf = mdd_buf_get(env, NULL, 0);
2563 struct lov_comp_md_v1 *lcm;
2564 struct thandle *handle;
2569 /* Verify acceptable operations */
2570 switch (mlc->mlc_opc) {
2571 case MD_LAYOUT_WRITE:
2572 case MD_LAYOUT_RESYNC:
2573 case MD_LAYOUT_RESYNC_DONE:
2579 handle = mdd_trans_create(env, mdd);
2581 RETURN(PTR_ERR(handle));
2583 rc = mdd_get_lov_ea(env, obj, buf);
2590 /* analyze the layout to make sure it's a FLR file */
2592 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2593 GOTO(out, rc = -EINVAL);
2595 flr_state = le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK;
2597 /* please refer to HLD of FLR for state transition */
2598 switch (flr_state) {
2600 rc = mdd_layout_instantiate_component(env, obj, mlc, handle);
2602 case LCM_FL_WRITE_PENDING:
2603 rc = mdd_layout_update_write_pending(env, obj, mlc, handle);
2606 rc = mdd_layout_update_rdonly(env, obj, mlc, handle);
2608 case LCM_FL_SYNC_PENDING:
2609 rc = mdd_object_update_sync_pending(env, obj, mlc, handle);
2618 mdd_trans_stop(env, mdd, rc, handle);
2623 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
2624 struct mdd_object *child, const struct lu_attr *attr,
2625 const struct md_op_spec *spec,
2626 struct dt_allocation_hint *hint)
2628 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
2629 struct dt_object *nc = mdd_object_child(child);
2631 memset(hint, 0, sizeof(*hint));
2633 /* For striped directory, give striping EA to lod_ah_init, which will
2634 * decide the stripe_offset and stripe count by it. */
2635 if (S_ISDIR(attr->la_mode) &&
2636 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2637 hint->dah_eadata = spec->u.sp_ea.eadata;
2638 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
2640 hint->dah_eadata = NULL;
2641 hint->dah_eadata_len = 0;
2644 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
2645 hint->dah_eadata, hint->dah_eadata_len);
2646 /* @hint will be initialized by underlying device. */
2647 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
2651 * do NOT or the MAY_*'s, you'll get the weakest
2653 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
2657 /* Sadly, NFSD reopens a file repeatedly during operation, so the
2658 * "acc_mode = 0" allowance for newly-created files isn't honoured.
2659 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
2660 * owner can write to a file even if it is marked readonly to hide
2661 * its brokenness. (bug 5781) */
2662 if (flags & MDS_OPEN_OWNEROVERRIDE) {
2663 struct lu_ucred *uc = lu_ucred_check(env);
2665 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
2669 if (flags & FMODE_READ)
2671 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
2673 if (flags & MDS_FMODE_EXEC)
2678 static int mdd_open_sanity_check(const struct lu_env *env,
2679 struct mdd_object *obj,
2680 const struct lu_attr *attr, int flag)
2686 if (mdd_is_dead_obj(obj))
2689 if (S_ISLNK(attr->la_mode))
2692 mode = accmode(env, attr, flag);
2694 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
2697 if (!(flag & MDS_OPEN_CREATED)) {
2698 rc = mdd_permission_internal(env, obj, attr, mode);
2703 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
2704 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
2705 flag &= ~MDS_OPEN_TRUNC;
2707 /* For writing append-only file must open it with append mode. */
2708 if (attr->la_flags & LUSTRE_APPEND_FL) {
2709 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
2711 if (flag & MDS_OPEN_TRUNC)
2718 static int mdd_open(const struct lu_env *env, struct md_object *obj,
2721 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2722 struct md_device *md_dev = lu2md_dev(mdd2lu_dev(mdo2mdd(obj)));
2723 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
2726 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
2728 rc = mdd_la_get(env, mdd_obj, attr);
2732 rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
2736 mdd_obj->mod_count++;
2738 mdd_changelog(env, CL_OPEN, flags, md_dev, mdo2fid(mdd_obj));
2742 mdd_write_unlock(env, mdd_obj);
2746 static int mdd_declare_close(const struct lu_env *env,
2747 struct mdd_object *obj,
2749 struct thandle *handle)
2753 rc = orph_declare_index_delete(env, obj, handle);
2757 return mdo_declare_destroy(env, obj, handle);
2761 * No permission check is needed.
2763 static int mdd_close(const struct lu_env *env, struct md_object *obj,
2764 struct md_attr *ma, int mode)
2766 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2767 struct mdd_device *mdd = mdo2mdd(obj);
2768 struct thandle *handle = NULL;
2771 bool blocked = false;
2774 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
2775 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
2776 mdd_obj->mod_count--;
2777 mdd_write_unlock(env, mdd_obj);
2779 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
2780 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
2781 "list\n", PFID(mdd_object_fid(mdd_obj)));
2785 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
2786 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
2787 /* check without any lock */
2788 is_orphan = mdd_obj->mod_count == 1 &&
2789 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
2793 /* mdd_trans_create() maybe failed because of barrier_entry(),
2794 * under such case, the orphan MDT-object will be left in the
2795 * orphan list, and when the MDT remount next time, the unused
2796 * orphans will be destroyed automatically.
2798 * One exception: the former mdd_finish_unlink may failed to
2799 * add the orphan MDT-object to the orphan list, then if the
2800 * mdd_trans_create() failed because of barrier_entry(), the
2801 * MDT-object will become real orphan that is neither in the
2802 * namespace nor in the orphan list. Such bad case should be
2803 * very rare and will be handled by e2fsck/lfsck. */
2804 handle = mdd_trans_create(env, mdo2mdd(obj));
2805 if (IS_ERR(handle)) {
2806 rc = PTR_ERR(handle);
2807 if (rc != -EINPROGRESS)
2815 rc = mdd_declare_close(env, mdd_obj, ma, handle);
2819 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
2823 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
2829 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
2830 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
2832 CERROR("Failed to get lu_attr of "DFID": %d\n",
2833 PFID(mdd_object_fid(mdd_obj)), rc);
2837 /* check again with lock */
2838 is_orphan = (mdd_obj->mod_count == 1) &&
2839 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
2840 ma->ma_attr.la_nlink == 0);
2842 if (is_orphan && !handle && !blocked) {
2843 mdd_write_unlock(env, mdd_obj);
2847 mdd_obj->mod_count--; /*release open count */
2849 if (!is_orphan || blocked)
2853 /* NB: Object maybe not in orphan list originally, it is rare case for
2854 * mdd_finish_unlink() failure, in that case, the object doesn't have
2855 * ORPHAN_OBJ flag */
2856 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
2857 /* remove link to object from orphan index */
2858 LASSERT(handle != NULL);
2859 rc = __mdd_orphan_del(env, mdd_obj, handle);
2861 CERROR("%s: unable to delete "DFID" from orphan list: "
2862 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
2863 PFID(mdd_object_fid(mdd_obj)), rc);
2864 /* If object was not deleted from orphan list, do not
2865 * destroy OSS objects, which will be done when next
2870 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
2871 "list, OSS objects to be destroyed.\n",
2872 PFID(mdd_object_fid(mdd_obj)));
2875 rc = mdo_destroy(env, mdd_obj, handle);
2878 CERROR("%s: unable to delete "DFID" from orphan list: "
2879 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
2880 PFID(mdd_object_fid(mdd_obj)), rc);
2885 mdd_write_unlock(env, mdd_obj);
2887 /* Record CL_CLOSE in changelog only if file was opened in write mode,
2888 * or if CL_OPEN was recorded.
2889 * Changelogs mask may change between open and close operations, but
2890 * this is not a big deal if we have a CL_CLOSE entry with no matching
2891 * CL_OPEN. Plus Changelogs mask may not change often.
2893 if (!rc && !blocked &&
2894 ((mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) ||
2895 (mdd->mdd_cl.mc_mask & (1 << CL_OPEN))) &&
2896 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
2897 if (handle == NULL) {
2898 handle = mdd_trans_create(env, mdo2mdd(obj));
2900 GOTO(stop, rc = PTR_ERR(handle));
2902 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL,
2907 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
2912 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
2917 if (handle != NULL && !IS_ERR(handle))
2918 rc = mdd_trans_stop(env, mdd, rc, handle);
2924 * Permission check is done when open,
2925 * no need check again.
2927 static int mdd_readpage_sanity_check(const struct lu_env *env,
2928 struct mdd_object *obj)
2930 struct dt_object *next = mdd_object_child(obj);
2934 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
2942 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
2943 size_t nob, const struct dt_it_ops *iops,
2944 struct dt_it *it, __u32 attr, void *arg)
2946 struct lu_dirpage *dp = &lp->lp_dir;
2950 struct lu_dirent *ent;
2951 struct lu_dirent *last = NULL;
2955 if (nob < sizeof(*dp))
2958 memset(area, 0, sizeof (*dp));
2959 area += sizeof (*dp);
2960 nob -= sizeof (*dp);
2967 len = iops->key_size(env, it);
2969 /* IAM iterator can return record with zero len. */
2973 hash = iops->store(env, it);
2974 if (unlikely(first)) {
2976 dp->ldp_hash_start = cpu_to_le64(hash);
2979 /* calculate max space required for lu_dirent */
2980 recsize = lu_dirent_calc_size(len, attr);
2982 if (nob >= recsize) {
2983 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
2984 if (result == -ESTALE)
2989 /* osd might not able to pack all attributes,
2990 * so recheck rec length */
2991 recsize = le16_to_cpu(ent->lde_reclen);
2993 if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
2994 fid_le_to_cpu(&fid, &ent->lde_fid);
2995 if (fid_is_dot_lustre(&fid))
2999 result = (last != NULL) ? 0 :-EINVAL;
3003 ent = (void *)ent + recsize;
3007 result = iops->next(env, it);
3008 if (result == -ESTALE)
3010 } while (result == 0);
3013 dp->ldp_hash_end = cpu_to_le64(hash);
3015 if (last->lde_hash == dp->ldp_hash_end)
3016 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
3017 last->lde_reclen = 0; /* end mark */
3020 /* end of directory */
3021 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3022 else if (result < 0)
3023 CWARN("build page failed: %d!\n", result);
3027 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
3028 const struct lu_rdpg *rdpg)
3030 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3034 if (mdd_object_exists(mdd_obj) == 0) {
3035 CERROR("%s: object "DFID" not found: rc = -2\n",
3036 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
3040 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
3041 rc = mdd_readpage_sanity_check(env, mdd_obj);
3043 GOTO(out_unlock, rc);
3045 if (mdd_is_dead_obj(mdd_obj)) {
3047 struct lu_dirpage *dp;
3050 * According to POSIX, please do not return any entry to client:
3051 * even dot and dotdot should not be returned.
3053 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
3054 PFID(mdd_object_fid(mdd_obj)));
3056 if (rdpg->rp_count <= 0)
3057 GOTO(out_unlock, rc = -EFAULT);
3058 LASSERT(rdpg->rp_pages != NULL);
3060 pg = rdpg->rp_pages[0];
3061 dp = (struct lu_dirpage *)kmap(pg);
3062 memset(dp, 0 , sizeof(struct lu_dirpage));
3063 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3064 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3065 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3067 GOTO(out_unlock, rc = LU_PAGE_SIZE);
3070 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
3071 mdd_dir_page_build, NULL);
3073 struct lu_dirpage *dp;
3075 dp = kmap(rdpg->rp_pages[0]);
3076 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
3079 * No pages were processed, mark this for first page
3082 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
3083 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
3084 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
3086 kunmap(rdpg->rp_pages[0]);
3089 GOTO(out_unlock, rc);
3091 mdd_read_unlock(env, mdd_obj);
3095 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
3097 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3099 if (mdd_object_exists(mdd_obj) == 0) {
3102 CERROR("%s: object "DFID" not found: rc = %d\n",
3103 mdd_obj_dev_name(mdd_obj),
3104 PFID(mdd_object_fid(mdd_obj)), rc);
3107 return dt_object_sync(env, mdd_object_child(mdd_obj),
3111 static int mdd_object_lock(const struct lu_env *env,
3112 struct md_object *obj,
3113 struct lustre_handle *lh,
3114 struct ldlm_enqueue_info *einfo,
3115 union ldlm_policy_data *policy)
3117 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3118 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
3122 static int mdd_object_unlock(const struct lu_env *env,
3123 struct md_object *obj,
3124 struct ldlm_enqueue_info *einfo,
3125 union ldlm_policy_data *policy)
3127 struct mdd_object *mdd_obj = md2mdd_obj(obj);
3128 return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
3131 const struct md_object_operations mdd_obj_ops = {
3132 .moo_permission = mdd_permission,
3133 .moo_attr_get = mdd_attr_get,
3134 .moo_attr_set = mdd_attr_set,
3135 .moo_xattr_get = mdd_xattr_get,
3136 .moo_xattr_set = mdd_xattr_set,
3137 .moo_xattr_list = mdd_xattr_list,
3138 .moo_invalidate = mdd_invalidate,
3139 .moo_xattr_del = mdd_xattr_del,
3140 .moo_swap_layouts = mdd_swap_layouts,
3141 .moo_open = mdd_open,
3142 .moo_close = mdd_close,
3143 .moo_readpage = mdd_readpage,
3144 .moo_readlink = mdd_readlink,
3145 .moo_changelog = mdd_changelog,
3146 .moo_object_sync = mdd_object_sync,
3147 .moo_object_lock = mdd_object_lock,
3148 .moo_object_unlock = mdd_object_unlock,
3149 .moo_layout_change = mdd_layout_change,