4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
61 const struct lu_name *lname, struct lu_fid* fid,
63 static int mdd_declare_links_add(const struct lu_env *env,
64 struct mdd_object *mdd_obj,
65 struct thandle *handle);
66 static int mdd_links_add(const struct lu_env *env,
67 struct mdd_object *mdd_obj,
68 const struct lu_fid *pfid,
69 const struct lu_name *lname,
70 struct thandle *handle, int first);
71 static int mdd_links_rename(const struct lu_env *env,
72 struct mdd_object *mdd_obj,
73 const struct lu_fid *oldpfid,
74 const struct lu_name *oldlname,
75 const struct lu_fid *newpfid,
76 const struct lu_name *newlname,
77 struct thandle *handle);
80 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
81 const struct lu_name *lname, struct lu_fid* fid, int mask)
83 const char *name = lname->ln_name;
84 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
85 struct dynlock_handle *dlh;
88 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
89 if (unlikely(dlh == NULL))
91 rc = __mdd_lookup(env, pobj, lname, fid, mask);
92 mdd_pdo_read_unlock(env, mdd_obj, dlh);
97 int mdd_lookup(const struct lu_env *env,
98 struct md_object *pobj, const struct lu_name *lname,
99 struct lu_fid* fid, struct md_op_spec *spec)
103 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
107 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
110 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
114 * For root fid use special function, which does not compare version component
115 * of fid. Version component is different for root fids on all MDTs.
117 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
119 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
120 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
124 * return 1: if lf is the fid of the ancestor of p1;
127 * return -EREMOTE: if remote object is found, in this
128 * case fid of remote object is saved to @pf;
130 * otherwise: values < 0, errors.
132 static int mdd_is_parent(const struct lu_env *env,
133 struct mdd_device *mdd,
134 struct mdd_object *p1,
135 const struct lu_fid *lf,
138 struct mdd_object *parent = NULL;
143 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
144 pfid = &mdd_env_info(env)->mti_fid;
146 /* Check for root first. */
147 if (mdd_is_root(mdd, mdo2fid(p1)))
151 /* this is done recursively, bypass capa for each obj */
152 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
153 rc = mdd_parent_fid(env, p1, pfid);
156 if (mdd_is_root(mdd, pfid))
158 if (lu_fid_eq(pfid, lf))
161 mdd_object_put(env, parent);
162 parent = mdd_object_find(env, mdd, pfid);
164 /* cross-ref parent */
165 if (parent == NULL) {
168 GOTO(out, rc = -EREMOTE);
169 } else if (IS_ERR(parent))
170 GOTO(out, rc = PTR_ERR(parent));
175 if (parent && !IS_ERR(parent))
176 mdd_object_put(env, parent);
181 * No permission check is needed.
183 * returns 1: if fid is ancestor of @mo;
184 * returns 0: if fid is not a ancestor of @mo;
186 * returns EREMOTE if remote object is found, fid of remote object is saved to
189 * returns < 0: if error
191 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
192 const struct lu_fid *fid, struct lu_fid *sfid)
194 struct mdd_device *mdd = mdo2mdd(mo);
198 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
201 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
205 } else if (rc == 1) {
206 /* found @fid is parent */
214 * Check that @dir contains no entries except (possibly) dot and dotdot.
219 * -ENOTDIR not a directory object
220 * -ENOTEMPTY not empty
224 static int mdd_dir_is_empty(const struct lu_env *env,
225 struct mdd_object *dir)
228 struct dt_object *obj;
229 const struct dt_it_ops *iops;
233 obj = mdd_object_child(dir);
234 if (!dt_try_as_dir(env, obj))
237 iops = &obj->do_index_ops->dio_it;
238 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
240 result = iops->get(env, it, (const void *)"");
243 for (result = 0, i = 0; result == 0 && i < 3; ++i)
244 result = iops->next(env, it);
247 else if (result == +1)
249 } else if (result == 0)
251 * Huh? Index contains no zero key?
258 result = PTR_ERR(it);
262 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
264 struct mdd_device *m = mdd_obj2mdd_dev(obj);
265 struct lu_attr *la = &mdd_env_info(env)->mti_la;
269 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
274 * Subdir count limitation can be broken through.
276 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
277 !S_ISDIR(la->la_mode))
284 * Check whether it may create the cobj under the pobj.
287 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
288 struct mdd_object *cobj, int check_perm, int check_nlink)
293 if (cobj && mdd_object_exists(cobj))
296 if (mdd_is_dead_obj(pobj))
300 rc = mdd_permission_internal_locked(env, pobj, NULL,
301 MAY_WRITE | MAY_EXEC,
303 if (!rc && check_nlink)
304 rc = __mdd_may_link(env, pobj);
310 * Check whether can unlink from the pobj in the case of "cobj == NULL".
312 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
313 const struct lu_attr *attr)
318 if (mdd_is_dead_obj(pobj))
321 if ((attr->la_valid & LA_FLAGS) &&
322 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
325 rc = mdd_permission_internal_locked(env, pobj, NULL,
326 MAY_WRITE | MAY_EXEC,
331 if (mdd_is_append(pobj))
338 * pobj == NULL is remote ops case, under such case, pobj's
339 * VTX feature has been checked already, no need check again.
341 static inline int mdd_is_sticky(const struct lu_env *env,
342 struct mdd_object *pobj,
343 struct mdd_object *cobj)
345 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
346 struct md_ucred *uc = md_ucred(env);
350 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
354 if (!(tmp_la->la_mode & S_ISVTX) ||
355 (tmp_la->la_uid == uc->mu_fsuid))
359 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
363 if (tmp_la->la_uid == uc->mu_fsuid)
366 return !mdd_capable(uc, CFS_CAP_FOWNER);
370 * Check whether it may delete the cobj from the pobj.
373 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
374 struct mdd_object *cobj, struct lu_attr *cattr,
375 struct lu_attr *src_attr, int check_perm, int check_empty)
381 if (!mdd_object_exists(cobj))
384 if (mdd_is_dead_obj(cobj))
388 if (!mdd_object_exists(pobj))
391 if (mdd_is_dead_obj(pobj))
395 rc = mdd_permission_internal_locked(env, pobj, NULL,
396 MAY_WRITE | MAY_EXEC,
402 if (mdd_is_append(pobj))
406 if (mdd_is_sticky(env, pobj, cobj))
409 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
412 if ((cattr->la_valid & LA_FLAGS) &&
413 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
416 /* additional check the rename case */
418 if (S_ISDIR(src_attr->la_mode)) {
419 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
421 if (!S_ISDIR(cattr->la_mode))
424 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
426 } else if (S_ISDIR(cattr->la_mode))
430 if (S_ISDIR(cattr->la_mode) && check_empty)
431 rc = mdd_dir_is_empty(env, cobj);
438 * has mdd_write_lock on src already, but not on tgt yet
440 int mdd_link_sanity_check(const struct lu_env *env,
441 struct mdd_object *tgt_obj,
442 const struct lu_name *lname,
443 struct mdd_object *src_obj)
445 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
449 if (!mdd_object_exists(src_obj))
452 if (mdd_is_dead_obj(src_obj))
455 /* Local ops, no lookup before link, check filename length here. */
456 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
457 RETURN(-ENAMETOOLONG);
459 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
462 if (S_ISDIR(mdd_object_type(src_obj)))
465 LASSERT(src_obj != tgt_obj);
467 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
472 rc = __mdd_may_link(env, src_obj);
477 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
478 const char *name, struct thandle *handle,
479 struct lustre_capa *capa)
481 struct dt_object *next = mdd_object_child(pobj);
485 if (dt_try_as_dir(env, next)) {
486 rc = next->do_index_ops->dio_delete(env, next,
487 (struct dt_key *)name,
495 static int __mdd_index_insert_only(const struct lu_env *env,
496 struct mdd_object *pobj,
497 const struct lu_fid *lf, const char *name,
498 struct thandle *handle,
499 struct lustre_capa *capa)
501 struct dt_object *next = mdd_object_child(pobj);
505 if (dt_try_as_dir(env, next)) {
506 struct md_ucred *uc = md_ucred(env);
508 rc = next->do_index_ops->dio_insert(env, next,
510 (const struct dt_key *)name,
511 handle, capa, uc->mu_cap &
512 CFS_CAP_SYS_RESOURCE_MASK);
519 /* insert named index, add reference if isdir */
520 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
521 const struct lu_fid *lf, const char *name, int is_dir,
522 struct thandle *handle, struct lustre_capa *capa)
527 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
528 if (rc == 0 && is_dir) {
529 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
530 mdo_ref_add(env, pobj, handle);
531 mdd_write_unlock(env, pobj);
536 /* delete named index, drop reference if isdir */
537 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
538 const char *name, int is_dir, struct thandle *handle,
539 struct lustre_capa *capa)
544 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
545 if (rc == 0 && is_dir) {
546 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
547 mdo_ref_del(env, pobj, handle);
548 mdd_write_unlock(env, pobj);
554 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
555 int reclen, struct thandle *handle)
559 /* XXX: this is a temporary solution to declare llog changes
560 * will be fixed in 2.3 with new llog implementation */
562 LASSERT(mdd->mdd_capa);
564 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
565 * have to set the parameter 'size' as INT_MAX or 0 to inform
566 * OSD that this record write is for a llog write or catalog
567 * header update, and osd declare function will reserve less
568 * credits for optimization purpose.
570 * Reserve 6 blocks for a llog write, since the llog file is
571 * usually small, reserve 2 blocks for catalog header update,
572 * because we know for sure that catalog header is already
575 * This hack should be removed in 2.3.
579 rc = dt_declare_record_write(env, mdd->mdd_capa,
580 DECLARE_LLOG_WRITE, 0, handle);
584 /* header will be updated as well */
585 rc = dt_declare_record_write(env, mdd->mdd_capa,
586 DECLARE_LLOG_WRITE, 0, handle);
590 /* also we should be able to create new plain log */
591 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
595 /* new record referencing new plain llog */
596 rc = dt_declare_record_write(env, mdd->mdd_capa,
597 DECLARE_LLOG_WRITE, 0, handle);
601 /* catalog's header will be updated as well */
602 rc = dt_declare_record_write(env, mdd->mdd_capa,
603 DECLARE_LLOG_REWRITE, 0, handle);
608 int mdd_declare_changelog_store(const struct lu_env *env,
609 struct mdd_device *mdd,
610 const struct lu_name *fname,
611 struct thandle *handle)
616 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
619 /* we'll be writing payload + llog header */
620 reclen = sizeof(struct llog_changelog_rec);
622 reclen += fname->ln_namelen;
623 reclen = llog_data_len(reclen);
625 return mdd_declare_llog_record(env, mdd, reclen, handle);
628 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
629 struct mdd_device *mdd,
630 const struct lu_name *tname,
631 const struct lu_name *sname,
632 struct thandle *handle)
637 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
640 /* we'll be writing payload + llog header */
641 reclen = sizeof(struct llog_changelog_ext_rec);
643 reclen += tname->ln_namelen;
645 reclen += 1 + sname->ln_namelen;
646 reclen = llog_data_len(reclen);
648 return mdd_declare_llog_record(env, mdd, reclen, handle);
651 /** Store a namespace change changelog record
652 * If this fails, we must fail the whole transaction; we don't
653 * want the change to commit without the log entry.
654 * \param target - mdd_object of change
655 * \param parent - parent dir/object
656 * \param tname - target name string
657 * \param handle - transacion handle
659 static int mdd_changelog_ns_store(const struct lu_env *env,
660 struct mdd_device *mdd,
661 enum changelog_rec_type type,
663 struct mdd_object *target,
664 struct mdd_object *parent,
665 const struct lu_name *tname,
666 struct thandle *handle)
668 struct llog_changelog_rec *rec;
675 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
677 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
680 LASSERT(target != NULL);
681 LASSERT(parent != NULL);
682 LASSERT(tname != NULL);
683 LASSERT(handle != NULL);
685 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
686 buf = mdd_buf_alloc(env, reclen);
687 if (buf->lb_buf == NULL)
689 rec = (struct llog_changelog_rec *)buf->lb_buf;
691 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
692 rec->cr.cr_type = (__u32)type;
693 rec->cr.cr_tfid = *mdo2fid(target);
694 rec->cr.cr_pfid = *mdo2fid(parent);
695 rec->cr.cr_namelen = tname->ln_namelen;
696 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
698 target->mod_cltime = cfs_time_current_64();
700 rc = mdd_changelog_llog_write(mdd, rec, handle);
702 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
703 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
704 PFID(&rec->cr.cr_pfid));
712 /** Store a namespace change changelog record
713 * If this fails, we must fail the whole transaction; we don't
714 * want the change to commit without the log entry.
715 * \param target - mdd_object of change
716 * \param tpfid - target parent dir/object fid
717 * \param sfid - source object fid
718 * \param spfid - source parent fid
719 * \param tname - target name string
720 * \param sname - source name string
721 * \param handle - transacion handle
723 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
724 struct mdd_device *mdd,
725 enum changelog_rec_type type,
727 struct mdd_object *target,
728 const struct lu_fid *tpfid,
729 const struct lu_fid *sfid,
730 const struct lu_fid *spfid,
731 const struct lu_name *tname,
732 const struct lu_name *sname,
733 struct thandle *handle)
735 struct llog_changelog_ext_rec *rec;
742 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
744 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
747 LASSERT(sfid != NULL);
748 LASSERT(tpfid != NULL);
749 LASSERT(tname != NULL);
750 LASSERT(handle != NULL);
752 reclen = sizeof(*rec) + tname->ln_namelen;
754 reclen += 1 + sname->ln_namelen;
755 reclen = llog_data_len(reclen);
756 buf = mdd_buf_alloc(env, reclen);
757 if (buf->lb_buf == NULL)
759 rec = (struct llog_changelog_ext_rec *)buf->lb_buf;
761 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
762 rec->cr.cr_type = (__u32)type;
763 rec->cr.cr_pfid = *tpfid;
764 rec->cr.cr_sfid = *sfid;
765 rec->cr.cr_spfid = *spfid;
766 rec->cr.cr_namelen = tname->ln_namelen;
767 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
769 LASSERT(sfid != NULL);
770 rec->cr.cr_name[tname->ln_namelen] = '\0';
771 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
773 rec->cr.cr_namelen += 1 + sname->ln_namelen;
776 if (likely(target != NULL)) {
777 rec->cr.cr_tfid = *mdo2fid(target);
778 target->mod_cltime = cfs_time_current_64();
780 fid_zero(&rec->cr.cr_tfid);
783 rc = mdd_changelog_ext_llog_write(mdd, rec, handle);
785 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
786 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
793 static int mdd_declare_link(const struct lu_env *env,
794 struct mdd_device *mdd,
795 struct mdd_object *p,
796 struct mdd_object *c,
797 const struct lu_name *name,
798 struct thandle *handle)
802 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
806 rc = mdo_declare_ref_add(env, c, handle);
810 rc = mdo_declare_attr_set(env, p, NULL, handle);
814 rc = mdo_declare_attr_set(env, c, NULL, handle);
818 rc = mdd_declare_links_add(env, c, handle);
822 rc = mdd_declare_changelog_store(env, mdd, name, handle);
827 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
828 struct md_object *src_obj, const struct lu_name *lname,
831 const char *name = lname->ln_name;
832 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
833 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
834 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
835 struct mdd_device *mdd = mdo2mdd(src_obj);
836 struct dynlock_handle *dlh;
837 struct thandle *handle;
841 handle = mdd_trans_create(env, mdd);
843 GOTO(out_pending, rc = PTR_ERR(handle));
845 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
849 rc = mdd_trans_start(env, mdd, handle);
853 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
855 GOTO(out_trans, rc = -ENOMEM);
856 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
858 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
860 GOTO(out_unlock, rc);
862 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
864 mdd_object_capa(env, mdd_tobj));
866 GOTO(out_unlock, rc);
868 rc = mdo_ref_add(env, mdd_sobj, handle);
870 __mdd_index_delete_only(env, mdd_tobj, name, handle,
871 mdd_object_capa(env, mdd_tobj));
872 GOTO(out_unlock, rc);
875 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
876 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
878 la->la_valid = LA_CTIME | LA_MTIME;
879 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
881 GOTO(out_unlock, rc);
883 la->la_valid = LA_CTIME;
884 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
886 mdd_links_add(env, mdd_sobj,
887 mdo2fid(mdd_tobj), lname, handle, 0);
892 mdd_write_unlock(env, mdd_sobj);
893 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
896 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
897 mdd_tobj, lname, handle);
899 mdd_trans_stop(env, mdd, rc, handle);
904 int mdd_declare_finish_unlink(const struct lu_env *env,
905 struct mdd_object *obj,
907 struct thandle *handle)
911 rc = orph_declare_index_insert(env, obj, handle);
915 return mdd_declare_object_kill(env, obj, ma, handle);
918 /* caller should take a lock before calling */
919 int mdd_finish_unlink(const struct lu_env *env,
920 struct mdd_object *obj, struct md_attr *ma,
925 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
928 LASSERT(mdd_write_locked(env, obj) != 0);
930 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
931 obj->mod_flags |= DEAD_OBJ;
932 /* add new orphan and the object
933 * will be deleted during mdd_close() */
934 if (obj->mod_count) {
935 rc = __mdd_orphan_add(env, obj, th);
937 CDEBUG(D_HA, "Object "DFID" is inserted into "
938 "orphan list, open count = %d\n",
939 PFID(mdd_object_fid(obj)),
942 CERROR("Object "DFID" fail to be an orphan, "
943 "open count = %d, maybe cause failed "
945 PFID(mdd_object_fid(obj)),
948 rc = mdd_object_kill(env, obj, ma, th);
954 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
961 * has mdd_write_lock on cobj already, but not on pobj yet
963 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
964 struct mdd_object *cobj, struct lu_attr *cattr)
969 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
974 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
975 struct mdd_object *p, struct mdd_object *c,
976 const struct lu_name *name, struct md_attr *ma,
977 struct thandle *handle)
981 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
985 rc = mdo_declare_ref_del(env, p, handle);
989 rc = mdo_declare_ref_del(env, c, handle);
993 rc = mdo_declare_ref_del(env, c, handle);
997 rc = mdo_declare_attr_set(env, p, NULL, handle);
1001 rc = mdo_declare_attr_set(env, c, NULL, handle);
1005 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1009 rc = mdd_declare_links_add(env, c, handle);
1013 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1018 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1019 struct md_object *cobj, const struct lu_name *lname,
1022 const char *name = lname->ln_name;
1023 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1024 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1025 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1026 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1027 struct mdd_device *mdd = mdo2mdd(pobj);
1028 struct dynlock_handle *dlh;
1029 struct thandle *handle;
1033 if (mdd_object_exists(mdd_cobj) <= 0)
1036 handle = mdd_trans_create(env, mdd);
1038 RETURN(PTR_ERR(handle));
1040 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1045 rc = mdd_trans_start(env, mdd, handle);
1049 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1051 GOTO(stop, rc = -ENOMEM);
1052 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1055 rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1059 is_dir = S_ISDIR(cattr->la_mode);
1061 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1065 rc = mdo_ref_del(env, mdd_cobj, handle);
1067 __mdd_index_insert_only(env, mdd_pobj, mdo2fid(mdd_cobj),
1069 mdd_object_capa(env, mdd_pobj));
1073 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1074 mdd_object_capa(env, mdd_pobj));
1080 mdo_ref_del(env, mdd_cobj, handle);
1082 /* fetch updated nlink */
1083 rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1087 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1088 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1090 la->la_valid = LA_CTIME | LA_MTIME;
1091 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1095 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1096 /* update ctime of an unlinked file only if it is still
1097 * opened or a link still exists */
1098 la->la_valid = LA_CTIME;
1099 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1104 /* XXX: this transfer to ma will be removed with LOD/OSP */
1105 ma->ma_attr = *cattr;
1106 ma->ma_valid |= MA_INODE;
1107 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1109 /* fetch updated nlink */
1111 rc = mdd_la_get(env, mdd_cobj, cattr,
1112 mdd_object_capa(env, mdd_cobj));
1115 /* old files may not have link ea; ignore errors */
1116 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1117 lname, NULL, NULL, handle);
1119 /* if object is removed then we can't get its attrs, use last get */
1120 if (cattr->la_nlink == 0) {
1121 ma->ma_attr = *cattr;
1122 ma->ma_valid |= MA_INODE;
1126 mdd_write_unlock(env, mdd_cobj);
1127 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1131 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1132 if ((ma->ma_valid & MA_HSM) &&
1133 (ma->ma_hsm.mh_flags & HS_EXISTS))
1134 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1136 rc = mdd_changelog_ns_store(env, mdd,
1137 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1138 mdd_cobj, mdd_pobj, lname, handle);
1142 mdd_trans_stop(env, mdd, rc, handle);
1143 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
1144 if (rc == 0 && ma->ma_valid & MA_COOKIE && ma->ma_valid & MA_LOV &&
1145 ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_UNLINK_DESTROY)
1146 /* Since echo client is incapable of destorying ost object,
1147 * it will destory the object here. */
1148 rc = mdd_lovobj_unlink(env, mdd, mdd_cobj, la, ma, 1);
1150 #warning "please remove this after 2.4 (LOD/OSP)."
1157 * The permission has been checked when obj created, no need check again.
1159 static int mdd_cd_sanity_check(const struct lu_env *env,
1160 struct mdd_object *obj)
1165 if (!obj || mdd_is_dead_obj(obj))
1172 static int mdd_declare_create_data(const struct lu_env *env,
1173 struct mdd_device *mdd,
1174 struct mdd_object *obj,
1176 struct thandle *handle)
1178 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1182 buf->lb_len = lmm_size;
1183 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1188 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1193 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1194 struct md_object *cobj, const struct md_op_spec *spec,
1197 struct mdd_device *mdd = mdo2mdd(cobj);
1198 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1199 struct mdd_object *son = md2mdd_obj(cobj);
1200 struct lov_mds_md *lmm = NULL;
1202 struct thandle *handle;
1203 struct lu_attr *attr = &mdd_env_info(env)->mti_la_for_fix;
1207 rc = mdd_cd_sanity_check(env, son);
1211 if (!md_should_create(spec->sp_cr_flags))
1213 lmm_size = ma->ma_lmm_size;
1215 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1219 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1223 /* calling ->ah_make_hint() is used to transfer information from parent */
1224 mdd_object_make_hint(env, mdd_pobj, son, attr);
1226 handle = mdd_trans_create(env, mdd);
1228 GOTO(out_free, rc = PTR_ERR(handle));
1230 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1234 rc = mdd_trans_start(env, mdd, handle);
1239 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1240 * Should this be fixed?
1243 /* Replay creates has objects already */
1245 if (spec->no_create) {
1246 CDEBUG(D_INFO, "we already have lov ea\n");
1247 rc = mdd_lov_set_md(env, mdd_pobj, son,
1248 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1249 spec->u.sp_ea.eadatalen, handle, 0);
1252 /* No need mdd_lsm_sanity_check here */
1253 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1254 lmm_size, handle, 0);
1256 /* update lov_objid data, must be before transaction stop! */
1258 mdd_lov_objid_update(mdd, lmm);
1261 mdd_trans_stop(env, mdd, rc, handle);
1263 /* Finish mdd_lov_create() stuff. */
1264 /* if no_create == 0 (not replay), we free lmm allocated by
1265 * mdd_lov_create() */
1266 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1270 /* Get fid from name and parent */
1272 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1273 const struct lu_name *lname, struct lu_fid* fid, int mask)
1275 const char *name = lname->ln_name;
1276 const struct dt_key *key = (const struct dt_key *)name;
1277 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1278 struct mdd_device *m = mdo2mdd(pobj);
1279 struct dt_object *dir = mdd_object_child(mdd_obj);
1283 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1286 rc = mdd_object_exists(mdd_obj);
1287 if (unlikely(rc == 0))
1289 else if (unlikely(rc < 0)) {
1290 CERROR("Object "DFID" locates on remote server\n",
1291 PFID(mdo2fid(mdd_obj)));
1295 /* The common filename length check. */
1296 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1297 RETURN(-ENAMETOOLONG);
1299 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1304 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1305 dt_try_as_dir(env, dir))) {
1307 rc = dir->do_index_ops->dio_lookup(env, dir,
1308 (struct dt_rec *)fid, key,
1309 mdd_object_capa(env, mdd_obj));
1320 int mdd_declare_object_initialize(const struct lu_env *env,
1321 struct mdd_object *child,
1322 struct lu_attr *attr,
1323 struct thandle *handle)
1327 rc = mdo_declare_attr_set(env, child, attr, handle);
1328 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1329 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1332 rc = mdo_declare_ref_add(env, child, handle);
1336 mdd_declare_links_add(env, child, handle);
1341 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1342 const struct lu_name *lname, struct mdd_object *child,
1343 struct lu_attr *attr, struct thandle *handle,
1344 const struct md_op_spec *spec)
1350 * Update attributes for child.
1353 * (1) the valid bits should be converted between Lustre and Linux;
1354 * (2) maybe, the child attributes should be set in OSD when creation.
1357 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1361 if (S_ISDIR(attr->la_mode)) {
1362 /* Add "." and ".." for newly created dir */
1363 mdo_ref_add(env, child, handle);
1364 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1365 dot, handle, BYPASS_CAPA);
1367 rc = __mdd_index_insert_only(env, child, pfid,
1371 mdo_ref_del(env, child, handle);
1374 mdd_links_add(env, child, pfid, lname, handle, 1);
1379 /* has not lock on pobj yet */
1380 static int mdd_create_sanity_check(const struct lu_env *env,
1381 struct md_object *pobj,
1382 struct lu_attr *pattr,
1383 const struct lu_name *lname,
1384 struct lu_attr *cattr,
1385 struct md_op_spec *spec)
1387 struct mdd_thread_info *info = mdd_env_info(env);
1388 struct lu_fid *fid = &info->mti_fid;
1389 struct mdd_object *obj = md2mdd_obj(pobj);
1390 struct mdd_device *m = mdo2mdd(pobj);
1391 int lookup = spec->sp_cr_lookup;
1396 if (mdd_is_dead_obj(obj))
1400 * In some cases this lookup is not needed - we know before if name
1401 * exists or not because MDT performs lookup for it.
1402 * name length check is done in lookup.
1406 * Check if the name already exist, though it will be checked in
1407 * _index_insert also, for avoiding rolling back if exists
1410 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1411 MAY_WRITE | MAY_EXEC);
1413 RETURN(rc ? : -EEXIST);
1416 * Check WRITE permission for the parent.
1417 * EXEC permission have been checked
1418 * when lookup before create already.
1420 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1427 if (pattr->la_mode & S_ISGID) {
1428 cattr->la_gid = pattr->la_gid;
1429 if (S_ISDIR(cattr->la_mode)) {
1430 cattr->la_mode |= S_ISGID;
1431 cattr->la_valid |= LA_MODE;
1435 switch (cattr->la_mode & S_IFMT) {
1437 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1439 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1440 RETURN(-ENAMETOOLONG);
1459 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1460 struct mdd_object *p, struct mdd_object *c,
1461 const struct lu_name *name,
1462 struct lu_attr *attr, int lmm_size,
1464 struct thandle *handle,
1465 const struct md_op_spec *spec)
1467 struct mdd_thread_info *info = mdd_env_info(env);
1468 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1471 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1475 #ifdef CONFIG_FS_POSIX_ACL
1476 if (got_def_acl > 0) {
1477 struct lu_buf *acl_buf;
1479 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1480 /* if dir, then can inherit default ACl */
1481 if (S_ISDIR(attr->la_mode)) {
1482 rc = mdo_declare_xattr_set(env, c, acl_buf,
1483 XATTR_NAME_ACL_DEFAULT,
1489 rc = mdo_declare_attr_set(env, c, &info->mti_pattr, handle);
1493 rc = mdo_declare_xattr_set(env, c, acl_buf,
1494 XATTR_NAME_ACL_ACCESS, 0, handle);
1500 if (S_ISDIR(attr->la_mode)) {
1501 rc = mdo_declare_ref_add(env, p, handle);
1506 rc = mdd_declare_object_initialize(env, c, attr, handle);
1510 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1511 name->ln_name, handle);
1515 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1520 if (S_ISLNK(attr->la_mode)) {
1521 rc = dt_declare_record_write(env, mdd_object_child(c),
1522 strlen(spec->u.sp_symname), 0,
1528 rc = mdo_declare_attr_set(env, p, attr, handle);
1532 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1536 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1544 * Create object and insert it into namespace.
1546 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1547 const struct lu_name *lname, struct md_object *child,
1548 struct md_op_spec *spec, struct md_attr* ma)
1550 struct mdd_thread_info *info = mdd_env_info(env);
1551 struct lu_attr *la = &info->mti_la_for_fix;
1552 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1553 struct mdd_object *son = md2mdd_obj(child);
1554 struct mdd_device *mdd = mdo2mdd(pobj);
1555 struct lu_attr *attr = &ma->ma_attr;
1556 struct lov_mds_md *lmm = NULL;
1557 struct thandle *handle;
1558 struct lu_attr *pattr = &info->mti_pattr;
1559 struct dynlock_handle *dlh;
1560 const char *name = lname->ln_name;
1561 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1562 int got_def_acl = 0;
1566 * Two operations have to be performed:
1568 * - an allocation of a new object (->do_create()), and
1570 * - an insertion into a parent index (->dio_insert()).
1572 * Due to locking, operation order is not important, when both are
1573 * successful, *but* error handling cases are quite different:
1575 * - if insertion is done first, and following object creation fails,
1576 * insertion has to be rolled back, but this operation might fail
1577 * also leaving us with dangling index entry.
1579 * - if creation is done first, is has to be undone if insertion
1580 * fails, leaving us with leaked space, which is neither good, nor
1583 * It seems that creation-first is simplest solution, but it is
1584 * sub-optimal in the frequent
1589 * case, because second mkdir is bound to create object, only to
1590 * destroy it immediately.
1592 * To avoid this follow local file systems that do double lookup:
1594 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1596 * 1. create (mdd_object_create_internal())
1598 * 2. insert (__mdd_index_insert(), lookup again)
1601 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1605 /* Sanity checks before big job. */
1606 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1610 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1611 GOTO(out_pending, rc = -EINPROGRESS);
1614 * No RPC inside the transaction, so OST objects should be created at
1617 if (S_ISREG(attr->la_mode)) {
1618 lmm_size = ma->ma_lmm_size;
1619 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1622 GOTO(out_pending, rc);
1625 if (!S_ISLNK(attr->la_mode)) {
1626 struct lu_buf *acl_buf;
1628 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1629 sizeof(info->mti_xattr_buf));
1630 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1631 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1632 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1633 mdd_read_unlock(env, mdd_pobj);
1636 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1640 mdd_object_make_hint(env, mdd_pobj, son, attr);
1642 handle = mdd_trans_create(env, mdd);
1644 GOTO(out_free, rc = PTR_ERR(handle));
1646 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1647 got_def_acl, lmm_size, handle, spec);
1651 rc = mdd_trans_start(env, mdd, handle);
1655 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1657 GOTO(out_trans, rc = -ENOMEM);
1659 mdd_write_lock(env, son, MOR_TGT_CHILD);
1660 rc = mdd_object_create_internal(env, mdd_pobj, son, attr, handle, spec);
1662 mdd_write_unlock(env, son);
1668 #ifdef CONFIG_FS_POSIX_ACL
1670 struct lu_buf *acl_buf;
1672 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1673 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1675 mdd_write_unlock(env, son);
1681 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1682 son, attr, handle, spec);
1683 mdd_write_unlock(env, son);
1686 * Object has no links, so it will be destroyed when last
1687 * reference is released. (XXX not now.)
1693 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1694 name, S_ISDIR(attr->la_mode), handle,
1695 mdd_object_capa(env, mdd_pobj));
1701 /* No need mdd_lsm_sanity_check here */
1702 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1704 CERROR("error on stripe info copy %d \n", rc);
1708 if (S_ISLNK(attr->la_mode)) {
1709 struct md_ucred *uc = md_ucred(env);
1710 struct dt_object *dt = mdd_object_child(son);
1711 const char *target_name = spec->u.sp_symname;
1712 int sym_len = strlen(target_name);
1713 const struct lu_buf *buf;
1716 buf = mdd_buf_get_const(env, target_name, sym_len);
1717 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1718 mdd_object_capa(env, son),
1720 CFS_CAP_SYS_RESOURCE_MASK);
1725 GOTO(cleanup, rc = -EFAULT);
1729 la->la_valid = LA_CTIME | LA_MTIME;
1730 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1736 if (rc != 0 && created != 0) {
1739 if (inserted != 0) {
1740 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1741 S_ISDIR(attr->la_mode),
1742 handle, BYPASS_CAPA);
1747 mdd_write_lock(env, son, MOR_TGT_CHILD);
1748 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1749 /* Drop the reference, no need to delete "."/"..",
1750 * because the object to be destroied directly. */
1751 rc2 = mdo_ref_del(env, son, handle);
1753 mdd_write_unlock(env, son);
1758 rc2 = mdo_ref_del(env, son, handle);
1760 mdd_write_unlock(env, son);
1764 mdo_destroy(env, son, handle);
1765 mdd_write_unlock(env, son);
1768 /* update lov_objid data, must be before transaction stop! */
1770 mdd_lov_objid_update(mdd, lmm);
1772 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1775 rc = mdd_changelog_ns_store(env, mdd,
1776 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1777 S_ISREG(attr->la_mode) ? CL_CREATE :
1778 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1779 0, son, mdd_pobj, lname, handle);
1781 mdd_trans_stop(env, mdd, rc, handle);
1783 /* finish lov_create stuff, free all temporary data */
1784 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1786 /* The child object shouldn't be cached anymore */
1788 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
1789 &child->mo_lu.lo_header->loh_flags);
1794 * Get locks on parents in proper order
1795 * RETURN: < 0 - error, rename_order if successful
1803 static int mdd_rename_order(const struct lu_env *env,
1804 struct mdd_device *mdd,
1805 struct mdd_object *src_pobj,
1806 struct mdd_object *tgt_pobj)
1808 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1812 if (src_pobj == tgt_pobj)
1813 RETURN(MDD_RN_SAME);
1815 /* compared the parent child relationship of src_p&tgt_p */
1816 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1818 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1821 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1834 /* has not mdd_write{read}_lock on any obj yet. */
1835 static int mdd_rename_sanity_check(const struct lu_env *env,
1836 struct mdd_object *src_pobj,
1837 struct mdd_object *tgt_pobj,
1838 struct mdd_object *sobj,
1839 struct mdd_object *tobj,
1840 struct lu_attr *so_attr,
1841 struct lu_attr *tg_attr)
1846 /* XXX: when get here, sobj must NOT be NULL,
1847 * the other case has been processed in cml_rename
1848 * before mdd_rename and enable MDS_PERM_BYPASS. */
1851 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
1855 /* XXX: when get here, "tobj == NULL" means tobj must
1856 * NOT exist (neither on remote MDS, such case has been
1857 * processed in cml_rename before mdd_rename and enable
1859 * So check may_create, but not check may_unlink. */
1861 rc = mdd_may_create(env, tgt_pobj, NULL,
1862 (src_pobj != tgt_pobj), 0);
1864 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
1865 (src_pobj != tgt_pobj), 1);
1867 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1868 S_ISDIR(so_attr->la_mode))
1869 rc = __mdd_may_link(env, tgt_pobj);
1874 static int mdd_declare_rename(const struct lu_env *env,
1875 struct mdd_device *mdd,
1876 struct mdd_object *mdd_spobj,
1877 struct mdd_object *mdd_tpobj,
1878 struct mdd_object *mdd_sobj,
1879 struct mdd_object *mdd_tobj,
1880 const struct lu_name *tname,
1881 const struct lu_name *sname,
1883 struct thandle *handle)
1891 /* name from source dir */
1892 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
1896 /* .. from source child */
1897 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
1898 /* source child can be directory,
1899 * counted by source dir's nlink */
1900 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
1904 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
1908 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
1913 /* new target child can be directory,
1914 * counted by target dir's nlink */
1915 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
1921 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
1925 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
1928 mdd_declare_links_add(env, mdd_sobj, handle);
1932 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
1937 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
1938 tname->ln_name, handle);
1942 /* name from target dir (old name), we declare it unconditionally
1943 * as mdd_rename() calls delete unconditionally as well. so just
1944 * to balance declarations vs calls to change ... */
1945 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
1949 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
1950 /* delete target child in target parent directory */
1951 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
1955 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
1956 /* target child can be directory,
1957 * delete "." reference in target child directory */
1958 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
1962 /* delete ".." reference in target parent directory */
1963 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
1968 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
1972 mdd_declare_links_add(env, mdd_tobj, handle);
1976 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
1981 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
1988 /* src object can be remote that is why we use only fid and type of object */
1989 static int mdd_rename(const struct lu_env *env,
1990 struct md_object *src_pobj, struct md_object *tgt_pobj,
1991 const struct lu_fid *lf, const struct lu_name *lsname,
1992 struct md_object *tobj, const struct lu_name *ltname,
1995 const char *sname = lsname->ln_name;
1996 const char *tname = ltname->ln_name;
1997 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1998 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
1999 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2000 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2001 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2002 struct mdd_device *mdd = mdo2mdd(src_pobj);
2003 struct mdd_object *mdd_sobj = NULL; /* source object */
2004 struct mdd_object *mdd_tobj = NULL;
2005 struct dynlock_handle *sdlh, *tdlh;
2006 struct thandle *handle;
2007 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2008 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2010 unsigned cl_flags = 0;
2015 mdd_tobj = md2mdd_obj(tobj);
2017 mdd_sobj = mdd_object_find(env, mdd, lf);
2019 handle = mdd_trans_create(env, mdd);
2021 GOTO(out_pending, rc = PTR_ERR(handle));
2023 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2024 mdd_tobj, lsname, ltname, ma, handle);
2028 rc = mdd_trans_start(env, mdd, handle);
2032 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2033 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2035 GOTO(cleanup_unlocked, rc);
2037 /* Get locks in determined order */
2038 if (rc == MDD_RN_SAME) {
2039 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2040 sname, MOR_SRC_PARENT);
2041 /* check hashes to determine do we need one lock or two */
2042 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2043 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2047 } else if (rc == MDD_RN_SRCTGT) {
2048 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2049 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2051 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2052 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2054 if (sdlh == NULL || tdlh == NULL)
2055 GOTO(cleanup, rc = -ENOMEM);
2057 rc = mdd_la_get(env, mdd_sobj, so_attr,
2058 mdd_object_capa(env, mdd_sobj));
2063 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2064 mdd_object_capa(env, mdd_tobj));
2069 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2070 mdd_tobj, so_attr, tg_attr);
2074 is_dir = S_ISDIR(so_attr->la_mode);
2076 /* Remove source name from source directory */
2077 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2078 mdd_object_capa(env, mdd_spobj));
2082 /* "mv dir1 dir2" needs "dir1/.." link update */
2083 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2084 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2085 mdd_object_capa(env, mdd_sobj));
2087 GOTO(fixup_spobj2, rc);
2089 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2090 handle, mdd_object_capa(env, mdd_sobj));
2092 GOTO(fixup_spobj, rc);
2095 /* Remove target name from target directory
2096 * Here tobj can be remote one, so we do index_delete unconditionally
2097 * and -ENOENT is allowed.
2099 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2100 mdd_object_capa(env, mdd_tpobj));
2103 /* tname might been renamed to something else */
2104 GOTO(fixup_spobj, rc);
2107 GOTO(fixup_spobj, rc);
2110 /* Insert new fid with target name into target dir */
2111 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2112 mdd_object_capa(env, mdd_tpobj));
2114 GOTO(fixup_tpobj, rc);
2116 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2117 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2119 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2121 la->la_valid = LA_CTIME;
2122 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2124 GOTO(fixup_tpobj, rc);
2127 /* Remove old target object
2128 * For tobj is remote case cmm layer has processed
2129 * and set tobj to NULL then. So when tobj is NOT NULL,
2130 * it must be local one.
2132 if (tobj && mdd_object_exists(mdd_tobj)) {
2133 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2134 if (mdd_is_dead_obj(mdd_tobj)) {
2135 mdd_write_unlock(env, mdd_tobj);
2136 /* shld not be dead, something is wrong */
2137 CERROR("tobj is dead, something is wrong\n");
2141 mdo_ref_del(env, mdd_tobj, handle);
2143 /* Remove dot reference. */
2144 if (S_ISDIR(tg_attr->la_mode))
2145 mdo_ref_del(env, mdd_tobj, handle);
2147 /* fetch updated nlink */
2148 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2149 mdd_object_capa(env, mdd_tobj));
2151 GOTO(fixup_tpobj, rc);
2153 la->la_valid = LA_CTIME;
2154 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2156 GOTO(fixup_tpobj, rc);
2158 /* XXX: this transfer to ma will be removed with LOD/OSP */
2159 ma->ma_attr = *tg_attr;
2160 ma->ma_valid |= MA_INODE;
2161 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2162 mdd_write_unlock(env, mdd_tobj);
2164 GOTO(fixup_tpobj, rc);
2166 /* fetch updated nlink */
2167 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2168 mdd_object_capa(env, mdd_tobj));
2170 GOTO(fixup_tpobj, rc);
2171 /* XXX: this transfer to ma will be removed with LOD/OSP */
2172 ma->ma_attr = *tg_attr;
2173 ma->ma_valid |= MA_INODE;
2175 if (so_attr->la_nlink == 0)
2176 cl_flags |= CLF_RENAME_LAST;
2179 la->la_valid = LA_CTIME | LA_MTIME;
2180 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2182 GOTO(fixup_tpobj, rc);
2184 if (mdd_spobj != mdd_tpobj) {
2185 la->la_valid = LA_CTIME | LA_MTIME;
2186 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2190 if (rc == 0 && mdd_sobj) {
2191 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2192 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2193 mdo2fid(mdd_tpobj), ltname, handle);
2195 /* Old files might not have EA entry */
2196 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2198 mdd_write_unlock(env, mdd_sobj);
2199 /* We don't fail the transaction if the link ea can't be
2200 updated -- fid2path will use alternate lookup method. */
2208 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2211 CWARN("tp obj fix error %d\n",rc2);
2213 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2214 !mdd_is_dead_obj(mdd_tobj)) {
2215 rc2 = __mdd_index_insert(env, mdd_tpobj,
2216 mdo2fid(mdd_tobj), tname,
2221 CWARN("tp obj fix error %d\n",rc2);
2226 if (rc && is_dir && mdd_sobj) {
2227 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2231 CWARN("sp obj dotdot delete error %d\n",rc2);
2234 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2235 dotdot, handle, BYPASS_CAPA);
2237 CWARN("sp obj dotdot insert error %d\n",rc2);
2242 rc2 = __mdd_index_insert(env, mdd_spobj,
2243 lf, sname, is_dir, handle, BYPASS_CAPA);
2245 CWARN("sp obj fix error %d\n",rc2);
2248 if (likely(tdlh) && sdlh != tdlh)
2249 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2251 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2254 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2255 mdd_tobj, tpobj_fid, lf,
2256 spobj_fid, ltname, lsname,
2260 mdd_trans_stop(env, mdd, rc, handle);
2262 mdd_object_put(env, mdd_sobj);
2267 /** enable/disable storing of hardlink info */
2268 int mdd_linkea_enable = 1;
2269 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2270 "record hardlink info in EAs");
2272 /** Read the link EA into a temp buffer.
2273 * Uses the name_buf since it is generally large.
2274 * \retval IS_ERR err
2275 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2277 struct lu_buf *mdd_links_get(const struct lu_env *env,
2278 struct mdd_object *mdd_obj)
2281 struct lustre_capa *capa;
2282 struct link_ea_header *leh;
2285 /* First try a small buf */
2286 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2287 if (buf->lb_buf == NULL)
2288 return ERR_PTR(-ENOMEM);
2290 if (!mdd_object_exists(mdd_obj))
2291 return ERR_PTR(-ENODATA);
2293 capa = mdd_object_capa(env, mdd_obj);
2294 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2295 if (rc == -ERANGE) {
2296 /* Buf was too small, figure out what we need. */
2298 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2301 buf = mdd_buf_alloc(env, rc);
2302 if (buf->lb_buf == NULL)
2303 return ERR_PTR(-ENOMEM);
2304 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2310 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2311 leh->leh_magic = LINK_EA_MAGIC;
2312 leh->leh_reccount = __swab32(leh->leh_reccount);
2313 leh->leh_len = __swab64(leh->leh_len);
2314 /* entries are swabbed by mdd_lee_unpack */
2316 if (leh->leh_magic != LINK_EA_MAGIC)
2317 return ERR_PTR(-EINVAL);
2318 if (leh->leh_reccount == 0)
2319 return ERR_PTR(-ENODATA);
2324 /** Pack a link_ea_entry.
2325 * All elements are stored as chars to avoid alignment issues.
2326 * Numbers are always big-endian
2327 * \retval record length
2329 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2330 const struct lu_fid *pfid)
2332 struct lu_fid tmpfid;
2335 fid_cpu_to_be(&tmpfid, pfid);
2336 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2337 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2338 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2340 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2341 lee->lee_reclen[1] = reclen & 0xff;
2345 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2346 struct lu_name *lname, struct lu_fid *pfid)
2348 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2349 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2350 fid_be_to_cpu(pfid, pfid);
2351 lname->ln_name = lee->lee_name;
2352 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2355 /** Add a record to the end of link ea buf */
2356 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2357 const struct lu_fid *pfid,
2358 const struct lu_name *lname)
2360 struct link_ea_header *leh;
2361 struct link_ea_entry *lee;
2364 if (lname == NULL || pfid == NULL)
2367 /* Make sure our buf is big enough for the new one */
2369 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2370 if (leh->leh_len + reclen > buf->lb_len) {
2371 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2376 lee = buf->lb_buf + leh->leh_len;
2377 reclen = mdd_lee_pack(lee, lname, pfid);
2378 leh->leh_len += reclen;
2379 leh->leh_reccount++;
2383 static int mdd_declare_links_add(const struct lu_env *env,
2384 struct mdd_object *mdd_obj,
2385 struct thandle *handle)
2389 /* XXX: max size? */
2390 rc = mdo_declare_xattr_set(env, mdd_obj,
2391 mdd_buf_get_const(env, NULL, 4096),
2392 XATTR_NAME_LINK, 0, handle);
2397 /* For pathologic linkers, we don't want to spend lots of time scanning the
2398 * link ea. Limit ourseleves to something reasonable; links not in the EA
2399 * can be looked up via (slower) parent lookup.
2401 #define LINKEA_MAX_COUNT 128
2403 static int mdd_links_add(const struct lu_env *env,
2404 struct mdd_object *mdd_obj,
2405 const struct lu_fid *pfid,
2406 const struct lu_name *lname,
2407 struct thandle *handle, int first)
2410 struct link_ea_header *leh;
2414 if (!mdd_linkea_enable)
2417 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2420 if (rc != -ENODATA) {
2421 CERROR("link_ea read failed %d "DFID"\n", rc,
2422 PFID(mdd_object_fid(mdd_obj)));
2425 /* empty EA; start one */
2426 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2427 if (buf->lb_buf == NULL)
2430 leh->leh_magic = LINK_EA_MAGIC;
2431 leh->leh_len = sizeof(struct link_ea_header);
2432 leh->leh_reccount = 0;
2436 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2439 rc = __mdd_links_add(env, buf, pfid, lname);
2444 rc = mdo_xattr_set(env, mdd_obj,
2445 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2446 XATTR_NAME_LINK, 0, handle,
2447 mdd_object_capa(env, mdd_obj));
2450 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2451 PFID(mdd_object_fid(mdd_obj)));
2453 CERROR("link_ea add failed %d "DFID"\n", rc,
2454 PFID(mdd_object_fid(mdd_obj)));
2457 if (buf->lb_len > OBD_ALLOC_BIG)
2458 /* if we vmalloced a large buffer drop it */
2464 static int mdd_links_rename(const struct lu_env *env,
2465 struct mdd_object *mdd_obj,
2466 const struct lu_fid *oldpfid,
2467 const struct lu_name *oldlname,
2468 const struct lu_fid *newpfid,
2469 const struct lu_name *newlname,
2470 struct thandle *handle)
2473 struct link_ea_header *leh;
2474 struct link_ea_entry *lee;
2475 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2476 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2482 if (!mdd_linkea_enable)
2485 if (mdd_obj->mod_flags & DEAD_OBJ)
2486 /* No more links, don't bother */
2489 buf = mdd_links_get(env, mdd_obj);
2493 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2494 rc, PFID(mdd_object_fid(mdd_obj)));
2496 CERROR("link_ea read failed %d "DFID"\n",
2497 rc, PFID(mdd_object_fid(mdd_obj)));
2501 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2503 /* Find the old record */
2504 for(count = 0; count < leh->leh_reccount; count++) {
2505 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2506 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2507 lu_fid_eq(tmpfid, oldpfid) &&
2508 (strncmp(tmpname->ln_name, oldlname->ln_name,
2509 tmpname->ln_namelen) == 0))
2511 lee = (struct link_ea_entry *)((char *)lee + reclen);
2513 if ((count + 1) > leh->leh_reccount) {
2514 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2515 oldlname->ln_namelen, oldlname->ln_name);
2516 GOTO(out, rc = -ENOENT);
2519 /* Remove the old record */
2520 leh->leh_reccount--;
2521 leh->leh_len -= reclen;
2522 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2525 /* If renaming, add the new record */
2526 if (newpfid != NULL) {
2527 /* if the add fails, we still delete the out-of-date old link */
2528 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2532 rc = mdo_xattr_set(env, mdd_obj,
2533 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2534 XATTR_NAME_LINK, 0, handle,
2535 mdd_object_capa(env, mdd_obj));
2541 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2542 oldlname->ln_namelen, oldlname->ln_name, rc,
2543 PFID(mdd_object_fid(mdd_obj)));
2545 if (buf->lb_len > OBD_ALLOC_BIG)
2546 /* if we vmalloced a large buffer drop it */
2552 const struct md_dir_operations mdd_dir_ops = {
2553 .mdo_is_subdir = mdd_is_subdir,
2554 .mdo_lookup = mdd_lookup,
2555 .mdo_create = mdd_create,
2556 .mdo_rename = mdd_rename,
2557 .mdo_link = mdd_link,
2558 .mdo_unlink = mdd_unlink,
2559 .mdo_create_data = mdd_create_data,