4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_internal.h
38 * Author: Wang Di <wangdi@clusterfs.com>
41 #ifndef _MDD_INTERNAL_H
42 #define _MDD_INTERNAL_H
44 #include <lustre_acl.h>
45 #include <lustre_eacl.h>
46 #include <md_object.h>
47 #include <dt_object.h>
48 #include <lustre_lfsck.h>
49 #include <lustre_fid.h>
50 #include <lprocfs_status.h>
51 #include <lustre_log.h>
52 #include <lustre_linkea.h>
55 /** changelog is recording */
56 #define CLM_ON 0x00001
57 /** internal error prevented changelogs from starting */
58 #define CLM_ERR 0x00002
60 /** changelogs turned on */
61 #define CLM_START 0x10000
62 /** changelogs turned off */
63 #define CLM_FINI 0x20000
64 /** some changelog records purged */
65 #define CLM_PURGE 0x40000
67 struct mdd_changelog {
68 spinlock_t mc_lock; /* for index */
73 spinlock_t mc_user_lock;
77 static inline __u64 cl_time(void) {
80 cfs_fs_time_current(&time);
81 return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
84 /** Objects in .lustre dir */
85 struct mdd_dot_lustre_objs {
86 struct mdd_object *mdd_obf;
87 struct mdd_object *mdd_lpf;
90 struct mdd_generic_thread {
91 struct completion mgt_started;
92 struct completion mgt_finished;
99 struct md_device mdd_md_dev;
100 struct obd_export *mdd_child_exp;
101 struct dt_device *mdd_child;
102 struct dt_device *mdd_bottom;
103 struct lu_fid mdd_root_fid; /* /ROOT */
104 struct lu_fid mdd_local_root_fid;
105 struct dt_device_param mdd_dt_conf;
106 struct dt_object *mdd_orphans; /* PENDING directory */
107 struct proc_dir_entry *mdd_proc_entry;
108 struct mdd_changelog mdd_cl;
109 unsigned long mdd_atime_diff;
110 struct mdd_object *mdd_dot_lustre;
111 struct mdd_dot_lustre_objs mdd_dot_lustre_objs;
112 unsigned int mdd_sync_permission;
114 struct local_oid_storage *mdd_los;
115 struct mdd_generic_thread mdd_orph_cleanup_thread;
119 /* The dir object has been unlinked */
125 struct md_object mod_obj;
130 unsigned long mod_flags;
133 struct mdd_thread_info {
134 struct lu_fid mti_fid;
135 struct lu_fid mti_fid2; /* used for be & cpu converting */
137 * only be used by MDD interfaces, can be passed into local MDD APIs.
139 struct lu_attr mti_pattr;
140 struct lu_attr mti_cattr;
141 struct lu_attr mti_tpattr;
142 struct lu_attr mti_tattr;
143 /** used to set c/mtime */
144 struct lu_attr mti_la_for_fix;
145 /* Only used in mdd_object_start */
146 struct lu_attr mti_la_for_start;
147 /* mti_ent and mti_key must be conjoint,
148 * then mti_ent::lde_name will be mti_key. */
149 struct lu_dirent mti_ent;
150 char mti_key[NAME_MAX + 16];
151 struct lu_buf mti_buf[4];
152 struct lu_buf mti_big_buf; /* biggish persistent buf */
153 struct lu_buf mti_link_buf; /* buf for link ea */
155 char mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
156 struct dt_allocation_hint mti_hint;
157 struct dt_object_format mti_dof;
158 struct linkea_data mti_link_data;
159 struct md_op_spec mti_spec;
160 struct dt_insert_rec mti_dt_rec;
161 struct lfsck_request mti_lr;
164 enum mdd_links_add_overflow {
169 extern const char orph_index_name[];
171 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
173 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
175 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
176 const struct md_attr *ma);
177 int mdd_attr_set_internal(const struct lu_env *env,
178 struct mdd_object *obj,
179 const struct lu_attr *attr,
180 struct thandle *handle,
182 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
183 const struct lu_attr *oattr, struct lu_attr *attr,
184 struct thandle *handle);
185 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
186 struct mdd_object *c, struct lu_attr *attr,
187 struct thandle *handle,
188 const struct md_op_spec *spec,
189 struct dt_allocation_hint *hint);
192 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
193 enum mdd_object_role role);
194 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
195 enum mdd_object_role role);
196 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
197 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
198 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
201 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
202 const struct lu_fid *fid, struct lu_fid *sfid);
203 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
204 const struct lu_attr *pattr, struct mdd_object *cobj,
206 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
207 const struct lu_attr *pattr, const struct lu_attr *attr);
208 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
209 const struct lu_attr *tpattr, struct mdd_object *tobj,
210 const struct lu_attr *tattr, const struct lu_attr *cattr,
211 int check_perm, int check_empty);
212 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
213 const struct lu_attr *pattr,
214 struct mdd_object *cobj,
215 const struct lu_attr *cattr);
216 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
217 struct md_attr *ma, const struct mdd_object *pobj,
218 const struct lu_name *lname, struct thandle *th);
220 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
221 int mdd_lookup(const struct lu_env *env,
222 struct md_object *pobj, const struct lu_name *lname,
223 struct lu_fid* fid, struct md_op_spec *spec);
224 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
225 struct linkea_data *ldata);
226 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
227 struct thandle *handle, struct linkea_data *ldata,
228 enum mdd_links_add_overflow overflow);
229 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
230 struct linkea_data *ldata, struct thandle *handle);
231 struct lu_buf *mdd_links_get(const struct lu_env *env,
232 struct mdd_object *mdd_obj);
233 int mdd_links_rename(const struct lu_env *env,
234 struct mdd_object *mdd_obj,
235 const struct lu_fid *oldpfid,
236 const struct lu_name *oldlname,
237 const struct lu_fid *newpfid,
238 const struct lu_name *newlname,
239 struct thandle *handle,
240 struct linkea_data *ldata,
241 int first, int check);
243 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
245 #define MDD_ENV_VAR(env, var) (&mdd_env_info(env)->mti_##var)
247 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
248 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
249 const void *area, ssize_t len);
251 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
252 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
254 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
256 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
257 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
258 int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
259 umode_t mode, struct thandle *);
260 int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
264 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
265 void mdd_procfs_fini(struct mdd_device *mdd);
268 extern struct kmem_cache *mdd_object_kmem;
269 extern const struct md_dir_operations mdd_dir_ops;
270 extern const struct md_object_operations mdd_obj_ops;
271 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
273 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags);
274 extern struct lu_context_key mdd_thread_key;
275 extern const struct lu_device_operations mdd_lu_ops;
277 struct mdd_object *mdd_object_find(const struct lu_env *env,
278 struct mdd_device *d,
279 const struct lu_fid *f);
280 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
281 const struct lu_rdpg *rdpg);
282 int mdd_declare_changelog_store(const struct lu_env *env,
283 struct mdd_device *mdd,
284 const struct lu_name *tname,
285 const struct lu_name *sname,
286 struct thandle *handle);
287 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid);
288 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
289 struct llog_changelog_rec *rec, struct thandle *th);
290 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
291 enum changelog_rec_type type, int flags,
292 struct mdd_object *mdd_obj,
293 struct thandle *handle);
294 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
295 enum changelog_rec_type type,
296 enum changelog_rec_flags crf,
297 struct mdd_object *target,
298 const struct lu_fid *tpfid,
299 const struct lu_fid *sfid,
300 const struct lu_fid *spfid,
301 const struct lu_name *tname,
302 const struct lu_name *sname,
303 struct thandle *handle);
304 int mdd_declare_object_create_internal(const struct lu_env *env,
305 struct mdd_object *p,
306 struct mdd_object *c,
307 struct lu_attr *attr,
308 struct thandle *handle,
309 const struct md_op_spec *spec,
310 struct dt_allocation_hint *hint);
311 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
312 struct lu_buf *lmm_buf);
315 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
316 struct mdd_object *child, const struct lu_attr *attr,
317 const struct md_op_spec *spec,
318 struct dt_allocation_hint *hint);
320 static inline void mdd_object_get(struct mdd_object *o)
322 lu_object_get(&o->mod_obj.mo_lu);
325 static inline void mdd_object_put(const struct lu_env *env,
326 struct mdd_object *o)
328 lu_object_put(env, &o->mod_obj.mo_lu);
331 struct thandle *mdd_trans_create(const struct lu_env *env,
332 struct mdd_device *mdd);
333 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
335 int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
336 int rc, struct thandle *handle);
339 struct lu_object *mdd_object_alloc(const struct lu_env *env,
340 const struct lu_object_header *hdr,
341 struct lu_device *d);
342 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
343 const struct lu_fid *pfid, const char *name,
344 __u32 mode, struct lu_fid *fid);
346 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
347 struct thandle *handle);
348 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
349 struct lu_attr *attr, const struct lu_buf *buf, int fl);
350 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
352 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
353 const struct lu_attr *la, int mask, int role);
354 int mdd_permission(const struct lu_env *env,
355 struct md_object *pobj, struct md_object *cobj,
356 struct md_attr *ma, int mask);
357 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
358 int (*func)(void *), void *data, char *name);
359 void mdd_generic_thread_stop(struct mdd_generic_thread *thread);
362 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
364 /* inline functions */
365 static inline int lu_device_is_mdd(struct lu_device *d)
367 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
370 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
372 LASSERT(lu_device_is_mdd(d));
373 return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
376 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
378 return (&d->mdd_md_dev.md_lu_dev);
381 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
383 LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
384 return container_of0(o, struct mdd_object, mod_obj.mo_lu);
387 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
389 return lu2mdd_dev(mdo->mo_lu.lo_dev);
392 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
394 return container_of0(mdo, struct mdd_object, mod_obj);
397 static inline const struct dt_device_operations *
398 mdd_child_ops(struct mdd_device *d)
400 return d->mdd_child->dd_ops;
403 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
405 return &obj->mod_obj.mo_lu;
408 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
410 return container_of0(lu_object_next(mdd2lu_obj(o)),
411 struct dt_object, do_lu);
414 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
416 return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
419 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
421 return mdo2mdd(&obj->mod_obj);
424 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
426 return lu_object_fid(&obj->mod_obj.mo_lu);
429 static inline umode_t mdd_object_type(const struct mdd_object *obj)
431 return lu_object_attr(&obj->mod_obj.mo_lu);
434 static inline int mdd_is_dead_obj(struct mdd_object *obj)
436 return obj && obj->mod_flags & DEAD_OBJ;
439 static inline int mdd_object_exists(struct mdd_object *obj)
441 return lu_object_exists(mdd2lu_obj(obj));
444 static inline int mdd_object_remote(struct mdd_object *obj)
446 return lu_object_remote(mdd2lu_obj(obj));
449 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
451 return lu_object_fid(mdd2lu_obj(obj));
454 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
456 return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
459 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
461 return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
464 #define MAX_ATIME_DIFF 60
466 static inline int mdd_permission_internal(const struct lu_env *env,
467 struct mdd_object *obj,
468 const struct lu_attr *la, int mask)
470 return __mdd_permission_internal(env, obj, la, mask, -1);
473 static inline int mdd_permission_internal_locked(const struct lu_env *env,
474 struct mdd_object *obj,
475 const struct lu_attr *la,
477 enum mdd_object_role role)
479 return __mdd_permission_internal(env, obj, la, mask, role);
482 /* mdd inline func for calling osd_dt_object ops */
483 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
486 struct dt_object *next = mdd_object_child(obj);
487 return dt_attr_get(env, next, la);
490 static inline int mdo_declare_attr_set(const struct lu_env *env,
491 struct mdd_object *obj,
492 const struct lu_attr *la,
493 struct thandle *handle)
495 struct dt_object *next = mdd_object_child(obj);
496 return dt_declare_attr_set(env, next, la, handle);
499 static inline int mdo_attr_set(const struct lu_env *env,
500 struct mdd_object *obj,
501 const struct lu_attr *la,
502 struct thandle *handle)
504 struct dt_object *next = mdd_object_child(obj);
506 if (!mdd_object_exists(obj))
509 return dt_attr_set(env, next, la, handle);
512 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
513 struct lu_buf *buf, const char *name)
515 struct dt_object *next = mdd_object_child(obj);
516 return dt_xattr_get(env, next, buf, name);
519 static inline int mdo_declare_xattr_set(const struct lu_env *env,
520 struct mdd_object *obj,
521 const struct lu_buf *buf,
523 int fl, struct thandle *handle)
525 struct dt_object *next = mdd_object_child(obj);
526 return dt_declare_xattr_set(env, next, buf, name, fl, handle);
529 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
530 const struct lu_buf *buf, const char *name,
531 int fl, struct thandle *handle)
533 struct dt_object *next = mdd_object_child(obj);
535 if (!mdd_object_exists(obj))
538 return dt_xattr_set(env, next, buf, name, fl, handle);
541 static inline int mdo_declare_xattr_del(const struct lu_env *env,
542 struct mdd_object *obj,
544 struct thandle *handle)
546 struct dt_object *next = mdd_object_child(obj);
547 return dt_declare_xattr_del(env, next, name, handle);
550 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
551 const char *name, struct thandle *handle)
553 struct dt_object *next = mdd_object_child(obj);
555 if (!mdd_object_exists(obj))
558 return dt_xattr_del(env, next, name, handle);
562 mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
565 struct dt_object *next = mdd_object_child(obj);
567 if (!mdd_object_exists(obj))
570 return dt_xattr_list(env, next, buf);
574 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
575 const struct lu_fid *fid, __u32 type,
576 const char *name, struct thandle *handle)
578 struct dt_object *next = mdd_object_child(obj);
582 * if the object doesn't exist yet, then it's supposed to be created
583 * and declaration of the creation should be enough to insert ./..
586 /* FIXME: remote object should not be awared by MDD layer, but local
587 * creation does not declare insert ./.. (comments above), which
588 * is required by remote directory creation.
589 * This remote check should be removed when mdd_object_exists check is
592 if (mdd_object_exists(obj) || mdd_object_remote(obj)) {
594 if (dt_try_as_dir(env, next)) {
595 struct dt_insert_rec *rec =
596 &mdd_env_info(env)->mti_dt_rec;
599 rec->rec_type = type;
600 rc = dt_declare_insert(env, next,
601 (const struct dt_rec *)rec,
602 (const struct dt_key *)name,
611 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
612 const char *name, struct thandle *handle)
614 struct dt_object *next = mdd_object_child(obj);
616 if (!dt_try_as_dir(env, next))
619 return dt_declare_delete(env, next, (const struct dt_key *)name,
623 static inline int mdo_declare_ref_add(const struct lu_env *env,
624 struct mdd_object *obj,
625 struct thandle *handle)
627 struct dt_object *next = mdd_object_child(obj);
628 return dt_declare_ref_add(env, next, handle);
631 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
632 struct thandle *handle)
634 struct dt_object *next = mdd_object_child(obj);
636 if (!mdd_object_exists(obj))
639 return dt_ref_add(env, next, handle);
642 static inline int mdo_declare_ref_del(const struct lu_env *env,
643 struct mdd_object *obj,
644 struct thandle *handle)
646 struct dt_object *next = mdd_object_child(obj);
647 return dt_declare_ref_del(env, next, handle);
650 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
651 struct thandle *handle)
653 struct dt_object *next = mdd_object_child(obj);
655 if (!mdd_object_exists(obj))
658 return dt_ref_del(env, next, handle);
662 mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
663 struct lu_attr *attr,
664 struct dt_allocation_hint *hint,
665 struct dt_object_format *dof,
666 struct thandle *handle)
668 struct dt_object *next = mdd_object_child(o);
669 return dt_declare_create(env, next, attr, hint, dof, handle);
673 mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
674 struct lu_attr *attr,
675 struct dt_allocation_hint *hint,
676 struct dt_object_format *dof,
677 struct thandle *handle)
679 struct dt_object *next = mdd_object_child(o);
680 return dt_create(env, next, attr, hint, dof, handle);
684 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
685 struct thandle *handle)
687 struct dt_object *next = mdd_object_child(o);
688 return dt_declare_destroy(env, next, handle);
692 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
693 struct thandle *handle)
695 struct dt_object *next = mdd_object_child(o);
696 return dt_destroy(env, next, handle);