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,
1082 ma->ma_lmm, ma->ma_lmm_size,
1085 #warning "please remove this after 2.4 (LOD/OSP)."
1088 #ifdef HAVE_QUOTA_SUPPORT
1090 /* Trigger dqrel on the owner of child and parent. If failed,
1091 * the next call for lquota_chkquota will process it. */
1092 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1098 /* has not lock on pobj yet */
1099 static int mdd_ni_sanity_check(const struct lu_env *env,
1100 struct md_object *pobj,
1101 const struct md_attr *ma)
1103 struct mdd_object *obj = md2mdd_obj(pobj);
1107 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1110 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1116 * Partial operation.
1118 static int mdd_name_insert(const struct lu_env *env,
1119 struct md_object *pobj,
1120 const struct lu_name *lname,
1121 const struct lu_fid *fid,
1122 const struct md_attr *ma)
1124 const char *name = lname->ln_name;
1125 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1126 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1127 struct dynlock_handle *dlh;
1128 struct thandle *handle;
1129 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1130 #ifdef HAVE_QUOTA_SUPPORT
1131 struct mdd_device *mdd = mdo2mdd(pobj);
1132 struct md_ucred *uc = md_ucred(env);
1133 struct obd_device *obd = mdd->mdd_obd_dev;
1134 struct obd_export *exp = md_quota(env)->mq_exp;
1135 struct mds_obd *mds = &obd->u.mds;
1136 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1137 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1138 cfs_cap_t save = uc->mu_cap;
1143 /* XXX: this code won't be used ever:
1144 * DNE uses slightly different approach */
1147 #ifdef HAVE_QUOTA_SUPPORT
1148 if (mds->mds_quota) {
1149 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1150 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1152 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1155 mdd_data_get(env, mdd_obj, &data);
1156 quota_opc = FSFILT_OP_LINK;
1157 mdd_quota_wrapper(la_tmp, qids);
1158 /* get block quota for parent */
1159 lquota_chkquota(mds_quota_interface_ref, obd,
1160 exp, qids, rec_pending, 1, NULL,
1161 LQUOTA_FLAGS_BLK, data, 1);
1164 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1168 handle = mdd_trans_create(env, mdo2mdd(pobj));
1170 GOTO(out_pending, rc = PTR_ERR(handle));
1172 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1174 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1176 GOTO(out_trans, rc = -ENOMEM);
1178 rc = mdd_ni_sanity_check(env, pobj, ma);
1180 GOTO(out_unlock, rc);
1182 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1183 handle, BYPASS_CAPA);
1185 GOTO(out_unlock, rc);
1188 * For some case, no need update obj's ctime (LA_CTIME is not set),
1190 * For other cases, update obj's ctime (LA_CTIME is set),
1193 if (ma->ma_attr.la_valid & LA_CTIME) {
1194 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1195 la->la_valid = LA_CTIME | LA_MTIME;
1196 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1201 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1203 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1205 #ifdef HAVE_QUOTA_SUPPORT
1206 if (mds->mds_quota) {
1208 lquota_pending_commit(mds_quota_interface_ref,
1209 obd, qids, rec_pending, 1);
1210 /* Trigger dqacq for the parent owner. If failed,
1211 * the next call for lquota_chkquota will process it*/
1212 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1222 /* has not lock on pobj yet */
1223 static int mdd_nr_sanity_check(const struct lu_env *env,
1224 struct md_object *pobj,
1225 const struct md_attr *ma)
1227 struct mdd_object *obj = md2mdd_obj(pobj);
1231 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1234 rc = mdd_may_unlink(env, obj, ma);
1240 * Partial operation.
1242 static int mdd_name_remove(const struct lu_env *env,
1243 struct md_object *pobj,
1244 const struct lu_name *lname,
1245 const struct md_attr *ma)
1247 const char *name = lname->ln_name;
1248 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1249 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1250 struct mdd_device *mdd = mdo2mdd(pobj);
1251 struct dynlock_handle *dlh;
1252 struct thandle *handle;
1253 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1254 #ifdef HAVE_QUOTA_SUPPORT
1255 struct obd_device *obd = mdd->mdd_obd_dev;
1256 struct mds_obd *mds = &obd->u.mds;
1257 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1263 /* XXX: this code won't be used ever:
1264 * DNE uses slightly different approach */
1267 #ifdef HAVE_QUOTA_SUPPORT
1268 if (mds->mds_quota) {
1269 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1271 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1273 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1274 mdd_quota_wrapper(la_tmp, qids);
1278 handle = mdd_trans_create(env, mdd);
1280 GOTO(out_pending, rc = PTR_ERR(handle));
1282 rc = mdd_trans_start(env, mdd, handle);
1284 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1286 GOTO(out_trans, rc = -ENOMEM);
1288 rc = mdd_nr_sanity_check(env, pobj, ma);
1290 GOTO(out_unlock, rc);
1292 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1293 handle, BYPASS_CAPA);
1295 GOTO(out_unlock, rc);
1298 * For some case, no need update obj's ctime (LA_CTIME is not set),
1300 * For other cases, update obj's ctime (LA_CTIME is set),
1303 if (ma->ma_attr.la_valid & LA_CTIME) {
1304 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1305 la->la_valid = LA_CTIME | LA_MTIME;
1306 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1311 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1313 mdd_trans_stop(env, mdd, rc, handle);
1315 #ifdef HAVE_QUOTA_SUPPORT
1316 /* Trigger dqrel for the parent owner.
1317 * If failed, the next call for lquota_chkquota will process it. */
1319 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1327 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1329 static int mdd_rt_sanity_check(const struct lu_env *env,
1330 struct mdd_object *tgt_pobj,
1331 struct mdd_object *tobj,
1337 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1340 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1341 * exist. In fact, tobj must exist, otherwise the call trace will be:
1342 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1343 * When get here, tobj must be NOT NULL, the other case has been
1344 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1346 * So check may_delete, but not check nlink of tgt_pobj. */
1348 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1353 /* Partial rename op on slave MDD */
1354 static int mdd_rename_tgt(const struct lu_env *env,
1355 struct md_object *pobj, struct md_object *tobj,
1356 const struct lu_fid *lf, const struct lu_name *lname,
1359 const char *name = lname->ln_name;
1360 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1361 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1362 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1363 struct mdd_device *mdd = mdo2mdd(pobj);
1364 struct dynlock_handle *dlh;
1365 struct thandle *handle;
1366 #ifdef HAVE_QUOTA_SUPPORT
1367 struct obd_device *obd = mdd->mdd_obd_dev;
1368 struct obd_export *exp = md_quota(env)->mq_exp;
1369 struct mds_obd *mds = &obd->u.mds;
1370 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1371 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1372 int quota_copc = 0, quota_popc = 0;
1373 int rec_pending[MAXQUOTAS] = { 0, 0 };
1378 /* XXX: this code won't be used ever:
1379 * DNE uses slightly different approach */
1382 #ifdef HAVE_QUOTA_SUPPORT
1383 if (mds->mds_quota && !tobj) {
1384 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1386 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1389 mdd_data_get(env, mdd_tpobj, &data);
1390 quota_popc = FSFILT_OP_LINK;
1391 mdd_quota_wrapper(la_tmp, qpids);
1392 /* get block quota for target parent */
1393 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1394 qpids, rec_pending, 1, NULL,
1395 LQUOTA_FLAGS_BLK, data, 1);
1399 handle = mdd_trans_create(env, mdd);
1401 GOTO(out_pending, rc = PTR_ERR(handle));
1403 rc = mdd_trans_start(env, mdd, handle);
1405 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1407 GOTO(out_trans, rc = -ENOMEM);
1409 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1411 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1416 * If rename_tgt is called then we should just re-insert name with
1417 * correct fid, no need to dec/inc parent nlink if obj is dir.
1419 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1423 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1428 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1429 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1431 la->la_valid = LA_CTIME | LA_MTIME;
1432 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1437 * For tobj is remote case cmm layer has processed
1438 * and pass NULL tobj to here. So when tobj is NOT NULL,
1439 * it must be local one.
1441 if (tobj && mdd_object_exists(mdd_tobj)) {
1442 mdo_ref_del(env, mdd_tobj, handle);
1444 /* Remove dot reference. */
1445 if (S_ISDIR(ma->ma_attr.la_mode))
1446 mdo_ref_del(env, mdd_tobj, handle);
1448 la->la_valid = LA_CTIME;
1449 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1453 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1457 #ifdef HAVE_QUOTA_SUPPORT
1458 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1459 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1460 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1461 mdd_quota_wrapper(&ma->ma_attr, qcids);
1468 mdd_write_unlock(env, mdd_tobj);
1469 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1472 /* Bare EXT record with no RENAME in front of it signifies
1473 a partial slave op */
1474 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1475 mdd_tpobj, NULL, lname, handle);
1477 mdd_trans_stop(env, mdd, rc, handle);
1479 #ifdef HAVE_QUOTA_SUPPORT
1480 if (mds->mds_quota) {
1482 lquota_pending_commit(mds_quota_interface_ref, obd,
1483 qpids, rec_pending, 1);
1486 /* Trigger dqrel on the target owner of child.
1487 * If failed, the next call for lquota_chkquota
1488 * will process it. */
1489 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1497 * The permission has been checked when obj created, no need check again.
1499 static int mdd_cd_sanity_check(const struct lu_env *env,
1500 struct mdd_object *obj)
1505 if (!obj || mdd_is_dead_obj(obj))
1512 static int mdd_declare_create_data(const struct lu_env *env,
1513 struct mdd_device *mdd,
1514 struct mdd_object *obj,
1516 struct thandle *handle)
1518 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1522 buf->lb_len = lmm_size;
1523 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1528 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1533 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1534 struct md_object *cobj, const struct md_op_spec *spec,
1537 struct mdd_device *mdd = mdo2mdd(cobj);
1538 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1539 struct mdd_object *son = md2mdd_obj(cobj);
1540 struct lov_mds_md *lmm = NULL;
1542 struct thandle *handle;
1546 rc = mdd_cd_sanity_check(env, son);
1550 if (!md_should_create(spec->sp_cr_flags))
1552 lmm_size = ma->ma_lmm_size;
1554 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1558 handle = mdd_trans_create(env, mdd);
1560 GOTO(out_free, rc = PTR_ERR(handle));
1562 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1566 rc = mdd_trans_start(env, mdd, handle);
1571 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1572 * Should this be fixed?
1575 /* Replay creates has objects already */
1577 if (spec->no_create) {
1578 CDEBUG(D_INFO, "we already have lov ea\n");
1579 rc = mdd_lov_set_md(env, mdd_pobj, son,
1580 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1581 spec->u.sp_ea.eadatalen, handle, 0);
1584 /* No need mdd_lsm_sanity_check here */
1585 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1586 lmm_size, handle, 0);
1589 rc = mdd_attr_get_internal_locked(env, son, ma);
1591 /* update lov_objid data, must be before transaction stop! */
1593 mdd_lov_objid_update(mdd, lmm);
1596 mdd_trans_stop(env, mdd, rc, handle);
1598 /* Finish mdd_lov_create() stuff. */
1599 /* if no_create == 0 (not replay), we free lmm allocated by
1600 * mdd_lov_create() */
1601 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1605 /* Get fid from name and parent */
1607 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1608 const struct lu_name *lname, struct lu_fid* fid, int mask)
1610 const char *name = lname->ln_name;
1611 const struct dt_key *key = (const struct dt_key *)name;
1612 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1613 struct mdd_device *m = mdo2mdd(pobj);
1614 struct dt_object *dir = mdd_object_child(mdd_obj);
1618 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1621 rc = mdd_object_exists(mdd_obj);
1622 if (unlikely(rc == 0))
1624 else if (unlikely(rc < 0)) {
1625 CERROR("Object "DFID" locates on remote server\n",
1626 PFID(mdo2fid(mdd_obj)));
1630 /* The common filename length check. */
1631 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1632 RETURN(-ENAMETOOLONG);
1634 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1639 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1640 dt_try_as_dir(env, dir))) {
1642 rc = dir->do_index_ops->dio_lookup(env, dir,
1643 (struct dt_rec *)fid, key,
1644 mdd_object_capa(env, mdd_obj));
1655 int mdd_declare_object_initialize(const struct lu_env *env,
1656 struct mdd_object *child,
1658 struct thandle *handle)
1662 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1663 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1664 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1667 rc = mdo_declare_ref_add(env, child, handle);
1670 mdd_declare_links_add(env, child, handle);
1675 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1676 const struct lu_name *lname, struct mdd_object *child,
1677 struct md_attr *ma, struct thandle *handle,
1678 const struct md_op_spec *spec)
1684 * Update attributes for child.
1687 * (1) the valid bits should be converted between Lustre and Linux;
1688 * (2) maybe, the child attributes should be set in OSD when creation.
1691 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1695 if (S_ISDIR(ma->ma_attr.la_mode)) {
1696 /* Add "." and ".." for newly created dir */
1697 mdo_ref_add(env, child, handle);
1698 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1699 dot, handle, BYPASS_CAPA);
1701 rc = __mdd_index_insert_only(env, child, pfid,
1705 mdo_ref_del(env, child, handle);
1708 mdd_links_add(env, child, pfid, lname, handle, 1);
1713 /* has not lock on pobj yet */
1714 static int mdd_create_sanity_check(const struct lu_env *env,
1715 struct md_object *pobj,
1716 const struct lu_name *lname,
1718 struct md_op_spec *spec)
1720 struct mdd_thread_info *info = mdd_env_info(env);
1721 struct lu_attr *la = &info->mti_la;
1722 struct lu_fid *fid = &info->mti_fid;
1723 struct mdd_object *obj = md2mdd_obj(pobj);
1724 struct mdd_device *m = mdo2mdd(pobj);
1725 int lookup = spec->sp_cr_lookup;
1730 if (mdd_is_dead_obj(obj))
1734 * In some cases this lookup is not needed - we know before if name
1735 * exists or not because MDT performs lookup for it.
1736 * name length check is done in lookup.
1740 * Check if the name already exist, though it will be checked in
1741 * _index_insert also, for avoiding rolling back if exists
1744 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1745 MAY_WRITE | MAY_EXEC);
1747 RETURN(rc ? : -EEXIST);
1750 * Check WRITE permission for the parent.
1751 * EXEC permission have been checked
1752 * when lookup before create already.
1754 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1761 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1765 if (la->la_mode & S_ISGID) {
1766 ma->ma_attr.la_gid = la->la_gid;
1767 if (S_ISDIR(ma->ma_attr.la_mode)) {
1768 ma->ma_attr.la_mode |= S_ISGID;
1769 ma->ma_attr.la_valid |= LA_MODE;
1773 switch (ma->ma_attr.la_mode & S_IFMT) {
1775 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1777 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1778 RETURN(-ENAMETOOLONG);
1797 static int mdd_declare_create(const struct lu_env *env,
1798 struct mdd_device *mdd,
1799 struct mdd_object *p,
1800 struct mdd_object *c,
1801 const struct lu_name *name,
1804 struct thandle *handle,
1805 const struct md_op_spec *spec)
1807 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1810 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1814 /* if dir, then can inherit default ACl */
1816 buf->lb_len = lmm_size;
1817 if (S_ISDIR(ma->ma_attr.la_mode)) {
1818 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1821 rc = mdo_declare_ref_add(env, p, handle);
1826 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1831 rc = mdd_declare_object_initialize(env, c, ma, handle);
1835 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1836 name->ln_name, handle);
1840 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1845 if (S_ISLNK(ma->ma_attr.la_mode)) {
1846 rc = dt_declare_record_write(env, mdd_object_child(c),
1847 strlen(spec->u.sp_symname), 0,
1852 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1856 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1860 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1868 * Create object and insert it into namespace.
1870 static int mdd_create(const struct lu_env *env,
1871 struct md_object *pobj,
1872 const struct lu_name *lname,
1873 struct md_object *child,
1874 struct md_op_spec *spec,
1877 struct mdd_thread_info *info = mdd_env_info(env);
1878 struct lu_attr *la = &info->mti_la_for_fix;
1879 struct md_attr *ma_acl = &info->mti_ma;
1880 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1881 struct mdd_object *son = md2mdd_obj(child);
1882 struct mdd_device *mdd = mdo2mdd(pobj);
1883 struct lu_attr *attr = &ma->ma_attr;
1884 struct lov_mds_md *lmm = NULL;
1885 struct thandle *handle;
1886 struct dynlock_handle *dlh;
1887 const char *name = lname->ln_name;
1888 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1889 int got_def_acl = 0;
1890 #ifdef HAVE_QUOTA_SUPPORT
1891 struct obd_device *obd = mdd->mdd_obd_dev;
1892 struct obd_export *exp = md_quota(env)->mq_exp;
1893 struct mds_obd *mds = &obd->u.mds;
1894 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1895 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1896 int quota_opc = 0, block_count = 0;
1897 int inode_pending[MAXQUOTAS] = { 0, 0 };
1898 int block_pending[MAXQUOTAS] = { 0, 0 };
1899 int parent_pending[MAXQUOTAS] = { 0, 0 };
1904 * Two operations have to be performed:
1906 * - an allocation of a new object (->do_create()), and
1908 * - an insertion into a parent index (->dio_insert()).
1910 * Due to locking, operation order is not important, when both are
1911 * successful, *but* error handling cases are quite different:
1913 * - if insertion is done first, and following object creation fails,
1914 * insertion has to be rolled back, but this operation might fail
1915 * also leaving us with dangling index entry.
1917 * - if creation is done first, is has to be undone if insertion
1918 * fails, leaving us with leaked space, which is neither good, nor
1921 * It seems that creation-first is simplest solution, but it is
1922 * sub-optimal in the frequent
1927 * case, because second mkdir is bound to create object, only to
1928 * destroy it immediately.
1930 * To avoid this follow local file systems that do double lookup:
1932 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1934 * 1. create (mdd_object_create_internal())
1936 * 2. insert (__mdd_index_insert(), lookup again)
1939 /* Sanity checks before big job. */
1940 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1944 #ifdef HAVE_QUOTA_SUPPORT
1945 if (mds->mds_quota) {
1946 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1948 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1952 quota_opc = FSFILT_OP_CREATE;
1953 mdd_quota_wrapper(&ma->ma_attr, qcids);
1954 mdd_quota_wrapper(la_tmp, qpids);
1955 /* get file quota for child */
1956 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1957 qcids, inode_pending, 1, NULL, 0, NULL,
1959 switch (ma->ma_attr.la_mode & S_IFMT) {
1968 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1969 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1973 /* get block quota for child and parent */
1975 lquota_chkquota(mds_quota_interface_ref, obd,
1976 exp, qcids, block_pending,
1978 LQUOTA_FLAGS_BLK, NULL, 0);
1980 lquota_chkquota(mds_quota_interface_ref, obd,
1981 exp, qpids, parent_pending, 1,
1982 NULL, LQUOTA_FLAGS_BLK, NULL,
1988 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1989 GOTO(out_pending, rc = -EINPROGRESS);
1992 * No RPC inside the transaction, so OST objects should be created at
1995 if (S_ISREG(attr->la_mode)) {
1996 lmm_size = ma->ma_lmm_size;
1997 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
2000 GOTO(out_pending, rc);
2003 if (!S_ISLNK(attr->la_mode)) {
2004 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2005 ma_acl->ma_acl = info->mti_xattr_buf;
2006 ma_acl->ma_need = MA_ACL_DEF;
2007 ma_acl->ma_valid = 0;
2009 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2010 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2011 mdd_read_unlock(env, mdd_pobj);
2014 else if (ma_acl->ma_valid & MA_ACL_DEF)
2018 handle = mdd_trans_create(env, mdd);
2020 GOTO(out_free, rc = PTR_ERR(handle));
2022 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2023 lmm_size, handle, spec);
2027 rc = mdd_trans_start(env, mdd, handle);
2031 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2033 GOTO(out_trans, rc = -ENOMEM);
2035 mdd_write_lock(env, son, MOR_TGT_CHILD);
2036 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2038 mdd_write_unlock(env, son);
2044 #ifdef CONFIG_FS_POSIX_ACL
2046 struct lu_buf *acl_buf = &info->mti_buf;
2047 acl_buf->lb_buf = ma_acl->ma_acl;
2048 acl_buf->lb_len = ma_acl->ma_acl_size;
2050 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2052 mdd_write_unlock(env, son);
2055 ma->ma_attr.la_valid |= LA_MODE;
2060 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2061 son, ma, handle, spec);
2062 mdd_write_unlock(env, son);
2065 * Object has no links, so it will be destroyed when last
2066 * reference is released. (XXX not now.)
2072 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2073 name, S_ISDIR(attr->la_mode), handle,
2074 mdd_object_capa(env, mdd_pobj));
2081 /* No need mdd_lsm_sanity_check here */
2082 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2084 CERROR("error on stripe info copy %d \n", rc);
2087 if (lmm && lmm_size > 0) {
2088 /* Set Lov here, do not get lmm again later */
2089 if (lmm_size > ma->ma_lmm_size) {
2090 /* Reply buffer is smaller, need bigger one */
2091 mdd_max_lmm_buffer(env, lmm_size);
2092 if (unlikely(info->mti_max_lmm == NULL))
2093 GOTO(cleanup, rc = -ENOMEM);
2094 ma->ma_lmm = info->mti_max_lmm;
2095 ma->ma_big_lmm_used = 1;
2097 memcpy(ma->ma_lmm, lmm, lmm_size);
2098 ma->ma_lmm_size = lmm_size;
2099 ma->ma_valid |= MA_LOV;
2102 if (S_ISLNK(attr->la_mode)) {
2103 struct md_ucred *uc = md_ucred(env);
2104 struct dt_object *dt = mdd_object_child(son);
2105 const char *target_name = spec->u.sp_symname;
2106 int sym_len = strlen(target_name);
2107 const struct lu_buf *buf;
2110 buf = mdd_buf_get_const(env, target_name, sym_len);
2111 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2112 mdd_object_capa(env, son),
2114 CFS_CAP_SYS_RESOURCE_MASK);
2119 GOTO(cleanup, rc = -EFAULT);
2123 la->la_valid = LA_CTIME | LA_MTIME;
2124 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2128 /* Return attr back. */
2129 rc = mdd_attr_get_internal_locked(env, son, ma);
2132 if (rc && created) {
2136 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2137 S_ISDIR(attr->la_mode),
2138 handle, BYPASS_CAPA);
2140 CERROR("error can not cleanup destroy %d\n",
2145 mdd_write_lock(env, son, MOR_TGT_CHILD);
2146 mdo_ref_del(env, son, handle);
2147 if (initialized && S_ISDIR(attr->la_mode))
2148 mdo_ref_del(env, son, handle);
2149 mdd_write_unlock(env, son);
2153 /* update lov_objid data, must be before transaction stop! */
2155 mdd_lov_objid_update(mdd, lmm);
2157 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2160 rc = mdd_changelog_ns_store(env, mdd,
2161 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2162 S_ISREG(attr->la_mode) ? CL_CREATE :
2163 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2164 0, son, mdd_pobj, NULL, lname, handle);
2166 mdd_trans_stop(env, mdd, rc, handle);
2168 /* finish lov_create stuff, free all temporary data */
2169 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2171 #ifdef HAVE_QUOTA_SUPPORT
2173 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2175 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2177 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2179 /* Trigger dqacq on the owner of child and parent. If failed,
2180 * the next call for lquota_chkquota will process it. */
2181 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2185 /* The child object shouldn't be cached anymore */
2187 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
2188 &child->mo_lu.lo_header->loh_flags);
2193 * Get locks on parents in proper order
2194 * RETURN: < 0 - error, rename_order if successful
2202 static int mdd_rename_order(const struct lu_env *env,
2203 struct mdd_device *mdd,
2204 struct mdd_object *src_pobj,
2205 struct mdd_object *tgt_pobj)
2207 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2211 if (src_pobj == tgt_pobj)
2212 RETURN(MDD_RN_SAME);
2214 /* compared the parent child relationship of src_p&tgt_p */
2215 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2217 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2220 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2233 /* has not mdd_write{read}_lock on any obj yet. */
2234 static int mdd_rename_sanity_check(const struct lu_env *env,
2235 struct mdd_object *src_pobj,
2236 struct mdd_object *tgt_pobj,
2237 struct mdd_object *sobj,
2238 struct mdd_object *tobj,
2244 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2247 /* XXX: when get here, sobj must NOT be NULL,
2248 * the other case has been processed in cml_rename
2249 * before mdd_rename and enable MDS_PERM_BYPASS. */
2252 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2256 /* XXX: when get here, "tobj == NULL" means tobj must
2257 * NOT exist (neither on remote MDS, such case has been
2258 * processed in cml_rename before mdd_rename and enable
2260 * So check may_create, but not check may_unlink. */
2262 rc = mdd_may_create(env, tgt_pobj, NULL,
2263 (src_pobj != tgt_pobj), 0);
2265 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2266 (src_pobj != tgt_pobj), 1);
2268 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2269 S_ISDIR(ma->ma_attr.la_mode))
2270 rc = __mdd_may_link(env, tgt_pobj);
2275 static int mdd_declare_rename(const struct lu_env *env,
2276 struct mdd_device *mdd,
2277 struct mdd_object *mdd_spobj,
2278 struct mdd_object *mdd_tpobj,
2279 struct mdd_object *mdd_sobj,
2280 struct mdd_object *mdd_tobj,
2281 const struct lu_name *sname,
2282 const struct lu_name *tname,
2284 struct thandle *handle)
2292 /* name from source dir */
2293 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2297 /* .. from source child */
2298 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2299 /* source child can be directory,
2300 * counted by source dir's nlink */
2301 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2305 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2309 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2314 /* new target child can be directory,
2315 * counted by target dir's nlink */
2316 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2322 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2326 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2329 mdd_declare_links_add(env, mdd_sobj, handle);
2333 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2338 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2339 tname->ln_name, handle);
2343 /* name from target dir (old name), we declare it unconditionally
2344 * as mdd_rename() calls delete unconditionally as well. so just
2345 * to balance declarations vs calls to change ... */
2346 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2350 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2351 /* delete target child in target parent directory */
2352 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2356 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2357 /* target child can be directory,
2358 * delete "." reference in target child directory */
2359 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2363 /* delete ".." reference in target parent directory */
2364 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2369 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2373 mdd_declare_links_add(env, mdd_tobj, handle);
2377 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2382 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2386 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2393 /* src object can be remote that is why we use only fid and type of object */
2394 static int mdd_rename(const struct lu_env *env,
2395 struct md_object *src_pobj, struct md_object *tgt_pobj,
2396 const struct lu_fid *lf, const struct lu_name *lsname,
2397 struct md_object *tobj, const struct lu_name *ltname,
2400 const char *sname = lsname->ln_name;
2401 const char *tname = ltname->ln_name;
2402 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2403 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2404 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2405 struct mdd_device *mdd = mdo2mdd(src_pobj);
2406 struct mdd_object *mdd_sobj = NULL; /* source object */
2407 struct mdd_object *mdd_tobj = NULL;
2408 struct dynlock_handle *sdlh, *tdlh;
2409 struct thandle *handle;
2410 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2411 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2415 #ifdef HAVE_QUOTA_SUPPORT
2416 struct obd_device *obd = mdd->mdd_obd_dev;
2417 struct obd_export *exp = md_quota(env)->mq_exp;
2418 struct mds_obd *mds = &obd->u.mds;
2419 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2420 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2421 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2422 int quota_copc = 0, quota_popc = 0;
2423 int rec_pending[MAXQUOTAS] = { 0, 0 };
2427 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2428 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2431 mdd_tobj = md2mdd_obj(tobj);
2433 #ifdef HAVE_QUOTA_SUPPORT
2434 if (mds->mds_quota) {
2435 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2437 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2439 mdd_quota_wrapper(la_tmp, qspids);
2441 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2445 mdd_data_get(env, mdd_tpobj, &data);
2446 quota_popc = FSFILT_OP_LINK;
2447 mdd_quota_wrapper(la_tmp, qtpids);
2448 /* get block quota for target parent */
2449 lquota_chkquota(mds_quota_interface_ref,
2451 rec_pending, 1, NULL,
2459 mdd_sobj = mdd_object_find(env, mdd, lf);
2461 handle = mdd_trans_create(env, mdd);
2463 GOTO(out_pending, rc = PTR_ERR(handle));
2465 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2466 mdd_tobj, lsname, ltname, ma, handle);
2470 rc = mdd_trans_start(env, mdd, handle);
2474 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2475 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2477 GOTO(cleanup_unlocked, rc);
2479 /* Get locks in determined order */
2480 if (rc == MDD_RN_SAME) {
2481 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2482 sname, MOR_SRC_PARENT);
2483 /* check hashes to determine do we need one lock or two */
2484 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2485 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2489 } else if (rc == MDD_RN_SRCTGT) {
2490 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2491 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2493 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2494 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2496 if (sdlh == NULL || tdlh == NULL)
2497 GOTO(cleanup, rc = -ENOMEM);
2499 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2500 mdd_sobj, mdd_tobj, ma);
2504 /* Remove source name from source directory */
2505 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2506 mdd_object_capa(env, mdd_spobj));
2510 /* "mv dir1 dir2" needs "dir1/.." link update */
2511 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2512 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2513 mdd_object_capa(env, mdd_sobj));
2515 GOTO(fixup_spobj2, rc);
2517 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2518 handle, mdd_object_capa(env, mdd_sobj));
2520 GOTO(fixup_spobj, rc);
2523 /* Remove target name from target directory
2524 * Here tobj can be remote one, so we do index_delete unconditionally
2525 * and -ENOENT is allowed.
2527 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2528 mdd_object_capa(env, mdd_tpobj));
2531 /* tname might been renamed to something else */
2532 GOTO(fixup_spobj, rc);
2535 GOTO(fixup_spobj, rc);
2538 /* Insert new fid with target name into target dir */
2539 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2540 mdd_object_capa(env, mdd_tpobj));
2542 GOTO(fixup_tpobj, rc);
2544 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2545 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2547 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2549 la->la_valid = LA_CTIME;
2550 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2553 GOTO(fixup_tpobj, rc);
2556 /* Remove old target object
2557 * For tobj is remote case cmm layer has processed
2558 * and set tobj to NULL then. So when tobj is NOT NULL,
2559 * it must be local one.
2561 if (tobj && mdd_object_exists(mdd_tobj)) {
2562 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2563 if (mdd_is_dead_obj(mdd_tobj)) {
2564 mdd_write_unlock(env, mdd_tobj);
2565 /* shld not be dead, something is wrong */
2566 CERROR("tobj is dead, something is wrong\n");
2570 mdo_ref_del(env, mdd_tobj, handle);
2572 /* Remove dot reference. */
2574 mdo_ref_del(env, mdd_tobj, handle);
2576 la->la_valid = LA_CTIME;
2577 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2579 GOTO(fixup_tpobj, rc);
2581 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2582 mdd_write_unlock(env, mdd_tobj);
2584 GOTO(fixup_tpobj, rc);
2586 #ifdef HAVE_QUOTA_SUPPORT
2587 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2588 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2589 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2590 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2595 la->la_valid = LA_CTIME | LA_MTIME;
2596 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2598 GOTO(fixup_tpobj, rc);
2600 if (mdd_spobj != mdd_tpobj) {
2601 la->la_valid = LA_CTIME | LA_MTIME;
2602 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2606 if (rc == 0 && mdd_sobj) {
2607 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2608 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2609 mdo2fid(mdd_tpobj), ltname, handle);
2611 /* Old files might not have EA entry */
2612 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2614 mdd_write_unlock(env, mdd_sobj);
2615 /* We don't fail the transaction if the link ea can't be
2616 updated -- fid2path will use alternate lookup method. */
2624 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2627 CWARN("tp obj fix error %d\n",rc2);
2629 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2630 !mdd_is_dead_obj(mdd_tobj)) {
2631 rc2 = __mdd_index_insert(env, mdd_tpobj,
2632 mdo2fid(mdd_tobj), tname,
2637 CWARN("tp obj fix error %d\n",rc2);
2642 if (rc && is_dir && mdd_sobj) {
2643 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2647 CWARN("sp obj dotdot delete error %d\n",rc2);
2650 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2651 dotdot, handle, BYPASS_CAPA);
2653 CWARN("sp obj dotdot insert error %d\n",rc2);
2658 rc2 = __mdd_index_insert(env, mdd_spobj,
2659 lf, sname, is_dir, handle, BYPASS_CAPA);
2661 CWARN("sp obj fix error %d\n",rc2);
2664 if (likely(tdlh) && sdlh != tdlh)
2665 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2667 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2670 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2671 mdd_spobj, lf, lsname, handle);
2673 struct lu_fid zero_fid;
2674 fid_zero(&zero_fid);
2675 /* If the rename target exist, The CL_EXT record should save
2676 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2677 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2679 mdd_tobj ? NULL : &zero_fid,
2684 mdd_trans_stop(env, mdd, rc, handle);
2686 mdd_object_put(env, mdd_sobj);
2688 #ifdef HAVE_QUOTA_SUPPORT
2689 if (mds->mds_quota) {
2691 lquota_pending_commit(mds_quota_interface_ref, obd,
2692 qtpids, rec_pending, 1);
2695 /* Trigger dqrel on the source owner of parent.
2696 * If failed, the next call for lquota_chkquota will
2698 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2699 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2701 /* Trigger dqrel on the target owner of child.
2702 * If failed, the next call for lquota_chkquota
2703 * will process it. */
2704 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2705 qtpids, rc, quota_copc);
2712 /** enable/disable storing of hardlink info */
2713 int mdd_linkea_enable = 1;
2714 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2715 "record hardlink info in EAs");
2717 /** Read the link EA into a temp buffer.
2718 * Uses the name_buf since it is generally large.
2719 * \retval IS_ERR err
2720 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2722 struct lu_buf *mdd_links_get(const struct lu_env *env,
2723 struct mdd_object *mdd_obj)
2726 struct lustre_capa *capa;
2727 struct link_ea_header *leh;
2730 /* First try a small buf */
2731 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2732 if (buf->lb_buf == NULL)
2733 return ERR_PTR(-ENOMEM);
2735 capa = mdd_object_capa(env, mdd_obj);
2736 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2737 if (rc == -ERANGE) {
2738 /* Buf was too small, figure out what we need. */
2740 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2743 buf = mdd_buf_alloc(env, rc);
2744 if (buf->lb_buf == NULL)
2745 return ERR_PTR(-ENOMEM);
2746 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2752 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2753 leh->leh_magic = LINK_EA_MAGIC;
2754 leh->leh_reccount = __swab32(leh->leh_reccount);
2755 leh->leh_len = __swab64(leh->leh_len);
2756 /* entries are swabbed by mdd_lee_unpack */
2758 if (leh->leh_magic != LINK_EA_MAGIC)
2759 return ERR_PTR(-EINVAL);
2760 if (leh->leh_reccount == 0)
2761 return ERR_PTR(-ENODATA);
2766 /** Pack a link_ea_entry.
2767 * All elements are stored as chars to avoid alignment issues.
2768 * Numbers are always big-endian
2769 * \retval record length
2771 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2772 const struct lu_fid *pfid)
2774 struct lu_fid tmpfid;
2777 fid_cpu_to_be(&tmpfid, pfid);
2778 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2779 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2780 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2782 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2783 lee->lee_reclen[1] = reclen & 0xff;
2787 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2788 struct lu_name *lname, struct lu_fid *pfid)
2790 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2791 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2792 fid_be_to_cpu(pfid, pfid);
2793 lname->ln_name = lee->lee_name;
2794 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2797 /** Add a record to the end of link ea buf */
2798 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2799 const struct lu_fid *pfid,
2800 const struct lu_name *lname)
2802 struct link_ea_header *leh;
2803 struct link_ea_entry *lee;
2806 if (lname == NULL || pfid == NULL)
2809 /* Make sure our buf is big enough for the new one */
2811 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2812 if (leh->leh_len + reclen > buf->lb_len) {
2813 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2818 lee = buf->lb_buf + leh->leh_len;
2819 reclen = mdd_lee_pack(lee, lname, pfid);
2820 leh->leh_len += reclen;
2821 leh->leh_reccount++;
2825 static int mdd_declare_links_add(const struct lu_env *env,
2826 struct mdd_object *mdd_obj,
2827 struct thandle *handle)
2831 /* XXX: max size? */
2832 rc = mdo_declare_xattr_set(env, mdd_obj,
2833 mdd_buf_get_const(env, NULL, 4096),
2834 XATTR_NAME_LINK, 0, handle);
2839 /* For pathologic linkers, we don't want to spend lots of time scanning the
2840 * link ea. Limit ourseleves to something reasonable; links not in the EA
2841 * can be looked up via (slower) parent lookup.
2843 #define LINKEA_MAX_COUNT 128
2845 static int mdd_links_add(const struct lu_env *env,
2846 struct mdd_object *mdd_obj,
2847 const struct lu_fid *pfid,
2848 const struct lu_name *lname,
2849 struct thandle *handle, int first)
2852 struct link_ea_header *leh;
2856 if (!mdd_linkea_enable)
2859 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2862 if (rc != -ENODATA) {
2863 CERROR("link_ea read failed %d "DFID"\n", rc,
2864 PFID(mdd_object_fid(mdd_obj)));
2867 /* empty EA; start one */
2868 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2869 if (buf->lb_buf == NULL)
2872 leh->leh_magic = LINK_EA_MAGIC;
2873 leh->leh_len = sizeof(struct link_ea_header);
2874 leh->leh_reccount = 0;
2878 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2881 rc = __mdd_links_add(env, buf, pfid, lname);
2886 rc = __mdd_xattr_set(env, mdd_obj,
2887 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2888 XATTR_NAME_LINK, 0, handle);
2891 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2892 PFID(mdd_object_fid(mdd_obj)));
2894 CERROR("link_ea add failed %d "DFID"\n", rc,
2895 PFID(mdd_object_fid(mdd_obj)));
2898 if (buf->lb_len > OBD_ALLOC_BIG)
2899 /* if we vmalloced a large buffer drop it */
2905 static int mdd_links_rename(const struct lu_env *env,
2906 struct mdd_object *mdd_obj,
2907 const struct lu_fid *oldpfid,
2908 const struct lu_name *oldlname,
2909 const struct lu_fid *newpfid,
2910 const struct lu_name *newlname,
2911 struct thandle *handle)
2914 struct link_ea_header *leh;
2915 struct link_ea_entry *lee;
2916 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2917 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2923 if (!mdd_linkea_enable)
2926 if (mdd_obj->mod_flags & DEAD_OBJ)
2927 /* No more links, don't bother */
2930 buf = mdd_links_get(env, mdd_obj);
2934 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2935 rc, PFID(mdd_object_fid(mdd_obj)));
2937 CERROR("link_ea read failed %d "DFID"\n",
2938 rc, PFID(mdd_object_fid(mdd_obj)));
2942 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2944 /* Find the old record */
2945 for(count = 0; count < leh->leh_reccount; count++) {
2946 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2947 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2948 lu_fid_eq(tmpfid, oldpfid) &&
2949 (strncmp(tmpname->ln_name, oldlname->ln_name,
2950 tmpname->ln_namelen) == 0))
2952 lee = (struct link_ea_entry *)((char *)lee + reclen);
2954 if ((count + 1) > leh->leh_reccount) {
2955 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2956 oldlname->ln_namelen, oldlname->ln_name);
2957 GOTO(out, rc = -ENOENT);
2960 /* Remove the old record */
2961 leh->leh_reccount--;
2962 leh->leh_len -= reclen;
2963 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2966 /* If renaming, add the new record */
2967 if (newpfid != NULL) {
2968 /* if the add fails, we still delete the out-of-date old link */
2969 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2973 rc = __mdd_xattr_set(env, mdd_obj,
2974 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2975 XATTR_NAME_LINK, 0, handle);
2981 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2982 oldlname->ln_namelen, oldlname->ln_name, rc,
2983 PFID(mdd_object_fid(mdd_obj)));
2985 if (buf->lb_len > OBD_ALLOC_BIG)
2986 /* if we vmalloced a large buffer drop it */
2992 const struct md_dir_operations mdd_dir_ops = {
2993 .mdo_is_subdir = mdd_is_subdir,
2994 .mdo_lookup = mdd_lookup,
2995 .mdo_create = mdd_create,
2996 .mdo_rename = mdd_rename,
2997 .mdo_link = mdd_link,
2998 .mdo_unlink = mdd_unlink,
2999 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
3000 .mdo_name_insert = mdd_name_insert,
3001 .mdo_name_remove = mdd_name_remove,
3002 .mdo_rename_tgt = mdd_rename_tgt,
3003 .mdo_create_data = mdd_create_data,