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 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
200 const struct lu_fid *fid, struct lu_fid *sfid)
202 struct mdd_device *mdd = mdo2mdd(mo);
206 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
209 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
213 } else if (rc == 1) {
214 /* found @fid is parent */
222 * Check that @dir contains no entries except (possibly) dot and dotdot.
227 * -ENOTDIR not a directory object
228 * -ENOTEMPTY not empty
232 static int mdd_dir_is_empty(const struct lu_env *env,
233 struct mdd_object *dir)
236 struct dt_object *obj;
237 const struct dt_it_ops *iops;
241 obj = mdd_object_child(dir);
242 if (!dt_try_as_dir(env, obj))
245 iops = &obj->do_index_ops->dio_it;
246 it = iops->init(env, obj, BYPASS_CAPA);
248 result = iops->get(env, it, (const void *)"");
251 for (result = 0, i = 0; result == 0 && i < 3; ++i)
252 result = iops->next(env, it);
255 else if (result == +1)
257 } else if (result == 0)
259 * Huh? Index contains no zero key?
270 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
272 struct mdd_device *m = mdd_obj2mdd_dev(obj);
273 struct lu_attr *la = &mdd_env_info(env)->mti_la;
277 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
282 * Subdir count limitation can be broken through.
284 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
285 !S_ISDIR(la->la_mode))
292 * Check whether it may create the cobj under the pobj.
295 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
296 struct mdd_object *cobj, int check_perm, int check_nlink)
301 if (cobj && mdd_object_exists(cobj))
304 if (mdd_is_dead_obj(pobj))
308 rc = mdd_permission_internal_locked(env, pobj, NULL,
309 MAY_WRITE | MAY_EXEC,
312 if (!rc && check_nlink)
313 rc = __mdd_may_link(env, pobj);
319 * Check whether can unlink from the pobj in the case of "cobj == NULL".
321 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
322 const struct md_attr *ma)
327 if (mdd_is_dead_obj(pobj))
330 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
331 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
334 rc = mdd_permission_internal_locked(env, pobj, NULL,
335 MAY_WRITE | MAY_EXEC,
340 if (mdd_is_append(pobj))
347 * pobj == NULL is remote ops case, under such case, pobj's
348 * VTX feature has been checked already, no need check again.
350 static inline int mdd_is_sticky(const struct lu_env *env,
351 struct mdd_object *pobj,
352 struct mdd_object *cobj)
354 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
355 struct md_ucred *uc = md_ucred(env);
359 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
363 if (!(tmp_la->la_mode & S_ISVTX) ||
364 (tmp_la->la_uid == uc->mu_fsuid))
368 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
372 if (tmp_la->la_uid == uc->mu_fsuid)
375 return !mdd_capable(uc, CFS_CAP_FOWNER);
379 * Check whether it may delete the cobj from the pobj.
382 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
383 struct mdd_object *cobj, struct md_attr *ma,
384 int check_perm, int check_empty)
390 if (!mdd_object_exists(cobj))
393 if (mdd_is_dead_obj(cobj))
397 if (!mdd_object_exists(pobj))
400 if (mdd_is_dead_obj(pobj))
404 rc = mdd_permission_internal_locked(env, pobj, NULL,
405 MAY_WRITE | MAY_EXEC,
411 if (mdd_is_append(pobj))
415 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
416 mdd_is_sticky(env, pobj, cobj))
419 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
422 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
423 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
426 if (S_ISDIR(ma->ma_attr.la_mode)) {
427 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
429 if (!S_ISDIR(mdd_object_type(cobj)))
432 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
434 } else if (S_ISDIR(mdd_object_type(cobj)))
437 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
438 rc = mdd_dir_is_empty(env, cobj);
445 * has mdd_write_lock on src already, but not on tgt yet
447 int mdd_link_sanity_check(const struct lu_env *env,
448 struct mdd_object *tgt_obj,
449 const struct lu_name *lname,
450 struct mdd_object *src_obj)
452 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
456 if (!mdd_object_exists(src_obj))
459 if (mdd_is_dead_obj(src_obj))
462 /* Local ops, no lookup before link, check filename length here. */
463 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
464 RETURN(-ENAMETOOLONG);
466 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
469 if (S_ISDIR(mdd_object_type(src_obj)))
472 LASSERT(src_obj != tgt_obj);
474 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
479 rc = __mdd_may_link(env, src_obj);
485 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
486 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
487 * (maybe too large to be represented). It is a trick to break through the
488 * "i_nlink" limitation for subdir count.
490 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
491 struct thandle *handle)
493 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
494 struct mdd_device *m = mdd_obj2mdd_dev(obj);
496 if (!mdd_is_mnlink(obj)) {
497 if (S_ISDIR(mdd_object_type(obj))) {
498 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
501 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
502 obj->mod_flags |= MNLINK_OBJ;
503 tmp_la->la_nlink = 1;
504 tmp_la->la_valid = LA_NLINK;
505 mdd_attr_set_internal(env, obj, tmp_la, handle,
510 mdo_ref_add(env, obj, handle);
514 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
515 struct thandle *handle, int is_dot)
517 if (!mdd_is_mnlink(obj) || is_dot)
518 mdo_ref_del(env, obj, handle);
521 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
522 const char *name, struct thandle *handle,
523 struct lustre_capa *capa)
525 struct dt_object *next = mdd_object_child(pobj);
529 if (dt_try_as_dir(env, next)) {
530 rc = next->do_index_ops->dio_delete(env, next,
531 (struct dt_key *)name,
539 static int __mdd_index_insert_only(const struct lu_env *env,
540 struct mdd_object *pobj,
541 const struct lu_fid *lf, const char *name,
542 struct thandle *handle,
543 struct lustre_capa *capa)
545 struct dt_object *next = mdd_object_child(pobj);
549 if (dt_try_as_dir(env, next)) {
550 struct md_ucred *uc = md_ucred(env);
552 rc = next->do_index_ops->dio_insert(env, next,
554 (const struct dt_key *)name,
555 handle, capa, uc->mu_cap &
556 CFS_CAP_SYS_RESOURCE_MASK);
563 /* insert named index, add reference if isdir */
564 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
565 const struct lu_fid *lf, const char *name, int is_dir,
566 struct thandle *handle, struct lustre_capa *capa)
571 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
572 if (rc == 0 && is_dir) {
573 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
574 __mdd_ref_add(env, pobj, handle);
575 mdd_write_unlock(env, pobj);
580 /* delete named index, drop reference if isdir */
581 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
582 const char *name, int is_dir, struct thandle *handle,
583 struct lustre_capa *capa)
588 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
589 if (rc == 0 && is_dir) {
592 if (name != NULL && name[0] == '.' && name[1] == 0)
594 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
595 __mdd_ref_del(env, pobj, handle, is_dot);
596 mdd_write_unlock(env, pobj);
603 /** Store a namespace change changelog record
604 * If this fails, we must fail the whole transaction; we don't
605 * want the change to commit without the log entry.
606 * \param target - mdd_object of change
607 * \param parent - parent dir/object
608 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
609 * \param tname - target name string
610 * \param handle - transacion handle
612 static int mdd_changelog_ns_store(const struct lu_env *env,
613 struct mdd_device *mdd,
614 enum changelog_rec_type type,
615 struct mdd_object *target,
616 struct mdd_object *parent,
617 const struct lu_fid *tf,
618 const struct lu_name *tname,
619 struct thandle *handle)
621 const struct lu_fid *tfid;
622 const struct lu_fid *tpfid = mdo2fid(parent);
623 struct llog_changelog_rec *rec;
629 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
632 LASSERT(parent != NULL);
633 LASSERT(tname != NULL);
634 LASSERT(handle != NULL);
637 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
638 buf = mdd_buf_alloc(env, reclen);
639 if (buf->lb_buf == NULL)
641 rec = (struct llog_changelog_rec *)buf->lb_buf;
643 rec->cr_flags = CLF_VERSION;
644 rec->cr_type = (__u32)type;
645 tfid = tf ? tf : mdo2fid(target);
646 rec->cr_tfid = *tfid;
647 rec->cr_pfid = *tpfid;
648 rec->cr_namelen = tname->ln_namelen;
649 memcpy(rec->cr_name, tname->ln_name, rec->cr_namelen);
651 target->mod_cltime = cfs_time_current_64();
653 rc = mdd_changelog_llog_write(mdd, rec, handle);
655 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
656 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
663 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
664 struct md_object *src_obj, const struct lu_name *lname,
667 const char *name = lname->ln_name;
668 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
669 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
670 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
671 struct mdd_device *mdd = mdo2mdd(src_obj);
672 struct dynlock_handle *dlh;
673 struct thandle *handle;
674 #ifdef HAVE_QUOTA_SUPPORT
675 struct obd_device *obd = mdd->mdd_obd_dev;
676 struct mds_obd *mds = &obd->u.mds;
677 unsigned int qids[MAXQUOTAS] = { 0, 0 };
678 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
683 #ifdef HAVE_QUOTA_SUPPORT
684 if (mds->mds_quota) {
685 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
687 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
690 mdd_data_get(env, mdd_tobj, &data);
691 quota_opc = FSFILT_OP_LINK;
692 mdd_quota_wrapper(la_tmp, qids);
693 /* get block quota for parent */
694 lquota_chkquota(mds_quota_interface_ref, obd,
695 qids, rec_pending, 1, NULL,
696 LQUOTA_FLAGS_BLK, data, 1);
701 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
702 handle = mdd_trans_start(env, mdd);
704 GOTO(out_pending, rc = PTR_ERR(handle));
706 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
708 GOTO(out_trans, rc = -ENOMEM);
709 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
711 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
713 GOTO(out_unlock, rc);
715 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
717 mdd_object_capa(env, mdd_tobj));
719 GOTO(out_unlock, rc);
721 __mdd_ref_add(env, mdd_sobj, handle);
723 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
724 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
726 la->la_valid = LA_CTIME | LA_MTIME;
727 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
729 GOTO(out_unlock, rc);
731 la->la_valid = LA_CTIME;
732 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
734 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj), lname, handle);
738 mdd_write_unlock(env, mdd_sobj);
739 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
742 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, mdd_sobj,
743 mdd_tobj, NULL, lname, handle);
744 mdd_trans_stop(env, mdd, rc, handle);
746 #ifdef HAVE_QUOTA_SUPPORT
748 lquota_pending_commit(mds_quota_interface_ref, obd,
749 qids, rec_pending, 1);
750 /* Trigger dqacq for the parent owner. If failed,
751 * the next call for lquota_chkquota will process it. */
752 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
759 /* caller should take a lock before calling */
760 int mdd_finish_unlink(const struct lu_env *env,
761 struct mdd_object *obj, struct md_attr *ma,
768 LASSERT(mdd_write_locked(env, obj) != 0);
770 rc = mdd_iattr_get(env, obj, ma);
771 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
772 obj->mod_flags |= DEAD_OBJ;
773 /* add new orphan and the object
774 * will be deleted during mdd_close() */
775 if (obj->mod_count) {
776 rc = __mdd_orphan_add(env, obj, th);
778 CDEBUG(D_HA, "Object "DFID" is inserted into "
779 "orphan list, open count = %d\n",
780 PFID(mdd_object_fid(obj)),
783 CERROR("Object "DFID" fail to be an orphan, "
784 "open count = %d, maybe cause failed "
786 PFID(mdd_object_fid(obj)),
789 rc = mdd_object_kill(env, obj, ma);
796 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
803 * has mdd_write_lock on cobj already, but not on pobj yet
805 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
806 struct mdd_object *cobj, struct md_attr *ma)
811 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
816 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
817 struct md_object *cobj, const struct lu_name *lname,
820 const char *name = lname->ln_name;
821 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
822 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
823 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
824 struct mdd_device *mdd = mdo2mdd(pobj);
825 struct dynlock_handle *dlh;
826 struct thandle *handle;
827 #ifdef HAVE_QUOTA_SUPPORT
828 struct obd_device *obd = mdd->mdd_obd_dev;
829 struct mds_obd *mds = &obd->u.mds;
830 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
831 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
834 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
838 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
839 PFID(mdd_object_fid(mdd_cobj)));
841 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
845 handle = mdd_trans_start(env, mdd);
847 RETURN(PTR_ERR(handle));
849 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
851 GOTO(out_trans, rc = -ENOMEM);
852 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
854 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
858 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
859 mdd_object_capa(env, mdd_pobj));
863 __mdd_ref_del(env, mdd_cobj, handle, 0);
866 __mdd_ref_del(env, mdd_cobj, handle, 1);
868 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
869 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
871 la->la_valid = LA_CTIME | LA_MTIME;
872 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
876 la->la_valid = LA_CTIME;
877 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
881 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
882 #ifdef HAVE_QUOTA_SUPPORT
883 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
884 ma->ma_attr.la_nlink == 0) {
885 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
887 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
889 mdd_quota_wrapper(la_tmp, qpids);
890 if (mdd_cobj->mod_count == 0) {
891 quota_opc = FSFILT_OP_UNLINK;
892 mdd_quota_wrapper(&ma->ma_attr, qcids);
894 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
901 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
902 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
905 /* old files may not have link ea; ignore errors */
906 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
907 lname, NULL, NULL, handle);
911 mdd_write_unlock(env, mdd_cobj);
912 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
915 rc = mdd_changelog_ns_store(env, mdd,
916 is_dir ? CL_RMDIR : CL_UNLINK,
917 mdd_cobj, mdd_pobj, NULL, lname,
920 mdd_trans_stop(env, mdd, rc, handle);
921 #ifdef HAVE_QUOTA_SUPPORT
923 /* Trigger dqrel on the owner of child and parent. If failed,
924 * the next call for lquota_chkquota will process it. */
925 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
931 /* has not lock on pobj yet */
932 static int mdd_ni_sanity_check(const struct lu_env *env,
933 struct md_object *pobj,
934 const struct md_attr *ma)
936 struct mdd_object *obj = md2mdd_obj(pobj);
940 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
943 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
951 static int mdd_name_insert(const struct lu_env *env,
952 struct md_object *pobj,
953 const struct lu_name *lname,
954 const struct lu_fid *fid,
955 const struct md_attr *ma)
957 const char *name = lname->ln_name;
958 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
959 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
960 struct mdd_device *mdd = mdo2mdd(pobj);
961 struct dynlock_handle *dlh;
962 struct thandle *handle;
963 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
964 #ifdef HAVE_QUOTA_SUPPORT
965 struct md_ucred *uc = md_ucred(env);
966 struct obd_device *obd = mdd->mdd_obd_dev;
967 struct mds_obd *mds = &obd->u.mds;
968 unsigned int qids[MAXQUOTAS] = { 0, 0 };
969 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
970 cfs_cap_t save = uc->mu_cap;
975 #ifdef HAVE_QUOTA_SUPPORT
976 if (mds->mds_quota) {
977 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
978 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
980 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
983 mdd_data_get(env, mdd_obj, &data);
984 quota_opc = FSFILT_OP_LINK;
985 mdd_quota_wrapper(la_tmp, qids);
986 /* get block quota for parent */
987 lquota_chkquota(mds_quota_interface_ref, obd,
988 qids, rec_pending, 1, NULL,
989 LQUOTA_FLAGS_BLK, data, 1);
992 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
996 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
997 handle = mdd_trans_start(env, mdo2mdd(pobj));
999 GOTO(out_pending, rc = PTR_ERR(handle));
1001 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1003 GOTO(out_trans, rc = -ENOMEM);
1005 rc = mdd_ni_sanity_check(env, pobj, ma);
1007 GOTO(out_unlock, rc);
1009 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1010 handle, BYPASS_CAPA);
1012 GOTO(out_unlock, rc);
1015 * For some case, no need update obj's ctime (LA_CTIME is not set),
1017 * For other cases, update obj's ctime (LA_CTIME is set),
1020 if (ma->ma_attr.la_valid & LA_CTIME) {
1021 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1022 la->la_valid = LA_CTIME | LA_MTIME;
1023 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1028 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1030 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1032 #ifdef HAVE_QUOTA_SUPPORT
1033 if (mds->mds_quota) {
1035 lquota_pending_commit(mds_quota_interface_ref,
1036 obd, qids, rec_pending, 1);
1037 /* Trigger dqacq for the parent owner. If failed,
1038 * the next call for lquota_chkquota will process it*/
1039 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1049 /* has not lock on pobj yet */
1050 static int mdd_nr_sanity_check(const struct lu_env *env,
1051 struct md_object *pobj,
1052 const struct md_attr *ma)
1054 struct mdd_object *obj = md2mdd_obj(pobj);
1058 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1061 rc = mdd_may_unlink(env, obj, ma);
1067 * Partial operation.
1069 static int mdd_name_remove(const struct lu_env *env,
1070 struct md_object *pobj,
1071 const struct lu_name *lname,
1072 const struct md_attr *ma)
1074 const char *name = lname->ln_name;
1075 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1076 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1077 struct mdd_device *mdd = mdo2mdd(pobj);
1078 struct dynlock_handle *dlh;
1079 struct thandle *handle;
1080 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1081 #ifdef HAVE_QUOTA_SUPPORT
1082 struct obd_device *obd = mdd->mdd_obd_dev;
1083 struct mds_obd *mds = &obd->u.mds;
1084 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1090 #ifdef HAVE_QUOTA_SUPPORT
1091 if (mds->mds_quota) {
1092 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1094 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1096 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1097 mdd_quota_wrapper(la_tmp, qids);
1101 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
1102 handle = mdd_trans_start(env, mdd);
1104 GOTO(out_pending, rc = PTR_ERR(handle));
1106 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1108 GOTO(out_trans, rc = -ENOMEM);
1110 rc = mdd_nr_sanity_check(env, pobj, ma);
1112 GOTO(out_unlock, rc);
1114 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1115 handle, BYPASS_CAPA);
1117 GOTO(out_unlock, rc);
1120 * For some case, no need update obj's ctime (LA_CTIME is not set),
1122 * For other cases, update obj's ctime (LA_CTIME is set),
1125 if (ma->ma_attr.la_valid & LA_CTIME) {
1126 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1127 la->la_valid = LA_CTIME | LA_MTIME;
1128 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1133 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1135 mdd_trans_stop(env, mdd, rc, handle);
1137 #ifdef HAVE_QUOTA_SUPPORT
1138 /* Trigger dqrel for the parent owner.
1139 * If failed, the next call for lquota_chkquota will process it. */
1141 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1149 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1151 static int mdd_rt_sanity_check(const struct lu_env *env,
1152 struct mdd_object *tgt_pobj,
1153 struct mdd_object *tobj,
1159 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1162 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1163 * exist. In fact, tobj must exist, otherwise the call trace will be:
1164 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1165 * When get here, tobj must be NOT NULL, the other case has been
1166 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1168 * So check may_delete, but not check nlink of tgt_pobj. */
1170 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1175 /* Partial rename op on slave MDD */
1176 static int mdd_rename_tgt(const struct lu_env *env,
1177 struct md_object *pobj, struct md_object *tobj,
1178 const struct lu_fid *lf, const struct lu_name *lname,
1181 const char *name = lname->ln_name;
1182 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1183 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1184 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1185 struct mdd_device *mdd = mdo2mdd(pobj);
1186 struct dynlock_handle *dlh;
1187 struct thandle *handle;
1188 #ifdef HAVE_QUOTA_SUPPORT
1189 struct obd_device *obd = mdd->mdd_obd_dev;
1190 struct mds_obd *mds = &obd->u.mds;
1191 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1192 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1193 int quota_copc = 0, quota_popc = 0;
1194 int rec_pending[MAXQUOTAS] = { 0, 0 };
1199 #ifdef HAVE_QUOTA_SUPPORT
1200 if (mds->mds_quota && !tobj) {
1201 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1203 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1206 mdd_data_get(env, mdd_tpobj, &data);
1207 quota_popc = FSFILT_OP_LINK;
1208 mdd_quota_wrapper(la_tmp, qpids);
1209 /* get block quota for target parent */
1210 lquota_chkquota(mds_quota_interface_ref, obd,
1211 qpids, rec_pending, 1, NULL,
1212 LQUOTA_FLAGS_BLK, data, 1);
1216 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1217 handle = mdd_trans_start(env, mdd);
1219 GOTO(out_pending, rc = PTR_ERR(handle));
1221 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1223 GOTO(out_trans, rc = -ENOMEM);
1225 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1227 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1232 * If rename_tgt is called then we should just re-insert name with
1233 * correct fid, no need to dec/inc parent nlink if obj is dir.
1235 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1239 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1244 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1245 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1247 la->la_valid = LA_CTIME | LA_MTIME;
1248 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1253 * For tobj is remote case cmm layer has processed
1254 * and pass NULL tobj to here. So when tobj is NOT NULL,
1255 * it must be local one.
1257 if (tobj && mdd_object_exists(mdd_tobj)) {
1258 __mdd_ref_del(env, mdd_tobj, handle, 0);
1260 /* Remove dot reference. */
1261 if (S_ISDIR(ma->ma_attr.la_mode))
1262 __mdd_ref_del(env, mdd_tobj, handle, 1);
1264 la->la_valid = LA_CTIME;
1265 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1269 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1273 #ifdef HAVE_QUOTA_SUPPORT
1274 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1275 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1276 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1277 mdd_quota_wrapper(&ma->ma_attr, qcids);
1284 mdd_write_unlock(env, mdd_tobj);
1285 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1288 /* Bare EXT record with no RENAME in front of it signifies
1289 a partial slave op */
1290 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
1291 mdd_tpobj, NULL, lname, handle);
1293 mdd_trans_stop(env, mdd, rc, handle);
1295 #ifdef HAVE_QUOTA_SUPPORT
1296 if (mds->mds_quota) {
1298 lquota_pending_commit(mds_quota_interface_ref, obd,
1299 qpids, rec_pending, 1);
1302 /* Trigger dqrel on the target owner of child.
1303 * If failed, the next call for lquota_chkquota
1304 * will process it. */
1305 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1313 * The permission has been checked when obj created, no need check again.
1315 static int mdd_cd_sanity_check(const struct lu_env *env,
1316 struct mdd_object *obj)
1321 if (!obj || mdd_is_dead_obj(obj))
1328 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1329 struct md_object *cobj, const struct md_op_spec *spec,
1332 struct mdd_device *mdd = mdo2mdd(cobj);
1333 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1334 struct mdd_object *son = md2mdd_obj(cobj);
1335 struct lu_attr *attr = &ma->ma_attr;
1336 struct lov_mds_md *lmm = NULL;
1338 struct thandle *handle;
1342 rc = mdd_cd_sanity_check(env, son);
1346 if (!md_should_create(spec->sp_cr_flags))
1348 lmm_size = ma->ma_lmm_size;
1349 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1354 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1355 handle = mdd_trans_start(env, mdd);
1357 GOTO(out_free, rc = PTR_ERR(handle));
1360 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1361 * Should this be fixed?
1364 /* Replay creates has objects already */
1366 if (spec->no_create) {
1367 CDEBUG(D_INFO, "we already have lov ea\n");
1368 rc = mdd_lov_set_md(env, mdd_pobj, son,
1369 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1370 spec->u.sp_ea.eadatalen, handle, 0);
1373 /* No need mdd_lsm_sanity_check here */
1374 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1375 lmm_size, handle, 0);
1378 rc = mdd_attr_get_internal_locked(env, son, ma);
1380 /* update lov_objid data, must be before transaction stop! */
1382 mdd_lov_objid_update(mdd, lmm);
1384 mdd_trans_stop(env, mdd, rc, handle);
1386 /* Finish mdd_lov_create() stuff. */
1387 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1391 /* Get fid from name and parent */
1393 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1394 const struct lu_name *lname, struct lu_fid* fid, int mask)
1396 const char *name = lname->ln_name;
1397 const struct dt_key *key = (const struct dt_key *)name;
1398 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1399 struct mdd_device *m = mdo2mdd(pobj);
1400 struct dt_object *dir = mdd_object_child(mdd_obj);
1404 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1407 rc = mdd_object_exists(mdd_obj);
1408 if (unlikely(rc == 0))
1410 else if (unlikely(rc < 0)) {
1411 CERROR("Object "DFID" locates on remote server\n",
1412 PFID(mdo2fid(mdd_obj)));
1416 /* The common filename length check. */
1417 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1418 RETURN(-ENAMETOOLONG);
1420 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1425 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1426 dt_try_as_dir(env, dir))) {
1428 rc = dir->do_index_ops->dio_lookup(env, dir,
1429 (struct dt_rec *)fid, key,
1430 mdd_object_capa(env, mdd_obj));
1441 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1442 const struct lu_name *lname, struct mdd_object *child,
1443 struct md_attr *ma, struct thandle *handle,
1444 const struct md_op_spec *spec)
1450 * Update attributes for child.
1453 * (1) the valid bits should be converted between Lustre and Linux;
1454 * (2) maybe, the child attributes should be set in OSD when creation.
1457 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1461 if (S_ISDIR(ma->ma_attr.la_mode)) {
1462 /* Add "." and ".." for newly created dir */
1463 __mdd_ref_add(env, child, handle);
1464 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1465 dot, handle, BYPASS_CAPA);
1467 rc = __mdd_index_insert_only(env, child, pfid,
1471 __mdd_ref_del(env, child, handle, 1);
1474 mdd_links_add(env, child, pfid, lname, handle);
1479 /* has not lock on pobj yet */
1480 static int mdd_create_sanity_check(const struct lu_env *env,
1481 struct md_object *pobj,
1482 const struct lu_name *lname,
1484 struct md_op_spec *spec)
1486 struct mdd_thread_info *info = mdd_env_info(env);
1487 struct lu_attr *la = &info->mti_la;
1488 struct lu_fid *fid = &info->mti_fid;
1489 struct mdd_object *obj = md2mdd_obj(pobj);
1490 struct mdd_device *m = mdo2mdd(pobj);
1491 int lookup = spec->sp_cr_lookup;
1496 if (mdd_is_dead_obj(obj))
1500 * In some cases this lookup is not needed - we know before if name
1501 * exists or not because MDT performs lookup for it.
1502 * name length check is done in lookup.
1506 * Check if the name already exist, though it will be checked in
1507 * _index_insert also, for avoiding rolling back if exists
1510 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1511 MAY_WRITE | MAY_EXEC);
1513 RETURN(rc ? : -EEXIST);
1516 * Check WRITE permission for the parent.
1517 * EXEC permission have been checked
1518 * when lookup before create already.
1520 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1527 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1531 if (la->la_mode & S_ISGID) {
1532 ma->ma_attr.la_gid = la->la_gid;
1533 if (S_ISDIR(ma->ma_attr.la_mode)) {
1534 ma->ma_attr.la_mode |= S_ISGID;
1535 ma->ma_attr.la_valid |= LA_MODE;
1539 switch (ma->ma_attr.la_mode & S_IFMT) {
1541 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1543 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1544 RETURN(-ENAMETOOLONG);
1564 * Create object and insert it into namespace.
1566 static int mdd_create(const struct lu_env *env,
1567 struct md_object *pobj,
1568 const struct lu_name *lname,
1569 struct md_object *child,
1570 struct md_op_spec *spec,
1573 struct mdd_thread_info *info = mdd_env_info(env);
1574 struct lu_attr *la = &info->mti_la_for_fix;
1575 struct md_attr *ma_acl = &info->mti_ma;
1576 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1577 struct mdd_object *son = md2mdd_obj(child);
1578 struct mdd_device *mdd = mdo2mdd(pobj);
1579 struct lu_attr *attr = &ma->ma_attr;
1580 struct lov_mds_md *lmm = NULL;
1581 struct thandle *handle;
1582 struct dynlock_handle *dlh;
1583 const char *name = lname->ln_name;
1584 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1585 int got_def_acl = 0;
1586 #ifdef HAVE_QUOTA_SUPPORT
1587 struct obd_device *obd = mdd->mdd_obd_dev;
1588 struct mds_obd *mds = &obd->u.mds;
1589 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1590 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1591 int quota_opc = 0, block_count = 0;
1592 int inode_pending[MAXQUOTAS] = { 0, 0 };
1593 int block_pending[MAXQUOTAS] = { 0, 0 };
1594 int parent_pending[MAXQUOTAS] = { 0, 0 };
1599 * Two operations have to be performed:
1601 * - an allocation of a new object (->do_create()), and
1603 * - an insertion into a parent index (->dio_insert()).
1605 * Due to locking, operation order is not important, when both are
1606 * successful, *but* error handling cases are quite different:
1608 * - if insertion is done first, and following object creation fails,
1609 * insertion has to be rolled back, but this operation might fail
1610 * also leaving us with dangling index entry.
1612 * - if creation is done first, is has to be undone if insertion
1613 * fails, leaving us with leaked space, which is neither good, nor
1616 * It seems that creation-first is simplest solution, but it is
1617 * sub-optimal in the frequent
1622 * case, because second mkdir is bound to create object, only to
1623 * destroy it immediately.
1625 * To avoid this follow local file systems that do double lookup:
1627 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1629 * 1. create (mdd_object_create_internal())
1631 * 2. insert (__mdd_index_insert(), lookup again)
1634 /* Sanity checks before big job. */
1635 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1639 #ifdef HAVE_QUOTA_SUPPORT
1640 if (mds->mds_quota) {
1641 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1643 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1647 quota_opc = FSFILT_OP_CREATE;
1648 mdd_quota_wrapper(&ma->ma_attr, qcids);
1649 mdd_quota_wrapper(la_tmp, qpids);
1650 /* get file quota for child */
1651 lquota_chkquota(mds_quota_interface_ref, obd, qcids,
1652 inode_pending, 1, NULL, 0, NULL, 0);
1653 switch (ma->ma_attr.la_mode & S_IFMT) {
1662 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1663 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1667 /* get block quota for child and parent */
1669 lquota_chkquota(mds_quota_interface_ref, obd,
1670 qcids, block_pending,
1672 LQUOTA_FLAGS_BLK, NULL, 0);
1674 lquota_chkquota(mds_quota_interface_ref, obd,
1675 qpids, parent_pending, 1, NULL,
1676 LQUOTA_FLAGS_BLK, NULL, 0);
1682 * No RPC inside the transaction, so OST objects should be created at
1685 if (S_ISREG(attr->la_mode)) {
1686 lmm_size = ma->ma_lmm_size;
1687 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1690 GOTO(out_pending, rc);
1693 if (!S_ISLNK(attr->la_mode)) {
1694 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1695 ma_acl->ma_acl = info->mti_xattr_buf;
1696 ma_acl->ma_need = MA_ACL_DEF;
1697 ma_acl->ma_valid = 0;
1699 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1700 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1701 mdd_read_unlock(env, mdd_pobj);
1704 else if (ma_acl->ma_valid & MA_ACL_DEF)
1708 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1709 handle = mdd_trans_start(env, mdd);
1711 GOTO(out_free, rc = PTR_ERR(handle));
1713 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1715 GOTO(out_trans, rc = -ENOMEM);
1717 mdd_write_lock(env, son, MOR_TGT_CHILD);
1718 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1720 mdd_write_unlock(env, son);
1726 #ifdef CONFIG_FS_POSIX_ACL
1728 struct lu_buf *acl_buf = &info->mti_buf;
1729 acl_buf->lb_buf = ma_acl->ma_acl;
1730 acl_buf->lb_len = ma_acl->ma_acl_size;
1732 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1734 mdd_write_unlock(env, son);
1737 ma->ma_attr.la_valid |= LA_MODE;
1742 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1743 son, ma, handle, spec);
1744 mdd_write_unlock(env, son);
1747 * Object has no links, so it will be destroyed when last
1748 * reference is released. (XXX not now.)
1754 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1755 name, S_ISDIR(attr->la_mode), handle,
1756 mdd_object_capa(env, mdd_pobj));
1763 /* No need mdd_lsm_sanity_check here */
1764 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1766 CERROR("error on stripe info copy %d \n", rc);
1769 if (lmm && lmm_size > 0) {
1770 /* Set Lov here, do not get lmm again later */
1771 memcpy(ma->ma_lmm, lmm, lmm_size);
1772 ma->ma_lmm_size = lmm_size;
1773 ma->ma_valid |= MA_LOV;
1776 if (S_ISLNK(attr->la_mode)) {
1777 struct md_ucred *uc = md_ucred(env);
1778 struct dt_object *dt = mdd_object_child(son);
1779 const char *target_name = spec->u.sp_symname;
1780 int sym_len = strlen(target_name);
1781 const struct lu_buf *buf;
1784 buf = mdd_buf_get_const(env, target_name, sym_len);
1785 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1786 mdd_object_capa(env, son),
1788 CFS_CAP_SYS_RESOURCE_MASK);
1793 GOTO(cleanup, rc = -EFAULT);
1797 la->la_valid = LA_CTIME | LA_MTIME;
1798 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1802 /* Return attr back. */
1803 rc = mdd_attr_get_internal_locked(env, son, ma);
1806 if (rc && created) {
1810 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1811 S_ISDIR(attr->la_mode),
1812 handle, BYPASS_CAPA);
1814 CERROR("error can not cleanup destroy %d\n",
1819 mdd_write_lock(env, son, MOR_TGT_CHILD);
1820 __mdd_ref_del(env, son, handle, 0);
1821 if (initialized && S_ISDIR(attr->la_mode))
1822 __mdd_ref_del(env, son, handle, 1);
1823 mdd_write_unlock(env, son);
1827 /* update lov_objid data, must be before transaction stop! */
1829 mdd_lov_objid_update(mdd, lmm);
1831 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1834 rc = mdd_changelog_ns_store(env, mdd,
1835 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1836 S_ISREG(attr->la_mode) ? CL_CREATE :
1837 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1838 son, mdd_pobj, NULL, lname, handle);
1839 mdd_trans_stop(env, mdd, rc, handle);
1841 /* finis lov_create stuff, free all temporary data */
1842 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1844 #ifdef HAVE_QUOTA_SUPPORT
1846 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1848 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1850 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1852 /* Trigger dqacq on the owner of child and parent. If failed,
1853 * the next call for lquota_chkquota will process it. */
1854 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1862 * Get locks on parents in proper order
1863 * RETURN: < 0 - error, rename_order if successful
1871 static int mdd_rename_order(const struct lu_env *env,
1872 struct mdd_device *mdd,
1873 struct mdd_object *src_pobj,
1874 struct mdd_object *tgt_pobj)
1876 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1880 if (src_pobj == tgt_pobj)
1881 RETURN(MDD_RN_SAME);
1883 /* compared the parent child relationship of src_p&tgt_p */
1884 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1886 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1889 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1902 /* has not mdd_write{read}_lock on any obj yet. */
1903 static int mdd_rename_sanity_check(const struct lu_env *env,
1904 struct mdd_object *src_pobj,
1905 struct mdd_object *tgt_pobj,
1906 struct mdd_object *sobj,
1907 struct mdd_object *tobj,
1913 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1916 /* XXX: when get here, sobj must NOT be NULL,
1917 * the other case has been processed in cml_rename
1918 * before mdd_rename and enable MDS_PERM_BYPASS. */
1921 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1925 /* XXX: when get here, "tobj == NULL" means tobj must
1926 * NOT exist (neither on remote MDS, such case has been
1927 * processed in cml_rename before mdd_rename and enable
1929 * So check may_create, but not check may_unlink. */
1931 rc = mdd_may_create(env, tgt_pobj, NULL,
1932 (src_pobj != tgt_pobj), 0);
1934 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1935 (src_pobj != tgt_pobj), 1);
1937 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1938 S_ISDIR(ma->ma_attr.la_mode))
1939 rc = __mdd_may_link(env, tgt_pobj);
1944 /* src object can be remote that is why we use only fid and type of object */
1945 static int mdd_rename(const struct lu_env *env,
1946 struct md_object *src_pobj, struct md_object *tgt_pobj,
1947 const struct lu_fid *lf, const struct lu_name *lsname,
1948 struct md_object *tobj, const struct lu_name *ltname,
1951 const char *sname = lsname->ln_name;
1952 const char *tname = ltname->ln_name;
1953 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1954 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1955 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1956 struct mdd_device *mdd = mdo2mdd(src_pobj);
1957 struct mdd_object *mdd_sobj = NULL; /* source object */
1958 struct mdd_object *mdd_tobj = NULL;
1959 struct dynlock_handle *sdlh, *tdlh;
1960 struct thandle *handle;
1961 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1962 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1966 #ifdef HAVE_QUOTA_SUPPORT
1967 struct obd_device *obd = mdd->mdd_obd_dev;
1968 struct mds_obd *mds = &obd->u.mds;
1969 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1970 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1971 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1972 int quota_copc = 0, quota_popc = 0;
1973 int rec_pending[MAXQUOTAS] = { 0, 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_popc = FSFILT_OP_LINK;
1997 mdd_quota_wrapper(la_tmp, qtpids);
1998 /* get block quota for target parent */
1999 lquota_chkquota(mds_quota_interface_ref,
2001 rec_pending, 1, NULL,
2009 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2010 handle = mdd_trans_start(env, mdd);
2012 GOTO(out_pending, rc = PTR_ERR(handle));
2014 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2015 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2017 GOTO(cleanup_unlocked, rc);
2019 /* Get locks in determined order */
2020 if (rc == MDD_RN_SAME) {
2021 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2022 sname, MOR_SRC_PARENT);
2023 /* check hashes to determine do we need one lock or two */
2024 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2025 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2029 } else if (rc == MDD_RN_SRCTGT) {
2030 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2031 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2033 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2034 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2036 if (sdlh == NULL || tdlh == NULL)
2037 GOTO(cleanup, rc = -ENOMEM);
2039 mdd_sobj = mdd_object_find(env, mdd, lf);
2040 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2041 mdd_sobj, mdd_tobj, ma);
2045 /* Remove source name from source directory */
2046 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2047 mdd_object_capa(env, mdd_spobj));
2051 /* "mv dir1 dir2" needs "dir1/.." link update */
2052 if (is_dir && mdd_sobj) {
2053 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2054 mdd_object_capa(env, mdd_sobj));
2056 GOTO(fixup_spobj2, rc);
2058 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2059 handle, mdd_object_capa(env, mdd_sobj));
2061 GOTO(fixup_spobj, rc);
2065 /* Remove target name from target directory
2066 * Here tobj can be remote one, so we do index_delete unconditionally
2067 * and -ENOENT is allowed.
2069 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2070 mdd_object_capa(env, mdd_tpobj));
2073 /* tname might been renamed to something else */
2074 GOTO(fixup_spobj, rc);
2077 GOTO(fixup_spobj, rc);
2080 /* Insert new fid with target name into target dir */
2081 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2082 mdd_object_capa(env, mdd_tpobj));
2084 GOTO(fixup_tpobj, rc);
2086 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2087 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2089 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2091 la->la_valid = LA_CTIME;
2092 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2095 GOTO(fixup_tpobj, rc);
2098 /* Remove old target object
2099 * For tobj is remote case cmm layer has processed
2100 * and set tobj to NULL then. So when tobj is NOT NULL,
2101 * it must be local one.
2103 if (tobj && mdd_object_exists(mdd_tobj)) {
2104 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2105 if (mdd_is_dead_obj(mdd_tobj)) {
2106 mdd_write_unlock(env, mdd_tobj);
2107 /* shld not be dead, something is wrong */
2108 CERROR("tobj is dead, something is wrong\n");
2112 __mdd_ref_del(env, mdd_tobj, handle, 0);
2114 /* Remove dot reference. */
2116 __mdd_ref_del(env, mdd_tobj, handle, 1);
2118 la->la_valid = LA_CTIME;
2119 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2121 GOTO(fixup_tpobj, rc);
2123 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2124 mdd_write_unlock(env, mdd_tobj);
2126 GOTO(fixup_tpobj, rc);
2128 #ifdef HAVE_QUOTA_SUPPORT
2129 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2130 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2131 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2132 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2137 la->la_valid = LA_CTIME | LA_MTIME;
2138 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2140 GOTO(fixup_tpobj, rc);
2142 if (mdd_spobj != mdd_tpobj) {
2143 la->la_valid = LA_CTIME | LA_MTIME;
2144 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2148 if (rc == 0 && mdd_sobj) {
2149 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2150 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2151 mdo2fid(mdd_tpobj), ltname, handle);
2153 /* Old files might not have EA entry */
2154 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2156 mdd_write_unlock(env, mdd_sobj);
2157 /* We don't fail the transaction if the link ea can't be
2158 updated -- fid2path will use alternate lookup method. */
2166 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2169 CWARN("tp obj fix error %d\n",rc2);
2171 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2172 !mdd_is_dead_obj(mdd_tobj)) {
2173 rc2 = __mdd_index_insert(env, mdd_tpobj,
2174 mdo2fid(mdd_tobj), tname,
2179 CWARN("tp obj fix error %d\n",rc2);
2184 if (rc && is_dir && mdd_sobj) {
2185 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2189 CWARN("sp obj dotdot delete error %d\n",rc2);
2192 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2193 dotdot, handle, BYPASS_CAPA);
2195 CWARN("sp obj dotdot insert error %d\n",rc2);
2200 rc2 = __mdd_index_insert(env, mdd_spobj,
2201 lf, sname, is_dir, handle, BYPASS_CAPA);
2203 CWARN("sp obj fix error %d\n",rc2);
2206 if (likely(tdlh) && sdlh != tdlh)
2207 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2209 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2212 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2213 mdd_spobj, lf, lsname, handle);
2215 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2216 mdd_tpobj, lf, ltname, handle);
2218 mdd_trans_stop(env, mdd, rc, handle);
2220 mdd_object_put(env, mdd_sobj);
2222 #ifdef HAVE_QUOTA_SUPPORT
2223 if (mds->mds_quota) {
2225 lquota_pending_commit(mds_quota_interface_ref, obd,
2226 qtpids, rec_pending, 1);
2229 /* Trigger dqrel on the source owner of parent.
2230 * If failed, the next call for lquota_chkquota will
2232 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2233 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2235 /* Trigger dqrel on the target owner of child.
2236 * If failed, the next call for lquota_chkquota
2237 * will process it. */
2238 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2239 qtpids, rc, quota_copc);
2246 /** enable/disable storing of hardlink info */
2247 int mdd_linkea_enable = 1;
2248 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2249 "record hardlink info in EAs");
2251 /** Read the link EA into a temp buffer.
2252 * Uses the name_buf since it is generally large.
2253 * \retval IS_ERR err
2254 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2256 struct lu_buf *mdd_links_get(const struct lu_env *env,
2257 struct mdd_object *mdd_obj)
2260 struct lustre_capa *capa;
2261 struct link_ea_header *leh;
2264 /* First try a small buf */
2265 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2266 if (buf->lb_buf == NULL)
2267 return ERR_PTR(-ENOMEM);
2269 capa = mdd_object_capa(env, mdd_obj);
2270 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2271 if (rc == -ERANGE) {
2272 /* Buf was too small, figure out what we need. */
2275 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2278 buf = mdd_buf_alloc(env, rc);
2279 if (buf->lb_buf == NULL)
2280 return ERR_PTR(-ENOMEM);
2281 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2287 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2288 leh->leh_magic = LINK_EA_MAGIC;
2289 leh->leh_reccount = __swab32(leh->leh_reccount);
2290 leh->leh_len = __swab64(leh->leh_len);
2291 /* entries are swabbed by mdd_lee_unpack */
2293 if (leh->leh_magic != LINK_EA_MAGIC)
2294 return ERR_PTR(-EINVAL);
2295 if (leh->leh_reccount == 0)
2296 return ERR_PTR(-ENODATA);
2301 /** Pack a link_ea_entry.
2302 * All elements are stored as chars to avoid alignment issues.
2303 * Numbers are always big-endian
2304 * \retval record length
2306 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2307 const struct lu_fid *pfid)
2311 fid_cpu_to_be(&lee->lee_parent_fid, pfid);
2312 strncpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2313 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2315 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2316 lee->lee_reclen[1] = reclen & 0xff;
2320 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2321 struct lu_name *lname, struct lu_fid *pfid)
2323 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2324 fid_be_to_cpu(pfid, &lee->lee_parent_fid);
2325 lname->ln_name = lee->lee_name;
2326 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2329 /** Add a record to the end of link ea buf */
2330 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2331 const struct lu_fid *pfid,
2332 const struct lu_name *lname)
2334 struct link_ea_header *leh;
2335 struct link_ea_entry *lee;
2338 if (lname == NULL || pfid == NULL)
2341 /* Make sure our buf is big enough for the new one */
2343 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2344 if (leh->leh_len + reclen > buf->lb_len) {
2345 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2350 lee = buf->lb_buf + leh->leh_len;
2351 reclen = mdd_lee_pack(lee, lname, pfid);
2352 leh->leh_len += reclen;
2353 leh->leh_reccount++;
2357 /* For pathologic linkers, we don't want to spend lots of time scanning the
2358 * link ea. Limit ourseleves to something reasonable; links not in the EA
2359 * can be looked up via (slower) parent lookup.
2361 #define LINKEA_MAX_COUNT 128
2363 static int mdd_links_add(const struct lu_env *env,
2364 struct mdd_object *mdd_obj,
2365 const struct lu_fid *pfid,
2366 const struct lu_name *lname,
2367 struct thandle *handle)
2370 struct link_ea_header *leh;
2374 if (!mdd_linkea_enable)
2377 buf = mdd_links_get(env, mdd_obj);
2380 if (rc != -ENODATA) {
2381 CERROR("link_ea read failed %d "DFID"\n", rc,
2382 PFID(mdd_object_fid(mdd_obj)));
2385 /* empty EA; start one */
2386 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2387 if (buf->lb_buf == NULL)
2390 leh->leh_magic = LINK_EA_MAGIC;
2391 leh->leh_len = sizeof(struct link_ea_header);
2392 leh->leh_reccount = 0;
2396 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2399 rc = __mdd_links_add(env, buf, pfid, lname);
2404 rc = __mdd_xattr_set(env, mdd_obj,
2405 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2406 XATTR_NAME_LINK, 0, handle);
2408 CERROR("link_ea add failed %d "DFID"\n", rc,
2409 PFID(mdd_object_fid(mdd_obj)));
2411 if (buf->lb_vmalloc)
2412 /* if we vmalloced a large buffer drop it */
2418 static int mdd_links_rename(const struct lu_env *env,
2419 struct mdd_object *mdd_obj,
2420 const struct lu_fid *oldpfid,
2421 const struct lu_name *oldlname,
2422 const struct lu_fid *newpfid,
2423 const struct lu_name *newlname,
2424 struct thandle *handle)
2427 struct link_ea_header *leh;
2428 struct link_ea_entry *lee;
2429 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2430 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2436 if (!mdd_linkea_enable)
2439 if (mdd_obj->mod_flags & DEAD_OBJ)
2440 /* No more links, don't bother */
2443 buf = mdd_links_get(env, mdd_obj);
2446 CERROR("link_ea read failed %d "DFID"\n",
2447 rc, PFID(mdd_object_fid(mdd_obj)));
2451 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2453 /* Find the old record */
2454 for(count = 0; count <= leh->leh_reccount; count++) {
2455 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2456 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2457 lu_fid_eq(tmpfid, oldpfid) &&
2458 (strncmp(tmpname->ln_name, oldlname->ln_name,
2459 tmpname->ln_namelen) == 0))
2461 lee = (struct link_ea_entry *)((char *)lee + reclen);
2463 if (count > leh->leh_reccount) {
2464 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2465 oldlname->ln_namelen, oldlname->ln_name);
2466 GOTO(out, rc = -ENOENT);
2469 /* Remove the old record */
2470 leh->leh_reccount--;
2471 leh->leh_len -= reclen;
2472 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2475 /* If renaming, add the new record */
2476 if (newpfid != NULL) {
2477 /* if the add fails, we still delete the out-of-date old link */
2478 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2482 rc = __mdd_xattr_set(env, mdd_obj,
2483 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2484 XATTR_NAME_LINK, 0, handle);
2490 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2491 oldlname->ln_namelen, oldlname->ln_name, rc,
2492 PFID(mdd_object_fid(mdd_obj)));
2494 if (buf->lb_vmalloc)
2495 /* if we vmalloced a large buffer drop it */
2501 const struct md_dir_operations mdd_dir_ops = {
2502 .mdo_is_subdir = mdd_is_subdir,
2503 .mdo_lookup = mdd_lookup,
2504 .mdo_create = mdd_create,
2505 .mdo_rename = mdd_rename,
2506 .mdo_link = mdd_link,
2507 .mdo_unlink = mdd_unlink,
2508 .mdo_name_insert = mdd_name_insert,
2509 .mdo_name_remove = mdd_name_remove,
2510 .mdo_rename_tgt = mdd_rename_tgt,
2511 .mdo_create_data = mdd_create_data