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,
553 __mdd_fid_rec(env, lf),
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);
1401 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
1405 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1408 rc = mdd_object_exists(mdd_obj);
1409 if (unlikely(rc == 0))
1411 else if (unlikely(rc < 0)) {
1412 CERROR("Object "DFID" locates on remote server\n",
1413 PFID(mdo2fid(mdd_obj)));
1417 /* The common filename length check. */
1418 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1419 RETURN(-ENAMETOOLONG);
1421 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1426 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1427 dt_try_as_dir(env, dir))) {
1429 rc = dir->do_index_ops->dio_lookup(env, dir,
1430 (struct dt_rec *)pack, key,
1431 mdd_object_capa(env, mdd_obj));
1433 rc = fid_unpack(pack, fid);
1442 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1443 const struct lu_name *lname, struct mdd_object *child,
1444 struct md_attr *ma, struct thandle *handle,
1445 const struct md_op_spec *spec)
1451 * Update attributes for child.
1454 * (1) the valid bits should be converted between Lustre and Linux;
1455 * (2) maybe, the child attributes should be set in OSD when creation.
1458 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1462 if (S_ISDIR(ma->ma_attr.la_mode)) {
1463 /* Add "." and ".." for newly created dir */
1464 __mdd_ref_add(env, child, handle);
1465 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1466 dot, handle, BYPASS_CAPA);
1468 rc = __mdd_index_insert_only(env, child, pfid,
1472 __mdd_ref_del(env, child, handle, 1);
1475 mdd_links_add(env, child, pfid, lname, handle);
1480 /* has not lock on pobj yet */
1481 static int mdd_create_sanity_check(const struct lu_env *env,
1482 struct md_object *pobj,
1483 const struct lu_name *lname,
1485 struct md_op_spec *spec)
1487 struct mdd_thread_info *info = mdd_env_info(env);
1488 struct lu_attr *la = &info->mti_la;
1489 struct lu_fid *fid = &info->mti_fid;
1490 struct mdd_object *obj = md2mdd_obj(pobj);
1491 struct mdd_device *m = mdo2mdd(pobj);
1492 int lookup = spec->sp_cr_lookup;
1497 if (mdd_is_dead_obj(obj))
1501 * In some cases this lookup is not needed - we know before if name
1502 * exists or not because MDT performs lookup for it.
1503 * name length check is done in lookup.
1507 * Check if the name already exist, though it will be checked in
1508 * _index_insert also, for avoiding rolling back if exists
1511 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1512 MAY_WRITE | MAY_EXEC);
1514 RETURN(rc ? : -EEXIST);
1517 * Check WRITE permission for the parent.
1518 * EXEC permission have been checked
1519 * when lookup before create already.
1521 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1528 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1532 if (la->la_mode & S_ISGID) {
1533 ma->ma_attr.la_gid = la->la_gid;
1534 if (S_ISDIR(ma->ma_attr.la_mode)) {
1535 ma->ma_attr.la_mode |= S_ISGID;
1536 ma->ma_attr.la_valid |= LA_MODE;
1540 switch (ma->ma_attr.la_mode & S_IFMT) {
1542 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1544 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1545 RETURN(-ENAMETOOLONG);
1565 * Create object and insert it into namespace.
1567 static int mdd_create(const struct lu_env *env,
1568 struct md_object *pobj,
1569 const struct lu_name *lname,
1570 struct md_object *child,
1571 struct md_op_spec *spec,
1574 struct mdd_thread_info *info = mdd_env_info(env);
1575 struct lu_attr *la = &info->mti_la_for_fix;
1576 struct md_attr *ma_acl = &info->mti_ma;
1577 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1578 struct mdd_object *son = md2mdd_obj(child);
1579 struct mdd_device *mdd = mdo2mdd(pobj);
1580 struct lu_attr *attr = &ma->ma_attr;
1581 struct lov_mds_md *lmm = NULL;
1582 struct thandle *handle;
1583 struct dynlock_handle *dlh;
1584 const char *name = lname->ln_name;
1585 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1586 int got_def_acl = 0;
1587 #ifdef HAVE_QUOTA_SUPPORT
1588 struct obd_device *obd = mdd->mdd_obd_dev;
1589 struct mds_obd *mds = &obd->u.mds;
1590 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1591 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1592 int quota_opc = 0, block_count = 0;
1593 int inode_pending[MAXQUOTAS] = { 0, 0 };
1594 int block_pending[MAXQUOTAS] = { 0, 0 };
1595 int parent_pending[MAXQUOTAS] = { 0, 0 };
1600 * Two operations have to be performed:
1602 * - an allocation of a new object (->do_create()), and
1604 * - an insertion into a parent index (->dio_insert()).
1606 * Due to locking, operation order is not important, when both are
1607 * successful, *but* error handling cases are quite different:
1609 * - if insertion is done first, and following object creation fails,
1610 * insertion has to be rolled back, but this operation might fail
1611 * also leaving us with dangling index entry.
1613 * - if creation is done first, is has to be undone if insertion
1614 * fails, leaving us with leaked space, which is neither good, nor
1617 * It seems that creation-first is simplest solution, but it is
1618 * sub-optimal in the frequent
1623 * case, because second mkdir is bound to create object, only to
1624 * destroy it immediately.
1626 * To avoid this follow local file systems that do double lookup:
1628 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1630 * 1. create (mdd_object_create_internal())
1632 * 2. insert (__mdd_index_insert(), lookup again)
1635 /* Sanity checks before big job. */
1636 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1640 #ifdef HAVE_QUOTA_SUPPORT
1641 if (mds->mds_quota) {
1642 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1644 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1648 quota_opc = FSFILT_OP_CREATE;
1649 mdd_quota_wrapper(&ma->ma_attr, qcids);
1650 mdd_quota_wrapper(la_tmp, qpids);
1651 /* get file quota for child */
1652 lquota_chkquota(mds_quota_interface_ref, obd, qcids,
1653 inode_pending, 1, NULL, 0, NULL, 0);
1654 switch (ma->ma_attr.la_mode & S_IFMT) {
1663 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1664 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1668 /* get block quota for child and parent */
1670 lquota_chkquota(mds_quota_interface_ref, obd,
1671 qcids, block_pending,
1673 LQUOTA_FLAGS_BLK, NULL, 0);
1675 lquota_chkquota(mds_quota_interface_ref, obd,
1676 qpids, parent_pending, 1, NULL,
1677 LQUOTA_FLAGS_BLK, NULL, 0);
1683 * No RPC inside the transaction, so OST objects should be created at
1686 if (S_ISREG(attr->la_mode)) {
1687 lmm_size = ma->ma_lmm_size;
1688 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1691 GOTO(out_pending, rc);
1694 if (!S_ISLNK(attr->la_mode)) {
1695 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1696 ma_acl->ma_acl = info->mti_xattr_buf;
1697 ma_acl->ma_need = MA_ACL_DEF;
1698 ma_acl->ma_valid = 0;
1700 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1701 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1702 mdd_read_unlock(env, mdd_pobj);
1705 else if (ma_acl->ma_valid & MA_ACL_DEF)
1709 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1710 handle = mdd_trans_start(env, mdd);
1712 GOTO(out_free, rc = PTR_ERR(handle));
1714 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1716 GOTO(out_trans, rc = -ENOMEM);
1718 mdd_write_lock(env, son, MOR_TGT_CHILD);
1719 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1721 mdd_write_unlock(env, son);
1727 #ifdef CONFIG_FS_POSIX_ACL
1729 struct lu_buf *acl_buf = &info->mti_buf;
1730 acl_buf->lb_buf = ma_acl->ma_acl;
1731 acl_buf->lb_len = ma_acl->ma_acl_size;
1733 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1735 mdd_write_unlock(env, son);
1738 ma->ma_attr.la_valid |= LA_MODE;
1743 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1744 son, ma, handle, spec);
1745 mdd_write_unlock(env, son);
1748 * Object has no links, so it will be destroyed when last
1749 * reference is released. (XXX not now.)
1755 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1756 name, S_ISDIR(attr->la_mode), handle,
1757 mdd_object_capa(env, mdd_pobj));
1764 /* No need mdd_lsm_sanity_check here */
1765 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1767 CERROR("error on stripe info copy %d \n", rc);
1770 if (lmm && lmm_size > 0) {
1771 /* Set Lov here, do not get lmm again later */
1772 memcpy(ma->ma_lmm, lmm, lmm_size);
1773 ma->ma_lmm_size = lmm_size;
1774 ma->ma_valid |= MA_LOV;
1777 if (S_ISLNK(attr->la_mode)) {
1778 struct md_ucred *uc = md_ucred(env);
1779 struct dt_object *dt = mdd_object_child(son);
1780 const char *target_name = spec->u.sp_symname;
1781 int sym_len = strlen(target_name);
1782 const struct lu_buf *buf;
1785 buf = mdd_buf_get_const(env, target_name, sym_len);
1786 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1787 mdd_object_capa(env, son),
1789 CFS_CAP_SYS_RESOURCE_MASK);
1794 GOTO(cleanup, rc = -EFAULT);
1798 la->la_valid = LA_CTIME | LA_MTIME;
1799 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1803 /* Return attr back. */
1804 rc = mdd_attr_get_internal_locked(env, son, ma);
1807 if (rc && created) {
1811 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1812 S_ISDIR(attr->la_mode),
1813 handle, BYPASS_CAPA);
1815 CERROR("error can not cleanup destroy %d\n",
1820 mdd_write_lock(env, son, MOR_TGT_CHILD);
1821 __mdd_ref_del(env, son, handle, 0);
1822 if (initialized && S_ISDIR(attr->la_mode))
1823 __mdd_ref_del(env, son, handle, 1);
1824 mdd_write_unlock(env, son);
1828 /* update lov_objid data, must be before transaction stop! */
1830 mdd_lov_objid_update(mdd, lmm);
1832 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1835 rc = mdd_changelog_ns_store(env, mdd,
1836 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1837 S_ISREG(attr->la_mode) ? CL_CREATE :
1838 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1839 son, mdd_pobj, NULL, lname, handle);
1840 mdd_trans_stop(env, mdd, rc, handle);
1842 /* finis lov_create stuff, free all temporary data */
1843 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1845 #ifdef HAVE_QUOTA_SUPPORT
1847 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1849 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1851 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1853 /* Trigger dqacq on the owner of child and parent. If failed,
1854 * the next call for lquota_chkquota will process it. */
1855 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1863 * Get locks on parents in proper order
1864 * RETURN: < 0 - error, rename_order if successful
1872 static int mdd_rename_order(const struct lu_env *env,
1873 struct mdd_device *mdd,
1874 struct mdd_object *src_pobj,
1875 struct mdd_object *tgt_pobj)
1877 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1881 if (src_pobj == tgt_pobj)
1882 RETURN(MDD_RN_SAME);
1884 /* compared the parent child relationship of src_p&tgt_p */
1885 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1887 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1890 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1903 /* has not mdd_write{read}_lock on any obj yet. */
1904 static int mdd_rename_sanity_check(const struct lu_env *env,
1905 struct mdd_object *src_pobj,
1906 struct mdd_object *tgt_pobj,
1907 struct mdd_object *sobj,
1908 struct mdd_object *tobj,
1914 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1917 /* XXX: when get here, sobj must NOT be NULL,
1918 * the other case has been processed in cml_rename
1919 * before mdd_rename and enable MDS_PERM_BYPASS. */
1922 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1926 /* XXX: when get here, "tobj == NULL" means tobj must
1927 * NOT exist (neither on remote MDS, such case has been
1928 * processed in cml_rename before mdd_rename and enable
1930 * So check may_create, but not check may_unlink. */
1932 rc = mdd_may_create(env, tgt_pobj, NULL,
1933 (src_pobj != tgt_pobj), 0);
1935 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1936 (src_pobj != tgt_pobj), 1);
1938 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1939 S_ISDIR(ma->ma_attr.la_mode))
1940 rc = __mdd_may_link(env, tgt_pobj);
1945 /* src object can be remote that is why we use only fid and type of object */
1946 static int mdd_rename(const struct lu_env *env,
1947 struct md_object *src_pobj, struct md_object *tgt_pobj,
1948 const struct lu_fid *lf, const struct lu_name *lsname,
1949 struct md_object *tobj, const struct lu_name *ltname,
1952 const char *sname = lsname->ln_name;
1953 const char *tname = ltname->ln_name;
1954 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1955 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1956 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1957 struct mdd_device *mdd = mdo2mdd(src_pobj);
1958 struct mdd_object *mdd_sobj = NULL; /* source object */
1959 struct mdd_object *mdd_tobj = NULL;
1960 struct dynlock_handle *sdlh, *tdlh;
1961 struct thandle *handle;
1962 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1963 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1967 #ifdef HAVE_QUOTA_SUPPORT
1968 struct obd_device *obd = mdd->mdd_obd_dev;
1969 struct mds_obd *mds = &obd->u.mds;
1970 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1971 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1972 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1973 int quota_copc = 0, quota_popc = 0;
1974 int rec_pending[MAXQUOTAS] = { 0, 0 };
1978 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1979 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1982 mdd_tobj = md2mdd_obj(tobj);
1984 #ifdef HAVE_QUOTA_SUPPORT
1985 if (mds->mds_quota) {
1986 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1988 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
1990 mdd_quota_wrapper(la_tmp, qspids);
1992 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
1996 mdd_data_get(env, mdd_tpobj, &data);
1997 quota_popc = FSFILT_OP_LINK;
1998 mdd_quota_wrapper(la_tmp, qtpids);
1999 /* get block quota for target parent */
2000 lquota_chkquota(mds_quota_interface_ref,
2002 rec_pending, 1, NULL,
2010 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2011 handle = mdd_trans_start(env, mdd);
2013 GOTO(out_pending, rc = PTR_ERR(handle));
2015 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2016 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2018 GOTO(cleanup_unlocked, rc);
2020 /* Get locks in determined order */
2021 if (rc == MDD_RN_SAME) {
2022 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2023 sname, MOR_SRC_PARENT);
2024 /* check hashes to determine do we need one lock or two */
2025 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2026 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2030 } else if (rc == MDD_RN_SRCTGT) {
2031 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2032 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2034 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2035 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2037 if (sdlh == NULL || tdlh == NULL)
2038 GOTO(cleanup, rc = -ENOMEM);
2040 mdd_sobj = mdd_object_find(env, mdd, lf);
2041 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2042 mdd_sobj, mdd_tobj, ma);
2046 /* Remove source name from source directory */
2047 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2048 mdd_object_capa(env, mdd_spobj));
2052 /* "mv dir1 dir2" needs "dir1/.." link update */
2053 if (is_dir && mdd_sobj) {
2054 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2055 mdd_object_capa(env, mdd_sobj));
2057 GOTO(fixup_spobj2, rc);
2059 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2060 handle, mdd_object_capa(env, mdd_sobj));
2062 GOTO(fixup_spobj, rc);
2066 /* Remove target name from target directory
2067 * Here tobj can be remote one, so we do index_delete unconditionally
2068 * and -ENOENT is allowed.
2070 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2071 mdd_object_capa(env, mdd_tpobj));
2074 /* tname might been renamed to something else */
2075 GOTO(fixup_spobj, rc);
2078 GOTO(fixup_spobj, rc);
2081 /* Insert new fid with target name into target dir */
2082 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2083 mdd_object_capa(env, mdd_tpobj));
2085 GOTO(fixup_tpobj, rc);
2087 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2088 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2090 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2092 la->la_valid = LA_CTIME;
2093 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2096 GOTO(fixup_tpobj, rc);
2099 /* Remove old target object
2100 * For tobj is remote case cmm layer has processed
2101 * and set tobj to NULL then. So when tobj is NOT NULL,
2102 * it must be local one.
2104 if (tobj && mdd_object_exists(mdd_tobj)) {
2105 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2106 if (mdd_is_dead_obj(mdd_tobj)) {
2107 mdd_write_unlock(env, mdd_tobj);
2108 /* shld not be dead, something is wrong */
2109 CERROR("tobj is dead, something is wrong\n");
2113 __mdd_ref_del(env, mdd_tobj, handle, 0);
2115 /* Remove dot reference. */
2117 __mdd_ref_del(env, mdd_tobj, handle, 1);
2119 la->la_valid = LA_CTIME;
2120 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2122 GOTO(fixup_tpobj, rc);
2124 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2125 mdd_write_unlock(env, mdd_tobj);
2127 GOTO(fixup_tpobj, rc);
2129 #ifdef HAVE_QUOTA_SUPPORT
2130 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2131 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2132 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2133 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2138 la->la_valid = LA_CTIME | LA_MTIME;
2139 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2141 GOTO(fixup_tpobj, rc);
2143 if (mdd_spobj != mdd_tpobj) {
2144 la->la_valid = LA_CTIME | LA_MTIME;
2145 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2149 if (rc == 0 && mdd_sobj) {
2150 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2151 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2152 mdo2fid(mdd_tpobj), ltname, handle);
2154 /* Old files might not have EA entry */
2155 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2157 mdd_write_unlock(env, mdd_sobj);
2158 /* We don't fail the transaction if the link ea can't be
2159 updated -- fid2path will use alternate lookup method. */
2167 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2170 CWARN("tp obj fix error %d\n",rc2);
2172 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2173 !mdd_is_dead_obj(mdd_tobj)) {
2174 rc2 = __mdd_index_insert(env, mdd_tpobj,
2175 mdo2fid(mdd_tobj), tname,
2180 CWARN("tp obj fix error %d\n",rc2);
2185 if (rc && is_dir && mdd_sobj) {
2186 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2190 CWARN("sp obj dotdot delete error %d\n",rc2);
2193 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2194 dotdot, handle, BYPASS_CAPA);
2196 CWARN("sp obj dotdot insert error %d\n",rc2);
2201 rc2 = __mdd_index_insert(env, mdd_spobj,
2202 lf, sname, is_dir, handle, BYPASS_CAPA);
2204 CWARN("sp obj fix error %d\n",rc2);
2207 if (likely(tdlh) && sdlh != tdlh)
2208 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2210 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2213 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2214 mdd_spobj, lf, lsname, handle);
2216 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2217 mdd_tpobj, lf, ltname, handle);
2219 mdd_trans_stop(env, mdd, rc, handle);
2221 mdd_object_put(env, mdd_sobj);
2223 #ifdef HAVE_QUOTA_SUPPORT
2224 if (mds->mds_quota) {
2226 lquota_pending_commit(mds_quota_interface_ref, obd,
2227 qtpids, rec_pending, 1);
2230 /* Trigger dqrel on the source owner of parent.
2231 * If failed, the next call for lquota_chkquota will
2233 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2234 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2236 /* Trigger dqrel on the target owner of child.
2237 * If failed, the next call for lquota_chkquota
2238 * will process it. */
2239 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2240 qtpids, rc, quota_copc);
2247 /** enable/disable storing of hardlink info */
2248 int mdd_linkea_enable = 1;
2249 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2250 "record hardlink info in EAs");
2252 /** Read the link EA into a temp buffer.
2253 * Uses the name_buf since it is generally large.
2254 * \retval IS_ERR err
2255 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2257 struct lu_buf *mdd_links_get(const struct lu_env *env,
2258 struct mdd_object *mdd_obj)
2261 struct lustre_capa *capa;
2262 struct link_ea_header *leh;
2265 /* First try a small buf */
2266 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2267 if (buf->lb_buf == NULL)
2268 return ERR_PTR(-ENOMEM);
2270 capa = mdd_object_capa(env, mdd_obj);
2271 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2272 if (rc == -ERANGE) {
2273 /* Buf was too small, figure out what we need. */
2276 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2279 buf = mdd_buf_alloc(env, rc);
2280 if (buf->lb_buf == NULL)
2281 return ERR_PTR(-ENOMEM);
2282 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2288 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2289 leh->leh_magic = LINK_EA_MAGIC;
2290 leh->leh_reccount = __swab32(leh->leh_reccount);
2291 leh->leh_len = __swab64(leh->leh_len);
2292 /* entries are swabbed by mdd_lee_unpack */
2294 if (leh->leh_magic != LINK_EA_MAGIC)
2295 return ERR_PTR(-EINVAL);
2296 if (leh->leh_reccount == 0)
2297 return ERR_PTR(-ENODATA);
2302 /** Pack a link_ea_entry.
2303 * All elements are stored as chars to avoid alignment issues.
2304 * Numbers are always big-endian
2305 * \param packbuf is a temp fid buffer
2306 * \retval record length
2308 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2309 const struct lu_fid *pfid, struct lu_fid* packbuf)
2314 fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2315 ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2316 strncpy(ptr, lname->ln_name, lname->ln_namelen);
2317 reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2318 sizeof(lee->lee_reclen);
2319 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2320 lee->lee_reclen[1] = reclen & 0xff;
2324 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2325 struct lu_name *lname, struct lu_fid *pfid)
2327 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2328 fid_unpack(&lee->lee_parent_fid, pfid);
2329 lname->ln_name = (char *)&lee->lee_parent_fid +
2330 lee->lee_parent_fid.fp_len;
2331 lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2332 sizeof(lee->lee_reclen);
2335 /** Add a record to the end of link ea buf */
2336 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2337 const struct lu_fid *pfid,
2338 const struct lu_name *lname)
2340 struct link_ea_header *leh;
2341 struct link_ea_entry *lee;
2344 if (lname == NULL || pfid == NULL)
2347 /* Make sure our buf is big enough for the new one */
2349 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2350 if (leh->leh_len + reclen > buf->lb_len) {
2351 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2356 lee = buf->lb_buf + leh->leh_len;
2357 reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2358 leh->leh_len += reclen;
2359 leh->leh_reccount++;
2363 /* For pathologic linkers, we don't want to spend lots of time scanning the
2364 * link ea. Limit ourseleves to something reasonable; links not in the EA
2365 * can be looked up via (slower) parent lookup.
2367 #define LINKEA_MAX_COUNT 128
2369 static int mdd_links_add(const struct lu_env *env,
2370 struct mdd_object *mdd_obj,
2371 const struct lu_fid *pfid,
2372 const struct lu_name *lname,
2373 struct thandle *handle)
2376 struct link_ea_header *leh;
2380 if (!mdd_linkea_enable)
2383 buf = mdd_links_get(env, mdd_obj);
2386 if (rc != -ENODATA) {
2387 CERROR("link_ea read failed %d "DFID"\n", rc,
2388 PFID(mdd_object_fid(mdd_obj)));
2391 /* empty EA; start one */
2392 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2393 if (buf->lb_buf == NULL)
2396 leh->leh_magic = LINK_EA_MAGIC;
2397 leh->leh_len = sizeof(struct link_ea_header);
2398 leh->leh_reccount = 0;
2402 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2405 rc = __mdd_links_add(env, buf, pfid, lname);
2410 rc = __mdd_xattr_set(env, mdd_obj,
2411 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2412 XATTR_NAME_LINK, 0, handle);
2414 CERROR("link_ea add failed %d "DFID"\n", rc,
2415 PFID(mdd_object_fid(mdd_obj)));
2417 if (buf->lb_vmalloc)
2418 /* if we vmalloced a large buffer drop it */
2424 static int mdd_links_rename(const struct lu_env *env,
2425 struct mdd_object *mdd_obj,
2426 const struct lu_fid *oldpfid,
2427 const struct lu_name *oldlname,
2428 const struct lu_fid *newpfid,
2429 const struct lu_name *newlname,
2430 struct thandle *handle)
2433 struct link_ea_header *leh;
2434 struct link_ea_entry *lee;
2435 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2436 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2442 if (!mdd_linkea_enable)
2445 if (mdd_obj->mod_flags & DEAD_OBJ)
2446 /* No more links, don't bother */
2449 buf = mdd_links_get(env, mdd_obj);
2452 CERROR("link_ea read failed %d "DFID"\n",
2453 rc, PFID(mdd_object_fid(mdd_obj)));
2457 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2459 /* Find the old record */
2460 for(count = 0; count <= leh->leh_reccount; count++) {
2461 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2462 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2463 lu_fid_eq(tmpfid, oldpfid) &&
2464 (strncmp(tmpname->ln_name, oldlname->ln_name,
2465 tmpname->ln_namelen) == 0))
2467 lee = (struct link_ea_entry *)((char *)lee + reclen);
2469 if (count > leh->leh_reccount) {
2470 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2471 oldlname->ln_namelen, oldlname->ln_name);
2472 GOTO(out, rc = -ENOENT);
2475 /* Remove the old record */
2476 leh->leh_reccount--;
2477 leh->leh_len -= reclen;
2478 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2481 /* If renaming, add the new record */
2482 if (newpfid != NULL) {
2483 /* if the add fails, we still delete the out-of-date old link */
2484 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2488 rc = __mdd_xattr_set(env, mdd_obj,
2489 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2490 XATTR_NAME_LINK, 0, handle);
2496 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2497 oldlname->ln_namelen, oldlname->ln_name, rc,
2498 PFID(mdd_object_fid(mdd_obj)));
2500 if (buf->lb_vmalloc)
2501 /* if we vmalloced a large buffer drop it */
2507 const struct md_dir_operations mdd_dir_ops = {
2508 .mdo_is_subdir = mdd_is_subdir,
2509 .mdo_lookup = mdd_lookup,
2510 .mdo_create = mdd_create,
2511 .mdo_rename = mdd_rename,
2512 .mdo_link = mdd_link,
2513 .mdo_unlink = mdd_unlink,
2514 .mdo_name_insert = mdd_name_insert,
2515 .mdo_name_remove = mdd_name_remove,
2516 .mdo_rename_tgt = mdd_rename_tgt,
2517 .mdo_create_data = mdd_create_data