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 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 (unlikely(mti->mti_max_cookie != NULL))
110 mti->mti_max_cookie_size = max_cookie_size;
112 return mti->mti_max_cookie;
115 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
116 struct mdd_device *mdd)
118 struct mdd_thread_info *mti = mdd_env_info(env);
121 max_lmm_size = mdd_lov_mdsize(env, mdd);
122 if (unlikely(mti->mti_max_lmm_size < max_lmm_size)) {
123 if (mti->mti_max_lmm)
124 OBD_FREE(mti->mti_max_lmm, mti->mti_max_lmm_size);
125 mti->mti_max_lmm = NULL;
126 mti->mti_max_lmm_size = 0;
128 if (unlikely(mti->mti_max_lmm == NULL)) {
129 OBD_ALLOC(mti->mti_max_lmm, max_lmm_size);
130 if (unlikely(mti->mti_max_lmm != NULL))
131 mti->mti_max_lmm_size = max_lmm_size;
133 return mti->mti_max_lmm;
136 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
137 const void *area, ssize_t len)
141 buf = &mdd_env_info(env)->mti_buf;
142 buf->lb_buf = (void *)area;
147 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
149 struct mdd_thread_info *info;
151 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
152 LASSERT(info != NULL);
156 struct lu_object *mdd_object_alloc(const struct lu_env *env,
157 const struct lu_object_header *hdr,
160 struct mdd_object *mdd_obj;
162 OBD_ALLOC_PTR(mdd_obj);
163 if (mdd_obj != NULL) {
166 o = mdd2lu_obj(mdd_obj);
167 lu_object_init(o, NULL, d);
168 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
169 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
170 mdd_obj->mod_count = 0;
171 o->lo_ops = &mdd_lu_obj_ops;
178 static int mdd_object_init(const struct lu_env *env, struct lu_object *o)
180 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
181 struct lu_object *below;
182 struct lu_device *under;
185 under = &d->mdd_child->dd_lu_dev;
186 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
187 mdd_pdlock_init(lu2mdd_obj(o));
191 lu_object_add(o, below);
195 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
197 if (lu_object_exists(o))
198 return mdd_get_flags(env, lu2mdd_obj(o));
203 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
205 struct mdd_object *mdd = lu2mdd_obj(o);
211 static int mdd_object_print(const struct lu_env *env, void *cookie,
212 lu_printer_t p, const struct lu_object *o)
214 return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p", o);
217 /* orphan handling is here */
218 static void mdd_object_delete(const struct lu_env *env,
221 struct mdd_object *mdd_obj = lu2mdd_obj(o);
222 struct thandle *handle = NULL;
225 if (lu2mdd_dev(o->lo_dev)->mdd_orphans == NULL)
228 if (mdd_obj->mod_flags & ORPHAN_OBJ) {
229 mdd_txn_param_build(env, lu2mdd_dev(o->lo_dev),
230 MDD_TXN_INDEX_DELETE_OP);
231 handle = mdd_trans_start(env, lu2mdd_dev(o->lo_dev));
233 CERROR("Cannot get thandle\n");
235 mdd_write_lock(env, mdd_obj);
236 /* let's remove obj from the orphan list */
237 __mdd_orphan_del(env, mdd_obj, handle);
238 mdd_write_unlock(env, mdd_obj);
239 mdd_trans_stop(env, lu2mdd_dev(o->lo_dev),
245 static struct lu_object_operations mdd_lu_obj_ops = {
246 .loo_object_init = mdd_object_init,
247 .loo_object_start = mdd_object_start,
248 .loo_object_free = mdd_object_free,
249 .loo_object_print = mdd_object_print,
250 .loo_object_delete = mdd_object_delete
253 struct mdd_object *mdd_object_find(const struct lu_env *env,
254 struct mdd_device *d,
255 const struct lu_fid *f)
257 struct lu_object *o, *lo;
258 struct mdd_object *m;
261 o = lu_object_find(env, mdd2lu_dev(d)->ld_site, f);
263 m = (struct mdd_object *)o;
265 lo = lu_object_locate(o->lo_header, mdd2lu_dev(d)->ld_type);
266 /* remote object can't be located and should be put then */
268 lu_object_put(env, o);
274 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
276 struct lu_attr *la = &mdd_env_info(env)->mti_la;
280 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
282 mdd_flags_xlate(obj, la->la_flags);
283 if (S_ISDIR(la->la_mode) && la->la_nlink == 1)
284 obj->mod_flags |= MNLINK_OBJ;
289 /* get only inode attributes */
290 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
296 if (ma->ma_valid & MA_INODE)
299 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
300 mdd_object_capa(env, mdd_obj));
302 ma->ma_valid |= MA_INODE;
306 static int mdd_get_default_md(struct mdd_object *mdd_obj,
307 struct lov_mds_md *lmm, int *size)
309 struct lov_desc *ldesc;
310 struct mdd_device *mdd = mdo2mdd(&mdd_obj->mod_obj);
313 ldesc = &mdd->mdd_obd_dev->u.mds.mds_lov_desc;
314 LASSERT(ldesc != NULL);
319 lmm->lmm_magic = LOV_MAGIC_V1;
320 lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
321 lmm->lmm_pattern = ldesc->ld_pattern;
322 lmm->lmm_stripe_size = ldesc->ld_default_stripe_size;
323 lmm->lmm_stripe_count = ldesc->ld_default_stripe_count;
324 *size = sizeof(struct lov_mds_md);
326 RETURN(sizeof(struct lov_mds_md));
329 /* get lov EA only */
330 static int __mdd_lmm_get(const struct lu_env *env,
331 struct mdd_object *mdd_obj, struct md_attr *ma)
336 if (ma->ma_valid & MA_LOV)
339 rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
342 if (rc == 0 && (ma->ma_need & MA_LOV_DEF)) {
343 rc = mdd_get_default_md(mdd_obj, ma->ma_lmm,
348 ma->ma_valid |= MA_LOV;
354 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
360 mdd_read_lock(env, mdd_obj);
361 rc = __mdd_lmm_get(env, mdd_obj, ma);
362 mdd_read_unlock(env, mdd_obj);
367 static int __mdd_lmv_get(const struct lu_env *env,
368 struct mdd_object *mdd_obj, struct md_attr *ma)
373 if (ma->ma_valid & MA_LMV)
376 rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
379 ma->ma_valid |= MA_LMV;
385 static int mdd_attr_get_internal(const struct lu_env *env,
386 struct mdd_object *mdd_obj,
392 if (ma->ma_need & MA_INODE)
393 rc = mdd_iattr_get(env, mdd_obj, ma);
395 if (rc == 0 && ma->ma_need & MA_LOV) {
396 if (S_ISREG(mdd_object_type(mdd_obj)) ||
397 S_ISDIR(mdd_object_type(mdd_obj)))
398 rc = __mdd_lmm_get(env, mdd_obj, ma);
400 if (rc == 0 && ma->ma_need & MA_LMV) {
401 if (S_ISDIR(mdd_object_type(mdd_obj)))
402 rc = __mdd_lmv_get(env, mdd_obj, ma);
404 #ifdef CONFIG_FS_POSIX_ACL
405 if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
406 if (S_ISDIR(mdd_object_type(mdd_obj)))
407 rc = mdd_def_acl_get(env, mdd_obj, ma);
410 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
415 int mdd_attr_get_internal_locked(const struct lu_env *env,
416 struct mdd_object *mdd_obj, struct md_attr *ma)
419 int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
422 mdd_read_lock(env, mdd_obj);
423 rc = mdd_attr_get_internal(env, mdd_obj, ma);
425 mdd_read_unlock(env, mdd_obj);
430 * No permission check is needed.
432 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
435 struct mdd_object *mdd_obj = md2mdd_obj(obj);
439 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
444 * No permission check is needed.
446 static int mdd_xattr_get(const struct lu_env *env,
447 struct md_object *obj, struct lu_buf *buf,
450 struct mdd_object *mdd_obj = md2mdd_obj(obj);
455 LASSERT(mdd_object_exists(mdd_obj));
457 mdd_read_lock(env, mdd_obj);
458 rc = mdo_xattr_get(env, mdd_obj, buf, name,
459 mdd_object_capa(env, mdd_obj));
460 mdd_read_unlock(env, mdd_obj);
466 * Permission check is done when open,
467 * no need check again.
469 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
472 struct mdd_object *mdd_obj = md2mdd_obj(obj);
473 struct dt_object *next;
478 LASSERT(mdd_object_exists(mdd_obj));
480 next = mdd_object_child(mdd_obj);
481 mdd_read_lock(env, mdd_obj);
482 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
483 mdd_object_capa(env, mdd_obj));
484 mdd_read_unlock(env, mdd_obj);
489 * No permission check is needed.
491 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
494 struct mdd_object *mdd_obj = md2mdd_obj(obj);
499 mdd_read_lock(env, mdd_obj);
500 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
501 mdd_read_unlock(env, mdd_obj);
506 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
507 struct mdd_object *c, struct md_attr *ma,
508 struct thandle *handle)
510 struct lu_attr *attr = &ma->ma_attr;
511 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
515 if (!mdd_object_exists(c)) {
516 struct dt_object *next = mdd_object_child(c);
519 /* @hint will be initialized by underlying device. */
520 next->do_ops->do_ah_init(env, hint,
521 p ? mdd_object_child(p) : NULL,
522 attr->la_mode & S_IFMT);
523 rc = mdo_create_obj(env, c, attr, hint, handle);
524 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
532 * Make sure the ctime is increased only.
534 static inline int mdd_attr_check(const struct lu_env *env,
535 struct mdd_object *obj,
536 struct lu_attr *attr)
538 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
542 if (attr->la_valid & LA_CTIME) {
543 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
547 if (attr->la_ctime < tmp_la->la_ctime)
548 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
549 else if (attr->la_valid == LA_CTIME &&
550 attr->la_ctime == tmp_la->la_ctime)
551 attr->la_valid &= ~LA_CTIME;
556 int mdd_attr_set_internal(const struct lu_env *env,
557 struct mdd_object *obj,
558 struct lu_attr *attr,
559 struct thandle *handle,
565 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
566 #ifdef CONFIG_FS_POSIX_ACL
567 if (!rc && (attr->la_valid & LA_MODE) && needacl)
568 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
573 int mdd_attr_check_set_internal(const struct lu_env *env,
574 struct mdd_object *obj,
575 struct lu_attr *attr,
576 struct thandle *handle,
582 rc = mdd_attr_check(env, obj, attr);
587 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
591 static int mdd_attr_set_internal_locked(const struct lu_env *env,
592 struct mdd_object *obj,
593 struct lu_attr *attr,
594 struct thandle *handle,
600 needacl = needacl && (attr->la_valid & LA_MODE);
602 mdd_write_lock(env, obj);
603 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
605 mdd_write_unlock(env, obj);
609 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
610 struct mdd_object *obj,
611 struct lu_attr *attr,
612 struct thandle *handle,
618 needacl = needacl && (attr->la_valid & LA_MODE);
620 mdd_write_lock(env, obj);
621 rc = mdd_attr_check_set_internal(env, obj, attr, handle, needacl);
623 mdd_write_unlock(env, obj);
627 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
628 const struct lu_buf *buf, const char *name,
629 int fl, struct thandle *handle)
631 struct lustre_capa *capa = mdd_object_capa(env, obj);
635 if (buf->lb_buf && buf->lb_len > 0)
636 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
637 else if (buf->lb_buf == NULL && buf->lb_len == 0)
638 rc = mdo_xattr_del(env, obj, name, handle, capa);
644 * This gives the same functionality as the code between
645 * sys_chmod and inode_setattr
646 * chown_common and inode_setattr
647 * utimes and inode_setattr
648 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
650 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
651 struct lu_attr *la, const struct md_attr *ma)
653 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
654 struct md_ucred *uc = md_ucred(env);
661 /* Do not permit change file type */
662 if (la->la_valid & LA_TYPE)
665 /* They should not be processed by setattr */
666 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
669 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
673 if (la->la_valid == LA_CTIME) {
674 if (!(ma->ma_attr_flags & MDS_PERM_BYPASS))
675 /* This is only for set ctime when rename's source is
677 rc = mdd_may_delete(env, NULL, obj,
678 (struct md_attr *)ma, 1, 0);
679 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
680 la->la_valid &= ~LA_CTIME;
684 if (la->la_valid == LA_ATIME) {
685 /* This is atime only set for read atime update on close. */
686 if (la->la_atime <= tmp_la->la_atime +
687 mdd_obj2mdd_dev(obj)->mdd_atime_diff)
688 la->la_valid &= ~LA_ATIME;
692 /* Check if flags change. */
693 if (la->la_valid & LA_FLAGS) {
694 unsigned int oldflags = 0;
695 unsigned int newflags = la->la_flags &
696 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
698 if ((uc->mu_fsuid != tmp_la->la_uid) &&
699 !mdd_capable(uc, CFS_CAP_FOWNER))
702 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
703 * only be changed by the relevant capability. */
704 if (mdd_is_immutable(obj))
705 oldflags |= LUSTRE_IMMUTABLE_FL;
706 if (mdd_is_append(obj))
707 oldflags |= LUSTRE_APPEND_FL;
708 if ((oldflags ^ newflags) &&
709 !mdd_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
712 if (!S_ISDIR(tmp_la->la_mode))
713 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
716 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
717 (la->la_valid & ~LA_FLAGS) &&
718 !(ma->ma_attr_flags & MDS_PERM_BYPASS))
721 /* Check for setting the obj time. */
722 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
723 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
724 if ((uc->mu_fsuid != tmp_la->la_uid) &&
725 !mdd_capable(uc, CFS_CAP_FOWNER)) {
726 rc = mdd_permission_internal_locked(env, obj, tmp_la,
733 /* Make sure a caller can chmod. */
734 if (la->la_valid & LA_MODE) {
735 /* Bypass la_vaild == LA_MODE,
736 * this is for changing file with SUID or SGID. */
737 if ((la->la_valid & ~LA_MODE) &&
738 !(ma->ma_attr_flags & MDS_PERM_BYPASS) &&
739 (uc->mu_fsuid != tmp_la->la_uid) &&
740 !mdd_capable(uc, CFS_CAP_FOWNER))
743 if (la->la_mode == (umode_t) -1)
744 la->la_mode = tmp_la->la_mode;
746 la->la_mode = (la->la_mode & S_IALLUGO) |
747 (tmp_la->la_mode & ~S_IALLUGO);
749 /* Also check the setgid bit! */
750 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
751 la->la_gid : tmp_la->la_gid) &&
752 !mdd_capable(uc, CFS_CAP_FSETID))
753 la->la_mode &= ~S_ISGID;
755 la->la_mode = tmp_la->la_mode;
758 /* Make sure a caller can chown. */
759 if (la->la_valid & LA_UID) {
760 if (la->la_uid == (uid_t) -1)
761 la->la_uid = tmp_la->la_uid;
762 if (((uc->mu_fsuid != tmp_la->la_uid) ||
763 (la->la_uid != tmp_la->la_uid)) &&
764 !mdd_capable(uc, CFS_CAP_CHOWN))
767 /* If the user or group of a non-directory has been
768 * changed by a non-root user, remove the setuid bit.
769 * 19981026 David C Niemi <niemi@tux.org>
771 * Changed this to apply to all users, including root,
772 * to avoid some races. This is the behavior we had in
773 * 2.0. The check for non-root was definitely wrong
774 * for 2.2 anyway, as it should have been using
775 * CAP_FSETID rather than fsuid -- 19990830 SD. */
776 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
777 !S_ISDIR(tmp_la->la_mode)) {
778 la->la_mode &= ~S_ISUID;
779 la->la_valid |= LA_MODE;
783 /* Make sure caller can chgrp. */
784 if (la->la_valid & LA_GID) {
785 if (la->la_gid == (gid_t) -1)
786 la->la_gid = tmp_la->la_gid;
787 if (((uc->mu_fsuid != tmp_la->la_uid) ||
788 ((la->la_gid != tmp_la->la_gid) &&
789 !lustre_in_group_p(uc, la->la_gid))) &&
790 !mdd_capable(uc, CFS_CAP_CHOWN))
793 /* Likewise, if the user or group of a non-directory
794 * has been changed by a non-root user, remove the
795 * setgid bit UNLESS there is no group execute bit
796 * (this would be a file marked for mandatory
797 * locking). 19981026 David C Niemi <niemi@tux.org>
799 * Removed the fsuid check (see the comment above) --
801 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
802 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
803 la->la_mode &= ~S_ISGID;
804 la->la_valid |= LA_MODE;
808 /* For both Size-on-MDS case and truncate case,
809 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
810 * We distinguish them by "ma->ma_attr_flags & MDS_SOM".
811 * For SOM case, it is true, the MAY_WRITE perm has been checked
812 * when open, no need check again. For truncate case, it is false,
813 * the MAY_WRITE perm should be checked here. */
814 if (ma->ma_attr_flags & MDS_SOM) {
815 /* For the "Size-on-MDS" setattr update, merge coming
816 * attributes with the set in the inode. BUG 10641 */
817 if ((la->la_valid & LA_ATIME) &&
818 (la->la_atime <= tmp_la->la_atime))
819 la->la_valid &= ~LA_ATIME;
821 /* OST attributes do not have a priority over MDS attributes,
822 * so drop times if ctime is equal. */
823 if ((la->la_valid & LA_CTIME) &&
824 (la->la_ctime <= tmp_la->la_ctime))
825 la->la_valid &= ~(LA_MTIME | LA_CTIME);
827 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
828 if (!((ma->ma_attr_flags & MDS_OPEN_OWNEROVERRIDE) &&
829 (uc->mu_fsuid == tmp_la->la_uid)) &&
830 !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
831 rc = mdd_permission_internal_locked(env, obj,
837 if (la->la_valid & LA_CTIME) {
838 /* The pure setattr, it has the priority over what is
839 * already set, do not drop it if ctime is equal. */
840 if (la->la_ctime < tmp_la->la_ctime)
841 la->la_valid &= ~(LA_ATIME | LA_MTIME |
849 /* set attr and LOV EA at once, return updated attr */
850 static int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
851 const struct md_attr *ma)
853 struct mdd_object *mdd_obj = md2mdd_obj(obj);
854 struct mdd_device *mdd = mdo2mdd(obj);
855 struct thandle *handle;
856 struct lov_mds_md *lmm = NULL;
857 struct llog_cookie *logcookies = NULL;
858 int rc, lmm_size = 0, cookie_size = 0;
859 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
862 mdd_txn_param_build(env, mdd, MDD_TXN_ATTR_SET_OP);
863 handle = mdd_trans_start(env, mdd);
865 RETURN(PTR_ERR(handle));
866 /*TODO: add lock here*/
867 /* start a log jounal handle if needed */
868 if (S_ISREG(mdd_object_type(mdd_obj)) &&
869 ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
870 lmm_size = mdd_lov_mdsize(env, mdd);
871 lmm = mdd_max_lmm_get(env, mdd);
873 GOTO(cleanup, rc = -ENOMEM);
875 rc = mdd_get_md_locked(env, mdd_obj, lmm, &lmm_size,
882 if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
883 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
884 ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
886 *la_copy = ma->ma_attr;
887 rc = mdd_fix_attr(env, mdd_obj, la_copy, ma);
891 if (la_copy->la_valid & LA_FLAGS) {
892 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
895 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
896 } else if (la_copy->la_valid) { /* setattr */
897 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
899 /* journal chown/chgrp in llog, just like unlink */
900 if (rc == 0 && lmm_size){
901 cookie_size = mdd_lov_cookiesize(env, mdd);
902 logcookies = mdd_max_cookie_get(env, mdd);
903 if (logcookies == NULL)
904 GOTO(cleanup, rc = -ENOMEM);
906 if (mdd_setattr_log(env, mdd, ma, lmm, lmm_size,
907 logcookies, cookie_size) <= 0)
912 if (rc == 0 && ma->ma_valid & MA_LOV) {
915 mode = mdd_object_type(mdd_obj);
916 if (S_ISREG(mode) || S_ISDIR(mode)) {
917 rc = mdd_lsm_sanity_check(env, mdd_obj);
921 rc = mdd_lov_set_md(env, NULL, mdd_obj, ma->ma_lmm,
922 ma->ma_lmm_size, handle, 1);
927 mdd_trans_stop(env, mdd, rc, handle);
928 if (rc == 0 && (lmm != NULL && lmm_size > 0 )) {
929 /*set obd attr, if needed*/
930 rc = mdd_lov_setattr_async(env, mdd_obj, lmm, lmm_size,
936 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
937 const struct lu_buf *buf, const char *name, int fl,
938 struct thandle *handle)
943 mdd_write_lock(env, obj);
944 rc = __mdd_xattr_set(env, obj, buf, name, fl, handle);
945 mdd_write_unlock(env, obj);
950 static int mdd_xattr_sanity_check(const struct lu_env *env,
951 struct mdd_object *obj)
953 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
954 struct md_ucred *uc = md_ucred(env);
958 if (mdd_is_immutable(obj) || mdd_is_append(obj))
961 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
965 if ((uc->mu_fsuid != tmp_la->la_uid) &&
966 !mdd_capable(uc, CFS_CAP_FOWNER))
973 * The caller should guarantee to update the object ctime
974 * after xattr_set if needed.
976 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
977 const struct lu_buf *buf, const char *name,
980 struct mdd_object *mdd_obj = md2mdd_obj(obj);
981 struct mdd_device *mdd = mdo2mdd(obj);
982 struct thandle *handle;
986 rc = mdd_xattr_sanity_check(env, mdd_obj);
990 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
991 handle = mdd_trans_start(env, mdd);
993 RETURN(PTR_ERR(handle));
995 rc = mdd_xattr_set_txn(env, mdd_obj, buf, name, fl, handle);
996 mdd_trans_stop(env, mdd, rc, handle);
1002 * The caller should guarantee to update the object ctime
1003 * after xattr_set if needed.
1005 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1008 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1009 struct mdd_device *mdd = mdo2mdd(obj);
1010 struct thandle *handle;
1014 rc = mdd_xattr_sanity_check(env, mdd_obj);
1018 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1019 handle = mdd_trans_start(env, mdd);
1021 RETURN(PTR_ERR(handle));
1023 mdd_write_lock(env, mdd_obj);
1024 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1025 mdd_object_capa(env, mdd_obj));
1026 mdd_write_unlock(env, mdd_obj);
1027 mdd_trans_stop(env, mdd, rc, handle);
1032 /* partial unlink */
1033 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
1036 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1037 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1038 struct mdd_device *mdd = mdo2mdd(obj);
1039 struct thandle *handle;
1044 * Check -ENOENT early here because we need to get object type
1045 * to calculate credits before transaction start
1047 if (!mdd_object_exists(mdd_obj))
1050 LASSERT(mdd_object_exists(mdd_obj) > 0);
1052 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1056 handle = mdd_trans_start(env, mdd);
1060 mdd_write_lock(env, mdd_obj);
1062 rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
1066 __mdd_ref_del(env, mdd_obj, handle, 0);
1068 if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
1070 __mdd_ref_del(env, mdd_obj, handle, 1);
1073 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1074 la_copy->la_ctime = ma->ma_attr.la_ctime;
1076 la_copy->la_valid = LA_CTIME;
1077 rc = mdd_attr_check_set_internal(env, mdd_obj, la_copy, handle, 0);
1081 rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
1085 mdd_write_unlock(env, mdd_obj);
1086 mdd_trans_stop(env, mdd, rc, handle);
1090 /* partial operation */
1091 static int mdd_oc_sanity_check(const struct lu_env *env,
1092 struct mdd_object *obj,
1098 switch (ma->ma_attr.la_mode & S_IFMT) {
1115 static int mdd_object_create(const struct lu_env *env,
1116 struct md_object *obj,
1117 const struct md_op_spec *spec,
1121 struct mdd_device *mdd = mdo2mdd(obj);
1122 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1123 const struct lu_fid *pfid = spec->u.sp_pfid;
1124 struct thandle *handle;
1128 mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1129 handle = mdd_trans_start(env, mdd);
1131 RETURN(PTR_ERR(handle));
1133 mdd_write_lock(env, mdd_obj);
1134 rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1138 rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1142 if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1143 /* If creating the slave object, set slave EA here. */
1144 int lmv_size = spec->u.sp_ea.eadatalen;
1145 struct lmv_stripe_md *lmv;
1147 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1148 LASSERT(lmv != NULL && lmv_size > 0);
1150 rc = __mdd_xattr_set(env, mdd_obj,
1151 mdd_buf_get_const(env, lmv, lmv_size),
1152 MDS_LMV_MD_NAME, 0, handle);
1156 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr,
1159 #ifdef CONFIG_FS_POSIX_ACL
1160 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1161 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1163 buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1164 buf->lb_len = spec->u.sp_ea.eadatalen;
1165 if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1166 rc = __mdd_acl_init(env, mdd_obj, buf,
1167 &ma->ma_attr.la_mode,
1172 ma->ma_attr.la_valid |= LA_MODE;
1175 pfid = spec->u.sp_ea.fid;
1178 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1182 mdd_write_unlock(env, mdd_obj);
1184 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
1186 mdd_trans_stop(env, mdd, rc, handle);
1191 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1192 const struct md_attr *ma)
1194 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1195 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1196 struct mdd_device *mdd = mdo2mdd(obj);
1197 struct thandle *handle;
1201 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1202 handle = mdd_trans_start(env, mdd);
1206 mdd_write_lock(env, mdd_obj);
1207 rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1209 __mdd_ref_add(env, mdd_obj, handle);
1210 mdd_write_unlock(env, mdd_obj);
1212 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1213 la_copy->la_ctime = ma->ma_attr.la_ctime;
1215 la_copy->la_valid = LA_CTIME;
1216 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1219 mdd_trans_stop(env, mdd, 0, handle);
1225 * do NOT or the MAY_*'s, you'll get the weakest
1227 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1231 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1232 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1233 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1234 * owner can write to a file even if it is marked readonly to hide
1235 * its brokenness. (bug 5781) */
1236 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1237 struct md_ucred *uc = md_ucred(env);
1239 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1240 (la->la_uid == uc->mu_fsuid))
1244 if (flags & FMODE_READ)
1246 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1248 if (flags & MDS_FMODE_EXEC)
1253 static int mdd_open_sanity_check(const struct lu_env *env,
1254 struct mdd_object *obj, int flag)
1256 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1261 if (mdd_is_dead_obj(obj))
1264 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1268 if (S_ISLNK(tmp_la->la_mode))
1271 mode = accmode(env, tmp_la, flag);
1273 if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1276 if (!(flag & MDS_OPEN_CREATED)) {
1277 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1282 if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1283 S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1284 flag &= ~MDS_OPEN_TRUNC;
1286 /* For writing append-only file must open it with append mode. */
1287 if (mdd_is_append(obj)) {
1288 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1290 if (flag & MDS_OPEN_TRUNC)
1296 * Now, flag -- O_NOATIME does not be packed by client.
1298 if (flag & O_NOATIME) {
1299 struct md_ucred *uc = md_ucred(env);
1301 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1302 (uc->mu_valid == UCRED_NEW)) &&
1303 (uc->mu_fsuid != tmp_la->la_uid) &&
1304 !mdd_capable(uc, CFS_CAP_FOWNER))
1312 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1315 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1318 mdd_write_lock(env, mdd_obj);
1320 rc = mdd_open_sanity_check(env, mdd_obj, flags);
1322 mdd_obj->mod_count++;
1324 mdd_write_unlock(env, mdd_obj);
1328 /* return md_attr back,
1329 * if it is last unlink then return lov ea + llog cookie*/
1330 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1336 if (S_ISREG(mdd_object_type(obj))) {
1337 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1338 * Caller must be ready for that. */
1339 rc = __mdd_lmm_get(env, obj, ma);
1340 if ((ma->ma_valid & MA_LOV))
1341 rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1348 * No permission check is needed.
1350 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1354 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1355 struct thandle *handle;
1358 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1361 handle = mdd_trans_start(env, mdo2mdd(obj));
1363 RETURN(PTR_ERR(handle));
1365 mdd_write_lock(env, mdd_obj);
1366 /* release open count */
1367 mdd_obj->mod_count --;
1369 rc = mdd_iattr_get(env, mdd_obj, ma);
1370 if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1371 rc = mdd_object_kill(env, mdd_obj, ma);
1373 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1375 mdd_write_unlock(env, mdd_obj);
1376 mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1381 * Permission check is done when open,
1382 * no need check again.
1384 static int mdd_readpage_sanity_check(const struct lu_env *env,
1385 struct mdd_object *obj)
1387 struct dt_object *next = mdd_object_child(obj);
1391 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1399 static int mdd_dir_page_build(const struct lu_env *env, int first,
1400 void *area, int nob, struct dt_it_ops *iops,
1401 struct dt_it *it, __u64 *start, __u64 *end,
1402 struct lu_dirent **last)
1404 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
1405 struct mdd_thread_info *info = mdd_env_info(env);
1406 struct lu_fid_pack *pack = &info->mti_pack;
1408 struct lu_dirent *ent;
1411 memset(area, 0, sizeof (struct lu_dirpage));
1412 area += sizeof (struct lu_dirpage);
1413 nob -= sizeof (struct lu_dirpage);
1416 LASSERT(nob > sizeof *ent);
1426 name = (char *)iops->key(env, it);
1427 len = iops->key_size(env, it);
1429 pack = (struct lu_fid_pack *)iops->rec(env, it);
1430 result = fid_unpack(pack, fid);
1434 recsize = (sizeof(*ent) + len + 7) & ~7;
1435 hash = iops->store(env, it);
1438 CDEBUG(D_INFO, "%p %p %d "DFID": "LPU64" (%d) \"%*.*s\"\n",
1439 name, ent, nob, PFID(fid), hash, len, len, len, name);
1441 if (nob >= recsize) {
1442 ent->lde_fid = *fid;
1443 fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1444 ent->lde_hash = hash;
1445 ent->lde_namelen = cpu_to_le16(len);
1446 ent->lde_reclen = cpu_to_le16(recsize);
1447 memcpy(ent->lde_name, name, len);
1448 if (first && ent == area)
1451 ent = (void *)ent + recsize;
1453 result = iops->next(env, it);
1456 * record doesn't fit into page, enlarge previous one.
1458 LASSERT(*last != NULL);
1459 (*last)->lde_reclen =
1460 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1464 } while (result == 0);
1469 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1470 const struct lu_rdpg *rdpg)
1473 struct dt_object *next = mdd_object_child(obj);
1474 struct dt_it_ops *iops;
1476 struct lu_dirent *last = NULL;
1483 LASSERT(rdpg->rp_pages != NULL);
1484 LASSERT(next->do_index_ops != NULL);
1486 if (rdpg->rp_count <= 0)
1490 * iterate through directory and fill pages from @rdpg
1492 iops = &next->do_index_ops->dio_it;
1493 it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1497 rc = iops->load(env, it, rdpg->rp_hash);
1501 * Iterator didn't find record with exactly the key requested.
1503 * It is currently either
1505 * - positioned above record with key less than
1506 * requested---skip it.
1508 * - or not positioned at all (is in IAM_IT_SKEWED
1509 * state)---position it on the next item.
1511 rc = iops->next(env, it);
1516 * At this point and across for-loop:
1518 * rc == 0 -> ok, proceed.
1519 * rc > 0 -> end of directory.
1522 for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1523 i++, nob -= CFS_PAGE_SIZE) {
1524 LASSERT(i < rdpg->rp_npages);
1525 pg = rdpg->rp_pages[i];
1526 rc = mdd_dir_page_build(env, !i, cfs_kmap(pg),
1527 min_t(int, nob, CFS_PAGE_SIZE), iops,
1528 it, &hash_start, &hash_end, &last);
1529 if (rc != 0 || i == rdpg->rp_npages - 1)
1530 last->lde_reclen = 0;
1537 hash_end = DIR_END_OFF;
1541 struct lu_dirpage *dp;
1543 dp = cfs_kmap(rdpg->rp_pages[0]);
1544 dp->ldp_hash_start = rdpg->rp_hash;
1545 dp->ldp_hash_end = hash_end;
1548 * No pages were processed, mark this.
1550 dp->ldp_flags |= LDF_EMPTY;
1551 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1552 cfs_kunmap(rdpg->rp_pages[0]);
1555 iops->fini(env, it);
1560 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1561 const struct lu_rdpg *rdpg)
1563 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1567 LASSERT(mdd_object_exists(mdd_obj));
1569 mdd_read_lock(env, mdd_obj);
1570 rc = mdd_readpage_sanity_check(env, mdd_obj);
1572 GOTO(out_unlock, rc);
1574 if (mdd_is_dead_obj(mdd_obj)) {
1576 struct lu_dirpage *dp;
1579 * According to POSIX, please do not return any entry to client:
1580 * even dot and dotdot should not be returned.
1582 CWARN("readdir from dead object: "DFID"\n",
1583 PFID(mdd_object_fid(mdd_obj)));
1585 if (rdpg->rp_count <= 0)
1586 GOTO(out_unlock, rc = -EFAULT);
1587 LASSERT(rdpg->rp_pages != NULL);
1589 pg = rdpg->rp_pages[0];
1590 dp = (struct lu_dirpage*)cfs_kmap(pg);
1591 memset(dp, 0 , sizeof(struct lu_dirpage));
1592 dp->ldp_hash_start = rdpg->rp_hash;
1593 dp->ldp_hash_end = DIR_END_OFF;
1594 dp->ldp_flags |= LDF_EMPTY;
1595 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1597 GOTO(out_unlock, rc = 0);
1600 rc = __mdd_readpage(env, mdd_obj, rdpg);
1604 mdd_read_unlock(env, mdd_obj);
1608 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
1610 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1611 struct dt_object *next;
1613 LASSERT(mdd_object_exists(mdd_obj));
1614 next = mdd_object_child(mdd_obj);
1615 return next->do_ops->do_object_sync(env, next);
1618 struct md_object_operations mdd_obj_ops = {
1619 .moo_permission = mdd_permission,
1620 .moo_attr_get = mdd_attr_get,
1621 .moo_attr_set = mdd_attr_set,
1622 .moo_xattr_get = mdd_xattr_get,
1623 .moo_xattr_set = mdd_xattr_set,
1624 .moo_xattr_list = mdd_xattr_list,
1625 .moo_xattr_del = mdd_xattr_del,
1626 .moo_object_create = mdd_object_create,
1627 .moo_ref_add = mdd_ref_add,
1628 .moo_ref_del = mdd_ref_del,
1629 .moo_open = mdd_open,
1630 .moo_close = mdd_close,
1631 .moo_readpage = mdd_readpage,
1632 .moo_readlink = mdd_readlink,
1633 .moo_capa_get = mdd_capa_get,
1634 .moo_object_sync = mdd_object_sync,