1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdd/mdd_dir.c
41 * Lustre Metadata Server (mdd) routines
43 * Author: Wang Di <wangdi@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
52 #ifdef HAVE_EXT4_LDISKFS
53 #include <ldiskfs/ldiskfs_jbd2.h>
55 #include <linux/jbd.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
62 #ifdef HAVE_EXT4_LDISKFS
63 #include <ldiskfs/ldiskfs.h>
65 #include <linux/ldiskfs_fs.h>
67 #include <lustre_mds.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_fid.h>
71 #include "mdd_internal.h"
73 static const char dot[] = ".";
74 static const char dotdot[] = "..";
76 static struct lu_name lname_dotdot = {
81 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
82 const struct lu_name *lname, struct lu_fid* fid,
84 static int mdd_declare_links_add(const struct lu_env *env,
85 struct mdd_object *mdd_obj,
86 struct thandle *handle);
87 static int mdd_links_add(const struct lu_env *env,
88 struct mdd_object *mdd_obj,
89 const struct lu_fid *pfid,
90 const struct lu_name *lname,
91 struct thandle *handle, int first);
92 static int mdd_links_rename(const struct lu_env *env,
93 struct mdd_object *mdd_obj,
94 const struct lu_fid *oldpfid,
95 const struct lu_name *oldlname,
96 const struct lu_fid *newpfid,
97 const struct lu_name *newlname,
98 struct thandle *handle);
101 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
102 const struct lu_name *lname, struct lu_fid* fid, int mask)
104 const char *name = lname->ln_name;
105 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
106 struct dynlock_handle *dlh;
109 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
110 if (unlikely(dlh == NULL))
112 rc = __mdd_lookup(env, pobj, lname, fid, mask);
113 mdd_pdo_read_unlock(env, mdd_obj, dlh);
118 int mdd_lookup(const struct lu_env *env,
119 struct md_object *pobj, const struct lu_name *lname,
120 struct lu_fid* fid, struct md_op_spec *spec)
124 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
128 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
131 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
135 * For root fid use special function, which does not compare version component
136 * of fid. Version component is different for root fids on all MDTs.
138 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
140 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
141 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
145 * return 1: if lf is the fid of the ancestor of p1;
148 * return -EREMOTE: if remote object is found, in this
149 * case fid of remote object is saved to @pf;
151 * otherwise: values < 0, errors.
153 static int mdd_is_parent(const struct lu_env *env,
154 struct mdd_device *mdd,
155 struct mdd_object *p1,
156 const struct lu_fid *lf,
159 struct mdd_object *parent = NULL;
164 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
165 pfid = &mdd_env_info(env)->mti_fid;
167 /* Check for root first. */
168 if (mdd_is_root(mdd, mdo2fid(p1)))
172 /* this is done recursively, bypass capa for each obj */
173 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
174 rc = mdd_parent_fid(env, p1, pfid);
177 if (mdd_is_root(mdd, pfid))
179 if (lu_fid_eq(pfid, lf))
182 mdd_object_put(env, parent);
183 parent = mdd_object_find(env, mdd, pfid);
185 /* cross-ref parent */
186 if (parent == NULL) {
189 GOTO(out, rc = -EREMOTE);
190 } else if (IS_ERR(parent))
191 GOTO(out, rc = PTR_ERR(parent));
196 if (parent && !IS_ERR(parent))
197 mdd_object_put(env, parent);
202 * No permission check is needed.
204 * returns 1: if fid is ancestor of @mo;
205 * returns 0: if fid is not a ancestor of @mo;
207 * returns EREMOTE if remote object is found, fid of remote object is saved to
210 * returns < 0: if error
212 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
213 const struct lu_fid *fid, struct lu_fid *sfid)
215 struct mdd_device *mdd = mdo2mdd(mo);
219 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
222 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
226 } else if (rc == 1) {
227 /* found @fid is parent */
235 * Check that @dir contains no entries except (possibly) dot and dotdot.
240 * -ENOTDIR not a directory object
241 * -ENOTEMPTY not empty
245 static int mdd_dir_is_empty(const struct lu_env *env,
246 struct mdd_object *dir)
249 struct dt_object *obj;
250 const struct dt_it_ops *iops;
254 obj = mdd_object_child(dir);
255 if (!dt_try_as_dir(env, obj))
258 iops = &obj->do_index_ops->dio_it;
259 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
261 result = iops->get(env, it, (const void *)"");
264 for (result = 0, i = 0; result == 0 && i < 3; ++i)
265 result = iops->next(env, it);
268 else if (result == +1)
270 } else if (result == 0)
272 * Huh? Index contains no zero key?
279 result = PTR_ERR(it);
283 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
285 struct mdd_device *m = mdd_obj2mdd_dev(obj);
286 struct lu_attr *la = &mdd_env_info(env)->mti_la;
290 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
295 * Subdir count limitation can be broken through.
297 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
298 !S_ISDIR(la->la_mode))
305 * Check whether it may create the cobj under the pobj.
308 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
309 struct mdd_object *cobj, int check_perm, int check_nlink)
314 if (cobj && mdd_object_exists(cobj))
317 if (mdd_is_dead_obj(pobj))
321 rc = mdd_permission_internal_locked(env, pobj, NULL,
322 MAY_WRITE | MAY_EXEC,
325 if (!rc && check_nlink)
326 rc = __mdd_may_link(env, pobj);
332 * Check whether can unlink from the pobj in the case of "cobj == NULL".
334 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
335 const struct md_attr *ma)
340 if (mdd_is_dead_obj(pobj))
343 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
344 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
347 rc = mdd_permission_internal_locked(env, pobj, NULL,
348 MAY_WRITE | MAY_EXEC,
353 if (mdd_is_append(pobj))
360 * pobj == NULL is remote ops case, under such case, pobj's
361 * VTX feature has been checked already, no need check again.
363 static inline int mdd_is_sticky(const struct lu_env *env,
364 struct mdd_object *pobj,
365 struct mdd_object *cobj)
367 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
368 struct md_ucred *uc = md_ucred(env);
372 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
376 if (!(tmp_la->la_mode & S_ISVTX) ||
377 (tmp_la->la_uid == uc->mu_fsuid))
381 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
385 if (tmp_la->la_uid == uc->mu_fsuid)
388 return !mdd_capable(uc, CFS_CAP_FOWNER);
392 * Check whether it may delete the cobj from the pobj.
395 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
396 struct mdd_object *cobj, struct md_attr *ma,
397 int check_perm, int check_empty)
403 if (!mdd_object_exists(cobj))
406 if (mdd_is_dead_obj(cobj))
410 if (!mdd_object_exists(pobj))
413 if (mdd_is_dead_obj(pobj))
417 rc = mdd_permission_internal_locked(env, pobj, NULL,
418 MAY_WRITE | MAY_EXEC,
424 if (mdd_is_append(pobj))
428 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
429 mdd_is_sticky(env, pobj, cobj))
432 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
435 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
436 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
439 if (S_ISDIR(ma->ma_attr.la_mode)) {
440 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
442 if (!S_ISDIR(mdd_object_type(cobj)))
445 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
447 } else if (S_ISDIR(mdd_object_type(cobj)))
450 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
451 rc = mdd_dir_is_empty(env, cobj);
458 * has mdd_write_lock on src already, but not on tgt yet
460 int mdd_link_sanity_check(const struct lu_env *env,
461 struct mdd_object *tgt_obj,
462 const struct lu_name *lname,
463 struct mdd_object *src_obj)
465 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
469 if (!mdd_object_exists(src_obj))
472 if (mdd_is_dead_obj(src_obj))
475 /* Local ops, no lookup before link, check filename length here. */
476 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
477 RETURN(-ENAMETOOLONG);
479 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
482 if (S_ISDIR(mdd_object_type(src_obj)))
485 LASSERT(src_obj != tgt_obj);
487 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
492 rc = __mdd_may_link(env, src_obj);
497 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
498 const char *name, 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 rc = next->do_index_ops->dio_delete(env, next,
507 (struct dt_key *)name,
515 static int __mdd_index_insert_only(const struct lu_env *env,
516 struct mdd_object *pobj,
517 const struct lu_fid *lf, const char *name,
518 struct thandle *handle,
519 struct lustre_capa *capa)
521 struct dt_object *next = mdd_object_child(pobj);
525 if (dt_try_as_dir(env, next)) {
526 struct md_ucred *uc = md_ucred(env);
528 rc = next->do_index_ops->dio_insert(env, next,
530 (const struct dt_key *)name,
531 handle, capa, uc->mu_cap &
532 CFS_CAP_SYS_RESOURCE_MASK);
539 /* insert named index, add reference if isdir */
540 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
541 const struct lu_fid *lf, const char *name, int is_dir,
542 struct thandle *handle, struct lustre_capa *capa)
547 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
548 if (rc == 0 && is_dir) {
549 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
550 mdo_ref_add(env, pobj, handle);
551 mdd_write_unlock(env, pobj);
556 /* delete named index, drop reference if isdir */
557 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
558 const char *name, int is_dir, struct thandle *handle,
559 struct lustre_capa *capa)
564 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
565 if (rc == 0 && is_dir) {
566 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
567 mdo_ref_del(env, pobj, handle);
568 mdd_write_unlock(env, pobj);
574 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
575 int reclen, struct thandle *handle)
579 /* XXX: this is a temporary solution to declare llog changes
580 * will be fixed in 2.3 with new llog implementation */
582 LASSERT(mdd->mdd_capa);
585 rc = dt_declare_record_write(env, mdd->mdd_capa, reclen, 0, handle);
589 /* header will be updated as well */
590 rc = dt_declare_record_write(env, mdd->mdd_capa, LLOG_CHUNK_SIZE,
595 /* also we should be able to create new plain log */
596 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
600 /* new record referencing new plain llog */
601 rc = dt_declare_record_write(env, mdd->mdd_capa,
602 sizeof(struct llog_logid_rec), 0, handle);
606 /* catalog's header will be updated as well */
607 rc = dt_declare_record_write(env, mdd->mdd_capa, LLOG_CHUNK_SIZE,
613 int mdd_declare_changelog_store(const struct lu_env *env,
614 struct mdd_device *mdd,
615 const struct lu_name *fname,
616 struct thandle *handle)
621 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
624 /* we'll be writing payload + llog header */
625 reclen = sizeof(struct llog_changelog_rec);
627 reclen += llog_data_len(fname->ln_namelen);
629 return mdd_declare_llog_record(env, mdd, reclen, handle);
632 /** Store a namespace change changelog record
633 * If this fails, we must fail the whole transaction; we don't
634 * want the change to commit without the log entry.
635 * \param target - mdd_object of change
636 * \param parent - parent dir/object
637 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
638 * \param tname - target name string
639 * \param handle - transacion handle
641 static int mdd_changelog_ns_store(const struct lu_env *env,
642 struct mdd_device *mdd,
643 enum changelog_rec_type type,
645 struct mdd_object *target,
646 struct mdd_object *parent,
647 const struct lu_fid *tf,
648 const struct lu_name *tname,
649 struct thandle *handle)
651 const struct lu_fid *tfid;
652 const struct lu_fid *tpfid = mdo2fid(parent);
653 struct llog_changelog_rec *rec;
660 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
662 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
665 LASSERT(parent != NULL);
666 LASSERT(tname != NULL);
667 LASSERT(handle != NULL);
670 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
671 buf = mdd_buf_alloc(env, reclen);
672 if (buf->lb_buf == NULL)
674 rec = (struct llog_changelog_rec *)buf->lb_buf;
676 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
677 rec->cr.cr_type = (__u32)type;
678 tfid = tf ? tf : mdo2fid(target);
679 rec->cr.cr_tfid = *tfid;
680 rec->cr.cr_pfid = *tpfid;
681 rec->cr.cr_namelen = tname->ln_namelen;
682 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
684 target->mod_cltime = cfs_time_current_64();
686 rc = mdd_changelog_llog_write(mdd, rec, handle);
688 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
689 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
696 static int mdd_declare_link(const struct lu_env *env,
697 struct mdd_device *mdd,
698 struct mdd_object *p,
699 struct mdd_object *c,
700 const struct lu_name *name,
701 struct thandle *handle)
705 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
709 rc = mdo_declare_ref_add(env, c, handle);
713 rc = mdo_declare_attr_set(env, p, NULL, handle);
717 rc = mdo_declare_attr_set(env, c, NULL, handle);
721 rc = mdd_declare_links_add(env, c, handle);
725 rc = mdd_declare_changelog_store(env, mdd, name, handle);
730 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
731 struct md_object *src_obj, const struct lu_name *lname,
734 const char *name = lname->ln_name;
735 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
736 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
737 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
738 struct mdd_device *mdd = mdo2mdd(src_obj);
739 struct dynlock_handle *dlh;
740 struct thandle *handle;
741 #ifdef HAVE_QUOTA_SUPPORT
742 struct obd_device *obd = mdd->mdd_obd_dev;
743 struct obd_export *exp = md_quota(env)->mq_exp;
744 struct mds_obd *mds = &obd->u.mds;
745 unsigned int qids[MAXQUOTAS] = { 0, 0 };
746 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
751 #ifdef HAVE_QUOTA_SUPPORT
752 if (mds->mds_quota) {
753 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
755 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
758 mdd_data_get(env, mdd_tobj, &data);
759 quota_opc = FSFILT_OP_LINK;
760 mdd_quota_wrapper(la_tmp, qids);
761 /* get block quota for parent */
762 lquota_chkquota(mds_quota_interface_ref, obd, exp,
763 qids, rec_pending, 1, NULL,
764 LQUOTA_FLAGS_BLK, data, 1);
769 handle = mdd_trans_create(env, mdd);
771 GOTO(out_pending, rc = PTR_ERR(handle));
773 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
777 rc = mdd_trans_start(env, mdd, handle);
781 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
783 GOTO(out_trans, rc = -ENOMEM);
784 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
786 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
788 GOTO(out_unlock, rc);
790 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
792 mdd_object_capa(env, mdd_tobj));
794 GOTO(out_unlock, rc);
796 mdo_ref_add(env, mdd_sobj, handle);
798 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
799 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
801 la->la_valid = LA_CTIME | LA_MTIME;
802 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
804 GOTO(out_unlock, rc);
806 la->la_valid = LA_CTIME;
807 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
809 mdd_links_add(env, mdd_sobj,
810 mdo2fid(mdd_tobj), lname, handle, 0);
815 mdd_write_unlock(env, mdd_sobj);
816 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
819 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
820 mdd_tobj, NULL, lname, handle);
822 mdd_trans_stop(env, mdd, rc, handle);
824 #ifdef HAVE_QUOTA_SUPPORT
826 lquota_pending_commit(mds_quota_interface_ref, obd,
827 qids, rec_pending, 1);
828 /* Trigger dqacq for the parent owner. If failed,
829 * the next call for lquota_chkquota will process it. */
830 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
837 int mdd_declare_finish_unlink(const struct lu_env *env,
838 struct mdd_object *obj,
840 struct thandle *handle)
844 rc = orph_declare_index_insert(env, obj, handle);
848 return mdd_declare_object_kill(env, obj, ma, handle);
851 /* caller should take a lock before calling */
852 int mdd_finish_unlink(const struct lu_env *env,
853 struct mdd_object *obj, struct md_attr *ma,
858 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
861 LASSERT(mdd_write_locked(env, obj) != 0);
863 /* read HSM flags, needed to set changelogs flags */
864 ma->ma_need = MA_HSM | MA_INODE;
865 rc = mdd_attr_get_internal(env, obj, ma);
866 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
867 obj->mod_flags |= DEAD_OBJ;
868 /* add new orphan and the object
869 * will be deleted during mdd_close() */
870 if (obj->mod_count) {
871 rc = __mdd_orphan_add(env, obj, th);
873 CDEBUG(D_HA, "Object "DFID" is inserted into "
874 "orphan list, open count = %d\n",
875 PFID(mdd_object_fid(obj)),
878 CERROR("Object "DFID" fail to be an orphan, "
879 "open count = %d, maybe cause failed "
881 PFID(mdd_object_fid(obj)),
884 rc = mdd_object_kill(env, obj, ma, th);
889 /* get the i_nlink */
890 ma->ma_need = MA_INODE;
891 rc = mdd_attr_get_internal(env, obj, ma);
894 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
901 * has mdd_write_lock on cobj already, but not on pobj yet
903 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
904 struct mdd_object *cobj, struct md_attr *ma)
909 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
914 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
915 struct mdd_object *p, struct mdd_object *c,
916 const struct lu_name *name, struct md_attr *ma,
917 struct thandle *handle)
921 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
925 rc = mdo_declare_ref_del(env, p, handle);
929 rc = mdo_declare_ref_del(env, c, handle);
933 rc = mdo_declare_ref_del(env, c, handle);
937 rc = mdo_declare_attr_set(env, p, NULL, handle);
941 rc = mdo_declare_attr_set(env, c, NULL, handle);
945 rc = mdd_declare_finish_unlink(env, c, ma, handle);
949 rc = mdd_declare_links_add(env, c, handle);
953 rc = mdd_declare_changelog_store(env, mdd, name, handle);
958 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
959 struct md_object *cobj, const struct lu_name *lname,
962 const char *name = lname->ln_name;
963 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
964 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
965 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
966 struct mdd_device *mdd = mdo2mdd(pobj);
967 struct dynlock_handle *dlh;
968 struct thandle *handle;
969 #ifdef HAVE_QUOTA_SUPPORT
970 struct obd_device *obd = mdd->mdd_obd_dev;
971 struct mds_obd *mds = &obd->u.mds;
972 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
973 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
976 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
980 if (mdd_object_exists(mdd_cobj) <= 0)
983 handle = mdd_trans_create(env, mdd);
985 RETURN(PTR_ERR(handle));
987 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
992 rc = mdd_trans_start(env, mdd, handle);
996 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
998 GOTO(out_trans, rc = -ENOMEM);
999 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1001 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1005 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1006 mdd_object_capa(env, mdd_pobj));
1010 mdo_ref_del(env, mdd_cobj, handle);
1013 mdo_ref_del(env, mdd_cobj, handle);
1015 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1016 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1018 la->la_valid = LA_CTIME | LA_MTIME;
1019 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1023 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1024 /* update ctime of an unlinked file only if it is still
1025 * opened or a link still exists */
1026 la->la_valid = LA_CTIME;
1027 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1032 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1033 #ifdef HAVE_QUOTA_SUPPORT
1034 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1035 ma->ma_attr.la_nlink == 0) {
1036 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1038 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1040 mdd_quota_wrapper(la_tmp, qpids);
1041 if (mdd_cobj->mod_count == 0) {
1042 quota_opc = FSFILT_OP_UNLINK;
1043 mdd_quota_wrapper(&ma->ma_attr, qcids);
1045 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1051 /* old files may not have link ea; ignore errors */
1052 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1053 lname, NULL, NULL, handle);
1057 mdd_write_unlock(env, mdd_cobj);
1058 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1063 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1064 if ((ma->ma_valid & MA_HSM) &&
1065 (ma->ma_hsm.mh_flags & HS_EXISTS))
1066 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1068 rc = mdd_changelog_ns_store(env, mdd,
1069 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1070 mdd_cobj, mdd_pobj, NULL, lname, handle);
1074 mdd_trans_stop(env, mdd, rc, handle);
1075 #ifdef HAVE_QUOTA_SUPPORT
1077 /* Trigger dqrel on the owner of child and parent. If failed,
1078 * the next call for lquota_chkquota will process it. */
1079 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1085 /* has not lock on pobj yet */
1086 static int mdd_ni_sanity_check(const struct lu_env *env,
1087 struct md_object *pobj,
1088 const struct md_attr *ma)
1090 struct mdd_object *obj = md2mdd_obj(pobj);
1094 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1097 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1103 * Partial operation.
1105 static int mdd_name_insert(const struct lu_env *env,
1106 struct md_object *pobj,
1107 const struct lu_name *lname,
1108 const struct lu_fid *fid,
1109 const struct md_attr *ma)
1111 const char *name = lname->ln_name;
1112 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1113 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1114 struct mdd_device *mdd = mdo2mdd(pobj);
1115 struct dynlock_handle *dlh;
1116 struct thandle *handle;
1117 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1118 #ifdef HAVE_QUOTA_SUPPORT
1119 struct md_ucred *uc = md_ucred(env);
1120 struct obd_device *obd = mdd->mdd_obd_dev;
1121 struct obd_export *exp = md_quota(env)->mq_exp;
1122 struct mds_obd *mds = &obd->u.mds;
1123 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1124 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1125 cfs_cap_t save = uc->mu_cap;
1130 /* XXX: this code won't be used ever:
1131 * DNE uses slightly different approach */
1134 #ifdef HAVE_QUOTA_SUPPORT
1135 if (mds->mds_quota) {
1136 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1137 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1139 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1142 mdd_data_get(env, mdd_obj, &data);
1143 quota_opc = FSFILT_OP_LINK;
1144 mdd_quota_wrapper(la_tmp, qids);
1145 /* get block quota for parent */
1146 lquota_chkquota(mds_quota_interface_ref, obd,
1147 exp, qids, rec_pending, 1, NULL,
1148 LQUOTA_FLAGS_BLK, data, 1);
1151 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1155 handle = mdd_trans_create(env, mdo2mdd(pobj));
1157 GOTO(out_pending, rc = PTR_ERR(handle));
1159 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1161 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1163 GOTO(out_trans, rc = -ENOMEM);
1165 rc = mdd_ni_sanity_check(env, pobj, ma);
1167 GOTO(out_unlock, rc);
1169 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1170 handle, BYPASS_CAPA);
1172 GOTO(out_unlock, rc);
1175 * For some case, no need update obj's ctime (LA_CTIME is not set),
1177 * For other cases, update obj's ctime (LA_CTIME is set),
1180 if (ma->ma_attr.la_valid & LA_CTIME) {
1181 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1182 la->la_valid = LA_CTIME | LA_MTIME;
1183 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1188 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1190 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1192 #ifdef HAVE_QUOTA_SUPPORT
1193 if (mds->mds_quota) {
1195 lquota_pending_commit(mds_quota_interface_ref,
1196 obd, qids, rec_pending, 1);
1197 /* Trigger dqacq for the parent owner. If failed,
1198 * the next call for lquota_chkquota will process it*/
1199 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1209 /* has not lock on pobj yet */
1210 static int mdd_nr_sanity_check(const struct lu_env *env,
1211 struct md_object *pobj,
1212 const struct md_attr *ma)
1214 struct mdd_object *obj = md2mdd_obj(pobj);
1218 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1221 rc = mdd_may_unlink(env, obj, ma);
1227 * Partial operation.
1229 static int mdd_name_remove(const struct lu_env *env,
1230 struct md_object *pobj,
1231 const struct lu_name *lname,
1232 const struct md_attr *ma)
1234 const char *name = lname->ln_name;
1235 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1236 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1237 struct mdd_device *mdd = mdo2mdd(pobj);
1238 struct dynlock_handle *dlh;
1239 struct thandle *handle;
1240 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1241 #ifdef HAVE_QUOTA_SUPPORT
1242 struct obd_device *obd = mdd->mdd_obd_dev;
1243 struct mds_obd *mds = &obd->u.mds;
1244 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1250 /* XXX: this code won't be used ever:
1251 * DNE uses slightly different approach */
1254 #ifdef HAVE_QUOTA_SUPPORT
1255 if (mds->mds_quota) {
1256 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1258 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1260 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1261 mdd_quota_wrapper(la_tmp, qids);
1265 handle = mdd_trans_create(env, mdd);
1267 GOTO(out_pending, rc = PTR_ERR(handle));
1269 rc = mdd_trans_start(env, mdd, handle);
1271 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1273 GOTO(out_trans, rc = -ENOMEM);
1275 rc = mdd_nr_sanity_check(env, pobj, ma);
1277 GOTO(out_unlock, rc);
1279 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1280 handle, BYPASS_CAPA);
1282 GOTO(out_unlock, rc);
1285 * For some case, no need update obj's ctime (LA_CTIME is not set),
1287 * For other cases, update obj's ctime (LA_CTIME is set),
1290 if (ma->ma_attr.la_valid & LA_CTIME) {
1291 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1292 la->la_valid = LA_CTIME | LA_MTIME;
1293 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1298 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1300 mdd_trans_stop(env, mdd, rc, handle);
1302 #ifdef HAVE_QUOTA_SUPPORT
1303 /* Trigger dqrel for the parent owner.
1304 * If failed, the next call for lquota_chkquota will process it. */
1306 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1314 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1316 static int mdd_rt_sanity_check(const struct lu_env *env,
1317 struct mdd_object *tgt_pobj,
1318 struct mdd_object *tobj,
1324 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1327 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1328 * exist. In fact, tobj must exist, otherwise the call trace will be:
1329 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1330 * When get here, tobj must be NOT NULL, the other case has been
1331 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1333 * So check may_delete, but not check nlink of tgt_pobj. */
1335 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1340 /* Partial rename op on slave MDD */
1341 static int mdd_rename_tgt(const struct lu_env *env,
1342 struct md_object *pobj, struct md_object *tobj,
1343 const struct lu_fid *lf, const struct lu_name *lname,
1346 const char *name = lname->ln_name;
1347 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1348 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1349 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1350 struct mdd_device *mdd = mdo2mdd(pobj);
1351 struct dynlock_handle *dlh;
1352 struct thandle *handle;
1353 #ifdef HAVE_QUOTA_SUPPORT
1354 struct obd_device *obd = mdd->mdd_obd_dev;
1355 struct obd_export *exp = md_quota(env)->mq_exp;
1356 struct mds_obd *mds = &obd->u.mds;
1357 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1358 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1359 int quota_copc = 0, quota_popc = 0;
1360 int rec_pending[MAXQUOTAS] = { 0, 0 };
1365 /* XXX: this code won't be used ever:
1366 * DNE uses slightly different approach */
1369 #ifdef HAVE_QUOTA_SUPPORT
1370 if (mds->mds_quota && !tobj) {
1371 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1373 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1376 mdd_data_get(env, mdd_tpobj, &data);
1377 quota_popc = FSFILT_OP_LINK;
1378 mdd_quota_wrapper(la_tmp, qpids);
1379 /* get block quota for target parent */
1380 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1381 qpids, rec_pending, 1, NULL,
1382 LQUOTA_FLAGS_BLK, data, 1);
1386 handle = mdd_trans_create(env, mdd);
1388 GOTO(out_pending, rc = PTR_ERR(handle));
1390 rc = mdd_trans_start(env, mdd, handle);
1392 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1394 GOTO(out_trans, rc = -ENOMEM);
1396 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1398 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1403 * If rename_tgt is called then we should just re-insert name with
1404 * correct fid, no need to dec/inc parent nlink if obj is dir.
1406 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1410 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1415 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1416 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1418 la->la_valid = LA_CTIME | LA_MTIME;
1419 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1424 * For tobj is remote case cmm layer has processed
1425 * and pass NULL tobj to here. So when tobj is NOT NULL,
1426 * it must be local one.
1428 if (tobj && mdd_object_exists(mdd_tobj)) {
1429 mdo_ref_del(env, mdd_tobj, handle);
1431 /* Remove dot reference. */
1432 if (S_ISDIR(ma->ma_attr.la_mode))
1433 mdo_ref_del(env, mdd_tobj, handle);
1435 la->la_valid = LA_CTIME;
1436 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1440 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1444 #ifdef HAVE_QUOTA_SUPPORT
1445 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1446 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1447 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1448 mdd_quota_wrapper(&ma->ma_attr, qcids);
1455 mdd_write_unlock(env, mdd_tobj);
1456 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1459 /* Bare EXT record with no RENAME in front of it signifies
1460 a partial slave op */
1461 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1462 mdd_tpobj, NULL, lname, handle);
1464 mdd_trans_stop(env, mdd, rc, handle);
1466 #ifdef HAVE_QUOTA_SUPPORT
1467 if (mds->mds_quota) {
1469 lquota_pending_commit(mds_quota_interface_ref, obd,
1470 qpids, rec_pending, 1);
1473 /* Trigger dqrel on the target owner of child.
1474 * If failed, the next call for lquota_chkquota
1475 * will process it. */
1476 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1484 * The permission has been checked when obj created, no need check again.
1486 static int mdd_cd_sanity_check(const struct lu_env *env,
1487 struct mdd_object *obj)
1492 if (!obj || mdd_is_dead_obj(obj))
1499 static int mdd_declare_create_data(const struct lu_env *env,
1500 struct mdd_device *mdd,
1501 struct mdd_object *obj,
1503 struct thandle *handle)
1505 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1509 buf->lb_len = lmm_size;
1510 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1515 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1520 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1521 struct md_object *cobj, const struct md_op_spec *spec,
1524 struct mdd_device *mdd = mdo2mdd(cobj);
1525 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1526 struct mdd_object *son = md2mdd_obj(cobj);
1527 struct lov_mds_md *lmm = NULL;
1529 struct thandle *handle;
1533 rc = mdd_cd_sanity_check(env, son);
1537 if (!md_should_create(spec->sp_cr_flags))
1539 lmm_size = ma->ma_lmm_size;
1541 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1545 handle = mdd_trans_create(env, mdd);
1547 GOTO(out_free, rc = PTR_ERR(handle));
1549 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1553 rc = mdd_trans_start(env, mdd, handle);
1558 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1559 * Should this be fixed?
1562 /* Replay creates has objects already */
1564 if (spec->no_create) {
1565 CDEBUG(D_INFO, "we already have lov ea\n");
1566 rc = mdd_lov_set_md(env, mdd_pobj, son,
1567 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1568 spec->u.sp_ea.eadatalen, handle, 0);
1571 /* No need mdd_lsm_sanity_check here */
1572 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1573 lmm_size, handle, 0);
1576 rc = mdd_attr_get_internal_locked(env, son, ma);
1578 /* update lov_objid data, must be before transaction stop! */
1580 mdd_lov_objid_update(mdd, lmm);
1583 mdd_trans_stop(env, mdd, rc, handle);
1585 /* Finish mdd_lov_create() stuff. */
1586 /* if no_create == 0 (not replay), we free lmm allocated by
1587 * mdd_lov_create() */
1588 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1592 /* Get fid from name and parent */
1594 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1595 const struct lu_name *lname, struct lu_fid* fid, int mask)
1597 const char *name = lname->ln_name;
1598 const struct dt_key *key = (const struct dt_key *)name;
1599 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1600 struct mdd_device *m = mdo2mdd(pobj);
1601 struct dt_object *dir = mdd_object_child(mdd_obj);
1605 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1608 rc = mdd_object_exists(mdd_obj);
1609 if (unlikely(rc == 0))
1611 else if (unlikely(rc < 0)) {
1612 CERROR("Object "DFID" locates on remote server\n",
1613 PFID(mdo2fid(mdd_obj)));
1617 /* The common filename length check. */
1618 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1619 RETURN(-ENAMETOOLONG);
1621 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1626 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1627 dt_try_as_dir(env, dir))) {
1629 rc = dir->do_index_ops->dio_lookup(env, dir,
1630 (struct dt_rec *)fid, key,
1631 mdd_object_capa(env, mdd_obj));
1642 int mdd_declare_object_initialize(const struct lu_env *env,
1643 struct mdd_object *child,
1645 struct thandle *handle)
1649 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1650 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1651 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1654 rc = mdo_declare_ref_add(env, child, handle);
1657 mdd_declare_links_add(env, child, handle);
1662 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1663 const struct lu_name *lname, struct mdd_object *child,
1664 struct md_attr *ma, struct thandle *handle,
1665 const struct md_op_spec *spec)
1671 * Update attributes for child.
1674 * (1) the valid bits should be converted between Lustre and Linux;
1675 * (2) maybe, the child attributes should be set in OSD when creation.
1678 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1682 if (S_ISDIR(ma->ma_attr.la_mode)) {
1683 /* Add "." and ".." for newly created dir */
1684 mdo_ref_add(env, child, handle);
1685 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1686 dot, handle, BYPASS_CAPA);
1688 rc = __mdd_index_insert_only(env, child, pfid,
1692 mdo_ref_del(env, child, handle);
1695 mdd_links_add(env, child, pfid, lname, handle, 1);
1700 /* has not lock on pobj yet */
1701 static int mdd_create_sanity_check(const struct lu_env *env,
1702 struct md_object *pobj,
1703 const struct lu_name *lname,
1705 struct md_op_spec *spec)
1707 struct mdd_thread_info *info = mdd_env_info(env);
1708 struct lu_attr *la = &info->mti_la;
1709 struct lu_fid *fid = &info->mti_fid;
1710 struct mdd_object *obj = md2mdd_obj(pobj);
1711 struct mdd_device *m = mdo2mdd(pobj);
1712 int lookup = spec->sp_cr_lookup;
1717 if (mdd_is_dead_obj(obj))
1721 * In some cases this lookup is not needed - we know before if name
1722 * exists or not because MDT performs lookup for it.
1723 * name length check is done in lookup.
1727 * Check if the name already exist, though it will be checked in
1728 * _index_insert also, for avoiding rolling back if exists
1731 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1732 MAY_WRITE | MAY_EXEC);
1734 RETURN(rc ? : -EEXIST);
1737 * Check WRITE permission for the parent.
1738 * EXEC permission have been checked
1739 * when lookup before create already.
1741 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1748 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1752 if (la->la_mode & S_ISGID) {
1753 ma->ma_attr.la_gid = la->la_gid;
1754 if (S_ISDIR(ma->ma_attr.la_mode)) {
1755 ma->ma_attr.la_mode |= S_ISGID;
1756 ma->ma_attr.la_valid |= LA_MODE;
1760 switch (ma->ma_attr.la_mode & S_IFMT) {
1762 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1764 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1765 RETURN(-ENAMETOOLONG);
1784 static int mdd_declare_create(const struct lu_env *env,
1785 struct mdd_device *mdd,
1786 struct mdd_object *p,
1787 struct mdd_object *c,
1788 const struct lu_name *name,
1791 struct thandle *handle,
1792 const struct md_op_spec *spec)
1794 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1797 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1801 /* if dir, then can inherit default ACl */
1803 buf->lb_len = lmm_size;
1804 if (S_ISDIR(ma->ma_attr.la_mode)) {
1805 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1808 rc = mdo_declare_ref_add(env, p, handle);
1813 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1818 rc = mdd_declare_object_initialize(env, c, ma, handle);
1822 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1823 name->ln_name, handle);
1827 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1832 if (S_ISLNK(ma->ma_attr.la_mode)) {
1833 rc = dt_declare_record_write(env, mdd_object_child(c),
1834 strlen(spec->u.sp_symname), 0,
1839 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1843 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1847 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1855 * Create object and insert it into namespace.
1857 static int mdd_create(const struct lu_env *env,
1858 struct md_object *pobj,
1859 const struct lu_name *lname,
1860 struct md_object *child,
1861 struct md_op_spec *spec,
1864 struct mdd_thread_info *info = mdd_env_info(env);
1865 struct lu_attr *la = &info->mti_la_for_fix;
1866 struct md_attr *ma_acl = &info->mti_ma;
1867 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1868 struct mdd_object *son = md2mdd_obj(child);
1869 struct mdd_device *mdd = mdo2mdd(pobj);
1870 struct lu_attr *attr = &ma->ma_attr;
1871 struct lov_mds_md *lmm = NULL;
1872 struct thandle *handle;
1873 struct dynlock_handle *dlh;
1874 const char *name = lname->ln_name;
1875 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1876 int got_def_acl = 0;
1877 #ifdef HAVE_QUOTA_SUPPORT
1878 struct obd_device *obd = mdd->mdd_obd_dev;
1879 struct obd_export *exp = md_quota(env)->mq_exp;
1880 struct mds_obd *mds = &obd->u.mds;
1881 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1882 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1883 int quota_opc = 0, block_count = 0;
1884 int inode_pending[MAXQUOTAS] = { 0, 0 };
1885 int block_pending[MAXQUOTAS] = { 0, 0 };
1886 int parent_pending[MAXQUOTAS] = { 0, 0 };
1891 * Two operations have to be performed:
1893 * - an allocation of a new object (->do_create()), and
1895 * - an insertion into a parent index (->dio_insert()).
1897 * Due to locking, operation order is not important, when both are
1898 * successful, *but* error handling cases are quite different:
1900 * - if insertion is done first, and following object creation fails,
1901 * insertion has to be rolled back, but this operation might fail
1902 * also leaving us with dangling index entry.
1904 * - if creation is done first, is has to be undone if insertion
1905 * fails, leaving us with leaked space, which is neither good, nor
1908 * It seems that creation-first is simplest solution, but it is
1909 * sub-optimal in the frequent
1914 * case, because second mkdir is bound to create object, only to
1915 * destroy it immediately.
1917 * To avoid this follow local file systems that do double lookup:
1919 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1921 * 1. create (mdd_object_create_internal())
1923 * 2. insert (__mdd_index_insert(), lookup again)
1926 /* Sanity checks before big job. */
1927 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1931 #ifdef HAVE_QUOTA_SUPPORT
1932 if (mds->mds_quota) {
1933 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1935 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1939 quota_opc = FSFILT_OP_CREATE;
1940 mdd_quota_wrapper(&ma->ma_attr, qcids);
1941 mdd_quota_wrapper(la_tmp, qpids);
1942 /* get file quota for child */
1943 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1944 qcids, inode_pending, 1, NULL, 0, NULL,
1946 switch (ma->ma_attr.la_mode & S_IFMT) {
1955 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1956 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1960 /* get block quota for child and parent */
1962 lquota_chkquota(mds_quota_interface_ref, obd,
1963 exp, qcids, block_pending,
1965 LQUOTA_FLAGS_BLK, NULL, 0);
1967 lquota_chkquota(mds_quota_interface_ref, obd,
1968 exp, qpids, parent_pending, 1,
1969 NULL, LQUOTA_FLAGS_BLK, NULL,
1976 * No RPC inside the transaction, so OST objects should be created at
1979 if (S_ISREG(attr->la_mode)) {
1980 lmm_size = ma->ma_lmm_size;
1981 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1984 GOTO(out_pending, rc);
1987 if (!S_ISLNK(attr->la_mode)) {
1988 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1989 ma_acl->ma_acl = info->mti_xattr_buf;
1990 ma_acl->ma_need = MA_ACL_DEF;
1991 ma_acl->ma_valid = 0;
1993 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1994 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1995 mdd_read_unlock(env, mdd_pobj);
1998 else if (ma_acl->ma_valid & MA_ACL_DEF)
2002 handle = mdd_trans_create(env, mdd);
2004 GOTO(out_free, rc = PTR_ERR(handle));
2006 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2007 lmm_size, handle, spec);
2011 rc = mdd_trans_start(env, mdd, handle);
2015 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2017 GOTO(out_trans, rc = -ENOMEM);
2019 mdd_write_lock(env, son, MOR_TGT_CHILD);
2020 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2022 mdd_write_unlock(env, son);
2028 #ifdef CONFIG_FS_POSIX_ACL
2030 struct lu_buf *acl_buf = &info->mti_buf;
2031 acl_buf->lb_buf = ma_acl->ma_acl;
2032 acl_buf->lb_len = ma_acl->ma_acl_size;
2034 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2036 mdd_write_unlock(env, son);
2039 ma->ma_attr.la_valid |= LA_MODE;
2044 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2045 son, ma, handle, spec);
2046 mdd_write_unlock(env, son);
2049 * Object has no links, so it will be destroyed when last
2050 * reference is released. (XXX not now.)
2056 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2057 name, S_ISDIR(attr->la_mode), handle,
2058 mdd_object_capa(env, mdd_pobj));
2065 /* No need mdd_lsm_sanity_check here */
2066 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2068 CERROR("error on stripe info copy %d \n", rc);
2071 if (lmm && lmm_size > 0) {
2072 /* Set Lov here, do not get lmm again later */
2073 memcpy(ma->ma_lmm, lmm, lmm_size);
2074 ma->ma_lmm_size = lmm_size;
2075 ma->ma_valid |= MA_LOV;
2078 if (S_ISLNK(attr->la_mode)) {
2079 struct md_ucred *uc = md_ucred(env);
2080 struct dt_object *dt = mdd_object_child(son);
2081 const char *target_name = spec->u.sp_symname;
2082 int sym_len = strlen(target_name);
2083 const struct lu_buf *buf;
2086 buf = mdd_buf_get_const(env, target_name, sym_len);
2087 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2088 mdd_object_capa(env, son),
2090 CFS_CAP_SYS_RESOURCE_MASK);
2095 GOTO(cleanup, rc = -EFAULT);
2099 la->la_valid = LA_CTIME | LA_MTIME;
2100 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2104 /* Return attr back. */
2105 rc = mdd_attr_get_internal_locked(env, son, ma);
2108 if (rc && created) {
2112 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2113 S_ISDIR(attr->la_mode),
2114 handle, BYPASS_CAPA);
2116 CERROR("error can not cleanup destroy %d\n",
2121 mdd_write_lock(env, son, MOR_TGT_CHILD);
2122 mdo_ref_del(env, son, handle);
2123 if (initialized && S_ISDIR(attr->la_mode))
2124 mdo_ref_del(env, son, handle);
2125 mdd_write_unlock(env, son);
2129 /* update lov_objid data, must be before transaction stop! */
2131 mdd_lov_objid_update(mdd, lmm);
2133 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2136 rc = mdd_changelog_ns_store(env, mdd,
2137 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2138 S_ISREG(attr->la_mode) ? CL_CREATE :
2139 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2140 0, son, mdd_pobj, NULL, lname, handle);
2142 mdd_trans_stop(env, mdd, rc, handle);
2144 /* finish lov_create stuff, free all temporary data */
2145 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2147 #ifdef HAVE_QUOTA_SUPPORT
2149 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2151 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2153 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2155 /* Trigger dqacq on the owner of child and parent. If failed,
2156 * the next call for lquota_chkquota will process it. */
2157 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2165 * Get locks on parents in proper order
2166 * RETURN: < 0 - error, rename_order if successful
2174 static int mdd_rename_order(const struct lu_env *env,
2175 struct mdd_device *mdd,
2176 struct mdd_object *src_pobj,
2177 struct mdd_object *tgt_pobj)
2179 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2183 if (src_pobj == tgt_pobj)
2184 RETURN(MDD_RN_SAME);
2186 /* compared the parent child relationship of src_p&tgt_p */
2187 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2189 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2192 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2205 /* has not mdd_write{read}_lock on any obj yet. */
2206 static int mdd_rename_sanity_check(const struct lu_env *env,
2207 struct mdd_object *src_pobj,
2208 struct mdd_object *tgt_pobj,
2209 struct mdd_object *sobj,
2210 struct mdd_object *tobj,
2216 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2219 /* XXX: when get here, sobj must NOT be NULL,
2220 * the other case has been processed in cml_rename
2221 * before mdd_rename and enable MDS_PERM_BYPASS. */
2224 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2228 /* XXX: when get here, "tobj == NULL" means tobj must
2229 * NOT exist (neither on remote MDS, such case has been
2230 * processed in cml_rename before mdd_rename and enable
2232 * So check may_create, but not check may_unlink. */
2234 rc = mdd_may_create(env, tgt_pobj, NULL,
2235 (src_pobj != tgt_pobj), 0);
2237 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2238 (src_pobj != tgt_pobj), 1);
2240 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2241 S_ISDIR(ma->ma_attr.la_mode))
2242 rc = __mdd_may_link(env, tgt_pobj);
2247 static int mdd_declare_rename(const struct lu_env *env,
2248 struct mdd_device *mdd,
2249 struct mdd_object *mdd_spobj,
2250 struct mdd_object *mdd_tpobj,
2251 struct mdd_object *mdd_sobj,
2252 struct mdd_object *mdd_tobj,
2253 const struct lu_name *sname,
2254 const struct lu_name *tname,
2256 struct thandle *handle)
2264 /* name from source dir */
2265 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2269 /* .. from source child */
2270 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2271 /* source child can be directory,
2272 * counted by source dir's nlink */
2273 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2277 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2281 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2286 /* new target child can be directory,
2287 * counted by target dir's nlink */
2288 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2294 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2298 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2301 mdd_declare_links_add(env, mdd_sobj, handle);
2305 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2310 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2311 tname->ln_name, handle);
2315 /* name from target dir (old name), we declare it unconditionally
2316 * as mdd_rename() calls delete unconditionally as well. so just
2317 * to balance declarations vs calls to change ... */
2318 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2322 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2323 /* delete target child in target parent directory */
2324 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2328 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2329 /* target child can be directory,
2330 * delete "." reference in target child directory */
2331 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2335 /* delete ".." reference in target parent directory */
2336 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2341 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2345 mdd_declare_links_add(env, mdd_tobj, handle);
2349 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2354 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2358 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2365 /* src object can be remote that is why we use only fid and type of object */
2366 static int mdd_rename(const struct lu_env *env,
2367 struct md_object *src_pobj, struct md_object *tgt_pobj,
2368 const struct lu_fid *lf, const struct lu_name *lsname,
2369 struct md_object *tobj, const struct lu_name *ltname,
2372 const char *sname = lsname->ln_name;
2373 const char *tname = ltname->ln_name;
2374 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2375 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2376 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2377 struct mdd_device *mdd = mdo2mdd(src_pobj);
2378 struct mdd_object *mdd_sobj = NULL; /* source object */
2379 struct mdd_object *mdd_tobj = NULL;
2380 struct dynlock_handle *sdlh, *tdlh;
2381 struct thandle *handle;
2382 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2383 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2387 #ifdef HAVE_QUOTA_SUPPORT
2388 struct obd_device *obd = mdd->mdd_obd_dev;
2389 struct obd_export *exp = md_quota(env)->mq_exp;
2390 struct mds_obd *mds = &obd->u.mds;
2391 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2392 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2393 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2394 int quota_copc = 0, quota_popc = 0;
2395 int rec_pending[MAXQUOTAS] = { 0, 0 };
2399 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2400 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2403 mdd_tobj = md2mdd_obj(tobj);
2405 #ifdef HAVE_QUOTA_SUPPORT
2406 if (mds->mds_quota) {
2407 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2409 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2411 mdd_quota_wrapper(la_tmp, qspids);
2413 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2417 mdd_data_get(env, mdd_tpobj, &data);
2418 quota_popc = FSFILT_OP_LINK;
2419 mdd_quota_wrapper(la_tmp, qtpids);
2420 /* get block quota for target parent */
2421 lquota_chkquota(mds_quota_interface_ref,
2423 rec_pending, 1, NULL,
2431 mdd_sobj = mdd_object_find(env, mdd, lf);
2433 handle = mdd_trans_create(env, mdd);
2435 GOTO(out_pending, rc = PTR_ERR(handle));
2437 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2438 mdd_tobj, lsname, ltname, ma, handle);
2442 rc = mdd_trans_start(env, mdd, handle);
2446 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2447 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2449 GOTO(cleanup_unlocked, rc);
2451 /* Get locks in determined order */
2452 if (rc == MDD_RN_SAME) {
2453 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2454 sname, MOR_SRC_PARENT);
2455 /* check hashes to determine do we need one lock or two */
2456 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2457 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2461 } else if (rc == MDD_RN_SRCTGT) {
2462 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2463 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2465 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2466 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2468 if (sdlh == NULL || tdlh == NULL)
2469 GOTO(cleanup, rc = -ENOMEM);
2471 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2472 mdd_sobj, mdd_tobj, ma);
2476 /* Remove source name from source directory */
2477 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2478 mdd_object_capa(env, mdd_spobj));
2482 /* "mv dir1 dir2" needs "dir1/.." link update */
2483 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2484 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2485 mdd_object_capa(env, mdd_sobj));
2487 GOTO(fixup_spobj2, rc);
2489 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2490 handle, mdd_object_capa(env, mdd_sobj));
2492 GOTO(fixup_spobj, rc);
2495 /* Remove target name from target directory
2496 * Here tobj can be remote one, so we do index_delete unconditionally
2497 * and -ENOENT is allowed.
2499 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2500 mdd_object_capa(env, mdd_tpobj));
2503 /* tname might been renamed to something else */
2504 GOTO(fixup_spobj, rc);
2507 GOTO(fixup_spobj, rc);
2510 /* Insert new fid with target name into target dir */
2511 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2512 mdd_object_capa(env, mdd_tpobj));
2514 GOTO(fixup_tpobj, rc);
2516 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2517 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2519 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2521 la->la_valid = LA_CTIME;
2522 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2525 GOTO(fixup_tpobj, rc);
2528 /* Remove old target object
2529 * For tobj is remote case cmm layer has processed
2530 * and set tobj to NULL then. So when tobj is NOT NULL,
2531 * it must be local one.
2533 if (tobj && mdd_object_exists(mdd_tobj)) {
2534 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2535 if (mdd_is_dead_obj(mdd_tobj)) {
2536 mdd_write_unlock(env, mdd_tobj);
2537 /* shld not be dead, something is wrong */
2538 CERROR("tobj is dead, something is wrong\n");
2542 mdo_ref_del(env, mdd_tobj, handle);
2544 /* Remove dot reference. */
2546 mdo_ref_del(env, mdd_tobj, handle);
2548 la->la_valid = LA_CTIME;
2549 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2551 GOTO(fixup_tpobj, rc);
2553 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2554 mdd_write_unlock(env, mdd_tobj);
2556 GOTO(fixup_tpobj, rc);
2558 #ifdef HAVE_QUOTA_SUPPORT
2559 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2560 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2561 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2562 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2567 la->la_valid = LA_CTIME | LA_MTIME;
2568 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2570 GOTO(fixup_tpobj, rc);
2572 if (mdd_spobj != mdd_tpobj) {
2573 la->la_valid = LA_CTIME | LA_MTIME;
2574 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2578 if (rc == 0 && mdd_sobj) {
2579 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2580 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2581 mdo2fid(mdd_tpobj), ltname, handle);
2583 /* Old files might not have EA entry */
2584 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2586 mdd_write_unlock(env, mdd_sobj);
2587 /* We don't fail the transaction if the link ea can't be
2588 updated -- fid2path will use alternate lookup method. */
2596 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2599 CWARN("tp obj fix error %d\n",rc2);
2601 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2602 !mdd_is_dead_obj(mdd_tobj)) {
2603 rc2 = __mdd_index_insert(env, mdd_tpobj,
2604 mdo2fid(mdd_tobj), tname,
2609 CWARN("tp obj fix error %d\n",rc2);
2614 if (rc && is_dir && mdd_sobj) {
2615 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2619 CWARN("sp obj dotdot delete error %d\n",rc2);
2622 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2623 dotdot, handle, BYPASS_CAPA);
2625 CWARN("sp obj dotdot insert error %d\n",rc2);
2630 rc2 = __mdd_index_insert(env, mdd_spobj,
2631 lf, sname, is_dir, handle, BYPASS_CAPA);
2633 CWARN("sp obj fix error %d\n",rc2);
2636 if (likely(tdlh) && sdlh != tdlh)
2637 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2639 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2642 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2643 mdd_spobj, lf, lsname, handle);
2645 struct lu_fid zero_fid;
2646 fid_zero(&zero_fid);
2647 /* If the rename target exist, The CL_EXT record should save
2648 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2649 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2651 mdd_tobj ? NULL : &zero_fid,
2656 mdd_trans_stop(env, mdd, rc, handle);
2658 mdd_object_put(env, mdd_sobj);
2660 #ifdef HAVE_QUOTA_SUPPORT
2661 if (mds->mds_quota) {
2663 lquota_pending_commit(mds_quota_interface_ref, obd,
2664 qtpids, rec_pending, 1);
2667 /* Trigger dqrel on the source owner of parent.
2668 * If failed, the next call for lquota_chkquota will
2670 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2671 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2673 /* Trigger dqrel on the target owner of child.
2674 * If failed, the next call for lquota_chkquota
2675 * will process it. */
2676 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2677 qtpids, rc, quota_copc);
2684 /** enable/disable storing of hardlink info */
2685 int mdd_linkea_enable = 1;
2686 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2687 "record hardlink info in EAs");
2689 /** Read the link EA into a temp buffer.
2690 * Uses the name_buf since it is generally large.
2691 * \retval IS_ERR err
2692 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2694 struct lu_buf *mdd_links_get(const struct lu_env *env,
2695 struct mdd_object *mdd_obj)
2698 struct lustre_capa *capa;
2699 struct link_ea_header *leh;
2702 /* First try a small buf */
2703 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2704 if (buf->lb_buf == NULL)
2705 return ERR_PTR(-ENOMEM);
2707 capa = mdd_object_capa(env, mdd_obj);
2708 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2709 if (rc == -ERANGE) {
2710 /* Buf was too small, figure out what we need. */
2712 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2715 buf = mdd_buf_alloc(env, rc);
2716 if (buf->lb_buf == NULL)
2717 return ERR_PTR(-ENOMEM);
2718 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2724 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2725 leh->leh_magic = LINK_EA_MAGIC;
2726 leh->leh_reccount = __swab32(leh->leh_reccount);
2727 leh->leh_len = __swab64(leh->leh_len);
2728 /* entries are swabbed by mdd_lee_unpack */
2730 if (leh->leh_magic != LINK_EA_MAGIC)
2731 return ERR_PTR(-EINVAL);
2732 if (leh->leh_reccount == 0)
2733 return ERR_PTR(-ENODATA);
2738 /** Pack a link_ea_entry.
2739 * All elements are stored as chars to avoid alignment issues.
2740 * Numbers are always big-endian
2741 * \retval record length
2743 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2744 const struct lu_fid *pfid)
2746 struct lu_fid tmpfid;
2749 fid_cpu_to_be(&tmpfid, pfid);
2750 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2751 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2752 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2754 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2755 lee->lee_reclen[1] = reclen & 0xff;
2759 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2760 struct lu_name *lname, struct lu_fid *pfid)
2762 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2763 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2764 fid_be_to_cpu(pfid, pfid);
2765 lname->ln_name = lee->lee_name;
2766 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2769 /** Add a record to the end of link ea buf */
2770 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2771 const struct lu_fid *pfid,
2772 const struct lu_name *lname)
2774 struct link_ea_header *leh;
2775 struct link_ea_entry *lee;
2778 if (lname == NULL || pfid == NULL)
2781 /* Make sure our buf is big enough for the new one */
2783 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2784 if (leh->leh_len + reclen > buf->lb_len) {
2785 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2790 lee = buf->lb_buf + leh->leh_len;
2791 reclen = mdd_lee_pack(lee, lname, pfid);
2792 leh->leh_len += reclen;
2793 leh->leh_reccount++;
2797 static int mdd_declare_links_add(const struct lu_env *env,
2798 struct mdd_object *mdd_obj,
2799 struct thandle *handle)
2803 /* XXX: max size? */
2804 rc = mdo_declare_xattr_set(env, mdd_obj,
2805 mdd_buf_get_const(env, NULL, 4096),
2806 XATTR_NAME_LINK, 0, handle);
2811 /* For pathologic linkers, we don't want to spend lots of time scanning the
2812 * link ea. Limit ourseleves to something reasonable; links not in the EA
2813 * can be looked up via (slower) parent lookup.
2815 #define LINKEA_MAX_COUNT 128
2817 static int mdd_links_add(const struct lu_env *env,
2818 struct mdd_object *mdd_obj,
2819 const struct lu_fid *pfid,
2820 const struct lu_name *lname,
2821 struct thandle *handle, int first)
2824 struct link_ea_header *leh;
2828 if (!mdd_linkea_enable)
2831 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2834 if (rc != -ENODATA) {
2835 CERROR("link_ea read failed %d "DFID"\n", rc,
2836 PFID(mdd_object_fid(mdd_obj)));
2839 /* empty EA; start one */
2840 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2841 if (buf->lb_buf == NULL)
2844 leh->leh_magic = LINK_EA_MAGIC;
2845 leh->leh_len = sizeof(struct link_ea_header);
2846 leh->leh_reccount = 0;
2850 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2853 rc = __mdd_links_add(env, buf, pfid, lname);
2858 rc = __mdd_xattr_set(env, mdd_obj,
2859 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2860 XATTR_NAME_LINK, 0, handle);
2863 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2864 PFID(mdd_object_fid(mdd_obj)));
2866 CERROR("link_ea add failed %d "DFID"\n", rc,
2867 PFID(mdd_object_fid(mdd_obj)));
2870 if (buf->lb_len > OBD_ALLOC_BIG)
2871 /* if we vmalloced a large buffer drop it */
2877 static int mdd_links_rename(const struct lu_env *env,
2878 struct mdd_object *mdd_obj,
2879 const struct lu_fid *oldpfid,
2880 const struct lu_name *oldlname,
2881 const struct lu_fid *newpfid,
2882 const struct lu_name *newlname,
2883 struct thandle *handle)
2886 struct link_ea_header *leh;
2887 struct link_ea_entry *lee;
2888 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2889 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2895 if (!mdd_linkea_enable)
2898 if (mdd_obj->mod_flags & DEAD_OBJ)
2899 /* No more links, don't bother */
2902 buf = mdd_links_get(env, mdd_obj);
2906 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2907 rc, PFID(mdd_object_fid(mdd_obj)));
2909 CERROR("link_ea read failed %d "DFID"\n",
2910 rc, PFID(mdd_object_fid(mdd_obj)));
2914 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2916 /* Find the old record */
2917 for(count = 0; count < leh->leh_reccount; count++) {
2918 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2919 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2920 lu_fid_eq(tmpfid, oldpfid) &&
2921 (strncmp(tmpname->ln_name, oldlname->ln_name,
2922 tmpname->ln_namelen) == 0))
2924 lee = (struct link_ea_entry *)((char *)lee + reclen);
2926 if ((count + 1) > leh->leh_reccount) {
2927 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2928 oldlname->ln_namelen, oldlname->ln_name);
2929 GOTO(out, rc = -ENOENT);
2932 /* Remove the old record */
2933 leh->leh_reccount--;
2934 leh->leh_len -= reclen;
2935 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2938 /* If renaming, add the new record */
2939 if (newpfid != NULL) {
2940 /* if the add fails, we still delete the out-of-date old link */
2941 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2945 rc = __mdd_xattr_set(env, mdd_obj,
2946 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2947 XATTR_NAME_LINK, 0, handle);
2953 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2954 oldlname->ln_namelen, oldlname->ln_name, rc,
2955 PFID(mdd_object_fid(mdd_obj)));
2957 if (buf->lb_len > OBD_ALLOC_BIG)
2958 /* if we vmalloced a large buffer drop it */
2964 const struct md_dir_operations mdd_dir_ops = {
2965 .mdo_is_subdir = mdd_is_subdir,
2966 .mdo_lookup = mdd_lookup,
2967 .mdo_create = mdd_create,
2968 .mdo_rename = mdd_rename,
2969 .mdo_link = mdd_link,
2970 .mdo_unlink = mdd_unlink,
2971 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
2972 .mdo_name_insert = mdd_name_insert,
2973 .mdo_name_remove = mdd_name_remove,
2974 .mdo_rename_tgt = mdd_rename_tgt,
2975 .mdo_create_data = mdd_create_data,