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);
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);
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);
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);
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 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1244 handle = mdd_trans_start(env, mdd);
1246 GOTO(out_pending, rc = PTR_ERR(handle));
1248 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1250 GOTO(out_trans, rc = -ENOMEM);
1252 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1254 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1259 * If rename_tgt is called then we should just re-insert name with
1260 * correct fid, no need to dec/inc parent nlink if obj is dir.
1262 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1266 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1271 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1272 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1274 la->la_valid = LA_CTIME | LA_MTIME;
1275 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1280 * For tobj is remote case cmm layer has processed
1281 * and pass NULL tobj to here. So when tobj is NOT NULL,
1282 * it must be local one.
1284 if (tobj && mdd_object_exists(mdd_tobj)) {
1285 __mdd_ref_del(env, mdd_tobj, handle, 0);
1287 /* Remove dot reference. */
1288 if (S_ISDIR(ma->ma_attr.la_mode))
1289 __mdd_ref_del(env, mdd_tobj, handle, 1);
1291 la->la_valid = LA_CTIME;
1292 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1296 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1300 #ifdef HAVE_QUOTA_SUPPORT
1301 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1302 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1303 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1304 mdd_quota_wrapper(&ma->ma_attr, qcids);
1311 mdd_write_unlock(env, mdd_tobj);
1312 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1315 /* Bare EXT record with no RENAME in front of it signifies
1316 a partial slave op */
1317 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1318 mdd_tpobj, NULL, lname, handle);
1320 mdd_trans_stop(env, mdd, rc, handle);
1322 #ifdef HAVE_QUOTA_SUPPORT
1323 if (mds->mds_quota) {
1325 lquota_pending_commit(mds_quota_interface_ref, obd,
1326 qpids, rec_pending, 1);
1329 /* Trigger dqrel on the target owner of child.
1330 * If failed, the next call for lquota_chkquota
1331 * will process it. */
1332 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1340 * The permission has been checked when obj created, no need check again.
1342 static int mdd_cd_sanity_check(const struct lu_env *env,
1343 struct mdd_object *obj)
1348 if (!obj || mdd_is_dead_obj(obj))
1355 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1356 struct md_object *cobj, const struct md_op_spec *spec,
1359 struct mdd_device *mdd = mdo2mdd(cobj);
1360 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1361 struct mdd_object *son = md2mdd_obj(cobj);
1362 struct lu_attr *attr = &ma->ma_attr;
1363 struct lov_mds_md *lmm = NULL;
1365 struct thandle *handle;
1369 rc = mdd_cd_sanity_check(env, son);
1373 if (!md_should_create(spec->sp_cr_flags))
1375 lmm_size = ma->ma_lmm_size;
1376 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1381 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1382 handle = mdd_trans_start(env, mdd);
1384 GOTO(out_free, rc = PTR_ERR(handle));
1387 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1388 * Should this be fixed?
1391 /* Replay creates has objects already */
1393 if (spec->no_create) {
1394 CDEBUG(D_INFO, "we already have lov ea\n");
1395 rc = mdd_lov_set_md(env, mdd_pobj, son,
1396 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1397 spec->u.sp_ea.eadatalen, handle, 0);
1400 /* No need mdd_lsm_sanity_check here */
1401 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1402 lmm_size, handle, 0);
1405 rc = mdd_attr_get_internal_locked(env, son, ma);
1407 /* update lov_objid data, must be before transaction stop! */
1409 mdd_lov_objid_update(mdd, lmm);
1411 mdd_trans_stop(env, mdd, rc, handle);
1413 /* Finish mdd_lov_create() stuff. */
1414 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1418 /* Get fid from name and parent */
1420 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1421 const struct lu_name *lname, struct lu_fid* fid, int mask)
1423 const char *name = lname->ln_name;
1424 const struct dt_key *key = (const struct dt_key *)name;
1425 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1426 struct mdd_device *m = mdo2mdd(pobj);
1427 struct dt_object *dir = mdd_object_child(mdd_obj);
1431 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1434 rc = mdd_object_exists(mdd_obj);
1435 if (unlikely(rc == 0))
1437 else if (unlikely(rc < 0)) {
1438 CERROR("Object "DFID" locates on remote server\n",
1439 PFID(mdo2fid(mdd_obj)));
1443 /* The common filename length check. */
1444 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1445 RETURN(-ENAMETOOLONG);
1447 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1452 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1453 dt_try_as_dir(env, dir))) {
1455 rc = dir->do_index_ops->dio_lookup(env, dir,
1456 (struct dt_rec *)fid, key,
1457 mdd_object_capa(env, mdd_obj));
1468 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1469 const struct lu_name *lname, struct mdd_object *child,
1470 struct md_attr *ma, struct thandle *handle,
1471 const struct md_op_spec *spec)
1477 * Update attributes for child.
1480 * (1) the valid bits should be converted between Lustre and Linux;
1481 * (2) maybe, the child attributes should be set in OSD when creation.
1484 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1488 if (S_ISDIR(ma->ma_attr.la_mode)) {
1489 /* Add "." and ".." for newly created dir */
1490 __mdd_ref_add(env, child, handle);
1491 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1492 dot, handle, BYPASS_CAPA);
1494 rc = __mdd_index_insert_only(env, child, pfid,
1498 __mdd_ref_del(env, child, handle, 1);
1501 mdd_links_add(env, child, pfid, lname, handle, 1);
1506 /* has not lock on pobj yet */
1507 static int mdd_create_sanity_check(const struct lu_env *env,
1508 struct md_object *pobj,
1509 const struct lu_name *lname,
1511 struct md_op_spec *spec)
1513 struct mdd_thread_info *info = mdd_env_info(env);
1514 struct lu_attr *la = &info->mti_la;
1515 struct lu_fid *fid = &info->mti_fid;
1516 struct mdd_object *obj = md2mdd_obj(pobj);
1517 struct mdd_device *m = mdo2mdd(pobj);
1518 int lookup = spec->sp_cr_lookup;
1523 if (mdd_is_dead_obj(obj))
1527 * In some cases this lookup is not needed - we know before if name
1528 * exists or not because MDT performs lookup for it.
1529 * name length check is done in lookup.
1533 * Check if the name already exist, though it will be checked in
1534 * _index_insert also, for avoiding rolling back if exists
1537 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1538 MAY_WRITE | MAY_EXEC);
1540 RETURN(rc ? : -EEXIST);
1543 * Check WRITE permission for the parent.
1544 * EXEC permission have been checked
1545 * when lookup before create already.
1547 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1554 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1558 if (la->la_mode & S_ISGID) {
1559 ma->ma_attr.la_gid = la->la_gid;
1560 if (S_ISDIR(ma->ma_attr.la_mode)) {
1561 ma->ma_attr.la_mode |= S_ISGID;
1562 ma->ma_attr.la_valid |= LA_MODE;
1566 switch (ma->ma_attr.la_mode & S_IFMT) {
1568 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1570 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1571 RETURN(-ENAMETOOLONG);
1591 * Create object and insert it into namespace.
1593 static int mdd_create(const struct lu_env *env,
1594 struct md_object *pobj,
1595 const struct lu_name *lname,
1596 struct md_object *child,
1597 struct md_op_spec *spec,
1600 struct mdd_thread_info *info = mdd_env_info(env);
1601 struct lu_attr *la = &info->mti_la_for_fix;
1602 struct md_attr *ma_acl = &info->mti_ma;
1603 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1604 struct mdd_object *son = md2mdd_obj(child);
1605 struct mdd_device *mdd = mdo2mdd(pobj);
1606 struct lu_attr *attr = &ma->ma_attr;
1607 struct lov_mds_md *lmm = NULL;
1608 struct thandle *handle;
1609 struct dynlock_handle *dlh;
1610 const char *name = lname->ln_name;
1611 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1612 int got_def_acl = 0;
1613 #ifdef HAVE_QUOTA_SUPPORT
1614 struct obd_device *obd = mdd->mdd_obd_dev;
1615 struct obd_export *exp = md_quota(env)->mq_exp;
1616 struct mds_obd *mds = &obd->u.mds;
1617 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1618 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1619 int quota_opc = 0, block_count = 0;
1620 int inode_pending[MAXQUOTAS] = { 0, 0 };
1621 int block_pending[MAXQUOTAS] = { 0, 0 };
1622 int parent_pending[MAXQUOTAS] = { 0, 0 };
1627 * Two operations have to be performed:
1629 * - an allocation of a new object (->do_create()), and
1631 * - an insertion into a parent index (->dio_insert()).
1633 * Due to locking, operation order is not important, when both are
1634 * successful, *but* error handling cases are quite different:
1636 * - if insertion is done first, and following object creation fails,
1637 * insertion has to be rolled back, but this operation might fail
1638 * also leaving us with dangling index entry.
1640 * - if creation is done first, is has to be undone if insertion
1641 * fails, leaving us with leaked space, which is neither good, nor
1644 * It seems that creation-first is simplest solution, but it is
1645 * sub-optimal in the frequent
1650 * case, because second mkdir is bound to create object, only to
1651 * destroy it immediately.
1653 * To avoid this follow local file systems that do double lookup:
1655 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1657 * 1. create (mdd_object_create_internal())
1659 * 2. insert (__mdd_index_insert(), lookup again)
1662 /* Sanity checks before big job. */
1663 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1667 #ifdef HAVE_QUOTA_SUPPORT
1668 if (mds->mds_quota) {
1669 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1671 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1675 quota_opc = FSFILT_OP_CREATE;
1676 mdd_quota_wrapper(&ma->ma_attr, qcids);
1677 mdd_quota_wrapper(la_tmp, qpids);
1678 /* get file quota for child */
1679 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1680 qcids, inode_pending, 1, NULL, 0, NULL,
1682 switch (ma->ma_attr.la_mode & S_IFMT) {
1691 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1692 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1696 /* get block quota for child and parent */
1698 lquota_chkquota(mds_quota_interface_ref, obd,
1699 exp, qcids, block_pending,
1701 LQUOTA_FLAGS_BLK, NULL, 0);
1703 lquota_chkquota(mds_quota_interface_ref, obd,
1704 exp, qpids, parent_pending, 1,
1705 NULL, LQUOTA_FLAGS_BLK, NULL,
1712 * No RPC inside the transaction, so OST objects should be created at
1715 if (S_ISREG(attr->la_mode)) {
1716 lmm_size = ma->ma_lmm_size;
1717 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1720 GOTO(out_pending, rc);
1723 if (!S_ISLNK(attr->la_mode)) {
1724 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1725 ma_acl->ma_acl = info->mti_xattr_buf;
1726 ma_acl->ma_need = MA_ACL_DEF;
1727 ma_acl->ma_valid = 0;
1729 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1730 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1731 mdd_read_unlock(env, mdd_pobj);
1734 else if (ma_acl->ma_valid & MA_ACL_DEF)
1738 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1739 handle = mdd_trans_start(env, mdd);
1741 GOTO(out_free, rc = PTR_ERR(handle));
1743 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1745 GOTO(out_trans, rc = -ENOMEM);
1747 mdd_write_lock(env, son, MOR_TGT_CHILD);
1748 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1750 mdd_write_unlock(env, son);
1756 #ifdef CONFIG_FS_POSIX_ACL
1758 struct lu_buf *acl_buf = &info->mti_buf;
1759 acl_buf->lb_buf = ma_acl->ma_acl;
1760 acl_buf->lb_len = ma_acl->ma_acl_size;
1762 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1764 mdd_write_unlock(env, son);
1767 ma->ma_attr.la_valid |= LA_MODE;
1772 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1773 son, ma, handle, spec);
1774 mdd_write_unlock(env, son);
1777 * Object has no links, so it will be destroyed when last
1778 * reference is released. (XXX not now.)
1784 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1785 name, S_ISDIR(attr->la_mode), handle,
1786 mdd_object_capa(env, mdd_pobj));
1793 /* No need mdd_lsm_sanity_check here */
1794 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1796 CERROR("error on stripe info copy %d \n", rc);
1799 if (lmm && lmm_size > 0) {
1800 /* Set Lov here, do not get lmm again later */
1801 memcpy(ma->ma_lmm, lmm, lmm_size);
1802 ma->ma_lmm_size = lmm_size;
1803 ma->ma_valid |= MA_LOV;
1806 if (S_ISLNK(attr->la_mode)) {
1807 struct md_ucred *uc = md_ucred(env);
1808 struct dt_object *dt = mdd_object_child(son);
1809 const char *target_name = spec->u.sp_symname;
1810 int sym_len = strlen(target_name);
1811 const struct lu_buf *buf;
1814 buf = mdd_buf_get_const(env, target_name, sym_len);
1815 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1816 mdd_object_capa(env, son),
1818 CFS_CAP_SYS_RESOURCE_MASK);
1823 GOTO(cleanup, rc = -EFAULT);
1827 la->la_valid = LA_CTIME | LA_MTIME;
1828 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1832 /* Return attr back. */
1833 rc = mdd_attr_get_internal_locked(env, son, ma);
1836 if (rc && created) {
1840 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1841 S_ISDIR(attr->la_mode),
1842 handle, BYPASS_CAPA);
1844 CERROR("error can not cleanup destroy %d\n",
1849 mdd_write_lock(env, son, MOR_TGT_CHILD);
1850 __mdd_ref_del(env, son, handle, 0);
1851 if (initialized && S_ISDIR(attr->la_mode))
1852 __mdd_ref_del(env, son, handle, 1);
1853 mdd_write_unlock(env, son);
1857 /* update lov_objid data, must be before transaction stop! */
1859 mdd_lov_objid_update(mdd, lmm);
1861 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1864 rc = mdd_changelog_ns_store(env, mdd,
1865 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1866 S_ISREG(attr->la_mode) ? CL_CREATE :
1867 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1868 0, son, mdd_pobj, NULL, lname, handle);
1869 mdd_trans_stop(env, mdd, rc, handle);
1871 /* finish lov_create stuff, free all temporary data */
1872 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1874 #ifdef HAVE_QUOTA_SUPPORT
1876 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1878 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1880 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1882 /* Trigger dqacq on the owner of child and parent. If failed,
1883 * the next call for lquota_chkquota will process it. */
1884 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1892 * Get locks on parents in proper order
1893 * RETURN: < 0 - error, rename_order if successful
1901 static int mdd_rename_order(const struct lu_env *env,
1902 struct mdd_device *mdd,
1903 struct mdd_object *src_pobj,
1904 struct mdd_object *tgt_pobj)
1906 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1910 if (src_pobj == tgt_pobj)
1911 RETURN(MDD_RN_SAME);
1913 /* compared the parent child relationship of src_p&tgt_p */
1914 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1916 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1919 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1932 /* has not mdd_write{read}_lock on any obj yet. */
1933 static int mdd_rename_sanity_check(const struct lu_env *env,
1934 struct mdd_object *src_pobj,
1935 struct mdd_object *tgt_pobj,
1936 struct mdd_object *sobj,
1937 struct mdd_object *tobj,
1943 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1946 /* XXX: when get here, sobj must NOT be NULL,
1947 * the other case has been processed in cml_rename
1948 * before mdd_rename and enable MDS_PERM_BYPASS. */
1951 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1955 /* XXX: when get here, "tobj == NULL" means tobj must
1956 * NOT exist (neither on remote MDS, such case has been
1957 * processed in cml_rename before mdd_rename and enable
1959 * So check may_create, but not check may_unlink. */
1961 rc = mdd_may_create(env, tgt_pobj, NULL,
1962 (src_pobj != tgt_pobj), 0);
1964 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1965 (src_pobj != tgt_pobj), 1);
1967 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1968 S_ISDIR(ma->ma_attr.la_mode))
1969 rc = __mdd_may_link(env, tgt_pobj);
1974 /* src object can be remote that is why we use only fid and type of object */
1975 static int mdd_rename(const struct lu_env *env,
1976 struct md_object *src_pobj, struct md_object *tgt_pobj,
1977 const struct lu_fid *lf, const struct lu_name *lsname,
1978 struct md_object *tobj, const struct lu_name *ltname,
1981 const char *sname = lsname->ln_name;
1982 const char *tname = ltname->ln_name;
1983 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1984 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1985 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1986 struct mdd_device *mdd = mdo2mdd(src_pobj);
1987 struct mdd_object *mdd_sobj = NULL; /* source object */
1988 struct mdd_object *mdd_tobj = NULL;
1989 struct dynlock_handle *sdlh, *tdlh;
1990 struct thandle *handle;
1991 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1992 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1996 #ifdef HAVE_QUOTA_SUPPORT
1997 struct obd_device *obd = mdd->mdd_obd_dev;
1998 struct obd_export *exp = md_quota(env)->mq_exp;
1999 struct mds_obd *mds = &obd->u.mds;
2000 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2001 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2002 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2003 int quota_copc = 0, quota_popc = 0;
2004 int rec_pending[MAXQUOTAS] = { 0, 0 };
2008 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2009 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2012 mdd_tobj = md2mdd_obj(tobj);
2014 #ifdef HAVE_QUOTA_SUPPORT
2015 if (mds->mds_quota) {
2016 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2018 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2020 mdd_quota_wrapper(la_tmp, qspids);
2022 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2026 mdd_data_get(env, mdd_tpobj, &data);
2027 quota_popc = FSFILT_OP_LINK;
2028 mdd_quota_wrapper(la_tmp, qtpids);
2029 /* get block quota for target parent */
2030 lquota_chkquota(mds_quota_interface_ref,
2032 rec_pending, 1, NULL,
2040 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2041 handle = mdd_trans_start(env, mdd);
2043 GOTO(out_pending, rc = PTR_ERR(handle));
2045 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2046 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2048 GOTO(cleanup_unlocked, rc);
2050 /* Get locks in determined order */
2051 if (rc == MDD_RN_SAME) {
2052 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2053 sname, MOR_SRC_PARENT);
2054 /* check hashes to determine do we need one lock or two */
2055 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2056 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2060 } else if (rc == MDD_RN_SRCTGT) {
2061 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2062 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2064 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2065 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2067 if (sdlh == NULL || tdlh == NULL)
2068 GOTO(cleanup, rc = -ENOMEM);
2070 mdd_sobj = mdd_object_find(env, mdd, lf);
2071 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2072 mdd_sobj, mdd_tobj, ma);
2076 /* Remove source name from source directory */
2077 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2078 mdd_object_capa(env, mdd_spobj));
2082 /* "mv dir1 dir2" needs "dir1/.." link update */
2083 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2084 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2085 mdd_object_capa(env, mdd_sobj));
2087 GOTO(fixup_spobj2, rc);
2089 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2090 handle, mdd_object_capa(env, mdd_sobj));
2092 GOTO(fixup_spobj, rc);
2095 /* Remove target name from target directory
2096 * Here tobj can be remote one, so we do index_delete unconditionally
2097 * and -ENOENT is allowed.
2099 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2100 mdd_object_capa(env, mdd_tpobj));
2103 /* tname might been renamed to something else */
2104 GOTO(fixup_spobj, rc);
2107 GOTO(fixup_spobj, rc);
2110 /* Insert new fid with target name into target dir */
2111 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2112 mdd_object_capa(env, mdd_tpobj));
2114 GOTO(fixup_tpobj, rc);
2116 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2117 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2119 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2121 la->la_valid = LA_CTIME;
2122 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2125 GOTO(fixup_tpobj, rc);
2128 /* Remove old target object
2129 * For tobj is remote case cmm layer has processed
2130 * and set tobj to NULL then. So when tobj is NOT NULL,
2131 * it must be local one.
2133 if (tobj && mdd_object_exists(mdd_tobj)) {
2134 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2135 if (mdd_is_dead_obj(mdd_tobj)) {
2136 mdd_write_unlock(env, mdd_tobj);
2137 /* shld not be dead, something is wrong */
2138 CERROR("tobj is dead, something is wrong\n");
2142 __mdd_ref_del(env, mdd_tobj, handle, 0);
2144 /* Remove dot reference. */
2146 __mdd_ref_del(env, mdd_tobj, handle, 1);
2148 la->la_valid = LA_CTIME;
2149 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2151 GOTO(fixup_tpobj, rc);
2153 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2154 mdd_write_unlock(env, mdd_tobj);
2156 GOTO(fixup_tpobj, rc);
2158 #ifdef HAVE_QUOTA_SUPPORT
2159 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2160 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2161 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2162 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2167 la->la_valid = LA_CTIME | LA_MTIME;
2168 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2170 GOTO(fixup_tpobj, rc);
2172 if (mdd_spobj != mdd_tpobj) {
2173 la->la_valid = LA_CTIME | LA_MTIME;
2174 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2178 if (rc == 0 && mdd_sobj) {
2179 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2180 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2181 mdo2fid(mdd_tpobj), ltname, handle);
2183 /* Old files might not have EA entry */
2184 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2186 mdd_write_unlock(env, mdd_sobj);
2187 /* We don't fail the transaction if the link ea can't be
2188 updated -- fid2path will use alternate lookup method. */
2196 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2199 CWARN("tp obj fix error %d\n",rc2);
2201 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2202 !mdd_is_dead_obj(mdd_tobj)) {
2203 rc2 = __mdd_index_insert(env, mdd_tpobj,
2204 mdo2fid(mdd_tobj), tname,
2209 CWARN("tp obj fix error %d\n",rc2);
2214 if (rc && is_dir && mdd_sobj) {
2215 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2219 CWARN("sp obj dotdot delete error %d\n",rc2);
2222 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2223 dotdot, handle, BYPASS_CAPA);
2225 CWARN("sp obj dotdot insert error %d\n",rc2);
2230 rc2 = __mdd_index_insert(env, mdd_spobj,
2231 lf, sname, is_dir, handle, BYPASS_CAPA);
2233 CWARN("sp obj fix error %d\n",rc2);
2236 if (likely(tdlh) && sdlh != tdlh)
2237 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2239 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2242 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2243 mdd_spobj, lf, lsname, handle);
2245 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2246 mdd_tpobj, lf, ltname, handle);
2248 mdd_trans_stop(env, mdd, rc, handle);
2250 mdd_object_put(env, mdd_sobj);
2252 #ifdef HAVE_QUOTA_SUPPORT
2253 if (mds->mds_quota) {
2255 lquota_pending_commit(mds_quota_interface_ref, obd,
2256 qtpids, rec_pending, 1);
2259 /* Trigger dqrel on the source owner of parent.
2260 * If failed, the next call for lquota_chkquota will
2262 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2263 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2265 /* Trigger dqrel on the target owner of child.
2266 * If failed, the next call for lquota_chkquota
2267 * will process it. */
2268 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2269 qtpids, rc, quota_copc);
2276 /** enable/disable storing of hardlink info */
2277 int mdd_linkea_enable = 1;
2278 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2279 "record hardlink info in EAs");
2281 /** Read the link EA into a temp buffer.
2282 * Uses the name_buf since it is generally large.
2283 * \retval IS_ERR err
2284 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2286 struct lu_buf *mdd_links_get(const struct lu_env *env,
2287 struct mdd_object *mdd_obj)
2290 struct lustre_capa *capa;
2291 struct link_ea_header *leh;
2294 /* First try a small buf */
2295 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2296 if (buf->lb_buf == NULL)
2297 return ERR_PTR(-ENOMEM);
2299 capa = mdd_object_capa(env, mdd_obj);
2300 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2301 if (rc == -ERANGE) {
2302 /* Buf was too small, figure out what we need. */
2304 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2307 buf = mdd_buf_alloc(env, rc);
2308 if (buf->lb_buf == NULL)
2309 return ERR_PTR(-ENOMEM);
2310 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2316 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2317 leh->leh_magic = LINK_EA_MAGIC;
2318 leh->leh_reccount = __swab32(leh->leh_reccount);
2319 leh->leh_len = __swab64(leh->leh_len);
2320 /* entries are swabbed by mdd_lee_unpack */
2322 if (leh->leh_magic != LINK_EA_MAGIC)
2323 return ERR_PTR(-EINVAL);
2324 if (leh->leh_reccount == 0)
2325 return ERR_PTR(-ENODATA);
2330 /** Pack a link_ea_entry.
2331 * All elements are stored as chars to avoid alignment issues.
2332 * Numbers are always big-endian
2333 * \retval record length
2335 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2336 const struct lu_fid *pfid)
2338 struct lu_fid tmpfid;
2341 fid_cpu_to_be(&tmpfid, pfid);
2342 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2343 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2344 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2346 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2347 lee->lee_reclen[1] = reclen & 0xff;
2351 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2352 struct lu_name *lname, struct lu_fid *pfid)
2354 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2355 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2356 fid_be_to_cpu(pfid, pfid);
2357 lname->ln_name = lee->lee_name;
2358 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2361 /** Add a record to the end of link ea buf */
2362 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2363 const struct lu_fid *pfid,
2364 const struct lu_name *lname)
2366 struct link_ea_header *leh;
2367 struct link_ea_entry *lee;
2370 if (lname == NULL || pfid == NULL)
2373 /* Make sure our buf is big enough for the new one */
2375 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2376 if (leh->leh_len + reclen > buf->lb_len) {
2377 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2382 lee = buf->lb_buf + leh->leh_len;
2383 reclen = mdd_lee_pack(lee, lname, pfid);
2384 leh->leh_len += reclen;
2385 leh->leh_reccount++;
2389 /* For pathologic linkers, we don't want to spend lots of time scanning the
2390 * link ea. Limit ourseleves to something reasonable; links not in the EA
2391 * can be looked up via (slower) parent lookup.
2393 #define LINKEA_MAX_COUNT 128
2395 static int mdd_links_add(const struct lu_env *env,
2396 struct mdd_object *mdd_obj,
2397 const struct lu_fid *pfid,
2398 const struct lu_name *lname,
2399 struct thandle *handle, int first)
2402 struct link_ea_header *leh;
2406 if (!mdd_linkea_enable)
2409 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2412 if (rc != -ENODATA) {
2413 CERROR("link_ea read failed %d "DFID"\n", rc,
2414 PFID(mdd_object_fid(mdd_obj)));
2417 /* empty EA; start one */
2418 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2419 if (buf->lb_buf == NULL)
2422 leh->leh_magic = LINK_EA_MAGIC;
2423 leh->leh_len = sizeof(struct link_ea_header);
2424 leh->leh_reccount = 0;
2428 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2431 rc = __mdd_links_add(env, buf, pfid, lname);
2436 rc = __mdd_xattr_set(env, mdd_obj,
2437 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2438 XATTR_NAME_LINK, 0, handle);
2441 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2442 PFID(mdd_object_fid(mdd_obj)));
2444 CERROR("link_ea add failed %d "DFID"\n", rc,
2445 PFID(mdd_object_fid(mdd_obj)));
2448 if (buf->lb_len > OBD_ALLOC_BIG)
2449 /* if we vmalloced a large buffer drop it */
2455 static int mdd_links_rename(const struct lu_env *env,
2456 struct mdd_object *mdd_obj,
2457 const struct lu_fid *oldpfid,
2458 const struct lu_name *oldlname,
2459 const struct lu_fid *newpfid,
2460 const struct lu_name *newlname,
2461 struct thandle *handle)
2464 struct link_ea_header *leh;
2465 struct link_ea_entry *lee;
2466 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2467 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2473 if (!mdd_linkea_enable)
2476 if (mdd_obj->mod_flags & DEAD_OBJ)
2477 /* No more links, don't bother */
2480 buf = mdd_links_get(env, mdd_obj);
2484 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2485 rc, PFID(mdd_object_fid(mdd_obj)));
2487 CERROR("link_ea read failed %d "DFID"\n",
2488 rc, PFID(mdd_object_fid(mdd_obj)));
2492 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2494 /* Find the old record */
2495 for(count = 0; count < leh->leh_reccount; count++) {
2496 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2497 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2498 lu_fid_eq(tmpfid, oldpfid) &&
2499 (strncmp(tmpname->ln_name, oldlname->ln_name,
2500 tmpname->ln_namelen) == 0))
2502 lee = (struct link_ea_entry *)((char *)lee + reclen);
2504 if ((count + 1) > leh->leh_reccount) {
2505 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2506 oldlname->ln_namelen, oldlname->ln_name);
2507 GOTO(out, rc = -ENOENT);
2510 /* Remove the old record */
2511 leh->leh_reccount--;
2512 leh->leh_len -= reclen;
2513 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2516 /* If renaming, add the new record */
2517 if (newpfid != NULL) {
2518 /* if the add fails, we still delete the out-of-date old link */
2519 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2523 rc = __mdd_xattr_set(env, mdd_obj,
2524 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2525 XATTR_NAME_LINK, 0, handle);
2531 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2532 oldlname->ln_namelen, oldlname->ln_name, rc,
2533 PFID(mdd_object_fid(mdd_obj)));
2535 if (buf->lb_len > OBD_ALLOC_BIG)
2536 /* if we vmalloced a large buffer drop it */
2542 const struct md_dir_operations mdd_dir_ops = {
2543 .mdo_is_subdir = mdd_is_subdir,
2544 .mdo_lookup = mdd_lookup,
2545 .mdo_create = mdd_create,
2546 .mdo_rename = mdd_rename,
2547 .mdo_link = mdd_link,
2548 .mdo_unlink = mdd_unlink,
2549 .mdo_name_insert = mdd_name_insert,
2550 .mdo_name_remove = mdd_name_remove,
2551 .mdo_rename_tgt = mdd_rename_tgt,
2552 .mdo_create_data = mdd_create_data,