1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_fid.h>
61 #include "mdd_internal.h"
63 static const char dot[] = ".";
64 static const char dotdot[] = "..";
66 static struct lu_name lname_dotdot = {
71 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_name *lname, struct lu_fid* fid,
74 static int mdd_links_add(const struct lu_env *env,
75 struct mdd_object *mdd_obj,
76 const struct lu_fid *pfid,
77 const struct lu_name *lname,
78 struct thandle *handle);
79 static int mdd_links_rename(const struct lu_env *env,
80 struct mdd_object *mdd_obj,
81 const struct lu_fid *oldpfid,
82 const struct lu_name *oldlname,
83 const struct lu_fid *newpfid,
84 const struct lu_name *newlname,
85 struct thandle *handle);
88 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
89 const struct lu_name *lname, struct lu_fid* fid, int mask)
91 const char *name = lname->ln_name;
92 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
93 struct dynlock_handle *dlh;
96 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
97 if (unlikely(dlh == NULL))
99 rc = __mdd_lookup(env, pobj, lname, fid, mask);
100 mdd_pdo_read_unlock(env, mdd_obj, dlh);
105 int mdd_lookup(const struct lu_env *env,
106 struct md_object *pobj, const struct lu_name *lname,
107 struct lu_fid* fid, struct md_op_spec *spec)
111 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
115 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
118 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
122 * For root fid use special function, which does not compare version component
123 * of fid. Version component is different for root fids on all MDTs.
125 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
127 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
128 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
132 * return 1: if lf is the fid of the ancestor of p1;
135 * return -EREMOTE: if remote object is found, in this
136 * case fid of remote object is saved to @pf;
138 * otherwise: values < 0, errors.
140 static int mdd_is_parent(const struct lu_env *env,
141 struct mdd_device *mdd,
142 struct mdd_object *p1,
143 const struct lu_fid *lf,
146 struct mdd_object *parent = NULL;
151 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
152 pfid = &mdd_env_info(env)->mti_fid;
154 /* Check for root first. */
155 if (mdd_is_root(mdd, mdo2fid(p1)))
159 /* this is done recursively, bypass capa for each obj */
160 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
161 rc = mdd_parent_fid(env, p1, pfid);
164 if (mdd_is_root(mdd, pfid))
166 if (lu_fid_eq(pfid, lf))
169 mdd_object_put(env, parent);
170 parent = mdd_object_find(env, mdd, pfid);
172 /* cross-ref parent */
173 if (parent == NULL) {
176 GOTO(out, rc = -EREMOTE);
177 } else if (IS_ERR(parent))
178 GOTO(out, rc = PTR_ERR(parent));
183 if (parent && !IS_ERR(parent))
184 mdd_object_put(env, parent);
189 * No permission check is needed.
191 * returns 1: if fid is ancestor of @mo;
192 * returns 0: if fid is not a ancestor of @mo;
194 * returns EREMOTE if remote object is found, fid of remote object is saved to
197 * returns < 0: if error
199 static int mdd_is_subdir(const struct lu_env *env,
200 struct md_object *mo, const struct lu_fid *fid,
203 struct mdd_device *mdd = mdo2mdd(mo);
207 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
210 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
214 } else if (rc == 1) {
215 /* found @fid is parent */
223 * Check that @dir contains no entries except (possibly) dot and dotdot.
228 * -ENOTDIR not a directory object
229 * -ENOTEMPTY not empty
233 static int mdd_dir_is_empty(const struct lu_env *env,
234 struct mdd_object *dir)
237 struct dt_object *obj;
238 const struct dt_it_ops *iops;
242 obj = mdd_object_child(dir);
243 if (!dt_try_as_dir(env, obj))
246 iops = &obj->do_index_ops->dio_it;
247 it = iops->init(env, obj, BYPASS_CAPA);
249 result = iops->get(env, it, (const void *)"");
252 for (result = 0, i = 0; result == 0 && i < 3; ++i)
253 result = iops->next(env, it);
256 else if (result == +1)
258 } else if (result == 0)
260 * Huh? Index contains no zero key?
271 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
273 struct mdd_device *m = mdd_obj2mdd_dev(obj);
274 struct lu_attr *la = &mdd_env_info(env)->mti_la;
278 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
283 * Subdir count limitation can be broken through.
285 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
286 !S_ISDIR(la->la_mode))
293 * Check whether it may create the cobj under the pobj.
296 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
297 struct mdd_object *cobj, int check_perm, int check_nlink)
302 if (cobj && mdd_object_exists(cobj))
305 if (mdd_is_dead_obj(pobj))
309 rc = mdd_permission_internal_locked(env, pobj, NULL,
310 MAY_WRITE | MAY_EXEC,
313 if (!rc && check_nlink)
314 rc = __mdd_may_link(env, pobj);
320 * Check whether can unlink from the pobj in the case of "cobj == NULL".
322 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
323 const struct md_attr *ma)
328 if (mdd_is_dead_obj(pobj))
331 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
332 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
335 rc = mdd_permission_internal_locked(env, pobj, NULL,
336 MAY_WRITE | MAY_EXEC,
341 if (mdd_is_append(pobj))
348 * pobj == NULL is remote ops case, under such case, pobj's
349 * VTX feature has been checked already, no need check again.
351 static inline int mdd_is_sticky(const struct lu_env *env,
352 struct mdd_object *pobj,
353 struct mdd_object *cobj)
355 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
356 struct md_ucred *uc = md_ucred(env);
360 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
364 if (!(tmp_la->la_mode & S_ISVTX) ||
365 (tmp_la->la_uid == uc->mu_fsuid))
369 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
373 if (tmp_la->la_uid == uc->mu_fsuid)
376 return !mdd_capable(uc, CFS_CAP_FOWNER);
380 * Check whether it may delete the cobj from the pobj.
383 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
384 struct mdd_object *cobj, struct md_attr *ma,
385 int check_perm, int check_empty)
391 if (!mdd_object_exists(cobj))
395 if (mdd_is_dead_obj(pobj))
399 rc = mdd_permission_internal_locked(env, pobj, NULL,
400 MAY_WRITE | MAY_EXEC,
406 if (mdd_is_append(pobj))
410 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
411 mdd_is_sticky(env, pobj, cobj))
414 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
417 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
418 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
421 if (S_ISDIR(ma->ma_attr.la_mode)) {
422 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
424 if (!S_ISDIR(mdd_object_type(cobj)))
427 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
429 } else if (S_ISDIR(mdd_object_type(cobj)))
432 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
433 rc = mdd_dir_is_empty(env, cobj);
440 * has mdd_write_lock on src already, but not on tgt yet
442 int mdd_link_sanity_check(const struct lu_env *env,
443 struct mdd_object *tgt_obj,
444 const struct lu_name *lname,
445 struct mdd_object *src_obj)
447 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
451 if (mdd_is_dead_obj(src_obj))
454 /* Local ops, no lookup before link, check filename length here. */
455 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
456 RETURN(-ENAMETOOLONG);
458 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
461 if (S_ISDIR(mdd_object_type(src_obj)))
464 LASSERT(src_obj != tgt_obj);
466 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
471 rc = __mdd_may_link(env, src_obj);
477 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
478 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
479 * (maybe too large to be represented). It is a trick to break through the
480 * "i_nlink" limitation for subdir count.
482 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
483 struct thandle *handle)
485 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
486 struct mdd_device *m = mdd_obj2mdd_dev(obj);
488 if (!mdd_is_mnlink(obj)) {
489 if (S_ISDIR(mdd_object_type(obj))) {
490 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
493 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
494 obj->mod_flags |= MNLINK_OBJ;
495 tmp_la->la_nlink = 1;
496 tmp_la->la_valid = LA_NLINK;
497 mdd_attr_set_internal(env, obj, tmp_la, handle,
502 mdo_ref_add(env, obj, handle);
506 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
507 struct thandle *handle, int is_dot)
509 if (!mdd_is_mnlink(obj) || is_dot)
510 mdo_ref_del(env, obj, handle);
513 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
514 const char *name, struct thandle *handle,
515 struct lustre_capa *capa)
517 struct dt_object *next = mdd_object_child(pobj);
521 if (dt_try_as_dir(env, next)) {
522 rc = next->do_index_ops->dio_delete(env, next,
523 (struct dt_key *)name,
531 static int __mdd_index_insert_only(const struct lu_env *env,
532 struct mdd_object *pobj,
533 const struct lu_fid *lf, const char *name,
534 struct thandle *handle,
535 struct lustre_capa *capa)
537 struct dt_object *next = mdd_object_child(pobj);
541 if (dt_try_as_dir(env, next)) {
542 struct md_ucred *uc = md_ucred(env);
544 rc = next->do_index_ops->dio_insert(env, next,
545 __mdd_fid_rec(env, lf),
546 (const struct dt_key *)name,
547 handle, capa, uc->mu_cap &
548 CFS_CAP_SYS_RESOURCE_MASK);
555 /* insert named index, add reference if isdir */
556 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
557 const struct lu_fid *lf, const char *name, int is_dir,
558 struct thandle *handle, struct lustre_capa *capa)
563 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
564 if (rc == 0 && is_dir) {
565 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
566 __mdd_ref_add(env, pobj, handle);
567 mdd_write_unlock(env, pobj);
572 /* delete named index, drop reference if isdir */
573 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
574 const char *name, int is_dir, struct thandle *handle,
575 struct lustre_capa *capa)
580 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
581 if (rc == 0 && is_dir) {
584 if (name != NULL && name[0] == '.' && name[1] == 0)
586 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
587 __mdd_ref_del(env, pobj, handle, is_dot);
588 mdd_write_unlock(env, pobj);
595 /** Store a namespace change changelog record
596 * If this fails, we must fail the whole transaction; we don't
597 * want the change to commit without the log entry.
598 * \param target - mdd_object of change
599 * \param parent - parent dir/object
600 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
601 * \param tname - target name string
602 * \param handle - transacion handle
604 static int mdd_changelog_ns_store(const struct lu_env *env,
605 struct mdd_device *mdd,
606 enum changelog_rec_type type,
607 struct mdd_object *target,
608 struct mdd_object *parent,
609 const struct lu_fid *tf,
610 const struct lu_name *tname,
611 struct thandle *handle)
613 const struct lu_fid *tfid;
614 const struct lu_fid *tpfid = mdo2fid(parent);
615 struct llog_changelog_rec *rec;
621 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
624 LASSERT(parent != NULL);
625 LASSERT(tname != NULL);
626 LASSERT(handle != NULL);
629 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
630 buf = mdd_buf_alloc(env, reclen);
631 if (buf->lb_buf == NULL)
633 rec = (struct llog_changelog_rec *)buf->lb_buf;
635 rec->cr_flags = CLF_VERSION;
636 rec->cr_type = (__u32)type;
637 tfid = tf ? tf : mdo2fid(target);
638 rec->cr_tfid = *tfid;
639 rec->cr_pfid = *tpfid;
640 rec->cr_namelen = tname->ln_namelen;
641 memcpy(rec->cr_name, tname->ln_name, rec->cr_namelen);
643 target->mod_cltime = cfs_time_current_64();
645 rc = mdd_changelog_llog_write(mdd, rec, handle);
647 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
648 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
655 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
656 struct md_object *src_obj, const struct lu_name *lname,
659 const char *name = lname->ln_name;
660 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
661 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
662 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
663 struct mdd_device *mdd = mdo2mdd(src_obj);
664 struct dynlock_handle *dlh;
665 struct thandle *handle;
666 #ifdef HAVE_QUOTA_SUPPORT
667 struct obd_device *obd = mdd->mdd_obd_dev;
668 struct mds_obd *mds = &obd->u.mds;
669 unsigned int qids[MAXQUOTAS] = { 0, 0 };
670 int quota_opc = 0, rec_pending = 0;
675 #ifdef HAVE_QUOTA_SUPPORT
676 if (mds->mds_quota) {
677 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
679 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
682 mdd_data_get(env, mdd_tobj, &data);
683 quota_opc = FSFILT_OP_LINK;
684 mdd_quota_wrapper(la_tmp, qids);
685 /* get block quota for parent */
686 lquota_chkquota(mds_quota_interface_ref, obd,
687 qids[USRQUOTA], qids[GRPQUOTA], 1,
688 &rec_pending, NULL, LQUOTA_FLAGS_BLK,
694 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
695 handle = mdd_trans_start(env, mdd);
697 GOTO(out_pending, rc = PTR_ERR(handle));
699 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
701 GOTO(out_trans, rc = -ENOMEM);
702 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
704 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
706 GOTO(out_unlock, rc);
708 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
710 mdd_object_capa(env, mdd_tobj));
712 GOTO(out_unlock, rc);
714 __mdd_ref_add(env, mdd_sobj, handle);
716 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
717 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
719 la->la_valid = LA_CTIME | LA_MTIME;
720 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
722 GOTO(out_unlock, rc);
724 la->la_valid = LA_CTIME;
725 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
727 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj), lname, handle);
731 mdd_write_unlock(env, mdd_sobj);
732 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
735 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, mdd_sobj,
736 mdd_tobj, NULL, lname, handle);
737 mdd_trans_stop(env, mdd, rc, handle);
739 #ifdef HAVE_QUOTA_SUPPORT
742 lquota_pending_commit(mds_quota_interface_ref, obd,
743 qids[USRQUOTA], qids[GRPQUOTA],
745 /* Trigger dqacq for the parent owner. If failed,
746 * the next call for lquota_chkquota will process it. */
747 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
754 /* caller should take a lock before calling */
755 int mdd_finish_unlink(const struct lu_env *env,
756 struct mdd_object *obj, struct md_attr *ma,
763 rc = mdd_iattr_get(env, obj, ma);
764 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
765 /* add new orphan and the object
766 * will be deleted during mdd_close() */
767 if (obj->mod_count) {
768 rc = __mdd_orphan_add(env, obj, th);
770 obj->mod_flags |= ORPHAN_OBJ;
771 CDEBUG(D_HA, "Object "DFID" is going to be "
772 "an orphan, open count = %d\n",
773 PFID(mdd_object_fid(obj)),
778 obj->mod_flags |= DEAD_OBJ;
779 if (!(obj->mod_flags & ORPHAN_OBJ)) {
780 rc = mdd_object_kill(env, obj, ma);
787 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
794 * has mdd_write_lock on cobj already, but not on pobj yet
796 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
797 struct mdd_object *cobj, struct md_attr *ma)
802 if (mdd_is_dead_obj(cobj))
805 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
810 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
811 struct md_object *cobj, const struct lu_name *lname,
814 const char *name = lname->ln_name;
815 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
816 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
817 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
818 struct mdd_device *mdd = mdo2mdd(pobj);
819 struct dynlock_handle *dlh;
820 struct thandle *handle;
821 #ifdef HAVE_QUOTA_SUPPORT
822 struct obd_device *obd = mdd->mdd_obd_dev;
823 struct mds_obd *mds = &obd->u.mds;
824 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
825 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
828 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
832 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
833 PFID(mdd_object_fid(mdd_cobj)));
835 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
839 handle = mdd_trans_start(env, mdd);
841 RETURN(PTR_ERR(handle));
843 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
845 GOTO(out_trans, rc = -ENOMEM);
846 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
848 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
852 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
853 mdd_object_capa(env, mdd_pobj));
857 __mdd_ref_del(env, mdd_cobj, handle, 0);
860 __mdd_ref_del(env, mdd_cobj, handle, 1);
862 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
863 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
865 la->la_valid = LA_CTIME | LA_MTIME;
866 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
870 la->la_valid = LA_CTIME;
871 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
875 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
876 #ifdef HAVE_QUOTA_SUPPORT
877 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
878 ma->ma_attr.la_nlink == 0) {
879 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
881 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
883 mdd_quota_wrapper(la_tmp, qpids);
884 if (mdd_cobj->mod_count == 0) {
885 quota_opc = FSFILT_OP_UNLINK;
886 mdd_quota_wrapper(&ma->ma_attr, qcids);
888 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
895 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
896 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
899 /* old files may not have link ea; ignore errors */
900 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
901 lname, NULL, NULL, handle);
905 mdd_write_unlock(env, mdd_cobj);
906 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
909 rc = mdd_changelog_ns_store(env, mdd,
910 is_dir ? CL_RMDIR : CL_UNLINK,
911 mdd_cobj, mdd_pobj, NULL, lname,
914 mdd_trans_stop(env, mdd, rc, handle);
915 #ifdef HAVE_QUOTA_SUPPORT
917 /* Trigger dqrel on the owner of child and parent. If failed,
918 * the next call for lquota_chkquota will process it. */
919 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
925 /* has not lock on pobj yet */
926 static int mdd_ni_sanity_check(const struct lu_env *env,
927 struct md_object *pobj,
928 const struct md_attr *ma)
930 struct mdd_object *obj = md2mdd_obj(pobj);
934 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
937 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
945 static int mdd_name_insert(const struct lu_env *env,
946 struct md_object *pobj,
947 const struct lu_name *lname,
948 const struct lu_fid *fid,
949 const struct md_attr *ma)
951 const char *name = lname->ln_name;
952 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
953 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
954 struct mdd_device *mdd = mdo2mdd(pobj);
955 struct dynlock_handle *dlh;
956 struct thandle *handle;
957 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
958 #ifdef HAVE_QUOTA_SUPPORT
959 struct md_ucred *uc = md_ucred(env);
960 struct obd_device *obd = mdd->mdd_obd_dev;
961 struct mds_obd *mds = &obd->u.mds;
962 unsigned int qids[MAXQUOTAS] = { 0, 0 };
963 int quota_opc = 0, rec_pending = 0;
964 cfs_cap_t save = uc->mu_cap;
969 #ifdef HAVE_QUOTA_SUPPORT
970 if (mds->mds_quota) {
971 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
972 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
974 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
977 mdd_data_get(env, mdd_obj, &data);
978 quota_opc = FSFILT_OP_LINK;
979 mdd_quota_wrapper(la_tmp, qids);
980 /* get block quota for parent */
981 lquota_chkquota(mds_quota_interface_ref, obd,
982 qids[USRQUOTA], qids[GRPQUOTA],
983 1, &rec_pending, NULL,
984 LQUOTA_FLAGS_BLK, data, 1);
987 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
991 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
992 handle = mdd_trans_start(env, mdo2mdd(pobj));
994 GOTO(out_pending, rc = PTR_ERR(handle));
996 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
998 GOTO(out_trans, rc = -ENOMEM);
1000 rc = mdd_ni_sanity_check(env, pobj, ma);
1002 GOTO(out_unlock, rc);
1004 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1005 handle, BYPASS_CAPA);
1007 GOTO(out_unlock, rc);
1010 * For some case, no need update obj's ctime (LA_CTIME is not set),
1012 * For other cases, update obj's ctime (LA_CTIME is set),
1015 if (ma->ma_attr.la_valid & LA_CTIME) {
1016 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1017 la->la_valid = LA_CTIME | LA_MTIME;
1018 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1023 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1025 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1027 #ifdef HAVE_QUOTA_SUPPORT
1028 if (mds->mds_quota) {
1031 lquota_pending_commit(mds_quota_interface_ref,
1032 obd, qids[USRQUOTA],
1035 /* Trigger dqacq for the parent owner. If failed,
1036 * the next call for lquota_chkquota will process it*/
1037 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1047 /* has not lock on pobj yet */
1048 static int mdd_nr_sanity_check(const struct lu_env *env,
1049 struct md_object *pobj,
1050 const struct md_attr *ma)
1052 struct mdd_object *obj = md2mdd_obj(pobj);
1056 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1059 rc = mdd_may_unlink(env, obj, ma);
1065 * Partial operation.
1067 static int mdd_name_remove(const struct lu_env *env,
1068 struct md_object *pobj,
1069 const struct lu_name *lname,
1070 const struct md_attr *ma)
1072 const char *name = lname->ln_name;
1073 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1074 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1075 struct mdd_device *mdd = mdo2mdd(pobj);
1076 struct dynlock_handle *dlh;
1077 struct thandle *handle;
1078 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1079 #ifdef HAVE_QUOTA_SUPPORT
1080 struct obd_device *obd = mdd->mdd_obd_dev;
1081 struct mds_obd *mds = &obd->u.mds;
1082 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1088 #ifdef HAVE_QUOTA_SUPPORT
1089 if (mds->mds_quota) {
1090 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1092 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1094 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1095 mdd_quota_wrapper(la_tmp, qids);
1099 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
1100 handle = mdd_trans_start(env, mdd);
1102 GOTO(out_pending, rc = PTR_ERR(handle));
1104 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1106 GOTO(out_trans, rc = -ENOMEM);
1108 rc = mdd_nr_sanity_check(env, pobj, ma);
1110 GOTO(out_unlock, rc);
1112 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1113 handle, BYPASS_CAPA);
1115 GOTO(out_unlock, rc);
1118 * For some case, no need update obj's ctime (LA_CTIME is not set),
1120 * For other cases, update obj's ctime (LA_CTIME is set),
1123 if (ma->ma_attr.la_valid & LA_CTIME) {
1124 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1125 la->la_valid = LA_CTIME | LA_MTIME;
1126 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1131 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1133 mdd_trans_stop(env, mdd, rc, handle);
1135 #ifdef HAVE_QUOTA_SUPPORT
1136 /* Trigger dqrel for the parent owner.
1137 * If failed, the next call for lquota_chkquota will process it. */
1139 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1147 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1149 static int mdd_rt_sanity_check(const struct lu_env *env,
1150 struct mdd_object *tgt_pobj,
1151 struct mdd_object *tobj,
1157 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1160 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1161 * exist. In fact, tobj must exist, otherwise the call trace will be:
1162 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1163 * When get here, tobj must be NOT NULL, the other case has been
1164 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1166 * So check may_delete, but not check nlink of tgt_pobj. */
1168 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1173 /* Partial rename op on slave MDD */
1174 static int mdd_rename_tgt(const struct lu_env *env,
1175 struct md_object *pobj, struct md_object *tobj,
1176 const struct lu_fid *lf, const struct lu_name *lname,
1179 const char *name = lname->ln_name;
1180 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1181 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1182 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1183 struct mdd_device *mdd = mdo2mdd(pobj);
1184 struct dynlock_handle *dlh;
1185 struct thandle *handle;
1186 #ifdef HAVE_QUOTA_SUPPORT
1187 struct obd_device *obd = mdd->mdd_obd_dev;
1188 struct mds_obd *mds = &obd->u.mds;
1189 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1190 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1191 int quota_opc = 0, rec_pending = 0;
1196 #ifdef HAVE_QUOTA_SUPPORT
1197 if (mds->mds_quota && !tobj) {
1198 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1200 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1203 mdd_data_get(env, mdd_tpobj, &data);
1204 quota_opc = FSFILT_OP_LINK;
1205 mdd_quota_wrapper(la_tmp, qpids);
1206 /* get block quota for target parent */
1207 lquota_chkquota(mds_quota_interface_ref, obd,
1208 qpids[USRQUOTA], qpids[GRPQUOTA], 1,
1209 &rec_pending, NULL, LQUOTA_FLAGS_BLK,
1214 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1215 handle = mdd_trans_start(env, mdd);
1217 GOTO(out_pending, rc = PTR_ERR(handle));
1219 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1221 GOTO(out_trans, rc = -ENOMEM);
1223 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1225 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1230 * If rename_tgt is called then we should just re-insert name with
1231 * correct fid, no need to dec/inc parent nlink if obj is dir.
1233 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1237 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1242 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1243 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1245 la->la_valid = LA_CTIME | LA_MTIME;
1246 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1251 * For tobj is remote case cmm layer has processed
1252 * and pass NULL tobj to here. So when tobj is NOT NULL,
1253 * it must be local one.
1255 if (tobj && mdd_object_exists(mdd_tobj)) {
1256 __mdd_ref_del(env, mdd_tobj, handle, 0);
1258 /* Remove dot reference. */
1259 if (S_ISDIR(ma->ma_attr.la_mode))
1260 __mdd_ref_del(env, mdd_tobj, handle, 1);
1262 la->la_valid = LA_CTIME;
1263 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1267 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1271 #ifdef HAVE_QUOTA_SUPPORT
1272 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1273 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1274 quota_opc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1275 mdd_quota_wrapper(&ma->ma_attr, qcids);
1282 mdd_write_unlock(env, mdd_tobj);
1283 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1286 /* Bare EXT record with no RENAME in front of it signifies
1287 a partial slave op */
1288 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
1289 mdd_tpobj, NULL, lname, handle);
1291 mdd_trans_stop(env, mdd, rc, handle);
1293 #ifdef HAVE_QUOTA_SUPPORT
1294 if (mds->mds_quota) {
1296 lquota_pending_commit(mds_quota_interface_ref, obd,
1301 /* Trigger dqrel/dqacq on the target owner of child and
1302 * parent. If failed, the next call for lquota_chkquota
1303 * will process it. */
1304 lquota_adjust(mds_quota_interface_ref, obd, qcids,
1305 qpids, rc, quota_opc);
1312 * The permission has been checked when obj created, no need check again.
1314 static int mdd_cd_sanity_check(const struct lu_env *env,
1315 struct mdd_object *obj)
1320 if (!obj || mdd_is_dead_obj(obj))
1327 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1328 struct md_object *cobj, const struct md_op_spec *spec,
1331 struct mdd_device *mdd = mdo2mdd(cobj);
1332 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1333 struct mdd_object *son = md2mdd_obj(cobj);
1334 struct lu_attr *attr = &ma->ma_attr;
1335 struct lov_mds_md *lmm = NULL;
1337 struct thandle *handle;
1341 rc = mdd_cd_sanity_check(env, son);
1345 if (!md_should_create(spec->sp_cr_flags))
1348 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1353 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1354 handle = mdd_trans_start(env, mdd);
1356 GOTO(out_free, rc = PTR_ERR(handle));
1359 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1360 * Should this be fixed?
1363 /* Replay creates has objects already */
1365 if (spec->no_create) {
1366 CDEBUG(D_INFO, "we already have lov ea\n");
1367 rc = mdd_lov_set_md(env, mdd_pobj, son,
1368 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1369 spec->u.sp_ea.eadatalen, handle, 0);
1372 /* No need mdd_lsm_sanity_check here */
1373 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1374 lmm_size, handle, 0);
1377 rc = mdd_attr_get_internal_locked(env, son, ma);
1379 /* update lov_objid data, must be before transaction stop! */
1381 mdd_lov_objid_update(mdd, lmm);
1383 mdd_trans_stop(env, mdd, rc, handle);
1385 /* Finish mdd_lov_create() stuff. */
1386 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1390 /* Get fid from name and parent */
1392 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1393 const struct lu_name *lname, struct lu_fid* fid, int mask)
1395 const char *name = lname->ln_name;
1396 const struct dt_key *key = (const struct dt_key *)name;
1397 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1398 struct mdd_device *m = mdo2mdd(pobj);
1399 struct dt_object *dir = mdd_object_child(mdd_obj);
1400 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
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 *)pack, key,
1430 mdd_object_capa(env, mdd_obj));
1432 rc = fid_unpack(pack, fid);
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,
1473 rc2 = __mdd_index_delete(env, child, dot, 1,
1474 handle, BYPASS_CAPA);
1476 CERROR("Failure to cleanup after dotdot"
1477 " creation: %d (%d)\n", rc2, rc);
1482 mdd_links_add(env, child, pfid, lname, handle);
1487 /* has not lock on pobj yet */
1488 static int mdd_create_sanity_check(const struct lu_env *env,
1489 struct md_object *pobj,
1490 const struct lu_name *lname,
1492 struct md_op_spec *spec)
1494 struct mdd_thread_info *info = mdd_env_info(env);
1495 struct lu_attr *la = &info->mti_la;
1496 struct lu_fid *fid = &info->mti_fid;
1497 struct mdd_object *obj = md2mdd_obj(pobj);
1498 struct mdd_device *m = mdo2mdd(pobj);
1499 int lookup = spec->sp_cr_lookup;
1504 if (mdd_is_dead_obj(obj))
1508 * In some cases this lookup is not needed - we know before if name
1509 * exists or not because MDT performs lookup for it.
1510 * name length check is done in lookup.
1514 * Check if the name already exist, though it will be checked in
1515 * _index_insert also, for avoiding rolling back if exists
1518 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1519 MAY_WRITE | MAY_EXEC);
1521 RETURN(rc ? : -EEXIST);
1524 * Check WRITE permission for the parent.
1525 * EXEC permission have been checked
1526 * when lookup before create already.
1528 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1535 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1539 if (la->la_mode & S_ISGID) {
1540 ma->ma_attr.la_gid = la->la_gid;
1541 if (S_ISDIR(ma->ma_attr.la_mode)) {
1542 ma->ma_attr.la_mode |= S_ISGID;
1543 ma->ma_attr.la_valid |= LA_MODE;
1547 switch (ma->ma_attr.la_mode & S_IFMT) {
1549 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1551 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1552 RETURN(-ENAMETOOLONG);
1572 * Create object and insert it into namespace.
1574 static int mdd_create(const struct lu_env *env,
1575 struct md_object *pobj,
1576 const struct lu_name *lname,
1577 struct md_object *child,
1578 struct md_op_spec *spec,
1581 struct mdd_thread_info *info = mdd_env_info(env);
1582 struct lu_attr *la = &info->mti_la_for_fix;
1583 struct md_attr *ma_acl = &info->mti_ma;
1584 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1585 struct mdd_object *son = md2mdd_obj(child);
1586 struct mdd_device *mdd = mdo2mdd(pobj);
1587 struct lu_attr *attr = &ma->ma_attr;
1588 struct lov_mds_md *lmm = NULL;
1589 struct thandle *handle;
1590 struct dynlock_handle *dlh;
1591 const char *name = lname->ln_name;
1592 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1593 int got_def_acl = 0;
1594 #ifdef HAVE_QUOTA_SUPPORT
1595 struct obd_device *obd = mdd->mdd_obd_dev;
1596 struct mds_obd *mds = &obd->u.mds;
1597 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1598 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1599 int quota_opc = 0, block_count = 0;
1600 int inode_pending = 0, block_pending = 0, parent_pending = 0;
1605 * Two operations have to be performed:
1607 * - an allocation of a new object (->do_create()), and
1609 * - an insertion into a parent index (->dio_insert()).
1611 * Due to locking, operation order is not important, when both are
1612 * successful, *but* error handling cases are quite different:
1614 * - if insertion is done first, and following object creation fails,
1615 * insertion has to be rolled back, but this operation might fail
1616 * also leaving us with dangling index entry.
1618 * - if creation is done first, is has to be undone if insertion
1619 * fails, leaving us with leaked space, which is neither good, nor
1622 * It seems that creation-first is simplest solution, but it is
1623 * sub-optimal in the frequent
1628 * case, because second mkdir is bound to create object, only to
1629 * destroy it immediately.
1631 * To avoid this follow local file systems that do double lookup:
1633 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1635 * 1. create (mdd_object_create_internal())
1637 * 2. insert (__mdd_index_insert(), lookup again)
1640 /* Sanity checks before big job. */
1641 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1645 #ifdef HAVE_QUOTA_SUPPORT
1646 if (mds->mds_quota) {
1647 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1649 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1653 quota_opc = FSFILT_OP_CREATE;
1654 mdd_quota_wrapper(&ma->ma_attr, qcids);
1655 mdd_quota_wrapper(la_tmp, qpids);
1656 /* get file quota for child */
1657 lquota_chkquota(mds_quota_interface_ref, obd,
1658 qcids[USRQUOTA], qcids[GRPQUOTA], 1,
1659 &inode_pending, NULL, 0, NULL, 0);
1660 switch (ma->ma_attr.la_mode & S_IFMT) {
1669 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1670 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1674 /* get block quota for child and parent */
1676 lquota_chkquota(mds_quota_interface_ref, obd,
1677 qcids[USRQUOTA], qcids[GRPQUOTA],
1679 &block_pending, NULL,
1680 LQUOTA_FLAGS_BLK, NULL, 0);
1682 lquota_chkquota(mds_quota_interface_ref, obd,
1683 qpids[USRQUOTA], qpids[GRPQUOTA], 1,
1684 &parent_pending, NULL,
1685 LQUOTA_FLAGS_BLK, NULL, 0);
1691 * No RPC inside the transaction, so OST objects should be created at
1694 if (S_ISREG(attr->la_mode)) {
1695 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1698 GOTO(out_pending, rc);
1701 if (!S_ISLNK(attr->la_mode)) {
1702 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1703 ma_acl->ma_acl = info->mti_xattr_buf;
1704 ma_acl->ma_need = MA_ACL_DEF;
1705 ma_acl->ma_valid = 0;
1707 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1708 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1709 mdd_read_unlock(env, mdd_pobj);
1712 else if (ma_acl->ma_valid & MA_ACL_DEF)
1716 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1717 handle = mdd_trans_start(env, mdd);
1719 GOTO(out_free, rc = PTR_ERR(handle));
1721 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1723 GOTO(out_trans, rc = -ENOMEM);
1725 mdd_write_lock(env, son, MOR_TGT_CHILD);
1726 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1728 mdd_write_unlock(env, son);
1734 #ifdef CONFIG_FS_POSIX_ACL
1736 struct lu_buf *acl_buf = &info->mti_buf;
1737 acl_buf->lb_buf = ma_acl->ma_acl;
1738 acl_buf->lb_len = ma_acl->ma_acl_size;
1740 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1742 mdd_write_unlock(env, son);
1745 ma->ma_attr.la_valid |= LA_MODE;
1750 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1751 son, ma, handle, spec);
1752 mdd_write_unlock(env, son);
1755 * Object has no links, so it will be destroyed when last
1756 * reference is released. (XXX not now.)
1762 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1763 name, S_ISDIR(attr->la_mode), handle,
1764 mdd_object_capa(env, mdd_pobj));
1771 /* No need mdd_lsm_sanity_check here */
1772 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1774 CERROR("error on stripe info copy %d \n", rc);
1777 if (lmm && lmm_size > 0) {
1778 /* Set Lov here, do not get lmm again later */
1779 memcpy(ma->ma_lmm, lmm, lmm_size);
1780 ma->ma_lmm_size = lmm_size;
1781 ma->ma_valid |= MA_LOV;
1784 if (S_ISLNK(attr->la_mode)) {
1785 struct md_ucred *uc = md_ucred(env);
1786 struct dt_object *dt = mdd_object_child(son);
1787 const char *target_name = spec->u.sp_symname;
1788 int sym_len = strlen(target_name);
1789 const struct lu_buf *buf;
1792 buf = mdd_buf_get_const(env, target_name, sym_len);
1793 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1794 mdd_object_capa(env, son),
1796 CFS_CAP_SYS_RESOURCE_MASK);
1801 GOTO(cleanup, rc = -EFAULT);
1805 la->la_valid = LA_CTIME | LA_MTIME;
1806 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1810 /* Return attr back. */
1811 rc = mdd_attr_get_internal_locked(env, son, ma);
1814 if (rc && created) {
1818 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1819 S_ISDIR(attr->la_mode),
1820 handle, BYPASS_CAPA);
1822 CERROR("error can not cleanup destroy %d\n",
1827 mdd_write_lock(env, son, MOR_TGT_CHILD);
1828 __mdd_ref_del(env, son, handle, 0);
1829 if (initialized && S_ISDIR(attr->la_mode))
1830 __mdd_ref_del(env, son, handle, 1);
1831 mdd_write_unlock(env, son);
1835 /* update lov_objid data, must be before transaction stop! */
1837 mdd_lov_objid_update(mdd, lmm);
1839 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1842 rc = mdd_changelog_ns_store(env, mdd,
1843 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1844 S_ISREG(attr->la_mode) ? CL_CREATE :
1845 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1846 son, mdd_pobj, NULL, lname, handle);
1847 mdd_trans_stop(env, mdd, rc, handle);
1849 /* finis lov_create stuff, free all temporary data */
1850 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1852 #ifdef HAVE_QUOTA_SUPPORT
1855 lquota_pending_commit(mds_quota_interface_ref, obd,
1856 qcids[USRQUOTA], qcids[GRPQUOTA],
1859 lquota_pending_commit(mds_quota_interface_ref, obd,
1860 qcids[USRQUOTA], qcids[GRPQUOTA],
1863 lquota_pending_commit(mds_quota_interface_ref, obd,
1864 qpids[USRQUOTA], qpids[GRPQUOTA],
1866 /* Trigger dqacq on the owner of child and parent. If failed,
1867 * the next call for lquota_chkquota will process it. */
1868 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1876 * Get locks on parents in proper order
1877 * RETURN: < 0 - error, rename_order if successful
1885 static int mdd_rename_order(const struct lu_env *env,
1886 struct mdd_device *mdd,
1887 struct mdd_object *src_pobj,
1888 struct mdd_object *tgt_pobj)
1890 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1894 if (src_pobj == tgt_pobj)
1895 RETURN(MDD_RN_SAME);
1897 /* compared the parent child relationship of src_p&tgt_p */
1898 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1900 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1903 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1916 /* has not mdd_write{read}_lock on any obj yet. */
1917 static int mdd_rename_sanity_check(const struct lu_env *env,
1918 struct mdd_object *src_pobj,
1919 struct mdd_object *tgt_pobj,
1920 struct mdd_object *sobj,
1921 struct mdd_object *tobj,
1927 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1930 /* XXX: when get here, sobj must NOT be NULL,
1931 * the other case has been processed in cml_rename
1932 * before mdd_rename and enable MDS_PERM_BYPASS. */
1935 if (mdd_is_dead_obj(sobj))
1938 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1942 /* XXX: when get here, "tobj == NULL" means tobj must
1943 * NOT exist (neither on remote MDS, such case has been
1944 * processed in cml_rename before mdd_rename and enable
1946 * So check may_create, but not check may_unlink. */
1948 rc = mdd_may_create(env, tgt_pobj, NULL,
1949 (src_pobj != tgt_pobj), 0);
1951 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1952 (src_pobj != tgt_pobj), 1);
1954 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1955 S_ISDIR(ma->ma_attr.la_mode))
1956 rc = __mdd_may_link(env, tgt_pobj);
1961 /* src object can be remote that is why we use only fid and type of object */
1962 static int mdd_rename(const struct lu_env *env,
1963 struct md_object *src_pobj, struct md_object *tgt_pobj,
1964 const struct lu_fid *lf, const struct lu_name *lsname,
1965 struct md_object *tobj, const struct lu_name *ltname,
1968 const char *sname = lsname->ln_name;
1969 const char *tname = ltname->ln_name;
1970 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1971 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1972 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1973 struct mdd_device *mdd = mdo2mdd(src_pobj);
1974 struct mdd_object *mdd_sobj = NULL; /* source object */
1975 struct mdd_object *mdd_tobj = NULL;
1976 struct dynlock_handle *sdlh, *tdlh;
1977 struct thandle *handle;
1978 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1979 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1983 #ifdef HAVE_QUOTA_SUPPORT
1984 struct obd_device *obd = mdd->mdd_obd_dev;
1985 struct mds_obd *mds = &obd->u.mds;
1986 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1987 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1988 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1989 int quota_opc = 0, rec_pending = 0;
1993 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1994 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1997 mdd_tobj = md2mdd_obj(tobj);
1999 #ifdef HAVE_QUOTA_SUPPORT
2000 if (mds->mds_quota) {
2001 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2003 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2005 mdd_quota_wrapper(la_tmp, qspids);
2007 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2011 mdd_data_get(env, mdd_tpobj, &data);
2012 quota_opc = FSFILT_OP_LINK;
2013 mdd_quota_wrapper(la_tmp, qtpids);
2014 /* get block quota for target parent */
2015 lquota_chkquota(mds_quota_interface_ref,
2016 obd, qtpids[USRQUOTA],
2017 qtpids[GRPQUOTA], 1,
2026 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2027 handle = mdd_trans_start(env, mdd);
2029 GOTO(out_pending, rc = PTR_ERR(handle));
2031 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2032 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2034 GOTO(cleanup_unlocked, rc);
2036 /* Get locks in determined order */
2037 if (rc == MDD_RN_SAME) {
2038 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2039 sname, MOR_SRC_PARENT);
2040 /* check hashes to determine do we need one lock or two */
2041 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2042 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2046 } else if (rc == MDD_RN_SRCTGT) {
2047 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2048 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2050 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2051 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2053 if (sdlh == NULL || tdlh == NULL)
2054 GOTO(cleanup, rc = -ENOMEM);
2056 mdd_sobj = mdd_object_find(env, mdd, lf);
2057 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2058 mdd_sobj, mdd_tobj, ma);
2062 /* Remove source name from source directory */
2063 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2064 mdd_object_capa(env, mdd_spobj));
2068 /* "mv dir1 dir2" needs "dir1/.." link update */
2069 if (is_dir && mdd_sobj) {
2070 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2071 mdd_object_capa(env, mdd_spobj));
2073 GOTO(fixup_spobj2, rc);
2075 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2076 handle, mdd_object_capa(env, mdd_tpobj));
2078 GOTO(fixup_spobj, rc);
2082 /* Remove target name from target directory
2083 * Here tobj can be remote one, so we do index_delete unconditionally
2084 * and -ENOENT is allowed.
2086 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2087 mdd_object_capa(env, mdd_tpobj));
2089 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2090 /* tname might been renamed to something else */
2091 GOTO(fixup_spobj, rc);
2094 GOTO(fixup_spobj, rc);
2097 /* Insert new fid with target name into target dir */
2098 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2099 mdd_object_capa(env, mdd_tpobj));
2101 GOTO(fixup_spobj, rc);
2103 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2104 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2106 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2108 la->la_valid = LA_CTIME;
2109 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2112 GOTO(fixup_tpobj, rc);
2115 /* Remove old target object
2116 * For tobj is remote case cmm layer has processed
2117 * and set tobj to NULL then. So when tobj is NOT NULL,
2118 * it must be local one.
2120 if (tobj && mdd_object_exists(mdd_tobj)) {
2121 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2122 if (mdd_is_dead_obj(mdd_tobj)) {
2123 mdd_write_unlock(env, mdd_tobj);
2124 /* shld not be dead, something is wrong */
2125 CERROR("tobj is dead, something is wrong\n");
2129 __mdd_ref_del(env, mdd_tobj, handle, 0);
2131 /* Remove dot reference. */
2133 __mdd_ref_del(env, mdd_tobj, handle, 1);
2135 la->la_valid = LA_CTIME;
2136 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2138 GOTO(fixup_tpobj, rc);
2140 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2141 mdd_write_unlock(env, mdd_tobj);
2143 GOTO(fixup_tpobj, rc);
2145 #ifdef HAVE_QUOTA_SUPPORT
2146 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2147 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2148 quota_opc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2149 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2154 la->la_valid = LA_CTIME | LA_MTIME;
2155 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2157 GOTO(fixup_tpobj, rc);
2159 if (mdd_spobj != mdd_tpobj) {
2160 la->la_valid = LA_CTIME | LA_MTIME;
2161 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2165 if (rc == 0 && mdd_sobj) {
2166 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2167 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2168 mdo2fid(mdd_tpobj), ltname, handle);
2170 /* Old files might not have EA entry */
2171 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2173 mdd_write_unlock(env, mdd_sobj);
2174 /* We don't fail the transaction if the link ea can't be
2175 updated -- fid2path will use alternate lookup method. */
2183 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2184 mdd_object_capa(env, mdd_tpobj));
2186 CWARN("tp obj fix error %d\n",rc2);
2190 if (rc && is_dir && mdd_sobj) {
2191 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2192 mdd_object_capa(env, mdd_spobj));
2195 CWARN("sp obj dotdot delete error %d\n",rc2);
2198 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2200 mdd_object_capa(env, mdd_spobj));
2202 CWARN("sp obj dotdot insert error %d\n",rc2);
2207 rc2 = __mdd_index_insert(env, mdd_spobj,
2208 lf, sname, is_dir, handle,
2209 mdd_object_capa(env, mdd_tpobj));
2211 CWARN("sp obj fix error %d\n",rc2);
2214 if (likely(tdlh) && sdlh != tdlh)
2215 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2217 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2220 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2221 mdd_spobj, lf, lsname, handle);
2223 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2224 mdd_tpobj, lf, ltname, handle);
2226 mdd_trans_stop(env, mdd, rc, handle);
2228 mdd_object_put(env, mdd_sobj);
2230 #ifdef HAVE_QUOTA_SUPPORT
2231 if (mds->mds_quota) {
2233 lquota_pending_commit(mds_quota_interface_ref, obd,
2237 /* Trigger dqrel on the source owner of parent.
2238 * If failed, the next call for lquota_chkquota will
2240 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2241 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2243 /* Trigger dqrel/dqacq on the target owner of child and
2244 * parent. If failed, the next call for lquota_chkquota
2245 * will process it. */
2246 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2247 qtpids, rc, quota_opc);
2253 /** enable/disable storing of hardlink info */
2254 int mdd_linkea_enable = 1;
2255 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2256 "record hardlink info in EAs");
2258 /** Read the link EA into a temp buffer.
2259 * Uses the name_buf since it is generally large.
2260 * \retval IS_ERR err
2261 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2263 struct lu_buf *mdd_links_get(const struct lu_env *env,
2264 struct mdd_object *mdd_obj)
2267 struct lustre_capa *capa;
2268 struct link_ea_header *leh;
2271 /* First try a small buf */
2272 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2273 if (buf->lb_buf == NULL)
2274 return ERR_PTR(-ENOMEM);
2276 capa = mdd_object_capa(env, mdd_obj);
2277 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2278 if (rc == -ERANGE) {
2279 /* Buf was too small, figure out what we need. */
2282 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2285 buf = mdd_buf_alloc(env, rc);
2286 if (buf->lb_buf == NULL)
2287 return ERR_PTR(-ENOMEM);
2288 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2294 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2295 leh->leh_magic = LINK_EA_MAGIC;
2296 leh->leh_reccount = __swab32(leh->leh_reccount);
2297 leh->leh_len = __swab64(leh->leh_len);
2298 /* entries are swabbed by mdd_lee_unpack */
2300 if (leh->leh_magic != LINK_EA_MAGIC)
2301 return ERR_PTR(-EINVAL);
2302 if (leh->leh_reccount == 0)
2303 return ERR_PTR(-ENODATA);
2308 /** Pack a link_ea_entry.
2309 * All elements are stored as chars to avoid alignment issues.
2310 * Numbers are always big-endian
2311 * \param packbuf is a temp fid buffer
2312 * \retval record length
2314 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2315 const struct lu_fid *pfid, struct lu_fid* packbuf)
2320 fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2321 ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2322 strncpy(ptr, lname->ln_name, lname->ln_namelen);
2323 reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2324 sizeof(lee->lee_reclen);
2325 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2326 lee->lee_reclen[1] = reclen & 0xff;
2330 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2331 struct lu_name *lname, struct lu_fid *pfid)
2333 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2334 fid_unpack(&lee->lee_parent_fid, pfid);
2335 lname->ln_name = (char *)&lee->lee_parent_fid +
2336 lee->lee_parent_fid.fp_len;
2337 lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2338 sizeof(lee->lee_reclen);
2341 /** Add a record to the end of link ea buf */
2342 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2343 const struct lu_fid *pfid,
2344 const struct lu_name *lname)
2346 struct link_ea_header *leh;
2347 struct link_ea_entry *lee;
2350 if (lname == NULL || pfid == NULL)
2353 /* Make sure our buf is big enough for the new one */
2355 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2356 if (leh->leh_len + reclen > buf->lb_len) {
2357 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2362 lee = buf->lb_buf + leh->leh_len;
2363 reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2364 leh->leh_len += reclen;
2365 leh->leh_reccount++;
2369 /* For pathologic linkers, we don't want to spend lots of time scanning the
2370 * link ea. Limit ourseleves to something reasonable; links not in the EA
2371 * can be looked up via (slower) parent lookup.
2373 #define LINKEA_MAX_COUNT 128
2375 static int mdd_links_add(const struct lu_env *env,
2376 struct mdd_object *mdd_obj,
2377 const struct lu_fid *pfid,
2378 const struct lu_name *lname,
2379 struct thandle *handle)
2382 struct link_ea_header *leh;
2386 if (!mdd_linkea_enable)
2389 buf = mdd_links_get(env, mdd_obj);
2392 if (rc != -ENODATA) {
2393 CERROR("link_ea read failed %d "DFID"\n", rc,
2394 PFID(mdd_object_fid(mdd_obj)));
2397 /* empty EA; start one */
2398 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2399 if (buf->lb_buf == NULL)
2402 leh->leh_magic = LINK_EA_MAGIC;
2403 leh->leh_len = sizeof(struct link_ea_header);
2404 leh->leh_reccount = 0;
2408 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2411 rc = __mdd_links_add(env, buf, pfid, lname);
2416 rc = __mdd_xattr_set(env, mdd_obj,
2417 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2418 XATTR_NAME_LINK, 0, handle);
2420 CERROR("link_ea add failed %d "DFID"\n", rc,
2421 PFID(mdd_object_fid(mdd_obj)));
2423 if (buf->lb_vmalloc)
2424 /* if we vmalloced a large buffer drop it */
2430 static int mdd_links_rename(const struct lu_env *env,
2431 struct mdd_object *mdd_obj,
2432 const struct lu_fid *oldpfid,
2433 const struct lu_name *oldlname,
2434 const struct lu_fid *newpfid,
2435 const struct lu_name *newlname,
2436 struct thandle *handle)
2439 struct link_ea_header *leh;
2440 struct link_ea_entry *lee;
2441 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2442 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2448 if (!mdd_linkea_enable)
2451 if (mdd_obj->mod_flags & DEAD_OBJ)
2452 /* No more links, don't bother */
2455 buf = mdd_links_get(env, mdd_obj);
2458 CERROR("link_ea read failed %d "DFID"\n",
2459 rc, PFID(mdd_object_fid(mdd_obj)));
2463 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2465 /* Find the old record */
2466 for(count = 0; count <= leh->leh_reccount; count++) {
2467 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2468 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2469 lu_fid_eq(tmpfid, oldpfid) &&
2470 (strncmp(tmpname->ln_name, oldlname->ln_name,
2471 tmpname->ln_namelen) == 0))
2473 lee = (struct link_ea_entry *)((char *)lee + reclen);
2475 if (count > leh->leh_reccount) {
2476 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2477 oldlname->ln_namelen, oldlname->ln_name);
2478 GOTO(out, rc = -ENOENT);
2481 /* Remove the old record */
2482 leh->leh_reccount--;
2483 leh->leh_len -= reclen;
2484 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2487 /* If renaming, add the new record */
2488 if (newpfid != NULL) {
2489 /* if the add fails, we still delete the out-of-date old link */
2490 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2494 rc = __mdd_xattr_set(env, mdd_obj,
2495 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2496 XATTR_NAME_LINK, 0, handle);
2502 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2503 oldlname->ln_namelen, oldlname->ln_name, rc,
2504 PFID(mdd_object_fid(mdd_obj)));
2506 if (buf->lb_vmalloc)
2507 /* if we vmalloced a large buffer drop it */
2513 const struct md_dir_operations mdd_dir_ops = {
2514 .mdo_is_subdir = mdd_is_subdir,
2515 .mdo_lookup = mdd_lookup,
2516 .mdo_create = mdd_create,
2517 .mdo_rename = mdd_rename,
2518 .mdo_link = mdd_link,
2519 .mdo_unlink = mdd_unlink,
2520 .mdo_name_insert = mdd_name_insert,
2521 .mdo_name_remove = mdd_name_remove,
2522 .mdo_rename_tgt = mdd_rename_tgt,
2523 .mdo_create_data = mdd_create_data