1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mdd) routines
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
33 #include <linux/module.h>
34 #include <linux/jbd.h>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
41 #include <linux/ldiskfs_fs.h>
42 #include <lustre_mds.h>
43 #include <lustre/lustre_idl.h>
44 #include <lustre_fid.h>
46 #include "mdd_internal.h"
48 static const char dot[] = ".";
49 static const char dotdot[] = "..";
51 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
52 const char *name, struct lu_fid* fid, int mask);
54 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
55 const char *name, struct lu_fid* fid, int mask)
57 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
58 struct dynlock_handle *dlh;
61 dlh = mdd_pdo_read_lock(env, mdd_obj, name);
64 rc = __mdd_lookup(env, pobj, name, fid, mask);
65 mdd_pdo_read_unlock(env, mdd_obj, dlh);
70 static int mdd_lookup(const struct lu_env *env,
71 struct md_object *pobj, const char *name,
76 rc = __mdd_lookup_locked(env, pobj, name, fid, MAY_EXEC);
81 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
84 return __mdd_lookup_locked(env, &obj->mod_obj, dotdot, fid, 0);
88 * For root fid use special function, whcih does not compare version component
89 * of fid. Vresion component is different for root fids on all MDTs.
91 static int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
93 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
94 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
98 * return 1: if lf is the fid of the ancestor of p1;
101 * return -EREMOTE: if remote object is found, in this
102 * case fid of remote object is saved to @pf;
104 * otherwise: values < 0, errors.
106 static int mdd_is_parent(const struct lu_env *env,
107 struct mdd_device *mdd,
108 struct mdd_object *p1,
109 const struct lu_fid *lf,
112 struct mdd_object *parent = NULL;
117 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
118 pfid = &mdd_env_info(env)->mti_fid;
120 /* Check for root first. */
121 if (mdd_is_root(mdd, mdo2fid(p1)))
125 rc = mdd_parent_fid(env, p1, pfid);
128 if (mdd_is_root(mdd, pfid))
130 if (lu_fid_eq(pfid, lf))
133 mdd_object_put(env, parent);
134 parent = mdd_object_find(env, mdd, pfid);
136 /* cross-ref parent */
137 if (parent == NULL) {
140 GOTO(out, rc = -EREMOTE);
141 } else if (IS_ERR(parent))
142 GOTO(out, rc = PTR_ERR(parent));
147 if (parent && !IS_ERR(parent))
148 mdd_object_put(env, parent);
153 * No permission check is needed.
155 * returns 1: if fid is ancestor of @mo;
156 * returns 0: if fid is not a ancestor of @mo;
158 * returns EREMOTE if remote object is found, fid of remote object is saved to
161 * returns < 0: if error
163 static int mdd_is_subdir(const struct lu_env *env,
164 struct md_object *mo, const struct lu_fid *fid,
167 struct mdd_device *mdd = mdo2mdd(mo);
171 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
174 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
178 } else if (rc == 1) {
179 /* found @fid is parent */
186 /* Check whether it may create the cobj under the pobj */
187 static int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
188 struct mdd_object *cobj, int need_check)
193 if (cobj && lu_object_exists(&cobj->mod_obj.mo_lu))
196 if (mdd_is_dead_obj(pobj))
200 rc = mdd_permission_internal_locked(env, pobj,
201 (MAY_WRITE | MAY_EXEC));
207 * It's inline, so penalty for filesystems that don't use sticky bit is
210 static inline int mdd_is_sticky(const struct lu_env *env,
211 struct mdd_object *pobj,
212 struct mdd_object *cobj)
214 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
215 struct md_ucred *uc = md_ucred(env);
218 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
221 } else if (tmp_la->la_uid == uc->mu_fsuid) {
224 mdd_read_lock(env, pobj);
225 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
226 mdd_read_unlock(env, pobj);
229 else if (!(tmp_la->la_mode & S_ISVTX))
231 else if (tmp_la->la_uid == uc->mu_fsuid)
234 return !mdd_capable(uc, CAP_FOWNER);
238 /* Check whether it may delete the cobj under the pobj. */
239 static int mdd_may_delete(const struct lu_env *env,
240 struct mdd_object *pobj,
241 struct mdd_object *cobj,
242 int is_dir, int need_check)
244 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
250 if (!lu_object_exists(&cobj->mod_obj.mo_lu))
253 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
257 if (!S_ISDIR(mdd_object_type(cobj)))
260 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
263 } else if (S_ISDIR(mdd_object_type(cobj))) {
268 if (mdd_is_dead_obj(pobj))
271 if (mdd_is_sticky(env, pobj, cobj))
275 rc = mdd_permission_internal_locked(env, pobj,
282 int mdd_link_sanity_check(const struct lu_env *env, struct mdd_object *tgt_obj,
283 struct mdd_object *src_obj)
288 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
291 if (S_ISDIR(mdd_object_type(src_obj)))
294 LASSERT(src_obj != tgt_obj);
296 rc = mdd_may_create(env, tgt_obj, NULL, 1);
304 const struct dt_rec *__mdd_fid_rec(const struct lu_env *env,
305 const struct lu_fid *fid)
307 struct mdd_thread_info *info = mdd_env_info(env);
309 fid_cpu_to_be(&info->mti_fid2, fid);
310 return (const struct dt_rec *)&info->mti_fid2;
314 /* insert new index, add reference if isdir, update times */
315 static int __mdd_index_insert(const struct lu_env *env,
316 struct mdd_object *pobj, const struct lu_fid *lf,
317 const char *name, int isdir, struct thandle *th,
318 struct lustre_capa *capa)
320 struct dt_object *next = mdd_object_child(pobj);
321 struct timeval start;
325 mdd_lproc_time_start(mdo2mdd(&pobj->mod_obj), &start,
326 LPROC_MDD_INDEX_INSERT);
328 struct lu_attr *la = &mdd_env_info(env)->mti_la;
331 if (dt_try_as_dir(env, next))
332 rc = next->do_index_ops->dio_insert(env, next,
333 __mdd_fid_rec(env, lf),
334 (const struct dt_key *)name,
341 mdd_write_lock(env, pobj);
342 mdd_ref_add_internal(env, pobj, th);
343 mdd_write_unlock(env, pobj);
346 la->la_valid = LA_MTIME|LA_CTIME;
347 la->la_atime = ma->ma_attr.la_atime;
348 la->la_ctime = ma->ma_attr.la_ctime;
349 rc = mdd_attr_set_internal(env, mdd_obj, la, handle, 0);
352 mdd_lproc_time_end(mdo2mdd(&pobj->mod_obj), &start,
353 LPROC_MDD_INDEX_INSERT);
357 static int __mdd_index_delete(const struct lu_env *env,
358 struct mdd_object *pobj, const char *name,
359 int is_dir, struct thandle *handle,
360 struct lustre_capa *capa)
362 struct dt_object *next = mdd_object_child(pobj);
363 struct timeval start;
367 mdd_lproc_time_start(mdo2mdd(&pobj->mod_obj), &start,
368 LPROC_MDD_INDEX_DELETE);
369 if (dt_try_as_dir(env, next)) {
370 rc = next->do_index_ops->dio_delete(env, next,
371 (struct dt_key *)name,
373 if (rc == 0 && is_dir) {
374 mdd_write_lock(env, pobj);
375 mdd_ref_del_internal(env, pobj, handle);
376 mdd_write_unlock(env, pobj);
380 mdd_lproc_time_end(mdo2mdd(&pobj->mod_obj), &start,
381 LPROC_MDD_INDEX_DELETE);
385 static int __mdd_index_insert_only(const struct lu_env *env,
386 struct mdd_object *pobj,
387 const struct lu_fid *lf,
388 const char *name, struct thandle *th,
389 struct lustre_capa *capa)
391 struct dt_object *next = mdd_object_child(pobj);
395 if (dt_try_as_dir(env, next)) {
396 rc = next->do_index_ops->dio_insert(env, next,
397 __mdd_fid_rec(env, lf),
398 (const struct dt_key *)name,
406 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
407 struct md_object *src_obj, const char *name,
410 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
411 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
412 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
413 struct mdd_device *mdd = mdo2mdd(src_obj);
414 struct dynlock_handle *dlh;
415 struct thandle *handle;
419 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
420 handle = mdd_trans_start(env, mdd);
422 RETURN(PTR_ERR(handle));
424 dlh = mdd_pdo_write_lock(env, mdd_tobj, name);
426 GOTO(out_trans, rc = -ENOMEM);
427 mdd_write_lock(env, mdd_sobj);
429 rc = mdd_link_sanity_check(env, mdd_tobj, mdd_sobj);
431 GOTO(out_unlock, rc);
433 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
435 mdd_object_capa(env, mdd_tobj));
437 GOTO(out_unlock, rc);
439 mdd_ref_add_internal(env, mdd_sobj, handle);
441 *la_copy = ma->ma_attr;
442 la_copy->la_valid = LA_CTIME;
443 rc = mdd_attr_set_internal(env, mdd_sobj, la_copy, handle, 0);
445 GOTO(out_unlock, rc);
447 la_copy->la_valid = LA_CTIME | LA_MTIME;
448 rc = mdd_attr_set_internal_locked(env, mdd_tobj, la_copy, handle, 0);
451 mdd_write_unlock(env, mdd_sobj);
452 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
454 mdd_trans_stop(env, mdd, rc, handle);
458 /* caller should take a lock before calling */
459 int mdd_finish_unlink(const struct lu_env *env,
460 struct mdd_object *obj, struct md_attr *ma,
466 rc = mdd_iattr_get(env, obj, ma);
467 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
468 /* add new orphan and the object
469 * will be deleted during the object_put() */
470 if (__mdd_orphan_add(env, obj, th) == 0)
471 obj->mod_flags |= ORPHAN_OBJ;
473 if (obj->mod_count == 0)
474 rc = mdd_object_kill(env, obj, ma);
476 /* clear MA_LOV | MA_COOKIE, if we do not
477 * unlink it in case we get it somewhere */
478 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
480 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
486 * Check that @dir contains no entries except (possibly) dot and dotdot.
491 * -ENOTEMPTY not empty
495 static int mdd_dir_is_empty(const struct lu_env *env,
496 struct mdd_object *dir)
499 struct dt_object *obj;
500 struct dt_it_ops *iops;
504 obj = mdd_object_child(dir);
505 iops = &obj->do_index_ops->dio_it;
506 it = iops->init(env, obj, 0, BYPASS_CAPA);
508 result = iops->get(env, it, (const void *)"");
511 for (result = 0, i = 0; result == 0 && i < 3; ++i)
512 result = iops->next(env, it);
515 else if (result == +1)
517 } else if (result == 0)
519 * Huh? Index contains no zero key?
530 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
531 struct mdd_object *cobj, struct md_attr *ma)
533 struct dt_object *dt_cobj = mdd_object_child(cobj);
537 rc = mdd_may_delete(env, pobj, cobj,
538 S_ISDIR(ma->ma_attr.la_mode), 1);
542 if (S_ISDIR(mdd_object_type(cobj))) {
543 if (dt_try_as_dir(env, dt_cobj))
544 rc = mdd_dir_is_empty(env, cobj);
552 static int mdd_unlink(const struct lu_env *env,
553 struct md_object *pobj, struct md_object *cobj,
554 const char *name, struct md_attr *ma)
556 struct mdd_device *mdd = mdo2mdd(pobj);
557 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
558 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
559 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
560 struct thandle *handle;
561 struct dynlock_handle *dlh;
565 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
569 handle = mdd_trans_start(env, mdd);
571 RETURN(PTR_ERR(handle));
573 dlh = mdd_pdo_write_lock(env, mdd_pobj, name);
575 GOTO(out_trans, rc = -ENOMEM);
576 mdd_write_lock(env, mdd_cobj);
578 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
582 is_dir = S_ISDIR(lu_object_attr(&cobj->mo_lu));
583 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
584 mdd_object_capa(env, mdd_pobj));
588 mdd_ref_del_internal(env, mdd_cobj, handle);
589 *la_copy = ma->ma_attr;
592 mdd_ref_del_internal(env, mdd_cobj, handle);
594 la_copy->la_valid = LA_CTIME;
595 rc = mdd_attr_set_internal(env, mdd_cobj, la_copy, handle, 0);
600 la_copy->la_valid = LA_CTIME | LA_MTIME;
601 rc = mdd_attr_set_internal_locked(env, mdd_pobj, la_copy, handle, 0);
605 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
608 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
609 strlen("unlinked"), "unlinked", 0,
612 mdd_write_unlock(env, mdd_cobj);
613 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
615 mdd_trans_stop(env, mdd, rc, handle);
620 * Partial operation. Be aware, this is called with write lock taken, so we use
621 * locksless version of __mdd_lookup() here.
623 static int mdd_ni_sanity_check(const struct lu_env *env,
624 struct md_object *pobj,
626 const struct lu_fid *fid)
628 struct mdd_object *obj = md2mdd_obj(pobj);
635 if (mdd_is_dead_obj(obj))
638 /* The exist of the name will be checked in _index_insert. */
640 rc = __mdd_lookup(env, pobj, name, fid, MAY_WRITE | MAY_EXEC);
642 RETURN(rc ? : -EEXIST);
646 RETURN(mdd_permission_internal_locked(env, obj,
647 MAY_WRITE | MAY_EXEC));
650 static int mdd_name_insert(const struct lu_env *env,
651 struct md_object *pobj,
652 const char *name, const struct lu_fid *fid,
655 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
656 struct mdd_device *mdd = mdo2mdd(pobj);
657 struct thandle *handle;
658 struct dynlock_handle *dlh;
662 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
663 handle = mdd_trans_start(env, mdo2mdd(pobj));
665 RETURN(PTR_ERR(handle));
667 dlh = mdd_pdo_write_lock(env, mdd_obj, name);
669 GOTO(out_trans, rc = -ENOMEM);
670 rc = mdd_ni_sanity_check(env, pobj, name, fid);
672 GOTO(out_unlock, rc);
674 rc = __mdd_index_insert(env, mdd_obj, fid, name, isdir, handle,
679 mdd_pdo_write_unlock(env, mdd_obj, dlh);
681 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
686 * Be aware, this is called with write lock taken, so we use locksless version
687 * of __mdd_lookup() here.
689 static int mdd_nr_sanity_check(const struct lu_env *env,
690 struct md_object *pobj,
693 struct mdd_object *obj = md2mdd_obj(pobj);
695 struct mdd_thread_info *info = mdd_env_info(env);
696 struct lu_fid *fid = &info->mti_fid;
702 if (mdd_is_dead_obj(obj))
705 /* The exist of the name will be checked in _index_delete. */
707 rc = __mdd_lookup(env, pobj, name, fid, MAY_WRITE | MAY_EXEC);
710 RETURN(mdd_permission_internal_locked(env, obj,
711 MAY_WRITE | MAY_EXEC));
714 static int mdd_name_remove(const struct lu_env *env,
715 struct md_object *pobj,
716 const char *name, int is_dir)
718 struct mdd_device *mdd = mdo2mdd(pobj);
719 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
720 struct thandle *handle;
721 struct dynlock_handle *dlh;
725 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
726 handle = mdd_trans_start(env, mdd);
728 RETURN(PTR_ERR(handle));
730 dlh = mdd_pdo_write_lock(env, mdd_obj, name);
732 GOTO(out_trans, rc = -ENOMEM);
733 rc = mdd_nr_sanity_check(env, pobj, name);
735 GOTO(out_unlock, rc);
737 rc = __mdd_index_delete(env, mdd_obj, name, is_dir, handle,
741 mdd_pdo_write_unlock(env, mdd_obj, dlh);
743 mdd_trans_stop(env, mdd, rc, handle);
747 static int mdd_rt_sanity_check(const struct lu_env *env,
748 struct mdd_object *tgt_pobj,
749 struct mdd_object *tobj,
750 const struct lu_fid *sfid,
751 const char *name, struct md_attr *ma)
757 if (mdd_is_dead_obj(tgt_pobj))
760 src_is_dir = S_ISDIR(ma->ma_attr.la_mode);
762 rc = mdd_may_delete(env, tgt_pobj, tobj, src_is_dir, 1);
763 if (!rc && S_ISDIR(mdd_object_type(tobj)) &&
764 mdd_dir_is_empty(env, tobj))
767 rc = mdd_may_create(env, tgt_pobj, NULL, 1);
773 static int mdd_rename_tgt(const struct lu_env *env,
774 struct md_object *pobj, struct md_object *tobj,
775 const struct lu_fid *lf, const char *name,
778 struct mdd_device *mdd = mdo2mdd(pobj);
779 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
780 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
781 struct thandle *handle;
782 struct dynlock_handle *dlh;
786 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
787 handle = mdd_trans_start(env, mdd);
789 RETURN(PTR_ERR(handle));
791 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name);
793 GOTO(out_trans, rc = -ENOMEM);
795 mdd_write_lock(env, mdd_tobj);
797 /*TODO rename sanity checking*/
798 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, lf, name, ma);
802 /* if rename_tgt is called then we should just re-insert name with
803 * correct fid, no need to dec/inc parent nlink if obj is dir */
804 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
808 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
813 if (tobj && lu_object_exists(&tobj->mo_lu))
814 mdd_ref_del_internal(env, mdd_tobj, handle);
817 mdd_write_unlock(env, mdd_tobj);
818 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
820 mdd_trans_stop(env, mdd, rc, handle);
825 * The permission has been checked when obj created,
826 * no need check again.
828 static int mdd_cd_sanity_check(const struct lu_env *env,
829 struct mdd_object *obj)
835 if (!obj || mdd_is_dead_obj(obj))
839 mdd_read_lock(env, obj);
840 rc = mdd_permission_internal(env, obj, MAY_WRITE);
841 mdd_read_unlock(env, obj);
848 static int mdd_create_data(const struct lu_env *env,
849 struct md_object *pobj, struct md_object *cobj,
850 const struct md_create_spec *spec,
853 struct mdd_device *mdd = mdo2mdd(cobj);
854 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);/* XXX maybe NULL */
855 struct mdd_object *son = md2mdd_obj(cobj);
856 struct lu_attr *attr = &ma->ma_attr;
857 struct lov_mds_md *lmm = NULL;
859 struct thandle *handle;
863 rc = mdd_cd_sanity_check(env, son);
867 if (spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE ||
868 !(spec->sp_cr_flags & FMODE_WRITE))
870 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec,
875 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
876 handle = mdd_trans_start(env, mdd);
878 RETURN(rc = PTR_ERR(handle));
881 * XXX: Setting the lov ea is not locked but setting the attr is locked?
884 /* Replay creates has objects already */
885 if (spec->u.sp_ea.no_lov_create) {
886 CDEBUG(D_INFO, "we already have lov ea\n");
887 rc = mdd_lov_set_md(env, mdd_pobj, son,
888 (struct lov_mds_md *)spec->u.sp_ea.eadata,
889 spec->u.sp_ea.eadatalen, handle, 0);
891 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
892 lmm_size, handle, 0);
895 rc = mdd_attr_get_internal_locked(env, son, ma);
897 /* Finish mdd_lov_create() stuff. */
898 mdd_lov_create_finish(env, mdd, rc);
899 mdd_trans_stop(env, mdd, rc, handle);
901 OBD_FREE(lmm, lmm_size);
906 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
907 const char *name, struct lu_fid* fid, int mask)
909 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
910 struct dt_object *dir = mdd_object_child(mdd_obj);
911 struct dt_rec *rec = (struct dt_rec *)fid;
912 const struct dt_key *key = (const struct dt_key *)name;
913 struct timeval start;
917 mdd_lproc_time_start(mdo2mdd(pobj), &start, LPROC_MDD_LOOKUP);
918 if (mdd_is_dead_obj(mdd_obj))
921 rc = lu_object_exists(mdd2lu_obj(mdd_obj));
925 CERROR("Object "DFID" locates on remote server\n",
926 PFID(mdo2fid(mdd_obj)));
931 if (mask == MAY_EXEC)
932 rc = mdd_exec_permission_lite(env, mdd_obj);
935 rc = mdd_permission_internal_locked(env, mdd_obj, mask);
939 if (S_ISDIR(mdd_object_type(mdd_obj)) && dt_try_as_dir(env, dir)) {
940 rc = dir->do_index_ops->dio_lookup(env, dir, rec, key,
941 mdd_object_capa(env, mdd_obj));
943 fid_be_to_cpu(fid, fid);
947 mdd_lproc_time_end(mdo2mdd(pobj), &start, LPROC_MDD_LOOKUP);
951 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
952 struct mdd_object *child, struct md_attr *ma,
953 struct thandle *handle)
958 /* update attributes for child.
960 * (1) the valid bits should be converted between Lustre and Linux;
961 * (2) maybe, the child attributes should be set in OSD when creation.
964 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
968 if (S_ISDIR(ma->ma_attr.la_mode)) {
969 /* add . and .. for newly created dir */
970 mdd_ref_add_internal(env, child, handle);
971 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
972 dot, handle, BYPASS_CAPA);
974 rc = __mdd_index_insert_only(env, child, pfid,
980 rc2 = __mdd_index_delete(env, child, dot, 0,
981 handle, BYPASS_CAPA);
983 CERROR("Failure to cleanup after dotdot"
984 " creation: %d (%d)\n", rc2, rc);
986 mdd_ref_del_internal(env, child, handle);
993 static int mdd_create_sanity_check(const struct lu_env *env,
994 struct md_object *pobj,
995 const char *name, struct md_attr *ma)
997 struct mdd_thread_info *info = mdd_env_info(env);
998 struct lu_attr *la = &info->mti_la;
999 struct lu_fid *fid = &info->mti_fid;
1000 struct mdd_object *obj = md2mdd_obj(pobj);
1005 if (mdd_is_dead_obj(obj))
1009 * Check if the name already exist, though it will be checked
1010 * in _index_insert also, for avoiding rolling back if exists
1013 rc = __mdd_lookup_locked(env, pobj, name, fid,
1014 MAY_WRITE | MAY_EXEC);
1016 RETURN(rc ? : -EEXIST);
1019 mdd_read_lock(env, obj);
1020 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1021 mdd_read_unlock(env, obj);
1025 if (la->la_mode & S_ISGID) {
1026 ma->ma_attr.la_gid = la->la_gid;
1027 if (S_ISDIR(ma->ma_attr.la_mode)) {
1028 ma->ma_attr.la_mode |= S_ISGID;
1029 ma->ma_attr.la_valid |= LA_MODE;
1033 switch (ma->ma_attr.la_mode & S_IFMT) {
1051 * Create object and insert it into namespace.
1053 static int mdd_create(const struct lu_env *env,
1054 struct md_object *pobj, const char *name,
1055 struct md_object *child,
1056 struct md_create_spec *spec,
1059 struct mdd_device *mdd = mdo2mdd(pobj);
1060 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1061 struct mdd_object *son = md2mdd_obj(child);
1062 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1063 struct lu_attr *attr = &ma->ma_attr;
1064 struct lov_mds_md *lmm = NULL;
1065 struct thandle *handle;
1066 int rc, created = 0, inserted = 0, lmm_size = 0;
1067 struct dynlock_handle *dlh;
1068 struct timeval start;
1071 mdd_lproc_time_start(mdd, &start, LPROC_MDD_CREATE);
1073 * Two operations have to be performed:
1075 * - allocation of new object (->do_create()), and
1077 * - insertion into parent index (->dio_insert()).
1079 * Due to locking, operation order is not important, when both are
1080 * successful, *but* error handling cases are quite different:
1082 * - if insertion is done first, and following object creation fails,
1083 * insertion has to be rolled back, but this operation might fail
1084 * also leaving us with dangling index entry.
1086 * - if creation is done first, is has to be undone if insertion
1087 * fails, leaving us with leaked space, which is neither good, nor
1090 * It seems that creation-first is simplest solution, but it is
1091 * sub-optimal in the frequent
1096 * case, because second mkdir is bound to create object, only to
1097 * destroy it immediately.
1099 * To avoid this follow local file systems that do double lookup:
1101 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1103 * 1. create (mdd_object_create_internal())
1105 * 2. insert (__mdd_index_insert(), lookup again)
1108 /* sanity checks before big job */
1109 rc = mdd_create_sanity_check(env, pobj, name, ma);
1113 /* no RPC inside the transaction, so OST objects should be created at
1115 if (S_ISREG(attr->la_mode)) {
1116 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1122 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1123 handle = mdd_trans_start(env, mdd);
1125 RETURN(PTR_ERR(handle));
1127 dlh = mdd_pdo_write_lock(env, mdd_pobj, name);
1129 GOTO(out_trans, rc = -ENOMEM);
1132 * XXX check that link can be added to the parent in mkdir case.
1135 mdd_write_lock(env, son);
1136 rc = mdd_object_create_internal(env, son, ma, handle);
1138 mdd_write_unlock(env, son);
1144 #ifdef CONFIG_FS_POSIX_ACL
1145 mdd_read_lock(env, mdd_pobj);
1146 rc = mdd_acl_init(env, mdd_pobj, son, &ma->ma_attr.la_mode, handle);
1147 mdd_read_unlock(env, mdd_pobj);
1149 mdd_write_unlock(env, son);
1152 ma->ma_attr.la_valid |= LA_MODE;
1156 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj),
1158 mdd_write_unlock(env, son);
1161 * Object has no links, so it will be destroyed when last
1162 * reference is released. (XXX not now.)
1166 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1167 name, S_ISDIR(attr->la_mode), handle,
1168 mdd_object_capa(env, mdd_pobj));
1174 /* replay creates has objects already */
1175 if (spec->u.sp_ea.no_lov_create) {
1176 CDEBUG(D_INFO, "we already have lov ea\n");
1177 LASSERT(lmm == NULL);
1178 lmm = (struct lov_mds_md *)spec->u.sp_ea.eadata;
1179 lmm_size = spec->u.sp_ea.eadatalen;
1181 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1183 CERROR("error on stripe info copy %d \n", rc);
1186 if (lmm && lmm_size > 0) {
1187 /* set Lov here, do not get lmm again later */
1188 memcpy(ma->ma_lmm, lmm, lmm_size);
1189 ma->ma_lmm_size = lmm_size;
1190 ma->ma_valid |= MA_LOV;
1193 if (S_ISLNK(attr->la_mode)) {
1194 struct dt_object *dt = mdd_object_child(son);
1195 const char *target_name = spec->u.sp_symname;
1196 int sym_len = strlen(target_name);
1197 const struct lu_buf *buf;
1200 buf = mdd_buf_get_const(env, target_name, sym_len);
1201 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1202 mdd_object_capa(env, son));
1209 *la_copy = ma->ma_attr;
1210 la_copy->la_valid = LA_CTIME | LA_MTIME;
1211 rc = mdd_attr_set_internal_locked(env, mdd_pobj, la_copy, handle, 0);
1215 /* return attr back */
1216 rc = mdd_attr_get_internal_locked(env, son, ma);
1218 if (rc && created) {
1222 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1223 S_ISDIR(attr->la_mode),
1224 handle, BYPASS_CAPA);
1226 CERROR("error can not cleanup destroy %d\n",
1230 mdd_write_lock(env, son);
1231 mdd_ref_del_internal(env, son, handle);
1232 mdd_write_unlock(env, son);
1235 /* finish mdd_lov_create() stuff */
1236 mdd_lov_create_finish(env, mdd, rc);
1237 if (lmm && !spec->u.sp_ea.no_lov_create)
1238 OBD_FREE(lmm, lmm_size);
1239 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1241 mdd_trans_stop(env, mdd, rc, handle);
1242 mdd_lproc_time_end(mdd, &start, LPROC_MDD_CREATE);
1247 * Get locks on parents in proper order
1248 * RETURN: < 0 - error, rename_order if successful
1256 static int mdd_rename_order(const struct lu_env *env,
1257 struct mdd_device *mdd,
1258 struct mdd_object *src_pobj,
1259 struct mdd_object *tgt_pobj)
1261 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1265 if (src_pobj == tgt_pobj)
1266 RETURN(MDD_RN_SAME);
1268 /* compared the parent child relationship of src_p&tgt_p */
1269 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1271 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1274 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1287 static int mdd_rename_sanity_check(const struct lu_env *env,
1288 struct mdd_object *src_pobj,
1289 struct mdd_object *tgt_pobj,
1290 const struct lu_fid *sfid,
1292 struct mdd_object *tobj)
1297 if (mdd_is_dead_obj(src_pobj))
1300 /* The sobj maybe on the remote, check parent permission only here */
1301 rc = mdd_permission_internal_locked(env, src_pobj,
1302 MAY_WRITE | MAY_EXEC);
1307 rc = mdd_may_create(env, tgt_pobj, NULL,
1308 (src_pobj != tgt_pobj));
1310 mdd_read_lock(env, tobj);
1311 rc = mdd_may_delete(env, tgt_pobj, tobj, src_is_dir,
1312 (src_pobj != tgt_pobj));
1314 if (S_ISDIR(mdd_object_type(tobj))
1315 && mdd_dir_is_empty(env, tobj))
1317 mdd_read_unlock(env, tobj);
1322 /* src object can be remote that is why we use only fid and type of object */
1323 static int mdd_rename(const struct lu_env *env,
1324 struct md_object *src_pobj, struct md_object *tgt_pobj,
1325 const struct lu_fid *lf, const char *sname,
1326 struct md_object *tobj, const char *tname,
1329 struct mdd_device *mdd = mdo2mdd(src_pobj);
1330 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj);
1331 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1332 struct mdd_object *mdd_sobj = NULL;
1333 struct mdd_object *mdd_tobj = NULL;
1334 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1335 struct dynlock_handle *sdlh, *tdlh;
1336 struct thandle *handle;
1341 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1342 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1343 if (ma->ma_attr.la_valid & LA_FLAGS &&
1344 ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
1348 mdd_tobj = md2mdd_obj(tobj);
1350 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
1351 handle = mdd_trans_start(env, mdd);
1353 RETURN(PTR_ERR(handle));
1355 /* FIXME: Should consider tobj and sobj too in rename_lock. */
1356 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
1358 GOTO(cleanup_unlocked, rc);
1360 /* get locks in determined order */
1361 if (rc == MDD_RN_SAME) {
1362 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1363 /* check hashes to determine do we need one lock or two */
1364 if (mdd_name2hash(sname) != mdd_name2hash(tname))
1365 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1368 } else if (rc == MDD_RN_SRCTGT) {
1369 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1370 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1372 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname);
1373 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname);
1375 if (sdlh == NULL || tdlh == NULL)
1376 GOTO(cleanup, rc = -ENOMEM);
1378 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
1379 lf, is_dir, mdd_tobj);
1383 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
1384 mdd_object_capa(env, mdd_spobj));
1389 * Here tobj can be remote one, so we do index_delete unconditionally
1390 * and -ENOENT is allowed.
1392 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
1393 mdd_object_capa(env, mdd_tpobj));
1394 if (rc != 0 && rc != -ENOENT)
1397 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
1398 mdd_object_capa(env, mdd_tpobj));
1402 mdd_sobj = mdd_object_find(env, mdd, lf);
1403 *la_copy = ma->ma_attr;
1404 la_copy->la_valid = LA_CTIME;
1406 /*XXX: how to update ctime for remote sobj? */
1407 rc = mdd_attr_set_internal_locked(env, mdd_sobj, la_copy,
1412 if (tobj && lu_object_exists(&tobj->mo_lu)) {
1413 mdd_write_lock(env, mdd_tobj);
1414 mdd_ref_del_internal(env, mdd_tobj, handle);
1415 /* remove dot reference */
1417 mdd_ref_del_internal(env, mdd_tobj, handle);
1419 la_copy->la_valid = LA_CTIME;
1420 rc = mdd_attr_set_internal(env, mdd_tobj, la_copy, handle, 0);
1424 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1425 mdd_write_unlock(env, mdd_tobj);
1430 la_copy->la_valid = LA_CTIME | LA_MTIME;
1431 rc = mdd_attr_set_internal_locked(env, mdd_spobj, la_copy, handle, 0);
1435 if (mdd_spobj != mdd_tpobj) {
1436 la_copy->la_valid = LA_CTIME | LA_MTIME;
1437 rc = mdd_attr_set_internal_locked(env, mdd_tpobj, la_copy,
1442 if (likely(tdlh) && sdlh != tdlh)
1443 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
1445 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
1447 mdd_trans_stop(env, mdd, rc, handle);
1449 mdd_object_put(env, mdd_sobj);
1453 struct md_dir_operations mdd_dir_ops = {
1454 .mdo_is_subdir = mdd_is_subdir,
1455 .mdo_lookup = mdd_lookup,
1456 .mdo_create = mdd_create,
1457 .mdo_rename = mdd_rename,
1458 .mdo_link = mdd_link,
1459 .mdo_unlink = mdd_unlink,
1460 .mdo_name_insert = mdd_name_insert,
1461 .mdo_name_remove = mdd_name_remove,
1462 .mdo_rename_tgt = mdd_rename_tgt,
1463 .mdo_create_data = mdd_create_data