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);
498 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
499 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
500 * (maybe too large to be represented). It is a trick to break through the
501 * "i_nlink" limitation for subdir count.
503 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
504 struct thandle *handle)
506 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
507 struct mdd_device *m = mdd_obj2mdd_dev(obj);
509 if (!mdd_is_mnlink(obj)) {
510 if (S_ISDIR(mdd_object_type(obj))) {
511 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
514 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
515 obj->mod_flags |= MNLINK_OBJ;
516 tmp_la->la_nlink = 1;
517 tmp_la->la_valid = LA_NLINK;
518 mdd_attr_set_internal(env, obj, tmp_la, handle,
523 mdo_ref_add(env, obj, handle);
527 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
528 struct thandle *handle, int is_dot)
530 if (!mdd_is_mnlink(obj) || is_dot)
531 mdo_ref_del(env, obj, handle);
534 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
535 const char *name, struct thandle *handle,
536 struct lustre_capa *capa)
538 struct dt_object *next = mdd_object_child(pobj);
542 if (dt_try_as_dir(env, next)) {
543 rc = next->do_index_ops->dio_delete(env, next,
544 (struct dt_key *)name,
552 static int __mdd_index_insert_only(const struct lu_env *env,
553 struct mdd_object *pobj,
554 const struct lu_fid *lf, const char *name,
555 struct thandle *handle,
556 struct lustre_capa *capa)
558 struct dt_object *next = mdd_object_child(pobj);
562 if (dt_try_as_dir(env, next)) {
563 struct md_ucred *uc = md_ucred(env);
565 rc = next->do_index_ops->dio_insert(env, next,
567 (const struct dt_key *)name,
568 handle, capa, uc->mu_cap &
569 CFS_CAP_SYS_RESOURCE_MASK);
576 /* insert named index, add reference if isdir */
577 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
578 const struct lu_fid *lf, const char *name, int is_dir,
579 struct thandle *handle, struct lustre_capa *capa)
584 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
585 if (rc == 0 && is_dir) {
586 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
587 __mdd_ref_add(env, pobj, handle);
588 mdd_write_unlock(env, pobj);
593 /* delete named index, drop reference if isdir */
594 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
595 const char *name, int is_dir, struct thandle *handle,
596 struct lustre_capa *capa)
601 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
602 if (rc == 0 && is_dir) {
605 if (name != NULL && name[0] == '.' && name[1] == 0)
607 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
608 __mdd_ref_del(env, pobj, handle, is_dot);
609 mdd_write_unlock(env, pobj);
615 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
616 int reclen, struct thandle *handle)
620 /* XXX: this is a temporary solution to declare llog changes
621 * will be fixed in 2.3 with new llog implementation */
623 LASSERT(mdd->mdd_capa);
626 rc = dt_declare_record_write(env, mdd->mdd_capa, reclen, 0, handle);
630 /* header will be updated as well */
631 rc = dt_declare_record_write(env, mdd->mdd_capa, LLOG_CHUNK_SIZE,
636 /* also we should be able to create new plain log */
637 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
641 /* new record referencing new plain llog */
642 rc = dt_declare_record_write(env, mdd->mdd_capa,
643 sizeof(struct llog_logid_rec), 0, handle);
647 /* catalog's header will be updated as well */
648 rc = dt_declare_record_write(env, mdd->mdd_capa, LLOG_CHUNK_SIZE,
654 int mdd_declare_changelog_store(const struct lu_env *env,
655 struct mdd_device *mdd,
656 const struct lu_name *fname,
657 struct thandle *handle)
662 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
665 /* we'll be writing payload + llog header */
666 reclen = sizeof(struct llog_changelog_rec);
668 reclen += llog_data_len(fname->ln_namelen);
670 return mdd_declare_llog_record(env, mdd, reclen, handle);
673 /** Store a namespace change changelog record
674 * If this fails, we must fail the whole transaction; we don't
675 * want the change to commit without the log entry.
676 * \param target - mdd_object of change
677 * \param parent - parent dir/object
678 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
679 * \param tname - target name string
680 * \param handle - transacion handle
682 static int mdd_changelog_ns_store(const struct lu_env *env,
683 struct mdd_device *mdd,
684 enum changelog_rec_type type,
686 struct mdd_object *target,
687 struct mdd_object *parent,
688 const struct lu_fid *tf,
689 const struct lu_name *tname,
690 struct thandle *handle)
692 const struct lu_fid *tfid;
693 const struct lu_fid *tpfid = mdo2fid(parent);
694 struct llog_changelog_rec *rec;
701 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
703 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
706 LASSERT(parent != NULL);
707 LASSERT(tname != NULL);
708 LASSERT(handle != NULL);
711 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
712 buf = mdd_buf_alloc(env, reclen);
713 if (buf->lb_buf == NULL)
715 rec = (struct llog_changelog_rec *)buf->lb_buf;
717 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
718 rec->cr.cr_type = (__u32)type;
719 tfid = tf ? tf : mdo2fid(target);
720 rec->cr.cr_tfid = *tfid;
721 rec->cr.cr_pfid = *tpfid;
722 rec->cr.cr_namelen = tname->ln_namelen;
723 memcpy(rec->cr.cr_name, tname->ln_name, rec->cr.cr_namelen);
725 target->mod_cltime = cfs_time_current_64();
727 rc = mdd_changelog_llog_write(mdd, rec, handle);
729 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
730 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
737 static int mdd_declare_link(const struct lu_env *env,
738 struct mdd_device *mdd,
739 struct mdd_object *p,
740 struct mdd_object *c,
741 const struct lu_name *name,
742 struct thandle *handle)
746 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
750 rc = mdo_declare_ref_add(env, c, handle);
754 rc = mdo_declare_attr_set(env, p, NULL, handle);
758 rc = mdo_declare_attr_set(env, c, NULL, handle);
762 rc = mdd_declare_links_add(env, c, handle);
766 rc = mdd_declare_changelog_store(env, mdd, name, handle);
771 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
772 struct md_object *src_obj, const struct lu_name *lname,
775 const char *name = lname->ln_name;
776 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
777 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
778 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
779 struct mdd_device *mdd = mdo2mdd(src_obj);
780 struct dynlock_handle *dlh;
781 struct thandle *handle;
782 #ifdef HAVE_QUOTA_SUPPORT
783 struct obd_device *obd = mdd->mdd_obd_dev;
784 struct obd_export *exp = md_quota(env)->mq_exp;
785 struct mds_obd *mds = &obd->u.mds;
786 unsigned int qids[MAXQUOTAS] = { 0, 0 };
787 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
792 #ifdef HAVE_QUOTA_SUPPORT
793 if (mds->mds_quota) {
794 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
796 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
799 mdd_data_get(env, mdd_tobj, &data);
800 quota_opc = FSFILT_OP_LINK;
801 mdd_quota_wrapper(la_tmp, qids);
802 /* get block quota for parent */
803 lquota_chkquota(mds_quota_interface_ref, obd, exp,
804 qids, rec_pending, 1, NULL,
805 LQUOTA_FLAGS_BLK, data, 1);
810 handle = mdd_trans_create(env, mdd);
812 GOTO(out_pending, rc = PTR_ERR(handle));
814 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
818 rc = mdd_trans_start(env, mdd, handle);
822 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
824 GOTO(out_trans, rc = -ENOMEM);
825 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
827 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
829 GOTO(out_unlock, rc);
831 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
833 mdd_object_capa(env, mdd_tobj));
835 GOTO(out_unlock, rc);
837 __mdd_ref_add(env, mdd_sobj, handle);
839 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
840 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
842 la->la_valid = LA_CTIME | LA_MTIME;
843 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
845 GOTO(out_unlock, rc);
847 la->la_valid = LA_CTIME;
848 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
850 mdd_links_add(env, mdd_sobj,
851 mdo2fid(mdd_tobj), lname, handle, 0);
856 mdd_write_unlock(env, mdd_sobj);
857 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
860 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
861 mdd_tobj, NULL, lname, handle);
863 mdd_trans_stop(env, mdd, rc, handle);
865 #ifdef HAVE_QUOTA_SUPPORT
867 lquota_pending_commit(mds_quota_interface_ref, obd,
868 qids, rec_pending, 1);
869 /* Trigger dqacq for the parent owner. If failed,
870 * the next call for lquota_chkquota will process it. */
871 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
878 int mdd_declare_finish_unlink(const struct lu_env *env,
879 struct mdd_object *obj,
881 struct thandle *handle)
885 rc = orph_declare_index_insert(env, obj, handle);
889 return mdd_declare_object_kill(env, obj, ma, handle);
892 /* caller should take a lock before calling */
893 int mdd_finish_unlink(const struct lu_env *env,
894 struct mdd_object *obj, struct md_attr *ma,
901 LASSERT(mdd_write_locked(env, obj) != 0);
903 /* read HSM flags, needed to set changelogs flags */
904 ma->ma_need = MA_HSM | MA_INODE;
905 rc = mdd_attr_get_internal(env, obj, ma);
906 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
907 obj->mod_flags |= DEAD_OBJ;
908 /* add new orphan and the object
909 * will be deleted during mdd_close() */
910 if (obj->mod_count) {
911 rc = __mdd_orphan_add(env, obj, th);
913 CDEBUG(D_HA, "Object "DFID" is inserted into "
914 "orphan list, open count = %d\n",
915 PFID(mdd_object_fid(obj)),
918 CERROR("Object "DFID" fail to be an orphan, "
919 "open count = %d, maybe cause failed "
921 PFID(mdd_object_fid(obj)),
924 rc = mdd_object_kill(env, obj, ma, th);
931 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
938 * has mdd_write_lock on cobj already, but not on pobj yet
940 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
941 struct mdd_object *cobj, struct md_attr *ma)
946 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
951 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
952 struct mdd_object *p, struct mdd_object *c,
953 const struct lu_name *name, struct md_attr *ma,
954 struct thandle *handle)
958 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
962 rc = mdo_declare_ref_del(env, p, handle);
966 rc = mdo_declare_ref_del(env, c, handle);
970 rc = mdo_declare_ref_del(env, c, handle);
974 rc = mdo_declare_attr_set(env, p, NULL, handle);
978 rc = mdo_declare_attr_set(env, c, NULL, handle);
982 rc = mdd_declare_finish_unlink(env, c, ma, handle);
986 rc = mdd_declare_links_add(env, c, handle);
990 rc = mdd_declare_changelog_store(env, mdd, name, handle);
995 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
996 struct md_object *cobj, const struct lu_name *lname,
999 const char *name = lname->ln_name;
1000 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1001 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1002 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1003 struct mdd_device *mdd = mdo2mdd(pobj);
1004 struct dynlock_handle *dlh;
1005 struct thandle *handle;
1006 #ifdef HAVE_QUOTA_SUPPORT
1007 struct obd_device *obd = mdd->mdd_obd_dev;
1008 struct mds_obd *mds = &obd->u.mds;
1009 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1010 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1013 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1017 if (mdd_object_exists(mdd_cobj) <= 0)
1020 handle = mdd_trans_create(env, mdd);
1022 RETURN(PTR_ERR(handle));
1024 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1029 rc = mdd_trans_start(env, mdd, handle);
1033 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1035 GOTO(out_trans, rc = -ENOMEM);
1036 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1038 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
1042 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1043 mdd_object_capa(env, mdd_pobj));
1047 __mdd_ref_del(env, mdd_cobj, handle, 0);
1050 __mdd_ref_del(env, mdd_cobj, handle, 1);
1052 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1053 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1055 la->la_valid = LA_CTIME | LA_MTIME;
1056 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1060 if (ma->ma_attr.la_nlink > 0 || mdd_cobj->mod_count > 0) {
1061 /* update ctime of an unlinked file only if it is still
1062 * opened or a link still exists */
1063 la->la_valid = LA_CTIME;
1064 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1069 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1070 #ifdef HAVE_QUOTA_SUPPORT
1071 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1072 ma->ma_attr.la_nlink == 0) {
1073 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1075 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1077 mdd_quota_wrapper(la_tmp, qpids);
1078 if (mdd_cobj->mod_count == 0) {
1079 quota_opc = FSFILT_OP_UNLINK;
1080 mdd_quota_wrapper(&ma->ma_attr, qcids);
1082 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1088 /* old files may not have link ea; ignore errors */
1089 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1090 lname, NULL, NULL, handle);
1094 mdd_write_unlock(env, mdd_cobj);
1095 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1100 cl_flags = (ma->ma_attr.la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1101 if ((ma->ma_valid & MA_HSM) &&
1102 (ma->ma_hsm.mh_flags & HS_EXISTS))
1103 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1105 rc = mdd_changelog_ns_store(env, mdd,
1106 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1107 mdd_cobj, mdd_pobj, NULL, lname, handle);
1111 mdd_trans_stop(env, mdd, rc, handle);
1112 #ifdef HAVE_QUOTA_SUPPORT
1114 /* Trigger dqrel on the owner of child and parent. If failed,
1115 * the next call for lquota_chkquota will process it. */
1116 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1122 /* has not lock on pobj yet */
1123 static int mdd_ni_sanity_check(const struct lu_env *env,
1124 struct md_object *pobj,
1125 const struct md_attr *ma)
1127 struct mdd_object *obj = md2mdd_obj(pobj);
1131 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1134 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
1140 * Partial operation.
1142 static int mdd_name_insert(const struct lu_env *env,
1143 struct md_object *pobj,
1144 const struct lu_name *lname,
1145 const struct lu_fid *fid,
1146 const struct md_attr *ma)
1148 const char *name = lname->ln_name;
1149 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1150 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1151 struct mdd_device *mdd = mdo2mdd(pobj);
1152 struct dynlock_handle *dlh;
1153 struct thandle *handle;
1154 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1155 #ifdef HAVE_QUOTA_SUPPORT
1156 struct md_ucred *uc = md_ucred(env);
1157 struct obd_device *obd = mdd->mdd_obd_dev;
1158 struct obd_export *exp = md_quota(env)->mq_exp;
1159 struct mds_obd *mds = &obd->u.mds;
1160 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1161 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
1162 cfs_cap_t save = uc->mu_cap;
1167 /* XXX: this code won't be used ever:
1168 * DNE uses slightly different approach */
1171 #ifdef HAVE_QUOTA_SUPPORT
1172 if (mds->mds_quota) {
1173 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
1174 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1176 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1179 mdd_data_get(env, mdd_obj, &data);
1180 quota_opc = FSFILT_OP_LINK;
1181 mdd_quota_wrapper(la_tmp, qids);
1182 /* get block quota for parent */
1183 lquota_chkquota(mds_quota_interface_ref, obd,
1184 exp, qids, rec_pending, 1, NULL,
1185 LQUOTA_FLAGS_BLK, data, 1);
1188 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
1192 handle = mdd_trans_create(env, mdo2mdd(pobj));
1194 GOTO(out_pending, rc = PTR_ERR(handle));
1196 rc = mdd_trans_start(env, mdo2mdd(pobj), handle);
1198 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1200 GOTO(out_trans, rc = -ENOMEM);
1202 rc = mdd_ni_sanity_check(env, pobj, ma);
1204 GOTO(out_unlock, rc);
1206 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1207 handle, BYPASS_CAPA);
1209 GOTO(out_unlock, rc);
1212 * For some case, no need update obj's ctime (LA_CTIME is not set),
1214 * For other cases, update obj's ctime (LA_CTIME is set),
1217 if (ma->ma_attr.la_valid & LA_CTIME) {
1218 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1219 la->la_valid = LA_CTIME | LA_MTIME;
1220 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1225 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1227 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1229 #ifdef HAVE_QUOTA_SUPPORT
1230 if (mds->mds_quota) {
1232 lquota_pending_commit(mds_quota_interface_ref,
1233 obd, qids, rec_pending, 1);
1234 /* Trigger dqacq for the parent owner. If failed,
1235 * the next call for lquota_chkquota will process it*/
1236 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1246 /* has not lock on pobj yet */
1247 static int mdd_nr_sanity_check(const struct lu_env *env,
1248 struct md_object *pobj,
1249 const struct md_attr *ma)
1251 struct mdd_object *obj = md2mdd_obj(pobj);
1255 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1258 rc = mdd_may_unlink(env, obj, ma);
1264 * Partial operation.
1266 static int mdd_name_remove(const struct lu_env *env,
1267 struct md_object *pobj,
1268 const struct lu_name *lname,
1269 const struct md_attr *ma)
1271 const char *name = lname->ln_name;
1272 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1273 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1274 struct mdd_device *mdd = mdo2mdd(pobj);
1275 struct dynlock_handle *dlh;
1276 struct thandle *handle;
1277 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1278 #ifdef HAVE_QUOTA_SUPPORT
1279 struct obd_device *obd = mdd->mdd_obd_dev;
1280 struct mds_obd *mds = &obd->u.mds;
1281 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1287 /* XXX: this code won't be used ever:
1288 * DNE uses slightly different approach */
1291 #ifdef HAVE_QUOTA_SUPPORT
1292 if (mds->mds_quota) {
1293 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1295 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1297 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1298 mdd_quota_wrapper(la_tmp, qids);
1302 handle = mdd_trans_create(env, mdd);
1304 GOTO(out_pending, rc = PTR_ERR(handle));
1306 rc = mdd_trans_start(env, mdd, handle);
1308 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1310 GOTO(out_trans, rc = -ENOMEM);
1312 rc = mdd_nr_sanity_check(env, pobj, ma);
1314 GOTO(out_unlock, rc);
1316 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1317 handle, BYPASS_CAPA);
1319 GOTO(out_unlock, rc);
1322 * For some case, no need update obj's ctime (LA_CTIME is not set),
1324 * For other cases, update obj's ctime (LA_CTIME is set),
1327 if (ma->ma_attr.la_valid & LA_CTIME) {
1328 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1329 la->la_valid = LA_CTIME | LA_MTIME;
1330 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1335 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1337 mdd_trans_stop(env, mdd, rc, handle);
1339 #ifdef HAVE_QUOTA_SUPPORT
1340 /* Trigger dqrel for the parent owner.
1341 * If failed, the next call for lquota_chkquota will process it. */
1343 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1351 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1353 static int mdd_rt_sanity_check(const struct lu_env *env,
1354 struct mdd_object *tgt_pobj,
1355 struct mdd_object *tobj,
1361 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1364 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1365 * exist. In fact, tobj must exist, otherwise the call trace will be:
1366 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1367 * When get here, tobj must be NOT NULL, the other case has been
1368 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1370 * So check may_delete, but not check nlink of tgt_pobj. */
1372 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1377 /* Partial rename op on slave MDD */
1378 static int mdd_rename_tgt(const struct lu_env *env,
1379 struct md_object *pobj, struct md_object *tobj,
1380 const struct lu_fid *lf, const struct lu_name *lname,
1383 const char *name = lname->ln_name;
1384 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1385 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1386 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1387 struct mdd_device *mdd = mdo2mdd(pobj);
1388 struct dynlock_handle *dlh;
1389 struct thandle *handle;
1390 #ifdef HAVE_QUOTA_SUPPORT
1391 struct obd_device *obd = mdd->mdd_obd_dev;
1392 struct obd_export *exp = md_quota(env)->mq_exp;
1393 struct mds_obd *mds = &obd->u.mds;
1394 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1395 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1396 int quota_copc = 0, quota_popc = 0;
1397 int rec_pending[MAXQUOTAS] = { 0, 0 };
1402 /* XXX: this code won't be used ever:
1403 * DNE uses slightly different approach */
1406 #ifdef HAVE_QUOTA_SUPPORT
1407 if (mds->mds_quota && !tobj) {
1408 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1410 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1413 mdd_data_get(env, mdd_tpobj, &data);
1414 quota_popc = FSFILT_OP_LINK;
1415 mdd_quota_wrapper(la_tmp, qpids);
1416 /* get block quota for target parent */
1417 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1418 qpids, rec_pending, 1, NULL,
1419 LQUOTA_FLAGS_BLK, data, 1);
1423 handle = mdd_trans_create(env, mdd);
1425 GOTO(out_pending, rc = PTR_ERR(handle));
1427 rc = mdd_trans_start(env, mdd, handle);
1429 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1431 GOTO(out_trans, rc = -ENOMEM);
1433 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1435 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1440 * If rename_tgt is called then we should just re-insert name with
1441 * correct fid, no need to dec/inc parent nlink if obj is dir.
1443 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1447 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1452 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1453 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1455 la->la_valid = LA_CTIME | LA_MTIME;
1456 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1461 * For tobj is remote case cmm layer has processed
1462 * and pass NULL tobj to here. So when tobj is NOT NULL,
1463 * it must be local one.
1465 if (tobj && mdd_object_exists(mdd_tobj)) {
1466 __mdd_ref_del(env, mdd_tobj, handle, 0);
1468 /* Remove dot reference. */
1469 if (S_ISDIR(ma->ma_attr.la_mode))
1470 __mdd_ref_del(env, mdd_tobj, handle, 1);
1472 la->la_valid = LA_CTIME;
1473 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1477 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1481 #ifdef HAVE_QUOTA_SUPPORT
1482 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1483 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1484 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1485 mdd_quota_wrapper(&ma->ma_attr, qcids);
1492 mdd_write_unlock(env, mdd_tobj);
1493 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1496 /* Bare EXT record with no RENAME in front of it signifies
1497 a partial slave op */
1498 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1499 mdd_tpobj, NULL, lname, handle);
1501 mdd_trans_stop(env, mdd, rc, handle);
1503 #ifdef HAVE_QUOTA_SUPPORT
1504 if (mds->mds_quota) {
1506 lquota_pending_commit(mds_quota_interface_ref, obd,
1507 qpids, rec_pending, 1);
1510 /* Trigger dqrel on the target owner of child.
1511 * If failed, the next call for lquota_chkquota
1512 * will process it. */
1513 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1521 * The permission has been checked when obj created, no need check again.
1523 static int mdd_cd_sanity_check(const struct lu_env *env,
1524 struct mdd_object *obj)
1529 if (!obj || mdd_is_dead_obj(obj))
1536 static int mdd_declare_create_data(const struct lu_env *env,
1537 struct mdd_device *mdd,
1538 struct mdd_object *obj,
1540 struct thandle *handle)
1542 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1546 buf->lb_len = lmm_size;
1547 rc = mdo_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
1552 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1557 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1558 struct md_object *cobj, const struct md_op_spec *spec,
1561 struct mdd_device *mdd = mdo2mdd(cobj);
1562 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1563 struct mdd_object *son = md2mdd_obj(cobj);
1564 struct lu_attr *attr = &ma->ma_attr;
1565 struct lov_mds_md *lmm = NULL;
1567 struct thandle *handle;
1571 rc = mdd_cd_sanity_check(env, son);
1575 if (!md_should_create(spec->sp_cr_flags))
1577 lmm_size = ma->ma_lmm_size;
1578 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1583 handle = mdd_trans_create(env, mdd);
1585 GOTO(out_free, rc = PTR_ERR(handle));
1587 rc = mdd_declare_create_data(env, mdd, son, lmm_size, handle);
1591 rc = mdd_trans_start(env, mdd, handle);
1596 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1597 * Should this be fixed?
1600 /* Replay creates has objects already */
1602 if (spec->no_create) {
1603 CDEBUG(D_INFO, "we already have lov ea\n");
1604 rc = mdd_lov_set_md(env, mdd_pobj, son,
1605 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1606 spec->u.sp_ea.eadatalen, handle, 0);
1609 /* No need mdd_lsm_sanity_check here */
1610 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1611 lmm_size, handle, 0);
1614 rc = mdd_attr_get_internal_locked(env, son, ma);
1616 /* update lov_objid data, must be before transaction stop! */
1618 mdd_lov_objid_update(mdd, lmm);
1621 mdd_trans_stop(env, mdd, rc, handle);
1623 /* Finish mdd_lov_create() stuff. */
1624 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1628 /* Get fid from name and parent */
1630 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1631 const struct lu_name *lname, struct lu_fid* fid, int mask)
1633 const char *name = lname->ln_name;
1634 const struct dt_key *key = (const struct dt_key *)name;
1635 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1636 struct mdd_device *m = mdo2mdd(pobj);
1637 struct dt_object *dir = mdd_object_child(mdd_obj);
1641 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1644 rc = mdd_object_exists(mdd_obj);
1645 if (unlikely(rc == 0))
1647 else if (unlikely(rc < 0)) {
1648 CERROR("Object "DFID" locates on remote server\n",
1649 PFID(mdo2fid(mdd_obj)));
1653 /* The common filename length check. */
1654 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1655 RETURN(-ENAMETOOLONG);
1657 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1662 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1663 dt_try_as_dir(env, dir))) {
1665 rc = dir->do_index_ops->dio_lookup(env, dir,
1666 (struct dt_rec *)fid, key,
1667 mdd_object_capa(env, mdd_obj));
1678 int mdd_declare_object_initialize(const struct lu_env *env,
1679 struct mdd_object *child,
1681 struct thandle *handle)
1685 rc = mdo_declare_attr_set(env, child, &ma->ma_attr, handle);
1686 if (rc == 0 && S_ISDIR(ma->ma_attr.la_mode)) {
1687 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1690 rc = mdo_declare_ref_add(env, child, handle);
1693 mdd_declare_links_add(env, child, handle);
1698 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1699 const struct lu_name *lname, struct mdd_object *child,
1700 struct md_attr *ma, struct thandle *handle,
1701 const struct md_op_spec *spec)
1707 * Update attributes for child.
1710 * (1) the valid bits should be converted between Lustre and Linux;
1711 * (2) maybe, the child attributes should be set in OSD when creation.
1714 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1718 if (S_ISDIR(ma->ma_attr.la_mode)) {
1719 /* Add "." and ".." for newly created dir */
1720 __mdd_ref_add(env, child, handle);
1721 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1722 dot, handle, BYPASS_CAPA);
1724 rc = __mdd_index_insert_only(env, child, pfid,
1728 __mdd_ref_del(env, child, handle, 1);
1731 mdd_links_add(env, child, pfid, lname, handle, 1);
1736 /* has not lock on pobj yet */
1737 static int mdd_create_sanity_check(const struct lu_env *env,
1738 struct md_object *pobj,
1739 const struct lu_name *lname,
1741 struct md_op_spec *spec)
1743 struct mdd_thread_info *info = mdd_env_info(env);
1744 struct lu_attr *la = &info->mti_la;
1745 struct lu_fid *fid = &info->mti_fid;
1746 struct mdd_object *obj = md2mdd_obj(pobj);
1747 struct mdd_device *m = mdo2mdd(pobj);
1748 int lookup = spec->sp_cr_lookup;
1753 if (mdd_is_dead_obj(obj))
1757 * In some cases this lookup is not needed - we know before if name
1758 * exists or not because MDT performs lookup for it.
1759 * name length check is done in lookup.
1763 * Check if the name already exist, though it will be checked in
1764 * _index_insert also, for avoiding rolling back if exists
1767 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1768 MAY_WRITE | MAY_EXEC);
1770 RETURN(rc ? : -EEXIST);
1773 * Check WRITE permission for the parent.
1774 * EXEC permission have been checked
1775 * when lookup before create already.
1777 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1784 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1788 if (la->la_mode & S_ISGID) {
1789 ma->ma_attr.la_gid = la->la_gid;
1790 if (S_ISDIR(ma->ma_attr.la_mode)) {
1791 ma->ma_attr.la_mode |= S_ISGID;
1792 ma->ma_attr.la_valid |= LA_MODE;
1796 switch (ma->ma_attr.la_mode & S_IFMT) {
1798 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1800 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1801 RETURN(-ENAMETOOLONG);
1820 static int mdd_declare_create(const struct lu_env *env,
1821 struct mdd_device *mdd,
1822 struct mdd_object *p,
1823 struct mdd_object *c,
1824 const struct lu_name *name,
1827 struct thandle *handle,
1828 const struct md_op_spec *spec)
1830 struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1833 rc = mdd_declare_object_create_internal(env, p, c, ma, handle, spec);
1837 /* if dir, then can inherit default ACl */
1839 buf->lb_len = lmm_size;
1840 if (S_ISDIR(ma->ma_attr.la_mode)) {
1841 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_DEFAULT,
1844 rc = mdo_declare_ref_add(env, p, handle);
1849 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_ACL_ACCESS,
1854 rc = mdd_declare_object_initialize(env, c, ma, handle);
1858 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1859 name->ln_name, handle);
1863 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1868 if (S_ISLNK(ma->ma_attr.la_mode)) {
1869 rc = dt_declare_record_write(env, mdd_object_child(c),
1870 strlen(spec->u.sp_symname), 0,
1875 rc = mdo_declare_attr_set(env, p, &ma->ma_attr, handle);
1879 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1883 rc = mdd_declare_lov_objid_update(env, mdd, handle);
1891 * Create object and insert it into namespace.
1893 static int mdd_create(const struct lu_env *env,
1894 struct md_object *pobj,
1895 const struct lu_name *lname,
1896 struct md_object *child,
1897 struct md_op_spec *spec,
1900 struct mdd_thread_info *info = mdd_env_info(env);
1901 struct lu_attr *la = &info->mti_la_for_fix;
1902 struct md_attr *ma_acl = &info->mti_ma;
1903 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1904 struct mdd_object *son = md2mdd_obj(child);
1905 struct mdd_device *mdd = mdo2mdd(pobj);
1906 struct lu_attr *attr = &ma->ma_attr;
1907 struct lov_mds_md *lmm = NULL;
1908 struct thandle *handle;
1909 struct dynlock_handle *dlh;
1910 const char *name = lname->ln_name;
1911 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1912 int got_def_acl = 0;
1913 #ifdef HAVE_QUOTA_SUPPORT
1914 struct obd_device *obd = mdd->mdd_obd_dev;
1915 struct obd_export *exp = md_quota(env)->mq_exp;
1916 struct mds_obd *mds = &obd->u.mds;
1917 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1918 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1919 int quota_opc = 0, block_count = 0;
1920 int inode_pending[MAXQUOTAS] = { 0, 0 };
1921 int block_pending[MAXQUOTAS] = { 0, 0 };
1922 int parent_pending[MAXQUOTAS] = { 0, 0 };
1927 * Two operations have to be performed:
1929 * - an allocation of a new object (->do_create()), and
1931 * - an insertion into a parent index (->dio_insert()).
1933 * Due to locking, operation order is not important, when both are
1934 * successful, *but* error handling cases are quite different:
1936 * - if insertion is done first, and following object creation fails,
1937 * insertion has to be rolled back, but this operation might fail
1938 * also leaving us with dangling index entry.
1940 * - if creation is done first, is has to be undone if insertion
1941 * fails, leaving us with leaked space, which is neither good, nor
1944 * It seems that creation-first is simplest solution, but it is
1945 * sub-optimal in the frequent
1950 * case, because second mkdir is bound to create object, only to
1951 * destroy it immediately.
1953 * To avoid this follow local file systems that do double lookup:
1955 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1957 * 1. create (mdd_object_create_internal())
1959 * 2. insert (__mdd_index_insert(), lookup again)
1962 /* Sanity checks before big job. */
1963 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1967 #ifdef HAVE_QUOTA_SUPPORT
1968 if (mds->mds_quota) {
1969 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1971 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1975 quota_opc = FSFILT_OP_CREATE;
1976 mdd_quota_wrapper(&ma->ma_attr, qcids);
1977 mdd_quota_wrapper(la_tmp, qpids);
1978 /* get file quota for child */
1979 lquota_chkquota(mds_quota_interface_ref, obd, exp,
1980 qcids, inode_pending, 1, NULL, 0, NULL,
1982 switch (ma->ma_attr.la_mode & S_IFMT) {
1991 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1992 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1996 /* get block quota for child and parent */
1998 lquota_chkquota(mds_quota_interface_ref, obd,
1999 exp, qcids, block_pending,
2001 LQUOTA_FLAGS_BLK, NULL, 0);
2003 lquota_chkquota(mds_quota_interface_ref, obd,
2004 exp, qpids, parent_pending, 1,
2005 NULL, LQUOTA_FLAGS_BLK, NULL,
2012 * No RPC inside the transaction, so OST objects should be created at
2015 if (S_ISREG(attr->la_mode)) {
2016 lmm_size = ma->ma_lmm_size;
2017 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
2020 GOTO(out_pending, rc);
2023 if (!S_ISLNK(attr->la_mode)) {
2024 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
2025 ma_acl->ma_acl = info->mti_xattr_buf;
2026 ma_acl->ma_need = MA_ACL_DEF;
2027 ma_acl->ma_valid = 0;
2029 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
2030 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
2031 mdd_read_unlock(env, mdd_pobj);
2034 else if (ma_acl->ma_valid & MA_ACL_DEF)
2038 handle = mdd_trans_create(env, mdd);
2040 GOTO(out_free, rc = PTR_ERR(handle));
2042 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, ma,
2043 lmm_size, handle, spec);
2047 rc = mdd_trans_start(env, mdd, handle);
2051 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2053 GOTO(out_trans, rc = -ENOMEM);
2055 mdd_write_lock(env, son, MOR_TGT_CHILD);
2056 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
2058 mdd_write_unlock(env, son);
2064 #ifdef CONFIG_FS_POSIX_ACL
2066 struct lu_buf *acl_buf = &info->mti_buf;
2067 acl_buf->lb_buf = ma_acl->ma_acl;
2068 acl_buf->lb_len = ma_acl->ma_acl_size;
2070 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
2072 mdd_write_unlock(env, son);
2075 ma->ma_attr.la_valid |= LA_MODE;
2080 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2081 son, ma, handle, spec);
2082 mdd_write_unlock(env, son);
2085 * Object has no links, so it will be destroyed when last
2086 * reference is released. (XXX not now.)
2092 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2093 name, S_ISDIR(attr->la_mode), handle,
2094 mdd_object_capa(env, mdd_pobj));
2101 /* No need mdd_lsm_sanity_check here */
2102 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
2104 CERROR("error on stripe info copy %d \n", rc);
2107 if (lmm && lmm_size > 0) {
2108 /* Set Lov here, do not get lmm again later */
2109 memcpy(ma->ma_lmm, lmm, lmm_size);
2110 ma->ma_lmm_size = lmm_size;
2111 ma->ma_valid |= MA_LOV;
2114 if (S_ISLNK(attr->la_mode)) {
2115 struct md_ucred *uc = md_ucred(env);
2116 struct dt_object *dt = mdd_object_child(son);
2117 const char *target_name = spec->u.sp_symname;
2118 int sym_len = strlen(target_name);
2119 const struct lu_buf *buf;
2122 buf = mdd_buf_get_const(env, target_name, sym_len);
2123 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2124 mdd_object_capa(env, son),
2126 CFS_CAP_SYS_RESOURCE_MASK);
2131 GOTO(cleanup, rc = -EFAULT);
2135 la->la_valid = LA_CTIME | LA_MTIME;
2136 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
2140 /* Return attr back. */
2141 rc = mdd_attr_get_internal_locked(env, son, ma);
2144 if (rc && created) {
2148 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2149 S_ISDIR(attr->la_mode),
2150 handle, BYPASS_CAPA);
2152 CERROR("error can not cleanup destroy %d\n",
2157 mdd_write_lock(env, son, MOR_TGT_CHILD);
2158 __mdd_ref_del(env, son, handle, 0);
2159 if (initialized && S_ISDIR(attr->la_mode))
2160 __mdd_ref_del(env, son, handle, 1);
2161 mdd_write_unlock(env, son);
2165 /* update lov_objid data, must be before transaction stop! */
2167 mdd_lov_objid_update(mdd, lmm);
2169 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2172 rc = mdd_changelog_ns_store(env, mdd,
2173 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2174 S_ISREG(attr->la_mode) ? CL_CREATE :
2175 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2176 0, son, mdd_pobj, NULL, lname, handle);
2178 mdd_trans_stop(env, mdd, rc, handle);
2180 /* finish lov_create stuff, free all temporary data */
2181 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
2183 #ifdef HAVE_QUOTA_SUPPORT
2185 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2187 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
2189 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
2191 /* Trigger dqacq on the owner of child and parent. If failed,
2192 * the next call for lquota_chkquota will process it. */
2193 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
2201 * Get locks on parents in proper order
2202 * RETURN: < 0 - error, rename_order if successful
2210 static int mdd_rename_order(const struct lu_env *env,
2211 struct mdd_device *mdd,
2212 struct mdd_object *src_pobj,
2213 struct mdd_object *tgt_pobj)
2215 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2219 if (src_pobj == tgt_pobj)
2220 RETURN(MDD_RN_SAME);
2222 /* compared the parent child relationship of src_p&tgt_p */
2223 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2225 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2228 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2241 /* has not mdd_write{read}_lock on any obj yet. */
2242 static int mdd_rename_sanity_check(const struct lu_env *env,
2243 struct mdd_object *src_pobj,
2244 struct mdd_object *tgt_pobj,
2245 struct mdd_object *sobj,
2246 struct mdd_object *tobj,
2252 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
2255 /* XXX: when get here, sobj must NOT be NULL,
2256 * the other case has been processed in cml_rename
2257 * before mdd_rename and enable MDS_PERM_BYPASS. */
2260 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
2264 /* XXX: when get here, "tobj == NULL" means tobj must
2265 * NOT exist (neither on remote MDS, such case has been
2266 * processed in cml_rename before mdd_rename and enable
2268 * So check may_create, but not check may_unlink. */
2270 rc = mdd_may_create(env, tgt_pobj, NULL,
2271 (src_pobj != tgt_pobj), 0);
2273 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
2274 (src_pobj != tgt_pobj), 1);
2276 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2277 S_ISDIR(ma->ma_attr.la_mode))
2278 rc = __mdd_may_link(env, tgt_pobj);
2283 static int mdd_declare_rename(const struct lu_env *env,
2284 struct mdd_device *mdd,
2285 struct mdd_object *mdd_spobj,
2286 struct mdd_object *mdd_tpobj,
2287 struct mdd_object *mdd_sobj,
2288 struct mdd_object *mdd_tobj,
2289 const struct lu_name *sname,
2290 const struct lu_name *tname,
2292 struct thandle *handle)
2300 /* name from source dir */
2301 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2305 /* .. from source child */
2306 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2307 /* source child can be directory,
2308 * counted by source dir's nlink */
2309 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2313 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2317 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2322 /* new target child can be directory,
2323 * counted by target dir's nlink */
2324 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2330 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2334 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2337 mdd_declare_links_add(env, mdd_sobj, handle);
2341 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2346 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2347 tname->ln_name, handle);
2351 /* name from target dir (old name), we declare it unconditionally
2352 * as mdd_rename() calls delete unconditionally as well. so just
2353 * to balance declarations vs calls to change ... */
2354 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2358 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2359 /* delete target child in target parent directory */
2360 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2364 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2365 /* target child can be directory,
2366 * delete "." reference in target child directory */
2367 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2371 /* delete ".." reference in target parent directory */
2372 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2377 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2381 mdd_declare_links_add(env, mdd_tobj, handle);
2385 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2390 rc = mdd_declare_changelog_store(env, mdd, tname, handle);
2394 rc = mdd_declare_changelog_store(env, mdd, sname, handle);
2401 /* src object can be remote that is why we use only fid and type of object */
2402 static int mdd_rename(const struct lu_env *env,
2403 struct md_object *src_pobj, struct md_object *tgt_pobj,
2404 const struct lu_fid *lf, const struct lu_name *lsname,
2405 struct md_object *tobj, const struct lu_name *ltname,
2408 const char *sname = lsname->ln_name;
2409 const char *tname = ltname->ln_name;
2410 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2411 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2412 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2413 struct mdd_device *mdd = mdo2mdd(src_pobj);
2414 struct mdd_object *mdd_sobj = NULL; /* source object */
2415 struct mdd_object *mdd_tobj = NULL;
2416 struct dynlock_handle *sdlh, *tdlh;
2417 struct thandle *handle;
2418 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2419 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2423 #ifdef HAVE_QUOTA_SUPPORT
2424 struct obd_device *obd = mdd->mdd_obd_dev;
2425 struct obd_export *exp = md_quota(env)->mq_exp;
2426 struct mds_obd *mds = &obd->u.mds;
2427 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2428 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2429 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2430 int quota_copc = 0, quota_popc = 0;
2431 int rec_pending[MAXQUOTAS] = { 0, 0 };
2435 LASSERT(ma->ma_attr.la_mode & S_IFMT);
2436 is_dir = S_ISDIR(ma->ma_attr.la_mode);
2439 mdd_tobj = md2mdd_obj(tobj);
2441 #ifdef HAVE_QUOTA_SUPPORT
2442 if (mds->mds_quota) {
2443 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2445 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2447 mdd_quota_wrapper(la_tmp, qspids);
2449 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2453 mdd_data_get(env, mdd_tpobj, &data);
2454 quota_popc = FSFILT_OP_LINK;
2455 mdd_quota_wrapper(la_tmp, qtpids);
2456 /* get block quota for target parent */
2457 lquota_chkquota(mds_quota_interface_ref,
2459 rec_pending, 1, NULL,
2467 mdd_sobj = mdd_object_find(env, mdd, lf);
2469 handle = mdd_trans_create(env, mdd);
2471 GOTO(out_pending, rc = PTR_ERR(handle));
2473 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2474 mdd_tobj, lsname, ltname, ma, handle);
2478 rc = mdd_trans_start(env, mdd, handle);
2482 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2483 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2485 GOTO(cleanup_unlocked, rc);
2487 /* Get locks in determined order */
2488 if (rc == MDD_RN_SAME) {
2489 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2490 sname, MOR_SRC_PARENT);
2491 /* check hashes to determine do we need one lock or two */
2492 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2493 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2497 } else if (rc == MDD_RN_SRCTGT) {
2498 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2499 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2501 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2502 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2504 if (sdlh == NULL || tdlh == NULL)
2505 GOTO(cleanup, rc = -ENOMEM);
2507 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2508 mdd_sobj, mdd_tobj, ma);
2512 /* Remove source name from source directory */
2513 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2514 mdd_object_capa(env, mdd_spobj));
2518 /* "mv dir1 dir2" needs "dir1/.." link update */
2519 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2520 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2521 mdd_object_capa(env, mdd_sobj));
2523 GOTO(fixup_spobj2, rc);
2525 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2526 handle, mdd_object_capa(env, mdd_sobj));
2528 GOTO(fixup_spobj, rc);
2531 /* Remove target name from target directory
2532 * Here tobj can be remote one, so we do index_delete unconditionally
2533 * and -ENOENT is allowed.
2535 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2536 mdd_object_capa(env, mdd_tpobj));
2539 /* tname might been renamed to something else */
2540 GOTO(fixup_spobj, rc);
2543 GOTO(fixup_spobj, rc);
2546 /* Insert new fid with target name into target dir */
2547 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2548 mdd_object_capa(env, mdd_tpobj));
2550 GOTO(fixup_tpobj, rc);
2552 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2553 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2555 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2557 la->la_valid = LA_CTIME;
2558 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2561 GOTO(fixup_tpobj, rc);
2564 /* Remove old target object
2565 * For tobj is remote case cmm layer has processed
2566 * and set tobj to NULL then. So when tobj is NOT NULL,
2567 * it must be local one.
2569 if (tobj && mdd_object_exists(mdd_tobj)) {
2570 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2571 if (mdd_is_dead_obj(mdd_tobj)) {
2572 mdd_write_unlock(env, mdd_tobj);
2573 /* shld not be dead, something is wrong */
2574 CERROR("tobj is dead, something is wrong\n");
2578 __mdd_ref_del(env, mdd_tobj, handle, 0);
2580 /* Remove dot reference. */
2582 __mdd_ref_del(env, mdd_tobj, handle, 1);
2584 la->la_valid = LA_CTIME;
2585 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2587 GOTO(fixup_tpobj, rc);
2589 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2590 mdd_write_unlock(env, mdd_tobj);
2592 GOTO(fixup_tpobj, rc);
2594 #ifdef HAVE_QUOTA_SUPPORT
2595 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2596 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2597 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2598 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2603 la->la_valid = LA_CTIME | LA_MTIME;
2604 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2606 GOTO(fixup_tpobj, rc);
2608 if (mdd_spobj != mdd_tpobj) {
2609 la->la_valid = LA_CTIME | LA_MTIME;
2610 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2614 if (rc == 0 && mdd_sobj) {
2615 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2616 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2617 mdo2fid(mdd_tpobj), ltname, handle);
2619 /* Old files might not have EA entry */
2620 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2622 mdd_write_unlock(env, mdd_sobj);
2623 /* We don't fail the transaction if the link ea can't be
2624 updated -- fid2path will use alternate lookup method. */
2632 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2635 CWARN("tp obj fix error %d\n",rc2);
2637 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2638 !mdd_is_dead_obj(mdd_tobj)) {
2639 rc2 = __mdd_index_insert(env, mdd_tpobj,
2640 mdo2fid(mdd_tobj), tname,
2645 CWARN("tp obj fix error %d\n",rc2);
2650 if (rc && is_dir && mdd_sobj) {
2651 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2655 CWARN("sp obj dotdot delete error %d\n",rc2);
2658 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2659 dotdot, handle, BYPASS_CAPA);
2661 CWARN("sp obj dotdot insert error %d\n",rc2);
2666 rc2 = __mdd_index_insert(env, mdd_spobj,
2667 lf, sname, is_dir, handle, BYPASS_CAPA);
2669 CWARN("sp obj fix error %d\n",rc2);
2672 if (likely(tdlh) && sdlh != tdlh)
2673 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2675 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2678 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2679 mdd_spobj, lf, lsname, handle);
2681 struct lu_fid zero_fid;
2682 fid_zero(&zero_fid);
2683 /* If the rename target exist, The CL_EXT record should save
2684 * the target fid as tfid, otherwise, use zero fid. LU-543 */
2685 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2687 mdd_tobj ? NULL : &zero_fid,
2692 mdd_trans_stop(env, mdd, rc, handle);
2694 mdd_object_put(env, mdd_sobj);
2696 #ifdef HAVE_QUOTA_SUPPORT
2697 if (mds->mds_quota) {
2699 lquota_pending_commit(mds_quota_interface_ref, obd,
2700 qtpids, rec_pending, 1);
2703 /* Trigger dqrel on the source owner of parent.
2704 * If failed, the next call for lquota_chkquota will
2706 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2707 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2709 /* Trigger dqrel on the target owner of child.
2710 * If failed, the next call for lquota_chkquota
2711 * will process it. */
2712 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2713 qtpids, rc, quota_copc);
2720 /** enable/disable storing of hardlink info */
2721 int mdd_linkea_enable = 1;
2722 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2723 "record hardlink info in EAs");
2725 /** Read the link EA into a temp buffer.
2726 * Uses the name_buf since it is generally large.
2727 * \retval IS_ERR err
2728 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2730 struct lu_buf *mdd_links_get(const struct lu_env *env,
2731 struct mdd_object *mdd_obj)
2734 struct lustre_capa *capa;
2735 struct link_ea_header *leh;
2738 /* First try a small buf */
2739 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2740 if (buf->lb_buf == NULL)
2741 return ERR_PTR(-ENOMEM);
2743 capa = mdd_object_capa(env, mdd_obj);
2744 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2745 if (rc == -ERANGE) {
2746 /* Buf was too small, figure out what we need. */
2748 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2751 buf = mdd_buf_alloc(env, rc);
2752 if (buf->lb_buf == NULL)
2753 return ERR_PTR(-ENOMEM);
2754 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2760 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2761 leh->leh_magic = LINK_EA_MAGIC;
2762 leh->leh_reccount = __swab32(leh->leh_reccount);
2763 leh->leh_len = __swab64(leh->leh_len);
2764 /* entries are swabbed by mdd_lee_unpack */
2766 if (leh->leh_magic != LINK_EA_MAGIC)
2767 return ERR_PTR(-EINVAL);
2768 if (leh->leh_reccount == 0)
2769 return ERR_PTR(-ENODATA);
2774 /** Pack a link_ea_entry.
2775 * All elements are stored as chars to avoid alignment issues.
2776 * Numbers are always big-endian
2777 * \retval record length
2779 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2780 const struct lu_fid *pfid)
2782 struct lu_fid tmpfid;
2785 fid_cpu_to_be(&tmpfid, pfid);
2786 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2787 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2788 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2790 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2791 lee->lee_reclen[1] = reclen & 0xff;
2795 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2796 struct lu_name *lname, struct lu_fid *pfid)
2798 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2799 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2800 fid_be_to_cpu(pfid, pfid);
2801 lname->ln_name = lee->lee_name;
2802 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2805 /** Add a record to the end of link ea buf */
2806 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2807 const struct lu_fid *pfid,
2808 const struct lu_name *lname)
2810 struct link_ea_header *leh;
2811 struct link_ea_entry *lee;
2814 if (lname == NULL || pfid == NULL)
2817 /* Make sure our buf is big enough for the new one */
2819 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2820 if (leh->leh_len + reclen > buf->lb_len) {
2821 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2826 lee = buf->lb_buf + leh->leh_len;
2827 reclen = mdd_lee_pack(lee, lname, pfid);
2828 leh->leh_len += reclen;
2829 leh->leh_reccount++;
2833 static int mdd_declare_links_add(const struct lu_env *env,
2834 struct mdd_object *mdd_obj,
2835 struct thandle *handle)
2839 /* XXX: max size? */
2840 rc = mdo_declare_xattr_set(env, mdd_obj,
2841 mdd_buf_get_const(env, NULL, 4096),
2842 XATTR_NAME_LINK, 0, handle);
2847 /* For pathologic linkers, we don't want to spend lots of time scanning the
2848 * link ea. Limit ourseleves to something reasonable; links not in the EA
2849 * can be looked up via (slower) parent lookup.
2851 #define LINKEA_MAX_COUNT 128
2853 static int mdd_links_add(const struct lu_env *env,
2854 struct mdd_object *mdd_obj,
2855 const struct lu_fid *pfid,
2856 const struct lu_name *lname,
2857 struct thandle *handle, int first)
2860 struct link_ea_header *leh;
2864 if (!mdd_linkea_enable)
2867 buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2870 if (rc != -ENODATA) {
2871 CERROR("link_ea read failed %d "DFID"\n", rc,
2872 PFID(mdd_object_fid(mdd_obj)));
2875 /* empty EA; start one */
2876 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2877 if (buf->lb_buf == NULL)
2880 leh->leh_magic = LINK_EA_MAGIC;
2881 leh->leh_len = sizeof(struct link_ea_header);
2882 leh->leh_reccount = 0;
2886 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2889 rc = __mdd_links_add(env, buf, pfid, lname);
2894 rc = __mdd_xattr_set(env, mdd_obj,
2895 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2896 XATTR_NAME_LINK, 0, handle);
2899 CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2900 PFID(mdd_object_fid(mdd_obj)));
2902 CERROR("link_ea add failed %d "DFID"\n", rc,
2903 PFID(mdd_object_fid(mdd_obj)));
2906 if (buf->lb_len > OBD_ALLOC_BIG)
2907 /* if we vmalloced a large buffer drop it */
2913 static int mdd_links_rename(const struct lu_env *env,
2914 struct mdd_object *mdd_obj,
2915 const struct lu_fid *oldpfid,
2916 const struct lu_name *oldlname,
2917 const struct lu_fid *newpfid,
2918 const struct lu_name *newlname,
2919 struct thandle *handle)
2922 struct link_ea_header *leh;
2923 struct link_ea_entry *lee;
2924 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2925 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2931 if (!mdd_linkea_enable)
2934 if (mdd_obj->mod_flags & DEAD_OBJ)
2935 /* No more links, don't bother */
2938 buf = mdd_links_get(env, mdd_obj);
2942 CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2943 rc, PFID(mdd_object_fid(mdd_obj)));
2945 CERROR("link_ea read failed %d "DFID"\n",
2946 rc, PFID(mdd_object_fid(mdd_obj)));
2950 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2952 /* Find the old record */
2953 for(count = 0; count < leh->leh_reccount; count++) {
2954 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2955 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2956 lu_fid_eq(tmpfid, oldpfid) &&
2957 (strncmp(tmpname->ln_name, oldlname->ln_name,
2958 tmpname->ln_namelen) == 0))
2960 lee = (struct link_ea_entry *)((char *)lee + reclen);
2962 if ((count + 1) > leh->leh_reccount) {
2963 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2964 oldlname->ln_namelen, oldlname->ln_name);
2965 GOTO(out, rc = -ENOENT);
2968 /* Remove the old record */
2969 leh->leh_reccount--;
2970 leh->leh_len -= reclen;
2971 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2974 /* If renaming, add the new record */
2975 if (newpfid != NULL) {
2976 /* if the add fails, we still delete the out-of-date old link */
2977 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2981 rc = __mdd_xattr_set(env, mdd_obj,
2982 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2983 XATTR_NAME_LINK, 0, handle);
2989 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2990 oldlname->ln_namelen, oldlname->ln_name, rc,
2991 PFID(mdd_object_fid(mdd_obj)));
2993 if (buf->lb_len > OBD_ALLOC_BIG)
2994 /* if we vmalloced a large buffer drop it */
3000 const struct md_dir_operations mdd_dir_ops = {
3001 .mdo_is_subdir = mdd_is_subdir,
3002 .mdo_lookup = mdd_lookup,
3003 .mdo_create = mdd_create,
3004 .mdo_rename = mdd_rename,
3005 .mdo_link = mdd_link,
3006 .mdo_unlink = mdd_unlink,
3007 .mdo_name_insert = mdd_name_insert,
3008 .mdo_name_remove = mdd_name_remove,
3009 .mdo_rename_tgt = mdd_rename_tgt,
3010 .mdo_create_data = mdd_create_data,