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>
37 #include <obd_class.h>
38 #include <lustre_ver.h>
39 #include <obd_support.h>
40 #include <lprocfs_status.h>
42 #include <linux/ldiskfs_fs.h>
43 #include <lustre_mds.h>
44 #include <lu_object.h>
45 #include <md_object.h>
46 #include <dt_object.h>
48 #include "mdd_internal.h"
51 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
53 static void mdd_trans_stop(const struct lu_context *ctxt,
54 struct mdd_device *mdd, int rc,
55 struct thandle *handle);
56 static struct dt_object* mdd_object_child(struct mdd_object *o);
57 static void __mdd_ref_add(const struct lu_context *ctxt, struct mdd_object *obj,
58 struct thandle *handle);
59 static void __mdd_ref_del(const struct lu_context *ctxt, struct mdd_object *obj,
60 struct thandle *handle);
61 static int mdd_lookup(const struct lu_context *ctxt, struct md_object *pobj,
62 const char *name, struct lu_fid* fid);
64 static struct md_object_operations mdd_obj_ops;
65 static struct md_dir_operations mdd_dir_ops;
66 static struct lu_object_operations mdd_lu_obj_ops;
68 static struct lu_context_key mdd_thread_key;
70 static const char *mdd_root_dir_name = "root";
71 static const char dot[] = ".";
72 static const char dotdot[] = "..";
75 struct mdd_thread_info *mdd_ctx_info(const struct lu_context *ctx)
77 struct mdd_thread_info *info;
79 info = lu_context_key_get(ctx, &mdd_thread_key);
80 LASSERT(info != NULL);
84 static struct lu_object *mdd_object_alloc(const struct lu_context *ctxt,
85 const struct lu_object_header *hdr,
88 struct mdd_object *mdd_obj;
90 OBD_ALLOC_PTR(mdd_obj);
91 if (mdd_obj != NULL) {
94 o = mdd2lu_obj(mdd_obj);
95 lu_object_init(o, NULL, d);
96 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
97 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
98 atomic_set(&mdd_obj->mod_count, 0);
99 o->lo_ops = &mdd_lu_obj_ops;
106 static int mdd_object_init(const struct lu_context *ctxt, struct lu_object *o)
108 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
109 struct lu_object *below;
110 struct lu_device *under;
113 under = &d->mdd_child->dd_lu_dev;
114 below = under->ld_ops->ldo_object_alloc(ctxt, o->lo_header, under);
119 lu_object_add(o, below);
123 static int mdd_get_flags(const struct lu_context *ctxt, struct mdd_object *obj);
125 static int mdd_object_start(const struct lu_context *ctxt, struct lu_object *o)
127 if (lu_object_exists(o))
128 return mdd_get_flags(ctxt, lu2mdd_obj(o));
133 static void mdd_object_free(const struct lu_context *ctxt, struct lu_object *o)
135 struct mdd_object *mdd = lu2mdd_obj(o);
141 struct mdd_object *mdd_object_find(const struct lu_context *ctxt,
142 struct mdd_device *d,
143 const struct lu_fid *f)
146 struct mdd_object *m;
149 o = lu_object_find(ctxt, d->mdd_md_dev.md_lu_dev.ld_site, f);
151 m = (struct mdd_object *)o;
153 m = lu2mdd_obj(lu_object_locate(o->lo_header,
154 d->mdd_md_dev.md_lu_dev.ld_type));
158 static inline void mdd_object_put(const struct lu_context *ctxt,
159 struct mdd_object *o)
161 lu_object_put(ctxt, &o->mod_obj.mo_lu);
164 static inline int mdd_is_immutable(struct mdd_object *obj)
166 return obj->mod_flags & IMMUTE_OBJ;
169 static inline int mdd_is_append(struct mdd_object *obj)
171 return obj->mod_flags & APPEND_OBJ;
174 static void mdd_set_dead_obj(struct mdd_object *obj)
177 obj->mod_flags |= DEAD_OBJ;
180 static int mdd_is_dead_obj(struct mdd_object *obj)
182 return obj && obj->mod_flags & DEAD_OBJ;
185 /*Check whether it may create the cobj under the pobj*/
186 static int mdd_may_create(const struct lu_context *ctxt,
187 struct mdd_object *pobj, struct mdd_object *cobj)
190 if (cobj && lu_object_exists(&cobj->mod_obj.mo_lu))
193 if (mdd_is_dead_obj(pobj))
196 /*check pobj may create or not*/
200 static inline int __mdd_la_get(const struct lu_context *ctxt,
201 struct mdd_object *obj, struct lu_attr *la)
203 struct dt_object *next = mdd_object_child(obj);
204 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
205 return next->do_ops->do_attr_get(ctxt, next, la);
208 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
210 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
212 if (flags & LUSTRE_APPEND_FL)
213 obj->mod_flags |= APPEND_OBJ;
215 if (flags & LUSTRE_IMMUTABLE_FL)
216 obj->mod_flags |= IMMUTE_OBJ;
219 static int mdd_get_flags(const struct lu_context *ctxt, struct mdd_object *obj)
221 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
225 mdd_read_lock(ctxt, obj);
226 rc = __mdd_la_get(ctxt, obj, la);
227 mdd_read_unlock(ctxt, obj);
229 mdd_flags_xlate(obj, la->la_flags);
233 /*Check whether it may delete the cobj under the pobj*/
234 static int mdd_may_delete(const struct lu_context *ctxt,
235 struct mdd_object *pobj, struct mdd_object *cobj,
238 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
242 LASSERT(cobj && pobj);
244 if (!lu_object_exists(&cobj->mod_obj.mo_lu))
247 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
251 if (!S_ISDIR(mdd_object_type(cobj)))
254 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
257 } else if (S_ISDIR(mdd_object_type(cobj)))
260 if (mdd_is_dead_obj(pobj))
265 /* get only inode attributes */
266 static int __mdd_iattr_get(const struct lu_context *ctxt,
267 struct mdd_object *mdd_obj, struct md_attr *ma)
272 rc = __mdd_la_get(ctxt, mdd_obj, &ma->ma_attr);
274 ma->ma_valid = MA_INODE;
277 /* get lov EA only */
278 static int __mdd_lmm_get(const struct lu_context *ctxt,
279 struct mdd_object *mdd_obj, struct md_attr *ma)
283 LASSERT(ma->ma_lmm != NULL && ma->ma_lmm_size > 0);
284 rc = mdd_get_md(ctxt, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size, 0,
287 ma->ma_valid |= MA_LOV;
294 static int __mdd_lmv_get(const struct lu_context *ctxt,
295 struct mdd_object *mdd_obj, struct md_attr *ma)
299 rc = mdd_get_md(ctxt, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size, 0,
302 ma->ma_valid |= MA_LMV;
308 static int mdd_attr_get_internal(const struct lu_context *ctxt,
309 struct mdd_object *mdd_obj,
315 if (ma->ma_need & MA_INODE)
316 rc = __mdd_iattr_get(ctxt, mdd_obj, ma);
318 if (rc == 0 && ma->ma_need & MA_LOV) {
319 if (S_ISREG(mdd_object_type(mdd_obj)) ||
320 S_ISDIR(mdd_object_type(mdd_obj)))
321 rc = __mdd_lmm_get(ctxt, mdd_obj, ma);
323 if (rc == 0 && ma->ma_need & MA_LMV) {
324 if (S_ISDIR(mdd_object_type(mdd_obj)))
325 rc = __mdd_lmv_get(ctxt, mdd_obj, ma);
328 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
333 static inline int mdd_attr_get_internal_locked(const struct lu_context *ctxt,
334 struct mdd_object *mdd_obj,
338 mdd_read_lock(ctxt, mdd_obj);
339 rc = mdd_attr_get_internal(ctxt, mdd_obj, ma);
340 mdd_read_unlock(ctxt, mdd_obj);
344 static int mdd_attr_get(const struct lu_context *ctxt,
345 struct md_object *obj, struct md_attr *ma)
347 struct mdd_object *mdd_obj = md2mdd_obj(obj);
351 rc = mdd_attr_get_internal_locked(ctxt, mdd_obj, ma);
355 static int mdd_xattr_get(const struct lu_context *ctxt, struct md_object *obj,
356 void *buf, int buf_len, const char *name)
358 struct mdd_object *mdd_obj = md2mdd_obj(obj);
359 struct dt_object *next;
364 LASSERT(lu_object_exists(&obj->mo_lu));
366 next = mdd_object_child(mdd_obj);
367 mdd_read_lock(ctxt, mdd_obj);
368 rc = next->do_ops->do_xattr_get(ctxt, next, buf, buf_len, name);
369 mdd_read_unlock(ctxt, mdd_obj);
374 static int mdd_readlink(const struct lu_context *ctxt, struct md_object *obj,
375 void *buf, int buf_len)
377 struct mdd_object *mdd_obj = md2mdd_obj(obj);
378 struct dt_object *next;
383 LASSERT(lu_object_exists(&obj->mo_lu));
385 next = mdd_object_child(mdd_obj);
386 rc = next->do_body_ops->dbo_read(ctxt, next, buf, buf_len, &pos);
389 static int mdd_xattr_list(const struct lu_context *ctxt, struct md_object *obj,
390 void *buf, int buf_len)
392 struct mdd_object *mdd_obj = md2mdd_obj(obj);
393 struct dt_object *next;
398 LASSERT(lu_object_exists(&obj->mo_lu));
400 next = mdd_object_child(mdd_obj);
401 rc = next->do_ops->do_xattr_list(ctxt, next, buf, buf_len);
407 MDD_TXN_OBJECT_DESTROY_OP,
408 MDD_TXN_OBJECT_CREATE_OP,
410 MDD_TXN_XATTR_SET_OP,
411 MDD_TXN_INDEX_INSERT_OP,
412 MDD_TXN_INDEX_DELETE_OP,
416 MDD_TXN_CREATE_DATA_OP,
420 struct mdd_txn_op_descr {
421 enum mdd_txn_op mod_op;
422 unsigned int mod_credits;
426 MDD_TXN_OBJECT_DESTROY_CREDITS = 20,
427 MDD_TXN_OBJECT_CREATE_CREDITS = 20,
428 MDD_TXN_ATTR_SET_CREDITS = 20,
429 MDD_TXN_XATTR_SET_CREDITS = 20,
430 MDD_TXN_INDEX_INSERT_CREDITS = 20,
431 MDD_TXN_INDEX_DELETE_CREDITS = 20,
432 MDD_TXN_LINK_CREDITS = 20,
433 MDD_TXN_UNLINK_CREDITS = 20,
434 MDD_TXN_RENAME_CREDITS = 20,
435 MDD_TXN_CREATE_DATA_CREDITS = 20,
436 MDD_TXN_MKDIR_CREDITS = 20
439 #define DEFINE_MDD_TXN_OP_DESC(opname) \
440 static const struct mdd_txn_op_descr opname = { \
441 .mod_op = opname ## _OP, \
442 .mod_credits = opname ## _CREDITS, \
446 * number of blocks to reserve for particular operations. Should be function
447 * of ... something. Stub for now.
449 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_DESTROY);
450 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_CREATE);
451 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_ATTR_SET);
452 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_XATTR_SET);
453 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_INSERT);
454 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_DELETE);
455 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_LINK);
456 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_UNLINK);
457 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_RENAME);
458 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_CREATE_DATA);
459 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_MKDIR);
461 static void mdd_txn_param_build(const struct lu_context *ctx,
462 const struct mdd_txn_op_descr *opd)
464 mdd_ctx_info(ctx)->mti_param.tp_credits = opd->mod_credits;
467 static int mdd_object_print(const struct lu_context *ctxt, void *cookie,
468 lu_printer_t p, const struct lu_object *o)
470 return (*p)(ctxt, cookie, LUSTRE_MDD0_NAME"-object@%p", o);
473 static int mdd_mount(const struct lu_context *ctx, struct mdd_device *mdd)
476 struct dt_object *root;
479 root = dt_store_open(ctx, mdd->mdd_child, mdd_root_dir_name,
482 LASSERT(root != NULL);
483 lu_object_put(ctx, &root->do_lu);
491 static int mdd_device_init(const struct lu_context *ctx,
492 struct lu_device *d, struct lu_device *next)
494 struct mdd_device *mdd = lu2mdd_dev(d);
498 mdd->mdd_child = lu2dt_dev(next);
503 static struct lu_device *mdd_device_fini(const struct lu_context *ctx,
506 struct mdd_device *m = lu2mdd_dev(d);
507 struct lu_device *next = &m->mdd_child->dd_lu_dev;
511 static void mdd_device_shutdown(const struct lu_context *ctxt,
512 struct mdd_device *m)
514 mdd_fini_obd(ctxt, m);
517 static int mdd_process_config(const struct lu_context *ctxt,
518 struct lu_device *d, struct lustre_cfg *cfg)
520 struct mdd_device *m = lu2mdd_dev(d);
521 struct dt_device *dt = m->mdd_child;
522 struct lu_device *next = &dt->dd_lu_dev;
523 char *dev = lustre_cfg_string(cfg, 0);
526 switch (cfg->lcfg_command) {
528 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
531 dt->dd_ops->dt_conf_get(ctxt, dt, &m->mdd_dt_conf);
533 rc = mdd_mount(ctxt, m);
536 rc = mdd_init_obd(ctxt, m, dev);
538 CERROR("lov init error %d \n", rc);
543 mdd_device_shutdown(ctxt, m);
545 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
552 static int mdd_recovery_complete(const struct lu_context *ctxt,
555 struct mdd_device *mdd = lu2mdd_dev(d);
556 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
560 rc = mdd_lov_set_nextid(ctx, mdd);
562 CERROR("%s: mdd_lov_set_nextid failed %d\n",
566 rc = mdd_cleanup_unlink_llog(ctx, mdd);
568 obd_notify(obd->u.mds.mds_osc_obd, NULL,
569 obd->obd_async_recov ? OBD_NOTIFY_SYNC_NONBLOCK :
570 OBD_NOTIFY_SYNC, NULL);
572 /* TODO: orphans handling */
573 rc = next->ld_ops->ldo_recovery_complete(ctxt, next);
577 struct lu_device_operations mdd_lu_ops = {
578 .ldo_object_alloc = mdd_object_alloc,
579 .ldo_process_config = mdd_process_config,
580 .ldo_recovery_complete = mdd_recovery_complete
583 static struct lu_object_operations mdd_lu_obj_ops = {
584 .loo_object_init = mdd_object_init,
585 .loo_object_start = mdd_object_start,
586 .loo_object_free = mdd_object_free,
587 .loo_object_print = mdd_object_print
590 void mdd_write_lock(const struct lu_context *ctxt, struct mdd_object *obj)
592 struct dt_object *next = mdd_object_child(obj);
594 next->do_ops->do_write_lock(ctxt, next);
597 void mdd_read_lock(const struct lu_context *ctxt, struct mdd_object *obj)
599 struct dt_object *next = mdd_object_child(obj);
601 next->do_ops->do_read_lock(ctxt, next);
604 void mdd_write_unlock(const struct lu_context *ctxt, struct mdd_object *obj)
606 struct dt_object *next = mdd_object_child(obj);
608 next->do_ops->do_write_unlock(ctxt, next);
611 void mdd_read_unlock(const struct lu_context *ctxt, struct mdd_object *obj)
613 struct dt_object *next = mdd_object_child(obj);
615 next->do_ops->do_read_unlock(ctxt, next);
618 static void mdd_lock2(const struct lu_context *ctxt,
619 struct mdd_object *o0, struct mdd_object *o1)
621 mdd_write_lock(ctxt, o0);
622 mdd_write_lock(ctxt, o1);
625 static void mdd_unlock2(const struct lu_context *ctxt,
626 struct mdd_object *o0, struct mdd_object *o1)
628 mdd_write_unlock(ctxt, o1);
629 mdd_write_unlock(ctxt, o0);
632 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
633 struct mdd_device *mdd)
635 struct txn_param *p = &mdd_ctx_info(ctxt)->mti_param;
637 return mdd_child_ops(mdd)->dt_trans_start(ctxt, mdd->mdd_child, p);
640 static void mdd_trans_stop(const struct lu_context *ctxt,
641 struct mdd_device *mdd, int result,
642 struct thandle *handle)
644 handle->th_result = result;
645 mdd_child_ops(mdd)->dt_trans_stop(ctxt, handle);
648 static int __mdd_object_create(const struct lu_context *ctxt,
649 struct mdd_object *obj, struct md_attr *ma,
650 struct thandle *handle)
652 struct dt_object *next;
653 struct lu_attr *attr = &ma->ma_attr;
657 if (!lu_object_exists(mdd2lu_obj(obj))) {
658 next = mdd_object_child(obj);
659 rc = next->do_ops->do_create(ctxt, next, attr, handle);
663 LASSERT(ergo(rc == 0, lu_object_exists(mdd2lu_obj(obj))));
668 int mdd_attr_set_internal(const struct lu_context *ctxt, struct mdd_object *o,
669 const struct lu_attr *attr, struct thandle *handle)
671 struct dt_object *next;
673 LASSERT(lu_object_exists(mdd2lu_obj(o)));
674 next = mdd_object_child(o);
675 return next->do_ops->do_attr_set(ctxt, next, attr, handle);
678 int mdd_attr_set_internal_locked(const struct lu_context *ctxt,
679 struct mdd_object *o,
680 const struct lu_attr *attr,
681 struct thandle *handle)
684 mdd_write_lock(ctxt, o);
685 rc = mdd_attr_set_internal(ctxt, o, attr, handle);
686 mdd_write_unlock(ctxt, o);
690 static int __mdd_xattr_set(const struct lu_context *ctxt, struct mdd_object *o,
691 const void *buf, int buf_len, const char *name,
692 int fl, struct thandle *handle)
694 struct dt_object *next;
698 LASSERT(lu_object_exists(mdd2lu_obj(o)));
699 next = mdd_object_child(o);
700 if (buf && buf_len > 0) {
701 rc = next->do_ops->do_xattr_set(ctxt, next, buf, buf_len, name,
703 }else if (buf == NULL && buf_len == 0) {
704 rc = next->do_ops->do_xattr_del(ctxt, next, name, handle);
708 /* this gives the same functionality as the code between
709 * sys_chmod and inode_setattr
710 * chown_common and inode_setattr
711 * utimes and inode_setattr
712 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
715 int mdd_fix_attr(const struct lu_context *ctxt, struct mdd_object *obj,
716 const struct md_attr *ma, struct lu_attr *la)
718 struct lu_attr *tmp_la = &mdd_ctx_info(ctxt)->mti_la;
719 time_t now = CURRENT_SECONDS;
723 rc = __mdd_la_get(ctxt, obj, tmp_la);
726 /*XXX Check permission */
727 if (mdd_is_immutable(obj) || mdd_is_append(obj)) {
729 /*If only change flags of the object, we should
730 * let it pass, but also need capability check
731 * here if (!capable(CAP_LINUX_IMMUTABLE)),
732 * fix it, when implement capable in mds*/
733 if (la->la_valid & ~LA_FLAGS)
736 /*According to Ext3 implementation on this, the
737 *Ctime will be changed, but not clear why?*/
739 la->la_valid |= LA_CTIME;
742 if (!(la->la_valid & LA_CTIME)) {
744 la->la_valid |= LA_CTIME;
746 /*According to original MDS implementation, it
747 * will clear ATTR_CTIME_SET flags, but seems
748 * no sense, should clear ATTR_CTIME? XXX*/
749 la->la_valid &= ~LA_CTIME;
751 if (!(la->la_valid & LA_ATIME)) {
753 la->la_valid |= LA_ATIME;
755 if (!(la->la_valid & LA_MTIME)) {
757 la->la_valid |= LA_MTIME;
763 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
764 if (current->fsuid != inode->i_uid &&
765 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
768 if (ia_valid & ATTR_SIZE &&
769 /* NFSD hack for open(O_CREAT|O_TRUNC)=mknod+truncate (bug 5781) */
770 !(rec->ur_uc.luc_fsuid == inode->i_uid &&
771 ia_valid & MDS_OPEN_OWNEROVERRIDE)) {
772 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
777 if (la->la_valid & (LA_UID | LA_GID)) {
780 if (mdd_is_immutable(obj) || mdd_is_append(obj))
782 if (la->la_uid == (uid_t) -1)
783 la->la_uid = tmp_la->la_uid;
784 if (la->la_gid == (gid_t) -1)
785 la->la_gid = tmp_la->la_gid;
786 if (!(la->la_valid & LA_MODE))
787 la->la_mode = tmp_la->la_mode;
789 * If the user or group of a non-directory has been
790 * changed by a non-root user, remove the setuid bit.
791 * 19981026 David C Niemi <niemi@tux.org>
793 * Changed this to apply to all users, including root,
794 * to avoid some races. This is the behavior we had in
795 * 2.0. The check for non-root was definitely wrong
796 * for 2.2 anyway, as it should have been using
797 * CAP_FSETID rather than fsuid -- 19990830 SD.
799 if ((tmp_la->la_mode & S_ISUID) == S_ISUID &&
800 !S_ISDIR(tmp_la->la_mode)) {
801 la->la_mode &= ~S_ISUID;
802 la->la_valid |= LA_MODE;
805 * Likewise, if the user or group of a non-directory
806 * has been changed by a non-root user, remove the
807 * setgid bit UNLESS there is no group execute bit
808 * (this would be a file marked for mandatory
809 * locking). 19981026 David C Niemi <niemi@tux.org>
811 * Removed the fsuid check (see the comment above) --
814 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
815 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
816 la->la_mode &= ~S_ISGID;
817 la->la_valid |= LA_MODE;
819 } else if (la->la_valid & LA_MODE) {
820 int mode = la->la_mode;
822 if (la->la_mode == (umode_t)-1)
823 mode = tmp_la->la_mode;
825 (mode & S_IALLUGO) | (tmp_la->la_mode & ~S_IALLUGO);
831 /* set attr and LOV EA at once, return updated attr */
832 static int mdd_attr_set(const struct lu_context *ctxt,
833 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 int rc = 0, lmm_size = 0, max_size;
841 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
844 mdd_txn_param_build(ctxt, &MDD_TXN_ATTR_SET);
845 handle = mdd_trans_start(ctxt, mdd);
847 RETURN(PTR_ERR(handle));
848 /*TODO: add lock here*/
849 /* start a log jounal handle if needed */
850 if (S_ISREG(mdd_object_type(mdd_obj)) &&
851 ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
852 max_size = mdd_lov_mdsize(ctxt, mdd);
853 OBD_ALLOC(lmm, max_size);
855 GOTO(cleanup, rc = -ENOMEM);
857 rc = mdd_get_md(ctxt, mdd_obj, lmm, &lmm_size, 1,
864 if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
865 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
866 ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
868 *la_copy = ma->ma_attr;
869 mdd_write_lock(ctxt, mdd_obj);
870 rc = mdd_fix_attr(ctxt, mdd_obj, ma, la_copy);
871 mdd_write_unlock(ctxt, mdd_obj);
875 if (la_copy->la_valid & LA_FLAGS) {
876 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
879 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
880 } else if (la_copy->la_valid) { /* setattr */
881 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
883 /* journal chown/chgrp in llog, just like unlink */
884 if (rc == 0 && lmm_size){
885 /*TODO set_attr llog */
889 if (rc == 0 && ma->ma_valid & MA_LOV) {
892 mode = mdd_object_type(mdd_obj);
893 if (S_ISREG(mode) || S_ISDIR(mode)) {
894 /*TODO check permission*/
895 rc = mdd_lov_set_md(ctxt, NULL, mdd_obj, ma->ma_lmm,
896 ma->ma_lmm_size, handle, 1);
901 mdd_trans_stop(ctxt, mdd, rc, handle);
902 if (rc == 0 && lmm_size) {
903 /*set obd attr, if needed*/
904 rc = mdd_lov_setattr_async(ctxt, mdd_obj, lmm, lmm_size);
907 OBD_FREE(lmm, max_size);
913 int mdd_xattr_set_txn(const struct lu_context *ctxt, struct mdd_object *obj,
914 const void *buf, int buf_len, const char *name, int fl,
915 struct thandle *handle)
920 mdd_write_lock(ctxt, obj);
921 rc = __mdd_xattr_set(ctxt, obj, buf, buf_len, name, fl, handle);
922 mdd_write_unlock(ctxt, obj);
927 static int mdd_xattr_set(const struct lu_context *ctxt, struct md_object *obj,
928 const void *buf, int buf_len, const char *name,
931 struct mdd_device *mdd = mdo2mdd(obj);
932 struct thandle *handle;
936 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
937 handle = mdd_trans_start(ctxt, mdd);
939 RETURN(PTR_ERR(handle));
941 rc = mdd_xattr_set_txn(ctxt, md2mdd_obj(obj), buf, buf_len, name,
944 mdd_trans_stop(ctxt, mdd, rc, handle);
949 static int __mdd_xattr_del(const struct lu_context *ctxt,struct mdd_device *mdd,
950 struct mdd_object *obj,
951 const char *name, struct thandle *handle)
953 struct dt_object *next;
955 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
956 next = mdd_object_child(obj);
957 return next->do_ops->do_xattr_del(ctxt, next, name, handle);
960 int mdd_xattr_del(const struct lu_context *ctxt, struct md_object *obj,
963 struct mdd_object *mdd_obj = md2mdd_obj(obj);
964 struct mdd_device *mdd = mdo2mdd(obj);
965 struct thandle *handle;
969 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
970 handle = mdd_trans_start(ctxt, mdd);
972 RETURN(PTR_ERR(handle));
974 mdd_write_lock(ctxt, mdd_obj);
975 rc = __mdd_xattr_del(ctxt, mdd, md2mdd_obj(obj), name, handle);
976 mdd_write_unlock(ctxt, mdd_obj);
978 mdd_trans_stop(ctxt, mdd, rc, handle);
983 static int __mdd_index_insert_only(const struct lu_context *ctxt,
984 struct mdd_object *pobj,
985 const struct lu_fid *lf,
986 const char *name, struct thandle *th)
989 struct dt_object *next = mdd_object_child(pobj);
992 if (dt_try_as_dir(ctxt, next))
993 rc = next->do_index_ops->dio_insert(ctxt, next,
995 (struct dt_key *)name, th);
1001 /* insert new index, add reference if isdir, update times */
1002 static int __mdd_index_insert(const struct lu_context *ctxt,
1003 struct mdd_object *pobj, const struct lu_fid *lf,
1004 const char *name, int isdir, struct thandle *th)
1007 struct dt_object *next = mdd_object_child(pobj);
1011 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
1014 if (dt_try_as_dir(ctxt, next))
1015 rc = next->do_index_ops->dio_insert(ctxt, next,
1016 (struct dt_rec *)lf,
1017 (struct dt_key *)name, th);
1023 __mdd_ref_add(ctxt, pobj, th);
1025 la->la_valid = LA_MTIME|LA_CTIME;
1026 la->la_atime = ma->ma_attr.la_atime;
1027 la->la_ctime = ma->ma_attr.la_ctime;
1028 rc = mdd_attr_set_internal(ctxt, mdd_obj, la, handle);
1034 static int __mdd_index_delete(const struct lu_context *ctxt,
1035 struct mdd_object *pobj, const char *name,
1036 struct thandle *handle)
1039 struct dt_object *next = mdd_object_child(pobj);
1042 if (dt_try_as_dir(ctxt, next))
1043 rc = next->do_index_ops->dio_delete(ctxt, next,
1044 (struct dt_key *)name, handle);
1050 static int mdd_link_sanity_check(const struct lu_context *ctxt,
1051 struct mdd_object *tgt_obj,
1052 struct mdd_object *src_obj)
1056 rc = mdd_may_create(ctxt, tgt_obj, NULL);
1059 if (S_ISDIR(mdd_object_type(src_obj)))
1062 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
1068 static int mdd_link(const struct lu_context *ctxt, struct md_object *tgt_obj,
1069 struct md_object *src_obj, const char *name)
1071 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1072 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1073 struct mdd_device *mdd = mdo2mdd(src_obj);
1074 struct thandle *handle;
1078 mdd_txn_param_build(ctxt, &MDD_TXN_LINK);
1079 handle = mdd_trans_start(ctxt, mdd);
1081 RETURN(PTR_ERR(handle));
1083 mdd_lock2(ctxt, mdd_tobj, mdd_sobj);
1085 rc = mdd_link_sanity_check(ctxt, mdd_tobj, mdd_sobj);
1089 rc = __mdd_index_insert_only(ctxt, mdd_tobj, mdo2fid(mdd_sobj),
1092 __mdd_ref_add(ctxt, mdd_sobj, handle);
1095 mdd_unlock2(ctxt, mdd_tobj, mdd_sobj);
1096 mdd_trans_stop(ctxt, mdd, rc, handle);
1101 * Check that @dir contains no entries except (possibly) dot and dotdot.
1106 * -ENOTEMPTY not empty
1110 static int mdd_dir_is_empty(const struct lu_context *ctx,
1111 struct mdd_object *dir)
1114 struct dt_object *obj;
1115 struct dt_it_ops *iops;
1118 obj = mdd_object_child(dir);
1119 iops = &obj->do_index_ops->dio_it;
1120 it = iops->init(ctx, obj);
1122 result = iops->get(ctx, it, (const void *)"");
1125 for (result = 0, i = 0; result == 0 && i < 3; ++i)
1126 result = iops->next(ctx, it);
1129 result = -ENOTEMPTY;
1130 else if (result == +1)
1132 } else if (result == 0)
1134 * Huh? Index contains no zero key?
1137 iops->fini(ctx, it);
1143 /* return md_attr back,
1144 * if it is last unlink then return lov ea + llog cookie*/
1145 static int __mdd_finish_unlink(const struct lu_context *ctxt,
1146 struct mdd_object *obj, struct md_attr *ma)
1151 rc = __mdd_iattr_get(ctxt, obj, ma);
1152 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
1153 if (atomic_read(&obj->mod_count) == 0) {
1154 mdd_set_dead_obj(obj);
1155 if (S_ISREG(mdd_object_type(obj))) {
1156 rc = __mdd_lmm_get(ctxt, obj, ma);
1157 if (ma->ma_valid & MA_LOV)
1158 rc = mdd_unlink_log(ctxt,
1159 mdo2mdd(&obj->mod_obj),
1163 /* add new orphan */
1164 //__mdd_orphan_add(ctxt, obj);
1170 static int mdd_unlink_sanity_check(const struct lu_context *ctxt,
1171 struct mdd_object *pobj,
1172 struct mdd_object *cobj,
1175 struct dt_object *dt_cobj = mdd_object_child(cobj);
1179 rc = mdd_may_delete(ctxt, pobj, cobj, S_ISDIR(ma->ma_attr.la_mode));
1183 if (S_ISDIR(mdd_object_type(cobj)) &&
1184 dt_try_as_dir(ctxt, dt_cobj)) {
1185 rc = mdd_dir_is_empty(ctxt, cobj);
1193 static int mdd_unlink(const struct lu_context *ctxt, struct md_object *pobj,
1194 struct md_object *cobj, const char *name,
1197 struct mdd_device *mdd = mdo2mdd(pobj);
1198 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1199 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1200 struct thandle *handle;
1204 mdd_txn_param_build(ctxt, &MDD_TXN_UNLINK);
1205 handle = mdd_trans_start(ctxt, mdd);
1207 RETURN(PTR_ERR(handle));
1209 mdd_lock2(ctxt, mdd_pobj, mdd_cobj);
1211 rc = mdd_unlink_sanity_check(ctxt, mdd_pobj, mdd_cobj, ma);
1216 rc = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1220 __mdd_ref_del(ctxt, mdd_cobj, handle);
1221 if (S_ISDIR(lu_object_attr(&cobj->mo_lu))) {
1223 __mdd_ref_del(ctxt, mdd_cobj, handle);
1225 __mdd_ref_del(ctxt, mdd_pobj, handle);
1228 rc = __mdd_finish_unlink(ctxt, mdd_cobj, ma);
1231 mdd_unlock2(ctxt, mdd_pobj, mdd_cobj);
1232 mdd_trans_stop(ctxt, mdd, rc, handle);
1236 static int mdd_parent_fid(const struct lu_context *ctxt,
1237 struct mdd_object *obj,
1240 return mdd_lookup(ctxt, &obj->mod_obj, dotdot, fid);
1244 * return 0: if p2 is the parent of p1
1245 * otherwise: other_value
1247 static int mdd_is_parent(const struct lu_context *ctxt,
1248 struct mdd_device *mdd,
1249 struct mdd_object *p1,
1250 struct mdd_object *p2)
1252 struct lu_fid * pfid;
1253 struct mdd_object *parent = NULL;
1257 pfid = &mdd_ctx_info(ctxt)->mti_fid;
1258 if (lu_fid_eq(mdo2fid(p1), &mdd->mdd_root_fid))
1261 rc = mdd_parent_fid(ctxt, p1, pfid);
1264 if (lu_fid_eq(pfid, mdo2fid(p2)))
1266 if (lu_fid_eq(pfid, &mdd->mdd_root_fid))
1269 mdd_object_put(ctxt, parent);
1270 parent = mdd_object_find(ctxt, mdd, pfid);
1272 GOTO(out, rc = PTR_ERR(parent));
1276 if (parent && !IS_ERR(parent))
1277 mdd_object_put(ctxt, parent);
1281 static int mdd_rename_lock(const struct lu_context *ctxt,
1282 struct mdd_device *mdd,
1283 struct mdd_object *src_pobj,
1284 struct mdd_object *tgt_pobj)
1288 if (src_pobj == tgt_pobj) {
1289 mdd_write_lock(ctxt, src_pobj);
1292 /*compared the parent child relationship of src_p&tgt_p*/
1293 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1294 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1296 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1297 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1301 if (!mdd_is_parent(ctxt, mdd, src_pobj, tgt_pobj)) {
1302 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1306 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1311 static void mdd_rename_unlock(const struct lu_context *ctxt,
1312 struct mdd_object *src_pobj,
1313 struct mdd_object *tgt_pobj)
1315 mdd_write_unlock(ctxt, src_pobj);
1316 if (src_pobj != tgt_pobj)
1317 mdd_write_unlock(ctxt, tgt_pobj);
1320 static int mdd_rename_sanity_check(const struct lu_context *ctxt,
1321 struct mdd_object *src_pobj,
1322 struct mdd_object *tgt_pobj,
1323 struct mdd_object *sobj,
1324 struct mdd_object *tobj)
1326 struct mdd_device *mdd =mdo2mdd(&src_pobj->mod_obj);
1327 int rc = 0, src_is_dir, tgt_is_dir;
1330 src_is_dir = S_ISDIR(mdd_object_type(sobj));
1331 rc = mdd_may_delete(ctxt, src_pobj, sobj, src_is_dir);
1336 rc = mdd_may_create(ctxt, tgt_pobj, NULL);
1339 if (!mdd_is_parent(ctxt, mdd, tgt_pobj, sobj))
1340 GOTO(out, rc = -EINVAL);
1344 /* source should not be ancestor of target */
1345 if (!mdd_is_parent(ctxt, mdd, tobj, sobj))
1346 GOTO(out, rc = -EINVAL);
1348 rc = mdd_may_delete(ctxt, tgt_pobj, tobj, src_is_dir);
1352 tgt_is_dir = S_ISDIR(mdd_object_type(tobj));
1353 if (tgt_is_dir && mdd_dir_is_empty(ctxt, tobj))
1354 GOTO(out, rc = -ENOTEMPTY);
1356 mdd_object_put(ctxt, sobj);
1360 static int mdd_rename(const struct lu_context *ctxt, struct md_object *src_pobj,
1361 struct md_object *tgt_pobj, const struct lu_fid *lf,
1362 const char *sname, struct md_object *tobj,
1363 const char *tname, struct md_attr *ma)
1365 struct mdd_device *mdd = mdo2mdd(src_pobj);
1366 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj);
1367 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1368 struct mdd_object *mdd_sobj = mdd_object_find(ctxt, mdd, lf);
1369 struct mdd_object *mdd_tobj = NULL;
1370 struct thandle *handle;
1371 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
1376 mdd_tobj = md2mdd_obj(tobj);
1378 /*XXX: shouldn't this check be done under lock below? */
1379 rc = mdd_rename_sanity_check(ctxt, mdd_spobj, mdd_tpobj,
1380 mdd_sobj, mdd_tobj);
1384 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1385 handle = mdd_trans_start(ctxt, mdd);
1387 RETURN(PTR_ERR(handle));
1389 /*FIXME: Should consider tobj and sobj too in rename_lock*/
1390 rc = mdd_rename_lock(ctxt, mdd, mdd_spobj, mdd_tpobj);
1392 GOTO(cleanup_unlocked, rc);
1394 rc = __mdd_index_delete(ctxt, mdd_spobj, sname, handle);
1398 /*if sobj is dir, its parent object nlink should be dec too*/
1400 __mdd_ref_del(ctxt, mdd_spobj, handle);
1403 rc = __mdd_index_delete(ctxt, mdd_tpobj, tname, handle);
1408 rc = __mdd_index_insert(ctxt, mdd_tpobj, lf, tname, is_dir, handle);
1412 if (tobj && lu_object_exists(&tobj->mo_lu)) {
1413 mdd_write_lock(ctxt, mdd_tobj);
1414 __mdd_ref_del(ctxt, mdd_tobj, handle);
1415 /* remove dot reference */
1417 __mdd_ref_del(ctxt, mdd_tobj, handle);
1419 rc = __mdd_finish_unlink(ctxt, mdd_tobj, ma);
1420 mdd_write_unlock(ctxt, mdd_tobj);
1423 mdd_rename_unlock(ctxt, mdd_spobj, mdd_tpobj);
1425 mdd_trans_stop(ctxt, mdd, rc, handle);
1429 static int mdd_lookup(const struct lu_context *ctxt, struct md_object *pobj,
1430 const char *name, struct lu_fid* fid)
1432 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1433 struct dt_object *dir = mdd_object_child(mdd_obj);
1434 struct dt_rec *rec = (struct dt_rec *)fid;
1435 const struct dt_key *key = (const struct dt_key *)name;
1439 if (mdd_is_dead_obj(mdd_obj))
1441 mdd_read_lock(ctxt, mdd_obj);
1442 if (S_ISDIR(mdd_object_type(mdd_obj)) && dt_try_as_dir(ctxt, dir))
1443 rc = dir->do_index_ops->dio_lookup(ctxt, dir, rec, key);
1446 mdd_read_unlock(ctxt, mdd_obj);
1450 static int __mdd_object_initialize(const struct lu_context *ctxt,
1451 const struct lu_fid *pfid,
1452 struct mdd_object *child,
1453 struct md_attr *ma, struct thandle *handle)
1458 /* update attributes for child.
1460 * (1) the valid bits should be converted between Lustre and Linux;
1461 * (2) maybe, the child attributes should be set in OSD when creation.
1464 rc = mdd_attr_set_internal(ctxt, child, &ma->ma_attr, handle);
1468 if (S_ISDIR(ma->ma_attr.la_mode)) {
1469 /* add . and .. for newly created dir */
1470 __mdd_ref_add(ctxt, child, handle);
1471 rc = __mdd_index_insert_only(ctxt, child, mdo2fid(child),
1474 rc = __mdd_index_insert_only(ctxt, child, pfid,
1479 rc2 = __mdd_index_delete(ctxt,
1480 child, dot, handle);
1482 CERROR("Failure to cleanup after dotdot"
1483 " creation: %d (%d)\n", rc2, rc);
1485 __mdd_ref_del(ctxt, child, handle);
1492 static int mdd_create_data(const struct lu_context *ctxt,
1493 struct md_object *pobj, struct md_object *cobj,
1494 const struct md_create_spec *spec,
1497 struct mdd_device *mdd = mdo2mdd(pobj);
1498 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1499 struct mdd_object *son = md2mdd_obj(cobj);
1500 struct lu_attr *attr = &ma->ma_attr;
1501 struct lov_mds_md *lmm = NULL;
1503 struct thandle *handle;
1507 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size, spec,
1512 mdd_txn_param_build(ctxt, &MDD_TXN_CREATE_DATA);
1513 handle = mdd_trans_start(ctxt, mdd);
1515 RETURN(PTR_ERR(handle));
1517 /*XXX: setting the lov ea is not locked but setting the attr is locked? */
1519 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size,
1522 rc = mdd_attr_get_internal_locked(ctxt, son, ma);
1525 mdd_trans_stop(ctxt, mdd, rc, handle);
1529 static int mdd_create_sanity_check(const struct lu_context *ctxt,
1530 struct mdd_device *mdd,
1531 struct md_object *pobj,
1532 const char *name, struct md_attr *ma)
1534 struct mdd_thread_info *info = mdd_ctx_info(ctxt);
1535 struct lu_attr *la = &info->mti_la;
1536 struct lu_fid *fid = &info->mti_fid;
1537 struct mdd_object *obj = md2mdd_obj(pobj);
1542 if (mdd_is_dead_obj(obj))
1544 rc = mdd_lookup(ctxt, pobj, name, fid);
1546 RETURN(rc ? : -EEXIST);
1549 mdd_read_lock(ctxt, obj);
1550 rc = __mdd_la_get(ctxt, obj, la);
1551 mdd_read_unlock(ctxt, obj);
1555 if (la->la_mode & S_ISGID) {
1556 ma->ma_attr.la_gid = la->la_gid;
1557 if (S_ISDIR(ma->ma_attr.la_mode)) {
1558 ma->ma_attr.la_mode |= S_ISGID;
1559 ma->ma_attr.la_valid |= LA_MODE;
1563 switch (ma->ma_attr.la_mode & S_IFMT) {
1581 * Create object and insert it into namespace.
1583 static int mdd_create(const struct lu_context *ctxt, struct md_object *pobj,
1584 const char *name, struct md_object *child,
1585 const struct md_create_spec *spec,
1588 struct mdd_device *mdd = mdo2mdd(pobj);
1589 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1590 struct mdd_object *son = md2mdd_obj(child);
1591 struct lu_attr *attr = &ma->ma_attr;
1592 struct lov_mds_md *lmm = NULL;
1593 struct thandle *handle;
1594 int rc, created = 0, inserted = 0, lmm_size = 0;
1597 /* sanity checks before big job */
1598 rc = mdd_create_sanity_check(ctxt, mdd, pobj, name, ma);
1601 /* no RPC inside the transaction, so OST objects should be created at
1603 if (S_ISREG(attr->la_mode)) {
1604 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size,
1610 mdd_txn_param_build(ctxt, &MDD_TXN_MKDIR);
1611 handle = mdd_trans_start(ctxt, mdd);
1613 RETURN(PTR_ERR(handle));
1615 mdd_write_lock(ctxt, mdd_pobj);
1618 * XXX check that link can be added to the parent in mkdir case.
1622 * Two operations have to be performed:
1624 * - allocation of new object (->do_create()), and
1626 * - insertion into parent index (->dio_insert()).
1628 * Due to locking, operation order is not important, when both are
1629 * successful, *but* error handling cases are quite different:
1631 * - if insertion is done first, and following object creation fails,
1632 * insertion has to be rolled back, but this operation might fail
1633 * also leaving us with dangling index entry.
1635 * - if creation is done first, is has to be undone if insertion
1636 * fails, leaving us with leaked space, which is neither good, nor
1639 * It seems that creation-first is simplest solution, but it is
1640 * sub-optimal in the frequent
1645 * case, because second mkdir is bound to create object, only to
1646 * destroy it immediately.
1648 * Note that local file systems do
1650 * 0. lookup -> -EEXIST
1656 * Maybe we should do the same. For now: creation-first.
1659 mdd_write_lock(ctxt, son);
1660 rc = __mdd_object_create(ctxt, son, ma, handle);
1662 mdd_write_unlock(ctxt, son);
1668 rc = __mdd_object_initialize(ctxt, mdo2fid(mdd_pobj),
1670 mdd_write_unlock(ctxt, son);
1673 * Object has no links, so it will be destroyed when last
1674 * reference is released. (XXX not now.)
1678 rc = __mdd_index_insert(ctxt, mdd_pobj, mdo2fid(son),
1679 name, S_ISDIR(attr->la_mode), handle);
1685 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size, handle, 0);
1687 CERROR("error on stripe info copy %d \n", rc);
1691 if (S_ISLNK(attr->la_mode)) {
1692 struct dt_object *dt = mdd_object_child(son);
1693 const char *target_name = spec->u.sp_symname;
1694 int sym_len = strlen(target_name);
1697 rc = dt->do_body_ops->dbo_write(ctxt, dt, target_name,
1698 sym_len, &pos, handle);
1704 /* return attr back */
1705 rc = mdd_attr_get_internal_locked(ctxt, son, ma);
1707 if (rc && created) {
1711 rc2 = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1713 CERROR("error can not cleanup destroy %d\n",
1717 __mdd_ref_del(ctxt, son, handle);
1720 OBD_FREE(lmm, lmm_size);
1721 mdd_write_unlock(ctxt, mdd_pobj);
1722 mdd_trans_stop(ctxt, mdd, rc, handle);
1725 /* partial operation */
1726 static int mdd_object_create(const struct lu_context *ctxt,
1727 struct md_object *obj,
1728 const struct md_create_spec *spec,
1732 struct mdd_device *mdd = mdo2mdd(obj);
1733 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1734 struct thandle *handle;
1738 mdd_txn_param_build(ctxt, &MDD_TXN_OBJECT_CREATE);
1739 handle = mdd_trans_start(ctxt, mdd);
1741 RETURN(PTR_ERR(handle));
1743 mdd_write_lock(ctxt, mdd_obj);
1744 rc = __mdd_object_create(ctxt, mdd_obj, ma, handle);
1746 rc = __mdd_object_initialize(ctxt, spec->u.sp_pfid, mdd_obj,
1748 mdd_write_unlock(ctxt, mdd_obj);
1751 rc = mdd_attr_get_internal_locked(ctxt, mdd_obj, ma);
1753 mdd_trans_stop(ctxt, mdd, rc, handle);
1756 /* partial operation */
1757 static int mdd_name_insert(const struct lu_context *ctxt,
1758 struct md_object *pobj, const char *name,
1759 const struct lu_fid *fid, int isdir)
1761 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1762 struct thandle *handle;
1766 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_INSERT);
1767 handle = mdd_trans_start(ctxt, mdo2mdd(pobj));
1769 RETURN(PTR_ERR(handle));
1771 mdd_write_lock(ctxt, mdd_obj);
1772 rc = __mdd_index_insert(ctxt, mdd_obj, fid, name, isdir, handle);
1773 mdd_write_unlock(ctxt, mdd_obj);
1775 mdd_trans_stop(ctxt, mdo2mdd(pobj), rc, handle);
1779 static int mdd_name_remove(const struct lu_context *ctxt,
1780 struct md_object *pobj,
1783 struct mdd_device *mdd = mdo2mdd(pobj);
1784 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1785 struct thandle *handle;
1789 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_DELETE);
1790 handle = mdd_trans_start(ctxt, mdd);
1792 RETURN(PTR_ERR(handle));
1794 mdd_write_lock(ctxt, mdd_obj);
1796 rc = __mdd_index_delete(ctxt, mdd_obj, name, handle);
1798 mdd_write_unlock(ctxt, mdd_obj);
1800 mdd_trans_stop(ctxt, mdd, rc, handle);
1804 static int mdd_rename_tgt(const struct lu_context *ctxt, struct md_object *pobj,
1805 struct md_object *tobj, const struct lu_fid *lf,
1808 struct mdd_device *mdd = mdo2mdd(pobj);
1809 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1810 struct mdd_object *mdd_tobj = NULL;
1811 struct thandle *handle;
1815 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1816 handle = mdd_trans_start(ctxt, mdd);
1818 RETURN(PTR_ERR(handle));
1821 mdd_tobj = md2mdd_obj(tobj);
1823 mdd_lock2(ctxt, mdd_tpobj, mdd_tobj);
1825 /*TODO rename sanity checking*/
1827 rc = __mdd_index_delete(ctxt, mdd_tpobj, name, handle);
1832 rc = __mdd_index_insert_only(ctxt, mdd_tpobj, lf, name, handle);
1836 if (tobj && lu_object_exists(&tobj->mo_lu))
1837 __mdd_ref_del(ctxt, mdd_tobj, handle);
1839 mdd_unlock2(ctxt, mdd_tpobj, mdd_tobj);
1840 mdd_trans_stop(ctxt, mdd, rc, handle);
1844 static int mdd_root_get(const struct lu_context *ctx,
1845 struct md_device *m, struct lu_fid *f)
1847 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1850 *f = mdd->mdd_root_fid;
1854 static int mdd_statfs(const struct lu_context *ctx,
1855 struct md_device *m, struct kstatfs *sfs)
1857 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1862 rc = mdd_child_ops(mdd)->dt_statfs(ctx, mdd->mdd_child, sfs);
1867 static int mdd_get_maxsize(const struct lu_context *ctx,
1868 struct md_device *m, int *md_size,
1871 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1874 *md_size = mdd_lov_mdsize(ctx, mdd);
1875 *cookie_size = mdd_lov_cookiesize(ctx, mdd);
1880 static void __mdd_ref_add(const struct lu_context *ctxt, struct mdd_object *obj,
1881 struct thandle *handle)
1883 struct dt_object *next;
1885 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
1886 next = mdd_object_child(obj);
1887 next->do_ops->do_ref_add(ctxt, next, handle);
1890 static int mdd_ref_add(const struct lu_context *ctxt, struct md_object *obj)
1892 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1893 struct mdd_device *mdd = mdo2mdd(obj);
1894 struct thandle *handle;
1897 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1898 handle = mdd_trans_start(ctxt, mdd);
1902 mdd_write_lock(ctxt, mdd_obj);
1903 __mdd_ref_add(ctxt, mdd_obj, handle);
1904 mdd_write_unlock(ctxt, mdd_obj);
1906 mdd_trans_stop(ctxt, mdd, 0, handle);
1912 __mdd_ref_del(const struct lu_context *ctxt, struct mdd_object *obj,
1913 struct thandle *handle)
1915 struct dt_object *next = mdd_object_child(obj);
1917 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
1919 next->do_ops->do_ref_del(ctxt, next, handle);
1922 static int mdd_ref_del(const struct lu_context *ctxt, struct md_object *obj,
1925 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1926 struct mdd_device *mdd = mdo2mdd(obj);
1927 struct thandle *handle;
1932 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1933 handle = mdd_trans_start(ctxt, mdd);
1937 mdd_write_lock(ctxt, mdd_obj);
1939 isdir = S_ISDIR(lu_object_attr(&obj->mo_lu));
1941 if (isdir && dt_try_as_dir(ctxt, mdd_object_child(mdd_obj))) {
1942 rc = mdd_dir_is_empty(ctxt, mdd_obj);
1947 __mdd_ref_del(ctxt, mdd_obj, handle);
1951 __mdd_ref_del(ctxt, mdd_obj, handle);
1954 rc = __mdd_finish_unlink(ctxt, mdd_obj, ma);
1957 mdd_write_unlock(ctxt, mdd_obj);
1958 mdd_trans_stop(ctxt, mdd, rc, handle);
1962 /* do NOT or the MAY_*'s, you'll get the weakest */
1963 static int accmode(struct mdd_object *mdd_obj, int flags)
1968 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1969 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1970 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1971 * owner can write to a file even if it is marked readonly to hide
1972 * its brokenness. (bug 5781) */
1973 if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
1976 if (flags & FMODE_READ)
1978 if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
1980 if (flags & MDS_FMODE_EXEC)
1985 static int mdd_open(const struct lu_context *ctxt, struct md_object *obj,
1988 int mode = accmode(md2mdd_obj(obj), flags);
1990 if (mode & MAY_WRITE) {
1991 if (mdd_is_immutable(md2mdd_obj(obj)))
1995 atomic_inc(&md2mdd_obj(obj)->mod_count);
1999 static int mdd_close(const struct lu_context *ctxt, struct md_object *obj,
2003 struct mdd_object *mdd_obj;
2005 mdd_obj = md2mdd_obj(obj);
2006 if (atomic_dec_and_test(&mdd_obj->mod_count)) {
2007 /*TODO: Remove it from orphan list */
2010 mdd_read_lock(ctxt, mdd_obj);
2011 rc = __mdd_finish_unlink(ctxt, mdd_obj, ma);
2012 mdd_read_unlock(ctxt, mdd_obj);
2016 static int mdd_readpage(const struct lu_context *ctxt, struct md_object *obj,
2017 const struct lu_rdpg *rdpg)
2019 struct dt_object *next;
2020 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2023 LASSERT(lu_object_exists(mdd2lu_obj(mdd_obj)));
2024 next = mdd_object_child(mdd_obj);
2026 mdd_read_lock(ctxt, mdd_obj);
2027 if (S_ISDIR(mdd_object_type(mdd_obj)) &&
2028 dt_try_as_dir(ctxt, next))
2029 rc = next->do_ops->do_readpage(ctxt, next, rdpg);
2032 mdd_read_unlock(ctxt, mdd_obj);
2036 struct md_device_operations mdd_ops = {
2037 .mdo_root_get = mdd_root_get,
2038 .mdo_statfs = mdd_statfs,
2039 .mdo_get_maxsize = mdd_get_maxsize,
2042 static struct md_dir_operations mdd_dir_ops = {
2043 .mdo_lookup = mdd_lookup,
2044 .mdo_create = mdd_create,
2045 .mdo_rename = mdd_rename,
2046 .mdo_link = mdd_link,
2047 .mdo_unlink = mdd_unlink,
2048 .mdo_name_insert = mdd_name_insert,
2049 .mdo_name_remove = mdd_name_remove,
2050 .mdo_rename_tgt = mdd_rename_tgt,
2051 .mdo_create_data = mdd_create_data
2055 static struct md_object_operations mdd_obj_ops = {
2056 .moo_attr_get = mdd_attr_get,
2057 .moo_attr_set = mdd_attr_set,
2058 .moo_xattr_get = mdd_xattr_get,
2059 .moo_xattr_set = mdd_xattr_set,
2060 .moo_xattr_list = mdd_xattr_list,
2061 .moo_xattr_del = mdd_xattr_del,
2062 .moo_object_create = mdd_object_create,
2063 .moo_ref_add = mdd_ref_add,
2064 .moo_ref_del = mdd_ref_del,
2065 .moo_open = mdd_open,
2066 .moo_close = mdd_close,
2067 .moo_readpage = mdd_readpage,
2068 .moo_readlink = mdd_readlink
2071 static struct obd_ops mdd_obd_device_ops = {
2072 .o_owner = THIS_MODULE
2075 static struct lu_device *mdd_device_alloc(const struct lu_context *ctx,
2076 struct lu_device_type *t,
2077 struct lustre_cfg *lcfg)
2079 struct lu_device *l;
2080 struct mdd_device *m;
2084 l = ERR_PTR(-ENOMEM);
2086 md_device_init(&m->mdd_md_dev, t);
2088 l->ld_ops = &mdd_lu_ops;
2089 m->mdd_md_dev.md_ops = &mdd_ops;
2095 static void mdd_device_free(const struct lu_context *ctx,
2096 struct lu_device *lu)
2098 struct mdd_device *m = lu2mdd_dev(lu);
2100 LASSERT(atomic_read(&lu->ld_ref) == 0);
2101 md_device_fini(&m->mdd_md_dev);
2105 static int mdd_type_init(struct lu_device_type *t)
2107 return lu_context_key_register(&mdd_thread_key);
2110 static void mdd_type_fini(struct lu_device_type *t)
2112 lu_context_key_degister(&mdd_thread_key);
2115 static struct lu_device_type_operations mdd_device_type_ops = {
2116 .ldto_init = mdd_type_init,
2117 .ldto_fini = mdd_type_fini,
2119 .ldto_device_alloc = mdd_device_alloc,
2120 .ldto_device_free = mdd_device_free,
2122 .ldto_device_init = mdd_device_init,
2123 .ldto_device_fini = mdd_device_fini
2126 static struct lu_device_type mdd_device_type = {
2127 .ldt_tags = LU_DEVICE_MD,
2128 .ldt_name = LUSTRE_MDD0_NAME,
2129 .ldt_ops = &mdd_device_type_ops,
2130 .ldt_ctx_tags = LCT_MD_THREAD
2133 static void *mdd_key_init(const struct lu_context *ctx,
2134 struct lu_context_key *key)
2136 struct mdd_thread_info *info;
2138 OBD_ALLOC_PTR(info);
2140 info = ERR_PTR(-ENOMEM);
2144 static void mdd_key_fini(const struct lu_context *ctx,
2145 struct lu_context_key *key, void *data)
2147 struct mdd_thread_info *info = data;
2151 static struct lu_context_key mdd_thread_key = {
2152 .lct_tags = LCT_MD_THREAD,
2153 .lct_init = mdd_key_init,
2154 .lct_fini = mdd_key_fini
2157 struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
2161 struct lprocfs_vars lprocfs_mdd_module_vars[] = {
2165 LPROCFS_INIT_VARS(mdd, lprocfs_mdd_module_vars, lprocfs_mdd_obd_vars);
2167 static int __init mdd_mod_init(void)
2169 struct lprocfs_static_vars lvars;
2171 lprocfs_init_vars(mdd, &lvars);
2172 return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
2173 LUSTRE_MDD0_NAME, &mdd_device_type);
2176 static void __exit mdd_mod_exit(void)
2178 class_unregister_type(LUSTRE_MDD0_NAME);
2181 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2182 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD0_NAME")");
2183 MODULE_LICENSE("GPL");
2185 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);