4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #ifdef HAVE_EXT4_LDISKFS
50 #include <ldiskfs/ldiskfs_jbd2.h>
52 #include <linux/jbd.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
59 #ifdef HAVE_EXT4_LDISKFS
60 #include <ldiskfs/ldiskfs.h>
62 #include <linux/ldiskfs_fs.h>
64 #include <lustre_mds.h>
65 #include <lustre/lustre_idl.h>
66 #include <lustre_fid.h>
68 #include "mdd_internal.h"
70 static const char dot[] = ".";
71 static const char dotdot[] = "..";
73 static struct lu_name lname_dotdot = {
78 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
79 const struct lu_name *lname, struct lu_fid* fid,
81 static int mdd_declare_links_add(const struct lu_env *env,
82 struct mdd_object *mdd_obj,
83 struct thandle *handle);
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);
494 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
495 const char *name, struct thandle *handle,
496 struct lustre_capa *capa)
498 struct dt_object *next = mdd_object_child(pobj);
502 if (dt_try_as_dir(env, next)) {
503 rc = next->do_index_ops->dio_delete(env, next,
504 (struct dt_key *)name,
512 static int __mdd_index_insert_only(const struct lu_env *env,
513 struct mdd_object *pobj,
514 const struct lu_fid *lf, const char *name,
515 struct thandle *handle,
516 struct lustre_capa *capa)
518 struct dt_object *next = mdd_object_child(pobj);
522 if (dt_try_as_dir(env, next)) {
523 struct md_ucred *uc = md_ucred(env);
525 rc = next->do_index_ops->dio_insert(env, next,
527 (const struct dt_key *)name,
528 handle, capa, uc->mu_cap &
529 CFS_CAP_SYS_RESOURCE_MASK);
536 /* insert named index, add reference if isdir */
537 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
538 const struct lu_fid *lf, const char *name, int is_dir,
539 struct thandle *handle, struct lustre_capa *capa)
544 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
545 if (rc == 0 && is_dir) {
546 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
547 mdo_ref_add(env, pobj, handle);
548 mdd_write_unlock(env, pobj);
553 /* delete named index, drop reference if isdir */
554 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
555 const char *name, int is_dir, struct thandle *handle,
556 struct lustre_capa *capa)
561 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
562 if (rc == 0 && is_dir) {
563 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
564 mdo_ref_del(env, pobj, handle);
565 mdd_write_unlock(env, pobj);
571 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
572 int reclen, struct thandle *handle)
576 /* XXX: this is a temporary solution to declare llog changes
577 * will be fixed in 2.3 with new llog implementation */
579 LASSERT(mdd->mdd_capa);
581 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
582 * have to set the parameter 'size' as INT_MAX or 0 to inform
583 * OSD that this record write is for a llog write or catalog
584 * header update, and osd declare function will reserve less
585 * credits for optimization purpose.
587 * Reserve 6 blocks for a llog write, since the llog file is
588 * usually small, reserve 2 blocks for catalog header update,
589 * because we know for sure that catalog header is already
592 * This hack should be removed in 2.3.
596 rc = dt_declare_record_write(env, mdd->mdd_capa,
597 DECLARE_LLOG_WRITE, 0, handle);
601 /* header will be updated as well */
602 rc = dt_declare_record_write(env, mdd->mdd_capa,
603 DECLARE_LLOG_WRITE, 0, handle);
607 /* also we should be able to create new plain log */
608 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
612 /* new record referencing new plain llog */
613 rc = dt_declare_record_write(env, mdd->mdd_capa,
614 DECLARE_LLOG_WRITE, 0, handle);
618 /* catalog's header will be updated as well */
619 rc = dt_declare_record_write(env, mdd->mdd_capa,
620 DECLARE_LLOG_REWRITE, 0, handle);
625 int mdd_declare_changelog_store(const struct lu_env *env,
626 struct mdd_device *mdd,
627 const struct lu_name *fname,
628 struct thandle *handle)
633 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
636 /* we'll be writing payload + llog header */
637 reclen = sizeof(struct llog_changelog_rec);
639 reclen += llog_data_len(fname->ln_namelen);
641 return mdd_declare_llog_record(env, mdd, reclen, handle);
644 /** Store a namespace change changelog record
645 * If this fails, we must fail the whole transaction; we don't
646 * want the change to commit without the log entry.
647 * \param target - mdd_object of change
648 * \param parent - parent dir/object
649 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
650 * \param tname - target name string
651 * \param handle - transacion handle
653 static int mdd_changelog_ns_store(const struct lu_env *env,
654 struct mdd_device *mdd,
655 enum changelog_rec_type type,
657 struct mdd_object *target,
658 struct mdd_object *parent,
659 const struct lu_fid *tf,
660 const struct lu_name *tname,
661 struct thandle *handle)
663 const struct lu_fid *tfid;
664 const struct lu_fid *tpfid = mdo2fid(parent);
665 struct llog_changelog_rec *rec;
672 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
674 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
677 LASSERT(parent != NULL);
678 LASSERT(tname != NULL);
679 LASSERT(handle != NULL);
682 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
683 buf = mdd_buf_alloc(env, reclen);
684 if (buf->lb_buf == NULL)
686 rec = (struct llog_changelog_rec *)buf->lb_buf;
688 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
689 rec->cr.cr_type = (__u32)type;
690 tfid = tf ? tf : mdo2fid(target);
691 rec->cr.cr_tfid = *tfid;
692 rec->cr.cr_pfid = *tpfid;
693 rec->cr.cr_namelen = tname->ln_namelen;
694 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
696 target->mod_cltime = cfs_time_current_64();
698 rc = mdd_changelog_llog_write(mdd, rec, handle);
700 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
701 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
708 static int mdd_declare_link(const struct lu_env *env,
709 struct mdd_device *mdd,
710 struct mdd_object *p,
711 struct mdd_object *c,
712 const struct lu_name *name,
713 struct thandle *handle)
717 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
721 rc = mdo_declare_ref_add(env, c, handle);
725 rc = mdo_declare_attr_set(env, p, NULL, handle);
729 rc = mdo_declare_attr_set(env, c, NULL, handle);
733 rc = mdd_declare_links_add(env, c, handle);
737 rc = mdd_declare_changelog_store(env, mdd, name, handle);
742 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
743 struct md_object *src_obj, const struct lu_name *lname,
746 const char *name = lname->ln_name;
747 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
748 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
749 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
750 struct mdd_device *mdd = mdo2mdd(src_obj);
751 struct dynlock_handle *dlh;
752 struct thandle *handle;
753 #ifdef HAVE_QUOTA_SUPPORT
754 struct obd_device *obd = mdd->mdd_obd_dev;
755 struct obd_export *exp = md_quota(env)->mq_exp;
756 struct mds_obd *mds = &obd->u.mds;
757 unsigned int qids[MAXQUOTAS] = { 0, 0 };
758 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
763 #ifdef HAVE_QUOTA_SUPPORT
764 if (mds->mds_quota) {
765 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
767 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
770 mdd_data_get(env, mdd_tobj, &data);
771 quota_opc = FSFILT_OP_LINK;
772 mdd_quota_wrapper(la_tmp, qids);
773 /* get block quota for parent */
774 lquota_chkquota(mds_quota_interface_ref, obd, exp,
775 qids, rec_pending, 1, NULL,
776 LQUOTA_FLAGS_BLK, data, 1);
781 handle = mdd_trans_create(env, mdd);
783 GOTO(out_pending, rc = PTR_ERR(handle));
785 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
789 rc = mdd_trans_start(env, mdd, handle);
793 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
795 GOTO(out_trans, rc = -ENOMEM);
796 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
798 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
800 GOTO(out_unlock, rc);
802 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
804 mdd_object_capa(env, mdd_tobj));
806 GOTO(out_unlock, rc);
808 mdo_ref_add(env, mdd_sobj, handle);
810 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
811 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
813 la->la_valid = LA_CTIME | LA_MTIME;
814 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
816 GOTO(out_unlock, rc);
818 la->la_valid = LA_CTIME;
819 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
821 mdd_links_add(env, mdd_sobj,
822 mdo2fid(mdd_tobj), lname, handle, 0);
827 mdd_write_unlock(env, mdd_sobj);
828 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
831 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
832 mdd_tobj, NULL, lname, handle);
834 mdd_trans_stop(env, mdd, rc, handle);
836 #ifdef HAVE_QUOTA_SUPPORT
838 lquota_pending_commit(mds_quota_interface_ref, obd,
839 qids, rec_pending, 1);
840 /* Trigger dqacq for the parent owner. If failed,
841 * the next call for lquota_chkquota will process it. */
842 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
849 int mdd_declare_finish_unlink(const struct lu_env *env,
850 struct mdd_object *obj,
852 struct thandle *handle)
856 rc = orph_declare_index_insert(env, obj, handle);
860 return mdd_declare_object_kill(env, obj, ma, handle);
863 /* caller should take a lock before calling */
864 int mdd_finish_unlink(const struct lu_env *env,
865 struct mdd_object *obj, struct md_attr *ma,
870 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
873 LASSERT(mdd_write_locked(env, obj) != 0);
875 /* read HSM flags, needed to set changelogs flags */
876 ma->ma_need = MA_HSM | MA_INODE;
877 rc = mdd_attr_get_internal(env, obj, ma);
878 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
879 obj->mod_flags |= DEAD_OBJ;
880 /* add new orphan and the object
881 * will be deleted during mdd_close() */
882 if (obj->mod_count) {
883 rc = __mdd_orphan_add(env, obj, th);
885 CDEBUG(D_HA, "Object "DFID" is inserted into "
886 "orphan list, open count = %d\n",
887 PFID(mdd_object_fid(obj)),
890 CERROR("Object "DFID" fail to be an orphan, "
891 "open count = %d, maybe cause failed "
893 PFID(mdd_object_fid(obj)),
896 rc = mdd_object_kill(env, obj, ma, th);
901 /* get the i_nlink */
902 ma->ma_need = MA_INODE;
903 rc = mdd_attr_get_internal(env, obj, ma);
906 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
913 * has mdd_write_lock on cobj already, but not on pobj yet
915 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
916 struct mdd_object *cobj, struct md_attr *ma)
921 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
926 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
927 struct mdd_object *p, struct mdd_object *c,
928 const struct lu_name *name, struct md_attr *ma,
929 struct thandle *handle)
933 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
937 rc = mdo_declare_ref_del(env, p, handle);
941 rc = mdo_declare_ref_del(env, c, handle);
945 rc = mdo_declare_ref_del(env, c, handle);
949 rc = mdo_declare_attr_set(env, p, NULL, handle);
953 rc = mdo_declare_attr_set(env, c, NULL, handle);
957 rc = mdd_declare_finish_unlink(env, c, ma, handle);
961 rc = mdd_declare_links_add(env, c, handle);
965 rc = mdd_declare_changelog_store(env, mdd, name, handle);
970 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
971 struct md_object *cobj, const struct lu_name *lname,
974 const char *name = lname->ln_name;
975 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
976 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
977 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
978 struct mdd_device *mdd = mdo2mdd(pobj);
979 struct dynlock_handle *dlh;
980 struct thandle *handle;
981 #ifdef HAVE_QUOTA_SUPPORT
982 struct obd_device *obd = mdd->mdd_obd_dev;
983 struct mds_obd *mds = &obd->u.mds;
984 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
985 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
988 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
992 if (mdd_object_exists(mdd_cobj) <= 0)
995 handle = mdd_trans_create(env, mdd);
997 RETURN(PTR_ERR(handle));
999 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1004 rc = mdd_trans_start(env, mdd, handle);
1008 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1010 GOTO(out_trans, rc = -ENOMEM);
1011 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1013 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1017 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1018 mdd_object_capa(env, mdd_pobj));
1022 mdo_ref_del(env, mdd_cobj, handle);
1025 mdo_ref_del(env, mdd_cobj, handle);
1027 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1028 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1030 la->la_valid = LA_CTIME | LA_MTIME;
1031 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1035 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1036 /* update ctime of an unlinked file only if it is still
1037 * opened or a link still exists */
1038 la->la_valid = LA_CTIME;
1039 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1044 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1045 #ifdef HAVE_QUOTA_SUPPORT
1046 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1047 ma->ma_attr.la_nlink == 0) {
1048 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1050 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1052 mdd_quota_wrapper(la_tmp, qpids);
1053 if (mdd_cobj->mod_count == 0) {
1054 quota_opc = FSFILT_OP_UNLINK;
1055 mdd_quota_wrapper(&ma->ma_attr, qcids);
1057 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1063 /* old files may not have link ea; ignore errors */
1064 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1065 lname, NULL, NULL, handle);
1069 mdd_write_unlock(env, mdd_cobj);
1070 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1075 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1076 if ((ma->ma_valid & MA_HSM) &&
1077 (ma->ma_hsm.mh_flags & HS_EXISTS))
1078 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1080 rc = mdd_changelog_ns_store(env, mdd,
1081 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1082 mdd_cobj, mdd_pobj, NULL, lname, handle);
1086 mdd_trans_stop(env, mdd, rc, handle);
1087 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
1088 if (rc == 0 && ma->ma_valid & MA_COOKIE && ma->ma_valid & MA_LOV &&
1089 ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_UNLINK_DESTROY)
1090 /* Since echo client is incapable of destorying ost object,
1091 * it will destory the object here. */
1092 rc = mdd_lovobj_unlink(env, mdd, mdd_cobj, la,
1093 ma->ma_lmm, ma->ma_lmm_size,
1096 #warning "please remove this after 2.4 (LOD/OSP)."
1099 #ifdef HAVE_QUOTA_SUPPORT
1101 /* Trigger dqrel on the owner of child and parent. If failed,
1102 * the next call for lquota_chkquota will process it. */
1103 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1109 /* has not lock on pobj yet */
1110 static int mdd_ni_sanity_check(const struct lu_env *env,
1111 struct md_object *pobj,
1112 const struct md_attr *ma)
1114 struct mdd_object *obj = md2mdd_obj(pobj);
1118 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1121 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1127 * Partial operation.
1129 static int mdd_name_insert(const struct lu_env *env,
1130 struct md_object *pobj,
1131 const struct lu_name *lname,
1132 const struct lu_fid *fid,
1133 const struct md_attr *ma)
1135 const char *name = lname->ln_name;
1136 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1137 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1138 struct mdd_device *mdd = mdo2mdd(pobj);
1139 struct dynlock_handle *dlh;
1140 struct thandle *handle;
1141 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1142 #ifdef HAVE_QUOTA_SUPPORT
1143 struct md_ucred *uc = md_ucred(env);
1144 struct obd_device *obd = mdd->mdd_obd_dev;
1145 struct obd_export *exp = md_quota(env)->mq_exp;
1146 struct mds_obd *mds = &obd->u.mds;
1147 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1148 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1149 cfs_cap_t save = uc->mu_cap;
1154 /* XXX: this code won't be used ever:
1155 * DNE uses slightly different approach */
1158 #ifdef HAVE_QUOTA_SUPPORT
1159 if (mds->mds_quota) {
1160 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1161 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1163 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1166 mdd_data_get(env, mdd_obj, &data);
1167 quota_opc = FSFILT_OP_LINK;
1168 mdd_quota_wrapper(la_tmp, qids);
1169 /* get block quota for parent */
1170 lquota_chkquota(mds_quota_interface_ref, obd,
1171 exp, qids, rec_pending, 1, NULL,
1172 LQUOTA_FLAGS_BLK, data, 1);
1175 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1179 handle = mdd_trans_create(env, mdo2mdd(pobj));
1181 GOTO(out_pending, rc = PTR_ERR(handle));
1183 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1185 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1187 GOTO(out_trans, rc = -ENOMEM);
1189 rc = mdd_ni_sanity_check(env, pobj, ma);
1191 GOTO(out_unlock, rc);
1193 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1194 handle, BYPASS_CAPA);
1196 GOTO(out_unlock, rc);
1199 * For some case, no need update obj's ctime (LA_CTIME is not set),
1201 * For other cases, update obj's ctime (LA_CTIME is set),
1204 if (ma->ma_attr.la_valid & LA_CTIME) {
1205 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1206 la->la_valid = LA_CTIME | LA_MTIME;
1207 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1212 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1214 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1216 #ifdef HAVE_QUOTA_SUPPORT
1217 if (mds->mds_quota) {
1219 lquota_pending_commit(mds_quota_interface_ref,
1220 obd, qids, rec_pending, 1);
1221 /* Trigger dqacq for the parent owner. If failed,
1222 * the next call for lquota_chkquota will process it*/
1223 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1233 /* has not lock on pobj yet */
1234 static int mdd_nr_sanity_check(const struct lu_env *env,
1235 struct md_object *pobj,
1236 const struct md_attr *ma)
1238 struct mdd_object *obj = md2mdd_obj(pobj);
1242 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1245 rc = mdd_may_unlink(env, obj, ma);
1251 * Partial operation.
1253 static int mdd_name_remove(const struct lu_env *env,
1254 struct md_object *pobj,
1255 const struct lu_name *lname,
1256 const struct md_attr *ma)
1258 const char *name = lname->ln_name;
1259 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1260 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1261 struct mdd_device *mdd = mdo2mdd(pobj);
1262 struct dynlock_handle *dlh;
1263 struct thandle *handle;
1264 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1265 #ifdef HAVE_QUOTA_SUPPORT
1266 struct obd_device *obd = mdd->mdd_obd_dev;
1267 struct mds_obd *mds = &obd->u.mds;
1268 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1274 /* XXX: this code won't be used ever:
1275 * DNE uses slightly different approach */
1278 #ifdef HAVE_QUOTA_SUPPORT
1279 if (mds->mds_quota) {
1280 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1282 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1284 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1285 mdd_quota_wrapper(la_tmp, qids);
1289 handle = mdd_trans_create(env, mdd);
1291 GOTO(out_pending, rc = PTR_ERR(handle));
1293 rc = mdd_trans_start(env, mdd, handle);
1295 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1297 GOTO(out_trans, rc = -ENOMEM);
1299 rc = mdd_nr_sanity_check(env, pobj, ma);
1301 GOTO(out_unlock, rc);
1303 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1304 handle, BYPASS_CAPA);
1306 GOTO(out_unlock, rc);
1309 * For some case, no need update obj's ctime (LA_CTIME is not set),
1311 * For other cases, update obj's ctime (LA_CTIME is set),
1314 if (ma->ma_attr.la_valid & LA_CTIME) {
1315 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1316 la->la_valid = LA_CTIME | LA_MTIME;
1317 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1322 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1324 mdd_trans_stop(env, mdd, rc, handle);
1326 #ifdef HAVE_QUOTA_SUPPORT
1327 /* Trigger dqrel for the parent owner.
1328 * If failed, the next call for lquota_chkquota will process it. */
1330 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1338 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1340 static int mdd_rt_sanity_check(const struct lu_env *env,
1341 struct mdd_object *tgt_pobj,
1342 struct mdd_object *tobj,
1348 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1351 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1352 * exist. In fact, tobj must exist, otherwise the call trace will be:
1353 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1354 * When get here, tobj must be NOT NULL, the other case has been
1355 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1357 * So check may_delete, but not check nlink of tgt_pobj. */
1359 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1364 /* Partial rename op on slave MDD */
1365 static int mdd_rename_tgt(const struct lu_env *env,
1366 struct md_object *pobj, struct md_object *tobj,
1367 const struct lu_fid *lf, const struct lu_name *lname,
1370 const char *name = lname->ln_name;
1371 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1372 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1373 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1374 struct mdd_device *mdd = mdo2mdd(pobj);
1375 struct dynlock_handle *dlh;
1376 struct thandle *handle;
1377 #ifdef HAVE_QUOTA_SUPPORT
1378 struct obd_device *obd = mdd->mdd_obd_dev;
1379 struct obd_export *exp = md_quota(env)->mq_exp;
1380 struct mds_obd *mds = &obd->u.mds;
1381 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1382 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1383 int quota_copc = 0, quota_popc = 0;
1384 int rec_pending[MAXQUOTAS] = { 0, 0 };
1389 /* XXX: this code won't be used ever:
1390 * DNE uses slightly different approach */
1393 #ifdef HAVE_QUOTA_SUPPORT
1394 if (mds->mds_quota && !tobj) {
1395 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1397 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1400 mdd_data_get(env, mdd_tpobj, &data);
1401 quota_popc = FSFILT_OP_LINK;
1402 mdd_quota_wrapper(la_tmp, qpids);
1403 /* get block quota for target parent */
1404 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1405 qpids, rec_pending, 1, NULL,
1406 LQUOTA_FLAGS_BLK, data, 1);
1410 handle = mdd_trans_create(env, mdd);
1412 GOTO(out_pending, rc = PTR_ERR(handle));
1414 rc = mdd_trans_start(env, mdd, handle);
1416 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1418 GOTO(out_trans, rc = -ENOMEM);
1420 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1422 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1427 * If rename_tgt is called then we should just re-insert name with
1428 * correct fid, no need to dec/inc parent nlink if obj is dir.
1430 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1434 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1439 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1440 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1442 la->la_valid = LA_CTIME | LA_MTIME;
1443 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1448 * For tobj is remote case cmm layer has processed
1449 * and pass NULL tobj to here. So when tobj is NOT NULL,
1450 * it must be local one.
1452 if (tobj && mdd_object_exists(mdd_tobj)) {
1453 mdo_ref_del(env, mdd_tobj, handle);
1455 /* Remove dot reference. */
1456 if (S_ISDIR(ma->ma_attr.la_mode))
1457 mdo_ref_del(env, mdd_tobj, handle);
1459 la->la_valid = LA_CTIME;
1460 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1464 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1468 #ifdef HAVE_QUOTA_SUPPORT
1469 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1470 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1471 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1472 mdd_quota_wrapper(&ma->ma_attr, qcids);
1479 mdd_write_unlock(env, mdd_tobj);
1480 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1483 /* Bare EXT record with no RENAME in front of it signifies
1484 a partial slave op */
1485 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1486 mdd_tpobj, NULL, lname, handle);
1488 mdd_trans_stop(env, mdd, rc, handle);
1490 #ifdef HAVE_QUOTA_SUPPORT
1491 if (mds->mds_quota) {
1493 lquota_pending_commit(mds_quota_interface_ref, obd,
1494 qpids, rec_pending, 1);
1497 /* Trigger dqrel on the target owner of child.
1498 * If failed, the next call for lquota_chkquota
1499 * will process it. */
1500 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1508 * The permission has been checked when obj created, no need check again.
1510 static int mdd_cd_sanity_check(const struct lu_env *env,
1511 struct mdd_object *obj)
1516 if (!obj || mdd_is_dead_obj(obj))
1523 static int mdd_declare_create_data(const struct lu_env *env,
1524 struct mdd_device *mdd,
1525 struct mdd_object *obj,
1527 struct thandle *handle)
1529 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1533 buf->lb_len = lmm_size;
1534 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1539 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1544 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1545 struct md_object *cobj, const struct md_op_spec *spec,
1548 struct mdd_device *mdd = mdo2mdd(cobj);
1549 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1550 struct mdd_object *son = md2mdd_obj(cobj);
1551 struct lov_mds_md *lmm = NULL;
1553 struct thandle *handle;
1557 rc = mdd_cd_sanity_check(env, son);
1561 if (!md_should_create(spec->sp_cr_flags))
1563 lmm_size = ma->ma_lmm_size;
1565 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1569 handle = mdd_trans_create(env, mdd);
1571 GOTO(out_free, rc = PTR_ERR(handle));
1573 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1577 rc = mdd_trans_start(env, mdd, handle);
1582 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1583 * Should this be fixed?
1586 /* Replay creates has objects already */
1588 if (spec->no_create) {
1589 CDEBUG(D_INFO, "we already have lov ea\n");
1590 rc = mdd_lov_set_md(env, mdd_pobj, son,
1591 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1592 spec->u.sp_ea.eadatalen, handle, 0);
1595 /* No need mdd_lsm_sanity_check here */
1596 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1597 lmm_size, handle, 0);
1600 rc = mdd_attr_get_internal_locked(env, son, ma);
1602 /* update lov_objid data, must be before transaction stop! */
1604 mdd_lov_objid_update(mdd, lmm);
1607 mdd_trans_stop(env, mdd, rc, handle);
1609 /* Finish mdd_lov_create() stuff. */
1610 /* if no_create == 0 (not replay), we free lmm allocated by
1611 * mdd_lov_create() */
1612 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1616 /* Get fid from name and parent */
1618 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1619 const struct lu_name *lname, struct lu_fid* fid, int mask)
1621 const char *name = lname->ln_name;
1622 const struct dt_key *key = (const struct dt_key *)name;
1623 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1624 struct mdd_device *m = mdo2mdd(pobj);
1625 struct dt_object *dir = mdd_object_child(mdd_obj);
1629 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1632 rc = mdd_object_exists(mdd_obj);
1633 if (unlikely(rc == 0))
1635 else if (unlikely(rc < 0)) {
1636 CERROR("Object "DFID" locates on remote server\n",
1637 PFID(mdo2fid(mdd_obj)));
1641 /* The common filename length check. */
1642 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1643 RETURN(-ENAMETOOLONG);
1645 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1650 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1651 dt_try_as_dir(env, dir))) {
1653 rc = dir->do_index_ops->dio_lookup(env, dir,
1654 (struct dt_rec *)fid, key,
1655 mdd_object_capa(env, mdd_obj));
1666 int mdd_declare_object_initialize(const struct lu_env *env,
1667 struct mdd_object *child,
1669 struct thandle *handle)
1673 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1674 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1675 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1678 rc = mdo_declare_ref_add(env, child, handle);
1681 mdd_declare_links_add(env, child, handle);
1686 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1687 const struct lu_name *lname, struct mdd_object *child,
1688 struct md_attr *ma, struct thandle *handle,
1689 const struct md_op_spec *spec)
1695 * Update attributes for child.
1698 * (1) the valid bits should be converted between Lustre and Linux;
1699 * (2) maybe, the child attributes should be set in OSD when creation.
1702 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1706 if (S_ISDIR(ma->ma_attr.la_mode)) {
1707 /* Add "." and ".." for newly created dir */
1708 mdo_ref_add(env, child, handle);
1709 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1710 dot, handle, BYPASS_CAPA);
1712 rc = __mdd_index_insert_only(env, child, pfid,
1716 mdo_ref_del(env, child, handle);
1719 mdd_links_add(env, child, pfid, lname, handle, 1);
1724 /* has not lock on pobj yet */
1725 static int mdd_create_sanity_check(const struct lu_env *env,
1726 struct md_object *pobj,
1727 const struct lu_name *lname,
1729 struct md_op_spec *spec)
1731 struct mdd_thread_info *info = mdd_env_info(env);
1732 struct lu_attr *la = &info->mti_la;
1733 struct lu_fid *fid = &info->mti_fid;
1734 struct mdd_object *obj = md2mdd_obj(pobj);
1735 struct mdd_device *m = mdo2mdd(pobj);
1736 int lookup = spec->sp_cr_lookup;
1741 if (mdd_is_dead_obj(obj))
1745 * In some cases this lookup is not needed - we know before if name
1746 * exists or not because MDT performs lookup for it.
1747 * name length check is done in lookup.
1751 * Check if the name already exist, though it will be checked in
1752 * _index_insert also, for avoiding rolling back if exists
1755 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1756 MAY_WRITE | MAY_EXEC);
1758 RETURN(rc ? : -EEXIST);
1761 * Check WRITE permission for the parent.
1762 * EXEC permission have been checked
1763 * when lookup before create already.
1765 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1772 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1776 if (la->la_mode & S_ISGID) {
1777 ma->ma_attr.la_gid = la->la_gid;
1778 if (S_ISDIR(ma->ma_attr.la_mode)) {
1779 ma->ma_attr.la_mode |= S_ISGID;
1780 ma->ma_attr.la_valid |= LA_MODE;
1784 switch (ma->ma_attr.la_mode & S_IFMT) {
1786 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1788 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1789 RETURN(-ENAMETOOLONG);
1808 static int mdd_declare_create(const struct lu_env *env,
1809 struct mdd_device *mdd,
1810 struct mdd_object *p,
1811 struct mdd_object *c,
1812 const struct lu_name *name,
1815 struct thandle *handle,
1816 const struct md_op_spec *spec)
1818 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1821 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1825 /* if dir, then can inherit default ACl */
1827 buf->lb_len = lmm_size;
1828 if (S_ISDIR(ma->ma_attr.la_mode)) {
1829 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1832 rc = mdo_declare_ref_add(env, p, handle);
1837 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1842 rc = mdd_declare_object_initialize(env, c, ma, handle);
1846 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1847 name->ln_name, handle);
1851 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1856 if (S_ISLNK(ma->ma_attr.la_mode)) {
1857 rc = dt_declare_record_write(env, mdd_object_child(c),
1858 strlen(spec->u.sp_symname), 0,
1863 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1867 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1871 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1879 * Create object and insert it into namespace.
1881 static int mdd_create(const struct lu_env *env,
1882 struct md_object *pobj,
1883 const struct lu_name *lname,
1884 struct md_object *child,
1885 struct md_op_spec *spec,
1888 struct mdd_thread_info *info = mdd_env_info(env);
1889 struct lu_attr *la = &info->mti_la_for_fix;
1890 struct md_attr *ma_acl = &info->mti_ma;
1891 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1892 struct mdd_object *son = md2mdd_obj(child);
1893 struct mdd_device *mdd = mdo2mdd(pobj);
1894 struct lu_attr *attr = &ma->ma_attr;
1895 struct lov_mds_md *lmm = NULL;
1896 struct thandle *handle;
1897 struct dynlock_handle *dlh;
1898 const char *name = lname->ln_name;
1899 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1900 int got_def_acl = 0;
1901 #ifdef HAVE_QUOTA_SUPPORT
1902 struct obd_device *obd = mdd->mdd_obd_dev;
1903 struct obd_export *exp = md_quota(env)->mq_exp;
1904 struct mds_obd *mds = &obd->u.mds;
1905 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1906 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1907 int quota_opc = 0, block_count = 0;
1908 int inode_pending[MAXQUOTAS] = { 0, 0 };
1909 int block_pending[MAXQUOTAS] = { 0, 0 };
1910 int parent_pending[MAXQUOTAS] = { 0, 0 };
1915 * Two operations have to be performed:
1917 * - an allocation of a new object (->do_create()), and
1919 * - an insertion into a parent index (->dio_insert()).
1921 * Due to locking, operation order is not important, when both are
1922 * successful, *but* error handling cases are quite different:
1924 * - if insertion is done first, and following object creation fails,
1925 * insertion has to be rolled back, but this operation might fail
1926 * also leaving us with dangling index entry.
1928 * - if creation is done first, is has to be undone if insertion
1929 * fails, leaving us with leaked space, which is neither good, nor
1932 * It seems that creation-first is simplest solution, but it is
1933 * sub-optimal in the frequent
1938 * case, because second mkdir is bound to create object, only to
1939 * destroy it immediately.
1941 * To avoid this follow local file systems that do double lookup:
1943 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1945 * 1. create (mdd_object_create_internal())
1947 * 2. insert (__mdd_index_insert(), lookup again)
1950 /* Sanity checks before big job. */
1951 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1955 #ifdef HAVE_QUOTA_SUPPORT
1956 if (mds->mds_quota) {
1957 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1959 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1963 quota_opc = FSFILT_OP_CREATE;
1964 mdd_quota_wrapper(&ma->ma_attr, qcids);
1965 mdd_quota_wrapper(la_tmp, qpids);
1966 /* get file quota for child */
1967 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1968 qcids, inode_pending, 1, NULL, 0, NULL,
1970 switch (ma->ma_attr.la_mode & S_IFMT) {
1979 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1980 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1984 /* get block quota for child and parent */
1986 lquota_chkquota(mds_quota_interface_ref, obd,
1987 exp, qcids, block_pending,
1989 LQUOTA_FLAGS_BLK, NULL, 0);
1991 lquota_chkquota(mds_quota_interface_ref, obd,
1992 exp, qpids, parent_pending, 1,
1993 NULL, LQUOTA_FLAGS_BLK, NULL,
1999 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2000 GOTO(out_pending, rc = -EINPROGRESS);
2003 * No RPC inside the transaction, so OST objects should be created at
2006 if (S_ISREG(attr->la_mode)) {
2007 lmm_size = ma->ma_lmm_size;
2008 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
2011 GOTO(out_pending, rc);
2014 if (!S_ISLNK(attr->la_mode)) {
2015 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2016 ma_acl->ma_acl = info->mti_xattr_buf;
2017 ma_acl->ma_need = MA_ACL_DEF;
2018 ma_acl->ma_valid = 0;
2020 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2021 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2022 mdd_read_unlock(env, mdd_pobj);
2025 else if (ma_acl->ma_valid & MA_ACL_DEF)
2029 handle = mdd_trans_create(env, mdd);
2031 GOTO(out_free, rc = PTR_ERR(handle));
2033 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2034 lmm_size, handle, spec);
2038 rc = mdd_trans_start(env, mdd, handle);
2042 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2044 GOTO(out_trans, rc = -ENOMEM);
2046 mdd_write_lock(env, son, MOR_TGT_CHILD);
2047 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2049 mdd_write_unlock(env, son);
2055 #ifdef CONFIG_FS_POSIX_ACL
2057 struct lu_buf *acl_buf = &info->mti_buf;
2058 acl_buf->lb_buf = ma_acl->ma_acl;
2059 acl_buf->lb_len = ma_acl->ma_acl_size;
2061 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2063 mdd_write_unlock(env, son);
2066 ma->ma_attr.la_valid |= LA_MODE;
2071 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2072 son, ma, handle, spec);
2073 mdd_write_unlock(env, son);
2076 * Object has no links, so it will be destroyed when last
2077 * reference is released. (XXX not now.)
2083 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2084 name, S_ISDIR(attr->la_mode), handle,
2085 mdd_object_capa(env, mdd_pobj));
2092 /* No need mdd_lsm_sanity_check here */
2093 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2095 CERROR("error on stripe info copy %d \n", rc);
2098 if (lmm && lmm_size > 0) {
2099 /* Set Lov here, do not get lmm again later */
2100 if (lmm_size > ma->ma_lmm_size) {
2101 /* Reply buffer is smaller, need bigger one */
2102 mdd_max_lmm_buffer(env, lmm_size);
2103 if (unlikely(info->mti_max_lmm == NULL))
2104 GOTO(cleanup, rc = -ENOMEM);
2105 ma->ma_lmm = info->mti_max_lmm;
2106 ma->ma_big_lmm_used = 1;
2108 memcpy(ma->ma_lmm, lmm, lmm_size);
2109 ma->ma_lmm_size = lmm_size;
2110 ma->ma_valid |= MA_LOV;
2113 if (S_ISLNK(attr->la_mode)) {
2114 struct md_ucred *uc = md_ucred(env);
2115 struct dt_object *dt = mdd_object_child(son);
2116 const char *target_name = spec->u.sp_symname;
2117 int sym_len = strlen(target_name);
2118 const struct lu_buf *buf;
2121 buf = mdd_buf_get_const(env, target_name, sym_len);
2122 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2123 mdd_object_capa(env, son),
2125 CFS_CAP_SYS_RESOURCE_MASK);
2130 GOTO(cleanup, rc = -EFAULT);
2134 la->la_valid = LA_CTIME | LA_MTIME;
2135 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2139 /* Return attr back. */
2140 rc = mdd_attr_get_internal_locked(env, son, ma);
2143 if (rc && created) {
2147 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2148 S_ISDIR(attr->la_mode),
2149 handle, BYPASS_CAPA);
2151 CERROR("error can not cleanup destroy %d\n",
2156 mdd_write_lock(env, son, MOR_TGT_CHILD);
2157 mdo_ref_del(env, son, handle);
2158 if (initialized && S_ISDIR(attr->la_mode))
2159 mdo_ref_del(env, son, handle);
2160 mdd_write_unlock(env, son);
2164 /* update lov_objid data, must be before transaction stop! */
2166 mdd_lov_objid_update(mdd, lmm);
2168 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2171 rc = mdd_changelog_ns_store(env, mdd,
2172 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2173 S_ISREG(attr->la_mode) ? CL_CREATE :
2174 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2175 0, son, mdd_pobj, NULL, lname, handle);
2177 mdd_trans_stop(env, mdd, rc, handle);
2179 /* finish lov_create stuff, free all temporary data */
2180 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2182 #ifdef HAVE_QUOTA_SUPPORT
2184 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2186 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2188 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2190 /* Trigger dqacq on the owner of child and parent. If failed,
2191 * the next call for lquota_chkquota will process it. */
2192 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2196 /* The child object shouldn't be cached anymore */
2198 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
2199 &child->mo_lu.lo_header->loh_flags);
2204 * Get locks on parents in proper order
2205 * RETURN: < 0 - error, rename_order if successful
2213 static int mdd_rename_order(const struct lu_env *env,
2214 struct mdd_device *mdd,
2215 struct mdd_object *src_pobj,
2216 struct mdd_object *tgt_pobj)
2218 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2222 if (src_pobj == tgt_pobj)
2223 RETURN(MDD_RN_SAME);
2225 /* compared the parent child relationship of src_p&tgt_p */
2226 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2228 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2231 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2244 /* has not mdd_write{read}_lock on any obj yet. */
2245 static int mdd_rename_sanity_check(const struct lu_env *env,
2246 struct mdd_object *src_pobj,
2247 struct mdd_object *tgt_pobj,
2248 struct mdd_object *sobj,
2249 struct mdd_object *tobj,
2255 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2258 /* XXX: when get here, sobj must NOT be NULL,
2259 * the other case has been processed in cml_rename
2260 * before mdd_rename and enable MDS_PERM_BYPASS. */
2263 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2267 /* XXX: when get here, "tobj == NULL" means tobj must
2268 * NOT exist (neither on remote MDS, such case has been
2269 * processed in cml_rename before mdd_rename and enable
2271 * So check may_create, but not check may_unlink. */
2273 rc = mdd_may_create(env, tgt_pobj, NULL,
2274 (src_pobj != tgt_pobj), 0);
2276 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2277 (src_pobj != tgt_pobj), 1);
2279 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2280 S_ISDIR(ma->ma_attr.la_mode))
2281 rc = __mdd_may_link(env, tgt_pobj);
2286 static int mdd_declare_rename(const struct lu_env *env,
2287 struct mdd_device *mdd,
2288 struct mdd_object *mdd_spobj,
2289 struct mdd_object *mdd_tpobj,
2290 struct mdd_object *mdd_sobj,
2291 struct mdd_object *mdd_tobj,
2292 const struct lu_name *sname,
2293 const struct lu_name *tname,
2295 struct thandle *handle)
2303 /* name from source dir */
2304 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2308 /* .. from source child */
2309 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2310 /* source child can be directory,
2311 * counted by source dir's nlink */
2312 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2316 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2320 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2325 /* new target child can be directory,
2326 * counted by target dir's nlink */
2327 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2333 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2337 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2340 mdd_declare_links_add(env, mdd_sobj, handle);
2344 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2349 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2350 tname->ln_name, handle);
2354 /* name from target dir (old name), we declare it unconditionally
2355 * as mdd_rename() calls delete unconditionally as well. so just
2356 * to balance declarations vs calls to change ... */
2357 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2361 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2362 /* delete target child in target parent directory */
2363 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2367 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2368 /* target child can be directory,
2369 * delete "." reference in target child directory */
2370 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2374 /* delete ".." reference in target parent directory */
2375 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2380 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2384 mdd_declare_links_add(env, mdd_tobj, handle);
2388 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2393 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2397 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2404 /* src object can be remote that is why we use only fid and type of object */
2405 static int mdd_rename(const struct lu_env *env,
2406 struct md_object *src_pobj, struct md_object *tgt_pobj,
2407 const struct lu_fid *lf, const struct lu_name *lsname,
2408 struct md_object *tobj, const struct lu_name *ltname,
2411 const char *sname = lsname->ln_name;
2412 const char *tname = ltname->ln_name;
2413 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2414 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2415 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2416 struct mdd_device *mdd = mdo2mdd(src_pobj);
2417 struct mdd_object *mdd_sobj = NULL; /* source object */
2418 struct mdd_object *mdd_tobj = NULL;
2419 struct dynlock_handle *sdlh, *tdlh;
2420 struct thandle *handle;
2421 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2422 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2426 #ifdef HAVE_QUOTA_SUPPORT
2427 struct obd_device *obd = mdd->mdd_obd_dev;
2428 struct obd_export *exp = md_quota(env)->mq_exp;
2429 struct mds_obd *mds = &obd->u.mds;
2430 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2431 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2432 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2433 int quota_copc = 0, quota_popc = 0;
2434 int rec_pending[MAXQUOTAS] = { 0, 0 };
2438 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2439 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2442 mdd_tobj = md2mdd_obj(tobj);
2444 #ifdef HAVE_QUOTA_SUPPORT
2445 if (mds->mds_quota) {
2446 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2448 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2450 mdd_quota_wrapper(la_tmp, qspids);
2452 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2456 mdd_data_get(env, mdd_tpobj, &data);
2457 quota_popc = FSFILT_OP_LINK;
2458 mdd_quota_wrapper(la_tmp, qtpids);
2459 /* get block quota for target parent */
2460 lquota_chkquota(mds_quota_interface_ref,
2462 rec_pending, 1, NULL,
2470 mdd_sobj = mdd_object_find(env, mdd, lf);
2472 handle = mdd_trans_create(env, mdd);
2474 GOTO(out_pending, rc = PTR_ERR(handle));
2476 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2477 mdd_tobj, lsname, ltname, ma, handle);
2481 rc = mdd_trans_start(env, mdd, handle);
2485 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2486 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2488 GOTO(cleanup_unlocked, rc);
2490 /* Get locks in determined order */
2491 if (rc == MDD_RN_SAME) {
2492 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2493 sname, MOR_SRC_PARENT);
2494 /* check hashes to determine do we need one lock or two */
2495 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2496 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2500 } else if (rc == MDD_RN_SRCTGT) {
2501 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2502 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2504 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2505 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2507 if (sdlh == NULL || tdlh == NULL)
2508 GOTO(cleanup, rc = -ENOMEM);
2510 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2511 mdd_sobj, mdd_tobj, ma);
2515 /* Remove source name from source directory */
2516 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2517 mdd_object_capa(env, mdd_spobj));
2521 /* "mv dir1 dir2" needs "dir1/.." link update */
2522 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2523 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2524 mdd_object_capa(env, mdd_sobj));
2526 GOTO(fixup_spobj2, rc);
2528 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2529 handle, mdd_object_capa(env, mdd_sobj));
2531 GOTO(fixup_spobj, rc);
2534 /* Remove target name from target directory
2535 * Here tobj can be remote one, so we do index_delete unconditionally
2536 * and -ENOENT is allowed.
2538 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2539 mdd_object_capa(env, mdd_tpobj));
2542 /* tname might been renamed to something else */
2543 GOTO(fixup_spobj, rc);
2546 GOTO(fixup_spobj, rc);
2549 /* Insert new fid with target name into target dir */
2550 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2551 mdd_object_capa(env, mdd_tpobj));
2553 GOTO(fixup_tpobj, rc);
2555 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2556 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2558 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2560 la->la_valid = LA_CTIME;
2561 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2564 GOTO(fixup_tpobj, rc);
2567 /* Remove old target object
2568 * For tobj is remote case cmm layer has processed
2569 * and set tobj to NULL then. So when tobj is NOT NULL,
2570 * it must be local one.
2572 if (tobj && mdd_object_exists(mdd_tobj)) {
2573 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2574 if (mdd_is_dead_obj(mdd_tobj)) {
2575 mdd_write_unlock(env, mdd_tobj);
2576 /* shld not be dead, something is wrong */
2577 CERROR("tobj is dead, something is wrong\n");
2581 mdo_ref_del(env, mdd_tobj, handle);
2583 /* Remove dot reference. */
2585 mdo_ref_del(env, mdd_tobj, handle);
2587 la->la_valid = LA_CTIME;
2588 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2590 GOTO(fixup_tpobj, rc);
2592 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2593 mdd_write_unlock(env, mdd_tobj);
2595 GOTO(fixup_tpobj, rc);
2597 #ifdef HAVE_QUOTA_SUPPORT
2598 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2599 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2600 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2601 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2606 la->la_valid = LA_CTIME | LA_MTIME;
2607 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2609 GOTO(fixup_tpobj, rc);
2611 if (mdd_spobj != mdd_tpobj) {
2612 la->la_valid = LA_CTIME | LA_MTIME;
2613 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2617 if (rc == 0 && mdd_sobj) {
2618 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2619 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2620 mdo2fid(mdd_tpobj), ltname, handle);
2622 /* Old files might not have EA entry */
2623 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2625 mdd_write_unlock(env, mdd_sobj);
2626 /* We don't fail the transaction if the link ea can't be
2627 updated -- fid2path will use alternate lookup method. */
2635 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2638 CWARN("tp obj fix error %d\n",rc2);
2640 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2641 !mdd_is_dead_obj(mdd_tobj)) {
2642 rc2 = __mdd_index_insert(env, mdd_tpobj,
2643 mdo2fid(mdd_tobj), tname,
2648 CWARN("tp obj fix error %d\n",rc2);
2653 if (rc && is_dir && mdd_sobj) {
2654 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2658 CWARN("sp obj dotdot delete error %d\n",rc2);
2661 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2662 dotdot, handle, BYPASS_CAPA);
2664 CWARN("sp obj dotdot insert error %d\n",rc2);
2669 rc2 = __mdd_index_insert(env, mdd_spobj,
2670 lf, sname, is_dir, handle, BYPASS_CAPA);
2672 CWARN("sp obj fix error %d\n",rc2);
2675 if (likely(tdlh) && sdlh != tdlh)
2676 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2678 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2681 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2682 mdd_spobj, lf, lsname, handle);
2684 struct lu_fid zero_fid;
2685 fid_zero(&zero_fid);
2686 /* If the rename target exist, The CL_EXT record should save
2687 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2688 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2690 mdd_tobj ? NULL : &zero_fid,
2695 mdd_trans_stop(env, mdd, rc, handle);
2697 mdd_object_put(env, mdd_sobj);
2699 #ifdef HAVE_QUOTA_SUPPORT
2700 if (mds->mds_quota) {
2702 lquota_pending_commit(mds_quota_interface_ref, obd,
2703 qtpids, rec_pending, 1);
2706 /* Trigger dqrel on the source owner of parent.
2707 * If failed, the next call for lquota_chkquota will
2709 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2710 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2712 /* Trigger dqrel on the target owner of child.
2713 * If failed, the next call for lquota_chkquota
2714 * will process it. */
2715 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2716 qtpids, rc, quota_copc);
2723 /** enable/disable storing of hardlink info */
2724 int mdd_linkea_enable = 1;
2725 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2726 "record hardlink info in EAs");
2728 /** Read the link EA into a temp buffer.
2729 * Uses the name_buf since it is generally large.
2730 * \retval IS_ERR err
2731 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2733 struct lu_buf *mdd_links_get(const struct lu_env *env,
2734 struct mdd_object *mdd_obj)
2737 struct lustre_capa *capa;
2738 struct link_ea_header *leh;
2741 /* First try a small buf */
2742 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2743 if (buf->lb_buf == NULL)
2744 return ERR_PTR(-ENOMEM);
2746 capa = mdd_object_capa(env, mdd_obj);
2747 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2748 if (rc == -ERANGE) {
2749 /* Buf was too small, figure out what we need. */
2751 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2754 buf = mdd_buf_alloc(env, rc);
2755 if (buf->lb_buf == NULL)
2756 return ERR_PTR(-ENOMEM);
2757 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2763 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2764 leh->leh_magic = LINK_EA_MAGIC;
2765 leh->leh_reccount = __swab32(leh->leh_reccount);
2766 leh->leh_len = __swab64(leh->leh_len);
2767 /* entries are swabbed by mdd_lee_unpack */
2769 if (leh->leh_magic != LINK_EA_MAGIC)
2770 return ERR_PTR(-EINVAL);
2771 if (leh->leh_reccount == 0)
2772 return ERR_PTR(-ENODATA);
2777 /** Pack a link_ea_entry.
2778 * All elements are stored as chars to avoid alignment issues.
2779 * Numbers are always big-endian
2780 * \retval record length
2782 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2783 const struct lu_fid *pfid)
2785 struct lu_fid tmpfid;
2788 fid_cpu_to_be(&tmpfid, pfid);
2789 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2790 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2791 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2793 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2794 lee->lee_reclen[1] = reclen & 0xff;
2798 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2799 struct lu_name *lname, struct lu_fid *pfid)
2801 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2802 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2803 fid_be_to_cpu(pfid, pfid);
2804 lname->ln_name = lee->lee_name;
2805 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2808 /** Add a record to the end of link ea buf */
2809 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2810 const struct lu_fid *pfid,
2811 const struct lu_name *lname)
2813 struct link_ea_header *leh;
2814 struct link_ea_entry *lee;
2817 if (lname == NULL || pfid == NULL)
2820 /* Make sure our buf is big enough for the new one */
2822 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2823 if (leh->leh_len + reclen > buf->lb_len) {
2824 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2829 lee = buf->lb_buf + leh->leh_len;
2830 reclen = mdd_lee_pack(lee, lname, pfid);
2831 leh->leh_len += reclen;
2832 leh->leh_reccount++;
2836 static int mdd_declare_links_add(const struct lu_env *env,
2837 struct mdd_object *mdd_obj,
2838 struct thandle *handle)
2842 /* XXX: max size? */
2843 rc = mdo_declare_xattr_set(env, mdd_obj,
2844 mdd_buf_get_const(env, NULL, 4096),
2845 XATTR_NAME_LINK, 0, handle);
2850 /* For pathologic linkers, we don't want to spend lots of time scanning the
2851 * link ea. Limit ourseleves to something reasonable; links not in the EA
2852 * can be looked up via (slower) parent lookup.
2854 #define LINKEA_MAX_COUNT 128
2856 static int mdd_links_add(const struct lu_env *env,
2857 struct mdd_object *mdd_obj,
2858 const struct lu_fid *pfid,
2859 const struct lu_name *lname,
2860 struct thandle *handle, int first)
2863 struct link_ea_header *leh;
2867 if (!mdd_linkea_enable)
2870 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2873 if (rc != -ENODATA) {
2874 CERROR("link_ea read failed %d "DFID"\n", rc,
2875 PFID(mdd_object_fid(mdd_obj)));
2878 /* empty EA; start one */
2879 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2880 if (buf->lb_buf == NULL)
2883 leh->leh_magic = LINK_EA_MAGIC;
2884 leh->leh_len = sizeof(struct link_ea_header);
2885 leh->leh_reccount = 0;
2889 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2892 rc = __mdd_links_add(env, buf, pfid, lname);
2897 rc = __mdd_xattr_set(env, mdd_obj,
2898 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2899 XATTR_NAME_LINK, 0, handle);
2902 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2903 PFID(mdd_object_fid(mdd_obj)));
2905 CERROR("link_ea add failed %d "DFID"\n", rc,
2906 PFID(mdd_object_fid(mdd_obj)));
2909 if (buf->lb_len > OBD_ALLOC_BIG)
2910 /* if we vmalloced a large buffer drop it */
2916 static int mdd_links_rename(const struct lu_env *env,
2917 struct mdd_object *mdd_obj,
2918 const struct lu_fid *oldpfid,
2919 const struct lu_name *oldlname,
2920 const struct lu_fid *newpfid,
2921 const struct lu_name *newlname,
2922 struct thandle *handle)
2925 struct link_ea_header *leh;
2926 struct link_ea_entry *lee;
2927 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2928 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2934 if (!mdd_linkea_enable)
2937 if (mdd_obj->mod_flags & DEAD_OBJ)
2938 /* No more links, don't bother */
2941 buf = mdd_links_get(env, mdd_obj);
2945 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2946 rc, PFID(mdd_object_fid(mdd_obj)));
2948 CERROR("link_ea read failed %d "DFID"\n",
2949 rc, PFID(mdd_object_fid(mdd_obj)));
2953 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2955 /* Find the old record */
2956 for(count = 0; count < leh->leh_reccount; count++) {
2957 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2958 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2959 lu_fid_eq(tmpfid, oldpfid) &&
2960 (strncmp(tmpname->ln_name, oldlname->ln_name,
2961 tmpname->ln_namelen) == 0))
2963 lee = (struct link_ea_entry *)((char *)lee + reclen);
2965 if ((count + 1) > leh->leh_reccount) {
2966 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2967 oldlname->ln_namelen, oldlname->ln_name);
2968 GOTO(out, rc = -ENOENT);
2971 /* Remove the old record */
2972 leh->leh_reccount--;
2973 leh->leh_len -= reclen;
2974 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2977 /* If renaming, add the new record */
2978 if (newpfid != NULL) {
2979 /* if the add fails, we still delete the out-of-date old link */
2980 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2984 rc = __mdd_xattr_set(env, mdd_obj,
2985 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2986 XATTR_NAME_LINK, 0, handle);
2992 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2993 oldlname->ln_namelen, oldlname->ln_name, rc,
2994 PFID(mdd_object_fid(mdd_obj)));
2996 if (buf->lb_len > OBD_ALLOC_BIG)
2997 /* if we vmalloced a large buffer drop it */
3003 const struct md_dir_operations mdd_dir_ops = {
3004 .mdo_is_subdir = mdd_is_subdir,
3005 .mdo_lookup = mdd_lookup,
3006 .mdo_create = mdd_create,
3007 .mdo_rename = mdd_rename,
3008 .mdo_link = mdd_link,
3009 .mdo_unlink = mdd_unlink,
3010 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
3011 .mdo_name_insert = mdd_name_insert,
3012 .mdo_name_remove = mdd_name_remove,
3013 .mdo_rename_tgt = mdd_rename_tgt,
3014 .mdo_create_data = mdd_create_data,