1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mdd) routines
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
33 #include <linux/module.h>
34 #include <linux/jbd.h>
36 #include <obd_class.h>
37 #include <obd_support.h>
38 #include <lprocfs_status.h>
39 /* fid_be_cpu(), fid_cpu_to_be(). */
40 #include <lustre_fid.h>
42 #include <linux/ldiskfs_fs.h>
43 #include <lustre_mds.h>
44 #include <lustre/lustre_idl.h>
46 #include "mdd_internal.h"
48 static struct lu_object_operations mdd_lu_obj_ops;
50 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
51 struct lu_attr *la, struct lustre_capa *capa)
53 LASSERTF(mdd_object_exists(obj), "FID is "DFID"\n",
54 PFID(mdd_object_fid(obj)));
55 return mdo_attr_get(env, obj, la, capa);
58 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
60 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
62 if (flags & LUSTRE_APPEND_FL)
63 obj->mod_flags |= APPEND_OBJ;
65 if (flags & LUSTRE_IMMUTABLE_FL)
66 obj->mod_flags |= IMMUTE_OBJ;
69 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
73 buf = &mdd_env_info(env)->mti_buf;
79 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
80 struct mdd_device *mdd)
82 struct mdd_thread_info *mti = mdd_env_info(env);
85 max_cookie_size = mdd_lov_cookiesize(env, mdd);
86 if (unlikely(mti->mti_max_cookie_size < max_cookie_size)) {
87 if (mti->mti_max_cookie)
88 OBD_FREE(mti->mti_max_cookie, mti->mti_max_cookie_size);
89 mti->mti_max_cookie = NULL;
90 mti->mti_max_cookie_size = 0;
92 if (unlikely(mti->mti_max_cookie == NULL)) {
93 OBD_ALLOC(mti->mti_max_cookie, max_cookie_size);
94 if (unlikely(mti->mti_max_cookie != NULL))
95 mti->mti_max_cookie_size = max_cookie_size;
97 return mti->mti_max_cookie;
100 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
101 struct mdd_device *mdd)
103 struct mdd_thread_info *mti = mdd_env_info(env);
106 max_lmm_size = mdd_lov_mdsize(env, mdd);
107 if (unlikely(mti->mti_max_lmm_size < max_lmm_size)) {
108 if (mti->mti_max_lmm)
109 OBD_FREE(mti->mti_max_lmm, mti->mti_max_lmm_size);
110 mti->mti_max_lmm = NULL;
111 mti->mti_max_lmm_size = 0;
113 if (unlikely(mti->mti_max_lmm == NULL)) {
114 OBD_ALLOC(mti->mti_max_lmm, max_lmm_size);
115 if (unlikely(mti->mti_max_lmm != NULL))
116 mti->mti_max_lmm_size = max_lmm_size;
118 return mti->mti_max_lmm;
121 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
122 const void *area, ssize_t len)
126 buf = &mdd_env_info(env)->mti_buf;
127 buf->lb_buf = (void *)area;
132 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
134 struct mdd_thread_info *info;
136 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
137 LASSERT(info != NULL);
141 struct lu_object *mdd_object_alloc(const struct lu_env *env,
142 const struct lu_object_header *hdr,
145 struct mdd_object *mdd_obj;
147 OBD_ALLOC_PTR(mdd_obj);
148 if (mdd_obj != NULL) {
151 o = mdd2lu_obj(mdd_obj);
152 lu_object_init(o, NULL, d);
153 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
154 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
155 mdd_obj->mod_count = 0;
156 o->lo_ops = &mdd_lu_obj_ops;
163 static int mdd_object_init(const struct lu_env *env, struct lu_object *o)
165 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
166 struct lu_object *below;
167 struct lu_device *under;
170 under = &d->mdd_child->dd_lu_dev;
171 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
172 mdd_pdlock_init(lu2mdd_obj(o));
176 lu_object_add(o, below);
180 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
182 if (lu_object_exists(o))
183 return mdd_get_flags(env, lu2mdd_obj(o));
188 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
190 struct mdd_object *mdd = lu2mdd_obj(o);
196 static int mdd_object_print(const struct lu_env *env, void *cookie,
197 lu_printer_t p, const struct lu_object *o)
199 return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p", o);
202 /* orphan handling is here */
203 static void mdd_object_delete(const struct lu_env *env,
206 struct mdd_object *mdd_obj = lu2mdd_obj(o);
207 struct thandle *handle = NULL;
210 if (lu2mdd_dev(o->lo_dev)->mdd_orphans == NULL)
213 if (mdd_obj->mod_flags & ORPHAN_OBJ) {
214 mdd_txn_param_build(env, lu2mdd_dev(o->lo_dev),
215 MDD_TXN_INDEX_DELETE_OP);
216 handle = mdd_trans_start(env, lu2mdd_dev(o->lo_dev));
218 CERROR("Cannot get thandle\n");
220 mdd_write_lock(env, mdd_obj);
221 /* let's remove obj from the orphan list */
222 __mdd_orphan_del(env, mdd_obj, handle);
223 mdd_write_unlock(env, mdd_obj);
224 mdd_trans_stop(env, lu2mdd_dev(o->lo_dev),
230 static struct lu_object_operations mdd_lu_obj_ops = {
231 .loo_object_init = mdd_object_init,
232 .loo_object_start = mdd_object_start,
233 .loo_object_free = mdd_object_free,
234 .loo_object_print = mdd_object_print,
235 .loo_object_delete = mdd_object_delete
238 struct mdd_object *mdd_object_find(const struct lu_env *env,
239 struct mdd_device *d,
240 const struct lu_fid *f)
242 struct lu_object *o, *lo;
243 struct mdd_object *m;
246 o = lu_object_find(env, mdd2lu_dev(d)->ld_site, f);
248 m = (struct mdd_object *)o;
250 lo = lu_object_locate(o->lo_header, mdd2lu_dev(d)->ld_type);
251 /* remote object can't be located and should be put then */
253 lu_object_put(env, o);
259 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
261 struct lu_attr *la = &mdd_env_info(env)->mti_la;
265 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
267 mdd_flags_xlate(obj, la->la_flags);
268 if (S_ISDIR(la->la_mode) && la->la_nlink == 1)
269 obj->mod_flags |= MNLINK_OBJ;
274 /* get only inode attributes */
275 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
281 if (ma->ma_valid & MA_INODE)
284 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
285 mdd_object_capa(env, mdd_obj));
287 ma->ma_valid |= MA_INODE;
291 static int mdd_get_default_md(struct mdd_object *mdd_obj,
292 struct lov_mds_md *lmm, int *size)
294 struct lov_desc *ldesc;
295 struct mdd_device *mdd = mdo2mdd(&mdd_obj->mod_obj);
298 ldesc = &mdd->mdd_obd_dev->u.mds.mds_lov_desc;
299 LASSERT(ldesc != NULL);
304 lmm->lmm_magic = LOV_MAGIC_V1;
305 lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
306 lmm->lmm_pattern = ldesc->ld_pattern;
307 lmm->lmm_stripe_size = ldesc->ld_default_stripe_size;
308 lmm->lmm_stripe_count = ldesc->ld_default_stripe_count;
309 *size = sizeof(struct lov_mds_md);
311 RETURN(sizeof(struct lov_mds_md));
314 /* get lov EA only */
315 static int __mdd_lmm_get(const struct lu_env *env,
316 struct mdd_object *mdd_obj, struct md_attr *ma)
321 if (ma->ma_valid & MA_LOV)
324 rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
327 if (rc == 0 && (ma->ma_need & MA_LOV_DEF)) {
328 rc = mdd_get_default_md(mdd_obj, ma->ma_lmm,
333 ma->ma_valid |= MA_LOV;
339 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
345 mdd_read_lock(env, mdd_obj);
346 rc = __mdd_lmm_get(env, mdd_obj, ma);
347 mdd_read_unlock(env, mdd_obj);
352 static int __mdd_lmv_get(const struct lu_env *env,
353 struct mdd_object *mdd_obj, struct md_attr *ma)
358 if (ma->ma_valid & MA_LMV)
361 rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
364 ma->ma_valid |= MA_LMV;
370 static int mdd_attr_get_internal(const struct lu_env *env,
371 struct mdd_object *mdd_obj,
377 if (ma->ma_need & MA_INODE)
378 rc = mdd_iattr_get(env, mdd_obj, ma);
380 if (rc == 0 && ma->ma_need & MA_LOV) {
381 if (S_ISREG(mdd_object_type(mdd_obj)) ||
382 S_ISDIR(mdd_object_type(mdd_obj)))
383 rc = __mdd_lmm_get(env, mdd_obj, ma);
385 if (rc == 0 && ma->ma_need & MA_LMV) {
386 if (S_ISDIR(mdd_object_type(mdd_obj)))
387 rc = __mdd_lmv_get(env, mdd_obj, ma);
389 #ifdef CONFIG_FS_POSIX_ACL
390 if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
391 if (S_ISDIR(mdd_object_type(mdd_obj)))
392 rc = mdd_acl_def_get(env, mdd_obj, ma);
395 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
400 int mdd_attr_get_internal_locked(const struct lu_env *env,
401 struct mdd_object *mdd_obj, struct md_attr *ma)
404 int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
407 mdd_read_lock(env, mdd_obj);
408 rc = mdd_attr_get_internal(env, mdd_obj, ma);
410 mdd_read_unlock(env, mdd_obj);
415 * No permission check is needed.
417 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
420 struct mdd_object *mdd_obj = md2mdd_obj(obj);
424 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
429 * No permission check is needed.
431 static int mdd_xattr_get(const struct lu_env *env,
432 struct md_object *obj, struct lu_buf *buf,
435 struct mdd_object *mdd_obj = md2mdd_obj(obj);
440 LASSERT(mdd_object_exists(mdd_obj));
442 mdd_read_lock(env, mdd_obj);
443 rc = mdo_xattr_get(env, mdd_obj, buf, name,
444 mdd_object_capa(env, mdd_obj));
445 mdd_read_unlock(env, mdd_obj);
451 * Permission check is done when open,
452 * no need check again.
454 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
457 struct mdd_object *mdd_obj = md2mdd_obj(obj);
458 struct dt_object *next;
463 LASSERT(mdd_object_exists(mdd_obj));
465 next = mdd_object_child(mdd_obj);
466 mdd_read_lock(env, mdd_obj);
467 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
468 mdd_object_capa(env, mdd_obj));
469 mdd_read_unlock(env, mdd_obj);
474 * No permission check is needed.
476 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
479 struct mdd_object *mdd_obj = md2mdd_obj(obj);
484 mdd_read_lock(env, mdd_obj);
485 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
486 mdd_read_unlock(env, mdd_obj);
491 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
492 struct mdd_object *c, struct md_attr *ma,
493 struct thandle *handle)
495 struct lu_attr *attr = &ma->ma_attr;
496 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
500 if (!mdd_object_exists(c)) {
501 struct dt_object *next = mdd_object_child(c);
504 /* @hint will be initialized by underlying device. */
505 next->do_ops->do_ah_init(env, hint,
506 p ? mdd_object_child(p) : NULL,
507 attr->la_mode & S_IFMT);
508 rc = mdo_create_obj(env, c, attr, hint, handle);
509 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
517 * Make sure the ctime is increased only.
519 static inline int mdd_attr_check(const struct lu_env *env,
520 struct mdd_object *obj,
521 struct lu_attr *attr)
523 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
527 if (attr->la_valid & LA_CTIME) {
528 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
532 if (attr->la_ctime < tmp_la->la_ctime)
533 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
534 else if (attr->la_valid == LA_CTIME &&
535 attr->la_ctime == tmp_la->la_ctime)
536 attr->la_valid &= ~LA_CTIME;
541 int mdd_attr_set_internal(const struct lu_env *env,
542 struct mdd_object *obj,
543 struct lu_attr *attr,
544 struct thandle *handle,
550 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
551 #ifdef CONFIG_FS_POSIX_ACL
552 if (!rc && (attr->la_valid & LA_MODE) && needacl)
553 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
558 int mdd_attr_check_set_internal(const struct lu_env *env,
559 struct mdd_object *obj,
560 struct lu_attr *attr,
561 struct thandle *handle,
567 rc = mdd_attr_check(env, obj, attr);
572 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
576 static int mdd_attr_set_internal_locked(const struct lu_env *env,
577 struct mdd_object *obj,
578 struct lu_attr *attr,
579 struct thandle *handle,
585 needacl = needacl && (attr->la_valid & LA_MODE);
587 mdd_write_lock(env, obj);
588 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
590 mdd_write_unlock(env, obj);
594 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
595 struct mdd_object *obj,
596 struct lu_attr *attr,
597 struct thandle *handle,
603 needacl = needacl && (attr->la_valid & LA_MODE);
605 mdd_write_lock(env, obj);
606 rc = mdd_attr_check_set_internal(env, obj, attr, handle, needacl);
608 mdd_write_unlock(env, obj);
612 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
613 const struct lu_buf *buf, const char *name,
614 int fl, struct thandle *handle)
616 struct lustre_capa *capa = mdd_object_capa(env, obj);
620 if (buf->lb_buf && buf->lb_len > 0)
621 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
622 else if (buf->lb_buf == NULL && buf->lb_len == 0)
623 rc = mdo_xattr_del(env, obj, name, handle, capa);
629 * This gives the same functionality as the code between
630 * sys_chmod and inode_setattr
631 * chown_common and inode_setattr
632 * utimes and inode_setattr
633 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
635 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
636 struct lu_attr *la, const struct md_attr *ma)
638 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
639 struct md_ucred *uc = md_ucred(env);
646 /* Do not permit change file type */
647 if (la->la_valid & LA_TYPE)
650 /* They should not be processed by setattr */
651 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
654 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
658 if (la->la_valid == LA_CTIME) {
659 if (!(ma->ma_attr_flags & MDS_PERM_BYPASS))
660 /* This is only for set ctime when rename's source is
662 rc = mdd_may_delete(env, NULL, obj,
663 (struct md_attr *)ma, 1, 0);
664 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
665 la->la_valid &= ~LA_CTIME;
669 if (la->la_valid == LA_ATIME) {
670 /* This is atime only set for read atime update on close. */
671 if (la->la_atime <= tmp_la->la_atime +
672 mdd_obj2mdd_dev(obj)->mdd_atime_diff)
673 la->la_valid &= ~LA_ATIME;
677 /* Check if flags change. */
678 if (la->la_valid & LA_FLAGS) {
679 unsigned int oldflags = 0;
680 unsigned int newflags = la->la_flags &
681 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
683 if ((uc->mu_fsuid != tmp_la->la_uid) &&
684 !mdd_capable(uc, CAP_FOWNER))
687 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
688 * only be changed by the relevant capability. */
689 if (mdd_is_immutable(obj))
690 oldflags |= LUSTRE_IMMUTABLE_FL;
691 if (mdd_is_append(obj))
692 oldflags |= LUSTRE_APPEND_FL;
693 if ((oldflags ^ newflags) &&
694 !mdd_capable(uc, CAP_LINUX_IMMUTABLE))
697 if (!S_ISDIR(tmp_la->la_mode))
698 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
701 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
702 (la->la_valid & ~LA_FLAGS) &&
703 !(ma->ma_attr_flags & MDS_PERM_BYPASS))
706 /* Check for setting the obj time. */
707 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
708 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
709 if ((uc->mu_fsuid != tmp_la->la_uid) &&
710 !mdd_capable(uc, CAP_FOWNER)) {
711 rc = mdd_permission_internal_locked(env, obj, tmp_la,
718 /* Make sure a caller can chmod. */
719 if (la->la_valid & LA_MODE) {
720 /* Bypass la_vaild == LA_MODE,
721 * this is for changing file with SUID or SGID. */
722 if ((la->la_valid & ~LA_MODE) &&
723 (uc->mu_fsuid != tmp_la->la_uid) &&
724 !mdd_capable(uc, CAP_FOWNER))
727 if (la->la_mode == (umode_t) -1)
728 la->la_mode = tmp_la->la_mode;
730 la->la_mode = (la->la_mode & S_IALLUGO) |
731 (tmp_la->la_mode & ~S_IALLUGO);
733 /* Also check the setgid bit! */
734 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ? la->la_gid :
735 tmp_la->la_gid) && !mdd_capable(uc, CAP_FSETID))
736 la->la_mode &= ~S_ISGID;
738 la->la_mode = tmp_la->la_mode;
741 /* Make sure a caller can chown. */
742 if (la->la_valid & LA_UID) {
743 if (la->la_uid == (uid_t) -1)
744 la->la_uid = tmp_la->la_uid;
745 if (((uc->mu_fsuid != tmp_la->la_uid) ||
746 (la->la_uid != tmp_la->la_uid)) &&
747 !mdd_capable(uc, CAP_CHOWN))
750 /* If the user or group of a non-directory has been
751 * changed by a non-root user, remove the setuid bit.
752 * 19981026 David C Niemi <niemi@tux.org>
754 * Changed this to apply to all users, including root,
755 * to avoid some races. This is the behavior we had in
756 * 2.0. The check for non-root was definitely wrong
757 * for 2.2 anyway, as it should have been using
758 * CAP_FSETID rather than fsuid -- 19990830 SD. */
759 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
760 !S_ISDIR(tmp_la->la_mode)) {
761 la->la_mode &= ~S_ISUID;
762 la->la_valid |= LA_MODE;
766 /* Make sure caller can chgrp. */
767 if (la->la_valid & LA_GID) {
768 if (la->la_gid == (gid_t) -1)
769 la->la_gid = tmp_la->la_gid;
770 if (((uc->mu_fsuid != tmp_la->la_uid) ||
771 ((la->la_gid != tmp_la->la_gid) &&
772 !lustre_in_group_p(uc, la->la_gid))) &&
773 !mdd_capable(uc, CAP_CHOWN))
776 /* Likewise, if the user or group of a non-directory
777 * has been changed by a non-root user, remove the
778 * setgid bit UNLESS there is no group execute bit
779 * (this would be a file marked for mandatory
780 * locking). 19981026 David C Niemi <niemi@tux.org>
782 * Removed the fsuid check (see the comment above) --
784 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
785 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
786 la->la_mode &= ~S_ISGID;
787 la->la_valid |= LA_MODE;
791 /* For both Size-on-MDS case and truncate case,
792 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
793 * We distinguish them by "ma->ma_attr_flags & MDS_SOM".
794 * For SOM case, it is true, the MAY_WRITE perm has been checked
795 * when open, no need check again. For truncate case, it is false,
796 * the MAY_WRITE perm should be checked here. */
797 if (ma->ma_attr_flags & MDS_SOM) {
798 /* For the "Size-on-MDS" setattr update, merge coming
799 * attributes with the set in the inode. BUG 10641 */
800 if ((la->la_valid & LA_ATIME) &&
801 (la->la_atime <= tmp_la->la_atime))
802 la->la_valid &= ~LA_ATIME;
804 /* OST attributes do not have a priority over MDS attributes,
805 * so drop times if ctime is equal. */
806 if ((la->la_valid & LA_CTIME) &&
807 (la->la_ctime <= tmp_la->la_ctime))
808 la->la_valid &= ~(LA_MTIME | LA_CTIME);
810 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
811 if (!((ma->ma_attr_flags & MDS_OPEN_OWNEROVERRIDE) &&
812 (uc->mu_fsuid == tmp_la->la_uid)) &&
813 !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
814 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);
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) && !mdd_capable(uc, CAP_FOWNER))
955 * The caller should guarantee to update the object ctime
956 * after xattr_set if needed.
958 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
959 const struct lu_buf *buf, const char *name,
962 struct mdd_object *mdd_obj = md2mdd_obj(obj);
963 struct mdd_device *mdd = mdo2mdd(obj);
964 struct thandle *handle;
968 rc = mdd_xattr_sanity_check(env, mdd_obj);
972 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
973 handle = mdd_trans_start(env, mdd);
975 RETURN(PTR_ERR(handle));
977 rc = mdd_xattr_set_txn(env, mdd_obj, buf, name, fl, handle);
978 mdd_trans_stop(env, mdd, rc, handle);
984 * The caller should guarantee to update the object ctime
985 * after xattr_set if needed.
987 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
990 struct mdd_object *mdd_obj = md2mdd_obj(obj);
991 struct mdd_device *mdd = mdo2mdd(obj);
992 struct thandle *handle;
996 rc = mdd_xattr_sanity_check(env, mdd_obj);
1000 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1001 handle = mdd_trans_start(env, mdd);
1003 RETURN(PTR_ERR(handle));
1005 mdd_write_lock(env, mdd_obj);
1006 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1007 mdd_object_capa(env, mdd_obj));
1008 mdd_write_unlock(env, mdd_obj);
1009 mdd_trans_stop(env, mdd, rc, handle);
1014 /* partial unlink */
1015 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
1018 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1019 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1020 struct mdd_device *mdd = mdo2mdd(obj);
1021 struct thandle *handle;
1026 * Check -ENOENT early here because we need to get object type
1027 * to calculate credits before transaction start
1029 if (!mdd_object_exists(mdd_obj))
1032 LASSERT(mdd_object_exists(mdd_obj) > 0);
1034 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1038 handle = mdd_trans_start(env, mdd);
1042 mdd_write_lock(env, mdd_obj);
1044 rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
1048 __mdd_ref_del(env, mdd_obj, handle, 0);
1050 if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
1052 __mdd_ref_del(env, mdd_obj, handle, 1);
1055 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1056 la_copy->la_ctime = ma->ma_attr.la_ctime;
1058 la_copy->la_valid = LA_CTIME;
1059 rc = mdd_attr_check_set_internal(env, mdd_obj, la_copy, handle, 0);
1063 rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
1067 mdd_write_unlock(env, mdd_obj);
1068 mdd_trans_stop(env, mdd, rc, handle);
1072 /* partial operation */
1073 static int mdd_oc_sanity_check(const struct lu_env *env,
1074 struct mdd_object *obj,
1080 switch (ma->ma_attr.la_mode & S_IFMT) {
1097 static int mdd_object_create(const struct lu_env *env,
1098 struct md_object *obj,
1099 const struct md_op_spec *spec,
1103 struct mdd_device *mdd = mdo2mdd(obj);
1104 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1105 const struct lu_fid *pfid = spec->u.sp_pfid;
1106 struct thandle *handle;
1110 mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1111 handle = mdd_trans_start(env, mdd);
1113 RETURN(PTR_ERR(handle));
1115 mdd_write_lock(env, mdd_obj);
1116 rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1120 rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1124 if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1125 /* If creating the slave object, set slave EA here. */
1126 int lmv_size = spec->u.sp_ea.eadatalen;
1127 struct lmv_stripe_md *lmv;
1129 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1130 LASSERT(lmv != NULL && lmv_size > 0);
1132 rc = __mdd_xattr_set(env, mdd_obj,
1133 mdd_buf_get_const(env, lmv, lmv_size),
1134 MDS_LMV_MD_NAME, 0, handle);
1138 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr,
1141 #ifdef CONFIG_FS_POSIX_ACL
1142 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1143 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1145 buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1146 buf->lb_len = spec->u.sp_ea.eadatalen;
1147 if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1148 rc = __mdd_acl_init(env, mdd_obj, buf,
1149 &ma->ma_attr.la_mode,
1154 ma->ma_attr.la_valid |= LA_MODE;
1157 pfid = spec->u.sp_ea.fid;
1160 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1164 mdd_write_unlock(env, mdd_obj);
1166 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
1168 mdd_trans_stop(env, mdd, rc, handle);
1173 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1174 const struct md_attr *ma)
1176 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1177 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1178 struct mdd_device *mdd = mdo2mdd(obj);
1179 struct thandle *handle;
1183 mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1184 handle = mdd_trans_start(env, mdd);
1188 mdd_write_lock(env, mdd_obj);
1189 rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1191 __mdd_ref_add(env, mdd_obj, handle);
1192 mdd_write_unlock(env, mdd_obj);
1194 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1195 la_copy->la_ctime = ma->ma_attr.la_ctime;
1197 la_copy->la_valid = LA_CTIME;
1198 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1201 mdd_trans_stop(env, mdd, 0, handle);
1207 * do NOT or the MAY_*'s, you'll get the weakest
1209 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1213 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1214 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1215 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1216 * owner can write to a file even if it is marked readonly to hide
1217 * its brokenness. (bug 5781) */
1218 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1219 struct md_ucred *uc = md_ucred(env);
1221 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1222 (la->la_uid == uc->mu_fsuid))
1226 if (flags & FMODE_READ)
1228 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1230 if (flags & MDS_FMODE_EXEC)
1235 static int mdd_open_sanity_check(const struct lu_env *env,
1236 struct mdd_object *obj, int flag)
1238 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1243 if (mdd_is_dead_obj(obj))
1246 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1250 if (S_ISLNK(tmp_la->la_mode))
1253 mode = accmode(env, tmp_la, flag);
1255 if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1258 if (!(flag & MDS_OPEN_CREATED)) {
1259 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1264 if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1265 S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1266 flag &= ~MDS_OPEN_TRUNC;
1268 /* For writing append-only file must open it with append mode. */
1269 if (mdd_is_append(obj)) {
1270 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1272 if (flag & MDS_OPEN_TRUNC)
1278 * Now, flag -- O_NOATIME does not be packed by client.
1280 if (flag & O_NOATIME) {
1281 struct md_ucred *uc = md_ucred(env);
1283 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1284 (uc->mu_valid == UCRED_NEW)) &&
1285 (uc->mu_fsuid != tmp_la->la_uid) &&
1286 !mdd_capable(uc, CAP_FOWNER))
1294 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1297 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1300 mdd_write_lock(env, mdd_obj);
1302 rc = mdd_open_sanity_check(env, mdd_obj, flags);
1304 mdd_obj->mod_count++;
1306 mdd_write_unlock(env, mdd_obj);
1310 /* return md_attr back,
1311 * if it is last unlink then return lov ea + llog cookie*/
1312 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1318 if (S_ISREG(mdd_object_type(obj))) {
1319 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1320 * Caller must be ready for that. */
1321 rc = __mdd_lmm_get(env, obj, ma);
1322 if ((ma->ma_valid & MA_LOV))
1323 rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1330 * No permission check is needed.
1332 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1336 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1337 struct thandle *handle;
1340 rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1343 handle = mdd_trans_start(env, mdo2mdd(obj));
1345 RETURN(PTR_ERR(handle));
1347 mdd_write_lock(env, mdd_obj);
1348 /* release open count */
1349 mdd_obj->mod_count --;
1351 rc = mdd_iattr_get(env, mdd_obj, ma);
1352 if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1353 rc = mdd_object_kill(env, mdd_obj, ma);
1355 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1357 mdd_write_unlock(env, mdd_obj);
1358 mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1363 * Permission check is done when open,
1364 * no need check again.
1366 static int mdd_readpage_sanity_check(const struct lu_env *env,
1367 struct mdd_object *obj)
1369 struct dt_object *next = mdd_object_child(obj);
1373 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1381 static int mdd_dir_page_build(const struct lu_env *env, int first,
1382 void *area, int nob, struct dt_it_ops *iops,
1383 struct dt_it *it, __u64 *start, __u64 *end,
1384 struct lu_dirent **last)
1386 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
1387 struct mdd_thread_info *info = mdd_env_info(env);
1388 struct lu_fid_pack *pack = &info->mti_pack;
1390 struct lu_dirent *ent;
1393 memset(area, 0, sizeof (struct lu_dirpage));
1394 area += sizeof (struct lu_dirpage);
1395 nob -= sizeof (struct lu_dirpage);
1398 LASSERT(nob > sizeof *ent);
1408 name = (char *)iops->key(env, it);
1409 len = iops->key_size(env, it);
1411 pack = (struct lu_fid_pack *)iops->rec(env, it);
1412 result = fid_unpack(pack, fid);
1416 recsize = (sizeof(*ent) + len + 7) & ~7;
1417 hash = iops->store(env, it);
1420 CDEBUG(D_INFO, "%p %p %d "DFID": "LPU64" (%d) \"%*.*s\"\n",
1421 name, ent, nob, PFID(fid), hash, len, len, len, name);
1423 if (nob >= recsize) {
1424 ent->lde_fid = *fid;
1425 fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1426 ent->lde_hash = hash;
1427 ent->lde_namelen = cpu_to_le16(len);
1428 ent->lde_reclen = cpu_to_le16(recsize);
1429 memcpy(ent->lde_name, name, len);
1430 if (first && ent == area)
1433 ent = (void *)ent + recsize;
1435 result = iops->next(env, it);
1438 * record doesn't fit into page, enlarge previous one.
1440 LASSERT(*last != NULL);
1441 (*last)->lde_reclen =
1442 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1446 } while (result == 0);
1451 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1452 const struct lu_rdpg *rdpg)
1455 struct dt_object *next = mdd_object_child(obj);
1456 struct dt_it_ops *iops;
1458 struct lu_dirent *last;
1465 LASSERT(rdpg->rp_pages != NULL);
1466 LASSERT(next->do_index_ops != NULL);
1468 if (rdpg->rp_count <= 0)
1472 * iterate through directory and fill pages from @rdpg
1474 iops = &next->do_index_ops->dio_it;
1475 it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1479 rc = iops->load(env, it, rdpg->rp_hash);
1483 * Iterator didn't find record with exactly the key requested.
1485 * It is currently either
1487 * - positioned above record with key less than
1488 * requested---skip it.
1490 * - or not positioned at all (is in IAM_IT_SKEWED
1491 * state)---position it on the next item.
1493 rc = iops->next(env, it);
1498 * At this point and across for-loop:
1500 * rc == 0 -> ok, proceed.
1501 * rc > 0 -> end of directory.
1504 for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1505 i++, nob -= CFS_PAGE_SIZE) {
1506 LASSERT(i < rdpg->rp_npages);
1507 pg = rdpg->rp_pages[i];
1508 rc = mdd_dir_page_build(env, !i, cfs_kmap(pg),
1509 min_t(int, nob, CFS_PAGE_SIZE), iops,
1510 it, &hash_start, &hash_end, &last);
1511 if (rc != 0 || i == rdpg->rp_npages - 1)
1512 last->lde_reclen = 0;
1519 hash_end = DIR_END_OFF;
1523 struct lu_dirpage *dp;
1525 dp = cfs_kmap(rdpg->rp_pages[0]);
1526 dp->ldp_hash_start = rdpg->rp_hash;
1527 dp->ldp_hash_end = hash_end;
1530 * No pages were processed, mark this.
1532 dp->ldp_flags |= LDF_EMPTY;
1533 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1534 cfs_kunmap(rdpg->rp_pages[0]);
1537 iops->fini(env, it);
1542 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1543 const struct lu_rdpg *rdpg)
1545 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1549 LASSERT(mdd_object_exists(mdd_obj));
1551 mdd_read_lock(env, mdd_obj);
1552 rc = mdd_readpage_sanity_check(env, mdd_obj);
1554 GOTO(out_unlock, rc);
1556 if (mdd_is_dead_obj(mdd_obj)) {
1558 struct lu_dirpage *dp;
1561 * According to POSIX, please do not return any entry to client:
1562 * even dot and dotdot should not be returned.
1564 CWARN("readdir from dead object: "DFID"\n",
1565 PFID(mdd_object_fid(mdd_obj)));
1567 if (rdpg->rp_count <= 0)
1568 GOTO(out_unlock, rc = -EFAULT);
1569 LASSERT(rdpg->rp_pages != NULL);
1571 pg = rdpg->rp_pages[0];
1572 dp = (struct lu_dirpage*)cfs_kmap(pg);
1573 memset(dp, 0 , sizeof(struct lu_dirpage));
1574 dp->ldp_hash_start = rdpg->rp_hash;
1575 dp->ldp_hash_end = DIR_END_OFF;
1576 dp->ldp_flags |= LDF_EMPTY;
1577 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1579 GOTO(out_unlock, rc = 0);
1582 rc = __mdd_readpage(env, mdd_obj, rdpg);
1586 mdd_read_unlock(env, mdd_obj);
1590 struct md_object_operations mdd_obj_ops = {
1591 .moo_permission = mdd_permission,
1592 .moo_attr_get = mdd_attr_get,
1593 .moo_attr_set = mdd_attr_set,
1594 .moo_xattr_get = mdd_xattr_get,
1595 .moo_xattr_set = mdd_xattr_set,
1596 .moo_xattr_list = mdd_xattr_list,
1597 .moo_xattr_del = mdd_xattr_del,
1598 .moo_object_create = mdd_object_create,
1599 .moo_ref_add = mdd_ref_add,
1600 .moo_ref_del = mdd_ref_del,
1601 .moo_open = mdd_open,
1602 .moo_close = mdd_close,
1603 .moo_readpage = mdd_readpage,
1604 .moo_readlink = mdd_readlink,
1605 .moo_capa_get = mdd_capa_get