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 2008 Sun Microsystems, Inc. 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/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_fid.h>
61 #include "mdd_internal.h"
63 static const char dot[] = ".";
64 static const char dotdot[] = "..";
66 static struct lu_name lname_dotdot = {
71 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_name *lname, struct lu_fid* fid,
75 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
76 const struct lu_name *lname, struct lu_fid* fid, int mask)
78 char *name = lname->ln_name;
79 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
80 struct dynlock_handle *dlh;
83 dlh = mdd_pdo_read_lock(env, mdd_obj, name);
84 if (unlikely(dlh == NULL))
86 rc = __mdd_lookup(env, pobj, lname, fid, mask);
87 mdd_pdo_read_unlock(env, mdd_obj, dlh);
92 static int mdd_lookup(const struct lu_env *env,
93 struct md_object *pobj, const struct lu_name *lname,
94 struct lu_fid* fid, struct md_op_spec *spec)
98 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
103 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
106 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
110 * For root fid use special function, whcih does not compare version component
111 * of fid. Vresion component is different for root fids on all MDTs.
113 static int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
115 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
116 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
120 * return 1: if lf is the fid of the ancestor of p1;
123 * return -EREMOTE: if remote object is found, in this
124 * case fid of remote object is saved to @pf;
126 * otherwise: values < 0, errors.
128 static int mdd_is_parent(const struct lu_env *env,
129 struct mdd_device *mdd,
130 struct mdd_object *p1,
131 const struct lu_fid *lf,
134 struct mdd_object *parent = NULL;
139 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
140 pfid = &mdd_env_info(env)->mti_fid;
142 /* Check for root first. */
143 if (mdd_is_root(mdd, mdo2fid(p1)))
147 /* this is done recursively, bypass capa for each obj */
148 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
149 rc = mdd_parent_fid(env, p1, pfid);
152 if (mdd_is_root(mdd, pfid))
154 if (lu_fid_eq(pfid, lf))
157 mdd_object_put(env, parent);
158 parent = mdd_object_find(env, mdd, pfid);
160 /* cross-ref parent */
161 if (parent == NULL) {
164 GOTO(out, rc = -EREMOTE);
165 } else if (IS_ERR(parent))
166 GOTO(out, rc = PTR_ERR(parent));
171 if (parent && !IS_ERR(parent))
172 mdd_object_put(env, parent);
177 * No permission check is needed.
179 * returns 1: if fid is ancestor of @mo;
180 * returns 0: if fid is not a ancestor of @mo;
182 * returns EREMOTE if remote object is found, fid of remote object is saved to
185 * returns < 0: if error
187 static int mdd_is_subdir(const struct lu_env *env,
188 struct md_object *mo, const struct lu_fid *fid,
191 struct mdd_device *mdd = mdo2mdd(mo);
195 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
198 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
202 } else if (rc == 1) {
203 /* found @fid is parent */
211 * Check that @dir contains no entries except (possibly) dot and dotdot.
216 * -ENOTDIR not a directory object
217 * -ENOTEMPTY not empty
221 static int mdd_dir_is_empty(const struct lu_env *env,
222 struct mdd_object *dir)
225 struct dt_object *obj;
226 struct dt_it_ops *iops;
230 obj = mdd_object_child(dir);
231 if (!dt_try_as_dir(env, obj))
234 iops = &obj->do_index_ops->dio_it;
235 it = iops->init(env, obj, 0, BYPASS_CAPA);
237 result = iops->get(env, it, (const void *)"");
240 for (result = 0, i = 0; result == 0 && i < 3; ++i)
241 result = iops->next(env, it);
244 else if (result == +1)
246 } else if (result == 0)
248 * Huh? Index contains no zero key?
259 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
261 struct mdd_device *m = mdd_obj2mdd_dev(obj);
262 struct lu_attr *la = &mdd_env_info(env)->mti_la;
266 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
271 * Subdir count limitation can be broken through.
273 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
274 !S_ISDIR(la->la_mode))
281 * Check whether it may create the cobj under the pobj.
284 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
285 struct mdd_object *cobj, int check_perm, int check_nlink)
290 if (cobj && mdd_object_exists(cobj))
293 if (mdd_is_dead_obj(pobj))
297 rc = mdd_permission_internal_locked(env, pobj, NULL,
298 MAY_WRITE | MAY_EXEC);
300 if (!rc && check_nlink)
301 rc = __mdd_may_link(env, pobj);
307 * Check whether can unlink from the pobj in the case of "cobj == NULL".
309 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
310 const struct md_attr *ma)
315 if (mdd_is_dead_obj(pobj))
318 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
319 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
322 rc = mdd_permission_internal_locked(env, pobj, NULL,
323 MAY_WRITE | MAY_EXEC);
327 if (mdd_is_append(pobj))
334 * pobj == NULL is remote ops case, under such case, pobj's
335 * VTX feature has been checked already, no need check again.
337 static inline int mdd_is_sticky(const struct lu_env *env,
338 struct mdd_object *pobj,
339 struct mdd_object *cobj)
341 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
342 struct md_ucred *uc = md_ucred(env);
346 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
350 if (!(tmp_la->la_mode & S_ISVTX) ||
351 (tmp_la->la_uid == uc->mu_fsuid))
355 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
359 if (tmp_la->la_uid == uc->mu_fsuid)
362 return !mdd_capable(uc, CAP_FOWNER);
366 * Check whether it may delete the cobj from the pobj.
369 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
370 struct mdd_object *cobj, struct md_attr *ma,
371 int check_perm, int check_empty)
377 if (!mdd_object_exists(cobj))
381 if (mdd_is_dead_obj(pobj))
385 rc = mdd_permission_internal_locked(env, pobj, NULL,
386 MAY_WRITE | MAY_EXEC);
391 if (mdd_is_append(pobj))
395 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
396 mdd_is_sticky(env, pobj, cobj))
399 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
402 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
403 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
406 if (S_ISDIR(ma->ma_attr.la_mode)) {
407 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
409 if (!S_ISDIR(mdd_object_type(cobj)))
412 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
414 } else if (S_ISDIR(mdd_object_type(cobj)))
417 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
418 rc = mdd_dir_is_empty(env, cobj);
425 * has mdd_write_lock on src already, but not on tgt yet
427 int mdd_link_sanity_check(const struct lu_env *env,
428 struct mdd_object *tgt_obj,
429 const struct lu_name *lname,
430 struct mdd_object *src_obj)
432 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
436 /* Local ops, no lookup before link, check filename length here. */
437 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
438 RETURN(-ENAMETOOLONG);
440 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
443 if (S_ISDIR(mdd_object_type(src_obj)))
446 LASSERT(src_obj != tgt_obj);
448 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
453 rc = __mdd_may_link(env, src_obj);
458 const struct dt_rec *__mdd_fid_rec(const struct lu_env *env,
459 const struct lu_fid *fid)
461 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
463 fid_pack(pack, fid, &mdd_env_info(env)->mti_fid2);
464 return (const struct dt_rec *)pack;
468 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
469 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
470 * (maybe too large to be represented). It is a trick to break through the
471 * "i_nlink" limitation for subdir count.
473 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
474 struct thandle *handle)
476 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
477 struct mdd_device *m = mdd_obj2mdd_dev(obj);
479 if (!mdd_is_mnlink(obj)) {
480 if (S_ISDIR(mdd_object_type(obj))) {
481 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
484 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
485 obj->mod_flags |= MNLINK_OBJ;
486 tmp_la->la_nlink = 1;
487 tmp_la->la_valid = LA_NLINK;
488 mdd_attr_set_internal(env, obj, tmp_la, handle,
493 mdo_ref_add(env, obj, handle);
497 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
498 struct thandle *handle, int is_dot)
500 if (!mdd_is_mnlink(obj) || is_dot)
501 mdo_ref_del(env, obj, handle);
504 /* insert named index, add reference if isdir */
505 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
506 const struct lu_fid *lf, const char *name, int is_dir,
507 struct thandle *handle, struct lustre_capa *capa)
509 struct dt_object *next = mdd_object_child(pobj);
513 if (dt_try_as_dir(env, next)) {
514 rc = next->do_index_ops->dio_insert(env, next,
515 __mdd_fid_rec(env, lf),
516 (const struct dt_key *)name,
524 mdd_write_lock(env, pobj);
525 __mdd_ref_add(env, pobj, handle);
526 mdd_write_unlock(env, pobj);
532 /* delete named index, drop reference if isdir */
533 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
534 const char *name, int is_dir, struct thandle *handle,
535 struct lustre_capa *capa)
537 struct dt_object *next = mdd_object_child(pobj);
541 if (dt_try_as_dir(env, next)) {
542 rc = next->do_index_ops->dio_delete(env, next,
543 (struct dt_key *)name,
545 if (rc == 0 && is_dir) {
548 if (name != NULL && name[0] == '.' && name[1] == 0)
550 mdd_write_lock(env, pobj);
551 __mdd_ref_del(env, pobj, handle, is_dot);
552 mdd_write_unlock(env, pobj);
561 __mdd_index_insert_only(const struct lu_env *env, struct mdd_object *pobj,
562 const struct lu_fid *lf, const char *name,
563 struct thandle *handle, struct lustre_capa *capa)
565 struct dt_object *next = mdd_object_child(pobj);
569 if (dt_try_as_dir(env, next)) {
570 rc = next->do_index_ops->dio_insert(env, next,
571 __mdd_fid_rec(env, lf),
572 (const struct dt_key *)name,
580 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
581 struct md_object *src_obj, const struct lu_name *lname,
584 char *name = lname->ln_name;
585 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
586 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
587 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
588 struct mdd_device *mdd = mdo2mdd(src_obj);
589 struct dynlock_handle *dlh;
590 struct thandle *handle;
594 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
595 handle = mdd_trans_start(env, mdd);
597 RETURN(PTR_ERR(handle));
599 dlh = mdd_pdo_write_lock(env, mdd_tobj, name);
601 GOTO(out_trans, rc = -ENOMEM);
602 mdd_write_lock(env, mdd_sobj);
604 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
606 GOTO(out_unlock, rc);
608 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
610 mdd_object_capa(env, mdd_tobj));
612 GOTO(out_unlock, rc);
614 __mdd_ref_add(env, mdd_sobj, handle);
616 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
617 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
619 la->la_valid = LA_CTIME | LA_MTIME;
620 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
622 GOTO(out_unlock, rc);
624 la->la_valid = LA_CTIME;
625 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
628 mdd_write_unlock(env, mdd_sobj);
629 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
631 mdd_trans_stop(env, mdd, rc, handle);
635 /* caller should take a lock before calling */
636 int mdd_finish_unlink(const struct lu_env *env,
637 struct mdd_object *obj, struct md_attr *ma,
643 rc = mdd_iattr_get(env, obj, ma);
644 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
645 /* add new orphan and the object
646 * will be deleted during the object_put() */
647 if (__mdd_orphan_add(env, obj, th) == 0)
648 obj->mod_flags |= ORPHAN_OBJ;
650 obj->mod_flags |= DEAD_OBJ;
651 if (obj->mod_count == 0)
652 rc = mdd_object_kill(env, obj, ma);
654 /* clear MA_LOV | MA_COOKIE, if we do not
655 * unlink it in case we get it somewhere */
656 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
658 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
665 * has mdd_write_lock on cobj already, but not on pobj yet
667 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
668 struct mdd_object *cobj, struct md_attr *ma)
673 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
678 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
679 struct md_object *cobj, const struct lu_name *lname,
682 char *name = lname->ln_name;
683 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
684 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
685 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
686 struct mdd_device *mdd = mdo2mdd(pobj);
687 struct dynlock_handle *dlh;
688 struct thandle *handle;
692 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
693 PFID(mdd_object_fid(mdd_cobj)));
695 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
699 handle = mdd_trans_start(env, mdd);
701 RETURN(PTR_ERR(handle));
704 dlh = mdd_pdo_write_lock(env, mdd_pobj, name);
706 GOTO(out_trans, rc = -ENOMEM);
707 mdd_write_lock(env, mdd_cobj);
709 is_dir = S_ISDIR(ma->ma_attr.la_mode);
710 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
714 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
715 mdd_object_capa(env, mdd_pobj));
719 __mdd_ref_del(env, mdd_cobj, handle, 0);
722 __mdd_ref_del(env, mdd_cobj, handle, 1);
724 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
725 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
727 la->la_valid = LA_CTIME | LA_MTIME;
728 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
732 la->la_valid = LA_CTIME;
733 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
737 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
740 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
741 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
745 mdd_write_unlock(env, mdd_cobj);
746 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
748 mdd_trans_stop(env, mdd, rc, handle);
752 /* has not lock on pobj yet */
753 static int mdd_ni_sanity_check(const struct lu_env *env,
754 struct md_object *pobj,
755 const struct md_attr *ma)
757 struct mdd_object *obj = md2mdd_obj(pobj);
761 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
764 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
772 static int mdd_name_insert(const struct lu_env *env,
773 struct md_object *pobj,
774 const struct lu_name *lname,
775 const struct lu_fid *fid,
776 const struct md_attr *ma)
778 char *name = lname->ln_name;
779 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
780 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
781 struct mdd_device *mdd = mdo2mdd(pobj);
782 struct dynlock_handle *dlh;
783 struct thandle *handle;
784 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
788 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
789 handle = mdd_trans_start(env, mdo2mdd(pobj));
791 RETURN(PTR_ERR(handle));
793 dlh = mdd_pdo_write_lock(env, mdd_obj, name);
795 GOTO(out_trans, rc = -ENOMEM);
797 rc = mdd_ni_sanity_check(env, pobj, ma);
799 GOTO(out_unlock, rc);
801 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
802 handle, BYPASS_CAPA);
804 GOTO(out_unlock, rc);
807 * For some case, no need update obj's ctime (LA_CTIME is not set),
809 * For other cases, update obj's ctime (LA_CTIME is set),
812 if (ma->ma_attr.la_valid & LA_CTIME) {
813 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
814 la->la_valid = LA_CTIME | LA_MTIME;
815 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
820 mdd_pdo_write_unlock(env, mdd_obj, dlh);
822 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
826 /* has not lock on pobj yet */
827 static int mdd_nr_sanity_check(const struct lu_env *env,
828 struct md_object *pobj,
829 const struct md_attr *ma)
831 struct mdd_object *obj = md2mdd_obj(pobj);
835 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
838 rc = mdd_may_unlink(env, obj, ma);
846 static int mdd_name_remove(const struct lu_env *env,
847 struct md_object *pobj,
848 const struct lu_name *lname,
849 const struct md_attr *ma)
851 char *name = lname->ln_name;
852 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
853 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
854 struct mdd_device *mdd = mdo2mdd(pobj);
855 struct dynlock_handle *dlh;
856 struct thandle *handle;
857 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
861 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
862 handle = mdd_trans_start(env, mdd);
864 RETURN(PTR_ERR(handle));
866 dlh = mdd_pdo_write_lock(env, mdd_obj, name);
868 GOTO(out_trans, rc = -ENOMEM);
870 rc = mdd_nr_sanity_check(env, pobj, ma);
872 GOTO(out_unlock, rc);
874 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
875 handle, BYPASS_CAPA);
877 GOTO(out_unlock, rc);
880 * For some case, no need update obj's ctime (LA_CTIME is not set),
882 * For other cases, update obj's ctime (LA_CTIME is set),
885 if (ma->ma_attr.la_valid & LA_CTIME) {
886 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
887 la->la_valid = LA_CTIME | LA_MTIME;
888 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
893 mdd_pdo_write_unlock(env, mdd_obj, dlh);
895 mdd_trans_stop(env, mdd, rc, handle);
901 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
903 static int mdd_rt_sanity_check(const struct lu_env *env,
904 struct mdd_object *tgt_pobj,
905 struct mdd_object *tobj,
911 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
914 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
915 * exist. In fact, tobj must exist, otherwise the call trace will be:
916 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
917 * When get here, tobj must be NOT NULL, the other case has been
918 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
920 * So check may_delete, but not check nlink of tgt_pobj. */
922 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
927 static int mdd_rename_tgt(const struct lu_env *env,
928 struct md_object *pobj, struct md_object *tobj,
929 const struct lu_fid *lf, const struct lu_name *lname,
932 char *name = lname->ln_name;
933 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
934 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
935 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
936 struct mdd_device *mdd = mdo2mdd(pobj);
937 struct dynlock_handle *dlh;
938 struct thandle *handle;
942 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
943 handle = mdd_trans_start(env, mdd);
945 RETURN(PTR_ERR(handle));
947 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name);
949 GOTO(out_trans, rc = -ENOMEM);
951 mdd_write_lock(env, mdd_tobj);
953 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
958 * If rename_tgt is called then we should just re-insert name with
959 * correct fid, no need to dec/inc parent nlink if obj is dir.
961 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
965 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
970 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
971 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
973 la->la_valid = LA_CTIME | LA_MTIME;
974 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
979 * For tobj is remote case cmm layer has processed
980 * and pass NULL tobj to here. So when tobj is NOT NULL,
981 * it must be local one.
983 if (tobj && mdd_object_exists(mdd_tobj)) {
984 __mdd_ref_del(env, mdd_tobj, handle, 0);
986 /* Remove dot reference. */
987 if (S_ISDIR(ma->ma_attr.la_mode))
988 __mdd_ref_del(env, mdd_tobj, handle, 1);
990 la->la_valid = LA_CTIME;
991 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
995 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1002 mdd_write_unlock(env, mdd_tobj);
1003 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1005 mdd_trans_stop(env, mdd, rc, handle);
1010 * The permission has been checked when obj created, no need check again.
1012 static int mdd_cd_sanity_check(const struct lu_env *env,
1013 struct mdd_object *obj)
1018 if (!obj || mdd_is_dead_obj(obj))
1025 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1026 struct md_object *cobj, const struct md_op_spec *spec,
1029 struct mdd_device *mdd = mdo2mdd(cobj);
1030 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1031 struct mdd_object *son = md2mdd_obj(cobj);
1032 struct lu_attr *attr = &ma->ma_attr;
1033 struct lov_mds_md *lmm = NULL;
1035 struct thandle *handle;
1039 rc = mdd_cd_sanity_check(env, son);
1043 if (!md_should_create(spec->sp_cr_flags))
1046 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1051 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1052 handle = mdd_trans_start(env, mdd);
1054 GOTO(out_free, rc = PTR_ERR(handle));
1057 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1058 * Should this be fixed?
1061 /* Replay creates has objects already */
1063 if (spec->u.sp_ea.no_lov_create) {
1064 CDEBUG(D_INFO, "we already have lov ea\n");
1065 rc = mdd_lov_set_md(env, mdd_pobj, son,
1066 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1067 spec->u.sp_ea.eadatalen, handle, 0);
1070 /* No need mdd_lsm_sanity_check here */
1071 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1072 lmm_size, handle, 0);
1075 rc = mdd_attr_get_internal_locked(env, son, ma);
1077 /* update lov_objid data, must be before transaction stop! */
1079 mdd_lov_objid_update(mdd, lmm);
1081 mdd_trans_stop(env, mdd, rc, handle);
1083 /* Finish mdd_lov_create() stuff. */
1084 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1089 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1090 const struct lu_name *lname, struct lu_fid* fid, int mask)
1092 char *name = lname->ln_name;
1093 const struct dt_key *key = (const struct dt_key *)name;
1094 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1095 struct mdd_device *m = mdo2mdd(pobj);
1096 struct dt_object *dir = mdd_object_child(mdd_obj);
1097 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
1101 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1104 rc = mdd_object_exists(mdd_obj);
1105 if (unlikely(rc == 0))
1107 else if (unlikely(rc < 0)) {
1108 CERROR("Object "DFID" locates on remote server\n",
1109 PFID(mdo2fid(mdd_obj)));
1113 /* The common filename length check. */
1114 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1115 RETURN(-ENAMETOOLONG);
1117 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask);
1121 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1122 dt_try_as_dir(env, dir))) {
1123 rc = dir->do_index_ops->dio_lookup(env, dir,
1124 (struct dt_rec *)pack, key,
1125 mdd_object_capa(env, mdd_obj));
1127 rc = fid_unpack(pack, fid);
1134 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1135 struct mdd_object *child, struct md_attr *ma,
1136 struct thandle *handle)
1142 * Update attributes for child.
1145 * (1) the valid bits should be converted between Lustre and Linux;
1146 * (2) maybe, the child attributes should be set in OSD when creation.
1149 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1153 if (S_ISDIR(ma->ma_attr.la_mode)) {
1154 /* Add "." and ".." for newly created dir */
1155 __mdd_ref_add(env, child, handle);
1156 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1157 dot, handle, BYPASS_CAPA);
1159 rc = __mdd_index_insert_only(env, child, pfid,
1165 rc2 = __mdd_index_delete(env, child, dot, 1,
1166 handle, BYPASS_CAPA);
1168 CERROR("Failure to cleanup after dotdot"
1169 " creation: %d (%d)\n", rc2, rc);
1176 /* has not lock on pobj yet */
1177 static int mdd_create_sanity_check(const struct lu_env *env,
1178 struct md_object *pobj,
1179 const struct lu_name *lname,
1181 struct md_op_spec *spec)
1183 struct mdd_thread_info *info = mdd_env_info(env);
1184 struct lu_attr *la = &info->mti_la;
1185 struct lu_fid *fid = &info->mti_fid;
1186 struct mdd_object *obj = md2mdd_obj(pobj);
1187 struct mdd_device *m = mdo2mdd(pobj);
1188 int lookup = spec->sp_cr_lookup;
1193 if (mdd_is_dead_obj(obj))
1197 * In some cases this lookup is not needed - we know before if name
1198 * exists or not because MDT performs lookup for it.
1199 * name length check is done in lookup.
1203 * Check if the name already exist, though it will be checked in
1204 * _index_insert also, for avoiding rolling back if exists
1207 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1208 MAY_WRITE | MAY_EXEC);
1210 RETURN(rc ? : -EEXIST);
1213 * Check WRITE permission for the parent.
1214 * EXEC permission have been checked
1215 * when lookup before create already.
1217 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE);
1223 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1227 if (la->la_mode & S_ISGID) {
1228 ma->ma_attr.la_gid = la->la_gid;
1229 if (S_ISDIR(ma->ma_attr.la_mode)) {
1230 ma->ma_attr.la_mode |= S_ISGID;
1231 ma->ma_attr.la_valid |= LA_MODE;
1235 switch (ma->ma_attr.la_mode & S_IFMT) {
1237 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1239 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1240 RETURN(-ENAMETOOLONG);
1260 * Create object and insert it into namespace.
1262 static int mdd_create(const struct lu_env *env,
1263 struct md_object *pobj,
1264 const struct lu_name *lname,
1265 struct md_object *child,
1266 struct md_op_spec *spec,
1269 char *name = lname->ln_name;
1270 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1271 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1272 struct mdd_object *son = md2mdd_obj(child);
1273 struct mdd_device *mdd = mdo2mdd(pobj);
1274 struct lu_attr *attr = &ma->ma_attr;
1275 struct lov_mds_md *lmm = NULL;
1276 struct thandle *handle;
1277 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1278 struct dynlock_handle *dlh;
1282 * Two operations have to be performed:
1284 * - allocation of new object (->do_create()), and
1286 * - insertion into parent index (->dio_insert()).
1288 * Due to locking, operation order is not important, when both are
1289 * successful, *but* error handling cases are quite different:
1291 * - if insertion is done first, and following object creation fails,
1292 * insertion has to be rolled back, but this operation might fail
1293 * also leaving us with dangling index entry.
1295 * - if creation is done first, is has to be undone if insertion
1296 * fails, leaving us with leaked space, which is neither good, nor
1299 * It seems that creation-first is simplest solution, but it is
1300 * sub-optimal in the frequent
1305 * case, because second mkdir is bound to create object, only to
1306 * destroy it immediately.
1308 * To avoid this follow local file systems that do double lookup:
1310 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1312 * 1. create (mdd_object_create_internal())
1314 * 2. insert (__mdd_index_insert(), lookup again)
1317 /* Sanity checks before big job. */
1318 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1323 * No RPC inside the transaction, so OST objects should be created at
1326 if (S_ISREG(attr->la_mode)) {
1327 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1333 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1334 handle = mdd_trans_start(env, mdd);
1336 GOTO(out_free, rc = PTR_ERR(handle));
1338 dlh = mdd_pdo_write_lock(env, mdd_pobj, name);
1340 GOTO(out_trans, rc = -ENOMEM);
1343 * XXX: Check that link can be added to the parent in mkdir case.
1346 mdd_write_lock(env, son);
1347 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle);
1349 mdd_write_unlock(env, son);
1355 #ifdef CONFIG_FS_POSIX_ACL
1356 mdd_read_lock(env, mdd_pobj);
1357 rc = mdd_acl_init(env, mdd_pobj, son, &ma->ma_attr.la_mode, handle);
1358 mdd_read_unlock(env, mdd_pobj);
1360 mdd_write_unlock(env, son);
1363 ma->ma_attr.la_valid |= LA_MODE;
1367 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj),
1369 mdd_write_unlock(env, son);
1372 * Object has no links, so it will be destroyed when last
1373 * reference is released. (XXX not now.)
1379 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1380 name, S_ISDIR(attr->la_mode), handle,
1381 mdd_object_capa(env, mdd_pobj));
1388 /* No need mdd_lsm_sanity_check here */
1389 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1391 CERROR("error on stripe info copy %d \n", rc);
1394 if (lmm && lmm_size > 0) {
1395 /* Set Lov here, do not get lmm again later */
1396 memcpy(ma->ma_lmm, lmm, lmm_size);
1397 ma->ma_lmm_size = lmm_size;
1398 ma->ma_valid |= MA_LOV;
1401 if (S_ISLNK(attr->la_mode)) {
1402 struct dt_object *dt = mdd_object_child(son);
1403 const char *target_name = spec->u.sp_symname;
1404 int sym_len = strlen(target_name);
1405 const struct lu_buf *buf;
1408 buf = mdd_buf_get_const(env, target_name, sym_len);
1409 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1410 mdd_object_capa(env, son));
1415 GOTO(cleanup, rc = -EFAULT);
1419 la->la_valid = LA_CTIME | LA_MTIME;
1420 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1424 /* Return attr back. */
1425 rc = mdd_attr_get_internal_locked(env, son, ma);
1428 if (rc && created) {
1432 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1433 S_ISDIR(attr->la_mode),
1434 handle, BYPASS_CAPA);
1436 CERROR("error can not cleanup destroy %d\n",
1441 mdd_write_lock(env, son);
1442 __mdd_ref_del(env, son, handle, 0);
1443 if (initialized && S_ISDIR(attr->la_mode))
1444 __mdd_ref_del(env, son, handle, 1);
1445 mdd_write_unlock(env, son);
1449 /* update lov_objid data, must be before transaction stop! */
1451 mdd_lov_objid_update(mdd, lmm);
1453 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1455 mdd_trans_stop(env, mdd, rc, handle);
1457 /* finis lov_create stuff, free all temporary data */
1458 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1463 * Get locks on parents in proper order
1464 * RETURN: < 0 - error, rename_order if successful
1472 static int mdd_rename_order(const struct lu_env *env,
1473 struct mdd_device *mdd,
1474 struct mdd_object *src_pobj,
1475 struct mdd_object *tgt_pobj)
1477 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1481 if (src_pobj == tgt_pobj)
1482 RETURN(MDD_RN_SAME);
1484 /* compared the parent child relationship of src_p&tgt_p */
1485 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1487 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1490 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1503 /* has not mdd_write{read}_lock on any obj yet. */
1504 static int mdd_rename_sanity_check(const struct lu_env *env,
1505 struct mdd_object *src_pobj,
1506 struct mdd_object *tgt_pobj,
1507 struct mdd_object *sobj,
1508 struct mdd_object *tobj,
1514 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1517 /* XXX: when get here, sobj must NOT be NULL,
1518 * the other case has been processed in cml_rename
1519 * before mdd_rename and enable MDS_PERM_BYPASS. */
1521 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1525 /* XXX: when get here, "tobj == NULL" means tobj must
1526 * NOT exist (neither on remote MDS, such case has been
1527 * processed in cml_rename before mdd_rename and enable
1529 * So check may_create, but not check may_unlink. */
1531 rc = mdd_may_create(env, tgt_pobj, NULL,
1532 (src_pobj != tgt_pobj), 0);
1534 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1535 (src_pobj != tgt_pobj), 1);
1537 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1538 S_ISDIR(ma->ma_attr.la_mode))
1539 rc = __mdd_may_link(env, tgt_pobj);
1544 /* src object can be remote that is why we use only fid and type of object */
1545 static int mdd_rename(const struct lu_env *env,
1546 struct md_object *src_pobj, struct md_object *tgt_pobj,
1547 const struct lu_fid *lf, const struct lu_name *lsname,
1548 struct md_object *tobj, const struct lu_name *ltname,
1551 char *sname = lsname->ln_name;
1552 char *tname = ltname->ln_name;
1553 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1554 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj);
1555 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1556 struct mdd_device *mdd = mdo2mdd(src_pobj);
1557 struct mdd_object *mdd_sobj = NULL;
1558 struct mdd_object *mdd_tobj = NULL;
1559 struct dynlock_handle *sdlh, *tdlh;
1560 struct thandle *handle;
1565 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1566 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1569 mdd_tobj = md2mdd_obj(tobj);
1571 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
1572 handle = mdd_trans_start(env, mdd);
1574 RETURN(PTR_ERR(handle));
1576 /* FIXME: Should consider tobj and sobj too in rename_lock. */
1577 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
1579 GOTO(cleanup_unlocked, rc);
1581 /* Get locks in determined order */
1582 if (rc == MDD_RN_SAME) {
1583 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1584 /* check hashes to determine do we need one lock or two */
1585 if (mdd_name2hash(sname) != mdd_name2hash(tname))
1586 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1589 } else if (rc == MDD_RN_SRCTGT) {
1590 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1591 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1593 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1594 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1596 if (sdlh == NULL || tdlh == NULL)
1597 GOTO(cleanup, rc = -ENOMEM);
1599 mdd_sobj = mdd_object_find(env, mdd, lf);
1600 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
1601 mdd_sobj, mdd_tobj, ma);
1605 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
1606 mdd_object_capa(env, mdd_spobj));
1611 * Here tobj can be remote one, so we do index_delete unconditionally
1612 * and -ENOENT is allowed.
1614 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
1615 mdd_object_capa(env, mdd_tpobj));
1616 if (rc != 0 && rc != -ENOENT)
1619 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
1620 mdd_object_capa(env, mdd_tpobj));
1624 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1625 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1627 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
1629 la->la_valid = LA_CTIME;
1630 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
1637 * For tobj is remote case cmm layer has processed
1638 * and set tobj to NULL then. So when tobj is NOT NULL,
1639 * it must be local one.
1641 if (tobj && mdd_object_exists(mdd_tobj)) {
1642 mdd_write_lock(env, mdd_tobj);
1643 __mdd_ref_del(env, mdd_tobj, handle, 0);
1645 /* Remove dot reference. */
1647 __mdd_ref_del(env, mdd_tobj, handle, 1);
1649 la->la_valid = LA_CTIME;
1650 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1654 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1655 mdd_write_unlock(env, mdd_tobj);
1660 la->la_valid = LA_CTIME | LA_MTIME;
1661 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
1665 if (mdd_spobj != mdd_tpobj) {
1666 la->la_valid = LA_CTIME | LA_MTIME;
1667 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
1673 if (likely(tdlh) && sdlh != tdlh)
1674 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
1676 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
1678 mdd_trans_stop(env, mdd, rc, handle);
1680 mdd_object_put(env, mdd_sobj);
1684 struct md_dir_operations mdd_dir_ops = {
1685 .mdo_is_subdir = mdd_is_subdir,
1686 .mdo_lookup = mdd_lookup,
1687 .mdo_create = mdd_create,
1688 .mdo_rename = mdd_rename,
1689 .mdo_link = mdd_link,
1690 .mdo_unlink = mdd_unlink,
1691 .mdo_name_insert = mdd_name_insert,
1692 .mdo_name_remove = mdd_name_remove,
1693 .mdo_rename_tgt = mdd_rename_tgt,
1694 .mdo_create_data = mdd_create_data