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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdd/mdd_dir.c
41 * Lustre Metadata Server (mdd) routines
43 * Author: Wang Di <wangdi@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
52 #ifdef HAVE_EXT4_LDISKFS
53 #include <ldiskfs/ldiskfs_jbd2.h>
55 #include <linux/jbd.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
62 #ifdef HAVE_EXT4_LDISKFS
63 #include <ldiskfs/ldiskfs.h>
65 #include <linux/ldiskfs_fs.h>
67 #include <lustre_mds.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_fid.h>
71 #include "mdd_internal.h"
73 static const char dot[] = ".";
74 static const char dotdot[] = "..";
76 static struct lu_name lname_dotdot = {
81 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
82 const struct lu_name *lname, struct lu_fid* fid,
84 static int mdd_links_add(const struct lu_env *env,
85 struct mdd_object *mdd_obj,
86 const struct lu_fid *pfid,
87 const struct lu_name *lname,
88 struct thandle *handle, int first);
89 static int mdd_links_rename(const struct lu_env *env,
90 struct mdd_object *mdd_obj,
91 const struct lu_fid *oldpfid,
92 const struct lu_name *oldlname,
93 const struct lu_fid *newpfid,
94 const struct lu_name *newlname,
95 struct thandle *handle);
98 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
99 const struct lu_name *lname, struct lu_fid* fid, int mask)
101 const char *name = lname->ln_name;
102 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
103 struct dynlock_handle *dlh;
106 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
107 if (unlikely(dlh == NULL))
109 rc = __mdd_lookup(env, pobj, lname, fid, mask);
110 mdd_pdo_read_unlock(env, mdd_obj, dlh);
115 int mdd_lookup(const struct lu_env *env,
116 struct md_object *pobj, const struct lu_name *lname,
117 struct lu_fid* fid, struct md_op_spec *spec)
121 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
125 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
128 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
132 * For root fid use special function, which does not compare version component
133 * of fid. Version component is different for root fids on all MDTs.
135 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
137 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
138 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
142 * return 1: if lf is the fid of the ancestor of p1;
145 * return -EREMOTE: if remote object is found, in this
146 * case fid of remote object is saved to @pf;
148 * otherwise: values < 0, errors.
150 static int mdd_is_parent(const struct lu_env *env,
151 struct mdd_device *mdd,
152 struct mdd_object *p1,
153 const struct lu_fid *lf,
156 struct mdd_object *parent = NULL;
161 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
162 pfid = &mdd_env_info(env)->mti_fid;
164 /* Check for root first. */
165 if (mdd_is_root(mdd, mdo2fid(p1)))
169 /* this is done recursively, bypass capa for each obj */
170 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
171 rc = mdd_parent_fid(env, p1, pfid);
174 if (mdd_is_root(mdd, pfid))
176 if (lu_fid_eq(pfid, lf))
179 mdd_object_put(env, parent);
180 parent = mdd_object_find(env, mdd, pfid);
182 /* cross-ref parent */
183 if (parent == NULL) {
186 GOTO(out, rc = -EREMOTE);
187 } else if (IS_ERR(parent))
188 GOTO(out, rc = PTR_ERR(parent));
193 if (parent && !IS_ERR(parent))
194 mdd_object_put(env, parent);
199 * No permission check is needed.
201 * returns 1: if fid is ancestor of @mo;
202 * returns 0: if fid is not a ancestor of @mo;
204 * returns EREMOTE if remote object is found, fid of remote object is saved to
207 * returns < 0: if error
209 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
210 const struct lu_fid *fid, struct lu_fid *sfid)
212 struct mdd_device *mdd = mdo2mdd(mo);
216 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
219 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
223 } else if (rc == 1) {
224 /* found @fid is parent */
232 * Check that @dir contains no entries except (possibly) dot and dotdot.
237 * -ENOTDIR not a directory object
238 * -ENOTEMPTY not empty
242 static int mdd_dir_is_empty(const struct lu_env *env,
243 struct mdd_object *dir)
246 struct dt_object *obj;
247 const struct dt_it_ops *iops;
251 obj = mdd_object_child(dir);
252 if (!dt_try_as_dir(env, obj))
255 iops = &obj->do_index_ops->dio_it;
256 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
258 result = iops->get(env, it, (const void *)"");
261 for (result = 0, i = 0; result == 0 && i < 3; ++i)
262 result = iops->next(env, it);
265 else if (result == +1)
267 } else if (result == 0)
269 * Huh? Index contains no zero key?
276 result = PTR_ERR(it);
280 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
282 struct mdd_device *m = mdd_obj2mdd_dev(obj);
283 struct lu_attr *la = &mdd_env_info(env)->mti_la;
287 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
292 * Subdir count limitation can be broken through.
294 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
295 !S_ISDIR(la->la_mode))
302 * Check whether it may create the cobj under the pobj.
305 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
306 struct mdd_object *cobj, int check_perm, int check_nlink)
311 if (cobj && mdd_object_exists(cobj))
314 if (mdd_is_dead_obj(pobj))
318 rc = mdd_permission_internal_locked(env, pobj, NULL,
319 MAY_WRITE | MAY_EXEC,
322 if (!rc && check_nlink)
323 rc = __mdd_may_link(env, pobj);
329 * Check whether can unlink from the pobj in the case of "cobj == NULL".
331 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
332 const struct md_attr *ma)
337 if (mdd_is_dead_obj(pobj))
340 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
341 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
344 rc = mdd_permission_internal_locked(env, pobj, NULL,
345 MAY_WRITE | MAY_EXEC,
350 if (mdd_is_append(pobj))
357 * pobj == NULL is remote ops case, under such case, pobj's
358 * VTX feature has been checked already, no need check again.
360 static inline int mdd_is_sticky(const struct lu_env *env,
361 struct mdd_object *pobj,
362 struct mdd_object *cobj)
364 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
365 struct md_ucred *uc = md_ucred(env);
369 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
373 if (!(tmp_la->la_mode & S_ISVTX) ||
374 (tmp_la->la_uid == uc->mu_fsuid))
378 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
382 if (tmp_la->la_uid == uc->mu_fsuid)
385 return !mdd_capable(uc, CFS_CAP_FOWNER);
389 * Check whether it may delete the cobj from the pobj.
392 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
393 struct mdd_object *cobj, struct md_attr *ma,
394 int check_perm, int check_empty)
400 if (!mdd_object_exists(cobj))
403 if (mdd_is_dead_obj(cobj))
407 if (!mdd_object_exists(pobj))
410 if (mdd_is_dead_obj(pobj))
414 rc = mdd_permission_internal_locked(env, pobj, NULL,
415 MAY_WRITE | MAY_EXEC,
421 if (mdd_is_append(pobj))
425 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
426 mdd_is_sticky(env, pobj, cobj))
429 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
432 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
433 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
436 if (S_ISDIR(ma->ma_attr.la_mode)) {
437 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
439 if (!S_ISDIR(mdd_object_type(cobj)))
442 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
444 } else if (S_ISDIR(mdd_object_type(cobj)))
447 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
448 rc = mdd_dir_is_empty(env, cobj);
455 * has mdd_write_lock on src already, but not on tgt yet
457 int mdd_link_sanity_check(const struct lu_env *env,
458 struct mdd_object *tgt_obj,
459 const struct lu_name *lname,
460 struct mdd_object *src_obj)
462 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
466 if (!mdd_object_exists(src_obj))
469 if (mdd_is_dead_obj(src_obj))
472 /* Local ops, no lookup before link, check filename length here. */
473 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
474 RETURN(-ENAMETOOLONG);
476 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
479 if (S_ISDIR(mdd_object_type(src_obj)))
482 LASSERT(src_obj != tgt_obj);
484 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
489 rc = __mdd_may_link(env, src_obj);
495 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
496 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
497 * (maybe too large to be represented). It is a trick to break through the
498 * "i_nlink" limitation for subdir count.
500 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
501 struct thandle *handle)
503 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
504 struct mdd_device *m = mdd_obj2mdd_dev(obj);
506 if (!mdd_is_mnlink(obj)) {
507 if (S_ISDIR(mdd_object_type(obj))) {
508 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
511 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
512 obj->mod_flags |= MNLINK_OBJ;
513 tmp_la->la_nlink = 1;
514 tmp_la->la_valid = LA_NLINK;
515 mdd_attr_set_internal(env, obj, tmp_la, handle,
520 mdo_ref_add(env, obj, handle);
524 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
525 struct thandle *handle, int is_dot)
527 if (!mdd_is_mnlink(obj) || is_dot)
528 mdo_ref_del(env, obj, handle);
531 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
532 const char *name, struct thandle *handle,
533 struct lustre_capa *capa)
535 struct dt_object *next = mdd_object_child(pobj);
539 if (dt_try_as_dir(env, next)) {
540 rc = next->do_index_ops->dio_delete(env, next,
541 (struct dt_key *)name,
549 static int __mdd_index_insert_only(const struct lu_env *env,
550 struct mdd_object *pobj,
551 const struct lu_fid *lf, const char *name,
552 struct thandle *handle,
553 struct lustre_capa *capa)
555 struct dt_object *next = mdd_object_child(pobj);
559 if (dt_try_as_dir(env, next)) {
560 struct md_ucred *uc = md_ucred(env);
562 rc = next->do_index_ops->dio_insert(env, next,
564 (const struct dt_key *)name,
565 handle, capa, uc->mu_cap &
566 CFS_CAP_SYS_RESOURCE_MASK);
573 /* insert named index, add reference if isdir */
574 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
575 const struct lu_fid *lf, const char *name, int is_dir,
576 struct thandle *handle, struct lustre_capa *capa)
581 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
582 if (rc == 0 && is_dir) {
583 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
584 __mdd_ref_add(env, pobj, handle);
585 mdd_write_unlock(env, pobj);
590 /* delete named index, drop reference if isdir */
591 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
592 const char *name, int is_dir, struct thandle *handle,
593 struct lustre_capa *capa)
598 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
599 if (rc == 0 && is_dir) {
602 if (name != NULL && name[0] == '.' && name[1] == 0)
604 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
605 __mdd_ref_del(env, pobj, handle, is_dot);
606 mdd_write_unlock(env, pobj);
613 /** Store a namespace change changelog record
614 * If this fails, we must fail the whole transaction; we don't
615 * want the change to commit without the log entry.
616 * \param target - mdd_object of change
617 * \param parent - parent dir/object
618 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
619 * \param tname - target name string
620 * \param handle - transacion handle
622 static int mdd_changelog_ns_store(const struct lu_env *env,
623 struct mdd_device *mdd,
624 enum changelog_rec_type type,
626 struct mdd_object *target,
627 struct mdd_object *parent,
628 const struct lu_fid *tf,
629 const struct lu_name *tname,
630 struct thandle *handle)
632 const struct lu_fid *tfid;
633 const struct lu_fid *tpfid = mdo2fid(parent);
634 struct llog_changelog_rec *rec;
641 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
643 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
646 LASSERT(parent != NULL);
647 LASSERT(tname != NULL);
648 LASSERT(handle != NULL);
651 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
652 buf = mdd_buf_alloc(env, reclen);
653 if (buf->lb_buf == NULL)
655 rec = (struct llog_changelog_rec *)buf->lb_buf;
657 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
658 rec->cr.cr_type = (__u32)type;
659 tfid = tf ? tf : mdo2fid(target);
660 rec->cr.cr_tfid = *tfid;
661 rec->cr.cr_pfid = *tpfid;
662 rec->cr.cr_namelen = tname->ln_namelen;
663 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
665 target->mod_cltime = cfs_time_current_64();
667 rc = mdd_changelog_llog_write(mdd, rec, handle);
669 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
670 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
677 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
678 struct md_object *src_obj, const struct lu_name *lname,
681 const char *name = lname->ln_name;
682 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
683 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
684 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
685 struct mdd_device *mdd = mdo2mdd(src_obj);
686 struct dynlock_handle *dlh;
687 struct thandle *handle;
688 #ifdef HAVE_QUOTA_SUPPORT
689 struct obd_device *obd = mdd->mdd_obd_dev;
690 struct obd_export *exp = md_quota(env)->mq_exp;
691 struct mds_obd *mds = &obd->u.mds;
692 unsigned int qids[MAXQUOTAS] = { 0, 0 };
693 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
698 #ifdef HAVE_QUOTA_SUPPORT
699 if (mds->mds_quota) {
700 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
702 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
705 mdd_data_get(env, mdd_tobj, &data);
706 quota_opc = FSFILT_OP_LINK;
707 mdd_quota_wrapper(la_tmp, qids);
708 /* get block quota for parent */
709 lquota_chkquota(mds_quota_interface_ref, obd, exp,
710 qids, rec_pending, 1, NULL,
711 LQUOTA_FLAGS_BLK, data, 1);
716 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP, 1);
717 handle = mdd_trans_start(env, mdd);
719 GOTO(out_pending, rc = PTR_ERR(handle));
721 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
723 GOTO(out_trans, rc = -ENOMEM);
724 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
726 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
728 GOTO(out_unlock, rc);
730 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
732 mdd_object_capa(env, mdd_tobj));
734 GOTO(out_unlock, rc);
736 __mdd_ref_add(env, mdd_sobj, handle);
738 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
739 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
741 la->la_valid = LA_CTIME | LA_MTIME;
742 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
744 GOTO(out_unlock, rc);
746 la->la_valid = LA_CTIME;
747 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
749 mdd_links_add(env, mdd_sobj,
750 mdo2fid(mdd_tobj), lname, handle, 0);
755 mdd_write_unlock(env, mdd_sobj);
756 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
759 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
760 mdd_tobj, NULL, lname, handle);
761 mdd_trans_stop(env, mdd, rc, handle);
763 #ifdef HAVE_QUOTA_SUPPORT
765 lquota_pending_commit(mds_quota_interface_ref, obd,
766 qids, rec_pending, 1);
767 /* Trigger dqacq for the parent owner. If failed,
768 * the next call for lquota_chkquota will process it. */
769 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
776 /* caller should take a lock before calling */
777 int mdd_finish_unlink(const struct lu_env *env,
778 struct mdd_object *obj, struct md_attr *ma,
785 LASSERT(mdd_write_locked(env, obj) != 0);
787 /* read HSM flags, needed to set changelogs flags */
788 ma->ma_need = MA_HSM | MA_INODE;
789 rc = mdd_attr_get_internal(env, obj, ma);
790 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
791 obj->mod_flags |= DEAD_OBJ;
792 /* add new orphan and the object
793 * will be deleted during mdd_close() */
794 if (obj->mod_count) {
795 rc = __mdd_orphan_add(env, obj, th);
797 CDEBUG(D_HA, "Object "DFID" is inserted into "
798 "orphan list, open count = %d\n",
799 PFID(mdd_object_fid(obj)),
802 CERROR("Object "DFID" fail to be an orphan, "
803 "open count = %d, maybe cause failed "
805 PFID(mdd_object_fid(obj)),
808 rc = mdd_object_kill(env, obj, ma);
815 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
822 * has mdd_write_lock on cobj already, but not on pobj yet
824 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
825 struct mdd_object *cobj, struct md_attr *ma)
830 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
835 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
836 struct md_object *cobj, const struct lu_name *lname,
839 const char *name = lname->ln_name;
840 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
841 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
842 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
843 struct mdd_device *mdd = mdo2mdd(pobj);
844 struct dynlock_handle *dlh;
845 struct thandle *handle;
846 #ifdef HAVE_QUOTA_SUPPORT
847 struct obd_device *obd = mdd->mdd_obd_dev;
848 struct mds_obd *mds = &obd->u.mds;
849 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
850 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
853 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
857 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
858 PFID(mdd_object_fid(mdd_cobj)));
860 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP, 1);
864 handle = mdd_trans_start(env, mdd);
866 RETURN(PTR_ERR(handle));
868 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
870 GOTO(out_trans, rc = -ENOMEM);
871 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
873 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
877 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
878 mdd_object_capa(env, mdd_pobj));
882 __mdd_ref_del(env, mdd_cobj, handle, 0);
885 __mdd_ref_del(env, mdd_cobj, handle, 1);
887 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
888 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
890 la->la_valid = LA_CTIME | LA_MTIME;
891 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
895 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
896 /* update ctime of an unlinked file only if it is still
897 * opened or a link still exists */
898 la->la_valid = LA_CTIME;
899 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
904 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
905 #ifdef HAVE_QUOTA_SUPPORT
906 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
907 ma->ma_attr.la_nlink == 0) {
908 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
910 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
912 mdd_quota_wrapper(la_tmp, qpids);
913 if (mdd_cobj->mod_count == 0) {
914 quota_opc = FSFILT_OP_UNLINK;
915 mdd_quota_wrapper(&ma->ma_attr, qcids);
917 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
923 /* old files may not have link ea; ignore errors */
924 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
925 lname, NULL, NULL, handle);
929 mdd_write_unlock(env, mdd_cobj);
930 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
935 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
936 if ((ma->ma_valid & MA_HSM) &&
937 (ma->ma_hsm.mh_flags & HS_EXISTS))
938 cl_flags |= CLF_UNLINK_HSM_EXISTS;
940 rc = mdd_changelog_ns_store(env, mdd,
941 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
942 mdd_cobj, mdd_pobj, NULL, lname, handle);
945 mdd_trans_stop(env, mdd, rc, handle);
946 #ifdef HAVE_QUOTA_SUPPORT
948 /* Trigger dqrel on the owner of child and parent. If failed,
949 * the next call for lquota_chkquota will process it. */
950 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
956 /* has not lock on pobj yet */
957 static int mdd_ni_sanity_check(const struct lu_env *env,
958 struct md_object *pobj,
959 const struct md_attr *ma)
961 struct mdd_object *obj = md2mdd_obj(pobj);
965 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
968 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
976 static int mdd_name_insert(const struct lu_env *env,
977 struct md_object *pobj,
978 const struct lu_name *lname,
979 const struct lu_fid *fid,
980 const struct md_attr *ma)
982 const char *name = lname->ln_name;
983 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
984 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
985 struct mdd_device *mdd = mdo2mdd(pobj);
986 struct dynlock_handle *dlh;
987 struct thandle *handle;
988 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
989 #ifdef HAVE_QUOTA_SUPPORT
990 struct md_ucred *uc = md_ucred(env);
991 struct obd_device *obd = mdd->mdd_obd_dev;
992 struct obd_export *exp = md_quota(env)->mq_exp;
993 struct mds_obd *mds = &obd->u.mds;
994 unsigned int qids[MAXQUOTAS] = { 0, 0 };
995 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
996 cfs_cap_t save = uc->mu_cap;
1001 #ifdef HAVE_QUOTA_SUPPORT
1002 if (mds->mds_quota) {
1003 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1004 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1006 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1009 mdd_data_get(env, mdd_obj, &data);
1010 quota_opc = FSFILT_OP_LINK;
1011 mdd_quota_wrapper(la_tmp, qids);
1012 /* get block quota for parent */
1013 lquota_chkquota(mds_quota_interface_ref, obd,
1014 exp, qids, rec_pending, 1, NULL,
1015 LQUOTA_FLAGS_BLK, data, 1);
1018 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1022 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP, 0);
1023 handle = mdd_trans_start(env, mdo2mdd(pobj));
1025 GOTO(out_pending, rc = PTR_ERR(handle));
1027 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1029 GOTO(out_trans, rc = -ENOMEM);
1031 rc = mdd_ni_sanity_check(env, pobj, ma);
1033 GOTO(out_unlock, rc);
1035 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1036 handle, BYPASS_CAPA);
1038 GOTO(out_unlock, rc);
1041 * For some case, no need update obj's ctime (LA_CTIME is not set),
1043 * For other cases, update obj's ctime (LA_CTIME is set),
1046 if (ma->ma_attr.la_valid & LA_CTIME) {
1047 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1048 la->la_valid = LA_CTIME | LA_MTIME;
1049 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1054 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1056 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1058 #ifdef HAVE_QUOTA_SUPPORT
1059 if (mds->mds_quota) {
1061 lquota_pending_commit(mds_quota_interface_ref,
1062 obd, qids, rec_pending, 1);
1063 /* Trigger dqacq for the parent owner. If failed,
1064 * the next call for lquota_chkquota will process it*/
1065 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1075 /* has not lock on pobj yet */
1076 static int mdd_nr_sanity_check(const struct lu_env *env,
1077 struct md_object *pobj,
1078 const struct md_attr *ma)
1080 struct mdd_object *obj = md2mdd_obj(pobj);
1084 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1087 rc = mdd_may_unlink(env, obj, ma);
1093 * Partial operation.
1095 static int mdd_name_remove(const struct lu_env *env,
1096 struct md_object *pobj,
1097 const struct lu_name *lname,
1098 const struct md_attr *ma)
1100 const char *name = lname->ln_name;
1101 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1102 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1103 struct mdd_device *mdd = mdo2mdd(pobj);
1104 struct dynlock_handle *dlh;
1105 struct thandle *handle;
1106 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1107 #ifdef HAVE_QUOTA_SUPPORT
1108 struct obd_device *obd = mdd->mdd_obd_dev;
1109 struct mds_obd *mds = &obd->u.mds;
1110 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1116 #ifdef HAVE_QUOTA_SUPPORT
1117 if (mds->mds_quota) {
1118 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1120 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1122 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1123 mdd_quota_wrapper(la_tmp, qids);
1127 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP, 0);
1128 handle = mdd_trans_start(env, mdd);
1130 GOTO(out_pending, rc = PTR_ERR(handle));
1132 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1134 GOTO(out_trans, rc = -ENOMEM);
1136 rc = mdd_nr_sanity_check(env, pobj, ma);
1138 GOTO(out_unlock, rc);
1140 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1141 handle, BYPASS_CAPA);
1143 GOTO(out_unlock, rc);
1146 * For some case, no need update obj's ctime (LA_CTIME is not set),
1148 * For other cases, update obj's ctime (LA_CTIME is set),
1151 if (ma->ma_attr.la_valid & LA_CTIME) {
1152 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1153 la->la_valid = LA_CTIME | LA_MTIME;
1154 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1159 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1161 mdd_trans_stop(env, mdd, rc, handle);
1163 #ifdef HAVE_QUOTA_SUPPORT
1164 /* Trigger dqrel for the parent owner.
1165 * If failed, the next call for lquota_chkquota will process it. */
1167 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1175 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1177 static int mdd_rt_sanity_check(const struct lu_env *env,
1178 struct mdd_object *tgt_pobj,
1179 struct mdd_object *tobj,
1185 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1188 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1189 * exist. In fact, tobj must exist, otherwise the call trace will be:
1190 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1191 * When get here, tobj must be NOT NULL, the other case has been
1192 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1194 * So check may_delete, but not check nlink of tgt_pobj. */
1196 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1201 /* Partial rename op on slave MDD */
1202 static int mdd_rename_tgt(const struct lu_env *env,
1203 struct md_object *pobj, struct md_object *tobj,
1204 const struct lu_fid *lf, const struct lu_name *lname,
1207 const char *name = lname->ln_name;
1208 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1209 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1210 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1211 struct mdd_device *mdd = mdo2mdd(pobj);
1212 struct dynlock_handle *dlh;
1213 struct thandle *handle;
1214 #ifdef HAVE_QUOTA_SUPPORT
1215 struct obd_device *obd = mdd->mdd_obd_dev;
1216 struct obd_export *exp = md_quota(env)->mq_exp;
1217 struct mds_obd *mds = &obd->u.mds;
1218 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1219 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1220 int quota_copc = 0, quota_popc = 0;
1221 int rec_pending[MAXQUOTAS] = { 0, 0 };
1226 #ifdef HAVE_QUOTA_SUPPORT
1227 if (mds->mds_quota && !tobj) {
1228 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1230 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1233 mdd_data_get(env, mdd_tpobj, &data);
1234 quota_popc = FSFILT_OP_LINK;
1235 mdd_quota_wrapper(la_tmp, qpids);
1236 /* get block quota for target parent */
1237 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1238 qpids, rec_pending, 1, NULL,
1239 LQUOTA_FLAGS_BLK, data, 1);
1243 if (tobj && mdd_object_exists(mdd_tobj))
1244 mdd_log_txn_param_build(env, tobj, ma, MDD_TXN_RENAME_TGT_OP,1);
1246 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP, 1);
1247 handle = mdd_trans_start(env, mdd);
1249 GOTO(out_pending, rc = PTR_ERR(handle));
1251 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1253 GOTO(out_trans, rc = -ENOMEM);
1255 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1257 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1262 * If rename_tgt is called then we should just re-insert name with
1263 * correct fid, no need to dec/inc parent nlink if obj is dir.
1265 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1269 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1274 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1275 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1277 la->la_valid = LA_CTIME | LA_MTIME;
1278 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1283 * For tobj is remote case cmm layer has processed
1284 * and pass NULL tobj to here. So when tobj is NOT NULL,
1285 * it must be local one.
1287 if (tobj && mdd_object_exists(mdd_tobj)) {
1288 __mdd_ref_del(env, mdd_tobj, handle, 0);
1290 /* Remove dot reference. */
1291 if (S_ISDIR(ma->ma_attr.la_mode))
1292 __mdd_ref_del(env, mdd_tobj, handle, 1);
1294 la->la_valid = LA_CTIME;
1295 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1299 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1303 #ifdef HAVE_QUOTA_SUPPORT
1304 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1305 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1306 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1307 mdd_quota_wrapper(&ma->ma_attr, qcids);
1314 mdd_write_unlock(env, mdd_tobj);
1315 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1318 /* Bare EXT record with no RENAME in front of it signifies
1319 a partial slave op */
1320 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1321 mdd_tpobj, NULL, lname, handle);
1323 mdd_trans_stop(env, mdd, rc, handle);
1325 #ifdef HAVE_QUOTA_SUPPORT
1326 if (mds->mds_quota) {
1328 lquota_pending_commit(mds_quota_interface_ref, obd,
1329 qpids, rec_pending, 1);
1332 /* Trigger dqrel on the target owner of child.
1333 * If failed, the next call for lquota_chkquota
1334 * will process it. */
1335 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1343 * The permission has been checked when obj created, no need check again.
1345 static int mdd_cd_sanity_check(const struct lu_env *env,
1346 struct mdd_object *obj)
1351 if (!obj || mdd_is_dead_obj(obj))
1358 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1359 struct md_object *cobj, const struct md_op_spec *spec,
1362 struct mdd_device *mdd = mdo2mdd(cobj);
1363 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1364 struct mdd_object *son = md2mdd_obj(cobj);
1365 struct lu_attr *attr = &ma->ma_attr;
1366 struct lov_mds_md *lmm = NULL;
1368 struct thandle *handle;
1372 rc = mdd_cd_sanity_check(env, son);
1376 if (!md_should_create(spec->sp_cr_flags))
1378 lmm_size = ma->ma_lmm_size;
1379 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1384 mdd_create_txn_param_build(env, mdd, lmm, MDD_TXN_CREATE_DATA_OP, 0);
1385 handle = mdd_trans_start(env, mdd);
1387 GOTO(out_free, rc = PTR_ERR(handle));
1390 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1391 * Should this be fixed?
1394 /* Replay creates has objects already */
1396 if (spec->no_create) {
1397 CDEBUG(D_INFO, "we already have lov ea\n");
1398 rc = mdd_lov_set_md(env, mdd_pobj, son,
1399 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1400 spec->u.sp_ea.eadatalen, handle, 0);
1403 /* No need mdd_lsm_sanity_check here */
1404 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1405 lmm_size, handle, 0);
1408 rc = mdd_attr_get_internal_locked(env, son, ma);
1410 /* update lov_objid data, must be before transaction stop! */
1412 mdd_lov_objid_update(mdd, lmm);
1414 mdd_trans_stop(env, mdd, rc, handle);
1416 /* Finish mdd_lov_create() stuff. */
1417 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1421 /* Get fid from name and parent */
1423 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1424 const struct lu_name *lname, struct lu_fid* fid, int mask)
1426 const char *name = lname->ln_name;
1427 const struct dt_key *key = (const struct dt_key *)name;
1428 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1429 struct mdd_device *m = mdo2mdd(pobj);
1430 struct dt_object *dir = mdd_object_child(mdd_obj);
1434 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1437 rc = mdd_object_exists(mdd_obj);
1438 if (unlikely(rc == 0))
1440 else if (unlikely(rc < 0)) {
1441 CERROR("Object "DFID" locates on remote server\n",
1442 PFID(mdo2fid(mdd_obj)));
1446 /* The common filename length check. */
1447 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1448 RETURN(-ENAMETOOLONG);
1450 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1455 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1456 dt_try_as_dir(env, dir))) {
1458 rc = dir->do_index_ops->dio_lookup(env, dir,
1459 (struct dt_rec *)fid, key,
1460 mdd_object_capa(env, mdd_obj));
1471 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1472 const struct lu_name *lname, struct mdd_object *child,
1473 struct md_attr *ma, struct thandle *handle,
1474 const struct md_op_spec *spec)
1480 * Update attributes for child.
1483 * (1) the valid bits should be converted between Lustre and Linux;
1484 * (2) maybe, the child attributes should be set in OSD when creation.
1487 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1491 if (S_ISDIR(ma->ma_attr.la_mode)) {
1492 /* Add "." and ".." for newly created dir */
1493 __mdd_ref_add(env, child, handle);
1494 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1495 dot, handle, BYPASS_CAPA);
1497 rc = __mdd_index_insert_only(env, child, pfid,
1501 __mdd_ref_del(env, child, handle, 1);
1504 mdd_links_add(env, child, pfid, lname, handle, 1);
1509 /* has not lock on pobj yet */
1510 static int mdd_create_sanity_check(const struct lu_env *env,
1511 struct md_object *pobj,
1512 const struct lu_name *lname,
1514 struct md_op_spec *spec)
1516 struct mdd_thread_info *info = mdd_env_info(env);
1517 struct lu_attr *la = &info->mti_la;
1518 struct lu_fid *fid = &info->mti_fid;
1519 struct mdd_object *obj = md2mdd_obj(pobj);
1520 struct mdd_device *m = mdo2mdd(pobj);
1521 int lookup = spec->sp_cr_lookup;
1526 if (mdd_is_dead_obj(obj))
1530 * In some cases this lookup is not needed - we know before if name
1531 * exists or not because MDT performs lookup for it.
1532 * name length check is done in lookup.
1536 * Check if the name already exist, though it will be checked in
1537 * _index_insert also, for avoiding rolling back if exists
1540 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1541 MAY_WRITE | MAY_EXEC);
1543 RETURN(rc ? : -EEXIST);
1546 * Check WRITE permission for the parent.
1547 * EXEC permission have been checked
1548 * when lookup before create already.
1550 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1557 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1561 if (la->la_mode & S_ISGID) {
1562 ma->ma_attr.la_gid = la->la_gid;
1563 if (S_ISDIR(ma->ma_attr.la_mode)) {
1564 ma->ma_attr.la_mode |= S_ISGID;
1565 ma->ma_attr.la_valid |= LA_MODE;
1569 switch (ma->ma_attr.la_mode & S_IFMT) {
1571 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1573 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1574 RETURN(-ENAMETOOLONG);
1594 * Create object and insert it into namespace.
1596 static int mdd_create(const struct lu_env *env,
1597 struct md_object *pobj,
1598 const struct lu_name *lname,
1599 struct md_object *child,
1600 struct md_op_spec *spec,
1603 struct mdd_thread_info *info = mdd_env_info(env);
1604 struct lu_attr *la = &info->mti_la_for_fix;
1605 struct md_attr *ma_acl = &info->mti_ma;
1606 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1607 struct mdd_object *son = md2mdd_obj(child);
1608 struct mdd_device *mdd = mdo2mdd(pobj);
1609 struct lu_attr *attr = &ma->ma_attr;
1610 struct lov_mds_md *lmm = NULL;
1611 struct thandle *handle;
1612 struct dynlock_handle *dlh;
1613 const char *name = lname->ln_name;
1614 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1615 int got_def_acl = 0;
1616 #ifdef HAVE_QUOTA_SUPPORT
1617 struct obd_device *obd = mdd->mdd_obd_dev;
1618 struct obd_export *exp = md_quota(env)->mq_exp;
1619 struct mds_obd *mds = &obd->u.mds;
1620 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1621 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1622 int quota_opc = 0, block_count = 0;
1623 int inode_pending[MAXQUOTAS] = { 0, 0 };
1624 int block_pending[MAXQUOTAS] = { 0, 0 };
1625 int parent_pending[MAXQUOTAS] = { 0, 0 };
1630 * Two operations have to be performed:
1632 * - an allocation of a new object (->do_create()), and
1634 * - an insertion into a parent index (->dio_insert()).
1636 * Due to locking, operation order is not important, when both are
1637 * successful, *but* error handling cases are quite different:
1639 * - if insertion is done first, and following object creation fails,
1640 * insertion has to be rolled back, but this operation might fail
1641 * also leaving us with dangling index entry.
1643 * - if creation is done first, is has to be undone if insertion
1644 * fails, leaving us with leaked space, which is neither good, nor
1647 * It seems that creation-first is simplest solution, but it is
1648 * sub-optimal in the frequent
1653 * case, because second mkdir is bound to create object, only to
1654 * destroy it immediately.
1656 * To avoid this follow local file systems that do double lookup:
1658 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1660 * 1. create (mdd_object_create_internal())
1662 * 2. insert (__mdd_index_insert(), lookup again)
1665 /* Sanity checks before big job. */
1666 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1670 #ifdef HAVE_QUOTA_SUPPORT
1671 if (mds->mds_quota) {
1672 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1674 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1678 quota_opc = FSFILT_OP_CREATE;
1679 mdd_quota_wrapper(&ma->ma_attr, qcids);
1680 mdd_quota_wrapper(la_tmp, qpids);
1681 /* get file quota for child */
1682 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1683 qcids, inode_pending, 1, NULL, 0, NULL,
1685 switch (ma->ma_attr.la_mode & S_IFMT) {
1694 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1695 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1699 /* get block quota for child and parent */
1701 lquota_chkquota(mds_quota_interface_ref, obd,
1702 exp, qcids, block_pending,
1704 LQUOTA_FLAGS_BLK, NULL, 0);
1706 lquota_chkquota(mds_quota_interface_ref, obd,
1707 exp, qpids, parent_pending, 1,
1708 NULL, LQUOTA_FLAGS_BLK, NULL,
1715 * No RPC inside the transaction, so OST objects should be created at
1718 if (S_ISREG(attr->la_mode)) {
1719 lmm_size = ma->ma_lmm_size;
1720 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1723 GOTO(out_pending, rc);
1726 if (!S_ISLNK(attr->la_mode)) {
1727 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1728 ma_acl->ma_acl = info->mti_xattr_buf;
1729 ma_acl->ma_need = MA_ACL_DEF;
1730 ma_acl->ma_valid = 0;
1732 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1733 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1734 mdd_read_unlock(env, mdd_pobj);
1737 else if (ma_acl->ma_valid & MA_ACL_DEF)
1741 mdd_create_txn_param_build(env, mdd, lmm, MDD_TXN_MKDIR_OP, 1);
1742 handle = mdd_trans_start(env, mdd);
1744 GOTO(out_free, rc = PTR_ERR(handle));
1746 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1748 GOTO(out_trans, rc = -ENOMEM);
1750 mdd_write_lock(env, son, MOR_TGT_CHILD);
1751 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1753 mdd_write_unlock(env, son);
1759 #ifdef CONFIG_FS_POSIX_ACL
1761 struct lu_buf *acl_buf = &info->mti_buf;
1762 acl_buf->lb_buf = ma_acl->ma_acl;
1763 acl_buf->lb_len = ma_acl->ma_acl_size;
1765 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1767 mdd_write_unlock(env, son);
1770 ma->ma_attr.la_valid |= LA_MODE;
1775 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1776 son, ma, handle, spec);
1777 mdd_write_unlock(env, son);
1780 * Object has no links, so it will be destroyed when last
1781 * reference is released. (XXX not now.)
1787 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1788 name, S_ISDIR(attr->la_mode), handle,
1789 mdd_object_capa(env, mdd_pobj));
1796 /* No need mdd_lsm_sanity_check here */
1797 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1799 CERROR("error on stripe info copy %d \n", rc);
1802 if (lmm && lmm_size > 0) {
1803 /* Set Lov here, do not get lmm again later */
1804 memcpy(ma->ma_lmm, lmm, lmm_size);
1805 ma->ma_lmm_size = lmm_size;
1806 ma->ma_valid |= MA_LOV;
1809 if (S_ISLNK(attr->la_mode)) {
1810 struct md_ucred *uc = md_ucred(env);
1811 struct dt_object *dt = mdd_object_child(son);
1812 const char *target_name = spec->u.sp_symname;
1813 int sym_len = strlen(target_name);
1814 const struct lu_buf *buf;
1817 buf = mdd_buf_get_const(env, target_name, sym_len);
1818 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1819 mdd_object_capa(env, son),
1821 CFS_CAP_SYS_RESOURCE_MASK);
1826 GOTO(cleanup, rc = -EFAULT);
1830 la->la_valid = LA_CTIME | LA_MTIME;
1831 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1835 /* Return attr back. */
1836 rc = mdd_attr_get_internal_locked(env, son, ma);
1839 if (rc && created) {
1843 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1844 S_ISDIR(attr->la_mode),
1845 handle, BYPASS_CAPA);
1847 CERROR("error can not cleanup destroy %d\n",
1852 mdd_write_lock(env, son, MOR_TGT_CHILD);
1853 __mdd_ref_del(env, son, handle, 0);
1854 if (initialized && S_ISDIR(attr->la_mode))
1855 __mdd_ref_del(env, son, handle, 1);
1856 mdd_write_unlock(env, son);
1860 /* update lov_objid data, must be before transaction stop! */
1862 mdd_lov_objid_update(mdd, lmm);
1864 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1867 rc = mdd_changelog_ns_store(env, mdd,
1868 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1869 S_ISREG(attr->la_mode) ? CL_CREATE :
1870 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1871 0, son, mdd_pobj, NULL, lname, handle);
1872 mdd_trans_stop(env, mdd, rc, handle);
1874 /* finish lov_create stuff, free all temporary data */
1875 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1877 #ifdef HAVE_QUOTA_SUPPORT
1879 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1881 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1883 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1885 /* Trigger dqacq on the owner of child and parent. If failed,
1886 * the next call for lquota_chkquota will process it. */
1887 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1895 * Get locks on parents in proper order
1896 * RETURN: < 0 - error, rename_order if successful
1904 static int mdd_rename_order(const struct lu_env *env,
1905 struct mdd_device *mdd,
1906 struct mdd_object *src_pobj,
1907 struct mdd_object *tgt_pobj)
1909 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1913 if (src_pobj == tgt_pobj)
1914 RETURN(MDD_RN_SAME);
1916 /* compared the parent child relationship of src_p&tgt_p */
1917 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1919 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1922 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1935 /* has not mdd_write{read}_lock on any obj yet. */
1936 static int mdd_rename_sanity_check(const struct lu_env *env,
1937 struct mdd_object *src_pobj,
1938 struct mdd_object *tgt_pobj,
1939 struct mdd_object *sobj,
1940 struct mdd_object *tobj,
1946 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1949 /* XXX: when get here, sobj must NOT be NULL,
1950 * the other case has been processed in cml_rename
1951 * before mdd_rename and enable MDS_PERM_BYPASS. */
1954 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1958 /* XXX: when get here, "tobj == NULL" means tobj must
1959 * NOT exist (neither on remote MDS, such case has been
1960 * processed in cml_rename before mdd_rename and enable
1962 * So check may_create, but not check may_unlink. */
1964 rc = mdd_may_create(env, tgt_pobj, NULL,
1965 (src_pobj != tgt_pobj), 0);
1967 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1968 (src_pobj != tgt_pobj), 1);
1970 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1971 S_ISDIR(ma->ma_attr.la_mode))
1972 rc = __mdd_may_link(env, tgt_pobj);
1977 /* src object can be remote that is why we use only fid and type of object */
1978 static int mdd_rename(const struct lu_env *env,
1979 struct md_object *src_pobj, struct md_object *tgt_pobj,
1980 const struct lu_fid *lf, const struct lu_name *lsname,
1981 struct md_object *tobj, const struct lu_name *ltname,
1984 const char *sname = lsname->ln_name;
1985 const char *tname = ltname->ln_name;
1986 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1987 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1988 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1989 struct mdd_device *mdd = mdo2mdd(src_pobj);
1990 struct mdd_object *mdd_sobj = NULL; /* source object */
1991 struct mdd_object *mdd_tobj = NULL;
1992 struct dynlock_handle *sdlh, *tdlh;
1993 struct thandle *handle;
1994 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1995 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1999 #ifdef HAVE_QUOTA_SUPPORT
2000 struct obd_device *obd = mdd->mdd_obd_dev;
2001 struct obd_export *exp = md_quota(env)->mq_exp;
2002 struct mds_obd *mds = &obd->u.mds;
2003 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2004 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2005 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2006 int quota_copc = 0, quota_popc = 0;
2007 int rec_pending[MAXQUOTAS] = { 0, 0 };
2011 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2012 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2015 mdd_tobj = md2mdd_obj(tobj);
2017 #ifdef HAVE_QUOTA_SUPPORT
2018 if (mds->mds_quota) {
2019 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2021 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2023 mdd_quota_wrapper(la_tmp, qspids);
2025 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2029 mdd_data_get(env, mdd_tpobj, &data);
2030 quota_popc = FSFILT_OP_LINK;
2031 mdd_quota_wrapper(la_tmp, qtpids);
2032 /* get block quota for target parent */
2033 lquota_chkquota(mds_quota_interface_ref,
2035 rec_pending, 1, NULL,
2043 if (tobj && mdd_object_exists(mdd_tobj))
2044 mdd_log_txn_param_build(env, tobj, ma, MDD_TXN_RENAME_OP, 2);
2046 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP, 2);
2047 handle = mdd_trans_start(env, mdd);
2049 GOTO(out_pending, rc = PTR_ERR(handle));
2051 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2052 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2054 GOTO(cleanup_unlocked, rc);
2056 /* Get locks in determined order */
2057 if (rc == MDD_RN_SAME) {
2058 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2059 sname, MOR_SRC_PARENT);
2060 /* check hashes to determine do we need one lock or two */
2061 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2062 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2066 } else if (rc == MDD_RN_SRCTGT) {
2067 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2068 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2070 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2071 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2073 if (sdlh == NULL || tdlh == NULL)
2074 GOTO(cleanup, rc = -ENOMEM);
2076 mdd_sobj = mdd_object_find(env, mdd, lf);
2077 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2078 mdd_sobj, mdd_tobj, ma);
2082 /* Remove source name from source directory */
2083 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2084 mdd_object_capa(env, mdd_spobj));
2088 /* "mv dir1 dir2" needs "dir1/.." link update */
2089 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2090 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2091 mdd_object_capa(env, mdd_sobj));
2093 GOTO(fixup_spobj2, rc);
2095 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2096 handle, mdd_object_capa(env, mdd_sobj));
2098 GOTO(fixup_spobj, rc);
2101 /* Remove target name from target directory
2102 * Here tobj can be remote one, so we do index_delete unconditionally
2103 * and -ENOENT is allowed.
2105 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2106 mdd_object_capa(env, mdd_tpobj));
2109 /* tname might been renamed to something else */
2110 GOTO(fixup_spobj, rc);
2113 GOTO(fixup_spobj, rc);
2116 /* Insert new fid with target name into target dir */
2117 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2118 mdd_object_capa(env, mdd_tpobj));
2120 GOTO(fixup_tpobj, rc);
2122 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2123 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2125 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2127 la->la_valid = LA_CTIME;
2128 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2131 GOTO(fixup_tpobj, rc);
2134 /* Remove old target object
2135 * For tobj is remote case cmm layer has processed
2136 * and set tobj to NULL then. So when tobj is NOT NULL,
2137 * it must be local one.
2139 if (tobj && mdd_object_exists(mdd_tobj)) {
2140 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2141 if (mdd_is_dead_obj(mdd_tobj)) {
2142 mdd_write_unlock(env, mdd_tobj);
2143 /* shld not be dead, something is wrong */
2144 CERROR("tobj is dead, something is wrong\n");
2148 __mdd_ref_del(env, mdd_tobj, handle, 0);
2150 /* Remove dot reference. */
2152 __mdd_ref_del(env, mdd_tobj, handle, 1);
2154 la->la_valid = LA_CTIME;
2155 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2157 GOTO(fixup_tpobj, rc);
2159 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2160 mdd_write_unlock(env, mdd_tobj);
2162 GOTO(fixup_tpobj, rc);
2164 #ifdef HAVE_QUOTA_SUPPORT
2165 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2166 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2167 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2168 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2173 la->la_valid = LA_CTIME | LA_MTIME;
2174 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2176 GOTO(fixup_tpobj, rc);
2178 if (mdd_spobj != mdd_tpobj) {
2179 la->la_valid = LA_CTIME | LA_MTIME;
2180 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2184 if (rc == 0 && mdd_sobj) {
2185 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2186 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2187 mdo2fid(mdd_tpobj), ltname, handle);
2189 /* Old files might not have EA entry */
2190 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2192 mdd_write_unlock(env, mdd_sobj);
2193 /* We don't fail the transaction if the link ea can't be
2194 updated -- fid2path will use alternate lookup method. */
2202 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2205 CWARN("tp obj fix error %d\n",rc2);
2207 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2208 !mdd_is_dead_obj(mdd_tobj)) {
2209 rc2 = __mdd_index_insert(env, mdd_tpobj,
2210 mdo2fid(mdd_tobj), tname,
2215 CWARN("tp obj fix error %d\n",rc2);
2220 if (rc && is_dir && mdd_sobj) {
2221 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2225 CWARN("sp obj dotdot delete error %d\n",rc2);
2228 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2229 dotdot, handle, BYPASS_CAPA);
2231 CWARN("sp obj dotdot insert error %d\n",rc2);
2236 rc2 = __mdd_index_insert(env, mdd_spobj,
2237 lf, sname, is_dir, handle, BYPASS_CAPA);
2239 CWARN("sp obj fix error %d\n",rc2);
2242 if (likely(tdlh) && sdlh != tdlh)
2243 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2245 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2248 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2249 mdd_spobj, lf, lsname, handle);
2251 struct lu_fid zero_fid;
2252 fid_zero(&zero_fid);
2253 /* If the rename target exist, The CL_EXT record should save
2254 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2255 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2257 mdd_tobj ? NULL : &zero_fid,
2261 mdd_trans_stop(env, mdd, rc, handle);
2263 mdd_object_put(env, mdd_sobj);
2265 #ifdef HAVE_QUOTA_SUPPORT
2266 if (mds->mds_quota) {
2268 lquota_pending_commit(mds_quota_interface_ref, obd,
2269 qtpids, rec_pending, 1);
2272 /* Trigger dqrel on the source owner of parent.
2273 * If failed, the next call for lquota_chkquota will
2275 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2276 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2278 /* Trigger dqrel on the target owner of child.
2279 * If failed, the next call for lquota_chkquota
2280 * will process it. */
2281 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2282 qtpids, rc, quota_copc);
2289 /** enable/disable storing of hardlink info */
2290 int mdd_linkea_enable = 1;
2291 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2292 "record hardlink info in EAs");
2294 /** Read the link EA into a temp buffer.
2295 * Uses the name_buf since it is generally large.
2296 * \retval IS_ERR err
2297 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2299 struct lu_buf *mdd_links_get(const struct lu_env *env,
2300 struct mdd_object *mdd_obj)
2303 struct lustre_capa *capa;
2304 struct link_ea_header *leh;
2307 /* First try a small buf */
2308 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2309 if (buf->lb_buf == NULL)
2310 return ERR_PTR(-ENOMEM);
2312 capa = mdd_object_capa(env, mdd_obj);
2313 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2314 if (rc == -ERANGE) {
2315 /* Buf was too small, figure out what we need. */
2317 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2320 buf = mdd_buf_alloc(env, rc);
2321 if (buf->lb_buf == NULL)
2322 return ERR_PTR(-ENOMEM);
2323 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2329 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2330 leh->leh_magic = LINK_EA_MAGIC;
2331 leh->leh_reccount = __swab32(leh->leh_reccount);
2332 leh->leh_len = __swab64(leh->leh_len);
2333 /* entries are swabbed by mdd_lee_unpack */
2335 if (leh->leh_magic != LINK_EA_MAGIC)
2336 return ERR_PTR(-EINVAL);
2337 if (leh->leh_reccount == 0)
2338 return ERR_PTR(-ENODATA);
2343 /** Pack a link_ea_entry.
2344 * All elements are stored as chars to avoid alignment issues.
2345 * Numbers are always big-endian
2346 * \retval record length
2348 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2349 const struct lu_fid *pfid)
2351 struct lu_fid tmpfid;
2354 fid_cpu_to_be(&tmpfid, pfid);
2355 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2356 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2357 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2359 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2360 lee->lee_reclen[1] = reclen & 0xff;
2364 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2365 struct lu_name *lname, struct lu_fid *pfid)
2367 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2368 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2369 fid_be_to_cpu(pfid, pfid);
2370 lname->ln_name = lee->lee_name;
2371 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2374 /** Add a record to the end of link ea buf */
2375 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2376 const struct lu_fid *pfid,
2377 const struct lu_name *lname)
2379 struct link_ea_header *leh;
2380 struct link_ea_entry *lee;
2383 if (lname == NULL || pfid == NULL)
2386 /* Make sure our buf is big enough for the new one */
2388 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2389 if (leh->leh_len + reclen > buf->lb_len) {
2390 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2395 lee = buf->lb_buf + leh->leh_len;
2396 reclen = mdd_lee_pack(lee, lname, pfid);
2397 leh->leh_len += reclen;
2398 leh->leh_reccount++;
2402 /* For pathologic linkers, we don't want to spend lots of time scanning the
2403 * link ea. Limit ourseleves to something reasonable; links not in the EA
2404 * can be looked up via (slower) parent lookup.
2406 #define LINKEA_MAX_COUNT 128
2408 static int mdd_links_add(const struct lu_env *env,
2409 struct mdd_object *mdd_obj,
2410 const struct lu_fid *pfid,
2411 const struct lu_name *lname,
2412 struct thandle *handle, int first)
2415 struct link_ea_header *leh;
2419 if (!mdd_linkea_enable)
2422 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2425 if (rc != -ENODATA) {
2426 CERROR("link_ea read failed %d "DFID"\n", rc,
2427 PFID(mdd_object_fid(mdd_obj)));
2430 /* empty EA; start one */
2431 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2432 if (buf->lb_buf == NULL)
2435 leh->leh_magic = LINK_EA_MAGIC;
2436 leh->leh_len = sizeof(struct link_ea_header);
2437 leh->leh_reccount = 0;
2441 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2444 rc = __mdd_links_add(env, buf, pfid, lname);
2449 rc = __mdd_xattr_set(env, mdd_obj,
2450 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2451 XATTR_NAME_LINK, 0, handle);
2454 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2455 PFID(mdd_object_fid(mdd_obj)));
2457 CERROR("link_ea add failed %d "DFID"\n", rc,
2458 PFID(mdd_object_fid(mdd_obj)));
2461 if (buf->lb_len > OBD_ALLOC_BIG)
2462 /* if we vmalloced a large buffer drop it */
2468 static int mdd_links_rename(const struct lu_env *env,
2469 struct mdd_object *mdd_obj,
2470 const struct lu_fid *oldpfid,
2471 const struct lu_name *oldlname,
2472 const struct lu_fid *newpfid,
2473 const struct lu_name *newlname,
2474 struct thandle *handle)
2477 struct link_ea_header *leh;
2478 struct link_ea_entry *lee;
2479 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2480 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2486 if (!mdd_linkea_enable)
2489 if (mdd_obj->mod_flags & DEAD_OBJ)
2490 /* No more links, don't bother */
2493 buf = mdd_links_get(env, mdd_obj);
2497 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2498 rc, PFID(mdd_object_fid(mdd_obj)));
2500 CERROR("link_ea read failed %d "DFID"\n",
2501 rc, PFID(mdd_object_fid(mdd_obj)));
2505 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2507 /* Find the old record */
2508 for(count = 0; count < leh->leh_reccount; count++) {
2509 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2510 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2511 lu_fid_eq(tmpfid, oldpfid) &&
2512 (strncmp(tmpname->ln_name, oldlname->ln_name,
2513 tmpname->ln_namelen) == 0))
2515 lee = (struct link_ea_entry *)((char *)lee + reclen);
2517 if ((count + 1) > leh->leh_reccount) {
2518 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2519 oldlname->ln_namelen, oldlname->ln_name);
2520 GOTO(out, rc = -ENOENT);
2523 /* Remove the old record */
2524 leh->leh_reccount--;
2525 leh->leh_len -= reclen;
2526 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2529 /* If renaming, add the new record */
2530 if (newpfid != NULL) {
2531 /* if the add fails, we still delete the out-of-date old link */
2532 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2536 rc = __mdd_xattr_set(env, mdd_obj,
2537 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2538 XATTR_NAME_LINK, 0, handle);
2544 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2545 oldlname->ln_namelen, oldlname->ln_name, rc,
2546 PFID(mdd_object_fid(mdd_obj)));
2548 if (buf->lb_len > OBD_ALLOC_BIG)
2549 /* if we vmalloced a large buffer drop it */
2555 const struct md_dir_operations mdd_dir_ops = {
2556 .mdo_is_subdir = mdd_is_subdir,
2557 .mdo_lookup = mdd_lookup,
2558 .mdo_create = mdd_create,
2559 .mdo_rename = mdd_rename,
2560 .mdo_link = mdd_link,
2561 .mdo_unlink = mdd_unlink,
2562 .mdo_name_insert = mdd_name_insert,
2563 .mdo_name_remove = mdd_name_remove,
2564 .mdo_rename_tgt = mdd_rename_tgt,
2565 .mdo_create_data = mdd_create_data,