1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/cmm/cmm_object.c
5 * Lustre Cluster Metadata Manager (cmm)
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Mike Pershin <tappro@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.
30 # define EXPORT_SYMTAB
33 #define DEBUG_SUBSYSTEM S_MDS
35 #include <lustre_fid.h>
36 #include "cmm_internal.h"
37 #include "mdc_internal.h"
39 static int cmm_fld_lookup(struct cmm_device *cm,
40 const struct lu_fid *fid, mdsno_t *mds,
41 const struct lu_env *env)
47 LASSERT(fid_is_sane(fid));
49 ls = cm->cmm_md_dev.md_lu_dev.ld_site;
51 rc = fld_client_lookup(ls->ls_client_fld,
52 fid_seq(fid), mds, env);
54 CERROR("Can't find mds by seq "LPX64", rc %d\n",
59 if (*mds > cm->cmm_tgt_count) {
60 CERROR("Got invalid mdsno: "LPU64" (max: %u)\n",
61 *mds, cm->cmm_tgt_count);
64 CDEBUG(D_INFO, "CMM: got MDS "LPU64" for sequence: "LPU64"\n",
71 static struct md_object_operations cml_mo_ops;
72 static struct md_dir_operations cml_dir_ops;
73 static struct lu_object_operations cml_obj_ops;
75 static struct md_object_operations cmr_mo_ops;
76 static struct md_dir_operations cmr_dir_ops;
77 static struct lu_object_operations cmr_obj_ops;
79 struct lu_object *cmm_object_alloc(const struct lu_env *env,
80 const struct lu_object_header *loh,
83 struct lu_object *lo = NULL;
84 const struct lu_fid *fid = &loh->loh_fid;
85 struct cmm_device *cd;
92 if (cd->cmm_flags & CMM_INITIALIZED) {
93 /* get object location */
94 rc = cmm_fld_lookup(lu2cmm_dev(ld), fid, &mdsnum, env);
99 * Device is not yet initialized, cmm_object is being created
100 * as part of early bootstrap procedure (it is /ROOT, or /fld,
101 * etc.). Such object *has* to be local.
103 mdsnum = cd->cmm_local_num;
105 /* select the proper set of operations based on object location */
106 if (mdsnum == cd->cmm_local_num) {
107 struct cml_object *clo;
111 lo = &clo->cmm_obj.cmo_obj.mo_lu;
112 lu_object_init(lo, NULL, ld);
113 clo->cmm_obj.cmo_obj.mo_ops = &cml_mo_ops;
114 clo->cmm_obj.cmo_obj.mo_dir_ops = &cml_dir_ops;
115 lo->lo_ops = &cml_obj_ops;
118 struct cmr_object *cro;
122 lo = &cro->cmm_obj.cmo_obj.mo_lu;
123 lu_object_init(lo, NULL, ld);
124 cro->cmm_obj.cmo_obj.mo_ops = &cmr_mo_ops;
125 cro->cmm_obj.cmo_obj.mo_dir_ops = &cmr_dir_ops;
126 lo->lo_ops = &cmr_obj_ops;
127 cro->cmo_num = mdsnum;
134 * CMM has two types of objects - local and remote. They have different set
135 * of operations so we are avoiding multiple checks in code.
139 * local CMM object operations. cml_...
141 static inline struct cml_object *lu2cml_obj(struct lu_object *o)
143 return container_of0(o, struct cml_object, cmm_obj.cmo_obj.mo_lu);
145 static inline struct cml_object *md2cml_obj(struct md_object *mo)
147 return container_of0(mo, struct cml_object, cmm_obj.cmo_obj);
149 static inline struct cml_object *cmm2cml_obj(struct cmm_object *co)
151 return container_of0(co, struct cml_object, cmm_obj);
153 /* get local child device */
154 static struct lu_device *cml_child_dev(struct cmm_device *d)
156 return &d->cmm_child->md_lu_dev;
159 /* lu_object operations */
160 static void cml_object_free(const struct lu_env *env,
161 struct lu_object *lo)
163 struct cml_object *clo = lu2cml_obj(lo);
168 static int cml_object_init(const struct lu_env *env, struct lu_object *lo)
170 struct cmm_device *cd = lu2cmm_dev(lo->lo_dev);
171 struct lu_device *c_dev;
172 struct lu_object *c_obj;
177 c_dev = cml_child_dev(cd);
181 c_obj = c_dev->ld_ops->ldo_object_alloc(env,
182 lo->lo_header, c_dev);
184 lu_object_add(lo, c_obj);
194 static int cml_object_print(const struct lu_env *env, void *cookie,
195 lu_printer_t p, const struct lu_object *lo)
197 return (*p)(env, cookie, LUSTRE_CMM_NAME"-local@%p", lo);
200 static struct lu_object_operations cml_obj_ops = {
201 .loo_object_init = cml_object_init,
202 .loo_object_free = cml_object_free,
203 .loo_object_print = cml_object_print
206 /* CMM local md_object operations */
207 static int cml_object_create(const struct lu_env *env,
208 struct md_object *mo,
209 const struct md_create_spec *spec,
210 struct md_attr *attr)
214 rc = mo_object_create(env, md_object_next(mo), spec, attr);
218 static int cml_permission(const struct lu_env *env,
219 struct md_object *mo, int mask)
223 rc = mo_permission(env, md_object_next(mo), mask);
227 static int cml_attr_get(const struct lu_env *env, struct md_object *mo,
228 struct md_attr *attr)
232 rc = mo_attr_get(env, md_object_next(mo), attr);
236 static int cml_attr_set(const struct lu_env *env, struct md_object *mo,
237 const struct md_attr *attr)
241 rc = mo_attr_set(env, md_object_next(mo), attr);
245 static int cml_xattr_get(const struct lu_env *env, struct md_object *mo,
246 struct lu_buf *buf, const char *name)
250 rc = mo_xattr_get(env, md_object_next(mo), buf, name);
254 static int cml_readlink(const struct lu_env *env, struct md_object *mo,
259 rc = mo_readlink(env, md_object_next(mo), buf);
263 static int cml_xattr_list(const struct lu_env *env, struct md_object *mo,
268 rc = mo_xattr_list(env, md_object_next(mo), buf);
272 static int cml_xattr_set(const struct lu_env *env, struct md_object *mo,
273 const struct lu_buf *buf,
274 const char *name, int fl)
278 rc = mo_xattr_set(env, md_object_next(mo), buf, name, fl);
282 static int cml_xattr_del(const struct lu_env *env, struct md_object *mo,
287 rc = mo_xattr_del(env, md_object_next(mo), name);
291 static int cml_ref_add(const struct lu_env *env, struct md_object *mo)
295 rc = mo_ref_add(env, md_object_next(mo));
299 static int cml_ref_del(const struct lu_env *env, struct md_object *mo,
304 rc = mo_ref_del(env, md_object_next(mo), ma);
308 static int cml_open(const struct lu_env *env, struct md_object *mo,
313 rc = mo_open(env, md_object_next(mo), flags);
317 static int cml_close(const struct lu_env *env, struct md_object *mo,
322 rc = mo_close(env, md_object_next(mo), ma);
326 static int cml_readpage(const struct lu_env *env, struct md_object *mo,
327 const struct lu_rdpg *rdpg)
331 rc = mo_readpage(env, md_object_next(mo), rdpg);
335 static int cml_capa_get(const struct lu_env *env, struct md_object *mo,
336 struct lustre_capa *capa, int renewal)
340 rc = mo_capa_get(env, md_object_next(mo), capa, renewal);
344 static struct md_object_operations cml_mo_ops = {
345 .moo_permission = cml_permission,
346 .moo_attr_get = cml_attr_get,
347 .moo_attr_set = cml_attr_set,
348 .moo_xattr_get = cml_xattr_get,
349 .moo_xattr_list = cml_xattr_list,
350 .moo_xattr_set = cml_xattr_set,
351 .moo_xattr_del = cml_xattr_del,
352 .moo_object_create = cml_object_create,
353 .moo_ref_add = cml_ref_add,
354 .moo_ref_del = cml_ref_del,
355 .moo_open = cml_open,
356 .moo_close = cml_close,
357 .moo_readpage = cml_readpage,
358 .moo_readlink = cml_readlink,
359 .moo_capa_get = cml_capa_get
362 /* md_dir operations */
363 static int cml_lookup(const struct lu_env *env, struct md_object *mo_p,
364 const char *name, struct lu_fid *lf)
368 rc = mdo_lookup(env, md_object_next(mo_p), name, lf);
373 static int cml_create(const struct lu_env *env,
374 struct md_object *mo_p, const char *child_name,
375 struct md_object *mo_c, const struct md_create_spec *spec,
381 #ifdef HAVE_SPLIT_SUPPORT
382 rc = cml_try_to_split(env, mo_p);
387 rc = mdo_create(env, md_object_next(mo_p), child_name,
388 md_object_next(mo_c), spec, ma);
394 static int cml_create_data(const struct lu_env *env, struct md_object *p,
396 const struct md_create_spec *spec,
401 rc = mdo_create_data(env, md_object_next(p), md_object_next(o),
406 static int cml_link(const struct lu_env *env, struct md_object *mo_p,
407 struct md_object *mo_s, const char *name,
412 rc = mdo_link(env, md_object_next(mo_p), md_object_next(mo_s),
417 static int cml_unlink(const struct lu_env *env, struct md_object *mo_p,
418 struct md_object *mo_c, const char *name,
423 rc = mdo_unlink(env, md_object_next(mo_p), md_object_next(mo_c),
428 /* rename is split to local/remote by location of new parent dir */
429 struct md_object *md_object_find(const struct lu_env *env,
430 struct md_device *md,
431 const struct lu_fid *f)
437 o = lu_object_find(env, md2lu_dev(md)->ld_site, f);
439 m = (struct md_object *)o;
441 o = lu_object_locate(o->lo_header, md2lu_dev(md)->ld_type);
442 m = o ? lu2md(o) : NULL;
447 static int __cmm_mode_get(const struct lu_env *env, struct md_device *md,
448 const struct lu_fid *lf, struct md_attr *ma)
450 struct cmm_thread_info *cmi;
451 struct md_object *mo_s = md_object_find(env, md, lf);
452 struct md_attr *tmp_ma;
457 RETURN(PTR_ERR(mo_s));
459 cmi = cmm_env_info(env);
461 tmp_ma = &cmi->cmi_ma;
462 tmp_ma->ma_need = MA_INODE;
464 /* get type from src, can be remote req */
465 rc = mo_attr_get(env, md_object_next(mo_s), tmp_ma);
467 ma->ma_attr.la_mode = tmp_ma->ma_attr.la_mode;
468 ma->ma_attr.la_flags = tmp_ma->ma_attr.la_flags;
469 ma->ma_attr.la_valid |= LA_MODE | LA_FLAGS;
471 lu_object_put(env, &mo_s->mo_lu);
475 static int cml_rename(const struct lu_env *env, struct md_object *mo_po,
476 struct md_object *mo_pn, const struct lu_fid *lf,
477 const char *s_name, struct md_object *mo_t,
478 const char *t_name, struct md_attr *ma)
483 rc = __cmm_mode_get(env, md_obj2dev(mo_po), lf, ma);
487 if (mo_t && lu_object_exists(&mo_t->mo_lu) < 0) {
488 /* mo_t is remote object and there is RPC to unlink it */
489 rc = mo_ref_del(env, md_object_next(mo_t), ma);
495 /* local rename, mo_t can be NULL */
496 rc = mdo_rename(env, md_object_next(mo_po),
497 md_object_next(mo_pn), lf, s_name,
498 md_object_next(mo_t), t_name, ma);
502 static int cml_rename_tgt(const struct lu_env *env, struct md_object *mo_p,
503 struct md_object *mo_t, const struct lu_fid *lf,
504 const char *name, struct md_attr *ma)
509 rc = mdo_rename_tgt(env, md_object_next(mo_p),
510 md_object_next(mo_t), lf, name, ma);
513 /* used only in case of rename_tgt() when target is not exist */
514 static int cml_name_insert(const struct lu_env *env, struct md_object *p,
515 const char *name, const struct lu_fid *lf, int isdir)
520 rc = mdo_name_insert(env, md_object_next(p), name, lf, isdir);
525 /* Common method for remote and local use. */
526 static int cmm_is_subdir(const struct lu_env *env, struct md_object *mo,
527 const struct lu_fid *fid, struct lu_fid *sfid)
529 struct cmm_thread_info *cmi;
533 cmi = cmm_env_info(env);
534 rc = __cmm_mode_get(env, md_obj2dev(mo), fid, &cmi->cmi_ma);
538 if (!S_ISDIR(cmi->cmi_ma.ma_attr.la_mode))
541 rc = mdo_is_subdir(env, md_object_next(mo), fid, sfid);
545 static struct md_dir_operations cml_dir_ops = {
546 .mdo_is_subdir = cmm_is_subdir,
547 .mdo_lookup = cml_lookup,
548 .mdo_create = cml_create,
549 .mdo_link = cml_link,
550 .mdo_unlink = cml_unlink,
551 .mdo_name_insert = cml_name_insert,
552 .mdo_rename = cml_rename,
553 .mdo_rename_tgt = cml_rename_tgt,
554 .mdo_create_data = cml_create_data
557 /* -------------------------------------------------------------------
558 * remote CMM object operations. cmr_...
560 static inline struct cmr_object *lu2cmr_obj(struct lu_object *o)
562 return container_of0(o, struct cmr_object, cmm_obj.cmo_obj.mo_lu);
564 static inline struct cmr_object *md2cmr_obj(struct md_object *mo)
566 return container_of0(mo, struct cmr_object, cmm_obj.cmo_obj);
568 static inline struct cmr_object *cmm2cmr_obj(struct cmm_object *co)
570 return container_of0(co, struct cmr_object, cmm_obj);
573 /* get proper child device from MDCs */
574 static struct lu_device *cmr_child_dev(struct cmm_device *d, __u32 num)
576 struct lu_device *next = NULL;
577 struct mdc_device *mdc;
579 spin_lock(&d->cmm_tgt_guard);
580 list_for_each_entry(mdc, &d->cmm_targets, mc_linkage) {
581 if (mdc->mc_num == num) {
582 next = mdc2lu_dev(mdc);
586 spin_unlock(&d->cmm_tgt_guard);
590 /* lu_object operations */
591 static void cmr_object_free(const struct lu_env *env,
592 struct lu_object *lo)
594 struct cmr_object *cro = lu2cmr_obj(lo);
599 static int cmr_object_init(const struct lu_env *env, struct lu_object *lo)
601 struct cmm_device *cd = lu2cmm_dev(lo->lo_dev);
602 struct lu_device *c_dev;
603 struct lu_object *c_obj;
608 c_dev = cmr_child_dev(cd, lu2cmr_obj(lo)->cmo_num);
612 c_obj = c_dev->ld_ops->ldo_object_alloc(env,
613 lo->lo_header, c_dev);
615 lu_object_add(lo, c_obj);
625 static int cmr_object_print(const struct lu_env *env, void *cookie,
626 lu_printer_t p, const struct lu_object *lo)
628 return (*p)(env, cookie, LUSTRE_CMM_NAME"-remote@%p", lo);
631 static struct lu_object_operations cmr_obj_ops = {
632 .loo_object_init = cmr_object_init,
633 .loo_object_free = cmr_object_free,
634 .loo_object_print = cmr_object_print
637 /* CMM remote md_object operations. All are invalid */
638 static int cmr_object_create(const struct lu_env *env,
639 struct md_object *mo,
640 const struct md_create_spec *spec,
646 static int cmr_permission(const struct lu_env *env, struct md_object *mo,
652 static int cmr_attr_get(const struct lu_env *env, struct md_object *mo,
653 struct md_attr *attr)
658 static int cmr_attr_set(const struct lu_env *env, struct md_object *mo,
659 const struct md_attr *attr)
664 static int cmr_xattr_get(const struct lu_env *env, struct md_object *mo,
665 struct lu_buf *buf, const char *name)
670 static int cmr_readlink(const struct lu_env *env, struct md_object *mo,
676 static int cmr_xattr_list(const struct lu_env *env, struct md_object *mo,
682 static int cmr_xattr_set(const struct lu_env *env, struct md_object *mo,
683 const struct lu_buf *buf, const char *name, int fl)
688 static int cmr_xattr_del(const struct lu_env *env, struct md_object *mo,
694 static int cmr_ref_add(const struct lu_env *env, struct md_object *mo)
699 static int cmr_ref_del(const struct lu_env *env, struct md_object *mo,
705 static int cmr_open(const struct lu_env *env, struct md_object *mo,
711 static int cmr_close(const struct lu_env *env, struct md_object *mo,
717 static int cmr_readpage(const struct lu_env *env, struct md_object *mo,
718 const struct lu_rdpg *rdpg)
723 static int cmr_capa_get(const struct lu_env *env, struct md_object *mo,
724 struct lustre_capa *capa, int renewal)
729 static struct md_object_operations cmr_mo_ops = {
730 .moo_permission = cmr_permission,
731 .moo_attr_get = cmr_attr_get,
732 .moo_attr_set = cmr_attr_set,
733 .moo_xattr_get = cmr_xattr_get,
734 .moo_xattr_set = cmr_xattr_set,
735 .moo_xattr_list = cmr_xattr_list,
736 .moo_xattr_del = cmr_xattr_del,
737 .moo_object_create = cmr_object_create,
738 .moo_ref_add = cmr_ref_add,
739 .moo_ref_del = cmr_ref_del,
740 .moo_open = cmr_open,
741 .moo_close = cmr_close,
742 .moo_readpage = cmr_readpage,
743 .moo_readlink = cmr_readlink,
744 .moo_capa_get = cmr_capa_get
747 /* remote part of md_dir operations */
748 static int cmr_lookup(const struct lu_env *env, struct md_object *mo_p,
749 const char *name, struct lu_fid *lf)
752 * This can happens while rename() If new parent is remote dir, lookup
760 * All methods below are cross-ref by nature. They consist of remote call and
761 * local operation. Due to future rollback functionality there are several
762 * limitations for such methods:
763 * 1) remote call should be done at first to do epoch negotiation between all
764 * MDS involved and to avoid the RPC inside transaction.
765 * 2) only one RPC can be sent - also due to epoch negotiation.
766 * For more details see rollback HLD/DLD.
768 static int cmr_create(const struct lu_env *env, struct md_object *mo_p,
769 const char *child_name, struct md_object *mo_c,
770 const struct md_create_spec *spec,
773 struct cmm_thread_info *cmi;
774 struct md_attr *tmp_ma;
778 /* check the SGID attr */
779 cmi = cmm_env_info(env);
781 tmp_ma = &cmi->cmi_ma;
782 tmp_ma->ma_need = MA_INODE;
783 rc = mo_attr_get(env, md_object_next(mo_p), tmp_ma);
787 if (tmp_ma->ma_attr.la_mode & S_ISGID) {
788 ma->ma_attr.la_gid = tmp_ma->ma_attr.la_gid;
789 if (S_ISDIR(ma->ma_attr.la_mode)) {
790 ma->ma_attr.la_mode |= S_ISGID;
791 ma->ma_attr.la_valid |= LA_MODE;
794 /* remote object creation and local name insert */
795 rc = mo_object_create(env, md_object_next(mo_c), spec, ma);
797 rc = mdo_name_insert(env, md_object_next(mo_p),
798 child_name, lu_object_fid(&mo_c->mo_lu),
799 S_ISDIR(ma->ma_attr.la_mode));
805 static int cmr_link(const struct lu_env *env, struct md_object *mo_p,
806 struct md_object *mo_s, const char *name,
812 //XXX: make sure that MDT checks name isn't exist
814 rc = mo_ref_add(env, md_object_next(mo_s));
816 rc = mdo_name_insert(env, md_object_next(mo_p),
817 name, lu_object_fid(&mo_s->mo_lu), 0);
823 static int cmr_unlink(const struct lu_env *env, struct md_object *mo_p,
824 struct md_object *mo_c, const char *name,
830 rc = mo_ref_del(env, md_object_next(mo_c), ma);
832 rc = mdo_name_remove(env, md_object_next(mo_p),
833 name, S_ISDIR(ma->ma_attr.la_mode));
839 static int cmr_rename(const struct lu_env *env,
840 struct md_object *mo_po, struct md_object *mo_pn,
841 const struct lu_fid *lf, const char *s_name,
842 struct md_object *mo_t, const char *t_name,
848 /* get real type of src */
849 rc = __cmm_mode_get(env, md_obj2dev(mo_po), lf, ma);
853 LASSERT(mo_t == NULL);
854 /* the mo_pn is remote directory, so we cannot even know if there is
855 * mo_t or not. Therefore mo_t is NULL here but remote server should do
856 * lookup and process this further */
857 rc = mdo_rename_tgt(env, md_object_next(mo_pn),
858 NULL/* mo_t */, lf, t_name, ma);
859 /* only old name is removed localy */
861 rc = mdo_name_remove(env, md_object_next(mo_po),
862 s_name, S_ISDIR(ma->ma_attr.la_mode));
867 /* part of cross-ref rename(). Used to insert new name in new parent
868 * and unlink target */
869 static int cmr_rename_tgt(const struct lu_env *env,
870 struct md_object *mo_p, struct md_object *mo_t,
871 const struct lu_fid *lf, const char *name,
876 /* target object is remote one */
877 rc = mo_ref_del(env, md_object_next(mo_t), ma);
878 /* continue locally with name handling only */
880 rc = mdo_rename_tgt(env, md_object_next(mo_p),
885 static struct md_dir_operations cmr_dir_ops = {
886 .mdo_is_subdir = cmm_is_subdir,
887 .mdo_lookup = cmr_lookup,
888 .mdo_create = cmr_create,
889 .mdo_link = cmr_link,
890 .mdo_unlink = cmr_unlink,
891 .mdo_rename = cmr_rename,
892 .mdo_rename_tgt = cmr_rename_tgt,