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>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <linux/module.h>
46 #include <ldiskfs/ldiskfs_jbd2.h>
48 #include <obd_class.h>
49 #include <lustre_ver.h>
50 #include <obd_support.h>
51 #include <lprocfs_status.h>
52 #include <ldiskfs/ldiskfs.h>
53 #include <lustre_mds.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre_fid.h>
57 #include "mdd_internal.h"
59 static const char dot[] = ".";
60 static const char dotdot[] = "..";
62 static struct lu_name lname_dotdot = {
67 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
68 const struct lu_name *lname, struct lu_fid* fid,
70 static int mdd_declare_links_add(const struct lu_env *env,
71 struct mdd_object *mdd_obj,
72 struct thandle *handle);
73 static int mdd_links_add(const struct lu_env *env,
74 struct mdd_object *mdd_obj,
75 const struct lu_fid *pfid,
76 const struct lu_name *lname,
77 struct thandle *handle, int first);
78 static int mdd_links_rename(const struct lu_env *env,
79 struct mdd_object *mdd_obj,
80 const struct lu_fid *oldpfid,
81 const struct lu_name *oldlname,
82 const struct lu_fid *newpfid,
83 const struct lu_name *newlname,
84 struct thandle *handle);
87 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
88 const struct lu_name *lname, struct lu_fid* fid, int mask)
90 const char *name = lname->ln_name;
91 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
92 struct dynlock_handle *dlh;
95 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
96 if (unlikely(dlh == NULL))
98 rc = __mdd_lookup(env, pobj, lname, fid, mask);
99 mdd_pdo_read_unlock(env, mdd_obj, dlh);
104 int mdd_lookup(const struct lu_env *env,
105 struct md_object *pobj, const struct lu_name *lname,
106 struct lu_fid* fid, struct md_op_spec *spec)
110 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
114 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
117 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
121 * For root fid use special function, which does not compare version component
122 * of fid. Version component is different for root fids on all MDTs.
124 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
126 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
127 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
131 * return 1: if lf is the fid of the ancestor of p1;
134 * return -EREMOTE: if remote object is found, in this
135 * case fid of remote object is saved to @pf;
137 * otherwise: values < 0, errors.
139 static int mdd_is_parent(const struct lu_env *env,
140 struct mdd_device *mdd,
141 struct mdd_object *p1,
142 const struct lu_fid *lf,
145 struct mdd_object *parent = NULL;
150 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
151 pfid = &mdd_env_info(env)->mti_fid;
153 /* Check for root first. */
154 if (mdd_is_root(mdd, mdo2fid(p1)))
158 /* this is done recursively, bypass capa for each obj */
159 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
160 rc = mdd_parent_fid(env, p1, pfid);
163 if (mdd_is_root(mdd, pfid))
165 if (lu_fid_eq(pfid, lf))
168 mdd_object_put(env, parent);
169 parent = mdd_object_find(env, mdd, pfid);
171 /* cross-ref parent */
172 if (parent == NULL) {
175 GOTO(out, rc = -EREMOTE);
176 } else if (IS_ERR(parent))
177 GOTO(out, rc = PTR_ERR(parent));
182 if (parent && !IS_ERR(parent))
183 mdd_object_put(env, parent);
188 * No permission check is needed.
190 * returns 1: if fid is ancestor of @mo;
191 * returns 0: if fid is not a ancestor of @mo;
193 * returns EREMOTE if remote object is found, fid of remote object is saved to
196 * returns < 0: if error
198 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
199 const struct lu_fid *fid, struct lu_fid *sfid)
201 struct mdd_device *mdd = mdo2mdd(mo);
205 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
208 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
212 } else if (rc == 1) {
213 /* found @fid is parent */
221 * Check that @dir contains no entries except (possibly) dot and dotdot.
226 * -ENOTDIR not a directory object
227 * -ENOTEMPTY not empty
231 static int mdd_dir_is_empty(const struct lu_env *env,
232 struct mdd_object *dir)
235 struct dt_object *obj;
236 const struct dt_it_ops *iops;
240 obj = mdd_object_child(dir);
241 if (!dt_try_as_dir(env, obj))
244 iops = &obj->do_index_ops->dio_it;
245 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
247 result = iops->get(env, it, (const void *)"");
250 for (result = 0, i = 0; result == 0 && i < 3; ++i)
251 result = iops->next(env, it);
254 else if (result == +1)
256 } else if (result == 0)
258 * Huh? Index contains no zero key?
265 result = PTR_ERR(it);
269 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
271 struct mdd_device *m = mdd_obj2mdd_dev(obj);
272 struct lu_attr *la = &mdd_env_info(env)->mti_la;
276 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
281 * Subdir count limitation can be broken through.
283 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
284 !S_ISDIR(la->la_mode))
291 * Check whether it may create the cobj under the pobj.
294 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
295 struct mdd_object *cobj, int check_perm, int check_nlink)
300 if (cobj && mdd_object_exists(cobj))
303 if (mdd_is_dead_obj(pobj))
307 rc = mdd_permission_internal_locked(env, pobj, NULL,
308 MAY_WRITE | MAY_EXEC,
311 if (!rc && check_nlink)
312 rc = __mdd_may_link(env, pobj);
318 * Check whether can unlink from the pobj in the case of "cobj == NULL".
320 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
321 const struct md_attr *ma)
326 if (mdd_is_dead_obj(pobj))
329 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
330 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
333 rc = mdd_permission_internal_locked(env, pobj, NULL,
334 MAY_WRITE | MAY_EXEC,
339 if (mdd_is_append(pobj))
346 * pobj == NULL is remote ops case, under such case, pobj's
347 * VTX feature has been checked already, no need check again.
349 static inline int mdd_is_sticky(const struct lu_env *env,
350 struct mdd_object *pobj,
351 struct mdd_object *cobj)
353 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
354 struct md_ucred *uc = md_ucred(env);
358 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
362 if (!(tmp_la->la_mode & S_ISVTX) ||
363 (tmp_la->la_uid == uc->mu_fsuid))
367 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
371 if (tmp_la->la_uid == uc->mu_fsuid)
374 return !mdd_capable(uc, CFS_CAP_FOWNER);
378 * Check whether it may delete the cobj from the pobj.
381 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
382 struct mdd_object *cobj, struct md_attr *ma,
383 int check_perm, int check_empty)
389 if (!mdd_object_exists(cobj))
392 if (mdd_is_dead_obj(cobj))
396 if (!mdd_object_exists(pobj))
399 if (mdd_is_dead_obj(pobj))
403 rc = mdd_permission_internal_locked(env, pobj, NULL,
404 MAY_WRITE | MAY_EXEC,
410 if (mdd_is_append(pobj))
414 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
415 mdd_is_sticky(env, pobj, cobj))
418 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
421 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
422 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
425 if (S_ISDIR(ma->ma_attr.la_mode)) {
426 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
428 if (!S_ISDIR(mdd_object_type(cobj)))
431 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
433 } else if (S_ISDIR(mdd_object_type(cobj)))
436 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
437 rc = mdd_dir_is_empty(env, cobj);
444 * has mdd_write_lock on src already, but not on tgt yet
446 int mdd_link_sanity_check(const struct lu_env *env,
447 struct mdd_object *tgt_obj,
448 const struct lu_name *lname,
449 struct mdd_object *src_obj)
451 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
455 if (!mdd_object_exists(src_obj))
458 if (mdd_is_dead_obj(src_obj))
461 /* Local ops, no lookup before link, check filename length here. */
462 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
463 RETURN(-ENAMETOOLONG);
465 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
468 if (S_ISDIR(mdd_object_type(src_obj)))
471 LASSERT(src_obj != tgt_obj);
473 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
478 rc = __mdd_may_link(env, src_obj);
483 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
484 const char *name, struct thandle *handle,
485 struct lustre_capa *capa)
487 struct dt_object *next = mdd_object_child(pobj);
491 if (dt_try_as_dir(env, next)) {
492 rc = next->do_index_ops->dio_delete(env, next,
493 (struct dt_key *)name,
501 static int __mdd_index_insert_only(const struct lu_env *env,
502 struct mdd_object *pobj,
503 const struct lu_fid *lf, const char *name,
504 struct thandle *handle,
505 struct lustre_capa *capa)
507 struct dt_object *next = mdd_object_child(pobj);
511 if (dt_try_as_dir(env, next)) {
512 struct md_ucred *uc = md_ucred(env);
514 rc = next->do_index_ops->dio_insert(env, next,
516 (const struct dt_key *)name,
517 handle, capa, uc->mu_cap &
518 CFS_CAP_SYS_RESOURCE_MASK);
525 /* insert named index, add reference if isdir */
526 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
527 const struct lu_fid *lf, const char *name, int is_dir,
528 struct thandle *handle, struct lustre_capa *capa)
533 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
534 if (rc == 0 && is_dir) {
535 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
536 mdo_ref_add(env, pobj, handle);
537 mdd_write_unlock(env, pobj);
542 /* delete named index, drop reference if isdir */
543 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
544 const char *name, int is_dir, struct thandle *handle,
545 struct lustre_capa *capa)
550 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
551 if (rc == 0 && is_dir) {
552 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
553 mdo_ref_del(env, pobj, handle);
554 mdd_write_unlock(env, pobj);
560 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
561 int reclen, struct thandle *handle)
565 /* XXX: this is a temporary solution to declare llog changes
566 * will be fixed in 2.3 with new llog implementation */
568 LASSERT(mdd->mdd_capa);
570 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
571 * have to set the parameter 'size' as INT_MAX or 0 to inform
572 * OSD that this record write is for a llog write or catalog
573 * header update, and osd declare function will reserve less
574 * credits for optimization purpose.
576 * Reserve 6 blocks for a llog write, since the llog file is
577 * usually small, reserve 2 blocks for catalog header update,
578 * because we know for sure that catalog header is already
581 * This hack should be removed in 2.3.
585 rc = dt_declare_record_write(env, mdd->mdd_capa,
586 DECLARE_LLOG_WRITE, 0, handle);
590 /* header will be updated as well */
591 rc = dt_declare_record_write(env, mdd->mdd_capa,
592 DECLARE_LLOG_WRITE, 0, handle);
596 /* also we should be able to create new plain log */
597 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
601 /* new record referencing new plain llog */
602 rc = dt_declare_record_write(env, mdd->mdd_capa,
603 DECLARE_LLOG_WRITE, 0, handle);
607 /* catalog's header will be updated as well */
608 rc = dt_declare_record_write(env, mdd->mdd_capa,
609 DECLARE_LLOG_REWRITE, 0, handle);
614 int mdd_declare_changelog_store(const struct lu_env *env,
615 struct mdd_device *mdd,
616 const struct lu_name *fname,
617 struct thandle *handle)
622 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
625 /* we'll be writing payload + llog header */
626 reclen = sizeof(struct llog_changelog_rec);
628 reclen += llog_data_len(fname->ln_namelen);
630 return mdd_declare_llog_record(env, mdd, reclen, handle);
633 /** Store a namespace change changelog record
634 * If this fails, we must fail the whole transaction; we don't
635 * want the change to commit without the log entry.
636 * \param target - mdd_object of change
637 * \param parent - parent dir/object
638 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
639 * \param tname - target name string
640 * \param handle - transacion handle
642 static int mdd_changelog_ns_store(const struct lu_env *env,
643 struct mdd_device *mdd,
644 enum changelog_rec_type type,
646 struct mdd_object *target,
647 struct mdd_object *parent,
648 const struct lu_fid *tf,
649 const struct lu_name *tname,
650 struct thandle *handle)
652 const struct lu_fid *tfid;
653 const struct lu_fid *tpfid = mdo2fid(parent);
654 struct llog_changelog_rec *rec;
661 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
663 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
666 LASSERT(parent != NULL);
667 LASSERT(tname != NULL);
668 LASSERT(handle != NULL);
671 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
672 buf = mdd_buf_alloc(env, reclen);
673 if (buf->lb_buf == NULL)
675 rec = (struct llog_changelog_rec *)buf->lb_buf;
677 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
678 rec->cr.cr_type = (__u32)type;
679 tfid = tf ? tf : mdo2fid(target);
680 rec->cr.cr_tfid = *tfid;
681 rec->cr.cr_pfid = *tpfid;
682 rec->cr.cr_namelen = tname->ln_namelen;
683 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
685 target->mod_cltime = cfs_time_current_64();
687 rc = mdd_changelog_llog_write(mdd, rec, handle);
689 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
690 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
697 static int mdd_declare_link(const struct lu_env *env,
698 struct mdd_device *mdd,
699 struct mdd_object *p,
700 struct mdd_object *c,
701 const struct lu_name *name,
702 struct thandle *handle)
706 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
710 rc = mdo_declare_ref_add(env, c, handle);
714 rc = mdo_declare_attr_set(env, p, NULL, handle);
718 rc = mdo_declare_attr_set(env, c, NULL, handle);
722 rc = mdd_declare_links_add(env, c, handle);
726 rc = mdd_declare_changelog_store(env, mdd, name, handle);
731 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
732 struct md_object *src_obj, const struct lu_name *lname,
735 const char *name = lname->ln_name;
736 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
737 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
738 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
739 struct mdd_device *mdd = mdo2mdd(src_obj);
740 struct dynlock_handle *dlh;
741 struct thandle *handle;
742 #ifdef HAVE_QUOTA_SUPPORT
743 struct obd_device *obd = mdd->mdd_obd_dev;
744 struct obd_export *exp = md_quota(env)->mq_exp;
745 struct mds_obd *mds = &obd->u.mds;
746 unsigned int qids[MAXQUOTAS] = { 0, 0 };
747 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
752 #ifdef HAVE_QUOTA_SUPPORT
753 if (mds->mds_quota) {
754 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
756 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
759 mdd_data_get(env, mdd_tobj, &data);
760 quota_opc = FSFILT_OP_LINK;
761 mdd_quota_wrapper(la_tmp, qids);
762 /* get block quota for parent */
763 lquota_chkquota(mds_quota_interface_ref, obd, exp,
764 qids, rec_pending, 1, NULL,
765 LQUOTA_FLAGS_BLK, data, 1);
770 handle = mdd_trans_create(env, mdd);
772 GOTO(out_pending, rc = PTR_ERR(handle));
774 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
778 rc = mdd_trans_start(env, mdd, handle);
782 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
784 GOTO(out_trans, rc = -ENOMEM);
785 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
787 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
789 GOTO(out_unlock, rc);
791 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
793 mdd_object_capa(env, mdd_tobj));
795 GOTO(out_unlock, rc);
797 mdo_ref_add(env, mdd_sobj, handle);
799 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
800 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
802 la->la_valid = LA_CTIME | LA_MTIME;
803 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
805 GOTO(out_unlock, rc);
807 la->la_valid = LA_CTIME;
808 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
810 mdd_links_add(env, mdd_sobj,
811 mdo2fid(mdd_tobj), lname, handle, 0);
816 mdd_write_unlock(env, mdd_sobj);
817 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
820 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
821 mdd_tobj, NULL, lname, handle);
823 mdd_trans_stop(env, mdd, rc, handle);
825 #ifdef HAVE_QUOTA_SUPPORT
827 lquota_pending_commit(mds_quota_interface_ref, obd,
828 qids, rec_pending, 1);
829 /* Trigger dqacq for the parent owner. If failed,
830 * the next call for lquota_chkquota will process it. */
831 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
838 int mdd_declare_finish_unlink(const struct lu_env *env,
839 struct mdd_object *obj,
841 struct thandle *handle)
845 rc = orph_declare_index_insert(env, obj, handle);
849 return mdd_declare_object_kill(env, obj, ma, handle);
852 /* caller should take a lock before calling */
853 int mdd_finish_unlink(const struct lu_env *env,
854 struct mdd_object *obj, struct md_attr *ma,
859 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
862 LASSERT(mdd_write_locked(env, obj) != 0);
864 /* read HSM flags, needed to set changelogs flags */
865 ma->ma_need = MA_HSM | MA_INODE;
866 rc = mdd_attr_get_internal(env, obj, ma);
867 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
868 obj->mod_flags |= DEAD_OBJ;
869 /* add new orphan and the object
870 * will be deleted during mdd_close() */
871 if (obj->mod_count) {
872 rc = __mdd_orphan_add(env, obj, th);
874 CDEBUG(D_HA, "Object "DFID" is inserted into "
875 "orphan list, open count = %d\n",
876 PFID(mdd_object_fid(obj)),
879 CERROR("Object "DFID" fail to be an orphan, "
880 "open count = %d, maybe cause failed "
882 PFID(mdd_object_fid(obj)),
885 rc = mdd_object_kill(env, obj, ma, th);
890 /* get the i_nlink */
891 ma->ma_need = MA_INODE;
892 rc = mdd_attr_get_internal(env, obj, ma);
895 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
902 * has mdd_write_lock on cobj already, but not on pobj yet
904 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
905 struct mdd_object *cobj, struct md_attr *ma)
910 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
915 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
916 struct mdd_object *p, struct mdd_object *c,
917 const struct lu_name *name, struct md_attr *ma,
918 struct thandle *handle)
922 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
926 rc = mdo_declare_ref_del(env, p, handle);
930 rc = mdo_declare_ref_del(env, c, handle);
934 rc = mdo_declare_ref_del(env, c, handle);
938 rc = mdo_declare_attr_set(env, p, NULL, handle);
942 rc = mdo_declare_attr_set(env, c, NULL, handle);
946 rc = mdd_declare_finish_unlink(env, c, ma, handle);
950 rc = mdd_declare_links_add(env, c, handle);
954 rc = mdd_declare_changelog_store(env, mdd, name, handle);
959 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
960 struct md_object *cobj, const struct lu_name *lname,
963 const char *name = lname->ln_name;
964 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
965 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
966 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
967 struct mdd_device *mdd = mdo2mdd(pobj);
968 struct dynlock_handle *dlh;
969 struct thandle *handle;
970 #ifdef HAVE_QUOTA_SUPPORT
971 struct obd_device *obd = mdd->mdd_obd_dev;
972 struct mds_obd *mds = &obd->u.mds;
973 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
974 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
977 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
981 if (mdd_object_exists(mdd_cobj) <= 0)
984 handle = mdd_trans_create(env, mdd);
986 RETURN(PTR_ERR(handle));
988 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
993 rc = mdd_trans_start(env, mdd, handle);
997 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
999 GOTO(out_trans, rc = -ENOMEM);
1000 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1002 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1006 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1007 mdd_object_capa(env, mdd_pobj));
1011 mdo_ref_del(env, mdd_cobj, handle);
1014 mdo_ref_del(env, mdd_cobj, handle);
1016 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1017 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1019 la->la_valid = LA_CTIME | LA_MTIME;
1020 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1024 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1025 /* update ctime of an unlinked file only if it is still
1026 * opened or a link still exists */
1027 la->la_valid = LA_CTIME;
1028 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1033 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1034 #ifdef HAVE_QUOTA_SUPPORT
1035 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1036 ma->ma_attr.la_nlink == 0) {
1037 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1039 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1041 mdd_quota_wrapper(la_tmp, qpids);
1042 if (mdd_cobj->mod_count == 0) {
1043 quota_opc = FSFILT_OP_UNLINK;
1044 mdd_quota_wrapper(&ma->ma_attr, qcids);
1046 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1052 /* old files may not have link ea; ignore errors */
1053 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1054 lname, NULL, NULL, handle);
1058 mdd_write_unlock(env, mdd_cobj);
1059 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1064 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1065 if ((ma->ma_valid & MA_HSM) &&
1066 (ma->ma_hsm.mh_flags & HS_EXISTS))
1067 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1069 rc = mdd_changelog_ns_store(env, mdd,
1070 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1071 mdd_cobj, mdd_pobj, NULL, lname, handle);
1075 mdd_trans_stop(env, mdd, rc, handle);
1076 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
1077 if (rc == 0 && ma->ma_valid & MA_COOKIE && ma->ma_valid & MA_LOV &&
1078 ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_UNLINK_DESTROY)
1079 /* Since echo client is incapable of destorying ost object,
1080 * it will destory the object here. */
1081 rc = mdd_lovobj_unlink(env, mdd, mdd_cobj, la, ma, 1);
1083 #warning "please remove this after 2.4 (LOD/OSP)."
1086 #ifdef HAVE_QUOTA_SUPPORT
1088 /* Trigger dqrel on the owner of child and parent. If failed,
1089 * the next call for lquota_chkquota will process it. */
1090 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1096 /* has not lock on pobj yet */
1097 static int mdd_ni_sanity_check(const struct lu_env *env,
1098 struct md_object *pobj,
1099 const struct md_attr *ma)
1101 struct mdd_object *obj = md2mdd_obj(pobj);
1105 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1108 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1114 * Partial operation.
1116 static int mdd_name_insert(const struct lu_env *env,
1117 struct md_object *pobj,
1118 const struct lu_name *lname,
1119 const struct lu_fid *fid,
1120 const struct md_attr *ma)
1122 const char *name = lname->ln_name;
1123 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1124 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1125 struct dynlock_handle *dlh;
1126 struct thandle *handle;
1127 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1128 #ifdef HAVE_QUOTA_SUPPORT
1129 struct mdd_device *mdd = mdo2mdd(pobj);
1130 struct md_ucred *uc = md_ucred(env);
1131 struct obd_device *obd = mdd->mdd_obd_dev;
1132 struct obd_export *exp = md_quota(env)->mq_exp;
1133 struct mds_obd *mds = &obd->u.mds;
1134 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1135 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1136 cfs_cap_t save = uc->mu_cap;
1141 /* XXX: this code won't be used ever:
1142 * DNE uses slightly different approach */
1145 #ifdef HAVE_QUOTA_SUPPORT
1146 if (mds->mds_quota) {
1147 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1148 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1150 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1153 mdd_data_get(env, mdd_obj, &data);
1154 quota_opc = FSFILT_OP_LINK;
1155 mdd_quota_wrapper(la_tmp, qids);
1156 /* get block quota for parent */
1157 lquota_chkquota(mds_quota_interface_ref, obd,
1158 exp, qids, rec_pending, 1, NULL,
1159 LQUOTA_FLAGS_BLK, data, 1);
1162 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1166 handle = mdd_trans_create(env, mdo2mdd(pobj));
1168 GOTO(out_pending, rc = PTR_ERR(handle));
1170 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1172 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1174 GOTO(out_trans, rc = -ENOMEM);
1176 rc = mdd_ni_sanity_check(env, pobj, ma);
1178 GOTO(out_unlock, rc);
1180 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1181 handle, BYPASS_CAPA);
1183 GOTO(out_unlock, rc);
1186 * For some case, no need update obj's ctime (LA_CTIME is not set),
1188 * For other cases, update obj's ctime (LA_CTIME is set),
1191 if (ma->ma_attr.la_valid & LA_CTIME) {
1192 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1193 la->la_valid = LA_CTIME | LA_MTIME;
1194 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1199 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1201 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1203 #ifdef HAVE_QUOTA_SUPPORT
1204 if (mds->mds_quota) {
1206 lquota_pending_commit(mds_quota_interface_ref,
1207 obd, qids, rec_pending, 1);
1208 /* Trigger dqacq for the parent owner. If failed,
1209 * the next call for lquota_chkquota will process it*/
1210 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1220 /* has not lock on pobj yet */
1221 static int mdd_nr_sanity_check(const struct lu_env *env,
1222 struct md_object *pobj,
1223 const struct md_attr *ma)
1225 struct mdd_object *obj = md2mdd_obj(pobj);
1229 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1232 rc = mdd_may_unlink(env, obj, ma);
1238 * Partial operation.
1240 static int mdd_name_remove(const struct lu_env *env,
1241 struct md_object *pobj,
1242 const struct lu_name *lname,
1243 const struct md_attr *ma)
1245 const char *name = lname->ln_name;
1246 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1247 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1248 struct mdd_device *mdd = mdo2mdd(pobj);
1249 struct dynlock_handle *dlh;
1250 struct thandle *handle;
1251 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1252 #ifdef HAVE_QUOTA_SUPPORT
1253 struct obd_device *obd = mdd->mdd_obd_dev;
1254 struct mds_obd *mds = &obd->u.mds;
1255 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1261 /* XXX: this code won't be used ever:
1262 * DNE uses slightly different approach */
1265 #ifdef HAVE_QUOTA_SUPPORT
1266 if (mds->mds_quota) {
1267 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1269 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1271 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1272 mdd_quota_wrapper(la_tmp, qids);
1276 handle = mdd_trans_create(env, mdd);
1278 GOTO(out_pending, rc = PTR_ERR(handle));
1280 rc = mdd_trans_start(env, mdd, handle);
1282 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1284 GOTO(out_trans, rc = -ENOMEM);
1286 rc = mdd_nr_sanity_check(env, pobj, ma);
1288 GOTO(out_unlock, rc);
1290 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1291 handle, BYPASS_CAPA);
1293 GOTO(out_unlock, rc);
1296 * For some case, no need update obj's ctime (LA_CTIME is not set),
1298 * For other cases, update obj's ctime (LA_CTIME is set),
1301 if (ma->ma_attr.la_valid & LA_CTIME) {
1302 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1303 la->la_valid = LA_CTIME | LA_MTIME;
1304 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1309 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1311 mdd_trans_stop(env, mdd, rc, handle);
1313 #ifdef HAVE_QUOTA_SUPPORT
1314 /* Trigger dqrel for the parent owner.
1315 * If failed, the next call for lquota_chkquota will process it. */
1317 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1325 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1327 static int mdd_rt_sanity_check(const struct lu_env *env,
1328 struct mdd_object *tgt_pobj,
1329 struct mdd_object *tobj,
1335 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1338 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1339 * exist. In fact, tobj must exist, otherwise the call trace will be:
1340 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1341 * When get here, tobj must be NOT NULL, the other case has been
1342 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1344 * So check may_delete, but not check nlink of tgt_pobj. */
1346 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1351 /* Partial rename op on slave MDD */
1352 static int mdd_rename_tgt(const struct lu_env *env,
1353 struct md_object *pobj, struct md_object *tobj,
1354 const struct lu_fid *lf, const struct lu_name *lname,
1357 const char *name = lname->ln_name;
1358 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1359 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1360 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1361 struct mdd_device *mdd = mdo2mdd(pobj);
1362 struct dynlock_handle *dlh;
1363 struct thandle *handle;
1364 #ifdef HAVE_QUOTA_SUPPORT
1365 struct obd_device *obd = mdd->mdd_obd_dev;
1366 struct obd_export *exp = md_quota(env)->mq_exp;
1367 struct mds_obd *mds = &obd->u.mds;
1368 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1369 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1370 int quota_copc = 0, quota_popc = 0;
1371 int rec_pending[MAXQUOTAS] = { 0, 0 };
1376 /* XXX: this code won't be used ever:
1377 * DNE uses slightly different approach */
1380 #ifdef HAVE_QUOTA_SUPPORT
1381 if (mds->mds_quota && !tobj) {
1382 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1384 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1387 mdd_data_get(env, mdd_tpobj, &data);
1388 quota_popc = FSFILT_OP_LINK;
1389 mdd_quota_wrapper(la_tmp, qpids);
1390 /* get block quota for target parent */
1391 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1392 qpids, rec_pending, 1, NULL,
1393 LQUOTA_FLAGS_BLK, data, 1);
1397 handle = mdd_trans_create(env, mdd);
1399 GOTO(out_pending, rc = PTR_ERR(handle));
1401 rc = mdd_trans_start(env, mdd, handle);
1403 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1405 GOTO(out_trans, rc = -ENOMEM);
1407 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1409 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1414 * If rename_tgt is called then we should just re-insert name with
1415 * correct fid, no need to dec/inc parent nlink if obj is dir.
1417 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1421 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1426 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1427 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1429 la->la_valid = LA_CTIME | LA_MTIME;
1430 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1435 * For tobj is remote case cmm layer has processed
1436 * and pass NULL tobj to here. So when tobj is NOT NULL,
1437 * it must be local one.
1439 if (tobj && mdd_object_exists(mdd_tobj)) {
1440 mdo_ref_del(env, mdd_tobj, handle);
1442 /* Remove dot reference. */
1443 if (S_ISDIR(ma->ma_attr.la_mode))
1444 mdo_ref_del(env, mdd_tobj, handle);
1446 la->la_valid = LA_CTIME;
1447 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1451 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1455 #ifdef HAVE_QUOTA_SUPPORT
1456 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1457 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1458 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1459 mdd_quota_wrapper(&ma->ma_attr, qcids);
1466 mdd_write_unlock(env, mdd_tobj);
1467 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1470 /* Bare EXT record with no RENAME in front of it signifies
1471 a partial slave op */
1472 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1473 mdd_tpobj, NULL, lname, handle);
1475 mdd_trans_stop(env, mdd, rc, handle);
1477 #ifdef HAVE_QUOTA_SUPPORT
1478 if (mds->mds_quota) {
1480 lquota_pending_commit(mds_quota_interface_ref, obd,
1481 qpids, rec_pending, 1);
1484 /* Trigger dqrel on the target owner of child.
1485 * If failed, the next call for lquota_chkquota
1486 * will process it. */
1487 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1495 * The permission has been checked when obj created, no need check again.
1497 static int mdd_cd_sanity_check(const struct lu_env *env,
1498 struct mdd_object *obj)
1503 if (!obj || mdd_is_dead_obj(obj))
1510 static int mdd_declare_create_data(const struct lu_env *env,
1511 struct mdd_device *mdd,
1512 struct mdd_object *obj,
1514 struct thandle *handle)
1516 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1520 buf->lb_len = lmm_size;
1521 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1526 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1531 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1532 struct md_object *cobj, const struct md_op_spec *spec,
1535 struct mdd_device *mdd = mdo2mdd(cobj);
1536 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1537 struct mdd_object *son = md2mdd_obj(cobj);
1538 struct lov_mds_md *lmm = NULL;
1540 struct thandle *handle;
1544 rc = mdd_cd_sanity_check(env, son);
1548 if (!md_should_create(spec->sp_cr_flags))
1550 lmm_size = ma->ma_lmm_size;
1552 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1556 handle = mdd_trans_create(env, mdd);
1558 GOTO(out_free, rc = PTR_ERR(handle));
1560 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1564 rc = mdd_trans_start(env, mdd, handle);
1569 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1570 * Should this be fixed?
1573 /* Replay creates has objects already */
1575 if (spec->no_create) {
1576 CDEBUG(D_INFO, "we already have lov ea\n");
1577 rc = mdd_lov_set_md(env, mdd_pobj, son,
1578 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1579 spec->u.sp_ea.eadatalen, handle, 0);
1582 /* No need mdd_lsm_sanity_check here */
1583 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1584 lmm_size, handle, 0);
1587 rc = mdd_attr_get_internal_locked(env, son, ma);
1589 /* update lov_objid data, must be before transaction stop! */
1591 mdd_lov_objid_update(mdd, lmm);
1594 mdd_trans_stop(env, mdd, rc, handle);
1596 /* Finish mdd_lov_create() stuff. */
1597 /* if no_create == 0 (not replay), we free lmm allocated by
1598 * mdd_lov_create() */
1599 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1603 /* Get fid from name and parent */
1605 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1606 const struct lu_name *lname, struct lu_fid* fid, int mask)
1608 const char *name = lname->ln_name;
1609 const struct dt_key *key = (const struct dt_key *)name;
1610 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1611 struct mdd_device *m = mdo2mdd(pobj);
1612 struct dt_object *dir = mdd_object_child(mdd_obj);
1616 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1619 rc = mdd_object_exists(mdd_obj);
1620 if (unlikely(rc == 0))
1622 else if (unlikely(rc < 0)) {
1623 CERROR("Object "DFID" locates on remote server\n",
1624 PFID(mdo2fid(mdd_obj)));
1628 /* The common filename length check. */
1629 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1630 RETURN(-ENAMETOOLONG);
1632 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1637 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1638 dt_try_as_dir(env, dir))) {
1640 rc = dir->do_index_ops->dio_lookup(env, dir,
1641 (struct dt_rec *)fid, key,
1642 mdd_object_capa(env, mdd_obj));
1653 int mdd_declare_object_initialize(const struct lu_env *env,
1654 struct mdd_object *child,
1656 struct thandle *handle)
1660 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1661 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1662 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1665 rc = mdo_declare_ref_add(env, child, handle);
1668 mdd_declare_links_add(env, child, handle);
1673 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1674 const struct lu_name *lname, struct mdd_object *child,
1675 struct md_attr *ma, struct thandle *handle,
1676 const struct md_op_spec *spec)
1682 * Update attributes for child.
1685 * (1) the valid bits should be converted between Lustre and Linux;
1686 * (2) maybe, the child attributes should be set in OSD when creation.
1689 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1693 if (S_ISDIR(ma->ma_attr.la_mode)) {
1694 /* Add "." and ".." for newly created dir */
1695 mdo_ref_add(env, child, handle);
1696 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1697 dot, handle, BYPASS_CAPA);
1699 rc = __mdd_index_insert_only(env, child, pfid,
1703 mdo_ref_del(env, child, handle);
1706 mdd_links_add(env, child, pfid, lname, handle, 1);
1711 /* has not lock on pobj yet */
1712 static int mdd_create_sanity_check(const struct lu_env *env,
1713 struct md_object *pobj,
1714 const struct lu_name *lname,
1716 struct md_op_spec *spec)
1718 struct mdd_thread_info *info = mdd_env_info(env);
1719 struct lu_attr *la = &info->mti_la;
1720 struct lu_fid *fid = &info->mti_fid;
1721 struct mdd_object *obj = md2mdd_obj(pobj);
1722 struct mdd_device *m = mdo2mdd(pobj);
1723 int lookup = spec->sp_cr_lookup;
1728 if (mdd_is_dead_obj(obj))
1732 * In some cases this lookup is not needed - we know before if name
1733 * exists or not because MDT performs lookup for it.
1734 * name length check is done in lookup.
1738 * Check if the name already exist, though it will be checked in
1739 * _index_insert also, for avoiding rolling back if exists
1742 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1743 MAY_WRITE | MAY_EXEC);
1745 RETURN(rc ? : -EEXIST);
1748 * Check WRITE permission for the parent.
1749 * EXEC permission have been checked
1750 * when lookup before create already.
1752 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1759 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1763 if (la->la_mode & S_ISGID) {
1764 ma->ma_attr.la_gid = la->la_gid;
1765 if (S_ISDIR(ma->ma_attr.la_mode)) {
1766 ma->ma_attr.la_mode |= S_ISGID;
1767 ma->ma_attr.la_valid |= LA_MODE;
1771 switch (ma->ma_attr.la_mode & S_IFMT) {
1773 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1775 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1776 RETURN(-ENAMETOOLONG);
1795 static int mdd_declare_create(const struct lu_env *env,
1796 struct mdd_device *mdd,
1797 struct mdd_object *p,
1798 struct mdd_object *c,
1799 const struct lu_name *name,
1802 struct thandle *handle,
1803 const struct md_op_spec *spec)
1805 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1808 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1812 /* if dir, then can inherit default ACl */
1814 buf->lb_len = lmm_size;
1815 if (S_ISDIR(ma->ma_attr.la_mode)) {
1816 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1819 rc = mdo_declare_ref_add(env, p, handle);
1824 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1829 rc = mdd_declare_object_initialize(env, c, ma, handle);
1833 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1834 name->ln_name, handle);
1838 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1843 if (S_ISLNK(ma->ma_attr.la_mode)) {
1844 rc = dt_declare_record_write(env, mdd_object_child(c),
1845 strlen(spec->u.sp_symname), 0,
1850 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1854 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1858 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1866 * Create object and insert it into namespace.
1868 static int mdd_create(const struct lu_env *env,
1869 struct md_object *pobj,
1870 const struct lu_name *lname,
1871 struct md_object *child,
1872 struct md_op_spec *spec,
1875 struct mdd_thread_info *info = mdd_env_info(env);
1876 struct lu_attr *la = &info->mti_la_for_fix;
1877 struct md_attr *ma_acl = &info->mti_ma;
1878 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1879 struct mdd_object *son = md2mdd_obj(child);
1880 struct mdd_device *mdd = mdo2mdd(pobj);
1881 struct lu_attr *attr = &ma->ma_attr;
1882 struct lov_mds_md *lmm = NULL;
1883 struct thandle *handle;
1884 struct dynlock_handle *dlh;
1885 const char *name = lname->ln_name;
1886 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1887 int got_def_acl = 0;
1888 #ifdef HAVE_QUOTA_SUPPORT
1889 struct obd_device *obd = mdd->mdd_obd_dev;
1890 struct obd_export *exp = md_quota(env)->mq_exp;
1891 struct mds_obd *mds = &obd->u.mds;
1892 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1893 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1894 int quota_opc = 0, block_count = 0;
1895 int inode_pending[MAXQUOTAS] = { 0, 0 };
1896 int block_pending[MAXQUOTAS] = { 0, 0 };
1897 int parent_pending[MAXQUOTAS] = { 0, 0 };
1902 * Two operations have to be performed:
1904 * - an allocation of a new object (->do_create()), and
1906 * - an insertion into a parent index (->dio_insert()).
1908 * Due to locking, operation order is not important, when both are
1909 * successful, *but* error handling cases are quite different:
1911 * - if insertion is done first, and following object creation fails,
1912 * insertion has to be rolled back, but this operation might fail
1913 * also leaving us with dangling index entry.
1915 * - if creation is done first, is has to be undone if insertion
1916 * fails, leaving us with leaked space, which is neither good, nor
1919 * It seems that creation-first is simplest solution, but it is
1920 * sub-optimal in the frequent
1925 * case, because second mkdir is bound to create object, only to
1926 * destroy it immediately.
1928 * To avoid this follow local file systems that do double lookup:
1930 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1932 * 1. create (mdd_object_create_internal())
1934 * 2. insert (__mdd_index_insert(), lookup again)
1937 /* Sanity checks before big job. */
1938 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1942 #ifdef HAVE_QUOTA_SUPPORT
1943 if (mds->mds_quota) {
1944 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1946 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1950 quota_opc = FSFILT_OP_CREATE;
1951 mdd_quota_wrapper(&ma->ma_attr, qcids);
1952 mdd_quota_wrapper(la_tmp, qpids);
1953 /* get file quota for child */
1954 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1955 qcids, inode_pending, 1, NULL, 0, NULL,
1957 switch (ma->ma_attr.la_mode & S_IFMT) {
1966 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1967 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1971 /* get block quota for child and parent */
1973 lquota_chkquota(mds_quota_interface_ref, obd,
1974 exp, qcids, block_pending,
1976 LQUOTA_FLAGS_BLK, NULL, 0);
1978 lquota_chkquota(mds_quota_interface_ref, obd,
1979 exp, qpids, parent_pending, 1,
1980 NULL, LQUOTA_FLAGS_BLK, NULL,
1986 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1987 GOTO(out_pending, rc = -EINPROGRESS);
1990 * No RPC inside the transaction, so OST objects should be created at
1993 if (S_ISREG(attr->la_mode)) {
1994 lmm_size = ma->ma_lmm_size;
1995 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1998 GOTO(out_pending, rc);
2001 if (!S_ISLNK(attr->la_mode)) {
2002 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2003 ma_acl->ma_acl = info->mti_xattr_buf;
2004 ma_acl->ma_need = MA_ACL_DEF;
2005 ma_acl->ma_valid = 0;
2007 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2008 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2009 mdd_read_unlock(env, mdd_pobj);
2012 else if (ma_acl->ma_valid & MA_ACL_DEF)
2016 handle = mdd_trans_create(env, mdd);
2018 GOTO(out_free, rc = PTR_ERR(handle));
2020 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2021 lmm_size, handle, spec);
2025 rc = mdd_trans_start(env, mdd, handle);
2029 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2031 GOTO(out_trans, rc = -ENOMEM);
2033 mdd_write_lock(env, son, MOR_TGT_CHILD);
2034 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2036 mdd_write_unlock(env, son);
2042 #ifdef CONFIG_FS_POSIX_ACL
2044 struct lu_buf *acl_buf = &info->mti_buf;
2045 acl_buf->lb_buf = ma_acl->ma_acl;
2046 acl_buf->lb_len = ma_acl->ma_acl_size;
2048 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2050 mdd_write_unlock(env, son);
2053 ma->ma_attr.la_valid |= LA_MODE;
2058 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2059 son, ma, handle, spec);
2060 mdd_write_unlock(env, son);
2063 * Object has no links, so it will be destroyed when last
2064 * reference is released. (XXX not now.)
2070 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2071 name, S_ISDIR(attr->la_mode), handle,
2072 mdd_object_capa(env, mdd_pobj));
2079 /* No need mdd_lsm_sanity_check here */
2080 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2082 CERROR("error on stripe info copy %d \n", rc);
2085 if (lmm && lmm_size > 0) {
2086 /* Set Lov here, do not get lmm again later */
2087 if (lmm_size > ma->ma_lmm_size) {
2088 /* Reply buffer is smaller, need bigger one */
2089 mdd_max_lmm_buffer(env, lmm_size);
2090 if (unlikely(info->mti_max_lmm == NULL))
2091 GOTO(cleanup, rc = -ENOMEM);
2092 ma->ma_lmm = info->mti_max_lmm;
2093 ma->ma_big_lmm_used = 1;
2095 memcpy(ma->ma_lmm, lmm, lmm_size);
2096 ma->ma_lmm_size = lmm_size;
2097 ma->ma_valid |= MA_LOV;
2100 if (S_ISLNK(attr->la_mode)) {
2101 struct md_ucred *uc = md_ucred(env);
2102 struct dt_object *dt = mdd_object_child(son);
2103 const char *target_name = spec->u.sp_symname;
2104 int sym_len = strlen(target_name);
2105 const struct lu_buf *buf;
2108 buf = mdd_buf_get_const(env, target_name, sym_len);
2109 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2110 mdd_object_capa(env, son),
2112 CFS_CAP_SYS_RESOURCE_MASK);
2117 GOTO(cleanup, rc = -EFAULT);
2121 la->la_valid = LA_CTIME | LA_MTIME;
2122 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2126 /* Return attr back. */
2127 rc = mdd_attr_get_internal_locked(env, son, ma);
2130 if (rc && created) {
2134 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2135 S_ISDIR(attr->la_mode),
2136 handle, BYPASS_CAPA);
2138 CERROR("error can not cleanup destroy %d\n",
2143 mdd_write_lock(env, son, MOR_TGT_CHILD);
2144 mdo_ref_del(env, son, handle);
2145 if (initialized && S_ISDIR(attr->la_mode))
2146 mdo_ref_del(env, son, handle);
2147 mdd_write_unlock(env, son);
2151 /* update lov_objid data, must be before transaction stop! */
2153 mdd_lov_objid_update(mdd, lmm);
2155 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2158 rc = mdd_changelog_ns_store(env, mdd,
2159 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2160 S_ISREG(attr->la_mode) ? CL_CREATE :
2161 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2162 0, son, mdd_pobj, NULL, lname, handle);
2164 mdd_trans_stop(env, mdd, rc, handle);
2166 /* finish lov_create stuff, free all temporary data */
2167 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2169 #ifdef HAVE_QUOTA_SUPPORT
2171 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2173 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2175 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2177 /* Trigger dqacq on the owner of child and parent. If failed,
2178 * the next call for lquota_chkquota will process it. */
2179 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2183 /* The child object shouldn't be cached anymore */
2185 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
2186 &child->mo_lu.lo_header->loh_flags);
2191 * Get locks on parents in proper order
2192 * RETURN: < 0 - error, rename_order if successful
2200 static int mdd_rename_order(const struct lu_env *env,
2201 struct mdd_device *mdd,
2202 struct mdd_object *src_pobj,
2203 struct mdd_object *tgt_pobj)
2205 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2209 if (src_pobj == tgt_pobj)
2210 RETURN(MDD_RN_SAME);
2212 /* compared the parent child relationship of src_p&tgt_p */
2213 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2215 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2218 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2231 /* has not mdd_write{read}_lock on any obj yet. */
2232 static int mdd_rename_sanity_check(const struct lu_env *env,
2233 struct mdd_object *src_pobj,
2234 struct mdd_object *tgt_pobj,
2235 struct mdd_object *sobj,
2236 struct mdd_object *tobj,
2242 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2245 /* XXX: when get here, sobj must NOT be NULL,
2246 * the other case has been processed in cml_rename
2247 * before mdd_rename and enable MDS_PERM_BYPASS. */
2250 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2254 /* XXX: when get here, "tobj == NULL" means tobj must
2255 * NOT exist (neither on remote MDS, such case has been
2256 * processed in cml_rename before mdd_rename and enable
2258 * So check may_create, but not check may_unlink. */
2260 rc = mdd_may_create(env, tgt_pobj, NULL,
2261 (src_pobj != tgt_pobj), 0);
2263 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2264 (src_pobj != tgt_pobj), 1);
2266 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2267 S_ISDIR(ma->ma_attr.la_mode))
2268 rc = __mdd_may_link(env, tgt_pobj);
2273 static int mdd_declare_rename(const struct lu_env *env,
2274 struct mdd_device *mdd,
2275 struct mdd_object *mdd_spobj,
2276 struct mdd_object *mdd_tpobj,
2277 struct mdd_object *mdd_sobj,
2278 struct mdd_object *mdd_tobj,
2279 const struct lu_name *sname,
2280 const struct lu_name *tname,
2282 struct thandle *handle)
2290 /* name from source dir */
2291 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2295 /* .. from source child */
2296 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2297 /* source child can be directory,
2298 * counted by source dir's nlink */
2299 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2303 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2307 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2312 /* new target child can be directory,
2313 * counted by target dir's nlink */
2314 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2320 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2324 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2327 mdd_declare_links_add(env, mdd_sobj, handle);
2331 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2336 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2337 tname->ln_name, handle);
2341 /* name from target dir (old name), we declare it unconditionally
2342 * as mdd_rename() calls delete unconditionally as well. so just
2343 * to balance declarations vs calls to change ... */
2344 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2348 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2349 /* delete target child in target parent directory */
2350 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2354 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2355 /* target child can be directory,
2356 * delete "." reference in target child directory */
2357 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2361 /* delete ".." reference in target parent directory */
2362 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2367 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2371 mdd_declare_links_add(env, mdd_tobj, handle);
2375 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2380 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2384 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2391 /* src object can be remote that is why we use only fid and type of object */
2392 static int mdd_rename(const struct lu_env *env,
2393 struct md_object *src_pobj, struct md_object *tgt_pobj,
2394 const struct lu_fid *lf, const struct lu_name *lsname,
2395 struct md_object *tobj, const struct lu_name *ltname,
2398 const char *sname = lsname->ln_name;
2399 const char *tname = ltname->ln_name;
2400 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2401 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2402 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2403 struct mdd_device *mdd = mdo2mdd(src_pobj);
2404 struct mdd_object *mdd_sobj = NULL; /* source object */
2405 struct mdd_object *mdd_tobj = NULL;
2406 struct dynlock_handle *sdlh, *tdlh;
2407 struct thandle *handle;
2408 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2409 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2413 #ifdef HAVE_QUOTA_SUPPORT
2414 struct obd_device *obd = mdd->mdd_obd_dev;
2415 struct obd_export *exp = md_quota(env)->mq_exp;
2416 struct mds_obd *mds = &obd->u.mds;
2417 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2418 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2419 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2420 int quota_copc = 0, quota_popc = 0;
2421 int rec_pending[MAXQUOTAS] = { 0, 0 };
2425 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2426 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2429 mdd_tobj = md2mdd_obj(tobj);
2431 #ifdef HAVE_QUOTA_SUPPORT
2432 if (mds->mds_quota) {
2433 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2435 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2437 mdd_quota_wrapper(la_tmp, qspids);
2439 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2443 mdd_data_get(env, mdd_tpobj, &data);
2444 quota_popc = FSFILT_OP_LINK;
2445 mdd_quota_wrapper(la_tmp, qtpids);
2446 /* get block quota for target parent */
2447 lquota_chkquota(mds_quota_interface_ref,
2449 rec_pending, 1, NULL,
2457 mdd_sobj = mdd_object_find(env, mdd, lf);
2459 handle = mdd_trans_create(env, mdd);
2461 GOTO(out_pending, rc = PTR_ERR(handle));
2463 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2464 mdd_tobj, lsname, ltname, ma, handle);
2468 rc = mdd_trans_start(env, mdd, handle);
2472 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2473 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2475 GOTO(cleanup_unlocked, rc);
2477 /* Get locks in determined order */
2478 if (rc == MDD_RN_SAME) {
2479 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2480 sname, MOR_SRC_PARENT);
2481 /* check hashes to determine do we need one lock or two */
2482 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2483 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2487 } else if (rc == MDD_RN_SRCTGT) {
2488 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2489 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2491 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2492 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2494 if (sdlh == NULL || tdlh == NULL)
2495 GOTO(cleanup, rc = -ENOMEM);
2497 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2498 mdd_sobj, mdd_tobj, ma);
2502 /* Remove source name from source directory */
2503 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2504 mdd_object_capa(env, mdd_spobj));
2508 /* "mv dir1 dir2" needs "dir1/.." link update */
2509 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2510 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2511 mdd_object_capa(env, mdd_sobj));
2513 GOTO(fixup_spobj2, rc);
2515 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2516 handle, mdd_object_capa(env, mdd_sobj));
2518 GOTO(fixup_spobj, rc);
2521 /* Remove target name from target directory
2522 * Here tobj can be remote one, so we do index_delete unconditionally
2523 * and -ENOENT is allowed.
2525 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2526 mdd_object_capa(env, mdd_tpobj));
2529 /* tname might been renamed to something else */
2530 GOTO(fixup_spobj, rc);
2533 GOTO(fixup_spobj, rc);
2536 /* Insert new fid with target name into target dir */
2537 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2538 mdd_object_capa(env, mdd_tpobj));
2540 GOTO(fixup_tpobj, rc);
2542 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2543 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2545 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2547 la->la_valid = LA_CTIME;
2548 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2551 GOTO(fixup_tpobj, rc);
2554 /* Remove old target object
2555 * For tobj is remote case cmm layer has processed
2556 * and set tobj to NULL then. So when tobj is NOT NULL,
2557 * it must be local one.
2559 if (tobj && mdd_object_exists(mdd_tobj)) {
2560 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2561 if (mdd_is_dead_obj(mdd_tobj)) {
2562 mdd_write_unlock(env, mdd_tobj);
2563 /* shld not be dead, something is wrong */
2564 CERROR("tobj is dead, something is wrong\n");
2568 mdo_ref_del(env, mdd_tobj, handle);
2570 /* Remove dot reference. */
2572 mdo_ref_del(env, mdd_tobj, handle);
2574 la->la_valid = LA_CTIME;
2575 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2577 GOTO(fixup_tpobj, rc);
2579 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2580 mdd_write_unlock(env, mdd_tobj);
2582 GOTO(fixup_tpobj, rc);
2584 #ifdef HAVE_QUOTA_SUPPORT
2585 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2586 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2587 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2588 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2593 la->la_valid = LA_CTIME | LA_MTIME;
2594 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2596 GOTO(fixup_tpobj, rc);
2598 if (mdd_spobj != mdd_tpobj) {
2599 la->la_valid = LA_CTIME | LA_MTIME;
2600 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2604 if (rc == 0 && mdd_sobj) {
2605 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2606 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2607 mdo2fid(mdd_tpobj), ltname, handle);
2609 /* Old files might not have EA entry */
2610 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2612 mdd_write_unlock(env, mdd_sobj);
2613 /* We don't fail the transaction if the link ea can't be
2614 updated -- fid2path will use alternate lookup method. */
2622 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2625 CWARN("tp obj fix error %d\n",rc2);
2627 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2628 !mdd_is_dead_obj(mdd_tobj)) {
2629 rc2 = __mdd_index_insert(env, mdd_tpobj,
2630 mdo2fid(mdd_tobj), tname,
2635 CWARN("tp obj fix error %d\n",rc2);
2640 if (rc && is_dir && mdd_sobj) {
2641 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2645 CWARN("sp obj dotdot delete error %d\n",rc2);
2648 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2649 dotdot, handle, BYPASS_CAPA);
2651 CWARN("sp obj dotdot insert error %d\n",rc2);
2656 rc2 = __mdd_index_insert(env, mdd_spobj,
2657 lf, sname, is_dir, handle, BYPASS_CAPA);
2659 CWARN("sp obj fix error %d\n",rc2);
2662 if (likely(tdlh) && sdlh != tdlh)
2663 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2665 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2668 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2669 mdd_spobj, lf, lsname, handle);
2671 struct lu_fid zero_fid;
2672 fid_zero(&zero_fid);
2673 /* If the rename target exist, The CL_EXT record should save
2674 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2675 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2677 mdd_tobj ? NULL : &zero_fid,
2682 mdd_trans_stop(env, mdd, rc, handle);
2684 mdd_object_put(env, mdd_sobj);
2686 #ifdef HAVE_QUOTA_SUPPORT
2687 if (mds->mds_quota) {
2689 lquota_pending_commit(mds_quota_interface_ref, obd,
2690 qtpids, rec_pending, 1);
2693 /* Trigger dqrel on the source owner of parent.
2694 * If failed, the next call for lquota_chkquota will
2696 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2697 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2699 /* Trigger dqrel on the target owner of child.
2700 * If failed, the next call for lquota_chkquota
2701 * will process it. */
2702 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2703 qtpids, rc, quota_copc);
2710 /** enable/disable storing of hardlink info */
2711 int mdd_linkea_enable = 1;
2712 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2713 "record hardlink info in EAs");
2715 /** Read the link EA into a temp buffer.
2716 * Uses the name_buf since it is generally large.
2717 * \retval IS_ERR err
2718 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2720 struct lu_buf *mdd_links_get(const struct lu_env *env,
2721 struct mdd_object *mdd_obj)
2724 struct lustre_capa *capa;
2725 struct link_ea_header *leh;
2728 /* First try a small buf */
2729 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2730 if (buf->lb_buf == NULL)
2731 return ERR_PTR(-ENOMEM);
2733 capa = mdd_object_capa(env, mdd_obj);
2734 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2735 if (rc == -ERANGE) {
2736 /* Buf was too small, figure out what we need. */
2738 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2741 buf = mdd_buf_alloc(env, rc);
2742 if (buf->lb_buf == NULL)
2743 return ERR_PTR(-ENOMEM);
2744 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2750 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2751 leh->leh_magic = LINK_EA_MAGIC;
2752 leh->leh_reccount = __swab32(leh->leh_reccount);
2753 leh->leh_len = __swab64(leh->leh_len);
2754 /* entries are swabbed by mdd_lee_unpack */
2756 if (leh->leh_magic != LINK_EA_MAGIC)
2757 return ERR_PTR(-EINVAL);
2758 if (leh->leh_reccount == 0)
2759 return ERR_PTR(-ENODATA);
2764 /** Pack a link_ea_entry.
2765 * All elements are stored as chars to avoid alignment issues.
2766 * Numbers are always big-endian
2767 * \retval record length
2769 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2770 const struct lu_fid *pfid)
2772 struct lu_fid tmpfid;
2775 fid_cpu_to_be(&tmpfid, pfid);
2776 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2777 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2778 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2780 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2781 lee->lee_reclen[1] = reclen & 0xff;
2785 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2786 struct lu_name *lname, struct lu_fid *pfid)
2788 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2789 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2790 fid_be_to_cpu(pfid, pfid);
2791 lname->ln_name = lee->lee_name;
2792 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2795 /** Add a record to the end of link ea buf */
2796 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2797 const struct lu_fid *pfid,
2798 const struct lu_name *lname)
2800 struct link_ea_header *leh;
2801 struct link_ea_entry *lee;
2804 if (lname == NULL || pfid == NULL)
2807 /* Make sure our buf is big enough for the new one */
2809 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2810 if (leh->leh_len + reclen > buf->lb_len) {
2811 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2816 lee = buf->lb_buf + leh->leh_len;
2817 reclen = mdd_lee_pack(lee, lname, pfid);
2818 leh->leh_len += reclen;
2819 leh->leh_reccount++;
2823 static int mdd_declare_links_add(const struct lu_env *env,
2824 struct mdd_object *mdd_obj,
2825 struct thandle *handle)
2829 /* XXX: max size? */
2830 rc = mdo_declare_xattr_set(env, mdd_obj,
2831 mdd_buf_get_const(env, NULL, 4096),
2832 XATTR_NAME_LINK, 0, handle);
2837 /* For pathologic linkers, we don't want to spend lots of time scanning the
2838 * link ea. Limit ourseleves to something reasonable; links not in the EA
2839 * can be looked up via (slower) parent lookup.
2841 #define LINKEA_MAX_COUNT 128
2843 static int mdd_links_add(const struct lu_env *env,
2844 struct mdd_object *mdd_obj,
2845 const struct lu_fid *pfid,
2846 const struct lu_name *lname,
2847 struct thandle *handle, int first)
2850 struct link_ea_header *leh;
2854 if (!mdd_linkea_enable)
2857 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2860 if (rc != -ENODATA) {
2861 CERROR("link_ea read failed %d "DFID"\n", rc,
2862 PFID(mdd_object_fid(mdd_obj)));
2865 /* empty EA; start one */
2866 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2867 if (buf->lb_buf == NULL)
2870 leh->leh_magic = LINK_EA_MAGIC;
2871 leh->leh_len = sizeof(struct link_ea_header);
2872 leh->leh_reccount = 0;
2876 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2879 rc = __mdd_links_add(env, buf, pfid, lname);
2884 rc = __mdd_xattr_set(env, mdd_obj,
2885 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2886 XATTR_NAME_LINK, 0, handle);
2889 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2890 PFID(mdd_object_fid(mdd_obj)));
2892 CERROR("link_ea add failed %d "DFID"\n", rc,
2893 PFID(mdd_object_fid(mdd_obj)));
2896 if (buf->lb_len > OBD_ALLOC_BIG)
2897 /* if we vmalloced a large buffer drop it */
2903 static int mdd_links_rename(const struct lu_env *env,
2904 struct mdd_object *mdd_obj,
2905 const struct lu_fid *oldpfid,
2906 const struct lu_name *oldlname,
2907 const struct lu_fid *newpfid,
2908 const struct lu_name *newlname,
2909 struct thandle *handle)
2912 struct link_ea_header *leh;
2913 struct link_ea_entry *lee;
2914 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2915 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2921 if (!mdd_linkea_enable)
2924 if (mdd_obj->mod_flags & DEAD_OBJ)
2925 /* No more links, don't bother */
2928 buf = mdd_links_get(env, mdd_obj);
2932 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2933 rc, PFID(mdd_object_fid(mdd_obj)));
2935 CERROR("link_ea read failed %d "DFID"\n",
2936 rc, PFID(mdd_object_fid(mdd_obj)));
2940 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2942 /* Find the old record */
2943 for(count = 0; count < leh->leh_reccount; count++) {
2944 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2945 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2946 lu_fid_eq(tmpfid, oldpfid) &&
2947 (strncmp(tmpname->ln_name, oldlname->ln_name,
2948 tmpname->ln_namelen) == 0))
2950 lee = (struct link_ea_entry *)((char *)lee + reclen);
2952 if ((count + 1) > leh->leh_reccount) {
2953 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2954 oldlname->ln_namelen, oldlname->ln_name);
2955 GOTO(out, rc = -ENOENT);
2958 /* Remove the old record */
2959 leh->leh_reccount--;
2960 leh->leh_len -= reclen;
2961 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2964 /* If renaming, add the new record */
2965 if (newpfid != NULL) {
2966 /* if the add fails, we still delete the out-of-date old link */
2967 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2971 rc = __mdd_xattr_set(env, mdd_obj,
2972 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2973 XATTR_NAME_LINK, 0, handle);
2979 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2980 oldlname->ln_namelen, oldlname->ln_name, rc,
2981 PFID(mdd_object_fid(mdd_obj)));
2983 if (buf->lb_len > OBD_ALLOC_BIG)
2984 /* if we vmalloced a large buffer drop it */
2990 const struct md_dir_operations mdd_dir_ops = {
2991 .mdo_is_subdir = mdd_is_subdir,
2992 .mdo_lookup = mdd_lookup,
2993 .mdo_create = mdd_create,
2994 .mdo_rename = mdd_rename,
2995 .mdo_link = mdd_link,
2996 .mdo_unlink = mdd_unlink,
2997 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
2998 .mdo_name_insert = mdd_name_insert,
2999 .mdo_name_remove = mdd_name_remove,
3000 .mdo_rename_tgt = mdd_rename_tgt,
3001 .mdo_create_data = mdd_create_data,