1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdd/mdd_dir.c
41 * Lustre Metadata Server (mdd) routines
43 * Author: Wang Di <wangdi@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
52 #ifdef HAVE_EXT4_LDISKFS
53 #include <ldiskfs/ldiskfs_jbd2.h>
55 #include <linux/jbd.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
62 #ifdef HAVE_EXT4_LDISKFS
63 #include <ldiskfs/ldiskfs.h>
65 #include <linux/ldiskfs_fs.h>
67 #include <lustre_mds.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_fid.h>
71 #include "mdd_internal.h"
73 static const char dot[] = ".";
74 static const char dotdot[] = "..";
76 static struct lu_name lname_dotdot = {
81 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
82 const struct lu_name *lname, struct lu_fid* fid,
84 static int mdd_declare_links_add(const struct lu_env *env,
85 struct mdd_object *mdd_obj,
86 struct thandle *handle);
87 static int mdd_links_add(const struct lu_env *env,
88 struct mdd_object *mdd_obj,
89 const struct lu_fid *pfid,
90 const struct lu_name *lname,
91 struct thandle *handle, int first);
92 static int mdd_links_rename(const struct lu_env *env,
93 struct mdd_object *mdd_obj,
94 const struct lu_fid *oldpfid,
95 const struct lu_name *oldlname,
96 const struct lu_fid *newpfid,
97 const struct lu_name *newlname,
98 struct thandle *handle);
101 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
102 const struct lu_name *lname, struct lu_fid* fid, int mask)
104 const char *name = lname->ln_name;
105 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
106 struct dynlock_handle *dlh;
109 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
110 if (unlikely(dlh == NULL))
112 rc = __mdd_lookup(env, pobj, lname, fid, mask);
113 mdd_pdo_read_unlock(env, mdd_obj, dlh);
118 int mdd_lookup(const struct lu_env *env,
119 struct md_object *pobj, const struct lu_name *lname,
120 struct lu_fid* fid, struct md_op_spec *spec)
124 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
128 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
131 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
135 * For root fid use special function, which does not compare version component
136 * of fid. Version component is different for root fids on all MDTs.
138 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
140 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
141 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
145 * return 1: if lf is the fid of the ancestor of p1;
148 * return -EREMOTE: if remote object is found, in this
149 * case fid of remote object is saved to @pf;
151 * otherwise: values < 0, errors.
153 static int mdd_is_parent(const struct lu_env *env,
154 struct mdd_device *mdd,
155 struct mdd_object *p1,
156 const struct lu_fid *lf,
159 struct mdd_object *parent = NULL;
164 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
165 pfid = &mdd_env_info(env)->mti_fid;
167 /* Check for root first. */
168 if (mdd_is_root(mdd, mdo2fid(p1)))
172 /* this is done recursively, bypass capa for each obj */
173 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
174 rc = mdd_parent_fid(env, p1, pfid);
177 if (mdd_is_root(mdd, pfid))
179 if (lu_fid_eq(pfid, lf))
182 mdd_object_put(env, parent);
183 parent = mdd_object_find(env, mdd, pfid);
185 /* cross-ref parent */
186 if (parent == NULL) {
189 GOTO(out, rc = -EREMOTE);
190 } else if (IS_ERR(parent))
191 GOTO(out, rc = PTR_ERR(parent));
196 if (parent && !IS_ERR(parent))
197 mdd_object_put(env, parent);
202 * No permission check is needed.
204 * returns 1: if fid is ancestor of @mo;
205 * returns 0: if fid is not a ancestor of @mo;
207 * returns EREMOTE if remote object is found, fid of remote object is saved to
210 * returns < 0: if error
212 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
213 const struct lu_fid *fid, struct lu_fid *sfid)
215 struct mdd_device *mdd = mdo2mdd(mo);
219 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
222 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
226 } else if (rc == 1) {
227 /* found @fid is parent */
235 * Check that @dir contains no entries except (possibly) dot and dotdot.
240 * -ENOTDIR not a directory object
241 * -ENOTEMPTY not empty
245 static int mdd_dir_is_empty(const struct lu_env *env,
246 struct mdd_object *dir)
249 struct dt_object *obj;
250 const struct dt_it_ops *iops;
254 obj = mdd_object_child(dir);
255 if (!dt_try_as_dir(env, obj))
258 iops = &obj->do_index_ops->dio_it;
259 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
261 result = iops->get(env, it, (const void *)"");
264 for (result = 0, i = 0; result == 0 && i < 3; ++i)
265 result = iops->next(env, it);
268 else if (result == +1)
270 } else if (result == 0)
272 * Huh? Index contains no zero key?
279 result = PTR_ERR(it);
283 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
285 struct mdd_device *m = mdd_obj2mdd_dev(obj);
286 struct lu_attr *la = &mdd_env_info(env)->mti_la;
290 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
295 * Subdir count limitation can be broken through.
297 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
298 !S_ISDIR(la->la_mode))
305 * Check whether it may create the cobj under the pobj.
308 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
309 struct mdd_object *cobj, int check_perm, int check_nlink)
314 if (cobj && mdd_object_exists(cobj))
317 if (mdd_is_dead_obj(pobj))
321 rc = mdd_permission_internal_locked(env, pobj, NULL,
322 MAY_WRITE | MAY_EXEC,
325 if (!rc && check_nlink)
326 rc = __mdd_may_link(env, pobj);
332 * Check whether can unlink from the pobj in the case of "cobj == NULL".
334 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
335 const struct md_attr *ma)
340 if (mdd_is_dead_obj(pobj))
343 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
344 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
347 rc = mdd_permission_internal_locked(env, pobj, NULL,
348 MAY_WRITE | MAY_EXEC,
353 if (mdd_is_append(pobj))
360 * pobj == NULL is remote ops case, under such case, pobj's
361 * VTX feature has been checked already, no need check again.
363 static inline int mdd_is_sticky(const struct lu_env *env,
364 struct mdd_object *pobj,
365 struct mdd_object *cobj)
367 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
368 struct md_ucred *uc = md_ucred(env);
372 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
376 if (!(tmp_la->la_mode & S_ISVTX) ||
377 (tmp_la->la_uid == uc->mu_fsuid))
381 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
385 if (tmp_la->la_uid == uc->mu_fsuid)
388 return !mdd_capable(uc, CFS_CAP_FOWNER);
392 * Check whether it may delete the cobj from the pobj.
395 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
396 struct mdd_object *cobj, struct md_attr *ma,
397 int check_perm, int check_empty)
403 if (!mdd_object_exists(cobj))
406 if (mdd_is_dead_obj(cobj))
410 if (!mdd_object_exists(pobj))
413 if (mdd_is_dead_obj(pobj))
417 rc = mdd_permission_internal_locked(env, pobj, NULL,
418 MAY_WRITE | MAY_EXEC,
424 if (mdd_is_append(pobj))
428 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
429 mdd_is_sticky(env, pobj, cobj))
432 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
435 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
436 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
439 if (S_ISDIR(ma->ma_attr.la_mode)) {
440 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
442 if (!S_ISDIR(mdd_object_type(cobj)))
445 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
447 } else if (S_ISDIR(mdd_object_type(cobj)))
450 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
451 rc = mdd_dir_is_empty(env, cobj);
458 * has mdd_write_lock on src already, but not on tgt yet
460 int mdd_link_sanity_check(const struct lu_env *env,
461 struct mdd_object *tgt_obj,
462 const struct lu_name *lname,
463 struct mdd_object *src_obj)
465 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
469 if (!mdd_object_exists(src_obj))
472 if (mdd_is_dead_obj(src_obj))
475 /* Local ops, no lookup before link, check filename length here. */
476 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
477 RETURN(-ENAMETOOLONG);
479 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
482 if (S_ISDIR(mdd_object_type(src_obj)))
485 LASSERT(src_obj != tgt_obj);
487 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
492 rc = __mdd_may_link(env, src_obj);
497 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
498 const char *name, struct thandle *handle,
499 struct lustre_capa *capa)
501 struct dt_object *next = mdd_object_child(pobj);
505 if (dt_try_as_dir(env, next)) {
506 rc = next->do_index_ops->dio_delete(env, next,
507 (struct dt_key *)name,
515 static int __mdd_index_insert_only(const struct lu_env *env,
516 struct mdd_object *pobj,
517 const struct lu_fid *lf, const char *name,
518 struct thandle *handle,
519 struct lustre_capa *capa)
521 struct dt_object *next = mdd_object_child(pobj);
525 if (dt_try_as_dir(env, next)) {
526 struct md_ucred *uc = md_ucred(env);
528 rc = next->do_index_ops->dio_insert(env, next,
530 (const struct dt_key *)name,
531 handle, capa, uc->mu_cap &
532 CFS_CAP_SYS_RESOURCE_MASK);
539 /* insert named index, add reference if isdir */
540 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
541 const struct lu_fid *lf, const char *name, int is_dir,
542 struct thandle *handle, struct lustre_capa *capa)
547 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
548 if (rc == 0 && is_dir) {
549 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
550 mdo_ref_add(env, pobj, handle);
551 mdd_write_unlock(env, pobj);
556 /* delete named index, drop reference if isdir */
557 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
558 const char *name, int is_dir, struct thandle *handle,
559 struct lustre_capa *capa)
564 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
565 if (rc == 0 && is_dir) {
566 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
567 mdo_ref_del(env, pobj, handle);
568 mdd_write_unlock(env, pobj);
574 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
575 int reclen, struct thandle *handle)
579 /* XXX: this is a temporary solution to declare llog changes
580 * will be fixed in 2.3 with new llog implementation */
582 LASSERT(mdd->mdd_capa);
584 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
585 * have to set the parameter 'size' as INT_MAX or 0 to inform
586 * OSD that this record write is for a llog write or catalog
587 * header update, and osd declare function will reserve less
588 * credits for optimization purpose.
590 * Reserve 6 blocks for a llog write, since the llog file is
591 * usually small, reserve 2 blocks for catalog header update,
592 * because we know for sure that catalog header is already
595 * This hack should be removed in 2.3.
599 rc = dt_declare_record_write(env, mdd->mdd_capa,
600 DECLARE_LLOG_WRITE, 0, handle);
604 /* header will be updated as well */
605 rc = dt_declare_record_write(env, mdd->mdd_capa,
606 DECLARE_LLOG_WRITE, 0, handle);
610 /* also we should be able to create new plain log */
611 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
615 /* new record referencing new plain llog */
616 rc = dt_declare_record_write(env, mdd->mdd_capa,
617 DECLARE_LLOG_WRITE, 0, handle);
621 /* catalog's header will be updated as well */
622 rc = dt_declare_record_write(env, mdd->mdd_capa,
623 DECLARE_LLOG_REWRITE, 0, handle);
628 int mdd_declare_changelog_store(const struct lu_env *env,
629 struct mdd_device *mdd,
630 const struct lu_name *fname,
631 struct thandle *handle)
636 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
639 /* we'll be writing payload + llog header */
640 reclen = sizeof(struct llog_changelog_rec);
642 reclen += llog_data_len(fname->ln_namelen);
644 return mdd_declare_llog_record(env, mdd, reclen, handle);
647 /** Store a namespace change changelog record
648 * If this fails, we must fail the whole transaction; we don't
649 * want the change to commit without the log entry.
650 * \param target - mdd_object of change
651 * \param parent - parent dir/object
652 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
653 * \param tname - target name string
654 * \param handle - transacion handle
656 static int mdd_changelog_ns_store(const struct lu_env *env,
657 struct mdd_device *mdd,
658 enum changelog_rec_type type,
660 struct mdd_object *target,
661 struct mdd_object *parent,
662 const struct lu_fid *tf,
663 const struct lu_name *tname,
664 struct thandle *handle)
666 const struct lu_fid *tfid;
667 const struct lu_fid *tpfid = mdo2fid(parent);
668 struct llog_changelog_rec *rec;
675 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
677 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
680 LASSERT(parent != NULL);
681 LASSERT(tname != NULL);
682 LASSERT(handle != NULL);
685 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
686 buf = mdd_buf_alloc(env, reclen);
687 if (buf->lb_buf == NULL)
689 rec = (struct llog_changelog_rec *)buf->lb_buf;
691 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
692 rec->cr.cr_type = (__u32)type;
693 tfid = tf ? tf : mdo2fid(target);
694 rec->cr.cr_tfid = *tfid;
695 rec->cr.cr_pfid = *tpfid;
696 rec->cr.cr_namelen = tname->ln_namelen;
697 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
699 target->mod_cltime = cfs_time_current_64();
701 rc = mdd_changelog_llog_write(mdd, rec, handle);
703 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
704 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
711 static int mdd_declare_link(const struct lu_env *env,
712 struct mdd_device *mdd,
713 struct mdd_object *p,
714 struct mdd_object *c,
715 const struct lu_name *name,
716 struct thandle *handle)
720 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
724 rc = mdo_declare_ref_add(env, c, handle);
728 rc = mdo_declare_attr_set(env, p, NULL, handle);
732 rc = mdo_declare_attr_set(env, c, NULL, handle);
736 rc = mdd_declare_links_add(env, c, handle);
740 rc = mdd_declare_changelog_store(env, mdd, name, handle);
745 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
746 struct md_object *src_obj, const struct lu_name *lname,
749 const char *name = lname->ln_name;
750 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
751 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
752 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
753 struct mdd_device *mdd = mdo2mdd(src_obj);
754 struct dynlock_handle *dlh;
755 struct thandle *handle;
756 #ifdef HAVE_QUOTA_SUPPORT
757 struct obd_device *obd = mdd->mdd_obd_dev;
758 struct obd_export *exp = md_quota(env)->mq_exp;
759 struct mds_obd *mds = &obd->u.mds;
760 unsigned int qids[MAXQUOTAS] = { 0, 0 };
761 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
766 #ifdef HAVE_QUOTA_SUPPORT
767 if (mds->mds_quota) {
768 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
770 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
773 mdd_data_get(env, mdd_tobj, &data);
774 quota_opc = FSFILT_OP_LINK;
775 mdd_quota_wrapper(la_tmp, qids);
776 /* get block quota for parent */
777 lquota_chkquota(mds_quota_interface_ref, obd, exp,
778 qids, rec_pending, 1, NULL,
779 LQUOTA_FLAGS_BLK, data, 1);
784 handle = mdd_trans_create(env, mdd);
786 GOTO(out_pending, rc = PTR_ERR(handle));
788 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
792 rc = mdd_trans_start(env, mdd, handle);
796 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
798 GOTO(out_trans, rc = -ENOMEM);
799 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
801 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
803 GOTO(out_unlock, rc);
805 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
807 mdd_object_capa(env, mdd_tobj));
809 GOTO(out_unlock, rc);
811 mdo_ref_add(env, mdd_sobj, handle);
813 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
814 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
816 la->la_valid = LA_CTIME | LA_MTIME;
817 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
819 GOTO(out_unlock, rc);
821 la->la_valid = LA_CTIME;
822 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
824 mdd_links_add(env, mdd_sobj,
825 mdo2fid(mdd_tobj), lname, handle, 0);
830 mdd_write_unlock(env, mdd_sobj);
831 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
834 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
835 mdd_tobj, NULL, lname, handle);
837 mdd_trans_stop(env, mdd, rc, handle);
839 #ifdef HAVE_QUOTA_SUPPORT
841 lquota_pending_commit(mds_quota_interface_ref, obd,
842 qids, rec_pending, 1);
843 /* Trigger dqacq for the parent owner. If failed,
844 * the next call for lquota_chkquota will process it. */
845 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
852 int mdd_declare_finish_unlink(const struct lu_env *env,
853 struct mdd_object *obj,
855 struct thandle *handle)
859 rc = orph_declare_index_insert(env, obj, handle);
863 return mdd_declare_object_kill(env, obj, ma, handle);
866 /* caller should take a lock before calling */
867 int mdd_finish_unlink(const struct lu_env *env,
868 struct mdd_object *obj, struct md_attr *ma,
873 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
876 LASSERT(mdd_write_locked(env, obj) != 0);
878 /* read HSM flags, needed to set changelogs flags */
879 ma->ma_need = MA_HSM | MA_INODE;
880 rc = mdd_attr_get_internal(env, obj, ma);
881 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
882 obj->mod_flags |= DEAD_OBJ;
883 /* add new orphan and the object
884 * will be deleted during mdd_close() */
885 if (obj->mod_count) {
886 rc = __mdd_orphan_add(env, obj, th);
888 CDEBUG(D_HA, "Object "DFID" is inserted into "
889 "orphan list, open count = %d\n",
890 PFID(mdd_object_fid(obj)),
893 CERROR("Object "DFID" fail to be an orphan, "
894 "open count = %d, maybe cause failed "
896 PFID(mdd_object_fid(obj)),
899 rc = mdd_object_kill(env, obj, ma, th);
904 /* get the i_nlink */
905 ma->ma_need = MA_INODE;
906 rc = mdd_attr_get_internal(env, obj, ma);
909 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
916 * has mdd_write_lock on cobj already, but not on pobj yet
918 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
919 struct mdd_object *cobj, struct md_attr *ma)
924 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
929 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
930 struct mdd_object *p, struct mdd_object *c,
931 const struct lu_name *name, struct md_attr *ma,
932 struct thandle *handle)
936 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
940 rc = mdo_declare_ref_del(env, p, handle);
944 rc = mdo_declare_ref_del(env, c, handle);
948 rc = mdo_declare_ref_del(env, c, handle);
952 rc = mdo_declare_attr_set(env, p, NULL, handle);
956 rc = mdo_declare_attr_set(env, c, NULL, handle);
960 rc = mdd_declare_finish_unlink(env, c, ma, handle);
964 rc = mdd_declare_links_add(env, c, handle);
968 rc = mdd_declare_changelog_store(env, mdd, name, handle);
973 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
974 struct md_object *cobj, const struct lu_name *lname,
977 const char *name = lname->ln_name;
978 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
979 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
980 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
981 struct mdd_device *mdd = mdo2mdd(pobj);
982 struct dynlock_handle *dlh;
983 struct thandle *handle;
984 #ifdef HAVE_QUOTA_SUPPORT
985 struct obd_device *obd = mdd->mdd_obd_dev;
986 struct mds_obd *mds = &obd->u.mds;
987 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
988 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
991 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
995 if (mdd_object_exists(mdd_cobj) <= 0)
998 handle = mdd_trans_create(env, mdd);
1000 RETURN(PTR_ERR(handle));
1002 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1007 rc = mdd_trans_start(env, mdd, handle);
1011 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1013 GOTO(out_trans, rc = -ENOMEM);
1014 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1016 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1020 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1021 mdd_object_capa(env, mdd_pobj));
1025 mdo_ref_del(env, mdd_cobj, handle);
1028 mdo_ref_del(env, mdd_cobj, handle);
1030 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1031 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1033 la->la_valid = LA_CTIME | LA_MTIME;
1034 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1038 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1039 /* update ctime of an unlinked file only if it is still
1040 * opened or a link still exists */
1041 la->la_valid = LA_CTIME;
1042 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1047 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1048 #ifdef HAVE_QUOTA_SUPPORT
1049 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1050 ma->ma_attr.la_nlink == 0) {
1051 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1053 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1055 mdd_quota_wrapper(la_tmp, qpids);
1056 if (mdd_cobj->mod_count == 0) {
1057 quota_opc = FSFILT_OP_UNLINK;
1058 mdd_quota_wrapper(&ma->ma_attr, qcids);
1060 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1066 /* old files may not have link ea; ignore errors */
1067 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1068 lname, NULL, NULL, handle);
1072 mdd_write_unlock(env, mdd_cobj);
1073 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1078 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1079 if ((ma->ma_valid & MA_HSM) &&
1080 (ma->ma_hsm.mh_flags & HS_EXISTS))
1081 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1083 rc = mdd_changelog_ns_store(env, mdd,
1084 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1085 mdd_cobj, mdd_pobj, NULL, lname, handle);
1089 mdd_trans_stop(env, mdd, rc, handle);
1090 #ifdef HAVE_QUOTA_SUPPORT
1092 /* Trigger dqrel on the owner of child and parent. If failed,
1093 * the next call for lquota_chkquota will process it. */
1094 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1100 /* has not lock on pobj yet */
1101 static int mdd_ni_sanity_check(const struct lu_env *env,
1102 struct md_object *pobj,
1103 const struct md_attr *ma)
1105 struct mdd_object *obj = md2mdd_obj(pobj);
1109 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1112 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1118 * Partial operation.
1120 static int mdd_name_insert(const struct lu_env *env,
1121 struct md_object *pobj,
1122 const struct lu_name *lname,
1123 const struct lu_fid *fid,
1124 const struct md_attr *ma)
1126 const char *name = lname->ln_name;
1127 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1128 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1129 struct mdd_device *mdd = mdo2mdd(pobj);
1130 struct dynlock_handle *dlh;
1131 struct thandle *handle;
1132 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1133 #ifdef HAVE_QUOTA_SUPPORT
1134 struct md_ucred *uc = md_ucred(env);
1135 struct obd_device *obd = mdd->mdd_obd_dev;
1136 struct obd_export *exp = md_quota(env)->mq_exp;
1137 struct mds_obd *mds = &obd->u.mds;
1138 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1139 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1140 cfs_cap_t save = uc->mu_cap;
1145 /* XXX: this code won't be used ever:
1146 * DNE uses slightly different approach */
1149 #ifdef HAVE_QUOTA_SUPPORT
1150 if (mds->mds_quota) {
1151 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1152 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1154 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1157 mdd_data_get(env, mdd_obj, &data);
1158 quota_opc = FSFILT_OP_LINK;
1159 mdd_quota_wrapper(la_tmp, qids);
1160 /* get block quota for parent */
1161 lquota_chkquota(mds_quota_interface_ref, obd,
1162 exp, qids, rec_pending, 1, NULL,
1163 LQUOTA_FLAGS_BLK, data, 1);
1166 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1170 handle = mdd_trans_create(env, mdo2mdd(pobj));
1172 GOTO(out_pending, rc = PTR_ERR(handle));
1174 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1176 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1178 GOTO(out_trans, rc = -ENOMEM);
1180 rc = mdd_ni_sanity_check(env, pobj, ma);
1182 GOTO(out_unlock, rc);
1184 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1185 handle, BYPASS_CAPA);
1187 GOTO(out_unlock, rc);
1190 * For some case, no need update obj's ctime (LA_CTIME is not set),
1192 * For other cases, update obj's ctime (LA_CTIME is set),
1195 if (ma->ma_attr.la_valid & LA_CTIME) {
1196 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1197 la->la_valid = LA_CTIME | LA_MTIME;
1198 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1203 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1205 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1207 #ifdef HAVE_QUOTA_SUPPORT
1208 if (mds->mds_quota) {
1210 lquota_pending_commit(mds_quota_interface_ref,
1211 obd, qids, rec_pending, 1);
1212 /* Trigger dqacq for the parent owner. If failed,
1213 * the next call for lquota_chkquota will process it*/
1214 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1224 /* has not lock on pobj yet */
1225 static int mdd_nr_sanity_check(const struct lu_env *env,
1226 struct md_object *pobj,
1227 const struct md_attr *ma)
1229 struct mdd_object *obj = md2mdd_obj(pobj);
1233 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1236 rc = mdd_may_unlink(env, obj, ma);
1242 * Partial operation.
1244 static int mdd_name_remove(const struct lu_env *env,
1245 struct md_object *pobj,
1246 const struct lu_name *lname,
1247 const struct md_attr *ma)
1249 const char *name = lname->ln_name;
1250 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1251 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1252 struct mdd_device *mdd = mdo2mdd(pobj);
1253 struct dynlock_handle *dlh;
1254 struct thandle *handle;
1255 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1256 #ifdef HAVE_QUOTA_SUPPORT
1257 struct obd_device *obd = mdd->mdd_obd_dev;
1258 struct mds_obd *mds = &obd->u.mds;
1259 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1265 /* XXX: this code won't be used ever:
1266 * DNE uses slightly different approach */
1269 #ifdef HAVE_QUOTA_SUPPORT
1270 if (mds->mds_quota) {
1271 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1273 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1275 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1276 mdd_quota_wrapper(la_tmp, qids);
1280 handle = mdd_trans_create(env, mdd);
1282 GOTO(out_pending, rc = PTR_ERR(handle));
1284 rc = mdd_trans_start(env, mdd, handle);
1286 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1288 GOTO(out_trans, rc = -ENOMEM);
1290 rc = mdd_nr_sanity_check(env, pobj, ma);
1292 GOTO(out_unlock, rc);
1294 rc = __mdd_index_delete(env, mdd_obj, 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, mdd, rc, handle);
1317 #ifdef HAVE_QUOTA_SUPPORT
1318 /* Trigger dqrel for the parent owner.
1319 * If failed, the next call for lquota_chkquota will process it. */
1321 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1329 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1331 static int mdd_rt_sanity_check(const struct lu_env *env,
1332 struct mdd_object *tgt_pobj,
1333 struct mdd_object *tobj,
1339 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1342 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1343 * exist. In fact, tobj must exist, otherwise the call trace will be:
1344 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1345 * When get here, tobj must be NOT NULL, the other case has been
1346 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1348 * So check may_delete, but not check nlink of tgt_pobj. */
1350 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1355 /* Partial rename op on slave MDD */
1356 static int mdd_rename_tgt(const struct lu_env *env,
1357 struct md_object *pobj, struct md_object *tobj,
1358 const struct lu_fid *lf, const struct lu_name *lname,
1361 const char *name = lname->ln_name;
1362 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1363 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1364 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1365 struct mdd_device *mdd = mdo2mdd(pobj);
1366 struct dynlock_handle *dlh;
1367 struct thandle *handle;
1368 #ifdef HAVE_QUOTA_SUPPORT
1369 struct obd_device *obd = mdd->mdd_obd_dev;
1370 struct obd_export *exp = md_quota(env)->mq_exp;
1371 struct mds_obd *mds = &obd->u.mds;
1372 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1373 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1374 int quota_copc = 0, quota_popc = 0;
1375 int rec_pending[MAXQUOTAS] = { 0, 0 };
1380 /* XXX: this code won't be used ever:
1381 * DNE uses slightly different approach */
1384 #ifdef HAVE_QUOTA_SUPPORT
1385 if (mds->mds_quota && !tobj) {
1386 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1388 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1391 mdd_data_get(env, mdd_tpobj, &data);
1392 quota_popc = FSFILT_OP_LINK;
1393 mdd_quota_wrapper(la_tmp, qpids);
1394 /* get block quota for target parent */
1395 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1396 qpids, rec_pending, 1, NULL,
1397 LQUOTA_FLAGS_BLK, data, 1);
1401 handle = mdd_trans_create(env, mdd);
1403 GOTO(out_pending, rc = PTR_ERR(handle));
1405 rc = mdd_trans_start(env, mdd, handle);
1407 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1409 GOTO(out_trans, rc = -ENOMEM);
1411 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1413 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1418 * If rename_tgt is called then we should just re-insert name with
1419 * correct fid, no need to dec/inc parent nlink if obj is dir.
1421 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1425 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1430 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1431 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1433 la->la_valid = LA_CTIME | LA_MTIME;
1434 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1439 * For tobj is remote case cmm layer has processed
1440 * and pass NULL tobj to here. So when tobj is NOT NULL,
1441 * it must be local one.
1443 if (tobj && mdd_object_exists(mdd_tobj)) {
1444 mdo_ref_del(env, mdd_tobj, handle);
1446 /* Remove dot reference. */
1447 if (S_ISDIR(ma->ma_attr.la_mode))
1448 mdo_ref_del(env, mdd_tobj, handle);
1450 la->la_valid = LA_CTIME;
1451 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1455 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1459 #ifdef HAVE_QUOTA_SUPPORT
1460 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1461 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1462 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1463 mdd_quota_wrapper(&ma->ma_attr, qcids);
1470 mdd_write_unlock(env, mdd_tobj);
1471 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1474 /* Bare EXT record with no RENAME in front of it signifies
1475 a partial slave op */
1476 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1477 mdd_tpobj, NULL, lname, handle);
1479 mdd_trans_stop(env, mdd, rc, handle);
1481 #ifdef HAVE_QUOTA_SUPPORT
1482 if (mds->mds_quota) {
1484 lquota_pending_commit(mds_quota_interface_ref, obd,
1485 qpids, rec_pending, 1);
1488 /* Trigger dqrel on the target owner of child.
1489 * If failed, the next call for lquota_chkquota
1490 * will process it. */
1491 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1499 * The permission has been checked when obj created, no need check again.
1501 static int mdd_cd_sanity_check(const struct lu_env *env,
1502 struct mdd_object *obj)
1507 if (!obj || mdd_is_dead_obj(obj))
1514 static int mdd_declare_create_data(const struct lu_env *env,
1515 struct mdd_device *mdd,
1516 struct mdd_object *obj,
1518 struct thandle *handle)
1520 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1524 buf->lb_len = lmm_size;
1525 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1530 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1535 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1536 struct md_object *cobj, const struct md_op_spec *spec,
1539 struct mdd_device *mdd = mdo2mdd(cobj);
1540 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1541 struct mdd_object *son = md2mdd_obj(cobj);
1542 struct lov_mds_md *lmm = NULL;
1544 struct thandle *handle;
1548 rc = mdd_cd_sanity_check(env, son);
1552 if (!md_should_create(spec->sp_cr_flags))
1554 lmm_size = ma->ma_lmm_size;
1556 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size, spec, ma);
1560 handle = mdd_trans_create(env, mdd);
1562 GOTO(out_free, rc = PTR_ERR(handle));
1564 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1568 rc = mdd_trans_start(env, mdd, handle);
1573 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1574 * Should this be fixed?
1577 /* Replay creates has objects already */
1579 if (spec->no_create) {
1580 CDEBUG(D_INFO, "we already have lov ea\n");
1581 rc = mdd_lov_set_md(env, mdd_pobj, son,
1582 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1583 spec->u.sp_ea.eadatalen, handle, 0);
1586 /* No need mdd_lsm_sanity_check here */
1587 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1588 lmm_size, handle, 0);
1591 rc = mdd_attr_get_internal_locked(env, son, ma);
1593 /* update lov_objid data, must be before transaction stop! */
1595 mdd_lov_objid_update(mdd, lmm);
1598 mdd_trans_stop(env, mdd, rc, handle);
1600 /* Finish mdd_lov_create() stuff. */
1601 /* if no_create == 0 (not replay), we free lmm allocated by
1602 * mdd_lov_create() */
1603 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1607 /* Get fid from name and parent */
1609 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1610 const struct lu_name *lname, struct lu_fid* fid, int mask)
1612 const char *name = lname->ln_name;
1613 const struct dt_key *key = (const struct dt_key *)name;
1614 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1615 struct mdd_device *m = mdo2mdd(pobj);
1616 struct dt_object *dir = mdd_object_child(mdd_obj);
1620 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1623 rc = mdd_object_exists(mdd_obj);
1624 if (unlikely(rc == 0))
1626 else if (unlikely(rc < 0)) {
1627 CERROR("Object "DFID" locates on remote server\n",
1628 PFID(mdo2fid(mdd_obj)));
1632 /* The common filename length check. */
1633 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1634 RETURN(-ENAMETOOLONG);
1636 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1641 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1642 dt_try_as_dir(env, dir))) {
1644 rc = dir->do_index_ops->dio_lookup(env, dir,
1645 (struct dt_rec *)fid, key,
1646 mdd_object_capa(env, mdd_obj));
1657 int mdd_declare_object_initialize(const struct lu_env *env,
1658 struct mdd_object *child,
1660 struct thandle *handle)
1664 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1665 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1666 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1669 rc = mdo_declare_ref_add(env, child, handle);
1672 mdd_declare_links_add(env, child, handle);
1677 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1678 const struct lu_name *lname, struct mdd_object *child,
1679 struct md_attr *ma, struct thandle *handle,
1680 const struct md_op_spec *spec)
1686 * Update attributes for child.
1689 * (1) the valid bits should be converted between Lustre and Linux;
1690 * (2) maybe, the child attributes should be set in OSD when creation.
1693 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1697 if (S_ISDIR(ma->ma_attr.la_mode)) {
1698 /* Add "." and ".." for newly created dir */
1699 mdo_ref_add(env, child, handle);
1700 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1701 dot, handle, BYPASS_CAPA);
1703 rc = __mdd_index_insert_only(env, child, pfid,
1707 mdo_ref_del(env, child, handle);
1710 mdd_links_add(env, child, pfid, lname, handle, 1);
1715 /* has not lock on pobj yet */
1716 static int mdd_create_sanity_check(const struct lu_env *env,
1717 struct md_object *pobj,
1718 const struct lu_name *lname,
1720 struct md_op_spec *spec)
1722 struct mdd_thread_info *info = mdd_env_info(env);
1723 struct lu_attr *la = &info->mti_la;
1724 struct lu_fid *fid = &info->mti_fid;
1725 struct mdd_object *obj = md2mdd_obj(pobj);
1726 struct mdd_device *m = mdo2mdd(pobj);
1727 int lookup = spec->sp_cr_lookup;
1732 if (mdd_is_dead_obj(obj))
1736 * In some cases this lookup is not needed - we know before if name
1737 * exists or not because MDT performs lookup for it.
1738 * name length check is done in lookup.
1742 * Check if the name already exist, though it will be checked in
1743 * _index_insert also, for avoiding rolling back if exists
1746 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1747 MAY_WRITE | MAY_EXEC);
1749 RETURN(rc ? : -EEXIST);
1752 * Check WRITE permission for the parent.
1753 * EXEC permission have been checked
1754 * when lookup before create already.
1756 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1763 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1767 if (la->la_mode & S_ISGID) {
1768 ma->ma_attr.la_gid = la->la_gid;
1769 if (S_ISDIR(ma->ma_attr.la_mode)) {
1770 ma->ma_attr.la_mode |= S_ISGID;
1771 ma->ma_attr.la_valid |= LA_MODE;
1775 switch (ma->ma_attr.la_mode & S_IFMT) {
1777 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1779 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1780 RETURN(-ENAMETOOLONG);
1799 static int mdd_declare_create(const struct lu_env *env,
1800 struct mdd_device *mdd,
1801 struct mdd_object *p,
1802 struct mdd_object *c,
1803 const struct lu_name *name,
1806 struct thandle *handle,
1807 const struct md_op_spec *spec)
1809 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1812 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1816 /* if dir, then can inherit default ACl */
1818 buf->lb_len = lmm_size;
1819 if (S_ISDIR(ma->ma_attr.la_mode)) {
1820 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1823 rc = mdo_declare_ref_add(env, p, handle);
1828 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1833 rc = mdd_declare_object_initialize(env, c, ma, handle);
1837 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1838 name->ln_name, handle);
1842 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1847 if (S_ISLNK(ma->ma_attr.la_mode)) {
1848 rc = dt_declare_record_write(env, mdd_object_child(c),
1849 strlen(spec->u.sp_symname), 0,
1854 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1858 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1862 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1870 * Create object and insert it into namespace.
1872 static int mdd_create(const struct lu_env *env,
1873 struct md_object *pobj,
1874 const struct lu_name *lname,
1875 struct md_object *child,
1876 struct md_op_spec *spec,
1879 struct mdd_thread_info *info = mdd_env_info(env);
1880 struct lu_attr *la = &info->mti_la_for_fix;
1881 struct md_attr *ma_acl = &info->mti_ma;
1882 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1883 struct mdd_object *son = md2mdd_obj(child);
1884 struct mdd_device *mdd = mdo2mdd(pobj);
1885 struct lu_attr *attr = &ma->ma_attr;
1886 struct lov_mds_md *lmm = NULL;
1887 struct thandle *handle;
1888 struct dynlock_handle *dlh;
1889 const char *name = lname->ln_name;
1890 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1891 int got_def_acl = 0;
1892 #ifdef HAVE_QUOTA_SUPPORT
1893 struct obd_device *obd = mdd->mdd_obd_dev;
1894 struct obd_export *exp = md_quota(env)->mq_exp;
1895 struct mds_obd *mds = &obd->u.mds;
1896 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1897 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1898 int quota_opc = 0, block_count = 0;
1899 int inode_pending[MAXQUOTAS] = { 0, 0 };
1900 int block_pending[MAXQUOTAS] = { 0, 0 };
1901 int parent_pending[MAXQUOTAS] = { 0, 0 };
1906 * Two operations have to be performed:
1908 * - an allocation of a new object (->do_create()), and
1910 * - an insertion into a parent index (->dio_insert()).
1912 * Due to locking, operation order is not important, when both are
1913 * successful, *but* error handling cases are quite different:
1915 * - if insertion is done first, and following object creation fails,
1916 * insertion has to be rolled back, but this operation might fail
1917 * also leaving us with dangling index entry.
1919 * - if creation is done first, is has to be undone if insertion
1920 * fails, leaving us with leaked space, which is neither good, nor
1923 * It seems that creation-first is simplest solution, but it is
1924 * sub-optimal in the frequent
1929 * case, because second mkdir is bound to create object, only to
1930 * destroy it immediately.
1932 * To avoid this follow local file systems that do double lookup:
1934 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1936 * 1. create (mdd_object_create_internal())
1938 * 2. insert (__mdd_index_insert(), lookup again)
1941 /* Sanity checks before big job. */
1942 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1946 #ifdef HAVE_QUOTA_SUPPORT
1947 if (mds->mds_quota) {
1948 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1950 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1954 quota_opc = FSFILT_OP_CREATE;
1955 mdd_quota_wrapper(&ma->ma_attr, qcids);
1956 mdd_quota_wrapper(la_tmp, qpids);
1957 /* get file quota for child */
1958 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1959 qcids, inode_pending, 1, NULL, 0, NULL,
1961 switch (ma->ma_attr.la_mode & S_IFMT) {
1970 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1971 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1975 /* get block quota for child and parent */
1977 lquota_chkquota(mds_quota_interface_ref, obd,
1978 exp, qcids, block_pending,
1980 LQUOTA_FLAGS_BLK, NULL, 0);
1982 lquota_chkquota(mds_quota_interface_ref, obd,
1983 exp, qpids, parent_pending, 1,
1984 NULL, LQUOTA_FLAGS_BLK, NULL,
1991 * No RPC inside the transaction, so OST objects should be created at
1994 if (S_ISREG(attr->la_mode)) {
1995 lmm_size = ma->ma_lmm_size;
1996 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1999 GOTO(out_pending, rc);
2002 if (!S_ISLNK(attr->la_mode)) {
2003 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2004 ma_acl->ma_acl = info->mti_xattr_buf;
2005 ma_acl->ma_need = MA_ACL_DEF;
2006 ma_acl->ma_valid = 0;
2008 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2009 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2010 mdd_read_unlock(env, mdd_pobj);
2013 else if (ma_acl->ma_valid & MA_ACL_DEF)
2017 handle = mdd_trans_create(env, mdd);
2019 GOTO(out_free, rc = PTR_ERR(handle));
2021 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2022 lmm_size, handle, spec);
2026 rc = mdd_trans_start(env, mdd, handle);
2030 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2032 GOTO(out_trans, rc = -ENOMEM);
2034 mdd_write_lock(env, son, MOR_TGT_CHILD);
2035 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2037 mdd_write_unlock(env, son);
2043 #ifdef CONFIG_FS_POSIX_ACL
2045 struct lu_buf *acl_buf = &info->mti_buf;
2046 acl_buf->lb_buf = ma_acl->ma_acl;
2047 acl_buf->lb_len = ma_acl->ma_acl_size;
2049 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2051 mdd_write_unlock(env, son);
2054 ma->ma_attr.la_valid |= LA_MODE;
2059 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2060 son, ma, handle, spec);
2061 mdd_write_unlock(env, son);
2064 * Object has no links, so it will be destroyed when last
2065 * reference is released. (XXX not now.)
2071 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2072 name, S_ISDIR(attr->la_mode), handle,
2073 mdd_object_capa(env, mdd_pobj));
2080 /* No need mdd_lsm_sanity_check here */
2081 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2083 CERROR("error on stripe info copy %d \n", rc);
2086 if (lmm && lmm_size > 0) {
2087 /* Set Lov here, do not get lmm again later */
2088 memcpy(ma->ma_lmm, lmm, lmm_size);
2089 ma->ma_lmm_size = lmm_size;
2090 ma->ma_valid |= MA_LOV;
2093 if (S_ISLNK(attr->la_mode)) {
2094 struct md_ucred *uc = md_ucred(env);
2095 struct dt_object *dt = mdd_object_child(son);
2096 const char *target_name = spec->u.sp_symname;
2097 int sym_len = strlen(target_name);
2098 const struct lu_buf *buf;
2101 buf = mdd_buf_get_const(env, target_name, sym_len);
2102 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2103 mdd_object_capa(env, son),
2105 CFS_CAP_SYS_RESOURCE_MASK);
2110 GOTO(cleanup, rc = -EFAULT);
2114 la->la_valid = LA_CTIME | LA_MTIME;
2115 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2119 /* Return attr back. */
2120 rc = mdd_attr_get_internal_locked(env, son, ma);
2123 if (rc && created) {
2127 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2128 S_ISDIR(attr->la_mode),
2129 handle, BYPASS_CAPA);
2131 CERROR("error can not cleanup destroy %d\n",
2136 mdd_write_lock(env, son, MOR_TGT_CHILD);
2137 mdo_ref_del(env, son, handle);
2138 if (initialized && S_ISDIR(attr->la_mode))
2139 mdo_ref_del(env, son, handle);
2140 mdd_write_unlock(env, son);
2144 /* update lov_objid data, must be before transaction stop! */
2146 mdd_lov_objid_update(mdd, lmm);
2148 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2151 rc = mdd_changelog_ns_store(env, mdd,
2152 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2153 S_ISREG(attr->la_mode) ? CL_CREATE :
2154 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2155 0, son, mdd_pobj, NULL, lname, handle);
2157 mdd_trans_stop(env, mdd, rc, handle);
2159 /* finish lov_create stuff, free all temporary data */
2160 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2162 #ifdef HAVE_QUOTA_SUPPORT
2164 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2166 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2168 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2170 /* Trigger dqacq on the owner of child and parent. If failed,
2171 * the next call for lquota_chkquota will process it. */
2172 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2180 * Get locks on parents in proper order
2181 * RETURN: < 0 - error, rename_order if successful
2189 static int mdd_rename_order(const struct lu_env *env,
2190 struct mdd_device *mdd,
2191 struct mdd_object *src_pobj,
2192 struct mdd_object *tgt_pobj)
2194 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2198 if (src_pobj == tgt_pobj)
2199 RETURN(MDD_RN_SAME);
2201 /* compared the parent child relationship of src_p&tgt_p */
2202 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2204 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2207 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2220 /* has not mdd_write{read}_lock on any obj yet. */
2221 static int mdd_rename_sanity_check(const struct lu_env *env,
2222 struct mdd_object *src_pobj,
2223 struct mdd_object *tgt_pobj,
2224 struct mdd_object *sobj,
2225 struct mdd_object *tobj,
2231 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2234 /* XXX: when get here, sobj must NOT be NULL,
2235 * the other case has been processed in cml_rename
2236 * before mdd_rename and enable MDS_PERM_BYPASS. */
2239 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2243 /* XXX: when get here, "tobj == NULL" means tobj must
2244 * NOT exist (neither on remote MDS, such case has been
2245 * processed in cml_rename before mdd_rename and enable
2247 * So check may_create, but not check may_unlink. */
2249 rc = mdd_may_create(env, tgt_pobj, NULL,
2250 (src_pobj != tgt_pobj), 0);
2252 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2253 (src_pobj != tgt_pobj), 1);
2255 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2256 S_ISDIR(ma->ma_attr.la_mode))
2257 rc = __mdd_may_link(env, tgt_pobj);
2262 static int mdd_declare_rename(const struct lu_env *env,
2263 struct mdd_device *mdd,
2264 struct mdd_object *mdd_spobj,
2265 struct mdd_object *mdd_tpobj,
2266 struct mdd_object *mdd_sobj,
2267 struct mdd_object *mdd_tobj,
2268 const struct lu_name *sname,
2269 const struct lu_name *tname,
2271 struct thandle *handle)
2279 /* name from source dir */
2280 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2284 /* .. from source child */
2285 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2286 /* source child can be directory,
2287 * counted by source dir's nlink */
2288 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2292 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2296 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2301 /* new target child can be directory,
2302 * counted by target dir's nlink */
2303 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2309 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2313 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2316 mdd_declare_links_add(env, mdd_sobj, handle);
2320 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2325 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2326 tname->ln_name, handle);
2330 /* name from target dir (old name), we declare it unconditionally
2331 * as mdd_rename() calls delete unconditionally as well. so just
2332 * to balance declarations vs calls to change ... */
2333 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2337 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2338 /* delete target child in target parent directory */
2339 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2343 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2344 /* target child can be directory,
2345 * delete "." reference in target child directory */
2346 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2350 /* delete ".." reference in target parent directory */
2351 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2356 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2360 mdd_declare_links_add(env, mdd_tobj, handle);
2364 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2369 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2373 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2380 /* src object can be remote that is why we use only fid and type of object */
2381 static int mdd_rename(const struct lu_env *env,
2382 struct md_object *src_pobj, struct md_object *tgt_pobj,
2383 const struct lu_fid *lf, const struct lu_name *lsname,
2384 struct md_object *tobj, const struct lu_name *ltname,
2387 const char *sname = lsname->ln_name;
2388 const char *tname = ltname->ln_name;
2389 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2390 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2391 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2392 struct mdd_device *mdd = mdo2mdd(src_pobj);
2393 struct mdd_object *mdd_sobj = NULL; /* source object */
2394 struct mdd_object *mdd_tobj = NULL;
2395 struct dynlock_handle *sdlh, *tdlh;
2396 struct thandle *handle;
2397 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2398 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2402 #ifdef HAVE_QUOTA_SUPPORT
2403 struct obd_device *obd = mdd->mdd_obd_dev;
2404 struct obd_export *exp = md_quota(env)->mq_exp;
2405 struct mds_obd *mds = &obd->u.mds;
2406 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2407 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2408 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2409 int quota_copc = 0, quota_popc = 0;
2410 int rec_pending[MAXQUOTAS] = { 0, 0 };
2414 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2415 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2418 mdd_tobj = md2mdd_obj(tobj);
2420 #ifdef HAVE_QUOTA_SUPPORT
2421 if (mds->mds_quota) {
2422 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2424 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2426 mdd_quota_wrapper(la_tmp, qspids);
2428 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2432 mdd_data_get(env, mdd_tpobj, &data);
2433 quota_popc = FSFILT_OP_LINK;
2434 mdd_quota_wrapper(la_tmp, qtpids);
2435 /* get block quota for target parent */
2436 lquota_chkquota(mds_quota_interface_ref,
2438 rec_pending, 1, NULL,
2446 mdd_sobj = mdd_object_find(env, mdd, lf);
2448 handle = mdd_trans_create(env, mdd);
2450 GOTO(out_pending, rc = PTR_ERR(handle));
2452 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2453 mdd_tobj, lsname, ltname, ma, handle);
2457 rc = mdd_trans_start(env, mdd, handle);
2461 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2462 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2464 GOTO(cleanup_unlocked, rc);
2466 /* Get locks in determined order */
2467 if (rc == MDD_RN_SAME) {
2468 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2469 sname, MOR_SRC_PARENT);
2470 /* check hashes to determine do we need one lock or two */
2471 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2472 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2476 } else if (rc == MDD_RN_SRCTGT) {
2477 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2478 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2480 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2481 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2483 if (sdlh == NULL || tdlh == NULL)
2484 GOTO(cleanup, rc = -ENOMEM);
2486 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2487 mdd_sobj, mdd_tobj, ma);
2491 /* Remove source name from source directory */
2492 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2493 mdd_object_capa(env, mdd_spobj));
2497 /* "mv dir1 dir2" needs "dir1/.." link update */
2498 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2499 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2500 mdd_object_capa(env, mdd_sobj));
2502 GOTO(fixup_spobj2, rc);
2504 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2505 handle, mdd_object_capa(env, mdd_sobj));
2507 GOTO(fixup_spobj, rc);
2510 /* Remove target name from target directory
2511 * Here tobj can be remote one, so we do index_delete unconditionally
2512 * and -ENOENT is allowed.
2514 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2515 mdd_object_capa(env, mdd_tpobj));
2518 /* tname might been renamed to something else */
2519 GOTO(fixup_spobj, rc);
2522 GOTO(fixup_spobj, rc);
2525 /* Insert new fid with target name into target dir */
2526 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2527 mdd_object_capa(env, mdd_tpobj));
2529 GOTO(fixup_tpobj, rc);
2531 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2532 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2534 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2536 la->la_valid = LA_CTIME;
2537 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2540 GOTO(fixup_tpobj, rc);
2543 /* Remove old target object
2544 * For tobj is remote case cmm layer has processed
2545 * and set tobj to NULL then. So when tobj is NOT NULL,
2546 * it must be local one.
2548 if (tobj && mdd_object_exists(mdd_tobj)) {
2549 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2550 if (mdd_is_dead_obj(mdd_tobj)) {
2551 mdd_write_unlock(env, mdd_tobj);
2552 /* shld not be dead, something is wrong */
2553 CERROR("tobj is dead, something is wrong\n");
2557 mdo_ref_del(env, mdd_tobj, handle);
2559 /* Remove dot reference. */
2561 mdo_ref_del(env, mdd_tobj, handle);
2563 la->la_valid = LA_CTIME;
2564 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2566 GOTO(fixup_tpobj, rc);
2568 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2569 mdd_write_unlock(env, mdd_tobj);
2571 GOTO(fixup_tpobj, rc);
2573 #ifdef HAVE_QUOTA_SUPPORT
2574 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2575 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2576 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2577 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2582 la->la_valid = LA_CTIME | LA_MTIME;
2583 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2585 GOTO(fixup_tpobj, rc);
2587 if (mdd_spobj != mdd_tpobj) {
2588 la->la_valid = LA_CTIME | LA_MTIME;
2589 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2593 if (rc == 0 && mdd_sobj) {
2594 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2595 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2596 mdo2fid(mdd_tpobj), ltname, handle);
2598 /* Old files might not have EA entry */
2599 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2601 mdd_write_unlock(env, mdd_sobj);
2602 /* We don't fail the transaction if the link ea can't be
2603 updated -- fid2path will use alternate lookup method. */
2611 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2614 CWARN("tp obj fix error %d\n",rc2);
2616 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2617 !mdd_is_dead_obj(mdd_tobj)) {
2618 rc2 = __mdd_index_insert(env, mdd_tpobj,
2619 mdo2fid(mdd_tobj), tname,
2624 CWARN("tp obj fix error %d\n",rc2);
2629 if (rc && is_dir && mdd_sobj) {
2630 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2634 CWARN("sp obj dotdot delete error %d\n",rc2);
2637 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2638 dotdot, handle, BYPASS_CAPA);
2640 CWARN("sp obj dotdot insert error %d\n",rc2);
2645 rc2 = __mdd_index_insert(env, mdd_spobj,
2646 lf, sname, is_dir, handle, BYPASS_CAPA);
2648 CWARN("sp obj fix error %d\n",rc2);
2651 if (likely(tdlh) && sdlh != tdlh)
2652 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2654 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2657 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2658 mdd_spobj, lf, lsname, handle);
2660 struct lu_fid zero_fid;
2661 fid_zero(&zero_fid);
2662 /* If the rename target exist, The CL_EXT record should save
2663 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2664 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2666 mdd_tobj ? NULL : &zero_fid,
2671 mdd_trans_stop(env, mdd, rc, handle);
2673 mdd_object_put(env, mdd_sobj);
2675 #ifdef HAVE_QUOTA_SUPPORT
2676 if (mds->mds_quota) {
2678 lquota_pending_commit(mds_quota_interface_ref, obd,
2679 qtpids, rec_pending, 1);
2682 /* Trigger dqrel on the source owner of parent.
2683 * If failed, the next call for lquota_chkquota will
2685 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2686 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2688 /* Trigger dqrel on the target owner of child.
2689 * If failed, the next call for lquota_chkquota
2690 * will process it. */
2691 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2692 qtpids, rc, quota_copc);
2699 /** enable/disable storing of hardlink info */
2700 int mdd_linkea_enable = 1;
2701 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2702 "record hardlink info in EAs");
2704 /** Read the link EA into a temp buffer.
2705 * Uses the name_buf since it is generally large.
2706 * \retval IS_ERR err
2707 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2709 struct lu_buf *mdd_links_get(const struct lu_env *env,
2710 struct mdd_object *mdd_obj)
2713 struct lustre_capa *capa;
2714 struct link_ea_header *leh;
2717 /* First try a small buf */
2718 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2719 if (buf->lb_buf == NULL)
2720 return ERR_PTR(-ENOMEM);
2722 capa = mdd_object_capa(env, mdd_obj);
2723 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2724 if (rc == -ERANGE) {
2725 /* Buf was too small, figure out what we need. */
2727 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2730 buf = mdd_buf_alloc(env, rc);
2731 if (buf->lb_buf == NULL)
2732 return ERR_PTR(-ENOMEM);
2733 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2739 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2740 leh->leh_magic = LINK_EA_MAGIC;
2741 leh->leh_reccount = __swab32(leh->leh_reccount);
2742 leh->leh_len = __swab64(leh->leh_len);
2743 /* entries are swabbed by mdd_lee_unpack */
2745 if (leh->leh_magic != LINK_EA_MAGIC)
2746 return ERR_PTR(-EINVAL);
2747 if (leh->leh_reccount == 0)
2748 return ERR_PTR(-ENODATA);
2753 /** Pack a link_ea_entry.
2754 * All elements are stored as chars to avoid alignment issues.
2755 * Numbers are always big-endian
2756 * \retval record length
2758 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2759 const struct lu_fid *pfid)
2761 struct lu_fid tmpfid;
2764 fid_cpu_to_be(&tmpfid, pfid);
2765 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2766 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2767 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2769 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2770 lee->lee_reclen[1] = reclen & 0xff;
2774 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2775 struct lu_name *lname, struct lu_fid *pfid)
2777 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2778 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2779 fid_be_to_cpu(pfid, pfid);
2780 lname->ln_name = lee->lee_name;
2781 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2784 /** Add a record to the end of link ea buf */
2785 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2786 const struct lu_fid *pfid,
2787 const struct lu_name *lname)
2789 struct link_ea_header *leh;
2790 struct link_ea_entry *lee;
2793 if (lname == NULL || pfid == NULL)
2796 /* Make sure our buf is big enough for the new one */
2798 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2799 if (leh->leh_len + reclen > buf->lb_len) {
2800 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2805 lee = buf->lb_buf + leh->leh_len;
2806 reclen = mdd_lee_pack(lee, lname, pfid);
2807 leh->leh_len += reclen;
2808 leh->leh_reccount++;
2812 static int mdd_declare_links_add(const struct lu_env *env,
2813 struct mdd_object *mdd_obj,
2814 struct thandle *handle)
2818 /* XXX: max size? */
2819 rc = mdo_declare_xattr_set(env, mdd_obj,
2820 mdd_buf_get_const(env, NULL, 4096),
2821 XATTR_NAME_LINK, 0, handle);
2826 /* For pathologic linkers, we don't want to spend lots of time scanning the
2827 * link ea. Limit ourseleves to something reasonable; links not in the EA
2828 * can be looked up via (slower) parent lookup.
2830 #define LINKEA_MAX_COUNT 128
2832 static int mdd_links_add(const struct lu_env *env,
2833 struct mdd_object *mdd_obj,
2834 const struct lu_fid *pfid,
2835 const struct lu_name *lname,
2836 struct thandle *handle, int first)
2839 struct link_ea_header *leh;
2843 if (!mdd_linkea_enable)
2846 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2849 if (rc != -ENODATA) {
2850 CERROR("link_ea read failed %d "DFID"\n", rc,
2851 PFID(mdd_object_fid(mdd_obj)));
2854 /* empty EA; start one */
2855 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2856 if (buf->lb_buf == NULL)
2859 leh->leh_magic = LINK_EA_MAGIC;
2860 leh->leh_len = sizeof(struct link_ea_header);
2861 leh->leh_reccount = 0;
2865 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2868 rc = __mdd_links_add(env, buf, pfid, lname);
2873 rc = __mdd_xattr_set(env, mdd_obj,
2874 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2875 XATTR_NAME_LINK, 0, handle);
2878 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2879 PFID(mdd_object_fid(mdd_obj)));
2881 CERROR("link_ea add failed %d "DFID"\n", rc,
2882 PFID(mdd_object_fid(mdd_obj)));
2885 if (buf->lb_len > OBD_ALLOC_BIG)
2886 /* if we vmalloced a large buffer drop it */
2892 static int mdd_links_rename(const struct lu_env *env,
2893 struct mdd_object *mdd_obj,
2894 const struct lu_fid *oldpfid,
2895 const struct lu_name *oldlname,
2896 const struct lu_fid *newpfid,
2897 const struct lu_name *newlname,
2898 struct thandle *handle)
2901 struct link_ea_header *leh;
2902 struct link_ea_entry *lee;
2903 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2904 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2910 if (!mdd_linkea_enable)
2913 if (mdd_obj->mod_flags & DEAD_OBJ)
2914 /* No more links, don't bother */
2917 buf = mdd_links_get(env, mdd_obj);
2921 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2922 rc, PFID(mdd_object_fid(mdd_obj)));
2924 CERROR("link_ea read failed %d "DFID"\n",
2925 rc, PFID(mdd_object_fid(mdd_obj)));
2929 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2931 /* Find the old record */
2932 for(count = 0; count < leh->leh_reccount; count++) {
2933 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2934 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2935 lu_fid_eq(tmpfid, oldpfid) &&
2936 (strncmp(tmpname->ln_name, oldlname->ln_name,
2937 tmpname->ln_namelen) == 0))
2939 lee = (struct link_ea_entry *)((char *)lee + reclen);
2941 if ((count + 1) > leh->leh_reccount) {
2942 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2943 oldlname->ln_namelen, oldlname->ln_name);
2944 GOTO(out, rc = -ENOENT);
2947 /* Remove the old record */
2948 leh->leh_reccount--;
2949 leh->leh_len -= reclen;
2950 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2953 /* If renaming, add the new record */
2954 if (newpfid != NULL) {
2955 /* if the add fails, we still delete the out-of-date old link */
2956 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2960 rc = __mdd_xattr_set(env, mdd_obj,
2961 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2962 XATTR_NAME_LINK, 0, handle);
2968 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2969 oldlname->ln_namelen, oldlname->ln_name, rc,
2970 PFID(mdd_object_fid(mdd_obj)));
2972 if (buf->lb_len > OBD_ALLOC_BIG)
2973 /* if we vmalloced a large buffer drop it */
2979 const struct md_dir_operations mdd_dir_ops = {
2980 .mdo_is_subdir = mdd_is_subdir,
2981 .mdo_lookup = mdd_lookup,
2982 .mdo_create = mdd_create,
2983 .mdo_rename = mdd_rename,
2984 .mdo_link = mdd_link,
2985 .mdo_unlink = mdd_unlink,
2986 .mdo_lum_lmm_cmp = mdd_lum_lmm_cmp,
2987 .mdo_name_insert = mdd_name_insert,
2988 .mdo_name_remove = mdd_name_remove,
2989 .mdo_rename_tgt = mdd_rename_tgt,
2990 .mdo_create_data = mdd_create_data,