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;
1658 struct lu_attr *attr = &mdd_env_info(env)->mti_la_for_fix;
1662 rc = mdd_cd_sanity_check(env, son);
1666 if (!md_should_create(spec->sp_cr_flags))
1668 lmm_size = ma->ma_lmm_size;
1670 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1674 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1678 /* calling ->ah_make_hint() is used to transfer information from parent */
1679 mdd_object_make_hint(env, mdd_pobj, son, attr);
1681 handle = mdd_trans_create(env, mdd);
1683 GOTO(out_free, rc = PTR_ERR(handle));
1685 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1689 rc = mdd_trans_start(env, mdd, handle);
1694 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1695 * Should this be fixed?
1698 /* Replay creates has objects already */
1700 if (spec->no_create) {
1701 CDEBUG(D_INFO, "we already have lov ea\n");
1702 rc = mdd_lov_set_md(env, mdd_pobj, son,
1703 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1704 spec->u.sp_ea.eadatalen, handle, 0);
1707 /* No need mdd_lsm_sanity_check here */
1708 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1709 lmm_size, handle, 0);
1712 rc = mdd_attr_get_internal_locked(env, son, ma);
1714 /* update lov_objid data, must be before transaction stop! */
1716 mdd_lov_objid_update(mdd, lmm);
1719 mdd_trans_stop(env, mdd, rc, handle);
1721 /* Finish mdd_lov_create() stuff. */
1722 /* if no_create == 0 (not replay), we free lmm allocated by
1723 * mdd_lov_create() */
1724 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1728 /* Get fid from name and parent */
1730 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1731 const struct lu_name *lname, struct lu_fid* fid, int mask)
1733 const char *name = lname->ln_name;
1734 const struct dt_key *key = (const struct dt_key *)name;
1735 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1736 struct mdd_device *m = mdo2mdd(pobj);
1737 struct dt_object *dir = mdd_object_child(mdd_obj);
1741 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1744 rc = mdd_object_exists(mdd_obj);
1745 if (unlikely(rc == 0))
1747 else if (unlikely(rc < 0)) {
1748 CERROR("Object "DFID" locates on remote server\n",
1749 PFID(mdo2fid(mdd_obj)));
1753 /* The common filename length check. */
1754 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1755 RETURN(-ENAMETOOLONG);
1757 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1762 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1763 dt_try_as_dir(env, dir))) {
1765 rc = dir->do_index_ops->dio_lookup(env, dir,
1766 (struct dt_rec *)fid, key,
1767 mdd_object_capa(env, mdd_obj));
1778 int mdd_declare_object_initialize(const struct lu_env *env,
1779 struct mdd_object *child,
1781 struct thandle *handle)
1785 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1786 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1787 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1790 rc = mdo_declare_ref_add(env, child, handle);
1793 mdd_declare_links_add(env, child, handle);
1798 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1799 const struct lu_name *lname, struct mdd_object *child,
1800 struct md_attr *ma, struct thandle *handle,
1801 const struct md_op_spec *spec)
1807 * Update attributes for child.
1810 * (1) the valid bits should be converted between Lustre and Linux;
1811 * (2) maybe, the child attributes should be set in OSD when creation.
1814 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1818 if (S_ISDIR(ma->ma_attr.la_mode)) {
1819 /* Add "." and ".." for newly created dir */
1820 mdo_ref_add(env, child, handle);
1821 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1822 dot, handle, BYPASS_CAPA);
1824 rc = __mdd_index_insert_only(env, child, pfid,
1828 mdo_ref_del(env, child, handle);
1831 mdd_links_add(env, child, pfid, lname, handle, 1);
1836 /* has not lock on pobj yet */
1837 static int mdd_create_sanity_check(const struct lu_env *env,
1838 struct md_object *pobj,
1839 const struct lu_name *lname,
1841 struct md_op_spec *spec)
1843 struct mdd_thread_info *info = mdd_env_info(env);
1844 struct lu_attr *la = &info->mti_la;
1845 struct lu_fid *fid = &info->mti_fid;
1846 struct mdd_object *obj = md2mdd_obj(pobj);
1847 struct mdd_device *m = mdo2mdd(pobj);
1848 int lookup = spec->sp_cr_lookup;
1853 if (mdd_is_dead_obj(obj))
1857 * In some cases this lookup is not needed - we know before if name
1858 * exists or not because MDT performs lookup for it.
1859 * name length check is done in lookup.
1863 * Check if the name already exist, though it will be checked in
1864 * _index_insert also, for avoiding rolling back if exists
1867 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1868 MAY_WRITE | MAY_EXEC);
1870 RETURN(rc ? : -EEXIST);
1873 * Check WRITE permission for the parent.
1874 * EXEC permission have been checked
1875 * when lookup before create already.
1877 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1884 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1888 if (la->la_mode & S_ISGID) {
1889 ma->ma_attr.la_gid = la->la_gid;
1890 if (S_ISDIR(ma->ma_attr.la_mode)) {
1891 ma->ma_attr.la_mode |= S_ISGID;
1892 ma->ma_attr.la_valid |= LA_MODE;
1896 switch (ma->ma_attr.la_mode & S_IFMT) {
1898 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1900 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1901 RETURN(-ENAMETOOLONG);
1920 static int mdd_declare_create(const struct lu_env *env,
1921 struct mdd_device *mdd,
1922 struct mdd_object *p,
1923 struct mdd_object *c,
1924 const struct lu_name *name,
1927 struct thandle *handle,
1928 const struct md_op_spec *spec)
1930 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1933 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1937 /* if dir, then can inherit default ACl */
1939 buf->lb_len = lmm_size;
1940 if (S_ISDIR(ma->ma_attr.la_mode)) {
1941 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1944 rc = mdo_declare_ref_add(env, p, handle);
1949 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1954 rc = mdd_declare_object_initialize(env, c, ma, handle);
1958 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1959 name->ln_name, handle);
1963 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1968 if (S_ISLNK(ma->ma_attr.la_mode)) {
1969 rc = dt_declare_record_write(env, mdd_object_child(c),
1970 strlen(spec->u.sp_symname), 0,
1975 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1979 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1983 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1991 * Create object and insert it into namespace.
1993 static int mdd_create(const struct lu_env *env,
1994 struct md_object *pobj,
1995 const struct lu_name *lname,
1996 struct md_object *child,
1997 struct md_op_spec *spec,
2000 struct mdd_thread_info *info = mdd_env_info(env);
2001 struct lu_attr *la = &info->mti_la_for_fix;
2002 struct md_attr *ma_acl = &info->mti_ma;
2003 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2004 struct mdd_object *son = md2mdd_obj(child);
2005 struct mdd_device *mdd = mdo2mdd(pobj);
2006 struct lu_attr *attr = &ma->ma_attr;
2007 struct lov_mds_md *lmm = NULL;
2008 struct thandle *handle;
2009 struct dynlock_handle *dlh;
2010 const char *name = lname->ln_name;
2011 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
2012 int got_def_acl = 0;
2013 #ifdef HAVE_QUOTA_SUPPORT
2014 struct obd_device *obd = mdd->mdd_obd_dev;
2015 struct obd_export *exp = md_quota(env)->mq_exp;
2016 struct mds_obd *mds = &obd->u.mds;
2017 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
2018 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
2019 int quota_opc = 0, block_count = 0;
2020 int inode_pending[MAXQUOTAS] = { 0, 0 };
2021 int block_pending[MAXQUOTAS] = { 0, 0 };
2022 int parent_pending[MAXQUOTAS] = { 0, 0 };
2027 * Two operations have to be performed:
2029 * - an allocation of a new object (->do_create()), and
2031 * - an insertion into a parent index (->dio_insert()).
2033 * Due to locking, operation order is not important, when both are
2034 * successful, *but* error handling cases are quite different:
2036 * - if insertion is done first, and following object creation fails,
2037 * insertion has to be rolled back, but this operation might fail
2038 * also leaving us with dangling index entry.
2040 * - if creation is done first, is has to be undone if insertion
2041 * fails, leaving us with leaked space, which is neither good, nor
2044 * It seems that creation-first is simplest solution, but it is
2045 * sub-optimal in the frequent
2050 * case, because second mkdir is bound to create object, only to
2051 * destroy it immediately.
2053 * To avoid this follow local file systems that do double lookup:
2055 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2057 * 1. create (mdd_object_create_internal())
2059 * 2. insert (__mdd_index_insert(), lookup again)
2062 /* Sanity checks before big job. */
2063 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
2067 #ifdef HAVE_QUOTA_SUPPORT
2068 if (mds->mds_quota) {
2069 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2071 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
2075 quota_opc = FSFILT_OP_CREATE;
2076 mdd_quota_wrapper(&ma->ma_attr, qcids);
2077 mdd_quota_wrapper(la_tmp, qpids);
2078 /* get file quota for child */
2079 lquota_chkquota(mds_quota_interface_ref, obd, exp,
2080 qcids, inode_pending, 1, NULL, 0, NULL,
2082 switch (ma->ma_attr.la_mode & S_IFMT) {
2091 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
2092 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
2096 /* get block quota for child and parent */
2098 lquota_chkquota(mds_quota_interface_ref, obd,
2099 exp, qcids, block_pending,
2101 LQUOTA_FLAGS_BLK, NULL, 0);
2103 lquota_chkquota(mds_quota_interface_ref, obd,
2104 exp, qpids, parent_pending, 1,
2105 NULL, LQUOTA_FLAGS_BLK, NULL,
2111 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2112 GOTO(out_pending, rc = -EINPROGRESS);
2115 * No RPC inside the transaction, so OST objects should be created at
2118 if (S_ISREG(attr->la_mode)) {
2119 lmm_size = ma->ma_lmm_size;
2120 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
2123 GOTO(out_pending, rc);
2126 if (!S_ISLNK(attr->la_mode)) {
2127 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2128 ma_acl->ma_acl = info->mti_xattr_buf;
2129 ma_acl->ma_need = MA_ACL_DEF;
2130 ma_acl->ma_valid = 0;
2132 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2133 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2134 mdd_read_unlock(env, mdd_pobj);
2137 else if (ma_acl->ma_valid & MA_ACL_DEF)
2141 mdd_object_make_hint(env, mdd_pobj, son, attr);
2143 handle = mdd_trans_create(env, mdd);
2145 GOTO(out_free, rc = PTR_ERR(handle));
2147 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2148 lmm_size, handle, spec);
2152 rc = mdd_trans_start(env, mdd, handle);
2156 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2158 GOTO(out_trans, rc = -ENOMEM);
2160 mdd_write_lock(env, son, MOR_TGT_CHILD);
2161 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2163 mdd_write_unlock(env, son);
2169 #ifdef CONFIG_FS_POSIX_ACL
2171 struct lu_buf *acl_buf = &info->mti_buf;
2172 acl_buf->lb_buf = ma_acl->ma_acl;
2173 acl_buf->lb_len = ma_acl->ma_acl_size;
2175 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2177 mdd_write_unlock(env, son);
2180 ma->ma_attr.la_valid |= LA_MODE;
2185 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2186 son, ma, handle, spec);
2187 mdd_write_unlock(env, son);
2190 * Object has no links, so it will be destroyed when last
2191 * reference is released. (XXX not now.)
2197 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2198 name, S_ISDIR(attr->la_mode), handle,
2199 mdd_object_capa(env, mdd_pobj));
2206 /* No need mdd_lsm_sanity_check here */
2207 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2209 CERROR("error on stripe info copy %d \n", rc);
2212 if (lmm && lmm_size > 0) {
2213 /* Set Lov here, do not get lmm again later */
2214 if (lmm_size > ma->ma_lmm_size) {
2215 /* Reply buffer is smaller, need bigger one */
2216 mdd_max_lmm_buffer(env, lmm_size);
2217 if (unlikely(info->mti_max_lmm == NULL))
2218 GOTO(cleanup, rc = -ENOMEM);
2219 ma->ma_lmm = info->mti_max_lmm;
2220 ma->ma_big_lmm_used = 1;
2222 memcpy(ma->ma_lmm, lmm, lmm_size);
2223 ma->ma_lmm_size = lmm_size;
2224 ma->ma_valid |= MA_LOV;
2227 if (S_ISLNK(attr->la_mode)) {
2228 struct md_ucred *uc = md_ucred(env);
2229 struct dt_object *dt = mdd_object_child(son);
2230 const char *target_name = spec->u.sp_symname;
2231 int sym_len = strlen(target_name);
2232 const struct lu_buf *buf;
2235 buf = mdd_buf_get_const(env, target_name, sym_len);
2236 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2237 mdd_object_capa(env, son),
2239 CFS_CAP_SYS_RESOURCE_MASK);
2244 GOTO(cleanup, rc = -EFAULT);
2248 la->la_valid = LA_CTIME | LA_MTIME;
2249 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2253 /* Return attr back. */
2254 rc = mdd_attr_get_internal_locked(env, son, ma);
2257 if (rc != 0 && created != 0) {
2260 if (inserted != 0) {
2261 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2262 S_ISDIR(attr->la_mode),
2263 handle, BYPASS_CAPA);
2268 mdd_write_lock(env, son, MOR_TGT_CHILD);
2269 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2270 /* Drop the reference, no need to delete "."/"..",
2271 * because the object to be destroied directly. */
2272 rc2 = mdo_ref_del(env, son, handle);
2274 mdd_write_unlock(env, son);
2279 rc2 = mdo_ref_del(env, son, handle);
2281 mdd_write_unlock(env, son);
2285 mdo_destroy(env, son, handle);
2286 mdd_write_unlock(env, son);
2289 /* update lov_objid data, must be before transaction stop! */
2291 mdd_lov_objid_update(mdd, lmm);
2293 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2296 rc = mdd_changelog_ns_store(env, mdd,
2297 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2298 S_ISREG(attr->la_mode) ? CL_CREATE :
2299 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2300 0, son, mdd_pobj, lname, handle);
2302 mdd_trans_stop(env, mdd, rc, handle);
2304 /* finish lov_create stuff, free all temporary data */
2305 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2307 #ifdef HAVE_QUOTA_SUPPORT
2309 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2311 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2313 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2315 /* Trigger dqacq on the owner of child and parent. If failed,
2316 * the next call for lquota_chkquota will process it. */
2317 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2321 /* The child object shouldn't be cached anymore */
2323 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
2324 &child->mo_lu.lo_header->loh_flags);
2329 * Get locks on parents in proper order
2330 * RETURN: < 0 - error, rename_order if successful
2338 static int mdd_rename_order(const struct lu_env *env,
2339 struct mdd_device *mdd,
2340 struct mdd_object *src_pobj,
2341 struct mdd_object *tgt_pobj)
2343 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2347 if (src_pobj == tgt_pobj)
2348 RETURN(MDD_RN_SAME);
2350 /* compared the parent child relationship of src_p&tgt_p */
2351 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2353 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2356 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2369 /* has not mdd_write{read}_lock on any obj yet. */
2370 static int mdd_rename_sanity_check(const struct lu_env *env,
2371 struct mdd_object *src_pobj,
2372 struct mdd_object *tgt_pobj,
2373 struct mdd_object *sobj,
2374 struct mdd_object *tobj,
2380 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2383 /* XXX: when get here, sobj must NOT be NULL,
2384 * the other case has been processed in cml_rename
2385 * before mdd_rename and enable MDS_PERM_BYPASS. */
2388 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2392 /* XXX: when get here, "tobj == NULL" means tobj must
2393 * NOT exist (neither on remote MDS, such case has been
2394 * processed in cml_rename before mdd_rename and enable
2396 * So check may_create, but not check may_unlink. */
2398 rc = mdd_may_create(env, tgt_pobj, NULL,
2399 (src_pobj != tgt_pobj), 0);
2401 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2402 (src_pobj != tgt_pobj), 1);
2404 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2405 S_ISDIR(ma->ma_attr.la_mode))
2406 rc = __mdd_may_link(env, tgt_pobj);
2411 static int mdd_declare_rename(const struct lu_env *env,
2412 struct mdd_device *mdd,
2413 struct mdd_object *mdd_spobj,
2414 struct mdd_object *mdd_tpobj,
2415 struct mdd_object *mdd_sobj,
2416 struct mdd_object *mdd_tobj,
2417 const struct lu_name *tname,
2418 const struct lu_name *sname,
2420 struct thandle *handle)
2428 /* name from source dir */
2429 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2433 /* .. from source child */
2434 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2435 /* source child can be directory,
2436 * counted by source dir's nlink */
2437 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2441 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2445 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2450 /* new target child can be directory,
2451 * counted by target dir's nlink */
2452 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2458 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2462 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2465 mdd_declare_links_add(env, mdd_sobj, handle);
2469 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2474 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2475 tname->ln_name, handle);
2479 /* name from target dir (old name), we declare it unconditionally
2480 * as mdd_rename() calls delete unconditionally as well. so just
2481 * to balance declarations vs calls to change ... */
2482 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2486 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2487 /* delete target child in target parent directory */
2488 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2492 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2493 /* target child can be directory,
2494 * delete "." reference in target child directory */
2495 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2499 /* delete ".." reference in target parent directory */
2500 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2505 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2509 mdd_declare_links_add(env, mdd_tobj, handle);
2513 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2518 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2525 /* src object can be remote that is why we use only fid and type of object */
2526 static int mdd_rename(const struct lu_env *env,
2527 struct md_object *src_pobj, struct md_object *tgt_pobj,
2528 const struct lu_fid *lf, const struct lu_name *lsname,
2529 struct md_object *tobj, const struct lu_name *ltname,
2532 const char *sname = lsname->ln_name;
2533 const char *tname = ltname->ln_name;
2534 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2535 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2536 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2537 struct mdd_device *mdd = mdo2mdd(src_pobj);
2538 struct mdd_object *mdd_sobj = NULL; /* source object */
2539 struct mdd_object *mdd_tobj = NULL;
2540 struct dynlock_handle *sdlh, *tdlh;
2541 struct thandle *handle;
2542 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2543 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2545 unsigned cl_flags = 0;
2548 #ifdef HAVE_QUOTA_SUPPORT
2549 struct obd_device *obd = mdd->mdd_obd_dev;
2550 struct obd_export *exp = md_quota(env)->mq_exp;
2551 struct mds_obd *mds = &obd->u.mds;
2552 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2553 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2554 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2555 int quota_copc = 0, quota_popc = 0;
2556 int rec_pending[MAXQUOTAS] = { 0, 0 };
2560 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2561 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2564 mdd_tobj = md2mdd_obj(tobj);
2566 #ifdef HAVE_QUOTA_SUPPORT
2567 if (mds->mds_quota) {
2568 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2570 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2572 mdd_quota_wrapper(la_tmp, qspids);
2574 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2578 mdd_data_get(env, mdd_tpobj, &data);
2579 quota_popc = FSFILT_OP_LINK;
2580 mdd_quota_wrapper(la_tmp, qtpids);
2581 /* get block quota for target parent */
2582 lquota_chkquota(mds_quota_interface_ref,
2584 rec_pending, 1, NULL,
2592 mdd_sobj = mdd_object_find(env, mdd, lf);
2594 handle = mdd_trans_create(env, mdd);
2596 GOTO(out_pending, rc = PTR_ERR(handle));
2598 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2599 mdd_tobj, lsname, ltname, ma, handle);
2603 rc = mdd_trans_start(env, mdd, handle);
2607 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2608 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2610 GOTO(cleanup_unlocked, rc);
2612 /* Get locks in determined order */
2613 if (rc == MDD_RN_SAME) {
2614 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2615 sname, MOR_SRC_PARENT);
2616 /* check hashes to determine do we need one lock or two */
2617 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2618 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2622 } else if (rc == MDD_RN_SRCTGT) {
2623 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2624 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2626 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2627 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2629 if (sdlh == NULL || tdlh == NULL)
2630 GOTO(cleanup, rc = -ENOMEM);
2632 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2633 mdd_sobj, mdd_tobj, ma);
2637 /* Remove source name from source directory */
2638 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2639 mdd_object_capa(env, mdd_spobj));
2643 /* "mv dir1 dir2" needs "dir1/.." link update */
2644 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2645 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2646 mdd_object_capa(env, mdd_sobj));
2648 GOTO(fixup_spobj2, rc);
2650 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2651 handle, mdd_object_capa(env, mdd_sobj));
2653 GOTO(fixup_spobj, rc);
2656 /* Remove target name from target directory
2657 * Here tobj can be remote one, so we do index_delete unconditionally
2658 * and -ENOENT is allowed.
2660 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2661 mdd_object_capa(env, mdd_tpobj));
2664 /* tname might been renamed to something else */
2665 GOTO(fixup_spobj, rc);
2668 GOTO(fixup_spobj, rc);
2671 /* Insert new fid with target name into target dir */
2672 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2673 mdd_object_capa(env, mdd_tpobj));
2675 GOTO(fixup_tpobj, rc);
2677 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2678 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2680 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2682 la->la_valid = LA_CTIME;
2683 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2686 GOTO(fixup_tpobj, rc);
2689 /* Remove old target object
2690 * For tobj is remote case cmm layer has processed
2691 * and set tobj to NULL then. So when tobj is NOT NULL,
2692 * it must be local one.
2694 if (tobj && mdd_object_exists(mdd_tobj)) {
2695 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2696 if (mdd_is_dead_obj(mdd_tobj)) {
2697 mdd_write_unlock(env, mdd_tobj);
2698 /* shld not be dead, something is wrong */
2699 CERROR("tobj is dead, something is wrong\n");
2703 mdo_ref_del(env, mdd_tobj, handle);
2705 /* Remove dot reference. */
2707 mdo_ref_del(env, mdd_tobj, handle);
2709 la->la_valid = LA_CTIME;
2710 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2712 GOTO(fixup_tpobj, rc);
2714 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2715 mdd_write_unlock(env, mdd_tobj);
2717 GOTO(fixup_tpobj, rc);
2719 if (ma->ma_valid & MA_INODE && ma->ma_attr.la_nlink == 0) {
2720 cl_flags |= CLF_RENAME_LAST;
2721 #ifdef HAVE_QUOTA_SUPPORT
2722 if (mds->mds_quota && mdd_tobj->mod_count == 0) {
2723 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2724 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2730 la->la_valid = LA_CTIME | LA_MTIME;
2731 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2733 GOTO(fixup_tpobj, rc);
2735 if (mdd_spobj != mdd_tpobj) {
2736 la->la_valid = LA_CTIME | LA_MTIME;
2737 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2741 if (rc == 0 && mdd_sobj) {
2742 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2743 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2744 mdo2fid(mdd_tpobj), ltname, handle);
2746 /* Old files might not have EA entry */
2747 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2749 mdd_write_unlock(env, mdd_sobj);
2750 /* We don't fail the transaction if the link ea can't be
2751 updated -- fid2path will use alternate lookup method. */
2759 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2762 CWARN("tp obj fix error %d\n",rc2);
2764 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2765 !mdd_is_dead_obj(mdd_tobj)) {
2766 rc2 = __mdd_index_insert(env, mdd_tpobj,
2767 mdo2fid(mdd_tobj), tname,
2772 CWARN("tp obj fix error %d\n",rc2);
2777 if (rc && is_dir && mdd_sobj) {
2778 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2782 CWARN("sp obj dotdot delete error %d\n",rc2);
2785 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2786 dotdot, handle, BYPASS_CAPA);
2788 CWARN("sp obj dotdot insert error %d\n",rc2);
2793 rc2 = __mdd_index_insert(env, mdd_spobj,
2794 lf, sname, is_dir, handle, BYPASS_CAPA);
2796 CWARN("sp obj fix error %d\n",rc2);
2799 if (likely(tdlh) && sdlh != tdlh)
2800 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2802 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2805 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2806 mdd_tobj, tpobj_fid, lf,
2807 spobj_fid, ltname, lsname,
2811 mdd_trans_stop(env, mdd, rc, handle);
2813 mdd_object_put(env, mdd_sobj);
2815 #ifdef HAVE_QUOTA_SUPPORT
2816 if (mds->mds_quota) {
2818 lquota_pending_commit(mds_quota_interface_ref, obd,
2819 qtpids, rec_pending, 1);
2822 /* Trigger dqrel on the source owner of parent.
2823 * If failed, the next call for lquota_chkquota will
2825 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2826 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2828 /* Trigger dqrel on the target owner of child.
2829 * If failed, the next call for lquota_chkquota
2830 * will process it. */
2831 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2832 qtpids, rc, quota_copc);
2839 /** enable/disable storing of hardlink info */
2840 int mdd_linkea_enable = 1;
2841 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2842 "record hardlink info in EAs");
2844 /** Read the link EA into a temp buffer.
2845 * Uses the name_buf since it is generally large.
2846 * \retval IS_ERR err
2847 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2849 struct lu_buf *mdd_links_get(const struct lu_env *env,
2850 struct mdd_object *mdd_obj)
2853 struct lustre_capa *capa;
2854 struct link_ea_header *leh;
2857 /* First try a small buf */
2858 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2859 if (buf->lb_buf == NULL)
2860 return ERR_PTR(-ENOMEM);
2862 capa = mdd_object_capa(env, mdd_obj);
2863 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2864 if (rc == -ERANGE) {
2865 /* Buf was too small, figure out what we need. */
2867 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2870 buf = mdd_buf_alloc(env, rc);
2871 if (buf->lb_buf == NULL)
2872 return ERR_PTR(-ENOMEM);
2873 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2879 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2880 leh->leh_magic = LINK_EA_MAGIC;
2881 leh->leh_reccount = __swab32(leh->leh_reccount);
2882 leh->leh_len = __swab64(leh->leh_len);
2883 /* entries are swabbed by mdd_lee_unpack */
2885 if (leh->leh_magic != LINK_EA_MAGIC)
2886 return ERR_PTR(-EINVAL);
2887 if (leh->leh_reccount == 0)
2888 return ERR_PTR(-ENODATA);
2893 /** Pack a link_ea_entry.
2894 * All elements are stored as chars to avoid alignment issues.
2895 * Numbers are always big-endian
2896 * \retval record length
2898 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2899 const struct lu_fid *pfid)
2901 struct lu_fid tmpfid;
2904 fid_cpu_to_be(&tmpfid, pfid);
2905 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2906 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2907 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2909 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2910 lee->lee_reclen[1] = reclen & 0xff;
2914 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2915 struct lu_name *lname, struct lu_fid *pfid)
2917 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2918 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2919 fid_be_to_cpu(pfid, pfid);
2920 lname->ln_name = lee->lee_name;
2921 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2924 /** Add a record to the end of link ea buf */
2925 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2926 const struct lu_fid *pfid,
2927 const struct lu_name *lname)
2929 struct link_ea_header *leh;
2930 struct link_ea_entry *lee;
2933 if (lname == NULL || pfid == NULL)
2936 /* Make sure our buf is big enough for the new one */
2938 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2939 if (leh->leh_len + reclen > buf->lb_len) {
2940 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2945 lee = buf->lb_buf + leh->leh_len;
2946 reclen = mdd_lee_pack(lee, lname, pfid);
2947 leh->leh_len += reclen;
2948 leh->leh_reccount++;
2952 static int mdd_declare_links_add(const struct lu_env *env,
2953 struct mdd_object *mdd_obj,
2954 struct thandle *handle)
2958 /* XXX: max size? */
2959 rc = mdo_declare_xattr_set(env, mdd_obj,
2960 mdd_buf_get_const(env, NULL, 4096),
2961 XATTR_NAME_LINK, 0, handle);
2966 /* For pathologic linkers, we don't want to spend lots of time scanning the
2967 * link ea. Limit ourseleves to something reasonable; links not in the EA
2968 * can be looked up via (slower) parent lookup.
2970 #define LINKEA_MAX_COUNT 128
2972 static int mdd_links_add(const struct lu_env *env,
2973 struct mdd_object *mdd_obj,
2974 const struct lu_fid *pfid,
2975 const struct lu_name *lname,
2976 struct thandle *handle, int first)
2979 struct link_ea_header *leh;
2983 if (!mdd_linkea_enable)
2986 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2989 if (rc != -ENODATA) {
2990 CERROR("link_ea read failed %d "DFID"\n", rc,
2991 PFID(mdd_object_fid(mdd_obj)));
2994 /* empty EA; start one */
2995 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2996 if (buf->lb_buf == NULL)
2999 leh->leh_magic = LINK_EA_MAGIC;
3000 leh->leh_len = sizeof(struct link_ea_header);
3001 leh->leh_reccount = 0;
3005 if (leh->leh_reccount > LINKEA_MAX_COUNT)
3008 rc = __mdd_links_add(env, buf, pfid, lname);
3013 rc = __mdd_xattr_set(env, mdd_obj,
3014 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
3015 XATTR_NAME_LINK, 0, handle);
3018 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
3019 PFID(mdd_object_fid(mdd_obj)));
3021 CERROR("link_ea add failed %d "DFID"\n", rc,
3022 PFID(mdd_object_fid(mdd_obj)));
3025 if (buf->lb_len > OBD_ALLOC_BIG)
3026 /* if we vmalloced a large buffer drop it */
3032 static int mdd_links_rename(const struct lu_env *env,
3033 struct mdd_object *mdd_obj,
3034 const struct lu_fid *oldpfid,
3035 const struct lu_name *oldlname,
3036 const struct lu_fid *newpfid,
3037 const struct lu_name *newlname,
3038 struct thandle *handle)
3041 struct link_ea_header *leh;
3042 struct link_ea_entry *lee;
3043 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
3044 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
3050 if (!mdd_linkea_enable)
3053 if (mdd_obj->mod_flags & DEAD_OBJ)
3054 /* No more links, don't bother */
3057 buf = mdd_links_get(env, mdd_obj);
3061 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
3062 rc, PFID(mdd_object_fid(mdd_obj)));
3064 CERROR("link_ea read failed %d "DFID"\n",
3065 rc, PFID(mdd_object_fid(mdd_obj)));
3069 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
3071 /* Find the old record */
3072 for(count = 0; count < leh->leh_reccount; count++) {
3073 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
3074 if (tmpname->ln_namelen == oldlname->ln_namelen &&
3075 lu_fid_eq(tmpfid, oldpfid) &&
3076 (strncmp(tmpname->ln_name, oldlname->ln_name,
3077 tmpname->ln_namelen) == 0))
3079 lee = (struct link_ea_entry *)((char *)lee + reclen);
3081 if ((count + 1) > leh->leh_reccount) {
3082 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
3083 oldlname->ln_namelen, oldlname->ln_name);
3084 GOTO(out, rc = -ENOENT);
3087 /* Remove the old record */
3088 leh->leh_reccount--;
3089 leh->leh_len -= reclen;
3090 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
3093 /* If renaming, add the new record */
3094 if (newpfid != NULL) {
3095 /* if the add fails, we still delete the out-of-date old link */
3096 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
3100 rc = __mdd_xattr_set(env, mdd_obj,
3101 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
3102 XATTR_NAME_LINK, 0, handle);
3108 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
3109 oldlname->ln_namelen, oldlname->ln_name, rc,
3110 PFID(mdd_object_fid(mdd_obj)));
3112 if (buf->lb_len > OBD_ALLOC_BIG)
3113 /* if we vmalloced a large buffer drop it */
3119 const struct md_dir_operations mdd_dir_ops = {
3120 .mdo_is_subdir = mdd_is_subdir,
3121 .mdo_lookup = mdd_lookup,
3122 .mdo_create = mdd_create,
3123 .mdo_rename = mdd_rename,
3124 .mdo_link = mdd_link,
3125 .mdo_unlink = mdd_unlink,
3126 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
3127 .mdo_name_insert = mdd_name_insert,
3128 .mdo_name_remove = mdd_name_remove,
3129 .mdo_rename_tgt = mdd_rename_tgt,
3130 .mdo_create_data = mdd_create_data,