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 += fname->ln_namelen;
629 reclen = llog_data_len(reclen);
631 return mdd_declare_llog_record(env, mdd, reclen, handle);
634 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
635 struct mdd_device *mdd,
636 const struct lu_name *tname,
637 const struct lu_name *sname,
638 struct thandle *handle)
643 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
646 /* we'll be writing payload + llog header */
647 reclen = sizeof(struct llog_changelog_ext_rec);
649 reclen += tname->ln_namelen;
651 reclen += 1 + sname->ln_namelen;
652 reclen = llog_data_len(reclen);
654 return mdd_declare_llog_record(env, mdd, reclen, handle);
657 /** Store a namespace change changelog record
658 * If this fails, we must fail the whole transaction; we don't
659 * want the change to commit without the log entry.
660 * \param target - mdd_object of change
661 * \param parent - parent dir/object
662 * \param tname - target name string
663 * \param handle - transacion handle
665 static int mdd_changelog_ns_store(const struct lu_env *env,
666 struct mdd_device *mdd,
667 enum changelog_rec_type type,
669 struct mdd_object *target,
670 struct mdd_object *parent,
671 const struct lu_name *tname,
672 struct thandle *handle)
674 struct llog_changelog_rec *rec;
681 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
683 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
686 LASSERT(target != NULL);
687 LASSERT(parent != NULL);
688 LASSERT(tname != NULL);
689 LASSERT(handle != NULL);
691 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
692 buf = mdd_buf_alloc(env, reclen);
693 if (buf->lb_buf == NULL)
695 rec = (struct llog_changelog_rec *)buf->lb_buf;
697 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
698 rec->cr.cr_type = (__u32)type;
699 rec->cr.cr_tfid = *mdo2fid(target);
700 rec->cr.cr_pfid = *mdo2fid(parent);
701 rec->cr.cr_namelen = tname->ln_namelen;
702 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
704 target->mod_cltime = cfs_time_current_64();
706 rc = mdd_changelog_llog_write(mdd, rec, handle);
708 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
709 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
710 PFID(&rec->cr.cr_pfid));
718 /** Store a namespace change changelog record
719 * If this fails, we must fail the whole transaction; we don't
720 * want the change to commit without the log entry.
721 * \param target - mdd_object of change
722 * \param tpfid - target parent dir/object fid
723 * \param sfid - source object fid
724 * \param spfid - source parent fid
725 * \param tname - target name string
726 * \param sname - source name string
727 * \param handle - transacion handle
729 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
730 struct mdd_device *mdd,
731 enum changelog_rec_type type,
733 struct mdd_object *target,
734 const struct lu_fid *tpfid,
735 const struct lu_fid *sfid,
736 const struct lu_fid *spfid,
737 const struct lu_name *tname,
738 const struct lu_name *sname,
739 struct thandle *handle)
741 struct llog_changelog_ext_rec *rec;
748 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
750 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
753 LASSERT(sfid != NULL);
754 LASSERT(tpfid != NULL);
755 LASSERT(tname != NULL);
756 LASSERT(handle != NULL);
758 reclen = sizeof(*rec) + tname->ln_namelen;
760 reclen += 1 + sname->ln_namelen;
761 reclen = llog_data_len(reclen);
762 buf = mdd_buf_alloc(env, reclen);
763 if (buf->lb_buf == NULL)
765 rec = (struct llog_changelog_ext_rec *)buf->lb_buf;
767 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
768 rec->cr.cr_type = (__u32)type;
769 rec->cr.cr_pfid = *tpfid;
770 rec->cr.cr_sfid = *sfid;
771 rec->cr.cr_spfid = *spfid;
772 rec->cr.cr_namelen = tname->ln_namelen;
773 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
775 LASSERT(sfid != NULL);
776 rec->cr.cr_name[tname->ln_namelen] = '\0';
777 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
779 rec->cr.cr_namelen += 1 + sname->ln_namelen;
782 if (likely(target != NULL)) {
783 rec->cr.cr_tfid = *mdo2fid(target);
784 target->mod_cltime = cfs_time_current_64();
786 fid_zero(&rec->cr.cr_tfid);
789 rc = mdd_changelog_ext_llog_write(mdd, rec, handle);
791 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
792 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
799 static int mdd_declare_link(const struct lu_env *env,
800 struct mdd_device *mdd,
801 struct mdd_object *p,
802 struct mdd_object *c,
803 const struct lu_name *name,
804 struct thandle *handle)
808 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
812 rc = mdo_declare_ref_add(env, c, handle);
816 rc = mdo_declare_attr_set(env, p, NULL, handle);
820 rc = mdo_declare_attr_set(env, c, NULL, handle);
824 rc = mdd_declare_links_add(env, c, handle);
828 rc = mdd_declare_changelog_store(env, mdd, name, handle);
833 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
834 struct md_object *src_obj, const struct lu_name *lname,
837 const char *name = lname->ln_name;
838 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
839 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
840 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
841 struct mdd_device *mdd = mdo2mdd(src_obj);
842 struct dynlock_handle *dlh;
843 struct thandle *handle;
844 #ifdef HAVE_QUOTA_SUPPORT
845 struct obd_device *obd = mdd->mdd_obd_dev;
846 struct obd_export *exp = md_quota(env)->mq_exp;
847 struct mds_obd *mds = &obd->u.mds;
848 unsigned int qids[MAXQUOTAS] = { 0, 0 };
849 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
854 #ifdef HAVE_QUOTA_SUPPORT
855 if (mds->mds_quota) {
856 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
858 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
861 mdd_data_get(env, mdd_tobj, &data);
862 quota_opc = FSFILT_OP_LINK;
863 mdd_quota_wrapper(la_tmp, qids);
864 /* get block quota for parent */
865 lquota_chkquota(mds_quota_interface_ref, obd, exp,
866 qids, rec_pending, 1, NULL,
867 LQUOTA_FLAGS_BLK, data, 1);
872 handle = mdd_trans_create(env, mdd);
874 GOTO(out_pending, rc = PTR_ERR(handle));
876 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
880 rc = mdd_trans_start(env, mdd, handle);
884 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
886 GOTO(out_trans, rc = -ENOMEM);
887 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
889 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
891 GOTO(out_unlock, rc);
893 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
895 mdd_object_capa(env, mdd_tobj));
897 GOTO(out_unlock, rc);
899 rc = mdo_ref_add(env, mdd_sobj, handle);
901 __mdd_index_delete_only(env, mdd_tobj, name, handle,
902 mdd_object_capa(env, mdd_tobj));
903 GOTO(out_unlock, rc);
906 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
907 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
909 la->la_valid = LA_CTIME | LA_MTIME;
910 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
912 GOTO(out_unlock, rc);
914 la->la_valid = LA_CTIME;
915 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
917 mdd_links_add(env, mdd_sobj,
918 mdo2fid(mdd_tobj), lname, handle, 0);
923 mdd_write_unlock(env, mdd_sobj);
924 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
927 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
928 mdd_tobj, lname, handle);
930 mdd_trans_stop(env, mdd, rc, handle);
932 #ifdef HAVE_QUOTA_SUPPORT
934 lquota_pending_commit(mds_quota_interface_ref, obd,
935 qids, rec_pending, 1);
936 /* Trigger dqacq for the parent owner. If failed,
937 * the next call for lquota_chkquota will process it. */
938 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
945 int mdd_declare_finish_unlink(const struct lu_env *env,
946 struct mdd_object *obj,
948 struct thandle *handle)
952 rc = orph_declare_index_insert(env, obj, handle);
956 return mdd_declare_object_kill(env, obj, ma, handle);
959 /* caller should take a lock before calling */
960 int mdd_finish_unlink(const struct lu_env *env,
961 struct mdd_object *obj, struct md_attr *ma,
966 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
969 LASSERT(mdd_write_locked(env, obj) != 0);
971 /* read HSM flags, needed to set changelogs flags */
972 ma->ma_need = MA_HSM | MA_INODE;
973 rc = mdd_attr_get_internal(env, obj, ma);
974 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
975 obj->mod_flags |= DEAD_OBJ;
976 /* add new orphan and the object
977 * will be deleted during mdd_close() */
978 if (obj->mod_count) {
979 rc = __mdd_orphan_add(env, obj, th);
981 CDEBUG(D_HA, "Object "DFID" is inserted into "
982 "orphan list, open count = %d\n",
983 PFID(mdd_object_fid(obj)),
986 CERROR("Object "DFID" fail to be an orphan, "
987 "open count = %d, maybe cause failed "
989 PFID(mdd_object_fid(obj)),
992 rc = mdd_object_kill(env, obj, ma, th);
997 /* get the i_nlink */
998 ma->ma_need = MA_INODE;
999 rc = mdd_attr_get_internal(env, obj, ma);
1002 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1009 * has mdd_write_lock on cobj already, but not on pobj yet
1011 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1012 struct mdd_object *cobj, struct md_attr *ma)
1017 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
1022 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1023 struct mdd_object *p, struct mdd_object *c,
1024 const struct lu_name *name, struct md_attr *ma,
1025 struct thandle *handle)
1029 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1033 rc = mdo_declare_ref_del(env, p, handle);
1037 rc = mdo_declare_ref_del(env, c, handle);
1041 rc = mdo_declare_ref_del(env, c, handle);
1045 rc = mdo_declare_attr_set(env, p, NULL, handle);
1049 rc = mdo_declare_attr_set(env, c, NULL, handle);
1053 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1057 rc = mdd_declare_links_add(env, c, handle);
1061 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1066 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1067 struct md_object *cobj, const struct lu_name *lname,
1070 const char *name = lname->ln_name;
1071 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1072 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1073 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1074 struct mdd_device *mdd = mdo2mdd(pobj);
1075 struct dynlock_handle *dlh;
1076 struct thandle *handle;
1077 #ifdef HAVE_QUOTA_SUPPORT
1078 struct obd_device *obd = mdd->mdd_obd_dev;
1079 struct mds_obd *mds = &obd->u.mds;
1080 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1081 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1084 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1088 if (mdd_object_exists(mdd_cobj) <= 0)
1091 handle = mdd_trans_create(env, mdd);
1093 RETURN(PTR_ERR(handle));
1095 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1100 rc = mdd_trans_start(env, mdd, handle);
1104 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1106 GOTO(out_trans, rc = -ENOMEM);
1107 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1109 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1113 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1114 mdd_object_capa(env, mdd_pobj));
1118 rc = mdo_ref_del(env, mdd_cobj, handle);
1120 __mdd_index_insert_only(env, mdd_pobj, mdo2fid(mdd_cobj),
1122 mdd_object_capa(env, mdd_pobj));
1128 mdo_ref_del(env, mdd_cobj, handle);
1130 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1131 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1133 la->la_valid = LA_CTIME | LA_MTIME;
1134 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1138 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1139 /* update ctime of an unlinked file only if it is still
1140 * opened or a link still exists */
1141 la->la_valid = LA_CTIME;
1142 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1147 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1148 #ifdef HAVE_QUOTA_SUPPORT
1149 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1150 ma->ma_attr.la_nlink == 0) {
1151 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1153 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1155 mdd_quota_wrapper(la_tmp, qpids);
1156 if (mdd_cobj->mod_count == 0) {
1157 quota_opc = FSFILT_OP_UNLINK;
1158 mdd_quota_wrapper(&ma->ma_attr, qcids);
1160 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1166 /* old files may not have link ea; ignore errors */
1167 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1168 lname, NULL, NULL, handle);
1172 mdd_write_unlock(env, mdd_cobj);
1173 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1178 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1179 if ((ma->ma_valid & MA_HSM) &&
1180 (ma->ma_hsm.mh_flags & HS_EXISTS))
1181 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1183 rc = mdd_changelog_ns_store(env, mdd,
1184 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1185 mdd_cobj, mdd_pobj, lname, handle);
1189 mdd_trans_stop(env, mdd, rc, handle);
1190 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
1191 if (rc == 0 && ma->ma_valid & MA_COOKIE && ma->ma_valid & MA_LOV &&
1192 ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_UNLINK_DESTROY)
1193 /* Since echo client is incapable of destorying ost object,
1194 * it will destory the object here. */
1195 rc = mdd_lovobj_unlink(env, mdd, mdd_cobj, la, ma, 1);
1197 #warning "please remove this after 2.4 (LOD/OSP)."
1200 #ifdef HAVE_QUOTA_SUPPORT
1202 /* Trigger dqrel on the owner of child and parent. If failed,
1203 * the next call for lquota_chkquota will process it. */
1204 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1210 /* has not lock on pobj yet */
1211 static int mdd_ni_sanity_check(const struct lu_env *env,
1212 struct md_object *pobj,
1213 const struct md_attr *ma)
1215 struct mdd_object *obj = md2mdd_obj(pobj);
1219 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1222 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1228 * Partial operation.
1230 static int mdd_name_insert(const struct lu_env *env,
1231 struct md_object *pobj,
1232 const struct lu_name *lname,
1233 const struct lu_fid *fid,
1234 const struct md_attr *ma)
1236 const char *name = lname->ln_name;
1237 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1238 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1239 struct dynlock_handle *dlh;
1240 struct thandle *handle;
1241 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1242 #ifdef HAVE_QUOTA_SUPPORT
1243 struct mdd_device *mdd = mdo2mdd(pobj);
1244 struct md_ucred *uc = md_ucred(env);
1245 struct obd_device *obd = mdd->mdd_obd_dev;
1246 struct obd_export *exp = md_quota(env)->mq_exp;
1247 struct mds_obd *mds = &obd->u.mds;
1248 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1249 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1250 cfs_cap_t save = uc->mu_cap;
1255 /* XXX: this code won't be used ever:
1256 * DNE uses slightly different approach */
1259 #ifdef HAVE_QUOTA_SUPPORT
1260 if (mds->mds_quota) {
1261 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1262 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1264 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1267 mdd_data_get(env, mdd_obj, &data);
1268 quota_opc = FSFILT_OP_LINK;
1269 mdd_quota_wrapper(la_tmp, qids);
1270 /* get block quota for parent */
1271 lquota_chkquota(mds_quota_interface_ref, obd,
1272 exp, qids, rec_pending, 1, NULL,
1273 LQUOTA_FLAGS_BLK, data, 1);
1276 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1280 handle = mdd_trans_create(env, mdo2mdd(pobj));
1282 GOTO(out_pending, rc = PTR_ERR(handle));
1284 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1286 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1288 GOTO(out_trans, rc = -ENOMEM);
1290 rc = mdd_ni_sanity_check(env, pobj, ma);
1292 GOTO(out_unlock, rc);
1294 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1295 handle, BYPASS_CAPA);
1297 GOTO(out_unlock, rc);
1300 * For some case, no need update obj's ctime (LA_CTIME is not set),
1302 * For other cases, update obj's ctime (LA_CTIME is set),
1305 if (ma->ma_attr.la_valid & LA_CTIME) {
1306 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1307 la->la_valid = LA_CTIME | LA_MTIME;
1308 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1313 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1315 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1317 #ifdef HAVE_QUOTA_SUPPORT
1318 if (mds->mds_quota) {
1320 lquota_pending_commit(mds_quota_interface_ref,
1321 obd, qids, rec_pending, 1);
1322 /* Trigger dqacq for the parent owner. If failed,
1323 * the next call for lquota_chkquota will process it*/
1324 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1334 /* has not lock on pobj yet */
1335 static int mdd_nr_sanity_check(const struct lu_env *env,
1336 struct md_object *pobj,
1337 const struct md_attr *ma)
1339 struct mdd_object *obj = md2mdd_obj(pobj);
1343 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1346 rc = mdd_may_unlink(env, obj, ma);
1352 * Partial operation.
1354 static int mdd_name_remove(const struct lu_env *env,
1355 struct md_object *pobj,
1356 const struct lu_name *lname,
1357 const struct md_attr *ma)
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_obj = md2mdd_obj(pobj);
1362 struct mdd_device *mdd = mdo2mdd(pobj);
1363 struct dynlock_handle *dlh;
1364 struct thandle *handle;
1365 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1366 #ifdef HAVE_QUOTA_SUPPORT
1367 struct obd_device *obd = mdd->mdd_obd_dev;
1368 struct mds_obd *mds = &obd->u.mds;
1369 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1375 /* XXX: this code won't be used ever:
1376 * DNE uses slightly different approach */
1379 #ifdef HAVE_QUOTA_SUPPORT
1380 if (mds->mds_quota) {
1381 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1383 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1385 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1386 mdd_quota_wrapper(la_tmp, qids);
1390 handle = mdd_trans_create(env, mdd);
1392 GOTO(out_pending, rc = PTR_ERR(handle));
1394 rc = mdd_trans_start(env, mdd, handle);
1396 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1398 GOTO(out_trans, rc = -ENOMEM);
1400 rc = mdd_nr_sanity_check(env, pobj, ma);
1402 GOTO(out_unlock, rc);
1404 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1405 handle, BYPASS_CAPA);
1407 GOTO(out_unlock, rc);
1410 * For some case, no need update obj's ctime (LA_CTIME is not set),
1412 * For other cases, update obj's ctime (LA_CTIME is set),
1415 if (ma->ma_attr.la_valid & LA_CTIME) {
1416 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1417 la->la_valid = LA_CTIME | LA_MTIME;
1418 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1423 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1425 mdd_trans_stop(env, mdd, rc, handle);
1427 #ifdef HAVE_QUOTA_SUPPORT
1428 /* Trigger dqrel for the parent owner.
1429 * If failed, the next call for lquota_chkquota will process it. */
1431 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1439 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1441 static int mdd_rt_sanity_check(const struct lu_env *env,
1442 struct mdd_object *tgt_pobj,
1443 struct mdd_object *tobj,
1449 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1452 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1453 * exist. In fact, tobj must exist, otherwise the call trace will be:
1454 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1455 * When get here, tobj must be NOT NULL, the other case has been
1456 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1458 * So check may_delete, but not check nlink of tgt_pobj. */
1460 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1465 /* Partial rename op on slave MDD */
1466 static int mdd_rename_tgt(const struct lu_env *env,
1467 struct md_object *pobj, struct md_object *tobj,
1468 const struct lu_fid *lf, const struct lu_name *lname,
1471 const char *name = lname->ln_name;
1472 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1473 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1474 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1475 struct mdd_device *mdd = mdo2mdd(pobj);
1476 struct dynlock_handle *dlh;
1477 struct thandle *handle;
1478 #ifdef HAVE_QUOTA_SUPPORT
1479 struct obd_device *obd = mdd->mdd_obd_dev;
1480 struct obd_export *exp = md_quota(env)->mq_exp;
1481 struct mds_obd *mds = &obd->u.mds;
1482 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1483 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1484 int quota_copc = 0, quota_popc = 0;
1485 int rec_pending[MAXQUOTAS] = { 0, 0 };
1491 /* XXX: this code won't be used ever:
1492 * DNE uses slightly different approach */
1495 #ifdef HAVE_QUOTA_SUPPORT
1496 if (mds->mds_quota && !tobj) {
1497 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1499 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1502 mdd_data_get(env, mdd_tpobj, &data);
1503 quota_popc = FSFILT_OP_LINK;
1504 mdd_quota_wrapper(la_tmp, qpids);
1505 /* get block quota for target parent */
1506 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1507 qpids, rec_pending, 1, NULL,
1508 LQUOTA_FLAGS_BLK, data, 1);
1512 handle = mdd_trans_create(env, mdd);
1514 GOTO(out_pending, rc = PTR_ERR(handle));
1516 rc = mdd_trans_start(env, mdd, handle);
1518 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1520 GOTO(out_trans, rc = -ENOMEM);
1522 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1524 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1529 * If rename_tgt is called then we should just re-insert name with
1530 * correct fid, no need to dec/inc parent nlink if obj is dir.
1532 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1536 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1541 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1542 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1544 la->la_valid = LA_CTIME | LA_MTIME;
1545 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1550 * For tobj is remote case cmm layer has processed
1551 * and pass NULL tobj to here. So when tobj is NOT NULL,
1552 * it must be local one.
1554 if (tobj && mdd_object_exists(mdd_tobj)) {
1555 mdo_ref_del(env, mdd_tobj, handle);
1557 /* Remove dot reference. */
1558 if (S_ISDIR(ma->ma_attr.la_mode))
1559 mdo_ref_del(env, mdd_tobj, handle);
1561 la->la_valid = LA_CTIME;
1562 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1566 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1570 if (ma->ma_valid & MA_INODE && ma->ma_attr.la_nlink == 0) {
1571 cl_flags |= CLF_RENAME_LAST;
1572 #ifdef HAVE_QUOTA_SUPPORT
1573 if (mds->mds_quota && mdd_tobj->mod_count == 0) {
1574 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1575 mdd_quota_wrapper(&ma->ma_attr, qcids);
1583 mdd_write_unlock(env, mdd_tobj);
1584 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1587 /* Bare EXT record with no RENAME in front of it signifies
1588 a partial slave op */
1589 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, cl_flags,
1590 mdd_tobj, mdd_tpobj, lname, handle);
1592 mdd_trans_stop(env, mdd, rc, handle);
1594 #ifdef HAVE_QUOTA_SUPPORT
1595 if (mds->mds_quota) {
1597 lquota_pending_commit(mds_quota_interface_ref, obd,
1598 qpids, rec_pending, 1);
1601 /* Trigger dqrel on the target owner of child.
1602 * If failed, the next call for lquota_chkquota
1603 * will process it. */
1604 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1612 * The permission has been checked when obj created, no need check again.
1614 static int mdd_cd_sanity_check(const struct lu_env *env,
1615 struct mdd_object *obj)
1620 if (!obj || mdd_is_dead_obj(obj))
1627 static int mdd_declare_create_data(const struct lu_env *env,
1628 struct mdd_device *mdd,
1629 struct mdd_object *obj,
1631 struct thandle *handle)
1633 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1637 buf->lb_len = lmm_size;
1638 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1643 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1648 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1649 struct md_object *cobj, const struct md_op_spec *spec,
1652 struct mdd_device *mdd = mdo2mdd(cobj);
1653 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1654 struct mdd_object *son = md2mdd_obj(cobj);
1655 struct lov_mds_md *lmm = NULL;
1657 struct thandle *handle;
1661 rc = mdd_cd_sanity_check(env, son);
1665 if (!md_should_create(spec->sp_cr_flags))
1667 lmm_size = ma->ma_lmm_size;
1669 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1673 handle = mdd_trans_create(env, mdd);
1675 GOTO(out_free, rc = PTR_ERR(handle));
1677 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1681 rc = mdd_trans_start(env, mdd, handle);
1686 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1687 * Should this be fixed?
1690 /* Replay creates has objects already */
1692 if (spec->no_create) {
1693 CDEBUG(D_INFO, "we already have lov ea\n");
1694 rc = mdd_lov_set_md(env, mdd_pobj, son,
1695 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1696 spec->u.sp_ea.eadatalen, handle, 0);
1699 /* No need mdd_lsm_sanity_check here */
1700 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1701 lmm_size, handle, 0);
1704 rc = mdd_attr_get_internal_locked(env, son, ma);
1706 /* update lov_objid data, must be before transaction stop! */
1708 mdd_lov_objid_update(mdd, lmm);
1711 mdd_trans_stop(env, mdd, rc, handle);
1713 /* Finish mdd_lov_create() stuff. */
1714 /* if no_create == 0 (not replay), we free lmm allocated by
1715 * mdd_lov_create() */
1716 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1720 /* Get fid from name and parent */
1722 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1723 const struct lu_name *lname, struct lu_fid* fid, int mask)
1725 const char *name = lname->ln_name;
1726 const struct dt_key *key = (const struct dt_key *)name;
1727 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1728 struct mdd_device *m = mdo2mdd(pobj);
1729 struct dt_object *dir = mdd_object_child(mdd_obj);
1733 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1736 rc = mdd_object_exists(mdd_obj);
1737 if (unlikely(rc == 0))
1739 else if (unlikely(rc < 0)) {
1740 CERROR("Object "DFID" locates on remote server\n",
1741 PFID(mdo2fid(mdd_obj)));
1745 /* The common filename length check. */
1746 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1747 RETURN(-ENAMETOOLONG);
1749 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1754 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1755 dt_try_as_dir(env, dir))) {
1757 rc = dir->do_index_ops->dio_lookup(env, dir,
1758 (struct dt_rec *)fid, key,
1759 mdd_object_capa(env, mdd_obj));
1770 int mdd_declare_object_initialize(const struct lu_env *env,
1771 struct mdd_object *child,
1773 struct thandle *handle)
1777 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1778 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1779 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1782 rc = mdo_declare_ref_add(env, child, handle);
1785 mdd_declare_links_add(env, child, handle);
1790 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1791 const struct lu_name *lname, struct mdd_object *child,
1792 struct md_attr *ma, struct thandle *handle,
1793 const struct md_op_spec *spec)
1799 * Update attributes for child.
1802 * (1) the valid bits should be converted between Lustre and Linux;
1803 * (2) maybe, the child attributes should be set in OSD when creation.
1806 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1810 if (S_ISDIR(ma->ma_attr.la_mode)) {
1811 /* Add "." and ".." for newly created dir */
1812 mdo_ref_add(env, child, handle);
1813 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1814 dot, handle, BYPASS_CAPA);
1816 rc = __mdd_index_insert_only(env, child, pfid,
1820 mdo_ref_del(env, child, handle);
1823 mdd_links_add(env, child, pfid, lname, handle, 1);
1828 /* has not lock on pobj yet */
1829 static int mdd_create_sanity_check(const struct lu_env *env,
1830 struct md_object *pobj,
1831 const struct lu_name *lname,
1833 struct md_op_spec *spec)
1835 struct mdd_thread_info *info = mdd_env_info(env);
1836 struct lu_attr *la = &info->mti_la;
1837 struct lu_fid *fid = &info->mti_fid;
1838 struct mdd_object *obj = md2mdd_obj(pobj);
1839 struct mdd_device *m = mdo2mdd(pobj);
1840 int lookup = spec->sp_cr_lookup;
1845 if (mdd_is_dead_obj(obj))
1849 * In some cases this lookup is not needed - we know before if name
1850 * exists or not because MDT performs lookup for it.
1851 * name length check is done in lookup.
1855 * Check if the name already exist, though it will be checked in
1856 * _index_insert also, for avoiding rolling back if exists
1859 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1860 MAY_WRITE | MAY_EXEC);
1862 RETURN(rc ? : -EEXIST);
1865 * Check WRITE permission for the parent.
1866 * EXEC permission have been checked
1867 * when lookup before create already.
1869 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1876 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1880 if (la->la_mode & S_ISGID) {
1881 ma->ma_attr.la_gid = la->la_gid;
1882 if (S_ISDIR(ma->ma_attr.la_mode)) {
1883 ma->ma_attr.la_mode |= S_ISGID;
1884 ma->ma_attr.la_valid |= LA_MODE;
1888 switch (ma->ma_attr.la_mode & S_IFMT) {
1890 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1892 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1893 RETURN(-ENAMETOOLONG);
1912 static int mdd_declare_create(const struct lu_env *env,
1913 struct mdd_device *mdd,
1914 struct mdd_object *p,
1915 struct mdd_object *c,
1916 const struct lu_name *name,
1919 struct thandle *handle,
1920 const struct md_op_spec *spec)
1922 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1925 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1929 /* if dir, then can inherit default ACl */
1931 buf->lb_len = lmm_size;
1932 if (S_ISDIR(ma->ma_attr.la_mode)) {
1933 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1936 rc = mdo_declare_ref_add(env, p, handle);
1941 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1946 rc = mdd_declare_object_initialize(env, c, ma, handle);
1950 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1951 name->ln_name, handle);
1955 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1960 if (S_ISLNK(ma->ma_attr.la_mode)) {
1961 rc = dt_declare_record_write(env, mdd_object_child(c),
1962 strlen(spec->u.sp_symname), 0,
1967 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1971 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1975 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1983 * Create object and insert it into namespace.
1985 static int mdd_create(const struct lu_env *env,
1986 struct md_object *pobj,
1987 const struct lu_name *lname,
1988 struct md_object *child,
1989 struct md_op_spec *spec,
1992 struct mdd_thread_info *info = mdd_env_info(env);
1993 struct lu_attr *la = &info->mti_la_for_fix;
1994 struct md_attr *ma_acl = &info->mti_ma;
1995 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1996 struct mdd_object *son = md2mdd_obj(child);
1997 struct mdd_device *mdd = mdo2mdd(pobj);
1998 struct lu_attr *attr = &ma->ma_attr;
1999 struct lov_mds_md *lmm = NULL;
2000 struct thandle *handle;
2001 struct dynlock_handle *dlh;
2002 const char *name = lname->ln_name;
2003 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
2004 int got_def_acl = 0;
2005 #ifdef HAVE_QUOTA_SUPPORT
2006 struct obd_device *obd = mdd->mdd_obd_dev;
2007 struct obd_export *exp = md_quota(env)->mq_exp;
2008 struct mds_obd *mds = &obd->u.mds;
2009 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
2010 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
2011 int quota_opc = 0, block_count = 0;
2012 int inode_pending[MAXQUOTAS] = { 0, 0 };
2013 int block_pending[MAXQUOTAS] = { 0, 0 };
2014 int parent_pending[MAXQUOTAS] = { 0, 0 };
2019 * Two operations have to be performed:
2021 * - an allocation of a new object (->do_create()), and
2023 * - an insertion into a parent index (->dio_insert()).
2025 * Due to locking, operation order is not important, when both are
2026 * successful, *but* error handling cases are quite different:
2028 * - if insertion is done first, and following object creation fails,
2029 * insertion has to be rolled back, but this operation might fail
2030 * also leaving us with dangling index entry.
2032 * - if creation is done first, is has to be undone if insertion
2033 * fails, leaving us with leaked space, which is neither good, nor
2036 * It seems that creation-first is simplest solution, but it is
2037 * sub-optimal in the frequent
2042 * case, because second mkdir is bound to create object, only to
2043 * destroy it immediately.
2045 * To avoid this follow local file systems that do double lookup:
2047 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2049 * 1. create (mdd_object_create_internal())
2051 * 2. insert (__mdd_index_insert(), lookup again)
2054 /* Sanity checks before big job. */
2055 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
2059 #ifdef HAVE_QUOTA_SUPPORT
2060 if (mds->mds_quota) {
2061 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2063 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
2067 quota_opc = FSFILT_OP_CREATE;
2068 mdd_quota_wrapper(&ma->ma_attr, qcids);
2069 mdd_quota_wrapper(la_tmp, qpids);
2070 /* get file quota for child */
2071 lquota_chkquota(mds_quota_interface_ref, obd, exp,
2072 qcids, inode_pending, 1, NULL, 0, NULL,
2074 switch (ma->ma_attr.la_mode & S_IFMT) {
2083 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
2084 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
2088 /* get block quota for child and parent */
2090 lquota_chkquota(mds_quota_interface_ref, obd,
2091 exp, qcids, block_pending,
2093 LQUOTA_FLAGS_BLK, NULL, 0);
2095 lquota_chkquota(mds_quota_interface_ref, obd,
2096 exp, qpids, parent_pending, 1,
2097 NULL, LQUOTA_FLAGS_BLK, NULL,
2103 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2104 GOTO(out_pending, rc = -EINPROGRESS);
2107 * No RPC inside the transaction, so OST objects should be created at
2110 if (S_ISREG(attr->la_mode)) {
2111 lmm_size = ma->ma_lmm_size;
2112 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
2115 GOTO(out_pending, rc);
2118 if (!S_ISLNK(attr->la_mode)) {
2119 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2120 ma_acl->ma_acl = info->mti_xattr_buf;
2121 ma_acl->ma_need = MA_ACL_DEF;
2122 ma_acl->ma_valid = 0;
2124 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2125 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2126 mdd_read_unlock(env, mdd_pobj);
2129 else if (ma_acl->ma_valid & MA_ACL_DEF)
2133 handle = mdd_trans_create(env, mdd);
2135 GOTO(out_free, rc = PTR_ERR(handle));
2137 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2138 lmm_size, handle, spec);
2142 rc = mdd_trans_start(env, mdd, handle);
2146 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2148 GOTO(out_trans, rc = -ENOMEM);
2150 mdd_write_lock(env, son, MOR_TGT_CHILD);
2151 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2153 mdd_write_unlock(env, son);
2159 #ifdef CONFIG_FS_POSIX_ACL
2161 struct lu_buf *acl_buf = &info->mti_buf;
2162 acl_buf->lb_buf = ma_acl->ma_acl;
2163 acl_buf->lb_len = ma_acl->ma_acl_size;
2165 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2167 mdd_write_unlock(env, son);
2170 ma->ma_attr.la_valid |= LA_MODE;
2175 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2176 son, ma, handle, spec);
2177 mdd_write_unlock(env, son);
2180 * Object has no links, so it will be destroyed when last
2181 * reference is released. (XXX not now.)
2187 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2188 name, S_ISDIR(attr->la_mode), handle,
2189 mdd_object_capa(env, mdd_pobj));
2196 /* No need mdd_lsm_sanity_check here */
2197 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2199 CERROR("error on stripe info copy %d \n", rc);
2202 if (lmm && lmm_size > 0) {
2203 /* Set Lov here, do not get lmm again later */
2204 if (lmm_size > ma->ma_lmm_size) {
2205 /* Reply buffer is smaller, need bigger one */
2206 mdd_max_lmm_buffer(env, lmm_size);
2207 if (unlikely(info->mti_max_lmm == NULL))
2208 GOTO(cleanup, rc = -ENOMEM);
2209 ma->ma_lmm = info->mti_max_lmm;
2210 ma->ma_big_lmm_used = 1;
2212 memcpy(ma->ma_lmm, lmm, lmm_size);
2213 ma->ma_lmm_size = lmm_size;
2214 ma->ma_valid |= MA_LOV;
2217 if (S_ISLNK(attr->la_mode)) {
2218 struct md_ucred *uc = md_ucred(env);
2219 struct dt_object *dt = mdd_object_child(son);
2220 const char *target_name = spec->u.sp_symname;
2221 int sym_len = strlen(target_name);
2222 const struct lu_buf *buf;
2225 buf = mdd_buf_get_const(env, target_name, sym_len);
2226 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2227 mdd_object_capa(env, son),
2229 CFS_CAP_SYS_RESOURCE_MASK);
2234 GOTO(cleanup, rc = -EFAULT);
2238 la->la_valid = LA_CTIME | LA_MTIME;
2239 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2243 /* Return attr back. */
2244 rc = mdd_attr_get_internal_locked(env, son, ma);
2247 if (rc != 0 && created != 0) {
2250 if (inserted != 0) {
2251 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2252 S_ISDIR(attr->la_mode),
2253 handle, BYPASS_CAPA);
2258 mdd_write_lock(env, son, MOR_TGT_CHILD);
2259 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2260 /* Drop the reference, no need to delete "."/"..",
2261 * because the object to be destroied directly. */
2262 rc2 = mdo_ref_del(env, son, handle);
2264 mdd_write_unlock(env, son);
2269 rc2 = mdo_ref_del(env, son, handle);
2271 mdd_write_unlock(env, son);
2275 mdo_destroy(env, son, handle);
2276 mdd_write_unlock(env, son);
2279 /* update lov_objid data, must be before transaction stop! */
2281 mdd_lov_objid_update(mdd, lmm);
2283 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2286 rc = mdd_changelog_ns_store(env, mdd,
2287 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2288 S_ISREG(attr->la_mode) ? CL_CREATE :
2289 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2290 0, son, mdd_pobj, lname, handle);
2292 mdd_trans_stop(env, mdd, rc, handle);
2294 /* finish lov_create stuff, free all temporary data */
2295 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2297 #ifdef HAVE_QUOTA_SUPPORT
2299 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2301 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2303 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2305 /* Trigger dqacq on the owner of child and parent. If failed,
2306 * the next call for lquota_chkquota will process it. */
2307 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2311 /* The child object shouldn't be cached anymore */
2313 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
2314 &child->mo_lu.lo_header->loh_flags);
2319 * Get locks on parents in proper order
2320 * RETURN: < 0 - error, rename_order if successful
2328 static int mdd_rename_order(const struct lu_env *env,
2329 struct mdd_device *mdd,
2330 struct mdd_object *src_pobj,
2331 struct mdd_object *tgt_pobj)
2333 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2337 if (src_pobj == tgt_pobj)
2338 RETURN(MDD_RN_SAME);
2340 /* compared the parent child relationship of src_p&tgt_p */
2341 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2343 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2346 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2359 /* has not mdd_write{read}_lock on any obj yet. */
2360 static int mdd_rename_sanity_check(const struct lu_env *env,
2361 struct mdd_object *src_pobj,
2362 struct mdd_object *tgt_pobj,
2363 struct mdd_object *sobj,
2364 struct mdd_object *tobj,
2370 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2373 /* XXX: when get here, sobj must NOT be NULL,
2374 * the other case has been processed in cml_rename
2375 * before mdd_rename and enable MDS_PERM_BYPASS. */
2378 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2382 /* XXX: when get here, "tobj == NULL" means tobj must
2383 * NOT exist (neither on remote MDS, such case has been
2384 * processed in cml_rename before mdd_rename and enable
2386 * So check may_create, but not check may_unlink. */
2388 rc = mdd_may_create(env, tgt_pobj, NULL,
2389 (src_pobj != tgt_pobj), 0);
2391 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2392 (src_pobj != tgt_pobj), 1);
2394 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2395 S_ISDIR(ma->ma_attr.la_mode))
2396 rc = __mdd_may_link(env, tgt_pobj);
2401 static int mdd_declare_rename(const struct lu_env *env,
2402 struct mdd_device *mdd,
2403 struct mdd_object *mdd_spobj,
2404 struct mdd_object *mdd_tpobj,
2405 struct mdd_object *mdd_sobj,
2406 struct mdd_object *mdd_tobj,
2407 const struct lu_name *tname,
2408 const struct lu_name *sname,
2410 struct thandle *handle)
2418 /* name from source dir */
2419 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2423 /* .. from source child */
2424 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2425 /* source child can be directory,
2426 * counted by source dir's nlink */
2427 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2431 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2435 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2440 /* new target child can be directory,
2441 * counted by target dir's nlink */
2442 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2448 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2452 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2455 mdd_declare_links_add(env, mdd_sobj, handle);
2459 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2464 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2465 tname->ln_name, handle);
2469 /* name from target dir (old name), we declare it unconditionally
2470 * as mdd_rename() calls delete unconditionally as well. so just
2471 * to balance declarations vs calls to change ... */
2472 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2476 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2477 /* delete target child in target parent directory */
2478 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2482 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2483 /* target child can be directory,
2484 * delete "." reference in target child directory */
2485 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2489 /* delete ".." reference in target parent directory */
2490 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2495 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2499 mdd_declare_links_add(env, mdd_tobj, handle);
2503 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2508 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2515 /* src object can be remote that is why we use only fid and type of object */
2516 static int mdd_rename(const struct lu_env *env,
2517 struct md_object *src_pobj, struct md_object *tgt_pobj,
2518 const struct lu_fid *lf, const struct lu_name *lsname,
2519 struct md_object *tobj, const struct lu_name *ltname,
2522 const char *sname = lsname->ln_name;
2523 const char *tname = ltname->ln_name;
2524 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2525 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2526 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2527 struct mdd_device *mdd = mdo2mdd(src_pobj);
2528 struct mdd_object *mdd_sobj = NULL; /* source object */
2529 struct mdd_object *mdd_tobj = NULL;
2530 struct dynlock_handle *sdlh, *tdlh;
2531 struct thandle *handle;
2532 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2533 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2535 unsigned cl_flags = 0;
2538 #ifdef HAVE_QUOTA_SUPPORT
2539 struct obd_device *obd = mdd->mdd_obd_dev;
2540 struct obd_export *exp = md_quota(env)->mq_exp;
2541 struct mds_obd *mds = &obd->u.mds;
2542 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2543 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2544 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2545 int quota_copc = 0, quota_popc = 0;
2546 int rec_pending[MAXQUOTAS] = { 0, 0 };
2550 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2551 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2554 mdd_tobj = md2mdd_obj(tobj);
2556 #ifdef HAVE_QUOTA_SUPPORT
2557 if (mds->mds_quota) {
2558 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2560 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2562 mdd_quota_wrapper(la_tmp, qspids);
2564 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2568 mdd_data_get(env, mdd_tpobj, &data);
2569 quota_popc = FSFILT_OP_LINK;
2570 mdd_quota_wrapper(la_tmp, qtpids);
2571 /* get block quota for target parent */
2572 lquota_chkquota(mds_quota_interface_ref,
2574 rec_pending, 1, NULL,
2582 mdd_sobj = mdd_object_find(env, mdd, lf);
2584 handle = mdd_trans_create(env, mdd);
2586 GOTO(out_pending, rc = PTR_ERR(handle));
2588 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2589 mdd_tobj, lsname, ltname, ma, handle);
2593 rc = mdd_trans_start(env, mdd, handle);
2597 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2598 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2600 GOTO(cleanup_unlocked, rc);
2602 /* Get locks in determined order */
2603 if (rc == MDD_RN_SAME) {
2604 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2605 sname, MOR_SRC_PARENT);
2606 /* check hashes to determine do we need one lock or two */
2607 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2608 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2612 } else if (rc == MDD_RN_SRCTGT) {
2613 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2614 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2616 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2617 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2619 if (sdlh == NULL || tdlh == NULL)
2620 GOTO(cleanup, rc = -ENOMEM);
2622 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2623 mdd_sobj, mdd_tobj, ma);
2627 /* Remove source name from source directory */
2628 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2629 mdd_object_capa(env, mdd_spobj));
2633 /* "mv dir1 dir2" needs "dir1/.." link update */
2634 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2635 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2636 mdd_object_capa(env, mdd_sobj));
2638 GOTO(fixup_spobj2, rc);
2640 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2641 handle, mdd_object_capa(env, mdd_sobj));
2643 GOTO(fixup_spobj, rc);
2646 /* Remove target name from target directory
2647 * Here tobj can be remote one, so we do index_delete unconditionally
2648 * and -ENOENT is allowed.
2650 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2651 mdd_object_capa(env, mdd_tpobj));
2654 /* tname might been renamed to something else */
2655 GOTO(fixup_spobj, rc);
2658 GOTO(fixup_spobj, rc);
2661 /* Insert new fid with target name into target dir */
2662 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2663 mdd_object_capa(env, mdd_tpobj));
2665 GOTO(fixup_tpobj, rc);
2667 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2668 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2670 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2672 la->la_valid = LA_CTIME;
2673 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2676 GOTO(fixup_tpobj, rc);
2679 /* Remove old target object
2680 * For tobj is remote case cmm layer has processed
2681 * and set tobj to NULL then. So when tobj is NOT NULL,
2682 * it must be local one.
2684 if (tobj && mdd_object_exists(mdd_tobj)) {
2685 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2686 if (mdd_is_dead_obj(mdd_tobj)) {
2687 mdd_write_unlock(env, mdd_tobj);
2688 /* shld not be dead, something is wrong */
2689 CERROR("tobj is dead, something is wrong\n");
2693 mdo_ref_del(env, mdd_tobj, handle);
2695 /* Remove dot reference. */
2697 mdo_ref_del(env, mdd_tobj, handle);
2699 la->la_valid = LA_CTIME;
2700 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2702 GOTO(fixup_tpobj, rc);
2704 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2705 mdd_write_unlock(env, mdd_tobj);
2707 GOTO(fixup_tpobj, rc);
2709 if (ma->ma_valid & MA_INODE && ma->ma_attr.la_nlink == 0) {
2710 cl_flags |= CLF_RENAME_LAST;
2711 #ifdef HAVE_QUOTA_SUPPORT
2712 if (mds->mds_quota && mdd_tobj->mod_count == 0) {
2713 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2714 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2720 la->la_valid = LA_CTIME | LA_MTIME;
2721 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2723 GOTO(fixup_tpobj, rc);
2725 if (mdd_spobj != mdd_tpobj) {
2726 la->la_valid = LA_CTIME | LA_MTIME;
2727 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2731 if (rc == 0 && mdd_sobj) {
2732 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2733 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2734 mdo2fid(mdd_tpobj), ltname, handle);
2736 /* Old files might not have EA entry */
2737 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2739 mdd_write_unlock(env, mdd_sobj);
2740 /* We don't fail the transaction if the link ea can't be
2741 updated -- fid2path will use alternate lookup method. */
2749 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2752 CWARN("tp obj fix error %d\n",rc2);
2754 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2755 !mdd_is_dead_obj(mdd_tobj)) {
2756 rc2 = __mdd_index_insert(env, mdd_tpobj,
2757 mdo2fid(mdd_tobj), tname,
2762 CWARN("tp obj fix error %d\n",rc2);
2767 if (rc && is_dir && mdd_sobj) {
2768 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2772 CWARN("sp obj dotdot delete error %d\n",rc2);
2775 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2776 dotdot, handle, BYPASS_CAPA);
2778 CWARN("sp obj dotdot insert error %d\n",rc2);
2783 rc2 = __mdd_index_insert(env, mdd_spobj,
2784 lf, sname, is_dir, handle, BYPASS_CAPA);
2786 CWARN("sp obj fix error %d\n",rc2);
2789 if (likely(tdlh) && sdlh != tdlh)
2790 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2792 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2795 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2796 mdd_tobj, tpobj_fid, lf,
2797 spobj_fid, ltname, lsname,
2801 mdd_trans_stop(env, mdd, rc, handle);
2803 mdd_object_put(env, mdd_sobj);
2805 #ifdef HAVE_QUOTA_SUPPORT
2806 if (mds->mds_quota) {
2808 lquota_pending_commit(mds_quota_interface_ref, obd,
2809 qtpids, rec_pending, 1);
2812 /* Trigger dqrel on the source owner of parent.
2813 * If failed, the next call for lquota_chkquota will
2815 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2816 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2818 /* Trigger dqrel on the target owner of child.
2819 * If failed, the next call for lquota_chkquota
2820 * will process it. */
2821 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2822 qtpids, rc, quota_copc);
2829 /** enable/disable storing of hardlink info */
2830 int mdd_linkea_enable = 1;
2831 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2832 "record hardlink info in EAs");
2834 /** Read the link EA into a temp buffer.
2835 * Uses the name_buf since it is generally large.
2836 * \retval IS_ERR err
2837 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2839 struct lu_buf *mdd_links_get(const struct lu_env *env,
2840 struct mdd_object *mdd_obj)
2843 struct lustre_capa *capa;
2844 struct link_ea_header *leh;
2847 /* First try a small buf */
2848 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2849 if (buf->lb_buf == NULL)
2850 return ERR_PTR(-ENOMEM);
2852 capa = mdd_object_capa(env, mdd_obj);
2853 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2854 if (rc == -ERANGE) {
2855 /* Buf was too small, figure out what we need. */
2857 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2860 buf = mdd_buf_alloc(env, rc);
2861 if (buf->lb_buf == NULL)
2862 return ERR_PTR(-ENOMEM);
2863 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2869 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2870 leh->leh_magic = LINK_EA_MAGIC;
2871 leh->leh_reccount = __swab32(leh->leh_reccount);
2872 leh->leh_len = __swab64(leh->leh_len);
2873 /* entries are swabbed by mdd_lee_unpack */
2875 if (leh->leh_magic != LINK_EA_MAGIC)
2876 return ERR_PTR(-EINVAL);
2877 if (leh->leh_reccount == 0)
2878 return ERR_PTR(-ENODATA);
2883 /** Pack a link_ea_entry.
2884 * All elements are stored as chars to avoid alignment issues.
2885 * Numbers are always big-endian
2886 * \retval record length
2888 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2889 const struct lu_fid *pfid)
2891 struct lu_fid tmpfid;
2894 fid_cpu_to_be(&tmpfid, pfid);
2895 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2896 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2897 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2899 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2900 lee->lee_reclen[1] = reclen & 0xff;
2904 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2905 struct lu_name *lname, struct lu_fid *pfid)
2907 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2908 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2909 fid_be_to_cpu(pfid, pfid);
2910 lname->ln_name = lee->lee_name;
2911 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2914 /** Add a record to the end of link ea buf */
2915 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2916 const struct lu_fid *pfid,
2917 const struct lu_name *lname)
2919 struct link_ea_header *leh;
2920 struct link_ea_entry *lee;
2923 if (lname == NULL || pfid == NULL)
2926 /* Make sure our buf is big enough for the new one */
2928 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2929 if (leh->leh_len + reclen > buf->lb_len) {
2930 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2935 lee = buf->lb_buf + leh->leh_len;
2936 reclen = mdd_lee_pack(lee, lname, pfid);
2937 leh->leh_len += reclen;
2938 leh->leh_reccount++;
2942 static int mdd_declare_links_add(const struct lu_env *env,
2943 struct mdd_object *mdd_obj,
2944 struct thandle *handle)
2948 /* XXX: max size? */
2949 rc = mdo_declare_xattr_set(env, mdd_obj,
2950 mdd_buf_get_const(env, NULL, 4096),
2951 XATTR_NAME_LINK, 0, handle);
2956 /* For pathologic linkers, we don't want to spend lots of time scanning the
2957 * link ea. Limit ourseleves to something reasonable; links not in the EA
2958 * can be looked up via (slower) parent lookup.
2960 #define LINKEA_MAX_COUNT 128
2962 static int mdd_links_add(const struct lu_env *env,
2963 struct mdd_object *mdd_obj,
2964 const struct lu_fid *pfid,
2965 const struct lu_name *lname,
2966 struct thandle *handle, int first)
2969 struct link_ea_header *leh;
2973 if (!mdd_linkea_enable)
2976 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2979 if (rc != -ENODATA) {
2980 CERROR("link_ea read failed %d "DFID"\n", rc,
2981 PFID(mdd_object_fid(mdd_obj)));
2984 /* empty EA; start one */
2985 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2986 if (buf->lb_buf == NULL)
2989 leh->leh_magic = LINK_EA_MAGIC;
2990 leh->leh_len = sizeof(struct link_ea_header);
2991 leh->leh_reccount = 0;
2995 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2998 rc = __mdd_links_add(env, buf, pfid, lname);
3003 rc = __mdd_xattr_set(env, mdd_obj,
3004 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
3005 XATTR_NAME_LINK, 0, handle);
3008 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
3009 PFID(mdd_object_fid(mdd_obj)));
3011 CERROR("link_ea add failed %d "DFID"\n", rc,
3012 PFID(mdd_object_fid(mdd_obj)));
3015 if (buf->lb_len > OBD_ALLOC_BIG)
3016 /* if we vmalloced a large buffer drop it */
3022 static int mdd_links_rename(const struct lu_env *env,
3023 struct mdd_object *mdd_obj,
3024 const struct lu_fid *oldpfid,
3025 const struct lu_name *oldlname,
3026 const struct lu_fid *newpfid,
3027 const struct lu_name *newlname,
3028 struct thandle *handle)
3031 struct link_ea_header *leh;
3032 struct link_ea_entry *lee;
3033 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
3034 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
3040 if (!mdd_linkea_enable)
3043 if (mdd_obj->mod_flags & DEAD_OBJ)
3044 /* No more links, don't bother */
3047 buf = mdd_links_get(env, mdd_obj);
3051 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
3052 rc, PFID(mdd_object_fid(mdd_obj)));
3054 CERROR("link_ea read failed %d "DFID"\n",
3055 rc, PFID(mdd_object_fid(mdd_obj)));
3059 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
3061 /* Find the old record */
3062 for(count = 0; count < leh->leh_reccount; count++) {
3063 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
3064 if (tmpname->ln_namelen == oldlname->ln_namelen &&
3065 lu_fid_eq(tmpfid, oldpfid) &&
3066 (strncmp(tmpname->ln_name, oldlname->ln_name,
3067 tmpname->ln_namelen) == 0))
3069 lee = (struct link_ea_entry *)((char *)lee + reclen);
3071 if ((count + 1) > leh->leh_reccount) {
3072 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
3073 oldlname->ln_namelen, oldlname->ln_name);
3074 GOTO(out, rc = -ENOENT);
3077 /* Remove the old record */
3078 leh->leh_reccount--;
3079 leh->leh_len -= reclen;
3080 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
3083 /* If renaming, add the new record */
3084 if (newpfid != NULL) {
3085 /* if the add fails, we still delete the out-of-date old link */
3086 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
3090 rc = __mdd_xattr_set(env, mdd_obj,
3091 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
3092 XATTR_NAME_LINK, 0, handle);
3098 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
3099 oldlname->ln_namelen, oldlname->ln_name, rc,
3100 PFID(mdd_object_fid(mdd_obj)));
3102 if (buf->lb_len > OBD_ALLOC_BIG)
3103 /* if we vmalloced a large buffer drop it */
3109 const struct md_dir_operations mdd_dir_ops = {
3110 .mdo_is_subdir = mdd_is_subdir,
3111 .mdo_lookup = mdd_lookup,
3112 .mdo_create = mdd_create,
3113 .mdo_rename = mdd_rename,
3114 .mdo_link = mdd_link,
3115 .mdo_unlink = mdd_unlink,
3116 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
3117 .mdo_name_insert = mdd_name_insert,
3118 .mdo_name_remove = mdd_name_remove,
3119 .mdo_rename_tgt = mdd_rename_tgt,
3120 .mdo_create_data = mdd_create_data,