1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lprocfs_status.h>
54 /* fid_be_cpu(), fid_cpu_to_be(). */
55 #include <lustre_fid.h>
57 #include <linux/ldiskfs_fs.h>
58 #include <lustre_mds.h>
59 #include <lustre/lustre_idl.h>
61 #include "mdd_internal.h"
63 static const struct lu_object_operations mdd_lu_obj_ops;
65 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
66 struct lu_attr *la, struct lustre_capa *capa)
68 LASSERTF(mdd_object_exists(obj), "FID is "DFID"\n",
69 PFID(mdd_object_fid(obj)));
70 return mdo_attr_get(env, obj, la, capa);
73 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
75 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
77 if (flags & LUSTRE_APPEND_FL)
78 obj->mod_flags |= APPEND_OBJ;
80 if (flags & LUSTRE_IMMUTABLE_FL)
81 obj->mod_flags |= IMMUTE_OBJ;
84 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
88 buf = &mdd_env_info(env)->mti_buf;
94 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
95 struct mdd_device *mdd)
97 struct mdd_thread_info *mti = mdd_env_info(env);
100 max_cookie_size = mdd_lov_cookiesize(env, mdd);
101 if (unlikely(mti->mti_max_cookie_size < max_cookie_size)) {
102 if (mti->mti_max_cookie)
103 OBD_FREE(mti->mti_max_cookie, mti->mti_max_cookie_size);
104 mti->mti_max_cookie = NULL;
105 mti->mti_max_cookie_size = 0;
107 if (unlikely(mti->mti_max_cookie == NULL)) {
108 OBD_ALLOC(mti->mti_max_cookie, max_cookie_size);
109 if (likely(mti->mti_max_cookie != NULL))
110 mti->mti_max_cookie_size = max_cookie_size;
112 if (likely(mti->mti_max_cookie != NULL))
113 memset(mti->mti_max_cookie, 0, mti->mti_max_cookie_size);
114 return mti->mti_max_cookie;
117 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
118 struct mdd_device *mdd)
120 struct mdd_thread_info *mti = mdd_env_info(env);
123 max_lmm_size = mdd_lov_mdsize(env, mdd);
124 if (unlikely(mti->mti_max_lmm_size < max_lmm_size)) {
125 if (mti->mti_max_lmm)
126 OBD_FREE(mti->mti_max_lmm, mti->mti_max_lmm_size);
127 mti->mti_max_lmm = NULL;
128 mti->mti_max_lmm_size = 0;
130 if (unlikely(mti->mti_max_lmm == NULL)) {
131 OBD_ALLOC(mti->mti_max_lmm, max_lmm_size);
132 if (unlikely(mti->mti_max_lmm != NULL))
133 mti->mti_max_lmm_size = max_lmm_size;
135 return mti->mti_max_lmm;
138 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
139 const void *area, ssize_t len)
143 buf = &mdd_env_info(env)->mti_buf;
144 buf->lb_buf = (void *)area;
149 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
151 struct mdd_thread_info *info;
153 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
154 LASSERT(info != NULL);
158 struct lu_object *mdd_object_alloc(const struct lu_env *env,
159 const struct lu_object_header *hdr,
162 struct mdd_object *mdd_obj;
164 OBD_ALLOC_PTR(mdd_obj);
165 if (mdd_obj != NULL) {
168 o = mdd2lu_obj(mdd_obj);
169 lu_object_init(o, NULL, d);
170 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
171 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
172 mdd_obj->mod_count = 0;
173 o->lo_ops = &mdd_lu_obj_ops;
180 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
181 const struct lu_object_conf *_)
183 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
184 struct lu_object *below;
185 struct lu_device *under;
188 under = &d->mdd_child->dd_lu_dev;
189 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
190 mdd_pdlock_init(lu2mdd_obj(o));
194 lu_object_add(o, below);
198 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
200 if (lu_object_exists(o))
201 return mdd_get_flags(env, lu2mdd_obj(o));
206 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
208 struct mdd_object *mdd = lu2mdd_obj(o);
214 /* orphan handling is here */
215 static void mdd_object_delete(const struct lu_env *env, struct lu_object *o)
217 struct mdd_object *mdd_obj = lu2mdd_obj(o);
218 struct thandle *handle = NULL;
221 if (lu2mdd_dev(o->lo_dev)->mdd_orphans == NULL)
224 if (mdd_obj->mod_flags & ORPHAN_OBJ) {
225 mdd_txn_param_build(env, lu2mdd_dev(o->lo_dev),
226 MDD_TXN_INDEX_DELETE_OP);
227 handle = mdd_trans_start(env, lu2mdd_dev(o->lo_dev));
229 CERROR("Cannot get thandle\n");
231 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
232 /* let's remove obj from the orphan list */
233 __mdd_orphan_del(env, mdd_obj, handle);
234 mdd_write_unlock(env, mdd_obj);
235 mdd_trans_stop(env, lu2mdd_dev(o->lo_dev),
241 static const struct lu_object_operations mdd_lu_obj_ops = {
242 .loo_object_init = mdd_object_init,
243 .loo_object_start = mdd_object_start,
244 .loo_object_free = mdd_object_free,
245 .loo_object_delete = mdd_object_delete
248 struct mdd_object *mdd_object_find(const struct lu_env *env,
249 struct mdd_device *d,
250 const struct lu_fid *f)
252 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
255 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
257 struct lu_attr *la = &mdd_env_info(env)->mti_la;
261 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
263 mdd_flags_xlate(obj, la->la_flags);
264 if (S_ISDIR(la->la_mode) && la->la_nlink == 1)
265 obj->mod_flags |= MNLINK_OBJ;
270 /* get only inode attributes */
271 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
277 if (ma->ma_valid & MA_INODE)
280 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
281 mdd_object_capa(env, mdd_obj));
283 ma->ma_valid |= MA_INODE;
287 static int mdd_get_default_md(struct mdd_object *mdd_obj,
288 struct lov_mds_md *lmm, int *size)
290 struct lov_desc *ldesc;
291 struct mdd_device *mdd = mdo2mdd(&mdd_obj->mod_obj);
294 ldesc = &mdd->mdd_obd_dev->u.mds.mds_lov_desc;
295 LASSERT(ldesc != NULL);
300 lmm->lmm_magic = LOV_MAGIC_V1;
301 lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
302 lmm->lmm_pattern = ldesc->ld_pattern;
303 lmm->lmm_stripe_size = ldesc->ld_default_stripe_size;
304 lmm->lmm_stripe_count = ldesc->ld_default_stripe_count;
305 *size = sizeof(struct lov_mds_md);
307 RETURN(sizeof(struct lov_mds_md));
310 /* get lov EA only */
311 static int __mdd_lmm_get(const struct lu_env *env,
312 struct mdd_object *mdd_obj, struct md_attr *ma)
317 if (ma->ma_valid & MA_LOV)
320 rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
323 if (rc == 0 && (ma->ma_need & MA_LOV_DEF)) {
324 rc = mdd_get_default_md(mdd_obj, ma->ma_lmm,
329 ma->ma_valid |= MA_LOV;
335 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
341 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
342 rc = __mdd_lmm_get(env, mdd_obj, ma);
343 mdd_read_unlock(env, mdd_obj);
348 static int __mdd_lmv_get(const struct lu_env *env,
349 struct mdd_object *mdd_obj, struct md_attr *ma)
354 if (ma->ma_valid & MA_LMV)
357 rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
360 ma->ma_valid |= MA_LMV;
366 static int mdd_attr_get_internal(const struct lu_env *env,
367 struct mdd_object *mdd_obj,
373 if (ma->ma_need & MA_INODE)
374 rc = mdd_iattr_get(env, mdd_obj, ma);
376 if (rc == 0 && ma->ma_need & MA_LOV) {
377 if (S_ISREG(mdd_object_type(mdd_obj)) ||
378 S_ISDIR(mdd_object_type(mdd_obj)))
379 rc = __mdd_lmm_get(env, mdd_obj, ma);
381 if (rc == 0 && ma->ma_need & MA_LMV) {
382 if (S_ISDIR(mdd_object_type(mdd_obj)))
383 rc = __mdd_lmv_get(env, mdd_obj, ma);
385 #ifdef CONFIG_FS_POSIX_ACL
386 if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
387 if (S_ISDIR(mdd_object_type(mdd_obj)))
388 rc = mdd_def_acl_get(env, mdd_obj, ma);
391 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
396 int mdd_attr_get_internal_locked(const struct lu_env *env,
397 struct mdd_object *mdd_obj, struct md_attr *ma)
400 int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
403 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
404 rc = mdd_attr_get_internal(env, mdd_obj, ma);
406 mdd_read_unlock(env, mdd_obj);
411 * No permission check is needed.
413 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
416 struct mdd_object *mdd_obj = md2mdd_obj(obj);
420 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
425 * No permission check is needed.
427 static int mdd_xattr_get(const struct lu_env *env,
428 struct md_object *obj, struct lu_buf *buf,
431 struct mdd_object *mdd_obj = md2mdd_obj(obj);
436 LASSERT(mdd_object_exists(mdd_obj));
438 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
439 rc = mdo_xattr_get(env, mdd_obj, buf, name,
440 mdd_object_capa(env, mdd_obj));
441 mdd_read_unlock(env, mdd_obj);
447 * Permission check is done when open,
448 * no need check again.
450 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
453 struct mdd_object *mdd_obj = md2mdd_obj(obj);
454 struct dt_object *next;
459 LASSERT(mdd_object_exists(mdd_obj));
461 next = mdd_object_child(mdd_obj);
462 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
463 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
464 mdd_object_capa(env, mdd_obj));
465 mdd_read_unlock(env, mdd_obj);
470 * No permission check is needed.
472 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
475 struct mdd_object *mdd_obj = md2mdd_obj(obj);
480 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
481 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
482 mdd_read_unlock(env, mdd_obj);
487 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
488 struct mdd_object *c, struct md_attr *ma,
489 struct thandle *handle)
491 struct lu_attr *attr = &ma->ma_attr;
492 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
496 if (!mdd_object_exists(c)) {
497 struct dt_object *next = mdd_object_child(c);
500 /* @hint will be initialized by underlying device. */
501 next->do_ops->do_ah_init(env, hint,
502 p ? mdd_object_child(p) : NULL,
503 attr->la_mode & S_IFMT);
504 rc = mdo_create_obj(env, c, attr, hint, handle);
505 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
513 * Make sure the ctime is increased only.
515 static inline int mdd_attr_check(const struct lu_env *env,
516 struct mdd_object *obj,
517 struct lu_attr *attr)
519 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
523 if (attr->la_valid & LA_CTIME) {
524 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
528 if (attr->la_ctime < tmp_la->la_ctime)
529 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
530 else if (attr->la_valid == LA_CTIME &&
531 attr->la_ctime == tmp_la->la_ctime)
532 attr->la_valid &= ~LA_CTIME;
537 int mdd_attr_set_internal(const struct lu_env *env,
538 struct mdd_object *obj,
539 struct lu_attr *attr,
540 struct thandle *handle,
546 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
547 #ifdef CONFIG_FS_POSIX_ACL
548 if (!rc && (attr->la_valid & LA_MODE) && needacl)
549 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
554 int mdd_attr_check_set_internal(const struct lu_env *env,
555 struct mdd_object *obj,
556 struct lu_attr *attr,
557 struct thandle *handle,
563 rc = mdd_attr_check(env, obj, attr);
568 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
572 static int mdd_attr_set_internal_locked(const struct lu_env *env,
573 struct mdd_object *obj,
574 struct lu_attr *attr,
575 struct thandle *handle,
581 needacl = needacl && (attr->la_valid & LA_MODE);
583 mdd_write_lock(env, obj, MOR_TGT_CHILD);
584 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
586 mdd_write_unlock(env, obj);
590 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
591 struct mdd_object *obj,
592 struct lu_attr *attr,
593 struct thandle *handle,
599 needacl = needacl && (attr->la_valid & LA_MODE);
601 mdd_write_lock(env, obj, MOR_TGT_CHILD);
602 rc = mdd_attr_check_set_internal(env, obj, attr, handle, needacl);
604 mdd_write_unlock(env, obj);
608 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
609 const struct lu_buf *buf, const char *name,
610 int fl, struct thandle *handle)
612 struct lustre_capa *capa = mdd_object_capa(env, obj);
616 if (buf->lb_buf && buf->lb_len > 0)
617 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
618 else if (buf->lb_buf == NULL && buf->lb_len == 0)
619 rc = mdo_xattr_del(env, obj, name, handle, capa);
625 * This gives the same functionality as the code between
626 * sys_chmod and inode_setattr
627 * chown_common and inode_setattr
628 * utimes and inode_setattr
629 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
631 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
632 struct lu_attr *la, const struct md_attr *ma)
634 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
635 struct md_ucred *uc = md_ucred(env);
642 /* Do not permit change file type */
643 if (la->la_valid & LA_TYPE)
646 /* They should not be processed by setattr */
647 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
650 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
654 if (la->la_valid == LA_CTIME) {
655 if (!(ma->ma_attr_flags & MDS_PERM_BYPASS))
656 /* This is only for set ctime when rename's source is
658 rc = mdd_may_delete(env, NULL, obj,
659 (struct md_attr *)ma, 1, 0);
660 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
661 la->la_valid &= ~LA_CTIME;
665 if (la->la_valid == LA_ATIME) {
666 /* This is atime only set for read atime update on close. */
667 if (la->la_atime <= tmp_la->la_atime +
668 mdd_obj2mdd_dev(obj)->mdd_atime_diff)
669 la->la_valid &= ~LA_ATIME;
673 /* Check if flags change. */
674 if (la->la_valid & LA_FLAGS) {
675 unsigned int oldflags = 0;
676 unsigned int newflags = la->la_flags &
677 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
679 if ((uc->mu_fsuid != tmp_la->la_uid) &&
680 !mdd_capable(uc, CFS_CAP_FOWNER))
683 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
684 * only be changed by the relevant capability. */
685 if (mdd_is_immutable(obj))
686 oldflags |= LUSTRE_IMMUTABLE_FL;
687 if (mdd_is_append(obj))
688 oldflags |= LUSTRE_APPEND_FL;
689 if ((oldflags ^ newflags) &&
690 !mdd_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
693 if (!S_ISDIR(tmp_la->la_mode))
694 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
697 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
698 (la->la_valid & ~LA_FLAGS) &&
699 !(ma->ma_attr_flags & MDS_PERM_BYPASS))
702 /* Check for setting the obj time. */
703 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
704 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
705 if ((uc->mu_fsuid != tmp_la->la_uid) &&
706 !mdd_capable(uc, CFS_CAP_FOWNER)) {
707 rc = mdd_permission_internal_locked(env, obj, tmp_la,
715 /* Make sure a caller can chmod. */
716 if (la->la_valid & LA_MODE) {
717 /* Bypass la_vaild == LA_MODE,
718 * this is for changing file with SUID or SGID. */
719 if ((la->la_valid & ~LA_MODE) &&
720 !(ma->ma_attr_flags & MDS_PERM_BYPASS) &&
721 (uc->mu_fsuid != tmp_la->la_uid) &&
722 !mdd_capable(uc, CFS_CAP_FOWNER))
725 if (la->la_mode == (umode_t) -1)
726 la->la_mode = tmp_la->la_mode;
728 la->la_mode = (la->la_mode & S_IALLUGO) |
729 (tmp_la->la_mode & ~S_IALLUGO);
731 /* Also check the setgid bit! */
732 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
733 la->la_gid : tmp_la->la_gid) &&
734 !mdd_capable(uc, CFS_CAP_FSETID))
735 la->la_mode &= ~S_ISGID;
737 la->la_mode = tmp_la->la_mode;
740 /* Make sure a caller can chown. */
741 if (la->la_valid & LA_UID) {
742 if (la->la_uid == (uid_t) -1)
743 la->la_uid = tmp_la->la_uid;
744 if (((uc->mu_fsuid != tmp_la->la_uid) ||
745 (la->la_uid != tmp_la->la_uid)) &&
746 !mdd_capable(uc, CFS_CAP_CHOWN))
749 /* If the user or group of a non-directory has been
750 * changed by a non-root user, remove the setuid bit.
751 * 19981026 David C Niemi <niemi@tux.org>
753 * Changed this to apply to all users, including root,
754 * to avoid some races. This is the behavior we had in
755 * 2.0. The check for non-root was definitely wrong
756 * for 2.2 anyway, as it should have been using
757 * CAP_FSETID rather than fsuid -- 19990830 SD. */
758 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
759 !S_ISDIR(tmp_la->la_mode)) {
760 la->la_mode &= ~S_ISUID;
761 la->la_valid |= LA_MODE;
765 /* Make sure caller can chgrp. */
766 if (la->la_valid & LA_GID) {
767 if (la->la_gid == (gid_t) -1)
768 la->la_gid = tmp_la->la_gid;
769 if (((uc->mu_fsuid != tmp_la->la_uid) ||
770 ((la->la_gid != tmp_la->la_gid) &&
771 !lustre_in_group_p(uc, la->la_gid))) &&
772 !mdd_capable(uc, CFS_CAP_CHOWN))
775 /* Likewise, if the user or group of a non-directory
776 * has been changed by a non-root user, remove the
777 * setgid bit UNLESS there is no group execute bit
778 * (this would be a file marked for mandatory
779 * locking). 19981026 David C Niemi <niemi@tux.org>
781 * Removed the fsuid check (see the comment above) --
783 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
784 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
785 la->la_mode &= ~S_ISGID;
786 la->la_valid |= LA_MODE;
790 /* For both Size-on-MDS case and truncate case,
791 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
792 * We distinguish them by "ma->ma_attr_flags & MDS_SOM".
793 * For SOM case, it is true, the MAY_WRITE perm has been checked
794 * when open, no need check again. For truncate case, it is false,
795 * the MAY_WRITE perm should be checked here. */
796 if (ma->ma_attr_flags & MDS_SOM) {
797 /* For the "Size-on-MDS" setattr update, merge coming
798 * attributes with the set in the inode. BUG 10641 */
799 if ((la->la_valid & LA_ATIME) &&
800 (la->la_atime <= tmp_la->la_atime))
801 la->la_valid &= ~LA_ATIME;
803 /* OST attributes do not have a priority over MDS attributes,
804 * so drop times if ctime is equal. */
805 if ((la->la_valid & LA_CTIME) &&
806 (la->la_ctime <= tmp_la->la_ctime))
807 la->la_valid &= ~(LA_MTIME | LA_CTIME);
809 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
810 if (!((ma->ma_attr_flags & MDS_OPEN_OWNEROVERRIDE) &&
811 (uc->mu_fsuid == tmp_la->la_uid)) &&
812 !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
813 rc = mdd_permission_internal_locked(env, obj,
820 if (la->la_valid & LA_CTIME) {
821 /* The pure setattr, it has the priority over what is
822 * already set, do not drop it if ctime is equal. */
823 if (la->la_ctime < tmp_la->la_ctime)
824 la->la_valid &= ~(LA_ATIME | LA_MTIME |
832 /* set attr and LOV EA at once, return updated attr */
833 static int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
834 const struct md_attr *ma)
836 struct mdd_object *mdd_obj = md2mdd_obj(obj);
837 struct mdd_device *mdd = mdo2mdd(obj);
838 struct thandle *handle;
839 struct lov_mds_md *lmm = NULL;
840 struct llog_cookie *logcookies = NULL;
841 int rc, lmm_size = 0, cookie_size = 0;
842 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
845 mdd_txn_param_build(env, mdd, MDD_TXN_ATTR_SET_OP);
846 handle = mdd_trans_start(env, mdd);
848 RETURN(PTR_ERR(handle));
849 /*TODO: add lock here*/
850 /* start a log jounal handle if needed */
851 if (S_ISREG(mdd_object_type(mdd_obj)) &&
852 ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
853 lmm_size = mdd_lov_mdsize(env, mdd);
854 lmm = mdd_max_lmm_get(env, mdd);
856 GOTO(cleanup, rc = -ENOMEM);
858 rc = mdd_get_md_locked(env, mdd_obj, lmm, &lmm_size,
865 if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
866 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
867 ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
869 *la_copy = ma->ma_attr;
870 rc = mdd_fix_attr(env, mdd_obj, la_copy, ma);
874 if (la_copy->la_valid & LA_FLAGS) {
875 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
878 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
879 } else if (la_copy->la_valid) { /* setattr */
880 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
882 /* journal chown/chgrp in llog, just like unlink */
883 if (rc == 0 && lmm_size){
884 cookie_size = mdd_lov_cookiesize(env, mdd);
885 logcookies = mdd_max_cookie_get(env, mdd);
886 if (logcookies == NULL)
887 GOTO(cleanup, rc = -ENOMEM);
889 if (mdd_setattr_log(env, mdd, ma, lmm, lmm_size,
890 logcookies, cookie_size) <= 0)
895 if (rc == 0 && ma->ma_valid & MA_LOV) {
898 mode = mdd_object_type(mdd_obj);
899 if (S_ISREG(mode) || S_ISDIR(mode)) {
900 rc = mdd_lsm_sanity_check(env, mdd_obj);
904 rc = mdd_lov_set_md(env, NULL, mdd_obj, ma->ma_lmm,
905 ma->ma_lmm_size, handle, 1);
910 mdd_trans_stop(env, mdd, rc, handle);
911 if (rc == 0 && (lmm != NULL && lmm_size > 0 )) {
912 /*set obd attr, if needed*/
913 rc = mdd_lov_setattr_async(env, mdd_obj, lmm, lmm_size,
919 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
920 const struct lu_buf *buf, const char *name, int fl,
921 struct thandle *handle)
926 mdd_write_lock(env, obj, MOR_TGT_CHILD);
927 rc = __mdd_xattr_set(env, obj, buf, name, fl, handle);
928 mdd_write_unlock(env, obj);
933 static int mdd_xattr_sanity_check(const struct lu_env *env,
934 struct mdd_object *obj)
936 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
937 struct md_ucred *uc = md_ucred(env);
941 if (mdd_is_immutable(obj) || mdd_is_append(obj))
944 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
948 if ((uc->mu_fsuid != tmp_la->la_uid) &&
949 !mdd_capable(uc, CFS_CAP_FOWNER))
956 * The caller should guarantee to update the object ctime
957 * after xattr_set if needed.
959 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
960 const struct lu_buf *buf, const char *name,
963 struct mdd_object *mdd_obj = md2mdd_obj(obj);
964 struct mdd_device *mdd = mdo2mdd(obj);
965 struct thandle *handle;
969 rc = mdd_xattr_sanity_check(env, mdd_obj);
973 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
974 handle = mdd_trans_start(env, mdd);
976 RETURN(PTR_ERR(handle));
978 rc = mdd_xattr_set_txn(env, mdd_obj, buf, name, fl, handle);
979 mdd_trans_stop(env, mdd, rc, handle);
985 * The caller should guarantee to update the object ctime
986 * after xattr_set if needed.
988 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
991 struct mdd_object *mdd_obj = md2mdd_obj(obj);
992 struct mdd_device *mdd = mdo2mdd(obj);
993 struct thandle *handle;
997 rc = mdd_xattr_sanity_check(env, mdd_obj);
1001 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1002 handle = mdd_trans_start(env, mdd);
1004 RETURN(PTR_ERR(handle));
1006 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1007 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1008 mdd_object_capa(env, mdd_obj));
1009 mdd_write_unlock(env, mdd_obj);
1010 mdd_trans_stop(env, mdd, rc, handle);
1015 /* partial unlink */
1016 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
1019 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1020 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1021 struct mdd_device *mdd = mdo2mdd(obj);
1022 struct thandle *handle;
1027 * Check -ENOENT early here because we need to get object type
1028 * to calculate credits before transaction start
1030 if (!mdd_object_exists(mdd_obj))
1033 LASSERT(mdd_object_exists(mdd_obj) > 0);
1035 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1039 handle = mdd_trans_start(env, mdd);
1043 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1045 rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
1049 __mdd_ref_del(env, mdd_obj, handle, 0);
1051 if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
1053 __mdd_ref_del(env, mdd_obj, handle, 1);
1056 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1057 la_copy->la_ctime = ma->ma_attr.la_ctime;
1059 la_copy->la_valid = LA_CTIME;
1060 rc = mdd_attr_check_set_internal(env, mdd_obj, la_copy, handle, 0);
1064 rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
1068 mdd_write_unlock(env, mdd_obj);
1069 mdd_trans_stop(env, mdd, rc, handle);
1073 /* partial operation */
1074 static int mdd_oc_sanity_check(const struct lu_env *env,
1075 struct mdd_object *obj,
1081 switch (ma->ma_attr.la_mode & S_IFMT) {
1098 static int mdd_object_create(const struct lu_env *env,
1099 struct md_object *obj,
1100 const struct md_op_spec *spec,
1104 struct mdd_device *mdd = mdo2mdd(obj);
1105 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1106 const struct lu_fid *pfid = spec->u.sp_pfid;
1107 struct thandle *handle;
1111 mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1112 handle = mdd_trans_start(env, mdd);
1114 RETURN(PTR_ERR(handle));
1116 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1117 rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1121 rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1125 if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1126 /* If creating the slave object, set slave EA here. */
1127 int lmv_size = spec->u.sp_ea.eadatalen;
1128 struct lmv_stripe_md *lmv;
1130 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1131 LASSERT(lmv != NULL && lmv_size > 0);
1133 rc = __mdd_xattr_set(env, mdd_obj,
1134 mdd_buf_get_const(env, lmv, lmv_size),
1135 MDS_LMV_MD_NAME, 0, handle);
1139 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr,
1142 #ifdef CONFIG_FS_POSIX_ACL
1143 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1144 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1146 buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1147 buf->lb_len = spec->u.sp_ea.eadatalen;
1148 if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1149 rc = __mdd_acl_init(env, mdd_obj, buf,
1150 &ma->ma_attr.la_mode,
1155 ma->ma_attr.la_valid |= LA_MODE;
1158 pfid = spec->u.sp_ea.fid;
1161 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1166 rc = mdd_attr_get_internal(env, mdd_obj, ma);
1167 mdd_write_unlock(env, mdd_obj);
1169 mdd_trans_stop(env, mdd, rc, handle);
1174 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1175 const struct md_attr *ma)
1177 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1178 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1179 struct mdd_device *mdd = mdo2mdd(obj);
1180 struct thandle *handle;
1184 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1185 handle = mdd_trans_start(env, mdd);
1189 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1190 rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1192 __mdd_ref_add(env, mdd_obj, handle);
1193 mdd_write_unlock(env, mdd_obj);
1195 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1196 la_copy->la_ctime = ma->ma_attr.la_ctime;
1198 la_copy->la_valid = LA_CTIME;
1199 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1202 mdd_trans_stop(env, mdd, 0, handle);
1208 * do NOT or the MAY_*'s, you'll get the weakest
1210 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1214 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1215 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1216 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1217 * owner can write to a file even if it is marked readonly to hide
1218 * its brokenness. (bug 5781) */
1219 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1220 struct md_ucred *uc = md_ucred(env);
1222 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1223 (la->la_uid == uc->mu_fsuid))
1227 if (flags & FMODE_READ)
1229 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1231 if (flags & MDS_FMODE_EXEC)
1236 static int mdd_open_sanity_check(const struct lu_env *env,
1237 struct mdd_object *obj, int flag)
1239 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1244 if (mdd_is_dead_obj(obj))
1247 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1251 if (S_ISLNK(tmp_la->la_mode))
1254 mode = accmode(env, tmp_la, flag);
1256 if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1259 if (!(flag & MDS_OPEN_CREATED)) {
1260 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1265 if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1266 S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1267 flag &= ~MDS_OPEN_TRUNC;
1269 /* For writing append-only file must open it with append mode. */
1270 if (mdd_is_append(obj)) {
1271 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1273 if (flag & MDS_OPEN_TRUNC)
1279 * Now, flag -- O_NOATIME does not be packed by client.
1281 if (flag & O_NOATIME) {
1282 struct md_ucred *uc = md_ucred(env);
1284 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1285 (uc->mu_valid == UCRED_NEW)) &&
1286 (uc->mu_fsuid != tmp_la->la_uid) &&
1287 !mdd_capable(uc, CFS_CAP_FOWNER))
1295 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1298 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1301 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1303 rc = mdd_open_sanity_check(env, mdd_obj, flags);
1305 mdd_obj->mod_count++;
1307 mdd_write_unlock(env, mdd_obj);
1311 /* return md_attr back,
1312 * if it is last unlink then return lov ea + llog cookie*/
1313 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1319 if (S_ISREG(mdd_object_type(obj))) {
1320 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1321 * Caller must be ready for that. */
1322 rc = __mdd_lmm_get(env, obj, ma);
1323 if ((ma->ma_valid & MA_LOV))
1324 rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1331 * No permission check is needed.
1333 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1337 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1338 struct thandle *handle;
1341 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1344 handle = mdd_trans_start(env, mdo2mdd(obj));
1346 RETURN(PTR_ERR(handle));
1348 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1349 /* release open count */
1350 mdd_obj->mod_count --;
1352 rc = mdd_iattr_get(env, mdd_obj, ma);
1353 if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1354 rc = mdd_object_kill(env, mdd_obj, ma);
1356 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1358 mdd_write_unlock(env, mdd_obj);
1359 mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1364 * Permission check is done when open,
1365 * no need check again.
1367 static int mdd_readpage_sanity_check(const struct lu_env *env,
1368 struct mdd_object *obj)
1370 struct dt_object *next = mdd_object_child(obj);
1374 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1382 static int mdd_dir_page_build(const struct lu_env *env, int first,
1383 void *area, int nob, const struct dt_it_ops *iops,
1384 struct dt_it *it, __u64 *start, __u64 *end,
1385 struct lu_dirent **last)
1387 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
1388 struct mdd_thread_info *info = mdd_env_info(env);
1389 struct lu_fid_pack *pack = &info->mti_pack;
1391 struct lu_dirent *ent;
1394 memset(area, 0, sizeof (struct lu_dirpage));
1395 area += sizeof (struct lu_dirpage);
1396 nob -= sizeof (struct lu_dirpage);
1399 LASSERT(nob > sizeof *ent);
1409 name = (char *)iops->key(env, it);
1410 len = iops->key_size(env, it);
1412 pack = (struct lu_fid_pack *)iops->rec(env, it);
1413 result = fid_unpack(pack, fid);
1417 recsize = (sizeof(*ent) + len + 7) & ~7;
1418 hash = iops->store(env, it);
1421 CDEBUG(D_INFO, "%p %p %d "DFID": "LPU64" (%d) \"%*.*s\"\n",
1422 name, ent, nob, PFID(fid), hash, len, len, len, name);
1424 if (nob >= recsize) {
1425 ent->lde_fid = *fid;
1426 fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1427 ent->lde_hash = hash;
1428 ent->lde_namelen = cpu_to_le16(len);
1429 ent->lde_reclen = cpu_to_le16(recsize);
1430 memcpy(ent->lde_name, name, len);
1431 if (first && ent == area)
1434 ent = (void *)ent + recsize;
1436 result = iops->next(env, it);
1439 * record doesn't fit into page, enlarge previous one.
1441 LASSERT(*last != NULL);
1442 (*last)->lde_reclen =
1443 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1447 } while (result == 0);
1452 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1453 const struct lu_rdpg *rdpg)
1456 struct dt_object *next = mdd_object_child(obj);
1457 const struct dt_it_ops *iops;
1459 struct lu_dirent *last = NULL;
1466 LASSERT(rdpg->rp_pages != NULL);
1467 LASSERT(next->do_index_ops != NULL);
1469 if (rdpg->rp_count <= 0)
1473 * iterate through directory and fill pages from @rdpg
1475 iops = &next->do_index_ops->dio_it;
1476 it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1480 rc = iops->load(env, it, rdpg->rp_hash);
1484 * Iterator didn't find record with exactly the key requested.
1486 * It is currently either
1488 * - positioned above record with key less than
1489 * requested---skip it.
1491 * - or not positioned at all (is in IAM_IT_SKEWED
1492 * state)---position it on the next item.
1494 rc = iops->next(env, it);
1499 * At this point and across for-loop:
1501 * rc == 0 -> ok, proceed.
1502 * rc > 0 -> end of directory.
1505 for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1506 i++, nob -= CFS_PAGE_SIZE) {
1507 LASSERT(i < rdpg->rp_npages);
1508 pg = rdpg->rp_pages[i];
1509 rc = mdd_dir_page_build(env, !i, cfs_kmap(pg),
1510 min_t(int, nob, CFS_PAGE_SIZE), iops,
1511 it, &hash_start, &hash_end, &last);
1512 if (rc != 0 || i == rdpg->rp_npages - 1)
1513 last->lde_reclen = 0;
1520 hash_end = DIR_END_OFF;
1524 struct lu_dirpage *dp;
1526 dp = cfs_kmap(rdpg->rp_pages[0]);
1527 dp->ldp_hash_start = rdpg->rp_hash;
1528 dp->ldp_hash_end = hash_end;
1531 * No pages were processed, mark this.
1533 dp->ldp_flags |= LDF_EMPTY;
1534 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1535 cfs_kunmap(rdpg->rp_pages[0]);
1538 iops->fini(env, it);
1543 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1544 const struct lu_rdpg *rdpg)
1546 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1550 LASSERT(mdd_object_exists(mdd_obj));
1552 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1553 rc = mdd_readpage_sanity_check(env, mdd_obj);
1555 GOTO(out_unlock, rc);
1557 if (mdd_is_dead_obj(mdd_obj)) {
1559 struct lu_dirpage *dp;
1562 * According to POSIX, please do not return any entry to client:
1563 * even dot and dotdot should not be returned.
1565 CWARN("readdir from dead object: "DFID"\n",
1566 PFID(mdd_object_fid(mdd_obj)));
1568 if (rdpg->rp_count <= 0)
1569 GOTO(out_unlock, rc = -EFAULT);
1570 LASSERT(rdpg->rp_pages != NULL);
1572 pg = rdpg->rp_pages[0];
1573 dp = (struct lu_dirpage*)cfs_kmap(pg);
1574 memset(dp, 0 , sizeof(struct lu_dirpage));
1575 dp->ldp_hash_start = rdpg->rp_hash;
1576 dp->ldp_hash_end = DIR_END_OFF;
1577 dp->ldp_flags |= LDF_EMPTY;
1578 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1580 GOTO(out_unlock, rc = 0);
1583 rc = __mdd_readpage(env, mdd_obj, rdpg);
1587 mdd_read_unlock(env, mdd_obj);
1591 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
1593 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1594 struct dt_object *next;
1596 LASSERT(mdd_object_exists(mdd_obj));
1597 next = mdd_object_child(mdd_obj);
1598 return next->do_ops->do_object_sync(env, next);
1601 const struct md_object_operations mdd_obj_ops = {
1602 .moo_permission = mdd_permission,
1603 .moo_attr_get = mdd_attr_get,
1604 .moo_attr_set = mdd_attr_set,
1605 .moo_xattr_get = mdd_xattr_get,
1606 .moo_xattr_set = mdd_xattr_set,
1607 .moo_xattr_list = mdd_xattr_list,
1608 .moo_xattr_del = mdd_xattr_del,
1609 .moo_object_create = mdd_object_create,
1610 .moo_ref_add = mdd_ref_add,
1611 .moo_ref_del = mdd_ref_del,
1612 .moo_open = mdd_open,
1613 .moo_close = mdd_close,
1614 .moo_readpage = mdd_readpage,
1615 .moo_readlink = mdd_readlink,
1616 .moo_capa_get = mdd_capa_get,
1617 .moo_object_sync = mdd_object_sync,