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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_object.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <linux/module.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lprocfs_status.h>
50 /* fid_be_cpu(), fid_cpu_to_be(). */
51 #include <lustre_fid.h>
52 #include <lustre_idmap.h>
53 #include <lustre_param.h>
54 #include <lustre_mds.h>
55 #include <lustre/lustre_idl.h>
57 #include "mdd_internal.h"
59 static const struct lu_object_operations mdd_lu_obj_ops;
60 extern struct kmem_cache *mdd_object_kmem;
62 static int mdd_xattr_get(const struct lu_env *env,
63 struct md_object *obj, struct lu_buf *buf,
66 int mdd_data_get(const struct lu_env *env, struct mdd_object *obj,
69 if (mdd_object_exists(obj) == 0) {
70 CERROR("%s: object "DFID" not found: rc = -2\n",
71 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
74 mdo_data_get(env, obj, data);
78 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
79 struct lu_attr *la, struct lustre_capa *capa)
81 if (mdd_object_exists(obj) == 0) {
82 CERROR("%s: object "DFID" not found: rc = -2\n",
83 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
87 return mdo_attr_get(env, obj, la, capa);
90 void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
92 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
94 if (flags & LUSTRE_APPEND_FL)
95 obj->mod_flags |= APPEND_OBJ;
97 if (flags & LUSTRE_IMMUTABLE_FL)
98 obj->mod_flags |= IMMUTE_OBJ;
101 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
103 struct mdd_thread_info *info;
105 lu_env_refill((struct lu_env *)env);
106 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
107 LASSERT(info != NULL);
111 const struct lu_name *mdd_name_get_const(const struct lu_env *env,
112 const void *area, ssize_t len)
114 struct lu_name *lname;
116 lname = &mdd_env_info(env)->mti_name;
117 lname->ln_name = area;
118 lname->ln_namelen = len;
122 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
126 buf = &mdd_env_info(env)->mti_buf[0];
132 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
133 const void *area, ssize_t len)
137 buf = &mdd_env_info(env)->mti_buf[0];
138 buf->lb_buf = (void *)area;
143 struct lu_object *mdd_object_alloc(const struct lu_env *env,
144 const struct lu_object_header *hdr,
147 struct mdd_object *mdd_obj;
149 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
150 if (mdd_obj != NULL) {
153 o = mdd2lu_obj(mdd_obj);
154 lu_object_init(o, NULL, d);
155 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
156 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
157 mdd_obj->mod_count = 0;
158 o->lo_ops = &mdd_lu_obj_ops;
165 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
166 const struct lu_object_conf *unused)
168 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
169 struct mdd_object *mdd_obj = lu2mdd_obj(o);
170 struct lu_object *below;
171 struct lu_device *under;
174 mdd_obj->mod_cltime = 0;
175 under = &d->mdd_child->dd_lu_dev;
176 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
178 RETURN(PTR_ERR(below));
180 lu_object_add(o, below);
185 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
189 if (lu_object_exists(o)) {
190 struct mdd_object *mdd_obj = lu2mdd_obj(o);
191 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
193 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
195 mdd_flags_xlate(mdd_obj, attr->la_flags);
201 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
203 struct mdd_object *mdd = lu2mdd_obj(o);
206 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
209 static int mdd_object_print(const struct lu_env *env, void *cookie,
210 lu_printer_t p, const struct lu_object *o)
212 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
213 return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p(open_count=%d, "
214 "valid=%x, cltime="LPU64", flags=%lx)",
215 mdd, mdd->mod_count, mdd->mod_valid,
216 mdd->mod_cltime, mdd->mod_flags);
219 static const struct lu_object_operations mdd_lu_obj_ops = {
220 .loo_object_init = mdd_object_init,
221 .loo_object_start = mdd_object_start,
222 .loo_object_free = mdd_object_free,
223 .loo_object_print = mdd_object_print,
226 struct mdd_object *mdd_object_find(const struct lu_env *env,
227 struct mdd_device *d,
228 const struct lu_fid *f)
230 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
234 * No permission check is needed.
236 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
239 struct mdd_object *mdd_obj = md2mdd_obj(obj);
244 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
245 mdd_object_capa(env, md2mdd_obj(obj)));
246 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
247 ma->ma_attr.la_nlink = 0;
253 * No permission check is needed.
255 static int mdd_xattr_get(const struct lu_env *env,
256 struct md_object *obj, struct lu_buf *buf,
259 struct mdd_object *mdd_obj = md2mdd_obj(obj);
264 if (mdd_object_exists(mdd_obj) == 0) {
265 CERROR("%s: object "DFID" not found: rc = -2\n",
266 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
270 /* If the object has been delete from the namespace, then
271 * get linkEA should return -ENOENT as well */
272 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
273 strcmp(name, XATTR_NAME_LINK) == 0))
276 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
277 rc = mdo_xattr_get(env, mdd_obj, buf, name,
278 mdd_object_capa(env, mdd_obj));
279 mdd_read_unlock(env, mdd_obj);
285 * Permission check is done when open,
286 * no need check again.
288 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
291 struct mdd_object *mdd_obj = md2mdd_obj(obj);
292 struct dt_object *next;
297 if (mdd_object_exists(mdd_obj) == 0) {
298 CERROR("%s: object "DFID" not found: rc = -2\n",
299 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
303 next = mdd_object_child(mdd_obj);
304 LASSERT(next != NULL);
305 LASSERT(next->do_body_ops != NULL);
306 LASSERT(next->do_body_ops->dbo_read != NULL);
307 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
308 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
309 mdd_object_capa(env, mdd_obj));
310 mdd_read_unlock(env, mdd_obj);
315 * No permission check is needed.
317 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
320 struct mdd_object *mdd_obj = md2mdd_obj(obj);
325 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
326 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
327 mdd_read_unlock(env, mdd_obj);
332 int mdd_declare_object_create_internal(const struct lu_env *env,
333 struct mdd_object *p,
334 struct mdd_object *c,
335 struct lu_attr *attr,
336 struct thandle *handle,
337 const struct md_op_spec *spec,
338 struct dt_allocation_hint *hint)
340 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
341 const struct dt_index_features *feat = spec->sp_feat;
345 if (feat != &dt_directory_features && feat != NULL) {
346 dof->dof_type = DFT_INDEX;
347 dof->u.dof_idx.di_feat = feat;
350 dof->dof_type = dt_mode_to_dft(attr->la_mode);
351 if (dof->dof_type == DFT_REGULAR) {
352 dof->u.dof_reg.striped =
353 md_should_create(spec->sp_cr_flags);
354 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
355 dof->u.dof_reg.striped = 0;
356 /* is this replay? */
358 dof->u.dof_reg.striped = 0;
362 rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
367 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
368 struct mdd_object *c, struct lu_attr *attr,
369 struct thandle *handle,
370 const struct md_op_spec *spec,
371 struct dt_allocation_hint *hint)
373 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
377 LASSERT(!mdd_object_exists(c));
379 rc = mdo_create_obj(env, c, attr, hint, dof, handle);
381 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
386 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
387 const struct lu_attr *attr, struct thandle *handle,
393 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
394 #ifdef CONFIG_FS_POSIX_ACL
395 if (!rc && (attr->la_valid & LA_MODE) && needacl)
396 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
401 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
402 const struct lu_attr *oattr, struct lu_attr *attr,
403 struct thandle *handle)
408 LASSERT(attr->la_valid & LA_CTIME);
409 LASSERT(oattr != NULL);
411 /* Make sure the ctime is increased only, however, it's not strictly
412 * reliable at here because there is not guarantee to hold lock on
413 * object, so we just bypass some unnecessary cmtime setting first
414 * and OSD has to check it again. */
415 if (attr->la_ctime < oattr->la_ctime)
416 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
417 else if (attr->la_valid == LA_CTIME &&
418 attr->la_ctime == oattr->la_ctime)
419 attr->la_valid &= ~LA_CTIME;
421 if (attr->la_valid != 0)
422 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
427 * This gives the same functionality as the code between
428 * sys_chmod and inode_setattr
429 * chown_common and inode_setattr
430 * utimes and inode_setattr
431 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
433 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
434 const struct lu_attr *oattr, struct lu_attr *la,
435 const unsigned long flags)
444 /* Do not permit change file type */
445 if (la->la_valid & LA_TYPE)
448 /* They should not be processed by setattr */
449 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
452 LASSERT(oattr != NULL);
454 /* export destroy does not have ->le_ses, but we may want
455 * to drop LUSTRE_SOM_FL. */
456 uc = lu_ucred_check(env);
460 if (la->la_valid == LA_CTIME) {
461 if (!(flags & MDS_PERM_BYPASS))
462 /* This is only for set ctime when rename's source is
464 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
466 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
467 la->la_valid &= ~LA_CTIME;
471 if (la->la_valid == LA_ATIME) {
472 /* This is atime only set for read atime update on close. */
473 if (la->la_atime >= oattr->la_atime &&
474 la->la_atime < (oattr->la_atime +
475 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
476 la->la_valid &= ~LA_ATIME;
480 /* Check if flags change. */
481 if (la->la_valid & LA_FLAGS) {
482 unsigned int oldflags = 0;
483 unsigned int newflags = la->la_flags &
484 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
486 if ((uc->uc_fsuid != oattr->la_uid) &&
487 !md_capable(uc, CFS_CAP_FOWNER))
490 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
491 * only be changed by the relevant capability. */
492 if (mdd_is_immutable(obj))
493 oldflags |= LUSTRE_IMMUTABLE_FL;
494 if (mdd_is_append(obj))
495 oldflags |= LUSTRE_APPEND_FL;
496 if ((oldflags ^ newflags) &&
497 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
500 if (!S_ISDIR(oattr->la_mode))
501 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
504 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
505 (la->la_valid & ~LA_FLAGS) &&
506 !(flags & MDS_PERM_BYPASS))
509 /* Check for setting the obj time. */
510 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
511 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
512 if ((uc->uc_fsuid != oattr->la_uid) &&
513 !md_capable(uc, CFS_CAP_FOWNER)) {
514 rc = mdd_permission_internal(env, obj, oattr,
521 if (la->la_valid & LA_KILL_SUID) {
522 la->la_valid &= ~LA_KILL_SUID;
523 if ((oattr->la_mode & S_ISUID) &&
524 !(la->la_valid & LA_MODE)) {
525 la->la_mode = oattr->la_mode;
526 la->la_valid |= LA_MODE;
528 la->la_mode &= ~S_ISUID;
531 if (la->la_valid & LA_KILL_SGID) {
532 la->la_valid &= ~LA_KILL_SGID;
533 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
534 (S_ISGID | S_IXGRP)) &&
535 !(la->la_valid & LA_MODE)) {
536 la->la_mode = oattr->la_mode;
537 la->la_valid |= LA_MODE;
539 la->la_mode &= ~S_ISGID;
542 /* Make sure a caller can chmod. */
543 if (la->la_valid & LA_MODE) {
544 if (!(flags & MDS_PERM_BYPASS) &&
545 (uc->uc_fsuid != oattr->la_uid) &&
546 !md_capable(uc, CFS_CAP_FOWNER))
549 if (la->la_mode == (umode_t) -1)
550 la->la_mode = oattr->la_mode;
552 la->la_mode = (la->la_mode & S_IALLUGO) |
553 (oattr->la_mode & ~S_IALLUGO);
555 /* Also check the setgid bit! */
556 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
557 la->la_gid : oattr->la_gid) &&
558 !md_capable(uc, CFS_CAP_FSETID))
559 la->la_mode &= ~S_ISGID;
561 la->la_mode = oattr->la_mode;
564 /* Make sure a caller can chown. */
565 if (la->la_valid & LA_UID) {
566 if (la->la_uid == (uid_t) -1)
567 la->la_uid = oattr->la_uid;
568 if (((uc->uc_fsuid != oattr->la_uid) ||
569 (la->la_uid != oattr->la_uid)) &&
570 !md_capable(uc, CFS_CAP_CHOWN))
573 /* If the user or group of a non-directory has been
574 * changed by a non-root user, remove the setuid bit.
575 * 19981026 David C Niemi <niemi@tux.org>
577 * Changed this to apply to all users, including root,
578 * to avoid some races. This is the behavior we had in
579 * 2.0. The check for non-root was definitely wrong
580 * for 2.2 anyway, as it should have been using
581 * CAP_FSETID rather than fsuid -- 19990830 SD. */
582 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
583 !S_ISDIR(oattr->la_mode)) {
584 la->la_mode &= ~S_ISUID;
585 la->la_valid |= LA_MODE;
589 /* Make sure caller can chgrp. */
590 if (la->la_valid & LA_GID) {
591 if (la->la_gid == (gid_t) -1)
592 la->la_gid = oattr->la_gid;
593 if (((uc->uc_fsuid != oattr->la_uid) ||
594 ((la->la_gid != oattr->la_gid) &&
595 !lustre_in_group_p(uc, la->la_gid))) &&
596 !md_capable(uc, CFS_CAP_CHOWN))
599 /* Likewise, if the user or group of a non-directory
600 * has been changed by a non-root user, remove the
601 * setgid bit UNLESS there is no group execute bit
602 * (this would be a file marked for mandatory
603 * locking). 19981026 David C Niemi <niemi@tux.org>
605 * Removed the fsuid check (see the comment above) --
607 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
608 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
609 la->la_mode &= ~S_ISGID;
610 la->la_valid |= LA_MODE;
614 /* For both Size-on-MDS case and truncate case,
615 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
616 * We distinguish them by "flags & MDS_SOM".
617 * For SOM case, it is true, the MAY_WRITE perm has been checked
618 * when open, no need check again. For truncate case, it is false,
619 * the MAY_WRITE perm should be checked here. */
620 if (flags & MDS_SOM) {
621 /* For the "Size-on-MDS" setattr update, merge coming
622 * attributes with the set in the inode. BUG 10641 */
623 if ((la->la_valid & LA_ATIME) &&
624 (la->la_atime <= oattr->la_atime))
625 la->la_valid &= ~LA_ATIME;
627 /* OST attributes do not have a priority over MDS attributes,
628 * so drop times if ctime is equal. */
629 if ((la->la_valid & LA_CTIME) &&
630 (la->la_ctime <= oattr->la_ctime))
631 la->la_valid &= ~(LA_MTIME | LA_CTIME);
633 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
634 if (!((flags & MDS_OWNEROVERRIDE) &&
635 (uc->uc_fsuid == oattr->la_uid)) &&
636 !(flags & MDS_PERM_BYPASS)) {
637 rc = mdd_permission_internal(env, obj,
643 if (la->la_valid & LA_CTIME) {
644 /* The pure setattr, it has the priority over what is
645 * already set, do not drop it if ctime is equal. */
646 if (la->la_ctime < oattr->la_ctime)
647 la->la_valid &= ~(LA_ATIME | LA_MTIME |
655 /** Store a data change changelog record
656 * If this fails, we must fail the whole transaction; we don't
657 * want the change to commit without the log entry.
658 * \param mdd_obj - mdd_object of change
659 * \param handle - transacion handle
661 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
662 enum changelog_rec_type type, int flags,
663 struct mdd_object *mdd_obj, struct thandle *handle)
665 const struct lu_fid *tfid;
666 struct llog_changelog_rec *rec;
672 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
674 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
677 LASSERT(mdd_obj != NULL);
678 LASSERT(handle != NULL);
680 tfid = mdo2fid(mdd_obj);
682 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
683 cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
684 /* Don't need multiple updates in this log */
685 /* Don't check under lock - no big deal if we get an extra
690 reclen = llog_data_len(sizeof(*rec));
691 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
692 if (buf->lb_buf == NULL)
696 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
697 rec->cr.cr_type = (__u32)type;
698 rec->cr.cr_tfid = *tfid;
699 rec->cr.cr_namelen = 0;
700 mdd_obj->mod_cltime = cfs_time_current_64();
702 rc = mdd_changelog_store(env, mdd, rec, handle);
707 int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
708 int flags, struct md_object *obj)
710 struct thandle *handle;
711 struct mdd_object *mdd_obj = md2mdd_obj(obj);
712 struct mdd_device *mdd = mdo2mdd(obj);
716 handle = mdd_trans_create(env, mdd);
718 RETURN(PTR_ERR(handle));
720 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
724 rc = mdd_trans_start(env, mdd, handle);
728 rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
732 mdd_trans_stop(env, mdd, rc, handle);
738 * Save LMA extended attributes with data from \a ma.
740 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
741 * not, LMA EA will be first read from disk, modified and write back.
744 /* Precedence for choosing record type when multiple
745 * attributes change: setattr > mtime > ctime > atime
746 * (ctime changes when mtime does, plus chmod/chown.
747 * atime and ctime are independent.) */
748 static int mdd_attr_set_changelog(const struct lu_env *env,
749 struct md_object *obj, struct thandle *handle,
752 struct mdd_device *mdd = mdo2mdd(obj);
755 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
756 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
757 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
758 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
759 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
760 bits = bits & mdd->mdd_cl.mc_mask;
761 /* This is an implementation limit rather than a protocol limit */
762 CLASSERT(CL_LAST <= sizeof(int) * 8);
766 /* The record type is the lowest non-masked set bit */
769 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
770 return mdd_changelog_data_store(env, mdd, type, (int)valid,
771 md2mdd_obj(obj), handle);
774 static int mdd_declare_attr_set(const struct lu_env *env,
775 struct mdd_device *mdd,
776 struct mdd_object *obj,
777 const struct lu_attr *attr,
778 struct thandle *handle)
782 rc = mdo_declare_attr_set(env, obj, attr, handle);
786 #ifdef CONFIG_FS_POSIX_ACL
787 if (attr->la_valid & LA_MODE) {
788 mdd_read_lock(env, obj, MOR_TGT_CHILD);
789 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
790 XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
791 mdd_read_unlock(env, obj);
792 if (rc == -EOPNOTSUPP || rc == -ENODATA)
798 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
799 rc = mdo_declare_xattr_set(env, obj, buf,
800 XATTR_NAME_ACL_ACCESS, 0,
808 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
815 * permission changes may require sync operation, to mitigate performance
816 * impact, only do this for dir and when permission is reduced.
818 * For regular files, version is updated with permission change (see VBR), async
819 * permission won't cause any issue, while missing permission change on
820 * directory may affect accessibility of other objects after recovery.
822 static inline bool permission_needs_sync(const struct lu_attr *old,
823 const struct lu_attr *new)
825 if (!S_ISDIR(old->la_mode))
828 if (new->la_valid & (LA_UID | LA_GID))
831 if (new->la_valid & LA_MODE &&
832 new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
835 if ((new->la_valid & LA_MODE) &&
836 ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
837 (old->la_mode & S_IRWXUGO))
843 /* set attr and LOV EA at once, return updated attr */
844 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
845 const struct md_attr *ma)
847 struct mdd_object *mdd_obj = md2mdd_obj(obj);
848 struct mdd_device *mdd = mdo2mdd(obj);
849 struct thandle *handle;
850 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
851 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
852 const struct lu_attr *la = &ma->ma_attr;
856 /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
857 LASSERT((ma->ma_valid & MA_LOV) == 0);
858 LASSERT((ma->ma_valid & MA_HSM) == 0);
859 LASSERT((ma->ma_valid & MA_SOM) == 0);
861 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
865 *la_copy = ma->ma_attr;
866 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
870 /* setattr on "close" only change atime, or do nothing */
871 if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
874 handle = mdd_trans_create(env, mdd);
876 RETURN(PTR_ERR(handle));
878 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
882 rc = mdd_trans_start(env, mdd, handle);
886 if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
889 if (la->la_valid & (LA_MTIME | LA_CTIME))
890 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
891 la->la_mtime, la->la_ctime);
893 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
894 if (la_copy->la_valid & LA_FLAGS) {
895 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
897 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
898 } else if (la_copy->la_valid) { /* setattr */
899 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
901 mdd_write_unlock(env, mdd_obj);
904 rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
909 mdd_trans_stop(env, mdd, rc, handle);
913 static int mdd_xattr_sanity_check(const struct lu_env *env,
914 struct mdd_object *obj,
915 const struct lu_attr *attr)
917 struct lu_ucred *uc = lu_ucred_assert(env);
920 if (mdd_is_immutable(obj) || mdd_is_append(obj))
923 if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
929 static int mdd_declare_xattr_set(const struct lu_env *env,
930 struct mdd_device *mdd,
931 struct mdd_object *obj,
932 const struct lu_buf *buf,
934 int fl, struct thandle *handle)
938 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
942 /* Only record user and layout xattr changes */
943 if (strncmp(XATTR_USER_PREFIX, name,
944 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
945 strcmp(XATTR_NAME_LOV, name) == 0) {
946 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
951 /* If HSM data is modified, this could add a changelog */
952 if (strcmp(XATTR_NAME_HSM, name) == 0) {
953 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
958 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
963 * Compare current and future data of HSM EA and add a changelog if needed.
965 * Caller should have write-locked \param obj.
967 * \param buf - Future HSM EA content.
968 * \retval 0 if no changelog is needed or changelog was added properly.
969 * \retval -ve errno if there was a problem
971 static int mdd_hsm_update_locked(const struct lu_env *env,
972 struct md_object *obj,
973 const struct lu_buf *buf,
974 struct thandle *handle)
976 struct mdd_thread_info *info = mdd_env_info(env);
977 struct mdd_device *mdd = mdo2mdd(obj);
978 struct mdd_object *mdd_obj = md2mdd_obj(obj);
979 struct lu_buf *current_buf;
980 struct md_hsm *current_mh;
981 struct md_hsm *new_mh;
985 OBD_ALLOC_PTR(current_mh);
986 if (current_mh == NULL)
989 /* Read HSM attrs from disk */
990 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
991 current_buf = mdd_buf_get(env, info->mti_xattr_buf,
992 sizeof(info->mti_xattr_buf));
993 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
994 mdd_object_capa(env, mdd_obj));
995 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
996 if (rc < 0 && rc != -ENODATA)
998 else if (rc == -ENODATA)
999 current_mh->mh_flags = 0;
1001 /* Map future HSM xattr */
1002 OBD_ALLOC_PTR(new_mh);
1004 GOTO(free, rc = -ENOMEM);
1005 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1007 /* If HSM flags are different, add a changelog */
1009 if (current_mh->mh_flags != new_mh->mh_flags) {
1011 hsm_set_cl_event(&flags, HE_STATE);
1012 if (new_mh->mh_flags & HS_DIRTY)
1013 hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
1015 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
1019 OBD_FREE_PTR(new_mh);
1021 OBD_FREE_PTR(current_mh);
1026 * The caller should guarantee to update the object ctime
1027 * after xattr_set if needed.
1029 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1030 const struct lu_buf *buf, const char *name,
1033 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1034 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1035 struct mdd_device *mdd = mdo2mdd(obj);
1036 struct thandle *handle;
1040 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1044 rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1048 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1049 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1053 handle = mdd_trans_create(env, mdd);
1055 RETURN(PTR_ERR(handle));
1057 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1061 rc = mdd_trans_start(env, mdd, handle);
1065 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1067 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1068 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1070 mdd_write_unlock(env, mdd_obj);
1075 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1076 mdd_object_capa(env, mdd_obj));
1077 mdd_write_unlock(env, mdd_obj);
1081 if (strcmp(XATTR_NAME_LOV, name) == 0)
1082 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1084 else if (strncmp(XATTR_USER_PREFIX, name,
1085 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1086 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1087 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1088 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1089 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1090 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1094 mdd_trans_stop(env, mdd, rc, handle);
1099 static int mdd_declare_xattr_del(const struct lu_env *env,
1100 struct mdd_device *mdd,
1101 struct mdd_object *obj,
1103 struct thandle *handle)
1107 rc = mdo_declare_xattr_del(env, obj, name, handle);
1111 /* Only record user xattr changes */
1112 if ((strncmp(XATTR_USER_PREFIX, name,
1113 sizeof(XATTR_USER_PREFIX) - 1) == 0))
1114 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1120 * The caller should guarantee to update the object ctime
1121 * after xattr_set if needed.
1123 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1126 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1127 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1128 struct mdd_device *mdd = mdo2mdd(obj);
1129 struct thandle *handle;
1133 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1137 rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1141 handle = mdd_trans_create(env, mdd);
1143 RETURN(PTR_ERR(handle));
1145 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1149 rc = mdd_trans_start(env, mdd, handle);
1153 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1154 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1155 mdd_object_capa(env, mdd_obj));
1156 mdd_write_unlock(env, mdd_obj);
1160 /* Only record system & user xattr changes */
1161 if (strncmp(XATTR_USER_PREFIX, name,
1162 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1163 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1164 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1165 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1166 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1167 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1171 mdd_trans_stop(env, mdd, rc, handle);
1177 * read lov EA of an object
1178 * return the lov EA in an allocated lu_buf
1180 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1181 struct lu_buf *lmm_buf)
1183 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
1188 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1189 mdd_object_capa(env, obj));
1191 if (rc == -ERANGE) {
1192 /* mti_big_buf is allocated but is too small
1193 * we need to increase it */
1194 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1196 if (buf->lb_buf == NULL)
1197 GOTO(out, rc = -ENOMEM);
1208 if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1209 /* mti_big_buf was not allocated, so we have to
1210 * allocate it based on the ea size */
1211 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1213 if (buf->lb_buf == NULL)
1214 GOTO(out, rc = -ENOMEM);
1218 lu_buf_alloc(lmm_buf, bufsize);
1219 if (lmm_buf->lb_buf == NULL)
1220 GOTO(out, rc = -ENOMEM);
1222 memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1228 lu_buf_free(lmm_buf);
1232 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1233 struct mdd_object *o, struct lu_buf *buf,
1234 struct thandle *handle)
1236 struct hsm_attrs *attrs;
1242 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1243 handle, mdd_object_capa(env, o));
1247 attrs = buf->lb_buf;
1248 hsm_flags = le32_to_cpu(attrs->hsm_flags);
1249 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1252 /* Add a changelog record for release. */
1253 hsm_set_cl_event(&flags, HE_RELEASE);
1254 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1260 * check if layout swapping between 2 objects is allowed
1262 * - only normal FIDs or non-system IGIFs
1263 * - same type of objects
1264 * - same owner/group (so quotas are still valid)
1266 static int mdd_layout_swap_allowed(const struct lu_env *env,
1267 struct mdd_object *o1,
1268 const struct lu_attr *attr1,
1269 struct mdd_object *o2,
1270 const struct lu_attr *attr2)
1272 const struct lu_fid *fid1, *fid2;
1278 if (!fid_is_norm(fid1) &&
1279 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1282 if (!fid_is_norm(fid2) &&
1283 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1286 if (mdd_object_type(o1) != mdd_object_type(o2)) {
1287 if (S_ISDIR(mdd_object_type(o1)))
1289 if (S_ISREG(mdd_object_type(o1)))
1294 if ((attr1->la_uid != attr2->la_uid) ||
1295 (attr1->la_gid != attr2->la_gid))
1302 * swap layouts between 2 lustre objects
1304 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1305 struct md_object *obj2, __u64 flags)
1307 struct mdd_thread_info *info = mdd_env_info(env);
1308 struct mdd_object *fst_o = md2mdd_obj(obj1);
1309 struct mdd_object *snd_o = md2mdd_obj(obj2);
1310 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
1311 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
1312 struct mdd_device *mdd = mdo2mdd(obj1);
1313 struct lov_mds_md *fst_lmm, *snd_lmm;
1314 struct lu_buf *fst_buf = &info->mti_buf[0];
1315 struct lu_buf *snd_buf = &info->mti_buf[1];
1316 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
1317 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
1318 struct ost_id *saved_oi = NULL;
1319 struct thandle *handle;
1320 __u16 fst_gen, snd_gen;
1326 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
1327 memset(info->mti_buf, 0, sizeof(info->mti_buf));
1329 /* we have to sort the 2 obj, so locking will always
1330 * be in the same order, even in case of 2 concurrent swaps */
1331 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
1332 if (rc == 0) /* same fid ? */
1338 rc = mdd_la_get(env, fst_o, fst_la, BYPASS_CAPA);
1342 rc = mdd_la_get(env, snd_o, snd_la, BYPASS_CAPA);
1346 /* check if layout swapping is allowed */
1347 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
1351 handle = mdd_trans_create(env, mdd);
1353 RETURN(PTR_ERR(handle));
1355 /* objects are already sorted */
1356 mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
1357 mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
1359 rc = mdd_get_lov_ea(env, fst_o, fst_buf);
1360 if (rc < 0 && rc != -ENODATA)
1363 rc = mdd_get_lov_ea(env, snd_o, snd_buf);
1364 if (rc < 0 && rc != -ENODATA)
1367 /* swapping 2 non existant layouts is a success */
1368 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
1371 /* to help inode migration between MDT, it is better to
1372 * start by the no layout file (if one), so we order the swap */
1373 if (snd_buf->lb_buf == NULL) {
1375 swap(fst_buf, snd_buf);
1378 /* lmm and generation layout initialization */
1379 if (fst_buf->lb_buf != NULL) {
1380 fst_lmm = fst_buf->lb_buf;
1381 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1382 fst_fl = LU_XATTR_REPLACE;
1386 fst_fl = LU_XATTR_CREATE;
1389 snd_lmm = snd_buf->lb_buf;
1390 snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1392 /* increase the generation layout numbers */
1396 /* set the file specific informations in lmm */
1397 if (fst_lmm != NULL) {
1398 saved_oi = &info->mti_oa.o_oi;
1400 *saved_oi = fst_lmm->lmm_oi;
1401 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1402 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1403 snd_lmm->lmm_oi = *saved_oi;
1405 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1406 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1407 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1408 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1410 GOTO(stop, rc = -EPROTO);
1412 snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1414 /* Prepare HSM attribute if it's required */
1415 if (flags & SWAP_LAYOUTS_MDS_HSM) {
1416 const int buflen = sizeof(struct hsm_attrs);
1418 lu_buf_alloc(fst_hsm_buf, buflen);
1419 lu_buf_alloc(snd_hsm_buf, buflen);
1420 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
1421 GOTO(stop, rc = -ENOMEM);
1423 /* Read HSM attribute */
1424 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM,
1429 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM,
1434 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
1435 XATTR_NAME_HSM, LU_XATTR_REPLACE,
1440 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
1441 XATTR_NAME_HSM, LU_XATTR_REPLACE,
1447 /* prepare transaction */
1448 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1453 if (fst_buf->lb_buf != NULL)
1454 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1455 XATTR_NAME_LOV, LU_XATTR_REPLACE,
1458 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1463 rc = mdd_trans_start(env, mdd, handle);
1467 if (flags & SWAP_LAYOUTS_MDS_HSM) {
1468 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
1472 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
1474 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
1477 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
1478 mdd_obj_dev_name(fst_o),
1479 PFID(mdo2fid(fst_o)), rc, rc2);
1484 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1485 mdd_object_capa(env, fst_o));
1489 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
1492 if (fst_buf->lb_buf != NULL)
1493 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1494 LU_XATTR_REPLACE, handle,
1495 mdd_object_capa(env, snd_o));
1497 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1498 mdd_object_capa(env, snd_o));
1504 /* failure on second file, but first was done, so we have
1505 * to roll back first. */
1506 if (fst_buf->lb_buf != NULL) {
1507 fst_lmm->lmm_oi = *saved_oi;
1508 fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
1509 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1510 LU_XATTR_REPLACE, handle,
1511 mdd_object_capa(env, fst_o));
1513 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1514 mdd_object_capa(env, fst_o));
1520 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
1525 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
1530 CERROR("%s: unable to roll back layout swap. FIDs: "
1531 DFID" and "DFID "error: %d/%d, steps: %d\n",
1532 mdd_obj_dev_name(fst_o),
1533 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1535 /* a solution to avoid journal commit is to panic,
1536 * but it has strong consequences so we use LBUG to
1537 * allow sysdamin to choose to panic or not
1544 /* Issue one changelog record per file */
1545 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1549 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1555 mdd_trans_stop(env, mdd, rc, handle);
1556 mdd_write_unlock(env, snd_o);
1557 mdd_write_unlock(env, fst_o);
1559 lu_buf_free(fst_buf);
1560 lu_buf_free(snd_buf);
1561 lu_buf_free(fst_hsm_buf);
1562 lu_buf_free(snd_hsm_buf);
1566 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1567 struct mdd_object *child, const struct lu_attr *attr,
1568 const struct md_op_spec *spec,
1569 struct dt_allocation_hint *hint)
1571 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
1572 struct dt_object *nc = mdd_object_child(child);
1574 memset(hint, 0, sizeof(*hint));
1576 /* For striped directory, give striping EA to lod_ah_init, which will
1577 * decide the stripe_offset and stripe count by it. */
1578 if (S_ISDIR(attr->la_mode) &&
1579 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1580 hint->dah_eadata = spec->u.sp_ea.eadata;
1581 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
1583 hint->dah_eadata = NULL;
1584 hint->dah_eadata_len = 0;
1587 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
1588 hint->dah_eadata, hint->dah_eadata_len);
1589 /* @hint will be initialized by underlying device. */
1590 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1594 * do NOT or the MAY_*'s, you'll get the weakest
1596 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
1600 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1601 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1602 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1603 * owner can write to a file even if it is marked readonly to hide
1604 * its brokenness. (bug 5781) */
1605 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1606 struct lu_ucred *uc = lu_ucred_check(env);
1608 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1612 if (flags & FMODE_READ)
1614 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1616 if (flags & MDS_FMODE_EXEC)
1621 static int mdd_open_sanity_check(const struct lu_env *env,
1622 struct mdd_object *obj,
1623 const struct lu_attr *attr, int flag)
1629 if (mdd_is_dead_obj(obj))
1632 if (S_ISLNK(attr->la_mode))
1635 mode = accmode(env, attr, flag);
1637 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
1640 if (!(flag & MDS_OPEN_CREATED)) {
1641 rc = mdd_permission_internal(env, obj, attr, mode);
1646 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
1647 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
1648 flag &= ~MDS_OPEN_TRUNC;
1650 /* For writing append-only file must open it with append mode. */
1651 if (mdd_is_append(obj)) {
1652 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1654 if (flag & MDS_OPEN_TRUNC)
1660 * Now, flag -- O_NOATIME does not be packed by client.
1662 if (flag & O_NOATIME) {
1663 struct lu_ucred *uc = lu_ucred(env);
1665 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1666 (uc->uc_valid == UCRED_NEW)) &&
1667 (uc->uc_fsuid != attr->la_uid) &&
1668 !md_capable(uc, CFS_CAP_FOWNER))
1676 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1679 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1680 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1683 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1685 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1689 rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
1691 mdd_obj->mod_count++;
1693 mdd_write_unlock(env, mdd_obj);
1697 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
1698 struct md_attr *ma, struct thandle *handle)
1700 return mdo_declare_destroy(env, obj, handle);
1703 /* return md_attr back,
1704 * if it is last unlink then return lov ea + llog cookie*/
1705 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1706 struct md_attr *ma, struct thandle *handle)
1711 rc = mdo_destroy(env, obj, handle);
1716 static int mdd_declare_close(const struct lu_env *env,
1717 struct mdd_object *obj,
1719 struct thandle *handle)
1723 rc = orph_declare_index_delete(env, obj, handle);
1727 return mdo_declare_destroy(env, obj, handle);
1731 * No permission check is needed.
1733 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1734 struct md_attr *ma, int mode)
1736 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1737 struct mdd_device *mdd = mdo2mdd(obj);
1738 struct thandle *handle = NULL;
1739 int rc, is_orphan = 0;
1742 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1743 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1744 mdd_obj->mod_count--;
1745 mdd_write_unlock(env, mdd_obj);
1747 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1748 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1749 "list\n", PFID(mdd_object_fid(mdd_obj)));
1753 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1754 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1755 /* check without any lock */
1756 is_orphan = mdd_obj->mod_count == 1 &&
1757 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1761 handle = mdd_trans_create(env, mdo2mdd(obj));
1763 RETURN(PTR_ERR(handle));
1765 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1769 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1773 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1778 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1779 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1780 mdd_object_capa(env, mdd_obj));
1782 CERROR("Failed to get lu_attr of "DFID": %d\n",
1783 PFID(mdd_object_fid(mdd_obj)), rc);
1787 /* check again with lock */
1788 is_orphan = (mdd_obj->mod_count == 1) &&
1789 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1790 ma->ma_attr.la_nlink == 0);
1792 if (is_orphan && handle == NULL) {
1793 mdd_write_unlock(env, mdd_obj);
1797 mdd_obj->mod_count--; /*release open count */
1803 /* NB: Object maybe not in orphan list originally, it is rare case for
1804 * mdd_finish_unlink() failure, in that case, the object doesn't have
1805 * ORPHAN_OBJ flag */
1806 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1807 /* remove link to object from orphan index */
1808 LASSERT(handle != NULL);
1809 rc = __mdd_orphan_del(env, mdd_obj, handle);
1811 CERROR("%s: unable to delete "DFID" from orphan list: "
1812 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1813 PFID(mdd_object_fid(mdd_obj)), rc);
1814 /* If object was not deleted from orphan list, do not
1815 * destroy OSS objects, which will be done when next
1820 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1821 "list, OSS objects to be destroyed.\n",
1822 PFID(mdd_object_fid(mdd_obj)));
1825 rc = mdo_destroy(env, mdd_obj, handle);
1828 CERROR("%s: unable to delete "DFID" from orphan list: "
1829 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1830 PFID(mdd_object_fid(mdd_obj)), rc);
1835 mdd_write_unlock(env, mdd_obj);
1838 (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1839 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1840 if (handle == NULL) {
1841 handle = mdd_trans_create(env, mdo2mdd(obj));
1843 GOTO(stop, rc = PTR_ERR(handle));
1845 rc = mdd_declare_changelog_store(env, mdd, NULL,
1850 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1855 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1860 if (handle != NULL && !IS_ERR(handle))
1861 mdd_trans_stop(env, mdd, rc, handle);
1867 * Permission check is done when open,
1868 * no need check again.
1870 static int mdd_readpage_sanity_check(const struct lu_env *env,
1871 struct mdd_object *obj)
1873 struct dt_object *next = mdd_object_child(obj);
1877 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1885 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1886 int nob, const struct dt_it_ops *iops,
1887 struct dt_it *it, __u32 attr, void *arg)
1889 struct lu_dirpage *dp = &lp->lp_dir;
1893 struct lu_dirent *ent;
1894 struct lu_dirent *last = NULL;
1897 memset(area, 0, sizeof (*dp));
1898 area += sizeof (*dp);
1899 nob -= sizeof (*dp);
1906 len = iops->key_size(env, it);
1908 /* IAM iterator can return record with zero len. */
1912 hash = iops->store(env, it);
1913 if (unlikely(first)) {
1915 dp->ldp_hash_start = cpu_to_le64(hash);
1918 /* calculate max space required for lu_dirent */
1919 recsize = lu_dirent_calc_size(len, attr);
1921 if (nob >= recsize) {
1922 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1923 if (result == -ESTALE)
1928 /* osd might not able to pack all attributes,
1929 * so recheck rec length */
1930 recsize = le16_to_cpu(ent->lde_reclen);
1932 result = (last != NULL) ? 0 :-EINVAL;
1936 ent = (void *)ent + recsize;
1940 result = iops->next(env, it);
1941 if (result == -ESTALE)
1943 } while (result == 0);
1946 dp->ldp_hash_end = cpu_to_le64(hash);
1948 if (last->lde_hash == dp->ldp_hash_end)
1949 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1950 last->lde_reclen = 0; /* end mark */
1953 /* end of directory */
1954 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1955 else if (result < 0)
1956 CWARN("build page failed: %d!\n", result);
1960 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1961 const struct lu_rdpg *rdpg)
1963 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1967 if (mdd_object_exists(mdd_obj) == 0) {
1968 CERROR("%s: object "DFID" not found: rc = -2\n",
1969 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1973 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1974 rc = mdd_readpage_sanity_check(env, mdd_obj);
1976 GOTO(out_unlock, rc);
1978 if (mdd_is_dead_obj(mdd_obj)) {
1980 struct lu_dirpage *dp;
1983 * According to POSIX, please do not return any entry to client:
1984 * even dot and dotdot should not be returned.
1986 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1987 PFID(mdd_object_fid(mdd_obj)));
1989 if (rdpg->rp_count <= 0)
1990 GOTO(out_unlock, rc = -EFAULT);
1991 LASSERT(rdpg->rp_pages != NULL);
1993 pg = rdpg->rp_pages[0];
1994 dp = (struct lu_dirpage *)kmap(pg);
1995 memset(dp, 0 , sizeof(struct lu_dirpage));
1996 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1997 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1998 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
2000 GOTO(out_unlock, rc = LU_PAGE_SIZE);
2003 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
2004 mdd_dir_page_build, NULL);
2006 struct lu_dirpage *dp;
2008 dp = kmap(rdpg->rp_pages[0]);
2009 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
2012 * No pages were processed, mark this for first page
2015 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
2016 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
2018 kunmap(rdpg->rp_pages[0]);
2021 GOTO(out_unlock, rc);
2023 mdd_read_unlock(env, mdd_obj);
2027 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
2029 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2031 if (mdd_object_exists(mdd_obj) == 0) {
2034 CERROR("%s: object "DFID" not found: rc = %d\n",
2035 mdd_obj_dev_name(mdd_obj),
2036 PFID(mdd_object_fid(mdd_obj)), rc);
2039 return dt_object_sync(env, mdd_object_child(mdd_obj),
2043 static int mdd_object_lock(const struct lu_env *env,
2044 struct md_object *obj,
2045 struct lustre_handle *lh,
2046 struct ldlm_enqueue_info *einfo,
2047 ldlm_policy_data_t *policy)
2049 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2050 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
2054 static int mdd_object_unlock(const struct lu_env *env,
2055 struct md_object *obj,
2056 struct ldlm_enqueue_info *einfo,
2057 ldlm_policy_data_t *policy)
2059 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2060 return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
2063 const struct md_object_operations mdd_obj_ops = {
2064 .moo_permission = mdd_permission,
2065 .moo_attr_get = mdd_attr_get,
2066 .moo_attr_set = mdd_attr_set,
2067 .moo_xattr_get = mdd_xattr_get,
2068 .moo_xattr_set = mdd_xattr_set,
2069 .moo_xattr_list = mdd_xattr_list,
2070 .moo_xattr_del = mdd_xattr_del,
2071 .moo_swap_layouts = mdd_swap_layouts,
2072 .moo_open = mdd_open,
2073 .moo_close = mdd_close,
2074 .moo_readpage = mdd_readpage,
2075 .moo_readlink = mdd_readlink,
2076 .moo_changelog = mdd_changelog,
2077 .moo_capa_get = mdd_capa_get,
2078 .moo_object_sync = mdd_object_sync,
2079 .moo_object_lock = mdd_object_lock,
2080 .moo_object_unlock = mdd_object_unlock,