1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/cmm/cmm_object.c
38 * Lustre Cluster Metadata Manager (cmm)
40 * Author: Mike Pershin <tappro@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <lustre_fid.h>
50 #include "cmm_internal.h"
51 #include "mdc_internal.h"
54 * Lookup MDS number \a mds by FID \a fid.
56 * \param fid FID of object to find MDS
57 * \param mds mds number to return.
59 int cmm_fld_lookup(struct cmm_device *cm, const struct lu_fid *fid,
60 mdsno_t *mds, const struct lu_env *env)
65 LASSERT(fid_is_sane(fid));
67 rc = fld_client_lookup(cm->cmm_fld, fid_seq(fid), mds,
68 LU_SEQ_RANGE_MDT, env);
70 CERROR("Can't find mds by seq "LPX64", rc %d\n",
75 if (*mds > cm->cmm_tgt_count) {
76 CERROR("Got invalid mdsno: %x (max: %x)\n",
77 *mds, cm->cmm_tgt_count);
80 CDEBUG(D_INFO, "CMM: got MDS %x for sequence: "
81 LPX64"\n", *mds, fid_seq(fid));
91 static const struct md_object_operations cml_mo_ops;
92 static const struct md_dir_operations cml_dir_ops;
93 static const struct lu_object_operations cml_obj_ops;
95 static const struct md_object_operations cmr_mo_ops;
96 static const struct md_dir_operations cmr_dir_ops;
97 static const struct lu_object_operations cmr_obj_ops;
101 * Allocate CMM object.
103 struct lu_object *cmm_object_alloc(const struct lu_env *env,
104 const struct lu_object_header *loh,
105 struct lu_device *ld)
107 const struct lu_fid *fid = &loh->loh_fid;
108 struct lu_object *lo = NULL;
109 struct cmm_device *cd;
116 if (cd->cmm_flags & CMM_INITIALIZED) {
117 /* get object location */
118 rc = cmm_fld_lookup(lu2cmm_dev(ld), fid, &mds, env);
123 * Device is not yet initialized, cmm_object is being created
124 * as part of early bootstrap procedure (it is /ROOT, or /fld,
125 * etc.). Such object *has* to be local.
127 mds = cd->cmm_local_num;
129 /* select the proper set of operations based on object location */
130 if (mds == cd->cmm_local_num) {
131 struct cml_object *clo;
135 lo = &clo->cmm_obj.cmo_obj.mo_lu;
136 lu_object_init(lo, NULL, ld);
137 clo->cmm_obj.cmo_obj.mo_ops = &cml_mo_ops;
138 clo->cmm_obj.cmo_obj.mo_dir_ops = &cml_dir_ops;
139 lo->lo_ops = &cml_obj_ops;
142 struct cmr_object *cro;
146 lo = &cro->cmm_obj.cmo_obj.mo_lu;
147 lu_object_init(lo, NULL, ld);
148 cro->cmm_obj.cmo_obj.mo_ops = &cmr_mo_ops;
149 cro->cmm_obj.cmo_obj.mo_dir_ops = &cmr_dir_ops;
150 lo->lo_ops = &cmr_obj_ops;
158 * Get local child device.
160 static struct lu_device *cml_child_dev(struct cmm_device *d)
162 return &d->cmm_child->md_lu_dev;
168 static void cml_object_free(const struct lu_env *env,
169 struct lu_object *lo)
171 struct cml_object *clo = lu2cml_obj(lo);
177 * Initialize cml_object.
179 static int cml_object_init(const struct lu_env *env, struct lu_object *lo,
180 const struct lu_object_conf *unused)
182 struct cmm_device *cd = lu2cmm_dev(lo->lo_dev);
183 struct lu_device *c_dev;
184 struct lu_object *c_obj;
189 #ifdef HAVE_SPLIT_SUPPORT
190 if (cd->cmm_tgt_count == 0)
191 lu2cml_obj(lo)->clo_split = CMM_SPLIT_DENIED;
193 lu2cml_obj(lo)->clo_split = CMM_SPLIT_UNKNOWN;
195 c_dev = cml_child_dev(cd);
199 c_obj = c_dev->ld_ops->ldo_object_alloc(env,
200 lo->lo_header, c_dev);
202 lu_object_add(lo, c_obj);
212 static int cml_object_print(const struct lu_env *env, void *cookie,
213 lu_printer_t p, const struct lu_object *lo)
215 return (*p)(env, cookie, "[local]");
218 static const struct lu_object_operations cml_obj_ops = {
219 .loo_object_init = cml_object_init,
220 .loo_object_free = cml_object_free,
221 .loo_object_print = cml_object_print
225 * \name CMM local md_object operations.
226 * All of them call just corresponding operations on next layer.
229 static int cml_object_create(const struct lu_env *env,
230 struct md_object *mo,
231 const struct md_op_spec *spec,
232 struct md_attr *attr)
236 rc = mo_object_create(env, md_object_next(mo), spec, attr);
240 static int cml_permission(const struct lu_env *env,
241 struct md_object *p, struct md_object *c,
242 struct md_attr *attr, int mask)
246 rc = mo_permission(env, md_object_next(p), md_object_next(c),
251 static int cml_attr_get(const struct lu_env *env, struct md_object *mo,
252 struct md_attr *attr)
256 rc = mo_attr_get(env, md_object_next(mo), attr);
260 static int cml_attr_set(const struct lu_env *env, struct md_object *mo,
261 const struct md_attr *attr)
265 rc = mo_attr_set(env, md_object_next(mo), attr);
269 static int cml_xattr_get(const struct lu_env *env, struct md_object *mo,
270 struct lu_buf *buf, const char *name)
274 rc = mo_xattr_get(env, md_object_next(mo), buf, name);
278 static int cml_readlink(const struct lu_env *env, struct md_object *mo,
283 rc = mo_readlink(env, md_object_next(mo), buf);
287 static int cml_changelog(const struct lu_env *env, enum changelog_rec_type type,
288 int flags, struct md_object *mo)
292 rc = mo_changelog(env, type, flags, md_object_next(mo));
296 static int cml_xattr_list(const struct lu_env *env, struct md_object *mo,
301 rc = mo_xattr_list(env, md_object_next(mo), buf);
305 static int cml_xattr_set(const struct lu_env *env, struct md_object *mo,
306 const struct lu_buf *buf, const char *name,
311 rc = mo_xattr_set(env, md_object_next(mo), buf, name, fl);
315 static int cml_xattr_del(const struct lu_env *env, struct md_object *mo,
320 rc = mo_xattr_del(env, md_object_next(mo), name);
324 static int cml_ref_add(const struct lu_env *env, struct md_object *mo,
325 const struct md_attr *ma)
329 rc = mo_ref_add(env, md_object_next(mo), ma);
333 static int cml_ref_del(const struct lu_env *env, struct md_object *mo,
338 rc = mo_ref_del(env, md_object_next(mo), ma);
342 static int cml_open(const struct lu_env *env, struct md_object *mo,
347 rc = mo_open(env, md_object_next(mo), flags);
351 static int cml_close(const struct lu_env *env, struct md_object *mo,
352 struct md_attr *ma, int mode)
356 rc = mo_close(env, md_object_next(mo), ma, mode);
360 static int cml_readpage(const struct lu_env *env, struct md_object *mo,
361 const struct lu_rdpg *rdpg)
365 rc = mo_readpage(env, md_object_next(mo), rdpg);
369 static int cml_capa_get(const struct lu_env *env, struct md_object *mo,
370 struct lustre_capa *capa, int renewal)
374 rc = mo_capa_get(env, md_object_next(mo), capa, renewal);
378 static int cml_path(const struct lu_env *env, struct md_object *mo,
379 char *path, int pathlen, __u64 *recno, int *linkno)
383 rc = mo_path(env, md_object_next(mo), path, pathlen, recno, linkno);
387 static int cml_file_lock(const struct lu_env *env, struct md_object *mo,
388 struct lov_mds_md *lmm, struct ldlm_extent *extent,
389 struct lustre_handle *lockh)
393 rc = mo_file_lock(env, md_object_next(mo), lmm, extent, lockh);
397 static int cml_file_unlock(const struct lu_env *env, struct md_object *mo,
398 struct lov_mds_md *lmm, struct lustre_handle *lockh)
402 rc = mo_file_unlock(env, md_object_next(mo), lmm, lockh);
406 static int cml_object_sync(const struct lu_env *env, struct md_object *mo)
410 rc = mo_object_sync(env, md_object_next(mo));
414 static const struct md_object_operations cml_mo_ops = {
415 .moo_permission = cml_permission,
416 .moo_attr_get = cml_attr_get,
417 .moo_attr_set = cml_attr_set,
418 .moo_xattr_get = cml_xattr_get,
419 .moo_xattr_list = cml_xattr_list,
420 .moo_xattr_set = cml_xattr_set,
421 .moo_xattr_del = cml_xattr_del,
422 .moo_object_create = cml_object_create,
423 .moo_ref_add = cml_ref_add,
424 .moo_ref_del = cml_ref_del,
425 .moo_open = cml_open,
426 .moo_close = cml_close,
427 .moo_readpage = cml_readpage,
428 .moo_readlink = cml_readlink,
429 .moo_changelog = cml_changelog,
430 .moo_capa_get = cml_capa_get,
431 .moo_object_sync = cml_object_sync,
432 .moo_path = cml_path,
433 .moo_file_lock = cml_file_lock,
434 .moo_file_unlock = cml_file_unlock,
439 * \name CMM local md_dir_operations.
443 * cml lookup object fid by name.
444 * This returns only FID by name.
446 static int cml_lookup(const struct lu_env *env, struct md_object *mo_p,
447 const struct lu_name *lname, struct lu_fid *lf,
448 struct md_op_spec *spec)
453 #ifdef HAVE_SPLIT_SUPPORT
454 if (spec != NULL && spec->sp_ck_split) {
455 rc = cmm_split_check(env, mo_p, lname->ln_name);
460 rc = mdo_lookup(env, md_object_next(mo_p), lname, lf, spec);
466 * Helper to return lock mode. Used in split cases only.
468 static mdl_mode_t cml_lock_mode(const struct lu_env *env,
469 struct md_object *mo, mdl_mode_t lm)
471 int rc = MDL_MINMODE;
474 #ifdef HAVE_SPLIT_SUPPORT
475 rc = cmm_split_access(env, mo, lm);
482 * Create operation for cml.
483 * Objects are local, but split can happen.
484 * If split is not needed this will call next layer mdo_create().
486 * \param mo_p Parent directory. Local object.
487 * \param lname name of file to create.
488 * \param mo_c Child object. It has no real inode yet.
489 * \param spec creation specification.
490 * \param ma child object attributes.
492 static int cml_create(const struct lu_env *env, struct md_object *mo_p,
493 const struct lu_name *lname, struct md_object *mo_c,
494 struct md_op_spec *spec, struct md_attr *ma)
499 #ifdef HAVE_SPLIT_SUPPORT
500 /* Lock mode always should be sane. */
501 LASSERT(spec->sp_cr_mode != MDL_MINMODE);
504 * Sigh... This is long story. MDT may have race with detecting if split
505 * is possible in cmm. We know this race and let it live, because
506 * getting it rid (with some sem or spinlock) will also mean that
507 * PDIROPS for create will not work because we kill parallel work, what
508 * is really bad for performance and makes no sense having PDIROPS. So,
509 * we better allow the race to live, but split dir only if some of
510 * concurrent threads takes EX lock, not matter which one. So that, say,
511 * two concurrent threads may have different lock modes on directory (CW
512 * and EX) and not first one which comes here and see that split is
513 * possible should split the dir, but only that one which has EX
514 * lock. And we do not care that in this case, split may happen a bit
515 * later (when dir size will not be necessarily 64K, but may be a bit
516 * larger). So that, we allow concurrent creates and protect split by EX
519 if (spec->sp_cr_mode == MDL_EX) {
522 * - Try to split \a mo_p upon each create operation.
523 * If split is ok, -ERESTART is returned and current thread
524 * will not peoceed with create. Instead it sends -ERESTART
525 * to client to let it know that correct MDT must be chosen.
526 * \see cmm_split_dir()
528 rc = cmm_split_dir(env, mo_p);
531 * -ERESTART or some split error is returned, we can't
532 * proceed with create.
537 if (spec != NULL && spec->sp_ck_split) {
539 * - Directory is split already. Let the caller know that
540 * it should tell client that directory is split and operation
541 * should repeat to correct MDT.
542 * \see cmm_split_check()
544 rc = cmm_split_check(env, mo_p, lname->ln_name);
550 rc = mdo_create(env, md_object_next(mo_p), lname, md_object_next(mo_c),
554 #ifdef HAVE_SPLIT_SUPPORT
560 /** Call mdo_create_data() on next layer. All objects are local. */
561 static int cml_create_data(const struct lu_env *env, struct md_object *p,
563 const struct md_op_spec *spec,
568 rc = mdo_create_data(env, md_object_next(p), md_object_next(o),
573 /** Call mdo_link() on next layer. All objects are local. */
574 static int cml_link(const struct lu_env *env, struct md_object *mo_p,
575 struct md_object *mo_s, const struct lu_name *lname,
580 rc = mdo_link(env, md_object_next(mo_p), md_object_next(mo_s),
585 /** Call mdo_unlink() on next layer. All objects are local. */
586 static int cml_unlink(const struct lu_env *env, struct md_object *mo_p,
587 struct md_object *mo_c, const struct lu_name *lname,
592 rc = mdo_unlink(env, md_object_next(mo_p), md_object_next(mo_c),
599 * Get mode of object.
600 * Used in both cml and cmr hence can produce RPC to another server.
602 static int cmm_mode_get(const struct lu_env *env, struct md_device *md,
603 const struct lu_fid *lf, struct md_attr *ma,
606 struct md_object *mo_s = md_object_find_slice(env, md, lf);
607 struct cmm_thread_info *cmi;
608 struct md_attr *tmp_ma;
613 RETURN(PTR_ERR(mo_s));
615 if (remote && (lu_object_exists(&mo_s->mo_lu) < 0))
618 cmi = cmm_env_info(env);
619 tmp_ma = &cmi->cmi_ma;
620 tmp_ma->ma_need = MA_INODE;
621 tmp_ma->ma_valid = 0;
622 /* get type from src, can be remote req */
623 rc = mo_attr_get(env, md_object_next(mo_s), tmp_ma);
625 ma->ma_attr.la_mode = tmp_ma->ma_attr.la_mode;
626 ma->ma_attr.la_uid = tmp_ma->ma_attr.la_uid;
627 ma->ma_attr.la_gid = tmp_ma->ma_attr.la_gid;
628 ma->ma_attr.la_flags = tmp_ma->ma_attr.la_flags;
629 ma->ma_attr.la_valid |= LA_MODE | LA_UID | LA_GID | LA_FLAGS;
631 lu_object_put(env, &mo_s->mo_lu);
637 * Set ctime for object.
638 * Used in both cml and cmr hence can produce RPC to another server.
640 static int cmm_rename_ctime(const struct lu_env *env, struct md_device *md,
641 const struct lu_fid *lf, struct md_attr *ma)
643 struct md_object *mo_s = md_object_find_slice(env, md, lf);
648 RETURN(PTR_ERR(mo_s));
650 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
651 /* set ctime to obj, can be remote req */
652 rc = mo_attr_set(env, md_object_next(mo_s), ma);
653 lu_object_put(env, &mo_s->mo_lu);
657 /** Helper to output debug information about rename operation. */
658 static inline void cml_rename_warn(const char *fname,
659 struct md_object *mo_po,
660 struct md_object *mo_pn,
661 const struct lu_fid *lf,
663 struct md_object *mo_t,
668 CWARN("cml_rename failed for %s, should revoke: [mo_po "DFID"] "
669 "[mo_pn "DFID"] [lf "DFID"] [sname %s] [mo_t "DFID"] "
670 "[tname %s] [err %d]\n", fname,
671 PFID(lu_object_fid(&mo_po->mo_lu)),
672 PFID(lu_object_fid(&mo_pn->mo_lu)),
674 PFID(lu_object_fid(&mo_t->mo_lu)),
677 CWARN("cml_rename failed for %s, should revoke: [mo_po "DFID"] "
678 "[mo_pn "DFID"] [lf "DFID"] [sname %s] [mo_t NULL] "
679 "[tname %s] [err %d]\n", fname,
680 PFID(lu_object_fid(&mo_po->mo_lu)),
681 PFID(lu_object_fid(&mo_pn->mo_lu)),
687 * Rename operation for cml.
689 * This is the most complex cross-reference operation. It may consist of up to 4
690 * MDS server and require several RPCs to be sent.
692 * \param mo_po Old parent object.
693 * \param mo_pn New parent object.
694 * \param lf FID of object to rename.
695 * \param ls_name Source file name.
696 * \param mo_t target object. Should be NULL here.
697 * \param lt_name Name of target file.
698 * \param ma object attributes.
700 static int cml_rename(const struct lu_env *env, struct md_object *mo_po,
701 struct md_object *mo_pn, const struct lu_fid *lf,
702 const struct lu_name *ls_name, struct md_object *mo_t,
703 const struct lu_name *lt_name, struct md_attr *ma)
705 struct cmm_thread_info *cmi;
706 struct md_attr *tmp_ma = NULL;
707 struct md_object *tmp_t = mo_t;
711 rc = cmm_mode_get(env, md_obj2dev(mo_po), lf, ma, &remote);
715 if (mo_t && lu_object_exists(&mo_t->mo_lu) < 0) {
717 * \note \a mo_t is remote object and there is RPC to unlink it.
718 * Before that, do local sanity check for rename first.
721 struct md_object *mo_s = md_object_find_slice(env,
722 md_obj2dev(mo_po), lf);
724 RETURN(PTR_ERR(mo_s));
726 LASSERT(lu_object_exists(&mo_s->mo_lu) > 0);
727 rc = mo_permission(env, md_object_next(mo_po),
728 md_object_next(mo_s),
730 lu_object_put(env, &mo_s->mo_lu);
734 rc = mo_permission(env, NULL, md_object_next(mo_po),
735 ma, MAY_UNLINK | MAY_VTX_FULL);
740 rc = mo_permission(env, NULL, md_object_next(mo_pn), ma,
741 MAY_UNLINK | MAY_VTX_PART);
746 * /note \a ma will be changed after mo_ref_del(), but we will use
747 * it for mdo_rename() later, so save it before mo_ref_del().
749 cmi = cmm_env_info(env);
750 tmp_ma = &cmi->cmi_ma;
752 rc = mo_ref_del(env, md_object_next(mo_t), ma);
756 tmp_ma->ma_attr_flags |= MDS_PERM_BYPASS;
761 * \note for src on remote MDS case, change its ctime before local
762 * rename. Firstly, do local sanity check for rename if necessary.
766 rc = mo_permission(env, NULL, md_object_next(mo_po),
767 ma, MAY_UNLINK | MAY_VTX_FULL);
772 LASSERT(lu_object_exists(&mo_t->mo_lu) > 0);
773 rc = mo_permission(env, md_object_next(mo_pn),
774 md_object_next(mo_t),
782 mask = (S_ISDIR(ma->ma_attr.la_mode) ?
783 MAY_LINK : MAY_CREATE);
786 rc = mo_permission(env, NULL,
787 md_object_next(mo_pn),
793 ma->ma_attr_flags |= MDS_PERM_BYPASS;
795 LASSERT(tmp_ma->ma_attr_flags & MDS_PERM_BYPASS);
798 rc = cmm_rename_ctime(env, md_obj2dev(mo_po), lf,
799 tmp_ma ? tmp_ma : ma);
801 /* TODO: revoke mo_t if necessary. */
802 cml_rename_warn("cmm_rename_ctime", mo_po,
803 mo_pn, lf, ls_name->ln_name,
804 tmp_t, lt_name->ln_name, rc);
809 /* local rename, mo_t can be NULL */
810 rc = mdo_rename(env, md_object_next(mo_po),
811 md_object_next(mo_pn), lf, ls_name,
812 md_object_next(mo_t), lt_name, tmp_ma ? tmp_ma : ma);
814 /* TODO: revoke all cml_rename */
815 cml_rename_warn("mdo_rename", mo_po, mo_pn, lf,
816 ls_name->ln_name, tmp_t, lt_name->ln_name, rc);
822 * Rename target partial operation.
823 * Used for cross-ref rename.
825 static int cml_rename_tgt(const struct lu_env *env, struct md_object *mo_p,
826 struct md_object *mo_t, const struct lu_fid *lf,
827 const struct lu_name *lname, struct md_attr *ma)
832 rc = mdo_rename_tgt(env, md_object_next(mo_p),
833 md_object_next(mo_t), lf, lname, ma);
838 * Name insert only operation.
839 * used only in case of rename_tgt() when target doesn't exist.
841 static int cml_name_insert(const struct lu_env *env, struct md_object *p,
842 const struct lu_name *lname, const struct lu_fid *lf,
843 const struct md_attr *ma)
848 rc = mdo_name_insert(env, md_object_next(p), lname, lf, ma);
855 * Check two fids are not subdirectories.
857 static int cmm_is_subdir(const struct lu_env *env, struct md_object *mo,
858 const struct lu_fid *fid, struct lu_fid *sfid)
860 struct cmm_thread_info *cmi;
864 cmi = cmm_env_info(env);
865 rc = cmm_mode_get(env, md_obj2dev(mo), fid, &cmi->cmi_ma, NULL);
869 if (!S_ISDIR(cmi->cmi_ma.ma_attr.la_mode))
872 rc = mdo_is_subdir(env, md_object_next(mo), fid, sfid);
876 static const struct md_dir_operations cml_dir_ops = {
877 .mdo_is_subdir = cmm_is_subdir,
878 .mdo_lookup = cml_lookup,
879 .mdo_lock_mode = cml_lock_mode,
880 .mdo_create = cml_create,
881 .mdo_link = cml_link,
882 .mdo_unlink = cml_unlink,
883 .mdo_name_insert = cml_name_insert,
884 .mdo_rename = cml_rename,
885 .mdo_rename_tgt = cml_rename_tgt,
886 .mdo_create_data = cml_create_data,
899 /** Get cmr_object from lu_object. */
900 static inline struct cmr_object *lu2cmr_obj(struct lu_object *o)
902 return container_of0(o, struct cmr_object, cmm_obj.cmo_obj.mo_lu);
904 /** Get cmr_object from md_object. */
905 static inline struct cmr_object *md2cmr_obj(struct md_object *mo)
907 return container_of0(mo, struct cmr_object, cmm_obj.cmo_obj);
909 /** Get cmr_object from cmm_object. */
910 static inline struct cmr_object *cmm2cmr_obj(struct cmm_object *co)
912 return container_of0(co, struct cmr_object, cmm_obj);
917 * Get proper child device from MDCs.
919 static struct lu_device *cmr_child_dev(struct cmm_device *d, __u32 num)
921 struct lu_device *next = NULL;
922 struct mdc_device *mdc;
924 cfs_spin_lock(&d->cmm_tgt_guard);
925 cfs_list_for_each_entry(mdc, &d->cmm_targets, mc_linkage) {
926 if (mdc->mc_num == num) {
927 next = mdc2lu_dev(mdc);
931 cfs_spin_unlock(&d->cmm_tgt_guard);
938 static void cmr_object_free(const struct lu_env *env,
939 struct lu_object *lo)
941 struct cmr_object *cro = lu2cmr_obj(lo);
947 * Initialize cmr object.
949 static int cmr_object_init(const struct lu_env *env, struct lu_object *lo,
950 const struct lu_object_conf *unused)
952 struct cmm_device *cd = lu2cmm_dev(lo->lo_dev);
953 struct lu_device *c_dev;
954 struct lu_object *c_obj;
959 c_dev = cmr_child_dev(cd, lu2cmr_obj(lo)->cmo_num);
963 c_obj = c_dev->ld_ops->ldo_object_alloc(env,
964 lo->lo_header, c_dev);
966 lu_object_add(lo, c_obj);
977 * Output lu_object data.
979 static int cmr_object_print(const struct lu_env *env, void *cookie,
980 lu_printer_t p, const struct lu_object *lo)
982 const struct cmr_object *cro = lu2cmr_obj((struct lu_object *)lo);
983 return (*p)(env, cookie, "[remote](mds_num=%d)", cro->cmo_num);
987 * Cmr instance of lu_object_operations.
989 static const struct lu_object_operations cmr_obj_ops = {
990 .loo_object_init = cmr_object_init,
991 .loo_object_free = cmr_object_free,
992 .loo_object_print = cmr_object_print
996 * \name cmr remote md_object operations.
997 * All operations here are invalid and return errors. There is no local object
998 * so these operations return two kinds of error:
999 * -# -EFAULT if operation is prohibited.
1000 * -# -EREMOTE if operation can be done just to notify upper level about remote
1005 static int cmr_object_create(const struct lu_env *env,
1006 struct md_object *mo,
1007 const struct md_op_spec *spec,
1013 static int cmr_permission(const struct lu_env *env,
1014 struct md_object *p, struct md_object *c,
1015 struct md_attr *attr, int mask)
1020 static int cmr_attr_get(const struct lu_env *env, struct md_object *mo,
1021 struct md_attr *attr)
1026 static int cmr_attr_set(const struct lu_env *env, struct md_object *mo,
1027 const struct md_attr *attr)
1032 static int cmr_xattr_get(const struct lu_env *env, struct md_object *mo,
1033 struct lu_buf *buf, const char *name)
1038 static int cmr_readlink(const struct lu_env *env, struct md_object *mo,
1044 static int cmr_changelog(const struct lu_env *env, enum changelog_rec_type type,
1045 int flags, struct md_object *mo)
1050 static int cmr_xattr_list(const struct lu_env *env, struct md_object *mo,
1056 static int cmr_xattr_set(const struct lu_env *env, struct md_object *mo,
1057 const struct lu_buf *buf, const char *name,
1063 static int cmr_xattr_del(const struct lu_env *env, struct md_object *mo,
1069 static int cmr_ref_add(const struct lu_env *env, struct md_object *mo,
1070 const struct md_attr *ma)
1075 static int cmr_ref_del(const struct lu_env *env, struct md_object *mo,
1081 static int cmr_open(const struct lu_env *env, struct md_object *mo,
1087 static int cmr_close(const struct lu_env *env, struct md_object *mo,
1088 struct md_attr *ma, int mode)
1093 static int cmr_readpage(const struct lu_env *env, struct md_object *mo,
1094 const struct lu_rdpg *rdpg)
1099 static int cmr_capa_get(const struct lu_env *env, struct md_object *mo,
1100 struct lustre_capa *capa, int renewal)
1105 static int cmr_path(const struct lu_env *env, struct md_object *obj,
1106 char *path, int pathlen, __u64 *recno, int *linkno)
1111 static int cmr_object_sync(const struct lu_env *env, struct md_object *mo)
1116 static int cmr_file_lock(const struct lu_env *env, struct md_object *mo,
1117 struct lov_mds_md *lmm, struct ldlm_extent *extent,
1118 struct lustre_handle *lockh)
1123 static int cmr_file_unlock(const struct lu_env *env, struct md_object *mo,
1124 struct lov_mds_md *lmm, struct lustre_handle *lockh)
1129 /** Set of md_object_operations for cmr. */
1130 static const struct md_object_operations cmr_mo_ops = {
1131 .moo_permission = cmr_permission,
1132 .moo_attr_get = cmr_attr_get,
1133 .moo_attr_set = cmr_attr_set,
1134 .moo_xattr_get = cmr_xattr_get,
1135 .moo_xattr_set = cmr_xattr_set,
1136 .moo_xattr_list = cmr_xattr_list,
1137 .moo_xattr_del = cmr_xattr_del,
1138 .moo_object_create = cmr_object_create,
1139 .moo_ref_add = cmr_ref_add,
1140 .moo_ref_del = cmr_ref_del,
1141 .moo_open = cmr_open,
1142 .moo_close = cmr_close,
1143 .moo_readpage = cmr_readpage,
1144 .moo_readlink = cmr_readlink,
1145 .moo_changelog = cmr_changelog,
1146 .moo_capa_get = cmr_capa_get,
1147 .moo_object_sync = cmr_object_sync,
1148 .moo_path = cmr_path,
1149 .moo_file_lock = cmr_file_lock,
1150 .moo_file_unlock = cmr_file_unlock,
1155 * \name cmr md_dir operations.
1157 * All methods below are cross-ref by nature. They consist of remote call and
1158 * local operation. Due to future rollback functionality there are several
1159 * limitations for such methods:
1160 * -# remote call should be done at first to do epoch negotiation between all
1161 * MDS involved and to avoid the RPC inside transaction.
1162 * -# only one RPC can be sent - also due to epoch negotiation.
1163 * For more details see rollback HLD/DLD.
1166 static int cmr_lookup(const struct lu_env *env, struct md_object *mo_p,
1167 const struct lu_name *lname, struct lu_fid *lf,
1168 struct md_op_spec *spec)
1171 * This can happens while rename() If new parent is remote dir, lookup
1178 /** Return lock mode. */
1179 static mdl_mode_t cmr_lock_mode(const struct lu_env *env,
1180 struct md_object *mo, mdl_mode_t lm)
1186 * Create operation for cmr.
1187 * Remote object creation and local name insert.
1189 * \param mo_p Parent directory. Local object.
1190 * \param lchild_name name of file to create.
1191 * \param mo_c Child object. It has no real inode yet.
1192 * \param spec creation specification.
1193 * \param ma child object attributes.
1195 static int cmr_create(const struct lu_env *env, struct md_object *mo_p,
1196 const struct lu_name *lchild_name, struct md_object *mo_c,
1197 struct md_op_spec *spec,
1200 struct cmm_thread_info *cmi;
1201 struct md_attr *tmp_ma;
1205 /* Make sure that name isn't exist before doing remote call. */
1206 rc = mdo_lookup(env, md_object_next(mo_p), lchild_name,
1207 &cmm_env_info(env)->cmi_fid, NULL);
1210 else if (rc != -ENOENT)
1213 /* check the SGID attr */
1214 cmi = cmm_env_info(env);
1216 tmp_ma = &cmi->cmi_ma;
1217 tmp_ma->ma_valid = 0;
1218 tmp_ma->ma_need = MA_INODE;
1220 #ifdef CONFIG_FS_POSIX_ACL
1221 if (!S_ISLNK(ma->ma_attr.la_mode)) {
1222 tmp_ma->ma_acl = cmi->cmi_xattr_buf;
1223 tmp_ma->ma_acl_size = sizeof(cmi->cmi_xattr_buf);
1224 tmp_ma->ma_need |= MA_ACL_DEF;
1227 rc = mo_attr_get(env, md_object_next(mo_p), tmp_ma);
1231 if (tmp_ma->ma_attr.la_mode & S_ISGID) {
1232 ma->ma_attr.la_gid = tmp_ma->ma_attr.la_gid;
1233 if (S_ISDIR(ma->ma_attr.la_mode)) {
1234 ma->ma_attr.la_mode |= S_ISGID;
1235 ma->ma_attr.la_valid |= LA_MODE;
1239 #ifdef CONFIG_FS_POSIX_ACL
1240 if (tmp_ma->ma_valid & MA_ACL_DEF) {
1241 spec->u.sp_ea.fid = spec->u.sp_pfid;
1242 spec->u.sp_ea.eadata = tmp_ma->ma_acl;
1243 spec->u.sp_ea.eadatalen = tmp_ma->ma_acl_size;
1244 spec->sp_cr_flags |= MDS_CREATE_RMT_ACL;
1248 /* Local permission check for name_insert before remote ops. */
1249 rc = mo_permission(env, NULL, md_object_next(mo_p), NULL,
1250 (S_ISDIR(ma->ma_attr.la_mode) ?
1251 MAY_LINK : MAY_CREATE));
1256 * \note \a ma will be changed after mo_object_create(), but we will use
1257 * it for mdo_name_insert() later, so save it before mo_object_create().
1260 rc = mo_object_create(env, md_object_next(mo_c), spec, ma);
1262 tmp_ma->ma_attr_flags |= MDS_PERM_BYPASS;
1263 rc = mdo_name_insert(env, md_object_next(mo_p), lchild_name,
1264 lu_object_fid(&mo_c->mo_lu), tmp_ma);
1266 /* TODO: remove object mo_c on remote MDS */
1267 CWARN("cmr_create failed, should revoke: [mo_p "DFID"]"
1268 " [name %s] [mo_c "DFID"] [err %d]\n",
1269 PFID(lu_object_fid(&mo_p->mo_lu)),
1270 lchild_name->ln_name,
1271 PFID(lu_object_fid(&mo_c->mo_lu)), rc);
1279 * Link operations for cmr.
1281 * The link RPC is always issued to the server where source parent is living.
1282 * The first operation to do is object nlink increment on remote server.
1283 * Second one is local mdo_name_insert().
1285 * \param mo_p parent directory. It is local.
1286 * \param mo_s source object to link. It is remote.
1287 * \param lname Name of link file.
1288 * \param ma object attributes.
1290 static int cmr_link(const struct lu_env *env, struct md_object *mo_p,
1291 struct md_object *mo_s, const struct lu_name *lname,
1297 /* Make sure that name isn't exist before doing remote call. */
1298 rc = mdo_lookup(env, md_object_next(mo_p), lname,
1299 &cmm_env_info(env)->cmi_fid, NULL);
1302 } else if (rc == -ENOENT) {
1303 /* Local permission check for name_insert before remote ops. */
1304 rc = mo_permission(env, NULL, md_object_next(mo_p), NULL,
1309 rc = mo_ref_add(env, md_object_next(mo_s), ma);
1311 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1312 rc = mdo_name_insert(env, md_object_next(mo_p), lname,
1313 lu_object_fid(&mo_s->mo_lu), ma);
1315 /* TODO: ref_del from mo_s on remote MDS */
1316 CWARN("cmr_link failed, should revoke: "
1317 "[mo_p "DFID"] [mo_s "DFID"] "
1318 "[name %s] [err %d]\n",
1319 PFID(lu_object_fid(&mo_p->mo_lu)),
1320 PFID(lu_object_fid(&mo_s->mo_lu)),
1321 lname->ln_name, rc);
1329 * Unlink operations for cmr.
1331 * The unlink RPC is always issued to the server where parent is living. Hence
1332 * the first operation to do is object unlink on remote server. Second one is
1333 * local mdo_name_remove().
1335 * \param mo_p parent md_object. It is local.
1336 * \param mo_c child object to be unlinked. It is remote.
1337 * \param lname Name of file to unlink.
1338 * \param ma object attributes.
1340 static int cmr_unlink(const struct lu_env *env, struct md_object *mo_p,
1341 struct md_object *mo_c, const struct lu_name *lname,
1344 struct cmm_thread_info *cmi;
1345 struct md_attr *tmp_ma;
1349 /* Local permission check for name_remove before remote ops. */
1350 rc = mo_permission(env, NULL, md_object_next(mo_p), ma,
1351 MAY_UNLINK | MAY_VTX_PART);
1356 * \note \a ma will be changed after mo_ref_del, but we will use
1357 * it for mdo_name_remove() later, so save it before mo_ref_del().
1359 cmi = cmm_env_info(env);
1360 tmp_ma = &cmi->cmi_ma;
1362 rc = mo_ref_del(env, md_object_next(mo_c), ma);
1364 tmp_ma->ma_attr_flags |= MDS_PERM_BYPASS;
1365 rc = mdo_name_remove(env, md_object_next(mo_p), lname, tmp_ma);
1367 /* TODO: ref_add to mo_c on remote MDS */
1368 CWARN("cmr_unlink failed, should revoke: [mo_p "DFID"]"
1369 " [mo_c "DFID"] [name %s] [err %d]\n",
1370 PFID(lu_object_fid(&mo_p->mo_lu)),
1371 PFID(lu_object_fid(&mo_c->mo_lu)),
1372 lname->ln_name, rc);
1379 /** Helper which outputs error message during cmr_rename() */
1380 static inline void cmr_rename_warn(const char *fname,
1381 struct md_object *mo_po,
1382 struct md_object *mo_pn,
1383 const struct lu_fid *lf,
1388 CWARN("cmr_rename failed for %s, should revoke: "
1389 "[mo_po "DFID"] [mo_pn "DFID"] [lf "DFID"] "
1390 "[sname %s] [tname %s] [err %d]\n", fname,
1391 PFID(lu_object_fid(&mo_po->mo_lu)),
1392 PFID(lu_object_fid(&mo_pn->mo_lu)),
1393 PFID(lf), s_name, t_name, err);
1397 * Rename operation for cmr.
1399 * This is the most complex cross-reference operation. It may consist of up to 4
1400 * MDS server and require several RPCs to be sent.
1402 * \param mo_po Old parent object.
1403 * \param mo_pn New parent object.
1404 * \param lf FID of object to rename.
1405 * \param ls_name Source file name.
1406 * \param mo_t target object. Should be NULL here.
1407 * \param lt_name Name of target file.
1408 * \param ma object attributes.
1410 static int cmr_rename(const struct lu_env *env,
1411 struct md_object *mo_po, struct md_object *mo_pn,
1412 const struct lu_fid *lf, const struct lu_name *ls_name,
1413 struct md_object *mo_t, const struct lu_name *lt_name,
1416 struct cmm_thread_info *cmi;
1417 struct md_attr *tmp_ma;
1421 LASSERT(mo_t == NULL);
1423 /* get real type of src */
1424 rc = cmm_mode_get(env, md_obj2dev(mo_po), lf, ma, NULL);
1428 /* Local permission check for name_remove before remote ops. */
1429 rc = mo_permission(env, NULL, md_object_next(mo_po), ma,
1430 MAY_UNLINK | MAY_VTX_FULL);
1435 * \todo \a ma maybe changed after mdo_rename_tgt(), but we will use it
1436 * for mdo_name_remove() later, so save it before mdo_rename_tgt.
1438 cmi = cmm_env_info(env);
1439 tmp_ma = &cmi->cmi_ma;
1442 * \note The \a mo_pn is remote directory, so we cannot even know if there is
1443 * \a mo_t or not. Therefore \a mo_t is NULL here but remote server should do
1444 * lookup and process this further.
1446 rc = mdo_rename_tgt(env, md_object_next(mo_pn),
1447 NULL/* mo_t */, lf, lt_name, ma);
1451 tmp_ma->ma_attr_flags |= MDS_PERM_BYPASS;
1453 /* src object maybe on remote MDS, do remote ops first. */
1454 rc = cmm_rename_ctime(env, md_obj2dev(mo_po), lf, tmp_ma);
1456 /* TODO: revoke mdo_rename_tgt */
1457 cmr_rename_warn("cmm_rename_ctime", mo_po, mo_pn, lf,
1458 ls_name->ln_name, lt_name->ln_name, rc);
1462 /* only old name is removed localy */
1463 rc = mdo_name_remove(env, md_object_next(mo_po), ls_name, tmp_ma);
1465 /* TODO: revoke all cmr_rename */
1466 cmr_rename_warn("mdo_name_remove", mo_po, mo_pn, lf,
1467 ls_name->ln_name, lt_name->ln_name, rc);
1473 * Part of cross-ref rename().
1474 * Used to insert new name in new parent and unlink target.
1476 static int cmr_rename_tgt(const struct lu_env *env,
1477 struct md_object *mo_p, struct md_object *mo_t,
1478 const struct lu_fid *lf, const struct lu_name *lname,
1481 struct cmm_thread_info *cmi;
1482 struct md_attr *tmp_ma;
1486 /* target object is remote one */
1487 /* Local permission check for rename_tgt before remote ops. */
1488 rc = mo_permission(env, NULL, md_object_next(mo_p), ma,
1489 MAY_UNLINK | MAY_VTX_PART);
1494 * XXX: @ma maybe changed after mo_ref_del, but we will use
1495 * it for mdo_rename_tgt later, so save it before mo_ref_del.
1497 cmi = cmm_env_info(env);
1498 tmp_ma = &cmi->cmi_ma;
1500 rc = mo_ref_del(env, md_object_next(mo_t), ma);
1501 /* continue locally with name handling only */
1503 tmp_ma->ma_attr_flags |= MDS_PERM_BYPASS;
1504 rc = mdo_rename_tgt(env, md_object_next(mo_p),
1505 NULL, lf, lname, tmp_ma);
1507 /* TODO: ref_add to mo_t on remote MDS */
1508 CWARN("cmr_rename_tgt failed, should revoke: "
1509 "[mo_p "DFID"] [mo_t "DFID"] [lf "DFID"] "
1510 "[name %s] [err %d]\n",
1511 PFID(lu_object_fid(&mo_p->mo_lu)),
1512 PFID(lu_object_fid(&mo_t->mo_lu)),
1514 lname->ln_name, rc);
1521 * The md_dir_operations for cmr.
1523 static const struct md_dir_operations cmr_dir_ops = {
1524 .mdo_is_subdir = cmm_is_subdir,
1525 .mdo_lookup = cmr_lookup,
1526 .mdo_lock_mode = cmr_lock_mode,
1527 .mdo_create = cmr_create,
1528 .mdo_link = cmr_link,
1529 .mdo_unlink = cmr_unlink,
1530 .mdo_rename = cmr_rename,
1531 .mdo_rename_tgt = cmr_rename_tgt