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))
394 if (mdd_is_dead_obj(cobj))
398 if (!mdd_object_exists(pobj))
401 if (mdd_is_dead_obj(pobj))
405 rc = mdd_permission_internal_locked(env, pobj, NULL,
406 MAY_WRITE | MAY_EXEC,
412 if (mdd_is_append(pobj))
416 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
417 mdd_is_sticky(env, pobj, cobj))
420 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
423 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
424 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
427 if (S_ISDIR(ma->ma_attr.la_mode)) {
428 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
430 if (!S_ISDIR(mdd_object_type(cobj)))
433 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
435 } else if (S_ISDIR(mdd_object_type(cobj)))
438 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
439 rc = mdd_dir_is_empty(env, cobj);
446 * has mdd_write_lock on src already, but not on tgt yet
448 int mdd_link_sanity_check(const struct lu_env *env,
449 struct mdd_object *tgt_obj,
450 const struct lu_name *lname,
451 struct mdd_object *src_obj)
453 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
457 if (!mdd_object_exists(src_obj))
460 if (mdd_is_dead_obj(src_obj))
463 /* Local ops, no lookup before link, check filename length here. */
464 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
465 RETURN(-ENAMETOOLONG);
467 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
470 if (S_ISDIR(mdd_object_type(src_obj)))
473 LASSERT(src_obj != tgt_obj);
475 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
480 rc = __mdd_may_link(env, src_obj);
486 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
487 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
488 * (maybe too large to be represented). It is a trick to break through the
489 * "i_nlink" limitation for subdir count.
491 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
492 struct thandle *handle)
494 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
495 struct mdd_device *m = mdd_obj2mdd_dev(obj);
497 if (!mdd_is_mnlink(obj)) {
498 if (S_ISDIR(mdd_object_type(obj))) {
499 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
502 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
503 obj->mod_flags |= MNLINK_OBJ;
504 tmp_la->la_nlink = 1;
505 tmp_la->la_valid = LA_NLINK;
506 mdd_attr_set_internal(env, obj, tmp_la, handle,
511 mdo_ref_add(env, obj, handle);
515 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
516 struct thandle *handle, int is_dot)
518 if (!mdd_is_mnlink(obj) || is_dot)
519 mdo_ref_del(env, obj, handle);
522 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
523 const char *name, struct thandle *handle,
524 struct lustre_capa *capa)
526 struct dt_object *next = mdd_object_child(pobj);
530 if (dt_try_as_dir(env, next)) {
531 rc = next->do_index_ops->dio_delete(env, next,
532 (struct dt_key *)name,
540 static int __mdd_index_insert_only(const struct lu_env *env,
541 struct mdd_object *pobj,
542 const struct lu_fid *lf, const char *name,
543 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 struct md_ucred *uc = md_ucred(env);
553 rc = next->do_index_ops->dio_insert(env, next,
554 __mdd_fid_rec(env, lf),
555 (const struct dt_key *)name,
556 handle, capa, uc->mu_cap &
557 CFS_CAP_SYS_RESOURCE_MASK);
564 /* insert named index, add reference if isdir */
565 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
566 const struct lu_fid *lf, const char *name, int is_dir,
567 struct thandle *handle, struct lustre_capa *capa)
572 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
573 if (rc == 0 && is_dir) {
574 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
575 __mdd_ref_add(env, pobj, handle);
576 mdd_write_unlock(env, pobj);
581 /* delete named index, drop reference if isdir */
582 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
583 const char *name, int is_dir, struct thandle *handle,
584 struct lustre_capa *capa)
589 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
590 if (rc == 0 && is_dir) {
593 if (name != NULL && name[0] == '.' && name[1] == 0)
595 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
596 __mdd_ref_del(env, pobj, handle, is_dot);
597 mdd_write_unlock(env, pobj);
604 /** Store a namespace change changelog record
605 * If this fails, we must fail the whole transaction; we don't
606 * want the change to commit without the log entry.
607 * \param target - mdd_object of change
608 * \param parent - parent dir/object
609 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
610 * \param tname - target name string
611 * \param handle - transacion handle
613 static int mdd_changelog_ns_store(const struct lu_env *env,
614 struct mdd_device *mdd,
615 enum changelog_rec_type type,
616 struct mdd_object *target,
617 struct mdd_object *parent,
618 const struct lu_fid *tf,
619 const struct lu_name *tname,
620 struct thandle *handle)
622 const struct lu_fid *tfid;
623 const struct lu_fid *tpfid = mdo2fid(parent);
624 struct llog_changelog_rec *rec;
630 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
633 LASSERT(parent != NULL);
634 LASSERT(tname != NULL);
635 LASSERT(handle != NULL);
638 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
639 buf = mdd_buf_alloc(env, reclen);
640 if (buf->lb_buf == NULL)
642 rec = (struct llog_changelog_rec *)buf->lb_buf;
644 rec->cr_flags = CLF_VERSION;
645 rec->cr_type = (__u32)type;
646 tfid = tf ? tf : mdo2fid(target);
647 rec->cr_tfid = *tfid;
648 rec->cr_pfid = *tpfid;
649 rec->cr_namelen = tname->ln_namelen;
650 memcpy(rec->cr_name, tname->ln_name, rec->cr_namelen);
652 target->mod_cltime = cfs_time_current_64();
654 rc = mdd_changelog_llog_write(mdd, rec, handle);
656 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
657 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
664 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
665 struct md_object *src_obj, const struct lu_name *lname,
668 const char *name = lname->ln_name;
669 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
670 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
671 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
672 struct mdd_device *mdd = mdo2mdd(src_obj);
673 struct dynlock_handle *dlh;
674 struct thandle *handle;
675 #ifdef HAVE_QUOTA_SUPPORT
676 struct obd_device *obd = mdd->mdd_obd_dev;
677 struct mds_obd *mds = &obd->u.mds;
678 unsigned int qids[MAXQUOTAS] = { 0, 0 };
679 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
684 #ifdef HAVE_QUOTA_SUPPORT
685 if (mds->mds_quota) {
686 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
688 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
691 mdd_data_get(env, mdd_tobj, &data);
692 quota_opc = FSFILT_OP_LINK;
693 mdd_quota_wrapper(la_tmp, qids);
694 /* get block quota for parent */
695 lquota_chkquota(mds_quota_interface_ref, obd,
696 qids, rec_pending, 1, NULL,
697 LQUOTA_FLAGS_BLK, data, 1);
702 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
703 handle = mdd_trans_start(env, mdd);
705 GOTO(out_pending, rc = PTR_ERR(handle));
707 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
709 GOTO(out_trans, rc = -ENOMEM);
710 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
712 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
714 GOTO(out_unlock, rc);
716 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
718 mdd_object_capa(env, mdd_tobj));
720 GOTO(out_unlock, rc);
722 __mdd_ref_add(env, mdd_sobj, handle);
724 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
725 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
727 la->la_valid = LA_CTIME | LA_MTIME;
728 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
730 GOTO(out_unlock, rc);
732 la->la_valid = LA_CTIME;
733 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
735 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj), lname, handle);
739 mdd_write_unlock(env, mdd_sobj);
740 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
743 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, mdd_sobj,
744 mdd_tobj, NULL, lname, handle);
745 mdd_trans_stop(env, mdd, rc, handle);
747 #ifdef HAVE_QUOTA_SUPPORT
749 lquota_pending_commit(mds_quota_interface_ref, obd,
750 qids, rec_pending, 1);
751 /* Trigger dqacq for the parent owner. If failed,
752 * the next call for lquota_chkquota will process it. */
753 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
760 /* caller should take a lock before calling */
761 int mdd_finish_unlink(const struct lu_env *env,
762 struct mdd_object *obj, struct md_attr *ma,
769 LASSERT(mdd_write_locked(env, obj) != 0);
771 rc = mdd_iattr_get(env, obj, ma);
772 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
773 obj->mod_flags |= DEAD_OBJ;
774 /* add new orphan and the object
775 * will be deleted during mdd_close() */
776 if (obj->mod_count) {
777 rc = __mdd_orphan_add(env, obj, th);
779 CDEBUG(D_HA, "Object "DFID" is inserted into "
780 "orphan list, open count = %d\n",
781 PFID(mdd_object_fid(obj)),
784 CERROR("Object "DFID" fail to be an orphan, "
785 "open count = %d, maybe cause failed "
787 PFID(mdd_object_fid(obj)),
790 rc = mdd_object_kill(env, obj, ma);
797 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
804 * has mdd_write_lock on cobj already, but not on pobj yet
806 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
807 struct mdd_object *cobj, struct md_attr *ma)
812 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
817 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
818 struct md_object *cobj, const struct lu_name *lname,
821 const char *name = lname->ln_name;
822 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
823 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
824 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
825 struct mdd_device *mdd = mdo2mdd(pobj);
826 struct dynlock_handle *dlh;
827 struct thandle *handle;
828 #ifdef HAVE_QUOTA_SUPPORT
829 struct obd_device *obd = mdd->mdd_obd_dev;
830 struct mds_obd *mds = &obd->u.mds;
831 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
832 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
835 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
839 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
840 PFID(mdd_object_fid(mdd_cobj)));
842 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
846 handle = mdd_trans_start(env, mdd);
848 RETURN(PTR_ERR(handle));
850 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
852 GOTO(out_trans, rc = -ENOMEM);
853 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
855 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
859 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
860 mdd_object_capa(env, mdd_pobj));
864 __mdd_ref_del(env, mdd_cobj, handle, 0);
867 __mdd_ref_del(env, mdd_cobj, handle, 1);
869 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
870 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
872 la->la_valid = LA_CTIME | LA_MTIME;
873 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
877 la->la_valid = LA_CTIME;
878 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
882 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
883 #ifdef HAVE_QUOTA_SUPPORT
884 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
885 ma->ma_attr.la_nlink == 0) {
886 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
888 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
890 mdd_quota_wrapper(la_tmp, qpids);
891 if (mdd_cobj->mod_count == 0) {
892 quota_opc = FSFILT_OP_UNLINK;
893 mdd_quota_wrapper(&ma->ma_attr, qcids);
895 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
902 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
903 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
906 /* old files may not have link ea; ignore errors */
907 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
908 lname, NULL, NULL, handle);
912 mdd_write_unlock(env, mdd_cobj);
913 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
916 rc = mdd_changelog_ns_store(env, mdd,
917 is_dir ? CL_RMDIR : CL_UNLINK,
918 mdd_cobj, mdd_pobj, NULL, lname,
921 mdd_trans_stop(env, mdd, rc, handle);
922 #ifdef HAVE_QUOTA_SUPPORT
924 /* Trigger dqrel on the owner of child and parent. If failed,
925 * the next call for lquota_chkquota will process it. */
926 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
932 /* has not lock on pobj yet */
933 static int mdd_ni_sanity_check(const struct lu_env *env,
934 struct md_object *pobj,
935 const struct md_attr *ma)
937 struct mdd_object *obj = md2mdd_obj(pobj);
941 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
944 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
952 static int mdd_name_insert(const struct lu_env *env,
953 struct md_object *pobj,
954 const struct lu_name *lname,
955 const struct lu_fid *fid,
956 const struct md_attr *ma)
958 const char *name = lname->ln_name;
959 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
960 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
961 struct mdd_device *mdd = mdo2mdd(pobj);
962 struct dynlock_handle *dlh;
963 struct thandle *handle;
964 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
965 #ifdef HAVE_QUOTA_SUPPORT
966 struct md_ucred *uc = md_ucred(env);
967 struct obd_device *obd = mdd->mdd_obd_dev;
968 struct mds_obd *mds = &obd->u.mds;
969 unsigned int qids[MAXQUOTAS] = { 0, 0 };
970 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
971 cfs_cap_t save = uc->mu_cap;
976 #ifdef HAVE_QUOTA_SUPPORT
977 if (mds->mds_quota) {
978 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
979 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
981 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
984 mdd_data_get(env, mdd_obj, &data);
985 quota_opc = FSFILT_OP_LINK;
986 mdd_quota_wrapper(la_tmp, qids);
987 /* get block quota for parent */
988 lquota_chkquota(mds_quota_interface_ref, obd,
989 qids, rec_pending, 1, NULL,
990 LQUOTA_FLAGS_BLK, data, 1);
993 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
997 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
998 handle = mdd_trans_start(env, mdo2mdd(pobj));
1000 GOTO(out_pending, rc = PTR_ERR(handle));
1002 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1004 GOTO(out_trans, rc = -ENOMEM);
1006 rc = mdd_ni_sanity_check(env, pobj, ma);
1008 GOTO(out_unlock, rc);
1010 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1011 handle, BYPASS_CAPA);
1013 GOTO(out_unlock, rc);
1016 * For some case, no need update obj's ctime (LA_CTIME is not set),
1018 * For other cases, update obj's ctime (LA_CTIME is set),
1021 if (ma->ma_attr.la_valid & LA_CTIME) {
1022 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1023 la->la_valid = LA_CTIME | LA_MTIME;
1024 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1029 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1031 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1033 #ifdef HAVE_QUOTA_SUPPORT
1034 if (mds->mds_quota) {
1036 lquota_pending_commit(mds_quota_interface_ref,
1037 obd, qids, rec_pending, 1);
1038 /* Trigger dqacq for the parent owner. If failed,
1039 * the next call for lquota_chkquota will process it*/
1040 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1050 /* has not lock on pobj yet */
1051 static int mdd_nr_sanity_check(const struct lu_env *env,
1052 struct md_object *pobj,
1053 const struct md_attr *ma)
1055 struct mdd_object *obj = md2mdd_obj(pobj);
1059 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1062 rc = mdd_may_unlink(env, obj, ma);
1068 * Partial operation.
1070 static int mdd_name_remove(const struct lu_env *env,
1071 struct md_object *pobj,
1072 const struct lu_name *lname,
1073 const struct md_attr *ma)
1075 const char *name = lname->ln_name;
1076 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1077 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1078 struct mdd_device *mdd = mdo2mdd(pobj);
1079 struct dynlock_handle *dlh;
1080 struct thandle *handle;
1081 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1082 #ifdef HAVE_QUOTA_SUPPORT
1083 struct obd_device *obd = mdd->mdd_obd_dev;
1084 struct mds_obd *mds = &obd->u.mds;
1085 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1091 #ifdef HAVE_QUOTA_SUPPORT
1092 if (mds->mds_quota) {
1093 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1095 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1097 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1098 mdd_quota_wrapper(la_tmp, qids);
1102 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
1103 handle = mdd_trans_start(env, mdd);
1105 GOTO(out_pending, rc = PTR_ERR(handle));
1107 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1109 GOTO(out_trans, rc = -ENOMEM);
1111 rc = mdd_nr_sanity_check(env, pobj, ma);
1113 GOTO(out_unlock, rc);
1115 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1116 handle, BYPASS_CAPA);
1118 GOTO(out_unlock, rc);
1121 * For some case, no need update obj's ctime (LA_CTIME is not set),
1123 * For other cases, update obj's ctime (LA_CTIME is set),
1126 if (ma->ma_attr.la_valid & LA_CTIME) {
1127 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1128 la->la_valid = LA_CTIME | LA_MTIME;
1129 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1134 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1136 mdd_trans_stop(env, mdd, rc, handle);
1138 #ifdef HAVE_QUOTA_SUPPORT
1139 /* Trigger dqrel for the parent owner.
1140 * If failed, the next call for lquota_chkquota will process it. */
1142 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1150 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1152 static int mdd_rt_sanity_check(const struct lu_env *env,
1153 struct mdd_object *tgt_pobj,
1154 struct mdd_object *tobj,
1160 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1163 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1164 * exist. In fact, tobj must exist, otherwise the call trace will be:
1165 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1166 * When get here, tobj must be NOT NULL, the other case has been
1167 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1169 * So check may_delete, but not check nlink of tgt_pobj. */
1171 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1176 /* Partial rename op on slave MDD */
1177 static int mdd_rename_tgt(const struct lu_env *env,
1178 struct md_object *pobj, struct md_object *tobj,
1179 const struct lu_fid *lf, const struct lu_name *lname,
1182 const char *name = lname->ln_name;
1183 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1184 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1185 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1186 struct mdd_device *mdd = mdo2mdd(pobj);
1187 struct dynlock_handle *dlh;
1188 struct thandle *handle;
1189 #ifdef HAVE_QUOTA_SUPPORT
1190 struct obd_device *obd = mdd->mdd_obd_dev;
1191 struct mds_obd *mds = &obd->u.mds;
1192 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1193 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1194 int quota_copc = 0, quota_popc = 0;
1195 int rec_pending[MAXQUOTAS] = { 0, 0 };
1200 #ifdef HAVE_QUOTA_SUPPORT
1201 if (mds->mds_quota && !tobj) {
1202 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1204 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1207 mdd_data_get(env, mdd_tpobj, &data);
1208 quota_popc = FSFILT_OP_LINK;
1209 mdd_quota_wrapper(la_tmp, qpids);
1210 /* get block quota for target parent */
1211 lquota_chkquota(mds_quota_interface_ref, obd,
1212 qpids, rec_pending, 1, NULL,
1213 LQUOTA_FLAGS_BLK, data, 1);
1217 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1218 handle = mdd_trans_start(env, mdd);
1220 GOTO(out_pending, rc = PTR_ERR(handle));
1222 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1224 GOTO(out_trans, rc = -ENOMEM);
1226 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1228 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1233 * If rename_tgt is called then we should just re-insert name with
1234 * correct fid, no need to dec/inc parent nlink if obj is dir.
1236 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1240 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1245 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1246 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1248 la->la_valid = LA_CTIME | LA_MTIME;
1249 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1254 * For tobj is remote case cmm layer has processed
1255 * and pass NULL tobj to here. So when tobj is NOT NULL,
1256 * it must be local one.
1258 if (tobj && mdd_object_exists(mdd_tobj)) {
1259 __mdd_ref_del(env, mdd_tobj, handle, 0);
1261 /* Remove dot reference. */
1262 if (S_ISDIR(ma->ma_attr.la_mode))
1263 __mdd_ref_del(env, mdd_tobj, handle, 1);
1265 la->la_valid = LA_CTIME;
1266 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1270 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1274 #ifdef HAVE_QUOTA_SUPPORT
1275 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1276 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1277 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1278 mdd_quota_wrapper(&ma->ma_attr, qcids);
1285 mdd_write_unlock(env, mdd_tobj);
1286 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1289 /* Bare EXT record with no RENAME in front of it signifies
1290 a partial slave op */
1291 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
1292 mdd_tpobj, NULL, lname, handle);
1294 mdd_trans_stop(env, mdd, rc, handle);
1296 #ifdef HAVE_QUOTA_SUPPORT
1297 if (mds->mds_quota) {
1299 lquota_pending_commit(mds_quota_interface_ref, obd,
1300 qpids, rec_pending, 1);
1303 /* Trigger dqrel on the target owner of child.
1304 * If failed, the next call for lquota_chkquota
1305 * will process it. */
1306 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1314 * The permission has been checked when obj created, no need check again.
1316 static int mdd_cd_sanity_check(const struct lu_env *env,
1317 struct mdd_object *obj)
1322 if (!obj || mdd_is_dead_obj(obj))
1329 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1330 struct md_object *cobj, const struct md_op_spec *spec,
1333 struct mdd_device *mdd = mdo2mdd(cobj);
1334 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1335 struct mdd_object *son = md2mdd_obj(cobj);
1336 struct lu_attr *attr = &ma->ma_attr;
1337 struct lov_mds_md *lmm = NULL;
1339 struct thandle *handle;
1343 rc = mdd_cd_sanity_check(env, son);
1347 if (!md_should_create(spec->sp_cr_flags))
1349 lmm_size = ma->ma_lmm_size;
1350 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1355 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1356 handle = mdd_trans_start(env, mdd);
1358 GOTO(out_free, rc = PTR_ERR(handle));
1361 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1362 * Should this be fixed?
1365 /* Replay creates has objects already */
1367 if (spec->no_create) {
1368 CDEBUG(D_INFO, "we already have lov ea\n");
1369 rc = mdd_lov_set_md(env, mdd_pobj, son,
1370 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1371 spec->u.sp_ea.eadatalen, handle, 0);
1374 /* No need mdd_lsm_sanity_check here */
1375 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1376 lmm_size, handle, 0);
1379 rc = mdd_attr_get_internal_locked(env, son, ma);
1381 /* update lov_objid data, must be before transaction stop! */
1383 mdd_lov_objid_update(mdd, lmm);
1385 mdd_trans_stop(env, mdd, rc, handle);
1387 /* Finish mdd_lov_create() stuff. */
1388 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1392 /* Get fid from name and parent */
1394 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1395 const struct lu_name *lname, struct lu_fid* fid, int mask)
1397 const char *name = lname->ln_name;
1398 const struct dt_key *key = (const struct dt_key *)name;
1399 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1400 struct mdd_device *m = mdo2mdd(pobj);
1401 struct dt_object *dir = mdd_object_child(mdd_obj);
1402 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
1406 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1409 rc = mdd_object_exists(mdd_obj);
1410 if (unlikely(rc == 0))
1412 else if (unlikely(rc < 0)) {
1413 CERROR("Object "DFID" locates on remote server\n",
1414 PFID(mdo2fid(mdd_obj)));
1418 /* The common filename length check. */
1419 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1420 RETURN(-ENAMETOOLONG);
1422 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1427 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1428 dt_try_as_dir(env, dir))) {
1430 rc = dir->do_index_ops->dio_lookup(env, dir,
1431 (struct dt_rec *)pack, key,
1432 mdd_object_capa(env, mdd_obj));
1434 rc = fid_unpack(pack, fid);
1443 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1444 const struct lu_name *lname, struct mdd_object *child,
1445 struct md_attr *ma, struct thandle *handle,
1446 const struct md_op_spec *spec)
1452 * Update attributes for child.
1455 * (1) the valid bits should be converted between Lustre and Linux;
1456 * (2) maybe, the child attributes should be set in OSD when creation.
1459 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1463 if (S_ISDIR(ma->ma_attr.la_mode)) {
1464 /* Add "." and ".." for newly created dir */
1465 __mdd_ref_add(env, child, handle);
1466 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1467 dot, handle, BYPASS_CAPA);
1469 rc = __mdd_index_insert_only(env, child, pfid,
1475 rc2 = __mdd_index_delete(env, child, dot, 1,
1476 handle, BYPASS_CAPA);
1478 CERROR("Failure to cleanup after dotdot"
1479 " creation: %d (%d)\n", rc2, rc);
1484 mdd_links_add(env, child, pfid, lname, handle);
1489 /* has not lock on pobj yet */
1490 static int mdd_create_sanity_check(const struct lu_env *env,
1491 struct md_object *pobj,
1492 const struct lu_name *lname,
1494 struct md_op_spec *spec)
1496 struct mdd_thread_info *info = mdd_env_info(env);
1497 struct lu_attr *la = &info->mti_la;
1498 struct lu_fid *fid = &info->mti_fid;
1499 struct mdd_object *obj = md2mdd_obj(pobj);
1500 struct mdd_device *m = mdo2mdd(pobj);
1501 int lookup = spec->sp_cr_lookup;
1506 if (mdd_is_dead_obj(obj))
1510 * In some cases this lookup is not needed - we know before if name
1511 * exists or not because MDT performs lookup for it.
1512 * name length check is done in lookup.
1516 * Check if the name already exist, though it will be checked in
1517 * _index_insert also, for avoiding rolling back if exists
1520 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1521 MAY_WRITE | MAY_EXEC);
1523 RETURN(rc ? : -EEXIST);
1526 * Check WRITE permission for the parent.
1527 * EXEC permission have been checked
1528 * when lookup before create already.
1530 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1537 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1541 if (la->la_mode & S_ISGID) {
1542 ma->ma_attr.la_gid = la->la_gid;
1543 if (S_ISDIR(ma->ma_attr.la_mode)) {
1544 ma->ma_attr.la_mode |= S_ISGID;
1545 ma->ma_attr.la_valid |= LA_MODE;
1549 switch (ma->ma_attr.la_mode & S_IFMT) {
1551 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1553 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1554 RETURN(-ENAMETOOLONG);
1574 * Create object and insert it into namespace.
1576 static int mdd_create(const struct lu_env *env,
1577 struct md_object *pobj,
1578 const struct lu_name *lname,
1579 struct md_object *child,
1580 struct md_op_spec *spec,
1583 struct mdd_thread_info *info = mdd_env_info(env);
1584 struct lu_attr *la = &info->mti_la_for_fix;
1585 struct md_attr *ma_acl = &info->mti_ma;
1586 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1587 struct mdd_object *son = md2mdd_obj(child);
1588 struct mdd_device *mdd = mdo2mdd(pobj);
1589 struct lu_attr *attr = &ma->ma_attr;
1590 struct lov_mds_md *lmm = NULL;
1591 struct thandle *handle;
1592 struct dynlock_handle *dlh;
1593 const char *name = lname->ln_name;
1594 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1595 int got_def_acl = 0;
1596 #ifdef HAVE_QUOTA_SUPPORT
1597 struct obd_device *obd = mdd->mdd_obd_dev;
1598 struct mds_obd *mds = &obd->u.mds;
1599 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1600 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1601 int quota_opc = 0, block_count = 0;
1602 int inode_pending[MAXQUOTAS] = { 0, 0 };
1603 int block_pending[MAXQUOTAS] = { 0, 0 };
1604 int parent_pending[MAXQUOTAS] = { 0, 0 };
1609 * Two operations have to be performed:
1611 * - an allocation of a new object (->do_create()), and
1613 * - an insertion into a parent index (->dio_insert()).
1615 * Due to locking, operation order is not important, when both are
1616 * successful, *but* error handling cases are quite different:
1618 * - if insertion is done first, and following object creation fails,
1619 * insertion has to be rolled back, but this operation might fail
1620 * also leaving us with dangling index entry.
1622 * - if creation is done first, is has to be undone if insertion
1623 * fails, leaving us with leaked space, which is neither good, nor
1626 * It seems that creation-first is simplest solution, but it is
1627 * sub-optimal in the frequent
1632 * case, because second mkdir is bound to create object, only to
1633 * destroy it immediately.
1635 * To avoid this follow local file systems that do double lookup:
1637 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1639 * 1. create (mdd_object_create_internal())
1641 * 2. insert (__mdd_index_insert(), lookup again)
1644 /* Sanity checks before big job. */
1645 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1649 #ifdef HAVE_QUOTA_SUPPORT
1650 if (mds->mds_quota) {
1651 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1653 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1657 quota_opc = FSFILT_OP_CREATE;
1658 mdd_quota_wrapper(&ma->ma_attr, qcids);
1659 mdd_quota_wrapper(la_tmp, qpids);
1660 /* get file quota for child */
1661 lquota_chkquota(mds_quota_interface_ref, obd, qcids,
1662 inode_pending, 1, NULL, 0, NULL, 0);
1663 switch (ma->ma_attr.la_mode & S_IFMT) {
1672 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1673 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1677 /* get block quota for child and parent */
1679 lquota_chkquota(mds_quota_interface_ref, obd,
1680 qcids, block_pending,
1682 LQUOTA_FLAGS_BLK, NULL, 0);
1684 lquota_chkquota(mds_quota_interface_ref, obd,
1685 qpids, parent_pending, 1, NULL,
1686 LQUOTA_FLAGS_BLK, NULL, 0);
1692 * No RPC inside the transaction, so OST objects should be created at
1695 if (S_ISREG(attr->la_mode)) {
1696 lmm_size = ma->ma_lmm_size;
1697 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1700 GOTO(out_pending, rc);
1703 if (!S_ISLNK(attr->la_mode)) {
1704 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1705 ma_acl->ma_acl = info->mti_xattr_buf;
1706 ma_acl->ma_need = MA_ACL_DEF;
1707 ma_acl->ma_valid = 0;
1709 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1710 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1711 mdd_read_unlock(env, mdd_pobj);
1714 else if (ma_acl->ma_valid & MA_ACL_DEF)
1718 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1719 handle = mdd_trans_start(env, mdd);
1721 GOTO(out_free, rc = PTR_ERR(handle));
1723 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1725 GOTO(out_trans, rc = -ENOMEM);
1727 mdd_write_lock(env, son, MOR_TGT_CHILD);
1728 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1730 mdd_write_unlock(env, son);
1736 #ifdef CONFIG_FS_POSIX_ACL
1738 struct lu_buf *acl_buf = &info->mti_buf;
1739 acl_buf->lb_buf = ma_acl->ma_acl;
1740 acl_buf->lb_len = ma_acl->ma_acl_size;
1742 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1744 mdd_write_unlock(env, son);
1747 ma->ma_attr.la_valid |= LA_MODE;
1752 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1753 son, ma, handle, spec);
1754 mdd_write_unlock(env, son);
1757 * Object has no links, so it will be destroyed when last
1758 * reference is released. (XXX not now.)
1764 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1765 name, S_ISDIR(attr->la_mode), handle,
1766 mdd_object_capa(env, mdd_pobj));
1773 /* No need mdd_lsm_sanity_check here */
1774 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1776 CERROR("error on stripe info copy %d \n", rc);
1779 if (lmm && lmm_size > 0) {
1780 /* Set Lov here, do not get lmm again later */
1781 memcpy(ma->ma_lmm, lmm, lmm_size);
1782 ma->ma_lmm_size = lmm_size;
1783 ma->ma_valid |= MA_LOV;
1786 if (S_ISLNK(attr->la_mode)) {
1787 struct md_ucred *uc = md_ucred(env);
1788 struct dt_object *dt = mdd_object_child(son);
1789 const char *target_name = spec->u.sp_symname;
1790 int sym_len = strlen(target_name);
1791 const struct lu_buf *buf;
1794 buf = mdd_buf_get_const(env, target_name, sym_len);
1795 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1796 mdd_object_capa(env, son),
1798 CFS_CAP_SYS_RESOURCE_MASK);
1803 GOTO(cleanup, rc = -EFAULT);
1807 la->la_valid = LA_CTIME | LA_MTIME;
1808 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1812 /* Return attr back. */
1813 rc = mdd_attr_get_internal_locked(env, son, ma);
1816 if (rc && created) {
1820 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1821 S_ISDIR(attr->la_mode),
1822 handle, BYPASS_CAPA);
1824 CERROR("error can not cleanup destroy %d\n",
1829 mdd_write_lock(env, son, MOR_TGT_CHILD);
1830 __mdd_ref_del(env, son, handle, 0);
1831 if (initialized && S_ISDIR(attr->la_mode))
1832 __mdd_ref_del(env, son, handle, 1);
1833 mdd_write_unlock(env, son);
1837 /* update lov_objid data, must be before transaction stop! */
1839 mdd_lov_objid_update(mdd, lmm);
1841 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1844 rc = mdd_changelog_ns_store(env, mdd,
1845 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1846 S_ISREG(attr->la_mode) ? CL_CREATE :
1847 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1848 son, mdd_pobj, NULL, lname, handle);
1849 mdd_trans_stop(env, mdd, rc, handle);
1851 /* finis lov_create stuff, free all temporary data */
1852 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1854 #ifdef HAVE_QUOTA_SUPPORT
1856 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1858 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1860 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1862 /* Trigger dqacq on the owner of child and parent. If failed,
1863 * the next call for lquota_chkquota will process it. */
1864 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1872 * Get locks on parents in proper order
1873 * RETURN: < 0 - error, rename_order if successful
1881 static int mdd_rename_order(const struct lu_env *env,
1882 struct mdd_device *mdd,
1883 struct mdd_object *src_pobj,
1884 struct mdd_object *tgt_pobj)
1886 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1890 if (src_pobj == tgt_pobj)
1891 RETURN(MDD_RN_SAME);
1893 /* compared the parent child relationship of src_p&tgt_p */
1894 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1896 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1899 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1912 /* has not mdd_write{read}_lock on any obj yet. */
1913 static int mdd_rename_sanity_check(const struct lu_env *env,
1914 struct mdd_object *src_pobj,
1915 struct mdd_object *tgt_pobj,
1916 struct mdd_object *sobj,
1917 struct mdd_object *tobj,
1923 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1926 /* XXX: when get here, sobj must NOT be NULL,
1927 * the other case has been processed in cml_rename
1928 * before mdd_rename and enable MDS_PERM_BYPASS. */
1931 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1935 /* XXX: when get here, "tobj == NULL" means tobj must
1936 * NOT exist (neither on remote MDS, such case has been
1937 * processed in cml_rename before mdd_rename and enable
1939 * So check may_create, but not check may_unlink. */
1941 rc = mdd_may_create(env, tgt_pobj, NULL,
1942 (src_pobj != tgt_pobj), 0);
1944 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1945 (src_pobj != tgt_pobj), 1);
1947 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1948 S_ISDIR(ma->ma_attr.la_mode))
1949 rc = __mdd_may_link(env, tgt_pobj);
1954 /* src object can be remote that is why we use only fid and type of object */
1955 static int mdd_rename(const struct lu_env *env,
1956 struct md_object *src_pobj, struct md_object *tgt_pobj,
1957 const struct lu_fid *lf, const struct lu_name *lsname,
1958 struct md_object *tobj, const struct lu_name *ltname,
1961 const char *sname = lsname->ln_name;
1962 const char *tname = ltname->ln_name;
1963 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1964 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1965 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1966 struct mdd_device *mdd = mdo2mdd(src_pobj);
1967 struct mdd_object *mdd_sobj = NULL; /* source object */
1968 struct mdd_object *mdd_tobj = NULL;
1969 struct dynlock_handle *sdlh, *tdlh;
1970 struct thandle *handle;
1971 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1972 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1976 #ifdef HAVE_QUOTA_SUPPORT
1977 struct obd_device *obd = mdd->mdd_obd_dev;
1978 struct mds_obd *mds = &obd->u.mds;
1979 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1980 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1981 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1982 int quota_copc = 0, quota_popc = 0;
1983 int rec_pending[MAXQUOTAS] = { 0, 0 };
1987 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1988 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1991 mdd_tobj = md2mdd_obj(tobj);
1993 #ifdef HAVE_QUOTA_SUPPORT
1994 if (mds->mds_quota) {
1995 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1997 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
1999 mdd_quota_wrapper(la_tmp, qspids);
2001 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2005 mdd_data_get(env, mdd_tpobj, &data);
2006 quota_popc = FSFILT_OP_LINK;
2007 mdd_quota_wrapper(la_tmp, qtpids);
2008 /* get block quota for target parent */
2009 lquota_chkquota(mds_quota_interface_ref,
2011 rec_pending, 1, NULL,
2019 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2020 handle = mdd_trans_start(env, mdd);
2022 GOTO(out_pending, rc = PTR_ERR(handle));
2024 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2025 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2027 GOTO(cleanup_unlocked, rc);
2029 /* Get locks in determined order */
2030 if (rc == MDD_RN_SAME) {
2031 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2032 sname, MOR_SRC_PARENT);
2033 /* check hashes to determine do we need one lock or two */
2034 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2035 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2039 } else if (rc == MDD_RN_SRCTGT) {
2040 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2041 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2043 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2044 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2046 if (sdlh == NULL || tdlh == NULL)
2047 GOTO(cleanup, rc = -ENOMEM);
2049 mdd_sobj = mdd_object_find(env, mdd, lf);
2050 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2051 mdd_sobj, mdd_tobj, ma);
2055 /* Remove source name from source directory */
2056 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2057 mdd_object_capa(env, mdd_spobj));
2061 /* "mv dir1 dir2" needs "dir1/.." link update */
2062 if (is_dir && mdd_sobj) {
2063 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2064 mdd_object_capa(env, mdd_sobj));
2066 GOTO(fixup_spobj2, rc);
2068 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2069 handle, mdd_object_capa(env, mdd_sobj));
2071 GOTO(fixup_spobj, rc);
2075 /* Remove target name from target directory
2076 * Here tobj can be remote one, so we do index_delete unconditionally
2077 * and -ENOENT is allowed.
2079 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2080 mdd_object_capa(env, mdd_tpobj));
2083 /* tname might been renamed to something else */
2084 GOTO(fixup_spobj, rc);
2087 GOTO(fixup_spobj, rc);
2090 /* Insert new fid with target name into target dir */
2091 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2092 mdd_object_capa(env, mdd_tpobj));
2094 GOTO(fixup_tpobj, rc);
2096 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2097 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2099 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2101 la->la_valid = LA_CTIME;
2102 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2105 GOTO(fixup_tpobj, rc);
2108 /* Remove old target object
2109 * For tobj is remote case cmm layer has processed
2110 * and set tobj to NULL then. So when tobj is NOT NULL,
2111 * it must be local one.
2113 if (tobj && mdd_object_exists(mdd_tobj)) {
2114 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2115 if (mdd_is_dead_obj(mdd_tobj)) {
2116 mdd_write_unlock(env, mdd_tobj);
2117 /* shld not be dead, something is wrong */
2118 CERROR("tobj is dead, something is wrong\n");
2122 __mdd_ref_del(env, mdd_tobj, handle, 0);
2124 /* Remove dot reference. */
2126 __mdd_ref_del(env, mdd_tobj, handle, 1);
2128 la->la_valid = LA_CTIME;
2129 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2131 GOTO(fixup_tpobj, rc);
2133 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2134 mdd_write_unlock(env, mdd_tobj);
2136 GOTO(fixup_tpobj, rc);
2138 #ifdef HAVE_QUOTA_SUPPORT
2139 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2140 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2141 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2142 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2147 la->la_valid = LA_CTIME | LA_MTIME;
2148 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2150 GOTO(fixup_tpobj, rc);
2152 if (mdd_spobj != mdd_tpobj) {
2153 la->la_valid = LA_CTIME | LA_MTIME;
2154 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2158 if (rc == 0 && mdd_sobj) {
2159 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2160 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2161 mdo2fid(mdd_tpobj), ltname, handle);
2163 /* Old files might not have EA entry */
2164 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2166 mdd_write_unlock(env, mdd_sobj);
2167 /* We don't fail the transaction if the link ea can't be
2168 updated -- fid2path will use alternate lookup method. */
2176 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2179 CWARN("tp obj fix error %d\n",rc2);
2181 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2182 !mdd_is_dead_obj(mdd_tobj)) {
2183 rc2 = __mdd_index_insert(env, mdd_tpobj,
2184 mdo2fid(mdd_tobj), tname,
2189 CWARN("tp obj fix error %d\n",rc2);
2194 if (rc && is_dir && mdd_sobj) {
2195 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2199 CWARN("sp obj dotdot delete error %d\n",rc2);
2202 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2203 dotdot, handle, BYPASS_CAPA);
2205 CWARN("sp obj dotdot insert error %d\n",rc2);
2210 rc2 = __mdd_index_insert(env, mdd_spobj,
2211 lf, sname, is_dir, handle, BYPASS_CAPA);
2213 CWARN("sp obj fix error %d\n",rc2);
2216 if (likely(tdlh) && sdlh != tdlh)
2217 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2219 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2222 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2223 mdd_spobj, lf, lsname, handle);
2225 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2226 mdd_tpobj, lf, ltname, handle);
2228 mdd_trans_stop(env, mdd, rc, handle);
2230 mdd_object_put(env, mdd_sobj);
2232 #ifdef HAVE_QUOTA_SUPPORT
2233 if (mds->mds_quota) {
2235 lquota_pending_commit(mds_quota_interface_ref, obd,
2236 qtpids, rec_pending, 1);
2239 /* Trigger dqrel on the source owner of parent.
2240 * If failed, the next call for lquota_chkquota will
2242 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2243 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2245 /* Trigger dqrel on the target owner of child.
2246 * If failed, the next call for lquota_chkquota
2247 * will process it. */
2248 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2249 qtpids, rc, quota_copc);
2256 /** enable/disable storing of hardlink info */
2257 int mdd_linkea_enable = 1;
2258 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2259 "record hardlink info in EAs");
2261 /** Read the link EA into a temp buffer.
2262 * Uses the name_buf since it is generally large.
2263 * \retval IS_ERR err
2264 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2266 struct lu_buf *mdd_links_get(const struct lu_env *env,
2267 struct mdd_object *mdd_obj)
2270 struct lustre_capa *capa;
2271 struct link_ea_header *leh;
2274 /* First try a small buf */
2275 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2276 if (buf->lb_buf == NULL)
2277 return ERR_PTR(-ENOMEM);
2279 capa = mdd_object_capa(env, mdd_obj);
2280 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2281 if (rc == -ERANGE) {
2282 /* Buf was too small, figure out what we need. */
2285 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2288 buf = mdd_buf_alloc(env, rc);
2289 if (buf->lb_buf == NULL)
2290 return ERR_PTR(-ENOMEM);
2291 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2297 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2298 leh->leh_magic = LINK_EA_MAGIC;
2299 leh->leh_reccount = __swab32(leh->leh_reccount);
2300 leh->leh_len = __swab64(leh->leh_len);
2301 /* entries are swabbed by mdd_lee_unpack */
2303 if (leh->leh_magic != LINK_EA_MAGIC)
2304 return ERR_PTR(-EINVAL);
2305 if (leh->leh_reccount == 0)
2306 return ERR_PTR(-ENODATA);
2311 /** Pack a link_ea_entry.
2312 * All elements are stored as chars to avoid alignment issues.
2313 * Numbers are always big-endian
2314 * \param packbuf is a temp fid buffer
2315 * \retval record length
2317 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2318 const struct lu_fid *pfid, struct lu_fid* packbuf)
2323 fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2324 ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2325 strncpy(ptr, lname->ln_name, lname->ln_namelen);
2326 reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2327 sizeof(lee->lee_reclen);
2328 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2329 lee->lee_reclen[1] = reclen & 0xff;
2333 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2334 struct lu_name *lname, struct lu_fid *pfid)
2336 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2337 fid_unpack(&lee->lee_parent_fid, pfid);
2338 lname->ln_name = (char *)&lee->lee_parent_fid +
2339 lee->lee_parent_fid.fp_len;
2340 lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2341 sizeof(lee->lee_reclen);
2344 /** Add a record to the end of link ea buf */
2345 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2346 const struct lu_fid *pfid,
2347 const struct lu_name *lname)
2349 struct link_ea_header *leh;
2350 struct link_ea_entry *lee;
2353 if (lname == NULL || pfid == NULL)
2356 /* Make sure our buf is big enough for the new one */
2358 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2359 if (leh->leh_len + reclen > buf->lb_len) {
2360 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2365 lee = buf->lb_buf + leh->leh_len;
2366 reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2367 leh->leh_len += reclen;
2368 leh->leh_reccount++;
2372 /* For pathologic linkers, we don't want to spend lots of time scanning the
2373 * link ea. Limit ourseleves to something reasonable; links not in the EA
2374 * can be looked up via (slower) parent lookup.
2376 #define LINKEA_MAX_COUNT 128
2378 static int mdd_links_add(const struct lu_env *env,
2379 struct mdd_object *mdd_obj,
2380 const struct lu_fid *pfid,
2381 const struct lu_name *lname,
2382 struct thandle *handle)
2385 struct link_ea_header *leh;
2389 if (!mdd_linkea_enable)
2392 buf = mdd_links_get(env, mdd_obj);
2395 if (rc != -ENODATA) {
2396 CERROR("link_ea read failed %d "DFID"\n", rc,
2397 PFID(mdd_object_fid(mdd_obj)));
2400 /* empty EA; start one */
2401 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2402 if (buf->lb_buf == NULL)
2405 leh->leh_magic = LINK_EA_MAGIC;
2406 leh->leh_len = sizeof(struct link_ea_header);
2407 leh->leh_reccount = 0;
2411 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2414 rc = __mdd_links_add(env, buf, pfid, lname);
2419 rc = __mdd_xattr_set(env, mdd_obj,
2420 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2421 XATTR_NAME_LINK, 0, handle);
2423 CERROR("link_ea add failed %d "DFID"\n", rc,
2424 PFID(mdd_object_fid(mdd_obj)));
2426 if (buf->lb_vmalloc)
2427 /* if we vmalloced a large buffer drop it */
2433 static int mdd_links_rename(const struct lu_env *env,
2434 struct mdd_object *mdd_obj,
2435 const struct lu_fid *oldpfid,
2436 const struct lu_name *oldlname,
2437 const struct lu_fid *newpfid,
2438 const struct lu_name *newlname,
2439 struct thandle *handle)
2442 struct link_ea_header *leh;
2443 struct link_ea_entry *lee;
2444 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2445 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2451 if (!mdd_linkea_enable)
2454 if (mdd_obj->mod_flags & DEAD_OBJ)
2455 /* No more links, don't bother */
2458 buf = mdd_links_get(env, mdd_obj);
2461 CERROR("link_ea read failed %d "DFID"\n",
2462 rc, PFID(mdd_object_fid(mdd_obj)));
2466 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2468 /* Find the old record */
2469 for(count = 0; count <= leh->leh_reccount; count++) {
2470 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2471 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2472 lu_fid_eq(tmpfid, oldpfid) &&
2473 (strncmp(tmpname->ln_name, oldlname->ln_name,
2474 tmpname->ln_namelen) == 0))
2476 lee = (struct link_ea_entry *)((char *)lee + reclen);
2478 if (count > leh->leh_reccount) {
2479 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2480 oldlname->ln_namelen, oldlname->ln_name);
2481 GOTO(out, rc = -ENOENT);
2484 /* Remove the old record */
2485 leh->leh_reccount--;
2486 leh->leh_len -= reclen;
2487 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2490 /* If renaming, add the new record */
2491 if (newpfid != NULL) {
2492 /* if the add fails, we still delete the out-of-date old link */
2493 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2497 rc = __mdd_xattr_set(env, mdd_obj,
2498 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2499 XATTR_NAME_LINK, 0, handle);
2505 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2506 oldlname->ln_namelen, oldlname->ln_name, rc,
2507 PFID(mdd_object_fid(mdd_obj)));
2509 if (buf->lb_vmalloc)
2510 /* if we vmalloced a large buffer drop it */
2516 const struct md_dir_operations mdd_dir_ops = {
2517 .mdo_is_subdir = mdd_is_subdir,
2518 .mdo_lookup = mdd_lookup,
2519 .mdo_create = mdd_create,
2520 .mdo_rename = mdd_rename,
2521 .mdo_link = mdd_link,
2522 .mdo_unlink = mdd_unlink,
2523 .mdo_name_insert = mdd_name_insert,
2524 .mdo_name_remove = mdd_name_remove,
2525 .mdo_rename_tgt = mdd_rename_tgt,
2526 .mdo_create_data = mdd_create_data