1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_fid.h>
61 #include "mdd_internal.h"
63 static const char dot[] = ".";
64 static const char dotdot[] = "..";
66 static struct lu_name lname_dotdot = {
71 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_name *lname, struct lu_fid* fid,
74 static int mdd_links_add(const struct lu_env *env,
75 struct mdd_object *mdd_obj,
76 const struct lu_fid *pfid,
77 const struct lu_name *lname,
78 struct thandle *handle);
79 static int mdd_links_rename(const struct lu_env *env,
80 struct mdd_object *mdd_obj,
81 const struct lu_fid *oldpfid,
82 const struct lu_name *oldlname,
83 const struct lu_fid *newpfid,
84 const struct lu_name *newlname,
85 struct thandle *handle);
88 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
89 const struct lu_name *lname, struct lu_fid* fid, int mask)
91 const char *name = lname->ln_name;
92 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
93 struct dynlock_handle *dlh;
96 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
97 if (unlikely(dlh == NULL))
99 rc = __mdd_lookup(env, pobj, lname, fid, mask);
100 mdd_pdo_read_unlock(env, mdd_obj, dlh);
105 int mdd_lookup(const struct lu_env *env,
106 struct md_object *pobj, const struct lu_name *lname,
107 struct lu_fid* fid, struct md_op_spec *spec)
111 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
115 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
118 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
122 * For root fid use special function, which does not compare version component
123 * of fid. Version component is different for root fids on all MDTs.
125 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
127 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
128 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
132 * return 1: if lf is the fid of the ancestor of p1;
135 * return -EREMOTE: if remote object is found, in this
136 * case fid of remote object is saved to @pf;
138 * otherwise: values < 0, errors.
140 static int mdd_is_parent(const struct lu_env *env,
141 struct mdd_device *mdd,
142 struct mdd_object *p1,
143 const struct lu_fid *lf,
146 struct mdd_object *parent = NULL;
151 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
152 pfid = &mdd_env_info(env)->mti_fid;
154 /* Check for root first. */
155 if (mdd_is_root(mdd, mdo2fid(p1)))
159 /* this is done recursively, bypass capa for each obj */
160 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
161 rc = mdd_parent_fid(env, p1, pfid);
164 if (mdd_is_root(mdd, pfid))
166 if (lu_fid_eq(pfid, lf))
169 mdd_object_put(env, parent);
170 parent = mdd_object_find(env, mdd, pfid);
172 /* cross-ref parent */
173 if (parent == NULL) {
176 GOTO(out, rc = -EREMOTE);
177 } else if (IS_ERR(parent))
178 GOTO(out, rc = PTR_ERR(parent));
183 if (parent && !IS_ERR(parent))
184 mdd_object_put(env, parent);
189 * No permission check is needed.
191 * returns 1: if fid is ancestor of @mo;
192 * returns 0: if fid is not a ancestor of @mo;
194 * returns EREMOTE if remote object is found, fid of remote object is saved to
197 * returns < 0: if error
199 static int mdd_is_subdir(const struct lu_env *env,
200 struct md_object *mo, const struct lu_fid *fid,
203 struct mdd_device *mdd = mdo2mdd(mo);
207 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
210 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
214 } else if (rc == 1) {
215 /* found @fid is parent */
223 * Check that @dir contains no entries except (possibly) dot and dotdot.
228 * -ENOTDIR not a directory object
229 * -ENOTEMPTY not empty
233 static int mdd_dir_is_empty(const struct lu_env *env,
234 struct mdd_object *dir)
237 struct dt_object *obj;
238 const struct dt_it_ops *iops;
242 obj = mdd_object_child(dir);
243 if (!dt_try_as_dir(env, obj))
246 iops = &obj->do_index_ops->dio_it;
247 it = iops->init(env, obj, BYPASS_CAPA);
249 result = iops->get(env, it, (const void *)"");
252 for (result = 0, i = 0; result == 0 && i < 3; ++i)
253 result = iops->next(env, it);
256 else if (result == +1)
258 } else if (result == 0)
260 * Huh? Index contains no zero key?
271 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
273 struct mdd_device *m = mdd_obj2mdd_dev(obj);
274 struct lu_attr *la = &mdd_env_info(env)->mti_la;
278 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
283 * Subdir count limitation can be broken through.
285 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
286 !S_ISDIR(la->la_mode))
293 * Check whether it may create the cobj under the pobj.
296 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
297 struct mdd_object *cobj, int check_perm, int check_nlink)
302 if (cobj && mdd_object_exists(cobj))
305 if (mdd_is_dead_obj(pobj))
309 rc = mdd_permission_internal_locked(env, pobj, NULL,
310 MAY_WRITE | MAY_EXEC,
313 if (!rc && check_nlink)
314 rc = __mdd_may_link(env, pobj);
320 * Check whether can unlink from the pobj in the case of "cobj == NULL".
322 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
323 const struct md_attr *ma)
328 if (mdd_is_dead_obj(pobj))
331 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
332 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
335 rc = mdd_permission_internal_locked(env, pobj, NULL,
336 MAY_WRITE | MAY_EXEC,
341 if (mdd_is_append(pobj))
348 * pobj == NULL is remote ops case, under such case, pobj's
349 * VTX feature has been checked already, no need check again.
351 static inline int mdd_is_sticky(const struct lu_env *env,
352 struct mdd_object *pobj,
353 struct mdd_object *cobj)
355 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
356 struct md_ucred *uc = md_ucred(env);
360 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
364 if (!(tmp_la->la_mode & S_ISVTX) ||
365 (tmp_la->la_uid == uc->mu_fsuid))
369 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
373 if (tmp_la->la_uid == uc->mu_fsuid)
376 return !mdd_capable(uc, CFS_CAP_FOWNER);
380 * Check whether it may delete the cobj from the pobj.
383 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
384 struct mdd_object *cobj, struct md_attr *ma,
385 int check_perm, int check_empty)
391 if (!mdd_object_exists(cobj))
395 if (mdd_is_dead_obj(pobj))
399 rc = mdd_permission_internal_locked(env, pobj, NULL,
400 MAY_WRITE | MAY_EXEC,
406 if (mdd_is_append(pobj))
410 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
411 mdd_is_sticky(env, pobj, cobj))
414 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
417 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
418 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
421 if (S_ISDIR(ma->ma_attr.la_mode)) {
422 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
424 if (!S_ISDIR(mdd_object_type(cobj)))
427 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
429 } else if (S_ISDIR(mdd_object_type(cobj)))
432 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
433 rc = mdd_dir_is_empty(env, cobj);
440 * has mdd_write_lock on src already, but not on tgt yet
442 int mdd_link_sanity_check(const struct lu_env *env,
443 struct mdd_object *tgt_obj,
444 const struct lu_name *lname,
445 struct mdd_object *src_obj)
447 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
451 /* Local ops, no lookup before link, check filename length here. */
452 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
453 RETURN(-ENAMETOOLONG);
455 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
458 if (S_ISDIR(mdd_object_type(src_obj)))
461 LASSERT(src_obj != tgt_obj);
463 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
468 rc = __mdd_may_link(env, src_obj);
474 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
475 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
476 * (maybe too large to be represented). It is a trick to break through the
477 * "i_nlink" limitation for subdir count.
479 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
480 struct thandle *handle)
482 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
483 struct mdd_device *m = mdd_obj2mdd_dev(obj);
485 if (!mdd_is_mnlink(obj)) {
486 if (S_ISDIR(mdd_object_type(obj))) {
487 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
490 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
491 obj->mod_flags |= MNLINK_OBJ;
492 tmp_la->la_nlink = 1;
493 tmp_la->la_valid = LA_NLINK;
494 mdd_attr_set_internal(env, obj, tmp_la, handle,
499 mdo_ref_add(env, obj, handle);
503 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
504 struct thandle *handle, int is_dot)
506 if (!mdd_is_mnlink(obj) || is_dot)
507 mdo_ref_del(env, obj, handle);
510 /* insert named index, add reference if isdir */
511 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
512 const struct lu_fid *lf, const char *name, int is_dir,
513 struct thandle *handle, struct lustre_capa *capa)
515 struct dt_object *next = mdd_object_child(pobj);
519 if (dt_try_as_dir(env, next)) {
520 struct md_ucred *uc = md_ucred(env);
522 rc = next->do_index_ops->dio_insert(env, next,
523 __mdd_fid_rec(env, lf),
524 (const struct dt_key *)name,
525 handle, capa, uc->mu_cap &
526 CFS_CAP_SYS_RESOURCE_MASK);
533 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
534 __mdd_ref_add(env, pobj, handle);
535 mdd_write_unlock(env, pobj);
541 /* delete named index, drop reference if isdir */
542 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
543 const char *name, int is_dir, struct thandle *handle,
544 struct lustre_capa *capa)
546 struct dt_object *next = mdd_object_child(pobj);
550 if (dt_try_as_dir(env, next)) {
551 rc = next->do_index_ops->dio_delete(env, next,
552 (struct dt_key *)name,
554 if (rc == 0 && is_dir) {
557 if (name != NULL && name[0] == '.' && name[1] == 0)
559 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
560 __mdd_ref_del(env, pobj, handle, is_dot);
561 mdd_write_unlock(env, pobj);
570 __mdd_index_insert_only(const struct lu_env *env, struct mdd_object *pobj,
571 const struct lu_fid *lf, const char *name,
572 struct thandle *handle, struct lustre_capa *capa)
574 struct dt_object *next = mdd_object_child(pobj);
578 if (dt_try_as_dir(env, next)) {
579 struct md_ucred *uc = md_ucred(env);
581 rc = next->do_index_ops->dio_insert(env, next,
582 __mdd_fid_rec(env, lf),
583 (const struct dt_key *)name,
584 handle, capa, uc->mu_cap &
585 CFS_CAP_SYS_RESOURCE_MASK);
592 /** Store a namespace change changelog record
593 * If this fails, we must fail the whole transaction; we don't
594 * want the change to commit without the log entry.
595 * \param target - mdd_object of change
596 * \param parent - parent dir/object
597 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
598 * \param tname - target name string
599 * \param handle - transacion handle
601 static int mdd_changelog_ns_store(const struct lu_env *env,
602 struct mdd_device *mdd,
603 enum changelog_rec_type type,
604 struct mdd_object *target,
605 struct mdd_object *parent,
606 const struct lu_fid *tf,
607 const struct lu_name *tname,
608 struct thandle *handle)
610 const struct lu_fid *tfid;
611 const struct lu_fid *tpfid = mdo2fid(parent);
612 struct llog_changelog_rec *rec;
618 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
621 LASSERT(parent != NULL);
622 LASSERT(tname != NULL);
623 LASSERT(handle != NULL);
626 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
627 buf = mdd_buf_alloc(env, reclen);
628 if (buf->lb_buf == NULL)
630 rec = (struct llog_changelog_rec *)buf->lb_buf;
632 rec->cr_flags = CLF_VERSION;
633 rec->cr_type = (__u32)type;
634 tfid = tf ? tf : mdo2fid(target);
635 rec->cr_tfid = *tfid;
636 rec->cr_pfid = *tpfid;
637 rec->cr_namelen = tname->ln_namelen;
638 memcpy(rec->cr_name, tname->ln_name, rec->cr_namelen);
640 target->mod_cltime = cfs_time_current_64();
642 rc = mdd_changelog_llog_write(mdd, rec, handle);
644 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
645 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
652 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
653 struct md_object *src_obj, const struct lu_name *lname,
656 const char *name = lname->ln_name;
657 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
658 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
659 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
660 struct mdd_device *mdd = mdo2mdd(src_obj);
661 struct dynlock_handle *dlh;
662 struct thandle *handle;
663 #ifdef HAVE_QUOTA_SUPPORT
664 struct obd_device *obd = mdd->mdd_obd_dev;
665 struct mds_obd *mds = &obd->u.mds;
666 unsigned int qids[MAXQUOTAS] = { 0, 0 };
667 int quota_opc = 0, rec_pending = 0;
672 #ifdef HAVE_QUOTA_SUPPORT
673 if (mds->mds_quota) {
674 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
676 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
679 mdd_data_get(env, mdd_tobj, &data);
680 quota_opc = FSFILT_OP_LINK;
681 mdd_quota_wrapper(la_tmp, qids);
682 /* get block quota for parent */
683 lquota_chkquota(mds_quota_interface_ref, obd,
684 qids[USRQUOTA], qids[GRPQUOTA], 1,
685 &rec_pending, NULL, LQUOTA_FLAGS_BLK,
691 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
692 handle = mdd_trans_start(env, mdd);
694 GOTO(out_pending, rc = PTR_ERR(handle));
696 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
698 GOTO(out_trans, rc = -ENOMEM);
699 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
701 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
703 GOTO(out_unlock, rc);
705 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
707 mdd_object_capa(env, mdd_tobj));
709 GOTO(out_unlock, rc);
711 __mdd_ref_add(env, mdd_sobj, handle);
713 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
714 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
716 la->la_valid = LA_CTIME | LA_MTIME;
717 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
719 GOTO(out_unlock, rc);
721 la->la_valid = LA_CTIME;
722 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
724 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj), lname, handle);
728 mdd_write_unlock(env, mdd_sobj);
729 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
732 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, mdd_sobj,
733 mdd_tobj, NULL, lname, handle);
734 mdd_trans_stop(env, mdd, rc, handle);
736 #ifdef HAVE_QUOTA_SUPPORT
739 lquota_pending_commit(mds_quota_interface_ref, obd,
740 qids[USRQUOTA], qids[GRPQUOTA],
742 /* Trigger dqacq for the parent owner. If failed,
743 * the next call for lquota_chkquota will process it. */
744 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
751 /* caller should take a lock before calling */
752 int mdd_finish_unlink(const struct lu_env *env,
753 struct mdd_object *obj, struct md_attr *ma,
760 rc = mdd_iattr_get(env, obj, ma);
761 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
762 /* add new orphan and the object
763 * will be deleted during mdd_close() */
764 if (obj->mod_count) {
765 rc = __mdd_orphan_add(env, obj, th);
767 obj->mod_flags |= ORPHAN_OBJ;
770 obj->mod_flags |= DEAD_OBJ;
771 if (!(obj->mod_flags & ORPHAN_OBJ)) {
772 rc = mdd_object_kill(env, obj, ma);
779 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
786 * has mdd_write_lock on cobj already, but not on pobj yet
788 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
789 struct mdd_object *cobj, struct md_attr *ma)
794 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
799 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
800 struct md_object *cobj, const struct lu_name *lname,
803 const char *name = lname->ln_name;
804 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
805 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
806 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
807 struct mdd_device *mdd = mdo2mdd(pobj);
808 struct dynlock_handle *dlh;
809 struct thandle *handle;
810 #ifdef HAVE_QUOTA_SUPPORT
811 struct obd_device *obd = mdd->mdd_obd_dev;
812 struct mds_obd *mds = &obd->u.mds;
813 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
814 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
817 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
821 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
822 PFID(mdd_object_fid(mdd_cobj)));
824 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
828 handle = mdd_trans_start(env, mdd);
830 RETURN(PTR_ERR(handle));
832 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
834 GOTO(out_trans, rc = -ENOMEM);
835 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
837 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
841 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
842 mdd_object_capa(env, mdd_pobj));
846 __mdd_ref_del(env, mdd_cobj, handle, 0);
849 __mdd_ref_del(env, mdd_cobj, handle, 1);
851 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
852 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
854 la->la_valid = LA_CTIME | LA_MTIME;
855 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
859 la->la_valid = LA_CTIME;
860 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
864 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
865 #ifdef HAVE_QUOTA_SUPPORT
866 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
867 ma->ma_attr.la_nlink == 0) {
868 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
870 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
872 mdd_quota_wrapper(la_tmp, qpids);
873 if (mdd_cobj->mod_count == 0) {
874 quota_opc = FSFILT_OP_UNLINK;
875 mdd_quota_wrapper(&ma->ma_attr, qcids);
877 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
884 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
885 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
888 /* old files may not have link ea; ignore errors */
889 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
890 lname, NULL, NULL, handle);
894 mdd_write_unlock(env, mdd_cobj);
895 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
898 rc = mdd_changelog_ns_store(env, mdd,
899 is_dir ? CL_RMDIR : CL_UNLINK,
900 mdd_cobj, mdd_pobj, NULL, lname,
903 mdd_trans_stop(env, mdd, rc, handle);
904 #ifdef HAVE_QUOTA_SUPPORT
906 /* Trigger dqrel on the owner of child and parent. If failed,
907 * the next call for lquota_chkquota will process it. */
908 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
914 /* has not lock on pobj yet */
915 static int mdd_ni_sanity_check(const struct lu_env *env,
916 struct md_object *pobj,
917 const struct md_attr *ma)
919 struct mdd_object *obj = md2mdd_obj(pobj);
923 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
926 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
934 static int mdd_name_insert(const struct lu_env *env,
935 struct md_object *pobj,
936 const struct lu_name *lname,
937 const struct lu_fid *fid,
938 const struct md_attr *ma)
940 const char *name = lname->ln_name;
941 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
942 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
943 struct mdd_device *mdd = mdo2mdd(pobj);
944 struct dynlock_handle *dlh;
945 struct thandle *handle;
946 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
947 #ifdef HAVE_QUOTA_SUPPORT
948 struct md_ucred *uc = md_ucred(env);
949 struct obd_device *obd = mdd->mdd_obd_dev;
950 struct mds_obd *mds = &obd->u.mds;
951 unsigned int qids[MAXQUOTAS] = { 0, 0 };
952 int quota_opc = 0, rec_pending = 0;
953 cfs_cap_t save = uc->mu_cap;
958 #ifdef HAVE_QUOTA_SUPPORT
959 if (mds->mds_quota) {
960 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
961 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
963 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
966 mdd_data_get(env, mdd_obj, &data);
967 quota_opc = FSFILT_OP_LINK;
968 mdd_quota_wrapper(la_tmp, qids);
969 /* get block quota for parent */
970 lquota_chkquota(mds_quota_interface_ref, obd,
971 qids[USRQUOTA], qids[GRPQUOTA],
972 1, &rec_pending, NULL,
973 LQUOTA_FLAGS_BLK, data, 1);
976 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
980 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
981 handle = mdd_trans_start(env, mdo2mdd(pobj));
983 GOTO(out_pending, rc = PTR_ERR(handle));
985 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
987 GOTO(out_trans, rc = -ENOMEM);
989 rc = mdd_ni_sanity_check(env, pobj, ma);
991 GOTO(out_unlock, rc);
993 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
994 handle, BYPASS_CAPA);
996 GOTO(out_unlock, rc);
999 * For some case, no need update obj's ctime (LA_CTIME is not set),
1001 * For other cases, update obj's ctime (LA_CTIME is set),
1004 if (ma->ma_attr.la_valid & LA_CTIME) {
1005 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1006 la->la_valid = LA_CTIME | LA_MTIME;
1007 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1012 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1014 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1016 #ifdef HAVE_QUOTA_SUPPORT
1017 if (mds->mds_quota) {
1020 lquota_pending_commit(mds_quota_interface_ref,
1021 obd, qids[USRQUOTA],
1024 /* Trigger dqacq for the parent owner. If failed,
1025 * the next call for lquota_chkquota will process it*/
1026 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1036 /* has not lock on pobj yet */
1037 static int mdd_nr_sanity_check(const struct lu_env *env,
1038 struct md_object *pobj,
1039 const struct md_attr *ma)
1041 struct mdd_object *obj = md2mdd_obj(pobj);
1045 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1048 rc = mdd_may_unlink(env, obj, ma);
1054 * Partial operation.
1056 static int mdd_name_remove(const struct lu_env *env,
1057 struct md_object *pobj,
1058 const struct lu_name *lname,
1059 const struct md_attr *ma)
1061 const char *name = lname->ln_name;
1062 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1063 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1064 struct mdd_device *mdd = mdo2mdd(pobj);
1065 struct dynlock_handle *dlh;
1066 struct thandle *handle;
1067 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1068 #ifdef HAVE_QUOTA_SUPPORT
1069 struct obd_device *obd = mdd->mdd_obd_dev;
1070 struct mds_obd *mds = &obd->u.mds;
1071 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1077 #ifdef HAVE_QUOTA_SUPPORT
1078 if (mds->mds_quota) {
1079 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1081 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1083 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1084 mdd_quota_wrapper(la_tmp, qids);
1088 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
1089 handle = mdd_trans_start(env, mdd);
1091 GOTO(out_pending, rc = PTR_ERR(handle));
1093 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1095 GOTO(out_trans, rc = -ENOMEM);
1097 rc = mdd_nr_sanity_check(env, pobj, ma);
1099 GOTO(out_unlock, rc);
1101 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1102 handle, BYPASS_CAPA);
1104 GOTO(out_unlock, rc);
1107 * For some case, no need update obj's ctime (LA_CTIME is not set),
1109 * For other cases, update obj's ctime (LA_CTIME is set),
1112 if (ma->ma_attr.la_valid & LA_CTIME) {
1113 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1114 la->la_valid = LA_CTIME | LA_MTIME;
1115 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1120 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1122 mdd_trans_stop(env, mdd, rc, handle);
1124 #ifdef HAVE_QUOTA_SUPPORT
1125 /* Trigger dqrel for the parent owner.
1126 * If failed, the next call for lquota_chkquota will process it. */
1128 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1136 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1138 static int mdd_rt_sanity_check(const struct lu_env *env,
1139 struct mdd_object *tgt_pobj,
1140 struct mdd_object *tobj,
1146 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1149 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1150 * exist. In fact, tobj must exist, otherwise the call trace will be:
1151 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1152 * When get here, tobj must be NOT NULL, the other case has been
1153 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1155 * So check may_delete, but not check nlink of tgt_pobj. */
1157 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1162 /* Partial rename op on slave MDD */
1163 static int mdd_rename_tgt(const struct lu_env *env,
1164 struct md_object *pobj, struct md_object *tobj,
1165 const struct lu_fid *lf, const struct lu_name *lname,
1168 const char *name = lname->ln_name;
1169 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1170 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1171 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1172 struct mdd_device *mdd = mdo2mdd(pobj);
1173 struct dynlock_handle *dlh;
1174 struct thandle *handle;
1175 #ifdef HAVE_QUOTA_SUPPORT
1176 struct obd_device *obd = mdd->mdd_obd_dev;
1177 struct mds_obd *mds = &obd->u.mds;
1178 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1179 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1180 int quota_opc = 0, rec_pending = 0;
1185 #ifdef HAVE_QUOTA_SUPPORT
1186 if (mds->mds_quota && !tobj) {
1187 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1189 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1192 mdd_data_get(env, mdd_tpobj, &data);
1193 quota_opc = FSFILT_OP_LINK;
1194 mdd_quota_wrapper(la_tmp, qpids);
1195 /* get block quota for target parent */
1196 lquota_chkquota(mds_quota_interface_ref, obd,
1197 qpids[USRQUOTA], qpids[GRPQUOTA], 1,
1198 &rec_pending, NULL, LQUOTA_FLAGS_BLK,
1203 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1204 handle = mdd_trans_start(env, mdd);
1206 GOTO(out_pending, rc = PTR_ERR(handle));
1208 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1210 GOTO(out_trans, rc = -ENOMEM);
1212 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1214 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1219 * If rename_tgt is called then we should just re-insert name with
1220 * correct fid, no need to dec/inc parent nlink if obj is dir.
1222 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1226 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1231 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1232 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1234 la->la_valid = LA_CTIME | LA_MTIME;
1235 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1240 * For tobj is remote case cmm layer has processed
1241 * and pass NULL tobj to here. So when tobj is NOT NULL,
1242 * it must be local one.
1244 if (tobj && mdd_object_exists(mdd_tobj)) {
1245 __mdd_ref_del(env, mdd_tobj, handle, 0);
1247 /* Remove dot reference. */
1248 if (S_ISDIR(ma->ma_attr.la_mode))
1249 __mdd_ref_del(env, mdd_tobj, handle, 1);
1251 la->la_valid = LA_CTIME;
1252 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1256 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1260 #ifdef HAVE_QUOTA_SUPPORT
1261 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1262 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1263 quota_opc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1264 mdd_quota_wrapper(&ma->ma_attr, qcids);
1271 mdd_write_unlock(env, mdd_tobj);
1272 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1275 /* Bare EXT record with no RENAME in front of it signifies
1276 a partial slave op */
1277 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
1278 mdd_tpobj, NULL, lname, handle);
1280 mdd_trans_stop(env, mdd, rc, handle);
1282 #ifdef HAVE_QUOTA_SUPPORT
1283 if (mds->mds_quota) {
1285 lquota_pending_commit(mds_quota_interface_ref, obd,
1290 /* Trigger dqrel/dqacq on the target owner of child and
1291 * parent. If failed, the next call for lquota_chkquota
1292 * will process it. */
1293 lquota_adjust(mds_quota_interface_ref, obd, qcids,
1294 qpids, rc, quota_opc);
1301 * The permission has been checked when obj created, no need check again.
1303 static int mdd_cd_sanity_check(const struct lu_env *env,
1304 struct mdd_object *obj)
1309 if (!obj || mdd_is_dead_obj(obj))
1316 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1317 struct md_object *cobj, const struct md_op_spec *spec,
1320 struct mdd_device *mdd = mdo2mdd(cobj);
1321 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1322 struct mdd_object *son = md2mdd_obj(cobj);
1323 struct lu_attr *attr = &ma->ma_attr;
1324 struct lov_mds_md *lmm = NULL;
1326 struct thandle *handle;
1330 rc = mdd_cd_sanity_check(env, son);
1334 if (!md_should_create(spec->sp_cr_flags))
1337 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1342 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1343 handle = mdd_trans_start(env, mdd);
1345 GOTO(out_free, rc = PTR_ERR(handle));
1348 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1349 * Should this be fixed?
1352 /* Replay creates has objects already */
1354 if (spec->no_create) {
1355 CDEBUG(D_INFO, "we already have lov ea\n");
1356 rc = mdd_lov_set_md(env, mdd_pobj, son,
1357 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1358 spec->u.sp_ea.eadatalen, handle, 0);
1361 /* No need mdd_lsm_sanity_check here */
1362 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1363 lmm_size, handle, 0);
1366 rc = mdd_attr_get_internal_locked(env, son, ma);
1368 /* update lov_objid data, must be before transaction stop! */
1370 mdd_lov_objid_update(mdd, lmm);
1372 mdd_trans_stop(env, mdd, rc, handle);
1374 /* Finish mdd_lov_create() stuff. */
1375 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1379 /* Get fid from name and parent */
1381 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1382 const struct lu_name *lname, struct lu_fid* fid, int mask)
1384 const char *name = lname->ln_name;
1385 const struct dt_key *key = (const struct dt_key *)name;
1386 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1387 struct mdd_device *m = mdo2mdd(pobj);
1388 struct dt_object *dir = mdd_object_child(mdd_obj);
1389 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
1393 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1396 rc = mdd_object_exists(mdd_obj);
1397 if (unlikely(rc == 0))
1399 else if (unlikely(rc < 0)) {
1400 CERROR("Object "DFID" locates on remote server\n",
1401 PFID(mdo2fid(mdd_obj)));
1405 /* The common filename length check. */
1406 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1407 RETURN(-ENAMETOOLONG);
1409 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1414 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1415 dt_try_as_dir(env, dir))) {
1417 rc = dir->do_index_ops->dio_lookup(env, dir,
1418 (struct dt_rec *)pack, key,
1419 mdd_object_capa(env, mdd_obj));
1421 rc = fid_unpack(pack, fid);
1430 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1431 const struct lu_name *lname, struct mdd_object *child,
1432 struct md_attr *ma, struct thandle *handle,
1433 const struct md_op_spec *spec)
1439 * Update attributes for child.
1442 * (1) the valid bits should be converted between Lustre and Linux;
1443 * (2) maybe, the child attributes should be set in OSD when creation.
1446 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1450 if (S_ISDIR(ma->ma_attr.la_mode)) {
1451 /* Add "." and ".." for newly created dir */
1452 __mdd_ref_add(env, child, handle);
1453 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1454 dot, handle, BYPASS_CAPA);
1456 rc = __mdd_index_insert_only(env, child, pfid,
1462 rc2 = __mdd_index_delete(env, child, dot, 1,
1463 handle, BYPASS_CAPA);
1465 CERROR("Failure to cleanup after dotdot"
1466 " creation: %d (%d)\n", rc2, rc);
1471 mdd_links_add(env, child, pfid, lname, handle);
1476 /* has not lock on pobj yet */
1477 static int mdd_create_sanity_check(const struct lu_env *env,
1478 struct md_object *pobj,
1479 const struct lu_name *lname,
1481 struct md_op_spec *spec)
1483 struct mdd_thread_info *info = mdd_env_info(env);
1484 struct lu_attr *la = &info->mti_la;
1485 struct lu_fid *fid = &info->mti_fid;
1486 struct mdd_object *obj = md2mdd_obj(pobj);
1487 struct mdd_device *m = mdo2mdd(pobj);
1488 int lookup = spec->sp_cr_lookup;
1493 if (mdd_is_dead_obj(obj))
1497 * In some cases this lookup is not needed - we know before if name
1498 * exists or not because MDT performs lookup for it.
1499 * name length check is done in lookup.
1503 * Check if the name already exist, though it will be checked in
1504 * _index_insert also, for avoiding rolling back if exists
1507 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1508 MAY_WRITE | MAY_EXEC);
1510 RETURN(rc ? : -EEXIST);
1513 * Check WRITE permission for the parent.
1514 * EXEC permission have been checked
1515 * when lookup before create already.
1517 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1524 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1528 if (la->la_mode & S_ISGID) {
1529 ma->ma_attr.la_gid = la->la_gid;
1530 if (S_ISDIR(ma->ma_attr.la_mode)) {
1531 ma->ma_attr.la_mode |= S_ISGID;
1532 ma->ma_attr.la_valid |= LA_MODE;
1536 switch (ma->ma_attr.la_mode & S_IFMT) {
1538 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1540 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1541 RETURN(-ENAMETOOLONG);
1561 * Create object and insert it into namespace.
1563 static int mdd_create(const struct lu_env *env,
1564 struct md_object *pobj,
1565 const struct lu_name *lname,
1566 struct md_object *child,
1567 struct md_op_spec *spec,
1570 struct mdd_thread_info *info = mdd_env_info(env);
1571 struct lu_attr *la = &info->mti_la_for_fix;
1572 struct md_attr *ma_acl = &info->mti_ma;
1573 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1574 struct mdd_object *son = md2mdd_obj(child);
1575 struct mdd_device *mdd = mdo2mdd(pobj);
1576 struct lu_attr *attr = &ma->ma_attr;
1577 struct lov_mds_md *lmm = NULL;
1578 struct thandle *handle;
1579 struct dynlock_handle *dlh;
1580 const char *name = lname->ln_name;
1581 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1582 int got_def_acl = 0;
1583 #ifdef HAVE_QUOTA_SUPPORT
1584 struct obd_device *obd = mdd->mdd_obd_dev;
1585 struct mds_obd *mds = &obd->u.mds;
1586 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1587 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1588 int quota_opc = 0, block_count = 0;
1589 int inode_pending = 0, block_pending = 0, parent_pending = 0;
1594 * Two operations have to be performed:
1596 * - an allocation of a new object (->do_create()), and
1598 * - an insertion into a parent index (->dio_insert()).
1600 * Due to locking, operation order is not important, when both are
1601 * successful, *but* error handling cases are quite different:
1603 * - if insertion is done first, and following object creation fails,
1604 * insertion has to be rolled back, but this operation might fail
1605 * also leaving us with dangling index entry.
1607 * - if creation is done first, is has to be undone if insertion
1608 * fails, leaving us with leaked space, which is neither good, nor
1611 * It seems that creation-first is simplest solution, but it is
1612 * sub-optimal in the frequent
1617 * case, because second mkdir is bound to create object, only to
1618 * destroy it immediately.
1620 * To avoid this follow local file systems that do double lookup:
1622 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1624 * 1. create (mdd_object_create_internal())
1626 * 2. insert (__mdd_index_insert(), lookup again)
1629 /* Sanity checks before big job. */
1630 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1634 #ifdef HAVE_QUOTA_SUPPORT
1635 if (mds->mds_quota) {
1636 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1638 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1642 quota_opc = FSFILT_OP_CREATE;
1643 mdd_quota_wrapper(&ma->ma_attr, qcids);
1644 mdd_quota_wrapper(la_tmp, qpids);
1645 /* get file quota for child */
1646 lquota_chkquota(mds_quota_interface_ref, obd,
1647 qcids[USRQUOTA], qcids[GRPQUOTA], 1,
1648 &inode_pending, NULL, 0, NULL, 0);
1649 switch (ma->ma_attr.la_mode & S_IFMT) {
1658 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1659 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1663 /* get block quota for child and parent */
1665 lquota_chkquota(mds_quota_interface_ref, obd,
1666 qcids[USRQUOTA], qcids[GRPQUOTA],
1668 &block_pending, NULL,
1669 LQUOTA_FLAGS_BLK, NULL, 0);
1671 lquota_chkquota(mds_quota_interface_ref, obd,
1672 qpids[USRQUOTA], qpids[GRPQUOTA], 1,
1673 &parent_pending, NULL,
1674 LQUOTA_FLAGS_BLK, NULL, 0);
1680 * No RPC inside the transaction, so OST objects should be created at
1683 if (S_ISREG(attr->la_mode)) {
1684 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1687 GOTO(out_pending, rc);
1690 if (!S_ISLNK(attr->la_mode)) {
1691 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1692 ma_acl->ma_acl = info->mti_xattr_buf;
1693 ma_acl->ma_need = MA_ACL_DEF;
1694 ma_acl->ma_valid = 0;
1696 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1697 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1698 mdd_read_unlock(env, mdd_pobj);
1701 else if (ma_acl->ma_valid & MA_ACL_DEF)
1705 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1706 handle = mdd_trans_start(env, mdd);
1708 GOTO(out_free, rc = PTR_ERR(handle));
1710 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1712 GOTO(out_trans, rc = -ENOMEM);
1714 mdd_write_lock(env, son, MOR_TGT_CHILD);
1715 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1717 mdd_write_unlock(env, son);
1723 #ifdef CONFIG_FS_POSIX_ACL
1725 struct lu_buf *acl_buf = &info->mti_buf;
1726 acl_buf->lb_buf = ma_acl->ma_acl;
1727 acl_buf->lb_len = ma_acl->ma_acl_size;
1729 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1731 mdd_write_unlock(env, son);
1734 ma->ma_attr.la_valid |= LA_MODE;
1739 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1740 son, ma, handle, spec);
1741 mdd_write_unlock(env, son);
1744 * Object has no links, so it will be destroyed when last
1745 * reference is released. (XXX not now.)
1751 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1752 name, S_ISDIR(attr->la_mode), handle,
1753 mdd_object_capa(env, mdd_pobj));
1760 /* No need mdd_lsm_sanity_check here */
1761 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1763 CERROR("error on stripe info copy %d \n", rc);
1766 if (lmm && lmm_size > 0) {
1767 /* Set Lov here, do not get lmm again later */
1768 memcpy(ma->ma_lmm, lmm, lmm_size);
1769 ma->ma_lmm_size = lmm_size;
1770 ma->ma_valid |= MA_LOV;
1773 if (S_ISLNK(attr->la_mode)) {
1774 struct md_ucred *uc = md_ucred(env);
1775 struct dt_object *dt = mdd_object_child(son);
1776 const char *target_name = spec->u.sp_symname;
1777 int sym_len = strlen(target_name);
1778 const struct lu_buf *buf;
1781 buf = mdd_buf_get_const(env, target_name, sym_len);
1782 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1783 mdd_object_capa(env, son),
1785 CFS_CAP_SYS_RESOURCE_MASK);
1790 GOTO(cleanup, rc = -EFAULT);
1794 la->la_valid = LA_CTIME | LA_MTIME;
1795 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1799 /* Return attr back. */
1800 rc = mdd_attr_get_internal_locked(env, son, ma);
1803 if (rc && created) {
1807 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1808 S_ISDIR(attr->la_mode),
1809 handle, BYPASS_CAPA);
1811 CERROR("error can not cleanup destroy %d\n",
1816 mdd_write_lock(env, son, MOR_TGT_CHILD);
1817 __mdd_ref_del(env, son, handle, 0);
1818 if (initialized && S_ISDIR(attr->la_mode))
1819 __mdd_ref_del(env, son, handle, 1);
1820 mdd_write_unlock(env, son);
1824 /* update lov_objid data, must be before transaction stop! */
1826 mdd_lov_objid_update(mdd, lmm);
1828 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1831 rc = mdd_changelog_ns_store(env, mdd,
1832 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1833 S_ISREG(attr->la_mode) ? CL_CREATE :
1834 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1835 son, mdd_pobj, NULL, lname, handle);
1836 mdd_trans_stop(env, mdd, rc, handle);
1838 /* finis lov_create stuff, free all temporary data */
1839 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1841 #ifdef HAVE_QUOTA_SUPPORT
1844 lquota_pending_commit(mds_quota_interface_ref, obd,
1845 qcids[USRQUOTA], qcids[GRPQUOTA],
1848 lquota_pending_commit(mds_quota_interface_ref, obd,
1849 qcids[USRQUOTA], qcids[GRPQUOTA],
1852 lquota_pending_commit(mds_quota_interface_ref, obd,
1853 qpids[USRQUOTA], qpids[GRPQUOTA],
1855 /* Trigger dqacq on the owner of child and parent. If failed,
1856 * the next call for lquota_chkquota will process it. */
1857 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1865 * Get locks on parents in proper order
1866 * RETURN: < 0 - error, rename_order if successful
1874 static int mdd_rename_order(const struct lu_env *env,
1875 struct mdd_device *mdd,
1876 struct mdd_object *src_pobj,
1877 struct mdd_object *tgt_pobj)
1879 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1883 if (src_pobj == tgt_pobj)
1884 RETURN(MDD_RN_SAME);
1886 /* compared the parent child relationship of src_p&tgt_p */
1887 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1889 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1892 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1905 /* has not mdd_write{read}_lock on any obj yet. */
1906 static int mdd_rename_sanity_check(const struct lu_env *env,
1907 struct mdd_object *src_pobj,
1908 struct mdd_object *tgt_pobj,
1909 struct mdd_object *sobj,
1910 struct mdd_object *tobj,
1916 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1919 /* XXX: when get here, sobj must NOT be NULL,
1920 * the other case has been processed in cml_rename
1921 * before mdd_rename and enable MDS_PERM_BYPASS. */
1923 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1927 /* XXX: when get here, "tobj == NULL" means tobj must
1928 * NOT exist (neither on remote MDS, such case has been
1929 * processed in cml_rename before mdd_rename and enable
1931 * So check may_create, but not check may_unlink. */
1933 rc = mdd_may_create(env, tgt_pobj, NULL,
1934 (src_pobj != tgt_pobj), 0);
1936 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1937 (src_pobj != tgt_pobj), 1);
1939 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1940 S_ISDIR(ma->ma_attr.la_mode))
1941 rc = __mdd_may_link(env, tgt_pobj);
1946 /* src object can be remote that is why we use only fid and type of object */
1947 static int mdd_rename(const struct lu_env *env,
1948 struct md_object *src_pobj, struct md_object *tgt_pobj,
1949 const struct lu_fid *lf, const struct lu_name *lsname,
1950 struct md_object *tobj, const struct lu_name *ltname,
1953 const char *sname = lsname->ln_name;
1954 const char *tname = ltname->ln_name;
1955 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1956 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1957 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1958 struct mdd_device *mdd = mdo2mdd(src_pobj);
1959 struct mdd_object *mdd_sobj = NULL; /* source object */
1960 struct mdd_object *mdd_tobj = NULL;
1961 struct dynlock_handle *sdlh, *tdlh;
1962 struct thandle *handle;
1963 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1967 #ifdef HAVE_QUOTA_SUPPORT
1968 struct obd_device *obd = mdd->mdd_obd_dev;
1969 struct mds_obd *mds = &obd->u.mds;
1970 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1971 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1972 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1973 int quota_opc = 0, rec_pending = 0;
1977 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1978 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1981 mdd_tobj = md2mdd_obj(tobj);
1983 #ifdef HAVE_QUOTA_SUPPORT
1984 if (mds->mds_quota) {
1985 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1987 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
1989 mdd_quota_wrapper(la_tmp, qspids);
1991 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
1995 mdd_data_get(env, mdd_tpobj, &data);
1996 quota_opc = FSFILT_OP_LINK;
1997 mdd_quota_wrapper(la_tmp, qtpids);
1998 /* get block quota for target parent */
1999 lquota_chkquota(mds_quota_interface_ref,
2000 obd, qtpids[USRQUOTA],
2001 qtpids[GRPQUOTA], 1,
2010 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2011 handle = mdd_trans_start(env, mdd);
2013 GOTO(out_pending, rc = PTR_ERR(handle));
2015 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2016 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2018 GOTO(cleanup_unlocked, rc);
2020 /* Get locks in determined order */
2021 if (rc == MDD_RN_SAME) {
2022 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2023 sname, MOR_SRC_PARENT);
2024 /* check hashes to determine do we need one lock or two */
2025 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2026 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2030 } else if (rc == MDD_RN_SRCTGT) {
2031 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2032 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2034 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2035 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2037 if (sdlh == NULL || tdlh == NULL)
2038 GOTO(cleanup, rc = -ENOMEM);
2040 mdd_sobj = mdd_object_find(env, mdd, lf);
2041 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2042 mdd_sobj, mdd_tobj, ma);
2046 /* Remove source name from source directory */
2047 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2048 mdd_object_capa(env, mdd_spobj));
2052 /* "mv dir1 dir2" needs "dir1/.." link update */
2053 if (is_dir && mdd_sobj) {
2054 rc = __mdd_index_delete(env, mdd_sobj, dotdot, is_dir, handle,
2055 mdd_object_capa(env, mdd_spobj));
2059 rc = __mdd_index_insert(env, mdd_sobj, tpobj_fid, dotdot,
2061 mdd_object_capa(env, mdd_tpobj));
2066 /* Remove target name from target directory
2067 * Here tobj can be remote one, so we do index_delete unconditionally
2068 * and -ENOENT is allowed.
2070 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2071 mdd_object_capa(env, mdd_tpobj));
2072 if (rc != 0 && rc != -ENOENT)
2075 /* Insert new fid with target name into target dir */
2076 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2077 mdd_object_capa(env, mdd_tpobj));
2081 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2082 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2084 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2086 la->la_valid = LA_CTIME;
2087 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2093 /* Remove old target object
2094 * For tobj is remote case cmm layer has processed
2095 * and set tobj to NULL then. So when tobj is NOT NULL,
2096 * it must be local one.
2098 if (tobj && mdd_object_exists(mdd_tobj)) {
2099 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2100 __mdd_ref_del(env, mdd_tobj, handle, 0);
2102 /* Remove dot reference. */
2104 __mdd_ref_del(env, mdd_tobj, handle, 1);
2106 la->la_valid = LA_CTIME;
2107 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2111 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2112 mdd_write_unlock(env, mdd_tobj);
2116 #ifdef HAVE_QUOTA_SUPPORT
2117 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2118 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2119 quota_opc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2120 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2125 la->la_valid = LA_CTIME | LA_MTIME;
2126 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2130 if (mdd_spobj != mdd_tpobj) {
2131 la->la_valid = LA_CTIME | LA_MTIME;
2132 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2136 if (rc == 0 && mdd_sobj) {
2137 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2138 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2139 mdo2fid(mdd_tpobj), ltname, handle);
2141 /* Old files might not have EA entry */
2142 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2144 mdd_write_unlock(env, mdd_sobj);
2145 /* We don't fail the transaction if the link ea can't be
2146 updated -- fid2path will use alternate lookup method. */
2152 if (likely(tdlh) && sdlh != tdlh)
2153 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2155 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2158 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2159 mdd_spobj, lf, lsname, handle);
2161 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2162 mdd_tpobj, lf, ltname, handle);
2164 mdd_trans_stop(env, mdd, rc, handle);
2166 mdd_object_put(env, mdd_sobj);
2168 #ifdef HAVE_QUOTA_SUPPORT
2169 if (mds->mds_quota) {
2171 lquota_pending_commit(mds_quota_interface_ref, obd,
2175 /* Trigger dqrel on the source owner of parent.
2176 * If failed, the next call for lquota_chkquota will
2178 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2179 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2181 /* Trigger dqrel/dqacq on the target owner of child and
2182 * parent. If failed, the next call for lquota_chkquota
2183 * will process it. */
2184 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2185 qtpids, rc, quota_opc);
2191 /** enable/disable storing of hardlink info */
2192 int mdd_linkea_enable = 1;
2193 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2194 "record hardlink info in EAs");
2196 /** Read the link EA into a temp buffer.
2197 * Uses the name_buf since it is generally large.
2198 * \retval IS_ERR err
2199 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2201 struct lu_buf *mdd_links_get(const struct lu_env *env,
2202 struct mdd_object *mdd_obj)
2205 struct lustre_capa *capa;
2206 struct link_ea_header *leh;
2209 /* First try a small buf */
2210 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2211 if (buf->lb_buf == NULL)
2212 return ERR_PTR(-ENOMEM);
2214 capa = mdd_object_capa(env, mdd_obj);
2215 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2216 if (rc == -ERANGE) {
2217 /* Buf was too small, figure out what we need. */
2220 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2223 buf = mdd_buf_alloc(env, rc);
2224 if (buf->lb_buf == NULL)
2225 return ERR_PTR(-ENOMEM);
2226 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2232 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2233 leh->leh_magic = LINK_EA_MAGIC;
2234 leh->leh_reccount = __swab32(leh->leh_reccount);
2235 leh->leh_len = __swab64(leh->leh_len);
2236 /* entries are swabbed by mdd_lee_unpack */
2238 if (leh->leh_magic != LINK_EA_MAGIC)
2239 return ERR_PTR(-EINVAL);
2240 if (leh->leh_reccount == 0)
2241 return ERR_PTR(-ENODATA);
2246 /** Pack a link_ea_entry.
2247 * All elements are stored as chars to avoid alignment issues.
2248 * Numbers are always big-endian
2249 * \param packbuf is a temp fid buffer
2250 * \retval record length
2252 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2253 const struct lu_fid *pfid, struct lu_fid* packbuf)
2258 fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2259 ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2260 strncpy(ptr, lname->ln_name, lname->ln_namelen);
2261 reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2262 sizeof(lee->lee_reclen);
2263 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2264 lee->lee_reclen[1] = reclen & 0xff;
2268 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2269 struct lu_name *lname, struct lu_fid *pfid)
2271 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2272 fid_unpack(&lee->lee_parent_fid, pfid);
2273 lname->ln_name = (char *)&lee->lee_parent_fid +
2274 lee->lee_parent_fid.fp_len;
2275 lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2276 sizeof(lee->lee_reclen);
2279 /** Add a record to the end of link ea buf */
2280 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2281 const struct lu_fid *pfid,
2282 const struct lu_name *lname)
2284 struct link_ea_header *leh;
2285 struct link_ea_entry *lee;
2288 if (lname == NULL || pfid == NULL)
2291 /* Make sure our buf is big enough for the new one */
2293 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2294 if (leh->leh_len + reclen > buf->lb_len) {
2295 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2300 lee = buf->lb_buf + leh->leh_len;
2301 reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2302 leh->leh_len += reclen;
2303 leh->leh_reccount++;
2307 /* For pathologic linkers, we don't want to spend lots of time scanning the
2308 * link ea. Limit ourseleves to something reasonable; links not in the EA
2309 * can be looked up via (slower) parent lookup.
2311 #define LINKEA_MAX_COUNT 128
2313 static int mdd_links_add(const struct lu_env *env,
2314 struct mdd_object *mdd_obj,
2315 const struct lu_fid *pfid,
2316 const struct lu_name *lname,
2317 struct thandle *handle)
2320 struct link_ea_header *leh;
2324 if (!mdd_linkea_enable)
2327 buf = mdd_links_get(env, mdd_obj);
2330 if (rc != -ENODATA) {
2331 CERROR("link_ea read failed %d "DFID"\n", rc,
2332 PFID(mdd_object_fid(mdd_obj)));
2335 /* empty EA; start one */
2336 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2337 if (buf->lb_buf == NULL)
2340 leh->leh_magic = LINK_EA_MAGIC;
2341 leh->leh_len = sizeof(struct link_ea_header);
2342 leh->leh_reccount = 0;
2346 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2349 rc = __mdd_links_add(env, buf, pfid, lname);
2354 rc = __mdd_xattr_set(env, mdd_obj,
2355 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2356 XATTR_NAME_LINK, 0, handle);
2358 CERROR("link_ea add failed %d "DFID"\n", rc,
2359 PFID(mdd_object_fid(mdd_obj)));
2361 if (buf->lb_vmalloc)
2362 /* if we vmalloced a large buffer drop it */
2368 static int mdd_links_rename(const struct lu_env *env,
2369 struct mdd_object *mdd_obj,
2370 const struct lu_fid *oldpfid,
2371 const struct lu_name *oldlname,
2372 const struct lu_fid *newpfid,
2373 const struct lu_name *newlname,
2374 struct thandle *handle)
2377 struct link_ea_header *leh;
2378 struct link_ea_entry *lee;
2379 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2380 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2386 if (!mdd_linkea_enable)
2389 if (mdd_obj->mod_flags & DEAD_OBJ)
2390 /* No more links, don't bother */
2393 buf = mdd_links_get(env, mdd_obj);
2396 CERROR("link_ea read failed %d "DFID"\n",
2397 rc, PFID(mdd_object_fid(mdd_obj)));
2401 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2403 /* Find the old record */
2404 for(count = 0; count <= leh->leh_reccount; count++) {
2405 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2406 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2407 lu_fid_eq(tmpfid, oldpfid) &&
2408 (strncmp(tmpname->ln_name, oldlname->ln_name,
2409 tmpname->ln_namelen) == 0))
2411 lee = (struct link_ea_entry *)((char *)lee + reclen);
2413 if (count > leh->leh_reccount) {
2414 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2415 oldlname->ln_namelen, oldlname->ln_name);
2416 GOTO(out, rc = -ENOENT);
2419 /* Remove the old record */
2420 leh->leh_reccount--;
2421 leh->leh_len -= reclen;
2422 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2425 /* If renaming, add the new record */
2426 if (newpfid != NULL) {
2427 /* if the add fails, we still delete the out-of-date old link */
2428 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2432 rc = __mdd_xattr_set(env, mdd_obj,
2433 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2434 XATTR_NAME_LINK, 0, handle);
2440 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2441 oldlname->ln_namelen, oldlname->ln_name, rc,
2442 PFID(mdd_object_fid(mdd_obj)));
2444 if (buf->lb_vmalloc)
2445 /* if we vmalloced a large buffer drop it */
2451 const struct md_dir_operations mdd_dir_ops = {
2452 .mdo_is_subdir = mdd_is_subdir,
2453 .mdo_lookup = mdd_lookup,
2454 .mdo_create = mdd_create,
2455 .mdo_rename = mdd_rename,
2456 .mdo_link = mdd_link,
2457 .mdo_unlink = mdd_unlink,
2458 .mdo_name_insert = mdd_name_insert,
2459 .mdo_name_remove = mdd_name_remove,
2460 .mdo_rename_tgt = mdd_rename_tgt,
2461 .mdo_create_data = mdd_create_data