1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mdd) routines
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
33 #include <linux/module.h>
34 #include <linux/jbd.h>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
41 #include <linux/ldiskfs_fs.h>
42 #include <lustre_mds.h>
44 #include "mdd_internal.h"
47 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
49 static void mdd_trans_stop(const struct lu_context *ctxt,
50 struct mdd_device *mdd, int rc,
51 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(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(&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(mdd2lu_obj(obj)));
201 return next->do_ops->do_attr_get(ctxt, next, la);
204 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
206 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
208 if (flags & LUSTRE_APPEND_FL)
209 obj->mod_flags |= APPEND_OBJ;
211 if (flags & LUSTRE_IMMUTABLE_FL)
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 mdd_read_lock(ctxt, obj);
222 rc = __mdd_la_get(ctxt, obj, la);
223 mdd_read_unlock(ctxt, obj);
225 mdd_flags_xlate(obj, la->la_flags);
229 /*Check whether it may delete the cobj under the pobj*/
230 static int mdd_may_delete(const struct lu_context *ctxt,
231 struct mdd_object *pobj, struct mdd_object *cobj,
234 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
238 LASSERT(cobj && pobj);
240 if (!lu_object_exists(&cobj->mod_obj.mo_lu))
243 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
247 if (!S_ISDIR(mdd_object_type(cobj)))
250 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
253 } else if (S_ISDIR(mdd_object_type(cobj)))
256 if (mdd_is_dead_obj(pobj))
261 /* get only inode attributes */
262 static int __mdd_iattr_get(const struct lu_context *ctxt,
263 struct mdd_object *mdd_obj, struct md_attr *ma)
268 rc = __mdd_la_get(ctxt, mdd_obj, &ma->ma_attr);
270 ma->ma_valid = MA_INODE;
273 /* get lov EA only */
274 static int __mdd_lmm_get(const struct lu_context *ctxt,
275 struct mdd_object *mdd_obj, struct md_attr *ma)
279 LASSERT(ma->ma_lmm != NULL && ma->ma_lmm_size > 0);
280 rc = mdd_get_md(ctxt, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size, 0,
283 ma->ma_valid |= MA_LOV;
289 #ifdef HAVE_SPLIT_SUPPORT
291 static int __mdd_lmv_get(const struct lu_context *ctxt,
292 struct mdd_object *mdd_obj, struct md_attr *ma)
296 rc = mdd_get_md(ctxt, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size, 0,
299 ma->ma_valid |= MA_LMV;
306 static int mdd_attr_get_internal(const struct lu_context *ctxt,
307 struct mdd_object *mdd_obj,
313 if (ma->ma_need & MA_INODE)
314 rc = __mdd_iattr_get(ctxt, mdd_obj, ma);
316 if (rc == 0 && ma->ma_need & MA_LOV) {
317 if (S_ISREG(mdd_object_type(mdd_obj)) ||
318 S_ISDIR(mdd_object_type(mdd_obj)))
319 rc = __mdd_lmm_get(ctxt, mdd_obj, ma);
321 #ifdef HAVE_SPLIT_SUPPORT
322 if (rc == 0 && ma->ma_need & MA_LMV) {
323 if (S_ISDIR(mdd_object_type(mdd_obj)))
324 rc = __mdd_lmv_get(ctxt, mdd_obj, ma);
327 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
332 static inline int mdd_attr_get_internal_locked(const struct lu_context *ctxt,
333 struct mdd_object *mdd_obj,
337 mdd_read_lock(ctxt, mdd_obj);
338 rc = mdd_attr_get_internal(ctxt, mdd_obj, ma);
339 mdd_read_unlock(ctxt, mdd_obj);
343 static int mdd_attr_get(const struct lu_context *ctxt,
344 struct md_object *obj, struct md_attr *ma)
346 struct mdd_object *mdd_obj = md2mdd_obj(obj);
350 rc = mdd_attr_get_internal_locked(ctxt, mdd_obj, ma);
354 static int mdd_xattr_get(const struct lu_context *ctxt, struct md_object *obj,
355 void *buf, int buf_len, const char *name)
357 struct mdd_object *mdd_obj = md2mdd_obj(obj);
358 struct dt_object *next;
363 LASSERT(lu_object_exists(&obj->mo_lu));
365 next = mdd_object_child(mdd_obj);
366 mdd_read_lock(ctxt, mdd_obj);
367 rc = next->do_ops->do_xattr_get(ctxt, next, buf, buf_len, name);
368 mdd_read_unlock(ctxt, mdd_obj);
373 static int mdd_readlink(const struct lu_context *ctxt, struct md_object *obj,
374 void *buf, int buf_len)
376 struct mdd_object *mdd_obj = md2mdd_obj(obj);
377 struct dt_object *next;
382 LASSERT(lu_object_exists(&obj->mo_lu));
384 next = mdd_object_child(mdd_obj);
385 rc = next->do_body_ops->dbo_read(ctxt, next, buf, buf_len, &pos);
388 static int mdd_xattr_list(const struct lu_context *ctxt, struct md_object *obj,
389 void *buf, int buf_len)
391 struct mdd_object *mdd_obj = md2mdd_obj(obj);
392 struct dt_object *next;
397 LASSERT(lu_object_exists(&obj->mo_lu));
399 next = mdd_object_child(mdd_obj);
400 rc = next->do_ops->do_xattr_list(ctxt, next, buf, buf_len);
406 MDD_TXN_OBJECT_DESTROY_OP,
407 MDD_TXN_OBJECT_CREATE_OP,
409 MDD_TXN_XATTR_SET_OP,
410 MDD_TXN_INDEX_INSERT_OP,
411 MDD_TXN_INDEX_DELETE_OP,
415 MDD_TXN_CREATE_DATA_OP,
419 struct mdd_txn_op_descr {
420 enum mdd_txn_op mod_op;
421 unsigned int mod_credits;
425 MDD_TXN_OBJECT_DESTROY_CREDITS = 20,
426 MDD_TXN_OBJECT_CREATE_CREDITS = 20,
427 MDD_TXN_ATTR_SET_CREDITS = 20,
428 MDD_TXN_XATTR_SET_CREDITS = 20,
429 MDD_TXN_INDEX_INSERT_CREDITS = 20,
430 MDD_TXN_INDEX_DELETE_CREDITS = 20,
431 MDD_TXN_LINK_CREDITS = 20,
432 MDD_TXN_UNLINK_CREDITS = 20,
433 MDD_TXN_RENAME_CREDITS = 20,
434 MDD_TXN_CREATE_DATA_CREDITS = 20,
435 MDD_TXN_MKDIR_CREDITS = 20
438 #define DEFINE_MDD_TXN_OP_DESC(opname) \
439 static const struct mdd_txn_op_descr opname = { \
440 .mod_op = opname ## _OP, \
441 .mod_credits = opname ## _CREDITS, \
445 * number of blocks to reserve for particular operations. Should be function
446 * of ... something. Stub for now.
448 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_DESTROY);
449 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_OBJECT_CREATE);
450 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_ATTR_SET);
451 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_XATTR_SET);
452 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_INSERT);
453 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_INDEX_DELETE);
454 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_LINK);
455 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_UNLINK);
456 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_RENAME);
457 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_CREATE_DATA);
458 DEFINE_MDD_TXN_OP_DESC(MDD_TXN_MKDIR);
460 static void mdd_txn_param_build(const struct lu_context *ctx,
461 const struct mdd_txn_op_descr *opd)
463 mdd_ctx_info(ctx)->mti_param.tp_credits = opd->mod_credits;
466 static int mdd_object_print(const struct lu_context *ctxt, void *cookie,
467 lu_printer_t p, const struct lu_object *o)
469 return (*p)(ctxt, cookie, LUSTRE_MDD0_NAME"-object@%p", o);
472 static int mdd_mount(const struct lu_context *ctx, struct mdd_device *mdd)
475 struct dt_object *root;
478 root = dt_store_open(ctx, mdd->mdd_child, mdd_root_dir_name,
481 LASSERT(root != NULL);
482 lu_object_put(ctx, &root->do_lu);
483 rc = orph_index_init(ctx, mdd);
490 static int mdd_device_init(const struct lu_context *ctx,
491 struct lu_device *d, struct lu_device *next)
493 struct mdd_device *mdd = lu2mdd_dev(d);
497 mdd->mdd_child = lu2dt_dev(next);
502 static struct lu_device *mdd_device_fini(const struct lu_context *ctx,
505 struct mdd_device *m = lu2mdd_dev(d);
506 struct lu_device *next = &m->mdd_child->dd_lu_dev;
510 static void mdd_device_shutdown(const struct lu_context *ctxt,
511 struct mdd_device *m)
514 mdd_fini_obd(ctxt, m);
515 orph_index_fini(ctxt, m);
518 static int mdd_process_config(const struct lu_context *ctxt,
519 struct lu_device *d, struct lustre_cfg *cfg)
521 struct mdd_device *m = lu2mdd_dev(d);
522 struct dt_device *dt = m->mdd_child;
523 struct lu_device *next = &dt->dd_lu_dev;
524 char *dev = lustre_cfg_string(cfg, 0);
527 switch (cfg->lcfg_command) {
529 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
532 dt->dd_ops->dt_conf_get(ctxt, dt, &m->mdd_dt_conf);
534 rc = mdd_mount(ctxt, m);
537 rc = mdd_init_obd(ctxt, m, dev);
539 CERROR("lov init error %d \n", rc);
544 mdd_device_shutdown(ctxt, m);
546 rc = next->ld_ops->ldo_process_config(ctxt, next, cfg);
553 static int mdd_recovery_complete(const struct lu_context *ctxt,
556 struct mdd_device *mdd = lu2mdd_dev(d);
557 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
561 rc = mdd_lov_set_nextid(ctx, mdd);
563 CERROR("%s: mdd_lov_set_nextid failed %d\n",
567 rc = mdd_cleanup_unlink_llog(ctx, mdd);
569 obd_notify(obd->u.mds.mds_osc_obd, NULL,
570 obd->obd_async_recov ? OBD_NOTIFY_SYNC_NONBLOCK :
571 OBD_NOTIFY_SYNC, NULL);
573 /* TODO: orphans handling */
574 rc = next->ld_ops->ldo_recovery_complete(ctxt, next);
578 struct lu_device_operations mdd_lu_ops = {
579 .ldo_object_alloc = mdd_object_alloc,
580 .ldo_process_config = mdd_process_config,
581 .ldo_recovery_complete = mdd_recovery_complete
584 static struct lu_object_operations mdd_lu_obj_ops = {
585 .loo_object_init = mdd_object_init,
586 .loo_object_start = mdd_object_start,
587 .loo_object_free = mdd_object_free,
588 .loo_object_print = mdd_object_print
591 void mdd_write_lock(const struct lu_context *ctxt, struct mdd_object *obj)
593 struct dt_object *next = mdd_object_child(obj);
595 next->do_ops->do_write_lock(ctxt, next);
598 void mdd_read_lock(const struct lu_context *ctxt, struct mdd_object *obj)
600 struct dt_object *next = mdd_object_child(obj);
602 next->do_ops->do_read_lock(ctxt, next);
605 void mdd_write_unlock(const struct lu_context *ctxt, struct mdd_object *obj)
607 struct dt_object *next = mdd_object_child(obj);
609 next->do_ops->do_write_unlock(ctxt, next);
612 void mdd_read_unlock(const struct lu_context *ctxt, struct mdd_object *obj)
614 struct dt_object *next = mdd_object_child(obj);
616 next->do_ops->do_read_unlock(ctxt, next);
619 static void mdd_lock2(const struct lu_context *ctxt,
620 struct mdd_object *o0, struct mdd_object *o1)
622 mdd_write_lock(ctxt, o0);
623 mdd_write_lock(ctxt, o1);
626 static void mdd_unlock2(const struct lu_context *ctxt,
627 struct mdd_object *o0, struct mdd_object *o1)
629 mdd_write_unlock(ctxt, o1);
630 mdd_write_unlock(ctxt, o0);
633 static struct thandle* mdd_trans_start(const struct lu_context *ctxt,
634 struct mdd_device *mdd)
636 struct txn_param *p = &mdd_ctx_info(ctxt)->mti_param;
638 return mdd_child_ops(mdd)->dt_trans_start(ctxt, mdd->mdd_child, p);
641 static void mdd_trans_stop(const struct lu_context *ctxt,
642 struct mdd_device *mdd, int result,
643 struct thandle *handle)
645 handle->th_result = result;
646 mdd_child_ops(mdd)->dt_trans_stop(ctxt, handle);
649 static int __mdd_object_create(const struct lu_context *ctxt,
650 struct mdd_object *obj, struct md_attr *ma,
651 struct thandle *handle)
653 struct dt_object *next;
654 struct lu_attr *attr = &ma->ma_attr;
658 if (!lu_object_exists(mdd2lu_obj(obj))) {
659 next = mdd_object_child(obj);
660 rc = next->do_ops->do_create(ctxt, next, attr, handle);
664 LASSERT(ergo(rc == 0, lu_object_exists(mdd2lu_obj(obj))));
669 int mdd_attr_set_internal(const struct lu_context *ctxt, struct mdd_object *o,
670 const struct lu_attr *attr, struct thandle *handle)
672 struct dt_object *next;
674 LASSERT(lu_object_exists(mdd2lu_obj(o)));
675 next = mdd_object_child(o);
676 return next->do_ops->do_attr_set(ctxt, next, attr, handle);
679 int mdd_attr_set_internal_locked(const struct lu_context *ctxt,
680 struct mdd_object *o,
681 const struct lu_attr *attr,
682 struct thandle *handle)
685 mdd_write_lock(ctxt, o);
686 rc = mdd_attr_set_internal(ctxt, o, attr, handle);
687 mdd_write_unlock(ctxt, o);
691 static int __mdd_xattr_set(const struct lu_context *ctxt, struct mdd_object *o,
692 const void *buf, int buf_len, const char *name,
693 int fl, struct thandle *handle)
695 struct dt_object *next;
699 LASSERT(lu_object_exists(mdd2lu_obj(o)));
700 next = mdd_object_child(o);
701 if (buf && buf_len > 0) {
702 rc = next->do_ops->do_xattr_set(ctxt, next, buf, buf_len, name,
704 }else if (buf == NULL && buf_len == 0) {
705 rc = next->do_ops->do_xattr_del(ctxt, next, name, handle);
709 /* this gives the same functionality as the code between
710 * sys_chmod and inode_setattr
711 * chown_common and inode_setattr
712 * utimes and inode_setattr
713 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
716 int mdd_fix_attr(const struct lu_context *ctxt, struct mdd_object *obj,
717 const struct md_attr *ma, struct lu_attr *la)
719 struct lu_attr *tmp_la = &mdd_ctx_info(ctxt)->mti_la;
720 time_t now = CURRENT_SECONDS;
724 rc = __mdd_la_get(ctxt, obj, tmp_la);
727 /*XXX Check permission */
728 if (mdd_is_immutable(obj) || mdd_is_append(obj)) {
730 /*If only change flags of the object, we should
731 * let it pass, but also need capability check
732 * here if (!capable(CAP_LINUX_IMMUTABLE)),
733 * fix it, when implement capable in mds*/
734 if (la->la_valid & ~LA_FLAGS)
737 /*According to Ext3 implementation on this, the
738 *Ctime will be changed, but not clear why?*/
740 la->la_valid |= LA_CTIME;
743 if (!(la->la_valid & LA_CTIME)) {
745 la->la_valid |= LA_CTIME;
750 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
751 if (current->fsuid != inode->i_uid &&
752 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
755 if (ia_valid & ATTR_SIZE &&
756 /* NFSD hack for open(O_CREAT|O_TRUNC)=mknod+truncate (bug 5781) */
757 !(rec->ur_uc.luc_fsuid == inode->i_uid &&
758 ia_valid & MDS_OPEN_OWNEROVERRIDE)) {
759 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
764 if (la->la_valid & (LA_UID | LA_GID)) {
767 if (mdd_is_immutable(obj) || mdd_is_append(obj))
769 if (la->la_uid == (uid_t) -1)
770 la->la_uid = tmp_la->la_uid;
771 if (la->la_gid == (gid_t) -1)
772 la->la_gid = tmp_la->la_gid;
773 if (!(la->la_valid & LA_MODE))
774 la->la_mode = tmp_la->la_mode;
776 * If the user or group of a non-directory has been
777 * changed by a non-root user, remove the setuid bit.
778 * 19981026 David C Niemi <niemi@tux.org>
780 * Changed this to apply to all users, including root,
781 * to avoid some races. This is the behavior we had in
782 * 2.0. The check for non-root was definitely wrong
783 * for 2.2 anyway, as it should have been using
784 * CAP_FSETID rather than fsuid -- 19990830 SD.
786 if ((tmp_la->la_mode & S_ISUID) == S_ISUID &&
787 !S_ISDIR(tmp_la->la_mode)) {
788 la->la_mode &= ~S_ISUID;
789 la->la_valid |= LA_MODE;
792 * Likewise, if the user or group of a non-directory
793 * has been changed by a non-root user, remove the
794 * setgid bit UNLESS there is no group execute bit
795 * (this would be a file marked for mandatory
796 * locking). 19981026 David C Niemi <niemi@tux.org>
798 * Removed the fsuid check (see the comment above) --
801 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
802 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
803 la->la_mode &= ~S_ISGID;
804 la->la_valid |= LA_MODE;
806 } else if (la->la_valid & LA_MODE) {
807 int mode = la->la_mode;
809 if (la->la_mode == (umode_t)-1)
810 mode = tmp_la->la_mode;
812 (mode & S_IALLUGO) | (tmp_la->la_mode & ~S_IALLUGO);
818 /* set attr and LOV EA at once, return updated attr */
819 static int mdd_attr_set(const struct lu_context *ctxt,
820 struct md_object *obj,
821 const struct md_attr *ma)
823 struct mdd_object *mdd_obj = md2mdd_obj(obj);
824 struct mdd_device *mdd = mdo2mdd(obj);
825 struct thandle *handle;
826 struct lov_mds_md *lmm = NULL;
827 int rc = 0, lmm_size = 0, max_size;
828 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
831 mdd_txn_param_build(ctxt, &MDD_TXN_ATTR_SET);
832 handle = mdd_trans_start(ctxt, mdd);
834 RETURN(PTR_ERR(handle));
835 /*TODO: add lock here*/
836 /* start a log jounal handle if needed */
837 if (S_ISREG(mdd_object_type(mdd_obj)) &&
838 ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
839 max_size = mdd_lov_mdsize(ctxt, mdd);
840 OBD_ALLOC(lmm, max_size);
842 GOTO(cleanup, rc = -ENOMEM);
844 rc = mdd_get_md(ctxt, mdd_obj, lmm, &lmm_size, 1,
851 if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
852 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
853 ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
855 *la_copy = ma->ma_attr;
856 mdd_write_lock(ctxt, mdd_obj);
857 rc = mdd_fix_attr(ctxt, mdd_obj, ma, la_copy);
858 mdd_write_unlock(ctxt, mdd_obj);
862 if (la_copy->la_valid & LA_FLAGS) {
863 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
866 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
867 } else if (la_copy->la_valid) { /* setattr */
868 rc = mdd_attr_set_internal_locked(ctxt, mdd_obj, la_copy,
870 /* journal chown/chgrp in llog, just like unlink */
871 if (rc == 0 && lmm_size){
872 /*TODO set_attr llog */
876 if (rc == 0 && ma->ma_valid & MA_LOV) {
879 mode = mdd_object_type(mdd_obj);
880 if (S_ISREG(mode) || S_ISDIR(mode)) {
881 /*TODO check permission*/
882 rc = mdd_lov_set_md(ctxt, NULL, mdd_obj, ma->ma_lmm,
883 ma->ma_lmm_size, handle, 1);
888 mdd_trans_stop(ctxt, mdd, rc, handle);
889 if (rc == 0 && lmm_size) {
890 /*set obd attr, if needed*/
891 rc = mdd_lov_setattr_async(ctxt, mdd_obj, lmm, lmm_size);
894 OBD_FREE(lmm, max_size);
900 int mdd_xattr_set_txn(const struct lu_context *ctxt, struct mdd_object *obj,
901 const void *buf, int buf_len, const char *name, int fl,
902 struct thandle *handle)
907 mdd_write_lock(ctxt, obj);
908 rc = __mdd_xattr_set(ctxt, obj, buf, buf_len, name, fl, handle);
909 mdd_write_unlock(ctxt, obj);
914 static int mdd_xattr_set(const struct lu_context *ctxt, struct md_object *obj,
915 const void *buf, int buf_len, const char *name,
918 struct mdd_device *mdd = mdo2mdd(obj);
919 struct thandle *handle;
923 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
924 handle = mdd_trans_start(ctxt, mdd);
926 RETURN(PTR_ERR(handle));
928 rc = mdd_xattr_set_txn(ctxt, md2mdd_obj(obj), buf, buf_len, name,
931 mdd_trans_stop(ctxt, mdd, rc, handle);
936 static int __mdd_xattr_del(const struct lu_context *ctxt,struct mdd_device *mdd,
937 struct mdd_object *obj,
938 const char *name, struct thandle *handle)
940 struct dt_object *next;
942 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
943 next = mdd_object_child(obj);
944 return next->do_ops->do_xattr_del(ctxt, next, name, handle);
947 int mdd_xattr_del(const struct lu_context *ctxt, struct md_object *obj,
950 struct mdd_object *mdd_obj = md2mdd_obj(obj);
951 struct mdd_device *mdd = mdo2mdd(obj);
952 struct thandle *handle;
956 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
957 handle = mdd_trans_start(ctxt, mdd);
959 RETURN(PTR_ERR(handle));
961 mdd_write_lock(ctxt, mdd_obj);
962 rc = __mdd_xattr_del(ctxt, mdd, md2mdd_obj(obj), name, handle);
963 mdd_write_unlock(ctxt, mdd_obj);
965 mdd_trans_stop(ctxt, mdd, rc, handle);
970 static int __mdd_index_insert_only(const struct lu_context *ctxt,
971 struct mdd_object *pobj,
972 const struct lu_fid *lf,
973 const char *name, struct thandle *th)
976 struct dt_object *next = mdd_object_child(pobj);
979 if (dt_try_as_dir(ctxt, next))
980 rc = next->do_index_ops->dio_insert(ctxt, next,
982 (struct dt_key *)name, th);
988 /* insert new index, add reference if isdir, update times */
989 static int __mdd_index_insert(const struct lu_context *ctxt,
990 struct mdd_object *pobj, const struct lu_fid *lf,
991 const char *name, int isdir, struct thandle *th)
994 struct dt_object *next = mdd_object_child(pobj);
998 struct lu_attr *la = &mdd_ctx_info(ctxt)->mti_la;
1001 if (dt_try_as_dir(ctxt, next))
1002 rc = next->do_index_ops->dio_insert(ctxt, next,
1003 (struct dt_rec *)lf,
1004 (struct dt_key *)name, th);
1010 __mdd_ref_add(ctxt, pobj, th);
1012 la->la_valid = LA_MTIME|LA_CTIME;
1013 la->la_atime = ma->ma_attr.la_atime;
1014 la->la_ctime = ma->ma_attr.la_ctime;
1015 rc = mdd_attr_set_internal(ctxt, mdd_obj, la, handle);
1021 static int __mdd_index_delete(const struct lu_context *ctxt,
1022 struct mdd_object *pobj, const char *name,
1023 struct thandle *handle)
1026 struct dt_object *next = mdd_object_child(pobj);
1029 if (dt_try_as_dir(ctxt, next))
1030 rc = next->do_index_ops->dio_delete(ctxt, next,
1031 (struct dt_key *)name, handle);
1037 static int mdd_link_sanity_check(const struct lu_context *ctxt,
1038 struct mdd_object *tgt_obj,
1039 struct mdd_object *src_obj)
1043 rc = mdd_may_create(ctxt, tgt_obj, NULL);
1046 if (S_ISDIR(mdd_object_type(src_obj)))
1049 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
1055 static int mdd_link(const struct lu_context *ctxt, struct md_object *tgt_obj,
1056 struct md_object *src_obj, const char *name,
1059 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1060 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1061 struct mdd_device *mdd = mdo2mdd(src_obj);
1062 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
1063 struct thandle *handle;
1067 mdd_txn_param_build(ctxt, &MDD_TXN_LINK);
1068 handle = mdd_trans_start(ctxt, mdd);
1070 RETURN(PTR_ERR(handle));
1072 mdd_lock2(ctxt, mdd_tobj, mdd_sobj);
1074 rc = mdd_link_sanity_check(ctxt, mdd_tobj, mdd_sobj);
1078 rc = __mdd_index_insert_only(ctxt, mdd_tobj, mdo2fid(mdd_sobj),
1081 __mdd_ref_add(ctxt, mdd_sobj, handle);
1083 *la_copy = ma->ma_attr;
1084 la_copy->la_valid = LA_CTIME;
1085 rc = mdd_attr_set_internal(ctxt, mdd_sobj, la_copy, handle);
1089 la_copy->la_valid = LA_CTIME | LA_MTIME;
1090 rc = mdd_attr_set_internal(ctxt, mdd_tobj, la_copy, handle);
1093 mdd_unlock2(ctxt, mdd_tobj, mdd_sobj);
1094 mdd_trans_stop(ctxt, mdd, rc, handle);
1099 * Check that @dir contains no entries except (possibly) dot and dotdot.
1104 * -ENOTEMPTY not empty
1108 static int mdd_dir_is_empty(const struct lu_context *ctx,
1109 struct mdd_object *dir)
1112 struct dt_object *obj;
1113 struct dt_it_ops *iops;
1116 obj = mdd_object_child(dir);
1117 iops = &obj->do_index_ops->dio_it;
1118 it = iops->init(ctx, obj);
1120 result = iops->get(ctx, it, (const void *)"");
1123 for (result = 0, i = 0; result == 0 && i < 3; ++i)
1124 result = iops->next(ctx, it);
1127 result = -ENOTEMPTY;
1128 else if (result == +1)
1130 } else if (result == 0)
1132 * Huh? Index contains no zero key?
1135 iops->fini(ctx, it);
1141 /* return md_attr back,
1142 * if it is last unlink then return lov ea + llog cookie*/
1143 static inline int __mdd_object_kill(const struct lu_context *ctxt,
1144 struct mdd_object *obj,
1149 mdd_set_dead_obj(obj);
1150 if (S_ISREG(mdd_object_type(obj))) {
1151 rc = __mdd_lmm_get(ctxt, obj, ma);
1152 if (ma->ma_valid & MA_LOV)
1153 rc = mdd_unlink_log(ctxt, mdo2mdd(&obj->mod_obj),
1159 static int __mdd_finish_unlink(const struct lu_context *ctxt,
1160 struct mdd_object *obj, struct md_attr *ma,
1166 rc = __mdd_iattr_get(ctxt, obj, ma);
1167 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
1168 if (atomic_read(&obj->mod_count) == 0) {
1169 rc = __mdd_object_kill(ctxt, obj, ma);
1171 /* add new orphan */
1172 rc = __mdd_orphan_add(ctxt, obj, th);
1178 static int mdd_unlink_sanity_check(const struct lu_context *ctxt,
1179 struct mdd_object *pobj,
1180 struct mdd_object *cobj,
1183 struct dt_object *dt_cobj = mdd_object_child(cobj);
1187 rc = mdd_may_delete(ctxt, pobj, cobj, S_ISDIR(ma->ma_attr.la_mode));
1191 if (S_ISDIR(mdd_object_type(cobj)) &&
1192 dt_try_as_dir(ctxt, dt_cobj)) {
1193 rc = mdd_dir_is_empty(ctxt, cobj);
1201 static int mdd_unlink(const struct lu_context *ctxt, struct md_object *pobj,
1202 struct md_object *cobj, const char *name,
1205 struct mdd_device *mdd = mdo2mdd(pobj);
1206 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1207 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1208 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
1209 struct thandle *handle;
1213 mdd_txn_param_build(ctxt, &MDD_TXN_UNLINK);
1214 handle = mdd_trans_start(ctxt, mdd);
1216 RETURN(PTR_ERR(handle));
1218 mdd_lock2(ctxt, mdd_pobj, mdd_cobj);
1220 rc = mdd_unlink_sanity_check(ctxt, mdd_pobj, mdd_cobj, ma);
1225 rc = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1229 __mdd_ref_del(ctxt, mdd_cobj, handle);
1230 *la_copy = ma->ma_attr;
1231 if (S_ISDIR(lu_object_attr(&cobj->mo_lu))) {
1233 __mdd_ref_del(ctxt, mdd_cobj, handle);
1235 __mdd_ref_del(ctxt, mdd_pobj, handle);
1237 la_copy->la_valid = LA_CTIME;
1238 rc = mdd_attr_set_internal(ctxt, mdd_cobj, la_copy, handle);
1243 la_copy->la_valid = LA_CTIME | LA_MTIME;
1244 rc = mdd_attr_set_internal(ctxt, mdd_pobj, la_copy, handle);
1248 rc = __mdd_finish_unlink(ctxt, mdd_cobj, ma, handle);
1251 mdd_unlock2(ctxt, mdd_pobj, mdd_cobj);
1252 mdd_trans_stop(ctxt, mdd, rc, handle);
1256 static int mdd_parent_fid(const struct lu_context *ctxt,
1257 struct mdd_object *obj,
1260 return mdd_lookup(ctxt, &obj->mod_obj, dotdot, fid);
1264 * return 0: if p2 is the parent of p1
1265 * otherwise: other_value
1267 static int mdd_is_parent(const struct lu_context *ctxt,
1268 struct mdd_device *mdd,
1269 struct mdd_object *p1,
1270 struct mdd_object *p2)
1272 struct lu_fid * pfid;
1273 struct mdd_object *parent = NULL;
1277 pfid = &mdd_ctx_info(ctxt)->mti_fid;
1278 if (lu_fid_eq(mdo2fid(p1), &mdd->mdd_root_fid))
1281 rc = mdd_parent_fid(ctxt, p1, pfid);
1284 if (lu_fid_eq(pfid, mdo2fid(p2)))
1286 if (lu_fid_eq(pfid, &mdd->mdd_root_fid))
1289 mdd_object_put(ctxt, parent);
1290 parent = mdd_object_find(ctxt, mdd, pfid);
1292 GOTO(out, rc = PTR_ERR(parent));
1296 if (parent && !IS_ERR(parent))
1297 mdd_object_put(ctxt, parent);
1301 static int mdd_rename_lock(const struct lu_context *ctxt,
1302 struct mdd_device *mdd,
1303 struct mdd_object *src_pobj,
1304 struct mdd_object *tgt_pobj)
1308 if (src_pobj == tgt_pobj) {
1309 mdd_write_lock(ctxt, src_pobj);
1312 /*compared the parent child relationship of src_p&tgt_p*/
1313 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1314 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1316 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1317 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1321 if (!mdd_is_parent(ctxt, mdd, src_pobj, tgt_pobj)) {
1322 mdd_lock2(ctxt, tgt_pobj, src_pobj);
1326 mdd_lock2(ctxt, src_pobj, tgt_pobj);
1331 static void mdd_rename_unlock(const struct lu_context *ctxt,
1332 struct mdd_object *src_pobj,
1333 struct mdd_object *tgt_pobj)
1335 mdd_write_unlock(ctxt, src_pobj);
1336 if (src_pobj != tgt_pobj)
1337 mdd_write_unlock(ctxt, tgt_pobj);
1340 static int mdd_rename_sanity_check(const struct lu_context *ctxt,
1341 struct mdd_object *src_pobj,
1342 struct mdd_object *tgt_pobj,
1343 struct mdd_object *sobj,
1344 struct mdd_object *tobj)
1346 struct mdd_device *mdd =mdo2mdd(&src_pobj->mod_obj);
1347 int rc = 0, src_is_dir, tgt_is_dir;
1350 src_is_dir = S_ISDIR(mdd_object_type(sobj));
1351 rc = mdd_may_delete(ctxt, src_pobj, sobj, src_is_dir);
1356 rc = mdd_may_create(ctxt, tgt_pobj, NULL);
1359 if (!mdd_is_parent(ctxt, mdd, tgt_pobj, sobj))
1360 GOTO(out, rc = -EINVAL);
1364 /* source should not be ancestor of target */
1365 if (!mdd_is_parent(ctxt, mdd, tobj, sobj))
1366 GOTO(out, rc = -EINVAL);
1368 rc = mdd_may_delete(ctxt, tgt_pobj, tobj, src_is_dir);
1372 tgt_is_dir = S_ISDIR(mdd_object_type(tobj));
1373 if (tgt_is_dir && mdd_dir_is_empty(ctxt, tobj))
1374 GOTO(out, rc = -ENOTEMPTY);
1376 mdd_object_put(ctxt, sobj);
1380 static int mdd_rename(const struct lu_context *ctxt, struct md_object *src_pobj,
1381 struct md_object *tgt_pobj, const struct lu_fid *lf,
1382 const char *sname, struct md_object *tobj,
1383 const char *tname, struct md_attr *ma)
1385 struct mdd_device *mdd = mdo2mdd(src_pobj);
1386 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj);
1387 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1388 struct mdd_object *mdd_sobj = mdd_object_find(ctxt, mdd, lf);
1389 struct mdd_object *mdd_tobj = NULL;
1390 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
1391 struct thandle *handle;
1392 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
1397 mdd_tobj = md2mdd_obj(tobj);
1399 /*XXX: shouldn't this check be done under lock below? */
1400 rc = mdd_rename_sanity_check(ctxt, mdd_spobj, mdd_tpobj,
1401 mdd_sobj, mdd_tobj);
1405 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1406 handle = mdd_trans_start(ctxt, mdd);
1408 RETURN(PTR_ERR(handle));
1410 /*FIXME: Should consider tobj and sobj too in rename_lock*/
1411 rc = mdd_rename_lock(ctxt, mdd, mdd_spobj, mdd_tpobj);
1413 GOTO(cleanup_unlocked, rc);
1415 rc = __mdd_index_delete(ctxt, mdd_spobj, sname, handle);
1419 /*if sobj is dir, its parent object nlink should be dec too*/
1421 __mdd_ref_del(ctxt, mdd_spobj, handle);
1424 rc = __mdd_index_delete(ctxt, mdd_tpobj, tname, handle);
1429 rc = __mdd_index_insert(ctxt, mdd_tpobj, lf, tname, is_dir, handle);
1433 *la_copy = ma->ma_attr;
1434 la_copy->la_valid = LA_CTIME;
1435 rc = mdd_attr_set_internal_locked(ctxt, mdd_sobj, la_copy, handle);
1439 if (tobj && lu_object_exists(&tobj->mo_lu)) {
1440 mdd_write_lock(ctxt, mdd_tobj);
1441 __mdd_ref_del(ctxt, mdd_tobj, handle);
1442 /* remove dot reference */
1444 __mdd_ref_del(ctxt, mdd_tobj, handle);
1446 la_copy->la_valid = LA_CTIME;
1447 rc = mdd_attr_set_internal(ctxt, mdd_tobj, la_copy, handle);
1451 rc = __mdd_finish_unlink(ctxt, mdd_tobj, ma, handle);
1452 mdd_write_unlock(ctxt, mdd_tobj);
1457 la_copy->la_valid = LA_CTIME | LA_MTIME;
1458 rc = mdd_attr_set_internal(ctxt, mdd_spobj, la_copy, handle);
1462 if (mdd_spobj != mdd_tpobj) {
1463 la_copy->la_valid = LA_CTIME | LA_MTIME;
1464 rc = mdd_attr_set_internal(ctxt, mdd_tpobj, la_copy, handle);
1468 mdd_rename_unlock(ctxt, mdd_spobj, mdd_tpobj);
1470 mdd_trans_stop(ctxt, mdd, rc, handle);
1474 static int mdd_lookup(const struct lu_context *ctxt, struct md_object *pobj,
1475 const char *name, struct lu_fid* fid)
1477 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1478 struct dt_object *dir = mdd_object_child(mdd_obj);
1479 struct dt_rec *rec = (struct dt_rec *)fid;
1480 const struct dt_key *key = (const struct dt_key *)name;
1484 if (mdd_is_dead_obj(mdd_obj))
1486 mdd_read_lock(ctxt, mdd_obj);
1487 if (S_ISDIR(mdd_object_type(mdd_obj)) && dt_try_as_dir(ctxt, dir))
1488 rc = dir->do_index_ops->dio_lookup(ctxt, dir, rec, key);
1491 mdd_read_unlock(ctxt, mdd_obj);
1495 static int __mdd_object_initialize(const struct lu_context *ctxt,
1496 const struct lu_fid *pfid,
1497 struct mdd_object *child,
1498 struct md_attr *ma, struct thandle *handle)
1503 /* update attributes for child.
1505 * (1) the valid bits should be converted between Lustre and Linux;
1506 * (2) maybe, the child attributes should be set in OSD when creation.
1509 rc = mdd_attr_set_internal(ctxt, child, &ma->ma_attr, handle);
1513 if (S_ISDIR(ma->ma_attr.la_mode)) {
1514 /* add . and .. for newly created dir */
1515 __mdd_ref_add(ctxt, child, handle);
1516 rc = __mdd_index_insert_only(ctxt, child, mdo2fid(child),
1519 rc = __mdd_index_insert_only(ctxt, child, pfid,
1524 rc2 = __mdd_index_delete(ctxt,
1525 child, dot, handle);
1527 CERROR("Failure to cleanup after dotdot"
1528 " creation: %d (%d)\n", rc2, rc);
1530 __mdd_ref_del(ctxt, child, handle);
1537 static int mdd_create_data(const struct lu_context *ctxt,
1538 struct md_object *pobj, struct md_object *cobj,
1539 const struct md_create_spec *spec,
1542 struct mdd_device *mdd = mdo2mdd(pobj);
1543 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1544 struct mdd_object *son = md2mdd_obj(cobj);
1545 struct lu_attr *attr = &ma->ma_attr;
1546 struct lov_mds_md *lmm = NULL;
1548 struct thandle *handle;
1552 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size, spec,
1557 mdd_txn_param_build(ctxt, &MDD_TXN_CREATE_DATA);
1558 handle = mdd_trans_start(ctxt, mdd);
1560 RETURN(PTR_ERR(handle));
1562 /*XXX: setting the lov ea is not locked but setting the attr is locked? */
1564 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size,
1567 rc = mdd_attr_get_internal_locked(ctxt, son, ma);
1570 mdd_trans_stop(ctxt, mdd, rc, handle);
1574 static int mdd_create_sanity_check(const struct lu_context *ctxt,
1575 struct mdd_device *mdd,
1576 struct md_object *pobj,
1577 const char *name, struct md_attr *ma)
1579 struct mdd_thread_info *info = mdd_ctx_info(ctxt);
1580 struct lu_attr *la = &info->mti_la;
1581 struct lu_fid *fid = &info->mti_fid;
1582 struct mdd_object *obj = md2mdd_obj(pobj);
1587 if (mdd_is_dead_obj(obj))
1589 rc = mdd_lookup(ctxt, pobj, name, fid);
1591 RETURN(rc ? : -EEXIST);
1594 mdd_read_lock(ctxt, obj);
1595 rc = __mdd_la_get(ctxt, obj, la);
1596 mdd_read_unlock(ctxt, obj);
1600 if (la->la_mode & S_ISGID) {
1601 ma->ma_attr.la_gid = la->la_gid;
1602 if (S_ISDIR(ma->ma_attr.la_mode)) {
1603 ma->ma_attr.la_mode |= S_ISGID;
1604 ma->ma_attr.la_valid |= LA_MODE;
1608 switch (ma->ma_attr.la_mode & S_IFMT) {
1626 * Create object and insert it into namespace.
1628 static int mdd_create(const struct lu_context *ctxt, struct md_object *pobj,
1629 const char *name, struct md_object *child,
1630 const struct md_create_spec *spec,
1633 struct mdd_device *mdd = mdo2mdd(pobj);
1634 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1635 struct mdd_object *son = md2mdd_obj(child);
1636 struct lu_attr *la_copy = &mdd_ctx_info(ctxt)->mti_la_for_fix;
1637 struct lu_attr *attr = &ma->ma_attr;
1638 struct lov_mds_md *lmm = NULL;
1639 struct thandle *handle;
1640 int rc, created = 0, inserted = 0, lmm_size = 0;
1643 /* sanity checks before big job */
1644 rc = mdd_create_sanity_check(ctxt, mdd, pobj, name, ma);
1647 /* no RPC inside the transaction, so OST objects should be created at
1649 if (S_ISREG(attr->la_mode)) {
1650 rc = mdd_lov_create(ctxt, mdd, mdd_pobj, son, &lmm, &lmm_size,
1656 mdd_txn_param_build(ctxt, &MDD_TXN_MKDIR);
1657 handle = mdd_trans_start(ctxt, mdd);
1659 RETURN(PTR_ERR(handle));
1661 mdd_write_lock(ctxt, mdd_pobj);
1664 * XXX check that link can be added to the parent in mkdir case.
1668 * Two operations have to be performed:
1670 * - allocation of new object (->do_create()), and
1672 * - insertion into parent index (->dio_insert()).
1674 * Due to locking, operation order is not important, when both are
1675 * successful, *but* error handling cases are quite different:
1677 * - if insertion is done first, and following object creation fails,
1678 * insertion has to be rolled back, but this operation might fail
1679 * also leaving us with dangling index entry.
1681 * - if creation is done first, is has to be undone if insertion
1682 * fails, leaving us with leaked space, which is neither good, nor
1685 * It seems that creation-first is simplest solution, but it is
1686 * sub-optimal in the frequent
1691 * case, because second mkdir is bound to create object, only to
1692 * destroy it immediately.
1694 * Note that local file systems do
1696 * 0. lookup -> -EEXIST
1702 * Maybe we should do the same. For now: creation-first.
1705 mdd_write_lock(ctxt, son);
1706 rc = __mdd_object_create(ctxt, son, ma, handle);
1708 mdd_write_unlock(ctxt, son);
1714 rc = __mdd_object_initialize(ctxt, mdo2fid(mdd_pobj),
1716 mdd_write_unlock(ctxt, son);
1719 * Object has no links, so it will be destroyed when last
1720 * reference is released. (XXX not now.)
1724 rc = __mdd_index_insert(ctxt, mdd_pobj, mdo2fid(son),
1725 name, S_ISDIR(attr->la_mode), handle);
1731 rc = mdd_lov_set_md(ctxt, mdd_pobj, son, lmm, lmm_size, handle, 0);
1733 CERROR("error on stripe info copy %d \n", rc);
1737 if (S_ISLNK(attr->la_mode)) {
1738 struct dt_object *dt = mdd_object_child(son);
1739 const char *target_name = spec->u.sp_symname;
1740 int sym_len = strlen(target_name);
1743 rc = dt->do_body_ops->dbo_write(ctxt, dt, target_name,
1744 sym_len, &pos, handle);
1751 *la_copy = ma->ma_attr;
1752 la_copy->la_valid = LA_CTIME | LA_MTIME;
1753 rc = mdd_attr_set_internal(ctxt, mdd_pobj, la_copy, handle);
1757 /* return attr back */
1758 rc = mdd_attr_get_internal_locked(ctxt, son, ma);
1760 if (rc && created) {
1764 rc2 = __mdd_index_delete(ctxt, mdd_pobj, name, handle);
1766 CERROR("error can not cleanup destroy %d\n",
1770 __mdd_ref_del(ctxt, son, handle);
1773 OBD_FREE(lmm, lmm_size);
1774 mdd_write_unlock(ctxt, mdd_pobj);
1775 mdd_trans_stop(ctxt, mdd, rc, handle);
1778 /* partial operation */
1779 static int mdd_object_create(const struct lu_context *ctxt,
1780 struct md_object *obj,
1781 const struct md_create_spec *spec,
1785 struct mdd_device *mdd = mdo2mdd(obj);
1786 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1787 struct thandle *handle;
1791 mdd_txn_param_build(ctxt, &MDD_TXN_OBJECT_CREATE);
1792 handle = mdd_trans_start(ctxt, mdd);
1794 RETURN(PTR_ERR(handle));
1796 mdd_write_lock(ctxt, mdd_obj);
1797 rc = __mdd_object_create(ctxt, mdd_obj, ma, handle);
1799 rc = __mdd_object_initialize(ctxt, spec->u.sp_pfid, mdd_obj,
1801 mdd_write_unlock(ctxt, mdd_obj);
1804 rc = mdd_attr_get_internal_locked(ctxt, mdd_obj, ma);
1806 mdd_trans_stop(ctxt, mdd, rc, handle);
1809 /* partial operation */
1810 static int mdd_name_insert(const struct lu_context *ctxt,
1811 struct md_object *pobj, const char *name,
1812 const struct lu_fid *fid, int isdir)
1814 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1815 struct thandle *handle;
1819 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_INSERT);
1820 handle = mdd_trans_start(ctxt, mdo2mdd(pobj));
1822 RETURN(PTR_ERR(handle));
1824 mdd_write_lock(ctxt, mdd_obj);
1825 rc = __mdd_index_insert(ctxt, mdd_obj, fid, name, isdir, handle);
1826 mdd_write_unlock(ctxt, mdd_obj);
1828 mdd_trans_stop(ctxt, mdo2mdd(pobj), rc, handle);
1832 static int mdd_name_remove(const struct lu_context *ctxt,
1833 struct md_object *pobj,
1836 struct mdd_device *mdd = mdo2mdd(pobj);
1837 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1838 struct thandle *handle;
1842 mdd_txn_param_build(ctxt, &MDD_TXN_INDEX_DELETE);
1843 handle = mdd_trans_start(ctxt, mdd);
1845 RETURN(PTR_ERR(handle));
1847 mdd_write_lock(ctxt, mdd_obj);
1849 rc = __mdd_index_delete(ctxt, mdd_obj, name, handle);
1851 mdd_write_unlock(ctxt, mdd_obj);
1853 mdd_trans_stop(ctxt, mdd, rc, handle);
1857 static int mdd_rename_tgt(const struct lu_context *ctxt, struct md_object *pobj,
1858 struct md_object *tobj, const struct lu_fid *lf,
1861 struct mdd_device *mdd = mdo2mdd(pobj);
1862 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1863 struct mdd_object *mdd_tobj = NULL;
1864 struct thandle *handle;
1868 mdd_txn_param_build(ctxt, &MDD_TXN_RENAME);
1869 handle = mdd_trans_start(ctxt, mdd);
1871 RETURN(PTR_ERR(handle));
1874 mdd_tobj = md2mdd_obj(tobj);
1876 mdd_lock2(ctxt, mdd_tpobj, mdd_tobj);
1878 /*TODO rename sanity checking*/
1880 rc = __mdd_index_delete(ctxt, mdd_tpobj, name, handle);
1885 rc = __mdd_index_insert_only(ctxt, mdd_tpobj, lf, name, handle);
1889 if (tobj && lu_object_exists(&tobj->mo_lu))
1890 __mdd_ref_del(ctxt, mdd_tobj, handle);
1892 mdd_unlock2(ctxt, mdd_tpobj, mdd_tobj);
1893 mdd_trans_stop(ctxt, mdd, rc, handle);
1897 static int mdd_root_get(const struct lu_context *ctx,
1898 struct md_device *m, struct lu_fid *f)
1900 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1903 *f = mdd->mdd_root_fid;
1907 static int mdd_statfs(const struct lu_context *ctx,
1908 struct md_device *m, struct kstatfs *sfs)
1910 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1915 rc = mdd_child_ops(mdd)->dt_statfs(ctx, mdd->mdd_child, sfs);
1920 static int mdd_get_maxsize(const struct lu_context *ctx,
1921 struct md_device *m, int *md_size,
1924 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1927 *md_size = mdd_lov_mdsize(ctx, mdd);
1928 *cookie_size = mdd_lov_cookiesize(ctx, mdd);
1933 static void __mdd_ref_add(const struct lu_context *ctxt, struct mdd_object *obj,
1934 struct thandle *handle)
1936 struct dt_object *next;
1938 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
1939 next = mdd_object_child(obj);
1940 next->do_ops->do_ref_add(ctxt, next, handle);
1943 static int mdd_ref_add(const struct lu_context *ctxt, struct md_object *obj)
1945 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1946 struct mdd_device *mdd = mdo2mdd(obj);
1947 struct thandle *handle;
1950 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1951 handle = mdd_trans_start(ctxt, mdd);
1955 mdd_write_lock(ctxt, mdd_obj);
1956 __mdd_ref_add(ctxt, mdd_obj, handle);
1957 mdd_write_unlock(ctxt, mdd_obj);
1959 mdd_trans_stop(ctxt, mdd, 0, handle);
1965 __mdd_ref_del(const struct lu_context *ctxt, struct mdd_object *obj,
1966 struct thandle *handle)
1968 struct dt_object *next = mdd_object_child(obj);
1970 LASSERT(lu_object_exists(mdd2lu_obj(obj)));
1972 next->do_ops->do_ref_del(ctxt, next, handle);
1975 static int mdd_ref_del(const struct lu_context *ctxt, struct md_object *obj,
1978 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1979 struct mdd_device *mdd = mdo2mdd(obj);
1980 struct thandle *handle;
1985 mdd_txn_param_build(ctxt, &MDD_TXN_XATTR_SET);
1986 handle = mdd_trans_start(ctxt, mdd);
1990 mdd_write_lock(ctxt, mdd_obj);
1992 isdir = S_ISDIR(lu_object_attr(&obj->mo_lu));
1994 if (isdir && dt_try_as_dir(ctxt, mdd_object_child(mdd_obj))) {
1995 rc = mdd_dir_is_empty(ctxt, mdd_obj);
2000 __mdd_ref_del(ctxt, mdd_obj, handle);
2004 __mdd_ref_del(ctxt, mdd_obj, handle);
2007 rc = __mdd_finish_unlink(ctxt, mdd_obj, ma, handle);
2010 mdd_write_unlock(ctxt, mdd_obj);
2011 mdd_trans_stop(ctxt, mdd, rc, handle);
2015 /* do NOT or the MAY_*'s, you'll get the weakest */
2016 static int accmode(struct mdd_object *mdd_obj, int flags)
2021 /* Sadly, NFSD reopens a file repeatedly during operation, so the
2022 * "acc_mode = 0" allowance for newly-created files isn't honoured.
2023 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
2024 * owner can write to a file even if it is marked readonly to hide
2025 * its brokenness. (bug 5781) */
2026 if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
2029 if (flags & FMODE_READ)
2031 if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
2033 if (flags & MDS_FMODE_EXEC)
2038 static int mdd_open(const struct lu_context *ctxt, struct md_object *obj,
2041 int mode = accmode(md2mdd_obj(obj), flags);
2043 if (mode & MAY_WRITE) {
2044 if (mdd_is_immutable(md2mdd_obj(obj)))
2048 atomic_inc(&md2mdd_obj(obj)->mod_count);
2052 static int mdd_close(const struct lu_context *ctxt, struct md_object *obj,
2056 struct mdd_object *mdd_obj;
2057 struct thandle *handle = NULL;
2060 mdd_obj = md2mdd_obj(obj);
2061 mdd_read_lock(ctxt, mdd_obj);
2062 rc = __mdd_iattr_get(ctxt, mdd_obj, ma);
2064 GOTO(out_locked, rc);
2066 if (atomic_dec_and_test(&mdd_obj->mod_count)) {
2067 if (ma->ma_attr.la_nlink == 0) {
2068 rc = __mdd_object_kill(ctxt, mdd_obj, ma);
2070 GOTO(out_locked, rc);
2071 mdd_read_unlock(ctxt, mdd_obj);
2072 /* let's remove obj from the orphan list */
2073 mdd_txn_param_build(ctxt, &MDD_TXN_MKDIR);
2074 handle = mdd_trans_start(ctxt, mdo2mdd(obj));
2076 GOTO(out, rc = -ENOMEM);
2078 rc = __mdd_orphan_del(ctxt, mdd_obj, handle);
2080 mdd_trans_stop(ctxt, mdo2mdd(obj), rc, handle);
2085 mdd_read_unlock(ctxt, mdd_obj);
2090 static int mdd_readpage(const struct lu_context *ctxt, struct md_object *obj,
2091 const struct lu_rdpg *rdpg)
2093 struct dt_object *next;
2094 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2097 LASSERT(lu_object_exists(mdd2lu_obj(mdd_obj)));
2098 next = mdd_object_child(mdd_obj);
2100 mdd_read_lock(ctxt, mdd_obj);
2101 if (S_ISDIR(mdd_object_type(mdd_obj)) &&
2102 dt_try_as_dir(ctxt, next))
2103 rc = next->do_ops->do_readpage(ctxt, next, rdpg);
2106 mdd_read_unlock(ctxt, mdd_obj);
2110 struct md_device_operations mdd_ops = {
2111 .mdo_root_get = mdd_root_get,
2112 .mdo_statfs = mdd_statfs,
2113 .mdo_get_maxsize = mdd_get_maxsize,
2116 static struct md_dir_operations mdd_dir_ops = {
2117 .mdo_lookup = mdd_lookup,
2118 .mdo_create = mdd_create,
2119 .mdo_rename = mdd_rename,
2120 .mdo_link = mdd_link,
2121 .mdo_unlink = mdd_unlink,
2122 .mdo_name_insert = mdd_name_insert,
2123 .mdo_name_remove = mdd_name_remove,
2124 .mdo_rename_tgt = mdd_rename_tgt,
2125 .mdo_create_data = mdd_create_data
2129 static struct md_object_operations mdd_obj_ops = {
2130 .moo_attr_get = mdd_attr_get,
2131 .moo_attr_set = mdd_attr_set,
2132 .moo_xattr_get = mdd_xattr_get,
2133 .moo_xattr_set = mdd_xattr_set,
2134 .moo_xattr_list = mdd_xattr_list,
2135 .moo_xattr_del = mdd_xattr_del,
2136 .moo_object_create = mdd_object_create,
2137 .moo_ref_add = mdd_ref_add,
2138 .moo_ref_del = mdd_ref_del,
2139 .moo_open = mdd_open,
2140 .moo_close = mdd_close,
2141 .moo_readpage = mdd_readpage,
2142 .moo_readlink = mdd_readlink
2145 static struct obd_ops mdd_obd_device_ops = {
2146 .o_owner = THIS_MODULE
2149 static struct lu_device *mdd_device_alloc(const struct lu_context *ctx,
2150 struct lu_device_type *t,
2151 struct lustre_cfg *lcfg)
2153 struct lu_device *l;
2154 struct mdd_device *m;
2158 l = ERR_PTR(-ENOMEM);
2160 md_device_init(&m->mdd_md_dev, t);
2162 l->ld_ops = &mdd_lu_ops;
2163 m->mdd_md_dev.md_ops = &mdd_ops;
2169 static void mdd_device_free(const struct lu_context *ctx,
2170 struct lu_device *lu)
2172 struct mdd_device *m = lu2mdd_dev(lu);
2174 LASSERT(atomic_read(&lu->ld_ref) == 0);
2175 md_device_fini(&m->mdd_md_dev);
2179 static int mdd_type_init(struct lu_device_type *t)
2181 return lu_context_key_register(&mdd_thread_key);
2184 static void mdd_type_fini(struct lu_device_type *t)
2186 lu_context_key_degister(&mdd_thread_key);
2189 static struct lu_device_type_operations mdd_device_type_ops = {
2190 .ldto_init = mdd_type_init,
2191 .ldto_fini = mdd_type_fini,
2193 .ldto_device_alloc = mdd_device_alloc,
2194 .ldto_device_free = mdd_device_free,
2196 .ldto_device_init = mdd_device_init,
2197 .ldto_device_fini = mdd_device_fini
2200 static struct lu_device_type mdd_device_type = {
2201 .ldt_tags = LU_DEVICE_MD,
2202 .ldt_name = LUSTRE_MDD0_NAME,
2203 .ldt_ops = &mdd_device_type_ops,
2204 .ldt_ctx_tags = LCT_MD_THREAD
2207 static void *mdd_key_init(const struct lu_context *ctx,
2208 struct lu_context_key *key)
2210 struct mdd_thread_info *info;
2212 OBD_ALLOC_PTR(info);
2214 info = ERR_PTR(-ENOMEM);
2218 static void mdd_key_fini(const struct lu_context *ctx,
2219 struct lu_context_key *key, void *data)
2221 struct mdd_thread_info *info = data;
2225 static struct lu_context_key mdd_thread_key = {
2226 .lct_tags = LCT_MD_THREAD,
2227 .lct_init = mdd_key_init,
2228 .lct_fini = mdd_key_fini
2231 struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
2235 struct lprocfs_vars lprocfs_mdd_module_vars[] = {
2239 LPROCFS_INIT_VARS(mdd, lprocfs_mdd_module_vars, lprocfs_mdd_obd_vars);
2241 static int __init mdd_mod_init(void)
2243 struct lprocfs_static_vars lvars;
2245 lprocfs_init_vars(mdd, &lvars);
2246 return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
2247 LUSTRE_MDD0_NAME, &mdd_device_type);
2250 static void __exit mdd_mod_exit(void)
2252 class_unregister_type(LUSTRE_MDD0_NAME);
2255 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2256 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD0_NAME")");
2257 MODULE_LICENSE("GPL");
2259 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);