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>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
41 #include <lu_object.h>
42 #include <md_object.h>
43 #include <dt_object.h>
45 #include "mdd_internal.h"
48 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
50 static void mdd_trans_stop(const struct lu_context *ctxt,
51 struct mdd_device *mdd, struct thandle *handle);
52 static struct dt_object* mdd_object_child(struct mdd_object *o);
53 static void __mdd_ref_add(const struct lu_context *ctxt, struct mdd_object *obj,
54 struct thandle *handle);
55 static void __mdd_ref_del(const struct lu_context *ctxt, struct mdd_object *obj,
56 struct thandle *handle);
57 static int mdd_lookup(const struct lu_context *ctxt, struct md_object *pobj,
58 const char *name, struct lu_fid* fid);
60 static struct md_object_operations mdd_obj_ops;
61 static struct md_dir_operations mdd_dir_ops;
62 static struct lu_object_operations mdd_lu_obj_ops;
64 static struct lu_context_key mdd_thread_key;
66 static const char *mdd_root_dir_name = "root";
67 static const char dot[] = ".";
68 static const char dotdot[] = "..";
71 struct mdd_thread_info *mdd_ctx_info(const struct lu_context *ctx)
73 struct mdd_thread_info *info;
75 info = lu_context_key_get(ctx, &mdd_thread_key);
76 LASSERT(info != NULL);
80 static struct lu_object *mdd_object_alloc(const struct lu_context *ctxt,
81 const struct lu_object_header *hdr,
84 struct mdd_object *mdd_obj;
86 OBD_ALLOC_PTR(mdd_obj);
87 if (mdd_obj != NULL) {
90 o = mdd2lu_obj(mdd_obj);
91 lu_object_init(o, NULL, d);
92 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
93 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
94 atomic_set(&mdd_obj->mod_count, 0);
95 o->lo_ops = &mdd_lu_obj_ops;
102 static int mdd_object_init(const struct lu_context *ctxt, struct lu_object *o)
104 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
105 struct lu_object *below;
106 struct lu_device *under;
109 under = &d->mdd_child->dd_lu_dev;
110 below = under->ld_ops->ldo_object_alloc(ctxt, o->lo_header, under);
115 lu_object_add(o, below);
119 static int mdd_get_flags(const struct lu_context *ctxt, struct mdd_object *obj);
121 static int mdd_object_start(const struct lu_context *ctxt, struct lu_object *o)
123 if (lu_object_exists(ctxt, o))
124 return mdd_get_flags(ctxt, lu2mdd_obj(o));
129 static void mdd_object_free(const struct lu_context *ctxt, struct lu_object *o)
131 struct mdd_object *mdd = lu2mdd_obj(o);
137 struct mdd_object *mdd_object_find(const struct lu_context *ctxt,
138 struct mdd_device *d,
139 const struct lu_fid *f)
142 struct mdd_object *m;
145 o = lu_object_find(ctxt, d->mdd_md_dev.md_lu_dev.ld_site, f);
147 m = (struct mdd_object *)o;
149 m = lu2mdd_obj(lu_object_locate(o->lo_header,
150 d->mdd_md_dev.md_lu_dev.ld_type));
154 static inline void mdd_object_put(const struct lu_context *ctxt,
155 struct mdd_object *o)
157 lu_object_put(ctxt, &o->mod_obj.mo_lu);
160 static inline int mdd_is_immutable(struct mdd_object *obj)
162 return obj->mod_flags & IMMUTE_OBJ;
165 static inline int mdd_is_append(struct mdd_object *obj)
167 return obj->mod_flags & APPEND_OBJ;
170 static void mdd_set_dead_obj(struct mdd_object *obj)
173 obj->mod_flags |= DEAD_OBJ;
176 static int mdd_is_dead_obj(struct mdd_object *obj)
178 return obj && obj->mod_flags & DEAD_OBJ;
181 /*Check whether it may create the cobj under the pobj*/
182 static int mdd_may_create(const struct lu_context *ctxt,
183 struct mdd_object *pobj, struct mdd_object *cobj)
186 if (cobj && lu_object_exists(ctxt, &cobj->mod_obj.mo_lu))
189 if (mdd_is_dead_obj(pobj))
192 /*check pobj may create or not*/
196 static inline int __mdd_la_get(const struct lu_context *ctxt,
197 struct mdd_object *obj, struct lu_attr *la)
199 struct dt_object *next = mdd_object_child(obj);
200 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(obj)));
201 return next->do_ops->do_attr_get(ctxt, next, la);
205 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
208 if (flags & S_APPEND)
209 obj->mod_flags |= APPEND_OBJ;
211 if (flags & S_IMMUTABLE)
212 obj->mod_flags |= IMMUTE_OBJ;
215 static int mdd_get_flags(const struct lu_context *ctxt, struct mdd_object *obj)
217 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
221 rc = __mdd_la_get(ctxt, obj, la);
223 mdd_flags_xlate(obj, la->la_flags);
227 /*Check whether it may delete the cobj under the pobj*/
228 static int mdd_may_delete(const struct lu_context *ctxt,
229 struct mdd_object *pobj, struct mdd_object *cobj,
232 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
236 LASSERT(cobj && pobj);
238 if (!lu_object_exists(ctxt, &cobj->mod_obj.mo_lu))
241 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
245 if (!S_ISDIR(mdd_object_type(ctxt, cobj)))
248 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
251 } else if (S_ISDIR(mdd_object_type(ctxt, cobj)))
254 if (mdd_is_dead_obj(pobj))
259 /* get only inode attributes */
260 static int __mdd_iattr_get(const struct lu_context *ctxt,
261 struct mdd_object *mdd_obj, struct md_attr *ma)
266 rc = __mdd_la_get(ctxt, mdd_obj, &ma->ma_attr);
268 ma->ma_valid = MA_INODE;
271 /* get lov EA only */
272 static int __mdd_lmm_get(const struct lu_context *ctxt,
273 struct mdd_object *mdd_obj, struct md_attr *ma)
277 LASSERT(ma->ma_lmm != NULL && ma->ma_lmm_size > 0);
278 rc = mdd_get_md(ctxt, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size, 0);
280 ma->ma_valid |= MA_LOV;
286 static int mdd_attr_get_internal(const struct lu_context *ctxt,
287 struct mdd_object *mdd_obj,
293 if (ma->ma_need & MA_INODE)
294 rc = __mdd_iattr_get(ctxt, mdd_obj, ma);
296 if (rc == 0 && ma->ma_need & MA_LOV) {
297 if (S_ISREG(lu_object_attr(ctxt, mdd2lu_obj(mdd_obj))) ||
298 S_ISDIR(lu_object_attr(ctxt, mdd2lu_obj(mdd_obj)))) {
299 rc = __mdd_lmm_get(ctxt, mdd_obj, ma);
303 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
308 static inline int mdd_attr_get_internal_locked (const struct lu_context *ctxt,
309 struct mdd_object *mdd_obj,
313 mdd_lock(ctxt, mdd_obj, DT_READ_LOCK);
314 rc = mdd_attr_get_internal(ctxt, mdd_obj, ma);
315 mdd_unlock(ctxt, mdd_obj, DT_READ_LOCK);
319 static int mdd_attr_get(const struct lu_context *ctxt,
320 struct md_object *obj, struct md_attr *ma)
322 struct mdd_object *mdd_obj = md2mdd_obj(obj);
326 rc = mdd_attr_get_internal_locked(ctxt, mdd_obj, ma);
330 static int mdd_xattr_get(const struct lu_context *ctxt, struct md_object *obj,
331 void *buf, int buf_len, const char *name)
333 struct mdd_object *mdd_obj = md2mdd_obj(obj);
334 struct dt_object *next;
339 LASSERT(lu_object_exists(ctxt, &obj->mo_lu));
341 next = mdd_object_child(mdd_obj);
342 rc = next->do_ops->do_xattr_get(ctxt, next, buf, buf_len, name);
347 static int mdd_readlink(const struct lu_context *ctxt, struct md_object *obj,
348 void *buf, int buf_len)
350 struct mdd_object *mdd_obj = md2mdd_obj(obj);
351 struct dt_object *next;
356 LASSERT(lu_object_exists(ctxt, &obj->mo_lu));
358 next = mdd_object_child(mdd_obj);
359 rc = next->do_body_ops->dbo_read(ctxt, next, buf, buf_len, &pos);
362 static int mdd_xattr_list(const struct lu_context *ctxt, struct md_object *obj,
363 void *buf, int buf_len)
365 struct mdd_object *mdd_obj = md2mdd_obj(obj);
366 struct dt_object *next;
371 LASSERT(lu_object_exists(ctxt, &obj->mo_lu));
373 next = mdd_object_child(mdd_obj);
374 rc = next->do_ops->do_xattr_list(ctxt, next, buf, buf_len);
380 MDD_TXN_OBJECT_DESTROY_OP,
381 MDD_TXN_OBJECT_CREATE_OP,
383 MDD_TXN_XATTR_SET_OP,
384 MDD_TXN_INDEX_INSERT_OP,
385 MDD_TXN_INDEX_DELETE_OP,
389 MDD_TXN_CREATE_DATA_OP,
393 struct mdd_txn_op_descr {
394 enum mdd_txn_op mod_op;
395 unsigned int mod_credits;
399 MDD_TXN_OBJECT_DESTROY_CREDITS = 20,
400 MDD_TXN_OBJECT_CREATE_CREDITS = 20,
401 MDD_TXN_ATTR_SET_CREDITS = 20,
402 MDD_TXN_XATTR_SET_CREDITS = 20,
403 MDD_TXN_INDEX_INSERT_CREDITS = 20,
404 MDD_TXN_INDEX_DELETE_CREDITS = 20,
405 MDD_TXN_LINK_CREDITS = 20,
406 MDD_TXN_UNLINK_CREDITS = 20,
407 MDD_TXN_RENAME_CREDITS = 20,
408 MDD_TXN_CREATE_DATA_CREDITS = 20,
409 MDD_TXN_MKDIR_CREDITS = 20
412 #define DEFINE_MDD_TXN_OP_DESC(opname) \
413 static const struct mdd_txn_op_descr opname = { \
414 .mod_op = opname ## _OP, \
415 .mod_credits = opname ## _CREDITS, \
419 * number of blocks to reserve for particular operations. Should be function
420 * of ... something. Stub for now.
422 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_DESTROY);
423 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_CREATE);
424 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_ATTR_SET);
425 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_XATTR_SET);
426 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_INSERT);
427 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_DELETE);
428 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_LINK);
429 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_UNLINK);
430 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_RENAME);
431 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_CREATE_DATA);
432 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_MKDIR);
434 static void mdd_txn_param_build(const struct lu_context *ctx,
435 const struct mdd_txn_op_descr *opd)
437 mdd_ctx_info(ctx)->mti_param.tp_credits = opd->mod_credits;
440 static int mdd_object_print(const struct lu_context *ctxt, void *cookie,
441 lu_printer_t p, const struct lu_object *o)
443 return (*p)(ctxt, cookie, LUSTRE_MDD0_NAME"-object@%p", o);
446 static int mdd_object_exists(const struct lu_context *ctx,
447 const struct lu_object *o)
449 return lu_object_exists(ctx, lu_object_next(o));
452 static int mdd_mount(const struct lu_context *ctx, struct mdd_device *mdd)
455 struct dt_object *root;
458 root = dt_store_open(ctx, mdd->mdd_child, mdd_root_dir_name,
461 LASSERT(root != NULL);
462 lu_object_put(ctx, &root->do_lu);
470 static int mdd_device_init(const struct lu_context *ctx,
471 struct lu_device *d, struct lu_device *next)
473 struct mdd_device *mdd = lu2mdd_dev(d);
477 mdd->mdd_child = lu2dt_dev(next);
482 static struct lu_device *mdd_device_fini(const struct lu_context *ctx,
485 struct mdd_device *m = lu2mdd_dev(d);
486 struct lu_device *next = &m->mdd_child->dd_lu_dev;
490 static void mdd_device_shutdown(const struct lu_context *ctxt,
491 struct mdd_device *m)
493 mdd_fini_obd(ctxt, m);
496 static int mdd_process_config(const struct lu_context *ctxt,
497 struct lu_device *d, struct lustre_cfg *cfg)
499 struct mdd_device *m = lu2mdd_dev(d);
500 struct dt_device *dt = m->mdd_child;
501 struct lu_device *next = &dt->dd_lu_dev;
502 char *dev = lustre_cfg_string(cfg, 0);
505 switch (cfg->lcfg_command) {
507 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
510 dt->dd_ops->dt_conf_get(ctxt, dt, &m->mdd_dt_conf);
512 rc = mdd_mount(ctxt, m);
515 rc = mdd_init_obd(ctxt, m, dev);
517 CERROR("lov init error %d \n", rc);
522 mdd_device_shutdown(ctxt, m);
524 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
531 struct lu_device_operations mdd_lu_ops = {
532 .ldo_object_alloc = mdd_object_alloc,
533 .ldo_process_config = mdd_process_config,
536 static struct lu_object_operations mdd_lu_obj_ops = {
537 .loo_object_init = mdd_object_init,
538 .loo_object_start = mdd_object_start,
539 .loo_object_free = mdd_object_free,
540 .loo_object_print = mdd_object_print,
541 .loo_object_exists = mdd_object_exists,
544 void mdd_lock(const struct lu_context *ctxt, struct mdd_object *obj,
545 enum dt_lock_mode mode)
547 struct dt_object *next = mdd_object_child(obj);
549 next->do_ops->do_lock(ctxt, next, mode);
552 void mdd_unlock(const struct lu_context *ctxt, struct mdd_object *obj,
553 enum dt_lock_mode mode)
555 struct dt_object *next = mdd_object_child(obj);
557 next->do_ops->do_unlock(ctxt, next, mode);
560 static void mdd_lock2(const struct lu_context *ctxt,
561 struct mdd_object *o0, struct mdd_object *o1)
563 mdd_lock(ctxt, o0, DT_WRITE_LOCK);
564 mdd_lock(ctxt, o1, DT_WRITE_LOCK);
567 static void mdd_unlock2(const struct lu_context *ctxt,
568 struct mdd_object *o0, struct mdd_object *o1)
570 mdd_unlock(ctxt, o0, DT_WRITE_LOCK);
571 mdd_unlock(ctxt, o1, DT_WRITE_LOCK);
574 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
575 struct mdd_device *mdd)
577 struct txn_param *p = &mdd_ctx_info(ctxt)->mti_param;
579 return mdd_child_ops(mdd)->dt_trans_start(ctxt, mdd->mdd_child, p);
582 static void mdd_trans_stop(const struct lu_context *ctxt,
583 struct mdd_device *mdd, struct thandle *handle)
585 mdd_child_ops(mdd)->dt_trans_stop(ctxt, handle);
588 static int __mdd_object_create(const struct lu_context *ctxt,
589 struct mdd_object *obj, struct md_attr *ma,
590 struct thandle *handle)
592 struct dt_object *next;
593 struct lu_attr *attr = &ma->ma_attr;
597 if (!lu_object_exists(ctxt, mdd2lu_obj(obj))) {
598 next = mdd_object_child(obj);
599 rc = next->do_ops->do_create(ctxt, next, attr, handle);
603 LASSERT(ergo(rc == 0, lu_object_exists(ctxt, mdd2lu_obj(obj))));
608 int mdd_attr_set_internal(const struct lu_context *ctxt, struct mdd_object *o,
609 const struct lu_attr *attr, struct thandle *handle)
611 struct dt_object *next;
613 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(o)));
614 next = mdd_object_child(o);
615 return next->do_ops->do_attr_set(ctxt, next, attr, handle);
618 int mdd_attr_set_internal_locked(const struct lu_context *ctxt,
619 struct mdd_object *o,
620 const struct lu_attr *attr,
621 struct thandle *handle)
624 mdd_lock(ctxt, o, DT_WRITE_LOCK);
625 rc = mdd_attr_set_internal(ctxt, o, attr, handle);
626 mdd_unlock(ctxt, o, DT_WRITE_LOCK);
630 static int __mdd_xattr_set(const struct lu_context *ctxt, struct mdd_object *o,
631 const void *buf, int buf_len, const char *name,
632 int fl, struct thandle *handle)
634 struct dt_object *next;
638 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(o)));
639 next = mdd_object_child(o);
640 if (buf && buf_len > 0) {
641 rc = next->do_ops->do_xattr_set(ctxt, next, buf, buf_len, name,
643 }else if (buf == NULL && buf_len == 0) {
644 rc = next->do_ops->do_xattr_del(ctxt, next, name, handle);
648 /* this gives the same functionality as the code between
649 * sys_chmod and inode_setattr
650 * chown_common and inode_setattr
651 * utimes and inode_setattr
652 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
655 int mdd_fix_attr(const struct lu_context *ctxt, struct mdd_object *obj,
656 const struct md_attr *ma, struct lu_attr *la)
658 struct lu_attr *tmp_la = &mdd_ctx_info(ctxt)->mti_la;
659 time_t now = CURRENT_SECONDS;
663 rc = __mdd_la_get(ctxt, obj, tmp_la);
666 /*XXX Check permission */
667 if (mdd_is_immutable(obj) || mdd_is_append(obj)) {
669 /*If only change flags of the object, we should
670 * let it pass, but also need capability check
671 * here if (!capable(CAP_LINUX_IMMUTABLE)),
672 * fix it, when implement capable in mds*/
673 if (la->la_valid & ~LA_FLAGS)
676 /*According to Ext3 implementation on this, the
677 *Ctime will be changed, but not clear why?*/
679 la->la_valid |= LA_CTIME;
682 if (!(la->la_valid & LA_CTIME)) {
684 la->la_valid |= LA_CTIME;
686 /*According to original MDS implementation, it
687 * will clear ATTR_CTIME_SET flags, but seems
688 * no sense, should clear ATTR_CTIME? XXX*/
689 la->la_valid &= ~LA_CTIME;
691 if (!(la->la_valid & LA_ATIME)) {
693 la->la_valid |= LA_ATIME;
695 if (!(la->la_valid & LA_MTIME)) {
697 la->la_valid |= LA_MTIME;
703 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
704 if (current->fsuid != inode->i_uid &&
705 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
708 if (ia_valid & ATTR_SIZE &&
709 /* NFSD hack for open(O_CREAT|O_TRUNC)=mknod+truncate (bug 5781) */
710 !(rec->ur_uc.luc_fsuid == inode->i_uid &&
711 ia_valid & MDS_OPEN_OWNEROVERRIDE)) {
712 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
717 if (la->la_valid & (LA_UID | LA_GID)) {
720 if (mdd_is_immutable(obj) || mdd_is_append(obj))
722 if (la->la_uid == (uid_t) -1)
723 la->la_uid = tmp_la->la_uid;
724 if (la->la_gid == (gid_t) -1)
725 la->la_gid = tmp_la->la_gid;
726 if (!(la->la_valid & LA_MODE))
727 la->la_mode = tmp_la->la_mode;
729 * If the user or group of a non-directory has been
730 * changed by a non-root user, remove the setuid bit.
731 * 19981026 David C Niemi <niemi@tux.org>
733 * Changed this to apply to all users, including root,
734 * to avoid some races. This is the behavior we had in
735 * 2.0. The check for non-root was definitely wrong
736 * for 2.2 anyway, as it should have been using
737 * CAP_FSETID rather than fsuid -- 19990830 SD.
739 if ((tmp_la->la_mode & S_ISUID) == S_ISUID &&
740 !S_ISDIR(tmp_la->la_mode)) {
741 la->la_mode &= ~S_ISUID;
742 la->la_valid |= LA_MODE;
745 * Likewise, if the user or group of a non-directory
746 * has been changed by a non-root user, remove the
747 * setgid bit UNLESS there is no group execute bit
748 * (this would be a file marked for mandatory
749 * locking). 19981026 David C Niemi <niemi@tux.org>
751 * Removed the fsuid check (see the comment above) --
754 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
755 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
756 la->la_mode &= ~S_ISGID;
757 la->la_valid |= LA_MODE;
759 } else if (la->la_valid & LA_MODE) {
760 int mode = la->la_mode;
762 if (la->la_mode == (umode_t)-1)
763 mode = tmp_la->la_mode;
765 (mode & S_IALLUGO) | (tmp_la->la_mode & ~S_IALLUGO);
771 /* set attr and LOV EA at once, return updated attr */
772 static int mdd_attr_set(const struct lu_context *ctxt,
773 struct md_object *obj,
774 const struct md_attr *ma)
776 struct mdd_object *mdd_obj = md2mdd_obj(obj);
777 struct mdd_device *mdd = mdo2mdd(obj);
778 struct thandle *handle;
779 struct lov_mds_md *lmm = NULL;
780 int rc = 0, lmm_size = 0, max_size;
781 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
784 mdd_txn_param_build(ctxt, &MDD_TXN_ATTR_SET);
785 handle = mdd_trans_start(ctxt, mdd);
787 RETURN(PTR_ERR(handle));
788 /*TODO: add lock here*/
789 /* start a log jounal handle if needed */
790 if (S_ISREG(mdd_object_type(ctxt, mdd_obj)) &&
791 ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
792 max_size = mdd_lov_mdsize(ctxt, mdd);
793 OBD_ALLOC(lmm, max_size);
795 GOTO(cleanup, rc = -ENOMEM);
797 rc = mdd_get_md(ctxt, mdd_obj, lmm, &lmm_size, 1);
803 if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
804 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
805 ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
807 *la_copy = ma->ma_attr;
808 rc = mdd_fix_attr(ctxt, mdd_obj, ma, la_copy);
812 if (la_copy->la_valid & LA_FLAGS) {
813 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
816 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
817 } else if (la_copy->la_valid) { /* setattr */
818 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
820 /* journal chown/chgrp in llog, just like unlink */
821 if (rc == 0 && lmm_size){
822 /*TODO set_attr llog */
826 if (rc == 0 && ma->ma_valid & MA_LOV) {
829 mode = mdd_object_type(ctxt, mdd_obj);
830 if (S_ISREG(mode) || S_ISDIR(mode)) {
831 /*TODO check permission*/
832 rc = mdd_lov_set_md(ctxt, NULL, mdd_obj, ma->ma_lmm,
833 ma->ma_lmm_size, handle, 1);
838 mdd_trans_stop(ctxt, mdd, handle);
839 if (rc == 0 && lmm_size) {
840 /*set obd attr, if needed*/
841 rc = mdd_lov_setattr_async(ctxt, mdd_obj, lmm, lmm_size);
844 OBD_FREE(lmm, max_size);
850 int mdd_xattr_set_txn(const struct lu_context *ctxt, struct mdd_object *obj,
851 const void *buf, int buf_len, const char *name, int fl,
852 struct thandle *handle)
857 mdd_lock(ctxt, obj, DT_WRITE_LOCK);
858 rc = __mdd_xattr_set(ctxt, obj, buf, buf_len, name,
860 mdd_unlock(ctxt, obj, DT_WRITE_LOCK);
865 static int mdd_xattr_set(const struct lu_context *ctxt, struct md_object *obj,
866 const void *buf, int buf_len, const char *name, int fl)
868 struct mdd_device *mdd = mdo2mdd(obj);
869 struct thandle *handle;
873 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
874 handle = mdd_trans_start(ctxt, mdd);
876 RETURN(PTR_ERR(handle));
878 rc = mdd_xattr_set_txn(ctxt, md2mdd_obj(obj), buf, buf_len, name,
881 mdd_trans_stop(ctxt, mdd, handle);
886 static int __mdd_xattr_del(const struct lu_context *ctxt,struct mdd_device *mdd,
887 struct mdd_object *obj,
888 const char *name, struct thandle *handle)
890 struct dt_object *next;
892 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(obj)));
893 next = mdd_object_child(obj);
894 return next->do_ops->do_xattr_del(ctxt, next, name, handle);
897 int mdd_xattr_del(const struct lu_context *ctxt, struct md_object *obj,
900 struct mdd_object *mdd_obj = md2mdd_obj(obj);
901 struct mdd_device *mdd = mdo2mdd(obj);
902 struct thandle *handle;
906 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
907 handle = mdd_trans_start(ctxt, mdd);
909 RETURN(PTR_ERR(handle));
911 mdd_lock(ctxt, mdd_obj, DT_WRITE_LOCK);
912 rc = __mdd_xattr_del(ctxt, mdd, md2mdd_obj(obj), name, handle);
913 mdd_unlock(ctxt, mdd_obj, DT_WRITE_LOCK);
915 mdd_trans_stop(ctxt, mdd, handle);
920 static int __mdd_index_insert(const struct lu_context *ctxt,
921 struct mdd_object *pobj, const struct lu_fid *lf,
922 const char *name, struct thandle *handle)
925 struct dt_object *next = mdd_object_child(pobj);
928 if (dt_try_as_dir(ctxt, next))
929 rc = next->do_index_ops->dio_insert(ctxt, next,
931 (struct dt_key *)name, handle);
937 static int __mdd_index_delete(const struct lu_context *ctxt,
938 struct mdd_object *pobj, const char *name,
939 struct thandle *handle)
942 struct dt_object *next = mdd_object_child(pobj);
945 if (dt_try_as_dir(ctxt, next))
946 rc = next->do_index_ops->dio_delete(ctxt, next,
947 (struct dt_key *)name, handle);
953 static int mdd_link_sanity_check(const struct lu_context *ctxt,
954 struct mdd_object *tgt_obj,
955 struct mdd_object *src_obj)
959 rc = mdd_may_create(ctxt, tgt_obj, NULL);
962 if (S_ISDIR(mdd_object_type(ctxt, src_obj)))
965 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
971 static int mdd_link(const struct lu_context *ctxt, struct md_object *tgt_obj,
972 struct md_object *src_obj, const char *name)
974 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
975 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
976 struct mdd_device *mdd = mdo2mdd(src_obj);
977 struct thandle *handle;
981 mdd_txn_param_build(ctxt, &MDD_TXN_LINK);
982 handle = mdd_trans_start(ctxt, mdd);
984 RETURN(PTR_ERR(handle));
986 mdd_lock2(ctxt, mdd_tobj, mdd_sobj);
988 rc = mdd_link_sanity_check(ctxt, mdd_tobj, mdd_sobj);
992 rc = __mdd_index_insert(ctxt, mdd_tobj, lu_object_fid(&src_obj->mo_lu),
995 __mdd_ref_add(ctxt, mdd_sobj, handle);
998 mdd_unlock2(ctxt, mdd_tobj, mdd_sobj);
999 mdd_trans_stop(ctxt, mdd, handle);
1004 * Check that @dir contains no entries except (possibly) dot and dotdot.
1009 * -ENOTEMPTY not empty
1013 static int mdd_dir_is_empty(const struct lu_context *ctx,
1014 struct mdd_object *dir)
1017 struct dt_object *obj;
1018 struct dt_it_ops *iops;
1021 obj = mdd_object_child(dir);
1022 iops = &obj->do_index_ops->dio_it;
1023 it = iops->init(ctx, obj);
1025 result = iops->get(ctx, it, (const void *)"");
1028 for (result = 0, i = 0; result == 0 && i < 3; ++i)
1029 result = iops->next(ctx, it);
1032 result = -ENOTEMPTY;
1033 else if (result == +1)
1035 } else if (result == 0)
1037 * Huh? Index contains no zero key?
1040 iops->fini(ctx, it);
1046 /* return md_attr back,
1047 * if it is last unlink then return lov ea + llog cookie*/
1048 static int __mdd_finish_unlink(const struct lu_context *ctxt,
1049 struct mdd_object *obj, struct md_attr *ma)
1054 rc = __mdd_iattr_get(ctxt, obj, ma);
1056 if (atomic_read(&obj->mod_count) == 0 &&
1057 ma->ma_attr.la_nlink == 0) {
1058 mdd_set_dead_obj(obj);
1059 if (S_ISREG(mdd_object_type(ctxt, obj))) {
1060 rc = __mdd_lmm_get(ctxt, obj, ma);
1061 if (ma->ma_valid & MA_LOV)
1062 rc = mdd_unlink_log(ctxt,
1063 mdo2mdd(&obj->mod_obj),
1071 static int mdd_unlink_sanity_check(const struct lu_context *ctxt,
1072 struct mdd_object *pobj,
1073 struct mdd_object *cobj,
1076 struct dt_object *dt_cobj = mdd_object_child(cobj);
1080 rc = mdd_may_delete(ctxt, pobj, cobj, S_ISDIR(ma->ma_attr.la_mode));
1084 if (S_ISDIR(mdd_object_type(ctxt, cobj)) &&
1085 dt_try_as_dir(ctxt, dt_cobj)) {
1086 rc = mdd_dir_is_empty(ctxt, cobj);
1094 static int mdd_unlink(const struct lu_context *ctxt, struct md_object *pobj,
1095 struct md_object *cobj, const char *name,
1098 struct mdd_device *mdd = mdo2mdd(pobj);
1099 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1100 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1101 struct thandle *handle;
1105 mdd_txn_param_build(ctxt, &MDD_TXN_UNLINK);
1106 handle = mdd_trans_start(ctxt, mdd);
1108 RETURN(PTR_ERR(handle));
1110 mdd_lock2(ctxt, mdd_pobj, mdd_cobj);
1112 rc = mdd_unlink_sanity_check(ctxt, mdd_pobj, mdd_cobj, ma);
1117 rc = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1121 __mdd_ref_del(ctxt, mdd_cobj, handle);
1122 if (S_ISDIR(lu_object_attr(ctxt, &cobj->mo_lu))) {
1124 __mdd_ref_del(ctxt, mdd_cobj, handle);
1126 __mdd_ref_del(ctxt, mdd_pobj, handle);
1129 rc = __mdd_finish_unlink(ctxt, mdd_cobj, ma);
1132 mdd_unlock2(ctxt, mdd_pobj, mdd_cobj);
1133 mdd_trans_stop(ctxt, mdd, handle);
1137 static int mdd_parent_fid(const struct lu_context *ctxt,
1138 struct mdd_object *obj,
1141 return mdd_lookup(ctxt, &obj->mod_obj, dotdot, fid);
1145 * return 0: if p2 is the parent of p1
1146 * otherwise: other_value
1148 static int mdd_is_parent(const struct lu_context *ctxt,
1149 struct mdd_device *mdd,
1150 struct mdd_object *p1,
1151 struct mdd_object *p2)
1153 struct lu_fid * pfid;
1154 struct mdd_object *parent = NULL;
1158 pfid = &mdd_ctx_info(ctxt)->mti_fid;
1159 if (lu_fid_eq(mdo2fid(p1), &mdd->mdd_root_fid))
1162 rc = mdd_parent_fid(ctxt, p1, pfid);
1165 if (lu_fid_eq(pfid, mdo2fid(p2)))
1167 if (lu_fid_eq(pfid, &mdd->mdd_root_fid))
1170 mdd_object_put(ctxt, parent);
1171 parent = mdd_object_find(ctxt, mdd, pfid);
1173 GOTO(out, rc = PTR_ERR(parent));
1177 if (parent && !IS_ERR(parent))
1178 mdd_object_put(ctxt, parent);
1182 static int mdd_rename_lock(const struct lu_context *ctxt,
1183 struct mdd_device *mdd,
1184 struct mdd_object *src_pobj,
1185 struct mdd_object *tgt_pobj)
1189 if (src_pobj == tgt_pobj) {
1190 mdd_lock(ctxt, src_pobj, DT_WRITE_LOCK);
1193 /*compared the parent child relationship of src_p&tgt_p*/
1194 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1195 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1197 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1198 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1202 if (!mdd_is_parent(ctxt, mdd, src_pobj, tgt_pobj)) {
1203 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1207 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1212 static void mdd_rename_unlock(const struct lu_context *ctxt,
1213 struct mdd_object *src_pobj,
1214 struct mdd_object *tgt_pobj)
1216 mdd_unlock(ctxt, src_pobj, DT_WRITE_LOCK);
1217 if (src_pobj != tgt_pobj)
1218 mdd_unlock(ctxt, tgt_pobj, DT_WRITE_LOCK);
1221 static int mdd_rename_sanity_check(const struct lu_context *ctxt,
1222 struct mdd_object *src_pobj,
1223 struct mdd_object *tgt_pobj,
1224 struct mdd_object *sobj,
1225 struct mdd_object *tobj)
1227 struct mdd_device *mdd =mdo2mdd(&src_pobj->mod_obj);
1228 int rc = 0, src_is_dir, tgt_is_dir;
1231 src_is_dir = S_ISDIR(mdd_object_type(ctxt, sobj));
1232 rc = mdd_may_delete(ctxt, src_pobj, sobj, src_is_dir);
1237 rc = mdd_may_create(ctxt, tgt_pobj, NULL);
1240 if (!mdd_is_parent(ctxt, mdd, tgt_pobj, sobj))
1241 GOTO(out, rc = -EINVAL);
1245 /* source should not be ancestor of target */
1246 if (!mdd_is_parent(ctxt, mdd, tobj, sobj))
1247 GOTO(out, rc = -EINVAL);
1249 rc = mdd_may_delete(ctxt, tgt_pobj, tobj, src_is_dir);
1253 tgt_is_dir = S_ISDIR(mdd_object_type(ctxt, tobj));
1254 if (tgt_is_dir && mdd_dir_is_empty(ctxt, tobj))
1255 GOTO(out, rc = -ENOTEMPTY);
1257 mdd_object_put(ctxt, sobj);
1261 static int mdd_rename(const struct lu_context *ctxt, struct md_object *src_pobj,
1262 struct md_object *tgt_pobj, const struct lu_fid *lf,
1263 const char *sname, struct md_object *tobj,
1264 const char *tname, struct md_attr *ma)
1266 struct mdd_device *mdd = mdo2mdd(src_pobj);
1267 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj);
1268 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1269 struct mdd_object *mdd_sobj = mdd_object_find(ctxt, mdd, lf);
1270 struct mdd_object *mdd_tobj = NULL;
1271 struct thandle *handle;
1276 mdd_tobj = md2mdd_obj(tobj);
1278 /*XXX: shouldn't this check be done under lock below? */
1279 rc = mdd_rename_sanity_check(ctxt, mdd_spobj, mdd_tpobj,
1280 mdd_sobj, mdd_tobj);
1284 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1285 handle = mdd_trans_start(ctxt, mdd);
1287 RETURN(PTR_ERR(handle));
1289 /*FIXME: Should consider tobj and sobj too in rename_lock*/
1290 rc = mdd_rename_lock(ctxt, mdd, mdd_spobj, mdd_tpobj);
1292 GOTO(cleanup_unlocked, rc);
1294 rc = __mdd_index_delete(ctxt, mdd_spobj, sname, handle);
1298 /*if sobj is dir, its parent object nlink should be dec too*/
1299 if (S_ISDIR(mdd_object_type(ctxt, mdd_sobj)))
1300 __mdd_ref_del(ctxt, mdd_spobj, handle);
1303 rc = __mdd_index_delete(ctxt, mdd_tpobj, tname, handle);
1308 rc = __mdd_index_insert(ctxt, mdd_tpobj, lf, tname, handle);
1312 if (tobj && lu_object_exists(ctxt, &tobj->mo_lu)) {
1313 __mdd_ref_del(ctxt, mdd_tobj, handle);
1314 /* remove dot reference */
1315 if (S_ISDIR(mdd_object_type(ctxt, mdd_tobj)))
1316 __mdd_ref_del(ctxt, mdd_tobj, handle);
1318 rc = __mdd_finish_unlink(ctxt, mdd_tobj, ma);
1321 mdd_rename_unlock(ctxt, mdd_spobj, mdd_tpobj);
1323 mdd_trans_stop(ctxt, mdd, handle);
1327 static int mdd_lookup(const struct lu_context *ctxt, struct md_object *pobj,
1328 const char *name, struct lu_fid* fid)
1330 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1331 struct dt_object *dir = mdd_object_child(mdd_obj);
1332 struct dt_rec *rec = (struct dt_rec *)fid;
1333 const struct dt_key *key = (const struct dt_key *)name;
1337 if (mdd_is_dead_obj(mdd_obj))
1339 mdd_lock(ctxt, mdd_obj, DT_READ_LOCK);
1340 if (S_ISDIR(mdd_object_type(ctxt, mdd_obj)) && dt_try_as_dir(ctxt, dir))
1341 rc = dir->do_index_ops->dio_lookup(ctxt, dir, rec, key);
1344 mdd_unlock(ctxt, mdd_obj, DT_READ_LOCK);
1348 static int __mdd_object_initialize(const struct lu_context *ctxt,
1349 struct mdd_object *parent,
1350 struct mdd_object *child,
1351 struct md_attr *ma, struct thandle *handle)
1353 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
1357 /* update attributes for child and parent.
1359 * (1) the valid bits should be converted between Lustre and Linux;
1360 * (2) maybe, the child attributes should be set in OSD when creation.
1363 rc = mdd_attr_set_internal(ctxt, child, &ma->ma_attr, handle);
1367 la->la_valid = LA_MTIME|LA_CTIME;
1368 la->la_atime = ma->ma_attr.la_atime;
1369 la->la_ctime = ma->ma_attr.la_ctime;
1370 rc = mdd_attr_set_internal(ctxt, parent, la, handle);
1374 if (S_ISDIR(ma->ma_attr.la_mode)) {
1375 /* add . and .. for newly created dir */
1376 __mdd_ref_add(ctxt, child, handle);
1377 rc = __mdd_index_insert(ctxt, child,
1378 mdo2fid(child), dot, handle);
1380 rc = __mdd_index_insert(ctxt, child, mdo2fid(parent),
1383 __mdd_ref_add(ctxt, parent, handle);
1387 rc2 = __mdd_index_delete(ctxt,
1388 child, dot, handle);
1390 CERROR("Failure to cleanup after dotdot"
1391 " creation: %d (%d)\n", rc2, rc);
1393 __mdd_ref_del(ctxt, child, handle);
1400 static int mdd_create_data(const struct lu_context *ctxt,
1401 struct md_object *pobj, struct md_object *cobj,
1402 const struct md_create_spec *spec,
1405 struct mdd_device *mdd = mdo2mdd(pobj);
1406 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1407 struct mdd_object *son = md2mdd_obj(cobj);
1408 struct lu_attr *attr = &ma->ma_attr;
1409 struct lov_mds_md *lmm = NULL;
1411 struct thandle *handle;
1415 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size, spec,
1420 mdd_txn_param_build(ctxt, &MDD_TXN_CREATE_DATA);
1421 handle = mdd_trans_start(ctxt, mdd);
1423 RETURN(PTR_ERR(handle));
1425 /*XXX: setting the lov ea is not locked but setting the attr is locked? */
1427 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size,
1430 rc = mdd_attr_get_internal_locked(ctxt, son, ma);
1433 mdd_trans_stop(ctxt, mdd, handle);
1437 static int mdd_create_sanity_check(const struct lu_context *ctxt,
1438 struct mdd_device *mdd,
1439 struct md_object *pobj,
1440 const char *name, struct md_attr *ma)
1442 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
1443 struct lu_fid *fid = &mdd_ctx_info(ctxt)->mti_fid;
1448 if (mdd_is_dead_obj(md2mdd_obj(pobj)))
1450 rc = mdd_lookup(ctxt, pobj, name, fid);
1451 if (rc != -ENOENT) {
1452 rc = rc ? rc : -EEXIST;
1456 rc = __mdd_la_get(ctxt, md2mdd_obj(pobj), la);
1460 if (la->la_mode & S_ISGID) {
1461 ma->ma_attr.la_gid = la->la_gid;
1462 if (S_ISDIR(ma->ma_attr.la_mode)) {
1463 ma->ma_attr.la_mode |= S_ISGID;
1464 ma->ma_attr.la_valid |= LA_MODE;
1468 switch (ma->ma_attr.la_mode & S_IFMT) {
1486 * Create object and insert it into namespace.
1488 static int mdd_create(const struct lu_context *ctxt, struct md_object *pobj,
1489 const char *name, struct md_object *child,
1490 const struct md_create_spec *spec,
1493 struct mdd_device *mdd = mdo2mdd(pobj);
1494 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1495 struct mdd_object *son = md2mdd_obj(child);
1496 struct lu_attr *attr = &ma->ma_attr;
1497 struct lov_mds_md *lmm = NULL;
1498 struct thandle *handle;
1499 int rc, created = 0, inserted = 0, lmm_size = 0;
1502 /* sanity checks before big job */
1503 rc = mdd_create_sanity_check(ctxt, mdd, pobj, name, ma);
1506 /* no RPC inside the transaction, so OST objects should be created at
1508 if (S_ISREG(attr->la_mode)) {
1509 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size,
1515 mdd_txn_param_build(ctxt, &MDD_TXN_MKDIR);
1516 handle = mdd_trans_start(ctxt, mdd);
1518 RETURN(PTR_ERR(handle));
1520 mdd_lock(ctxt, mdd_pobj, DT_WRITE_LOCK);
1523 * XXX check that link can be added to the parent in mkdir case.
1527 * Two operations have to be performed:
1529 * - allocation of new object (->do_create()), and
1531 * - insertion into parent index (->dio_insert()).
1533 * Due to locking, operation order is not important, when both are
1534 * successful, *but* error handling cases are quite different:
1536 * - if insertion is done first, and following object creation fails,
1537 * insertion has to be rolled back, but this operation might fail
1538 * also leaving us with dangling index entry.
1540 * - if creation is done first, is has to be undone if insertion
1541 * fails, leaving us with leaked space, which is neither good, nor
1544 * It seems that creation-first is simplest solution, but it is
1545 * sub-optimal in the frequent
1550 * case, because second mkdir is bound to create object, only to
1551 * destroy it immediately.
1553 * Note that local file systems do
1555 * 0. lookup -> -EEXIST
1561 * Maybe we should do the same. For now: creation-first.
1564 rc = __mdd_object_create(ctxt, son, ma, handle);
1570 rc = __mdd_object_initialize(ctxt, mdd_pobj, son, ma, handle);
1573 * Object has no links, so it will be destroyed when last
1574 * reference is released. (XXX not now.)
1578 rc = __mdd_index_insert(ctxt, mdd_pobj, lu_object_fid(&child->mo_lu),
1585 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size, handle, 0);
1587 CERROR("error on stripe info copy %d \n", rc);
1591 if (S_ISLNK(attr->la_mode)) {
1592 struct dt_object *dt = mdd_object_child(son);
1593 const char *target_name = spec->u.sp_symname;
1594 int sym_len = strlen(target_name);
1597 rc = dt->do_body_ops->dbo_write(ctxt, dt, target_name,
1598 sym_len, &pos, handle);
1604 /* return attr back */
1605 rc = mdd_attr_get_internal(ctxt, son, ma);
1607 if (rc && created) {
1611 rc2 = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1613 CERROR("error can not cleanup destroy %d\n",
1617 __mdd_ref_del(ctxt, son, handle);
1620 OBD_FREE(lmm, lmm_size);
1621 mdd_unlock(ctxt, mdd_pobj, DT_WRITE_LOCK);
1622 mdd_trans_stop(ctxt, mdd, handle);
1625 /* partial operation */
1626 static int mdd_object_create(const struct lu_context *ctxt,
1627 struct md_object *obj,
1628 const struct md_create_spec *spec,
1632 struct mdd_device *mdd = mdo2mdd(obj);
1633 struct thandle *handle;
1637 mdd_txn_param_build(ctxt, &MDD_TXN_OBJECT_CREATE);
1638 handle = mdd_trans_start(ctxt, mdd);
1640 RETURN(PTR_ERR(handle));
1642 rc = __mdd_object_create(ctxt, md2mdd_obj(obj), ma, handle);
1643 /* XXX: parent fid is needed here
1644 rc = __mdd_object_initialize(ctxt, mdo, son, ma, handle);
1649 rc = mdd_attr_get_internal(ctxt, md2mdd_obj(obj), ma);
1651 mdd_trans_stop(ctxt, mdd, handle);
1655 /* partial operation */
1656 static int mdd_name_insert(const struct lu_context *ctxt,
1657 struct md_object *pobj,
1658 const char *name, const struct lu_fid *fid)
1660 struct mdd_device *mdd = mdo2mdd(pobj);
1661 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1662 struct thandle *handle;
1666 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_INSERT);
1667 handle = mdd_trans_start(ctxt, mdd);
1669 RETURN(PTR_ERR(handle));
1671 mdd_lock(ctxt, mdd_obj, DT_WRITE_LOCK);
1673 rc = __mdd_index_insert(ctxt, mdd_obj, fid, name, handle);
1675 mdd_unlock(ctxt, mdd_obj, DT_WRITE_LOCK);
1676 mdd_trans_stop(ctxt, mdd, handle);
1680 static int mdd_name_remove(const struct lu_context *ctxt,
1681 struct md_object *pobj,
1684 struct mdd_device *mdd = mdo2mdd(pobj);
1685 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1686 struct thandle *handle;
1690 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_DELETE);
1691 handle = mdd_trans_start(ctxt, mdd);
1693 RETURN(PTR_ERR(handle));
1695 mdd_lock(ctxt, mdd_obj, DT_WRITE_LOCK);
1697 rc = __mdd_index_delete(ctxt, mdd_obj, name, handle);
1699 mdd_unlock(ctxt, mdd_obj, DT_WRITE_LOCK);
1701 mdd_trans_stop(ctxt, mdd, handle);
1705 static int mdd_rename_tgt(const struct lu_context *ctxt, struct md_object *pobj,
1706 struct md_object *tobj, const struct lu_fid *lf,
1709 struct mdd_device *mdd = mdo2mdd(pobj);
1710 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1711 struct mdd_object *mdd_tobj = NULL;
1712 struct thandle *handle;
1716 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1717 handle = mdd_trans_start(ctxt, mdd);
1719 RETURN(PTR_ERR(handle));
1722 mdd_tobj = md2mdd_obj(tobj);
1724 mdd_lock2(ctxt, mdd_tpobj, mdd_tobj);
1726 /*TODO rename sanity checking*/
1728 rc = __mdd_index_delete(ctxt, mdd_tpobj, name, handle);
1733 rc = __mdd_index_insert(ctxt, mdd_tpobj, lf, name, handle);
1737 if (tobj && lu_object_exists(ctxt, &tobj->mo_lu))
1738 __mdd_ref_del(ctxt, mdd_tobj, handle);
1740 mdd_unlock2(ctxt, mdd_tpobj, mdd_tobj);
1741 mdd_trans_stop(ctxt, mdd, handle);
1745 static int mdd_root_get(const struct lu_context *ctx,
1746 struct md_device *m, struct lu_fid *f)
1748 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1751 *f = mdd->mdd_root_fid;
1755 static int mdd_statfs(const struct lu_context *ctx,
1756 struct md_device *m, struct kstatfs *sfs)
1758 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1763 rc = mdd_child_ops(mdd)->dt_statfs(ctx, mdd->mdd_child, sfs);
1768 static int mdd_get_maxsize(const struct lu_context *ctx,
1769 struct md_device *m, int *md_size,
1772 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1775 *md_size = mdd_lov_mdsize(ctx, mdd);
1776 *cookie_size = mdd_lov_cookiesize(ctx, mdd);
1781 static void __mdd_ref_add(const struct lu_context *ctxt, struct mdd_object *obj,
1782 struct thandle *handle)
1784 struct dt_object *next;
1786 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(obj)));
1787 next = mdd_object_child(obj);
1788 next->do_ops->do_ref_add(ctxt, next, handle);
1791 static int mdd_ref_add(const struct lu_context *ctxt, struct md_object *obj)
1793 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1794 struct mdd_device *mdd = mdo2mdd(obj);
1795 struct thandle *handle;
1798 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1799 handle = mdd_trans_start(ctxt, mdd);
1803 mdd_lock(ctxt, mdd_obj, DT_WRITE_LOCK);
1804 __mdd_ref_add(ctxt, mdd_obj, handle);
1805 mdd_unlock(ctxt, mdd_obj, DT_WRITE_LOCK);
1807 mdd_trans_stop(ctxt, mdd, handle);
1813 __mdd_ref_del(const struct lu_context *ctxt, struct mdd_object *obj,
1814 struct thandle *handle)
1816 struct dt_object *next = mdd_object_child(obj);
1818 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(obj)));
1820 next->do_ops->do_ref_del(ctxt, next, handle);
1823 static int mdd_ref_del(const struct lu_context *ctxt, struct md_object *obj,
1826 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1827 struct mdd_device *mdd = mdo2mdd(obj);
1828 struct thandle *handle;
1832 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1833 handle = mdd_trans_start(ctxt, mdd);
1837 mdd_lock(ctxt, mdd_obj, DT_WRITE_LOCK);
1840 if (S_ISDIR(lu_object_attr(ctxt, &obj->mo_lu)) &&
1841 dt_try_as_dir(ctxt, mdd_object_child(mdd_obj))) {
1842 rc = mdd_dir_is_empty(ctxt, mdd_obj);
1847 __mdd_ref_del(ctxt, mdd_obj, handle);
1849 if (S_ISDIR(lu_object_attr(ctxt, &obj->mo_lu))) {
1851 __mdd_ref_del(ctxt, mdd_obj, handle);
1854 rc = __mdd_finish_unlink(ctxt, mdd_obj, ma);
1857 mdd_unlock(ctxt, mdd_obj, DT_WRITE_LOCK);
1858 mdd_trans_stop(ctxt, mdd, handle);
1862 static int mdd_open(const struct lu_context *ctxt, struct md_object *obj)
1864 /*XXX access mod checking*/
1865 atomic_inc(&md2mdd_obj(obj)->mod_count);
1869 static int mdd_close(const struct lu_context *ctxt, struct md_object *obj,
1872 if (atomic_dec_and_test(&md2mdd_obj(obj)->mod_count)) {
1873 /*TODO: Remove it from orphan list */
1876 return __mdd_finish_unlink(ctxt, md2mdd_obj(obj), ma);
1879 static int mdd_readpage(const struct lu_context *ctxt, struct md_object *obj,
1880 const struct lu_rdpg *rdpg)
1882 struct dt_object *next;
1883 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1886 LASSERT(lu_object_exists(ctxt, mdd2lu_obj(mdd_obj)));
1887 next = mdd_object_child(mdd_obj);
1889 mdd_lock(ctxt, mdd_obj, DT_READ_LOCK);
1890 if (S_ISDIR(mdd_object_type(ctxt, mdd_obj)) &&
1891 dt_try_as_dir(ctxt, next))
1892 rc = next->do_ops->do_readpage(ctxt, next, rdpg);
1895 mdd_unlock(ctxt, mdd_obj, DT_READ_LOCK);
1899 struct md_device_operations mdd_ops = {
1900 .mdo_root_get = mdd_root_get,
1901 .mdo_statfs = mdd_statfs,
1902 .mdo_get_maxsize = mdd_get_maxsize,
1905 static struct md_dir_operations mdd_dir_ops = {
1906 .mdo_lookup = mdd_lookup,
1907 .mdo_create = mdd_create,
1908 .mdo_rename = mdd_rename,
1909 .mdo_link = mdd_link,
1910 .mdo_unlink = mdd_unlink,
1911 .mdo_name_insert = mdd_name_insert,
1912 .mdo_name_remove = mdd_name_remove,
1913 .mdo_rename_tgt = mdd_rename_tgt,
1914 .mdo_create_data = mdd_create_data
1918 static struct md_object_operations mdd_obj_ops = {
1919 .moo_attr_get = mdd_attr_get,
1920 .moo_attr_set = mdd_attr_set,
1921 .moo_xattr_get = mdd_xattr_get,
1922 .moo_xattr_set = mdd_xattr_set,
1923 .moo_xattr_list = mdd_xattr_list,
1924 .moo_xattr_del = mdd_xattr_del,
1925 .moo_object_create = mdd_object_create,
1926 .moo_ref_add = mdd_ref_add,
1927 .moo_ref_del = mdd_ref_del,
1928 .moo_open = mdd_open,
1929 .moo_close = mdd_close,
1930 .moo_readpage = mdd_readpage,
1931 .moo_readlink = mdd_readlink
1934 static struct obd_ops mdd_obd_device_ops = {
1935 .o_owner = THIS_MODULE
1938 static struct lu_device *mdd_device_alloc(const struct lu_context *ctx,
1939 struct lu_device_type *t,
1940 struct lustre_cfg *lcfg)
1942 struct lu_device *l;
1943 struct mdd_device *m;
1947 l = ERR_PTR(-ENOMEM);
1949 md_device_init(&m->mdd_md_dev, t);
1951 l->ld_ops = &mdd_lu_ops;
1952 m->mdd_md_dev.md_ops = &mdd_ops;
1958 static void mdd_device_free(const struct lu_context *ctx,
1959 struct lu_device *lu)
1961 struct mdd_device *m = lu2mdd_dev(lu);
1963 LASSERT(atomic_read(&lu->ld_ref) == 0);
1964 md_device_fini(&m->mdd_md_dev);
1968 static int mdd_type_init(struct lu_device_type *t)
1970 return lu_context_key_register(&mdd_thread_key);
1973 static void mdd_type_fini(struct lu_device_type *t)
1975 lu_context_key_degister(&mdd_thread_key);
1978 static struct lu_device_type_operations mdd_device_type_ops = {
1979 .ldto_init = mdd_type_init,
1980 .ldto_fini = mdd_type_fini,
1982 .ldto_device_alloc = mdd_device_alloc,
1983 .ldto_device_free = mdd_device_free,
1985 .ldto_device_init = mdd_device_init,
1986 .ldto_device_fini = mdd_device_fini
1989 static struct lu_device_type mdd_device_type = {
1990 .ldt_tags = LU_DEVICE_MD,
1991 .ldt_name = LUSTRE_MDD0_NAME,
1992 .ldt_ops = &mdd_device_type_ops,
1993 .ldt_ctx_tags = LCT_MD_THREAD
1996 static void *mdd_key_init(const struct lu_context *ctx,
1997 struct lu_context_key *key)
1999 struct mdd_thread_info *info;
2001 OBD_ALLOC_PTR(info);
2003 info = ERR_PTR(-ENOMEM);
2007 static void mdd_key_fini(const struct lu_context *ctx,
2008 struct lu_context_key *key, void *data)
2010 struct mdd_thread_info *info = data;
2014 static struct lu_context_key mdd_thread_key = {
2015 .lct_tags = LCT_MD_THREAD,
2016 .lct_init = mdd_key_init,
2017 .lct_fini = mdd_key_fini
2020 struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
2024 struct lprocfs_vars lprocfs_mdd_module_vars[] = {
2028 LPROCFS_INIT_VARS(mdd, lprocfs_mdd_module_vars, lprocfs_mdd_obd_vars);
2030 static int __init mdd_mod_init(void)
2032 struct lprocfs_static_vars lvars;
2034 lprocfs_init_vars(mdd, &lvars);
2035 return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
2036 LUSTRE_MDD0_NAME, &mdd_device_type);
2039 static void __exit mdd_mod_exit(void)
2041 class_unregister_type(LUSTRE_MDD0_NAME);
2044 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2045 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD0_NAME")");
2046 MODULE_LICENSE("GPL");
2048 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);