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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_internal.h
34 * Author: Wang Di <wangdi@clusterfs.com>
37 #ifndef _MDD_INTERNAL_H
38 #define _MDD_INTERNAL_H
40 #include <lustre_acl.h>
41 #include <lustre_compat.h>
42 #include <lustre_eacl.h>
43 #include <md_object.h>
44 #include <dt_object.h>
45 #include <lustre_lfsck.h>
46 #include <lustre_fid.h>
47 #include <lprocfs_status.h>
48 #include <lustre_log.h>
49 #include <lustre_linkea.h>
52 /** changelog is recording */
53 #define CLM_ON 0x00001
54 /** internal error prevented changelogs from starting */
55 #define CLM_ERR 0x00002
57 /** changelogs turned on */
58 #define CLM_START 0x10000
59 /** changelogs turned off */
60 #define CLM_FINI 0x20000
61 /** some changelog records purged */
62 #define CLM_PURGE 0x40000
64 struct mdd_changelog {
65 spinlock_t mc_lock; /* for index */
70 spinlock_t mc_user_lock;
74 static inline __u64 cl_time(void)
76 struct timespec64 time;
78 ktime_get_real_ts64(&time);
79 return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
82 /** Objects in .lustre dir */
83 struct mdd_dot_lustre_objs {
84 struct mdd_object *mdd_obf;
85 struct mdd_object *mdd_lpf;
88 struct mdd_generic_thread {
89 struct completion mgt_started;
90 struct completion mgt_finished;
97 struct md_device mdd_md_dev;
98 struct obd_export *mdd_child_exp;
99 struct dt_device *mdd_child;
100 struct dt_device *mdd_bottom;
101 struct lu_fid mdd_root_fid; /* /ROOT */
102 struct lu_fid mdd_local_root_fid;
103 struct dt_device_param mdd_dt_conf;
104 struct dt_object *mdd_orphans; /* PENDING directory */
105 struct proc_dir_entry *mdd_proc_entry;
106 struct mdd_changelog mdd_cl;
107 unsigned long mdd_atime_diff;
108 struct mdd_object *mdd_dot_lustre;
109 struct mdd_dot_lustre_objs mdd_dot_lustre_objs;
110 unsigned int mdd_sync_permission;
112 struct local_oid_storage *mdd_los;
113 struct mdd_generic_thread mdd_orph_cleanup_thread;
117 /* The dir object has been unlinked */
123 struct md_object mod_obj;
128 unsigned long mod_flags;
131 struct mdd_thread_info {
132 struct lu_fid mti_fid;
133 struct lu_fid mti_fid2; /* used for be & cpu converting */
135 * only be used by MDD interfaces, can be passed into local MDD APIs.
137 struct lu_attr mti_pattr;
138 struct lu_attr mti_cattr;
139 struct lu_attr mti_tpattr;
140 struct lu_attr mti_tattr;
141 /** used to set c/mtime */
142 struct lu_attr mti_la_for_fix;
143 /* Only used in mdd_object_start */
144 struct lu_attr mti_la_for_start;
145 /* mti_ent and mti_key must be conjoint,
146 * then mti_ent::lde_name will be mti_key. */
147 struct lu_dirent mti_ent;
148 char mti_key[NAME_MAX + 16];
149 struct lu_buf mti_buf[4];
150 struct lu_buf mti_big_buf; /* biggish persistent buf */
151 struct lu_buf mti_link_buf; /* buf for link ea */
153 char mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
154 struct dt_allocation_hint mti_hint;
155 struct dt_object_format mti_dof;
156 struct linkea_data mti_link_data;
157 struct md_op_spec mti_spec;
158 struct dt_insert_rec mti_dt_rec;
159 struct lfsck_request mti_lr;
160 struct lu_seq_range mti_range;
163 extern const char orph_index_name[];
165 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
167 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
169 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
170 const struct md_attr *ma);
171 int mdd_attr_set_internal(const struct lu_env *env,
172 struct mdd_object *obj,
173 const struct lu_attr *attr,
174 struct thandle *handle,
176 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
177 const struct lu_attr *oattr, struct lu_attr *attr,
178 struct thandle *handle);
179 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
180 struct mdd_object *c, struct lu_attr *attr,
181 struct thandle *handle,
182 const struct md_op_spec *spec,
183 struct dt_allocation_hint *hint);
186 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
187 enum mdd_object_role role);
188 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
189 enum mdd_object_role role);
190 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
191 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
192 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
195 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
196 const struct lu_fid *fid, struct lu_fid *sfid);
197 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
198 const struct lu_attr *pattr, struct mdd_object *cobj,
200 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
201 const struct lu_attr *pattr, const struct lu_attr *attr);
202 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
203 const struct lu_attr *tpattr, struct mdd_object *tobj,
204 const struct lu_attr *tattr, const struct lu_attr *cattr,
205 int check_perm, int check_empty);
206 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
207 const struct lu_attr *pattr,
208 struct mdd_object *cobj,
209 const struct lu_attr *cattr);
210 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
211 struct md_attr *ma, const struct mdd_object *pobj,
212 const struct lu_name *lname, struct thandle *th);
214 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
215 int mdd_lookup(const struct lu_env *env,
216 struct md_object *pobj, const struct lu_name *lname,
217 struct lu_fid* fid, struct md_op_spec *spec);
218 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
219 struct linkea_data *ldata, struct thandle *handle);
220 struct lu_buf *mdd_links_get(const struct lu_env *env,
221 struct mdd_object *mdd_obj);
222 int mdd_links_rename(const struct lu_env *env,
223 struct mdd_object *mdd_obj,
224 const struct lu_fid *oldpfid,
225 const struct lu_name *oldlname,
226 const struct lu_fid *newpfid,
227 const struct lu_name *newlname,
228 struct thandle *handle,
229 struct linkea_data *ldata,
230 int first, int check);
232 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
234 #define MDD_ENV_VAR(env, var) (&mdd_env_info(env)->mti_##var)
236 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
237 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
238 const void *area, ssize_t len);
240 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
241 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
243 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
245 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
246 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
247 int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
248 umode_t mode, struct thandle *);
249 int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
253 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
254 void mdd_procfs_fini(struct mdd_device *mdd);
257 extern struct kmem_cache *mdd_object_kmem;
258 extern const struct md_dir_operations mdd_dir_ops;
259 extern const struct md_object_operations mdd_obj_ops;
260 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
262 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags);
263 extern struct lu_context_key mdd_thread_key;
264 extern const struct lu_device_operations mdd_lu_ops;
266 struct mdd_object *mdd_object_find(const struct lu_env *env,
267 struct mdd_device *d,
268 const struct lu_fid *f);
269 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
270 const struct lu_rdpg *rdpg);
271 int mdd_declare_changelog_store(const struct lu_env *env,
272 struct mdd_device *mdd,
273 const struct lu_name *tname,
274 const struct lu_name *sname,
275 struct thandle *handle);
276 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid);
277 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
278 struct llog_changelog_rec *rec, struct thandle *th);
279 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
280 enum changelog_rec_type type, int flags,
281 struct mdd_object *mdd_obj,
282 struct thandle *handle);
283 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
284 enum changelog_rec_type type,
285 enum changelog_rec_flags crf,
286 struct mdd_object *target,
287 const struct lu_fid *tpfid,
288 const struct lu_fid *sfid,
289 const struct lu_fid *spfid,
290 const struct lu_name *tname,
291 const struct lu_name *sname,
292 struct thandle *handle);
293 int mdd_invalidate(const struct lu_env *env, struct md_object *obj);
294 int mdd_declare_object_create_internal(const struct lu_env *env,
295 struct mdd_object *p,
296 struct mdd_object *c,
297 struct lu_attr *attr,
298 struct thandle *handle,
299 const struct md_op_spec *spec,
300 struct dt_allocation_hint *hint);
301 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
302 struct lu_buf *lmm_buf);
305 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
306 struct mdd_object *child, const struct lu_attr *attr,
307 const struct md_op_spec *spec,
308 struct dt_allocation_hint *hint);
310 static inline void mdd_object_get(struct mdd_object *o)
312 lu_object_get(&o->mod_obj.mo_lu);
315 static inline void mdd_object_put(const struct lu_env *env,
316 struct mdd_object *o)
318 lu_object_put(env, &o->mod_obj.mo_lu);
321 struct thandle *mdd_trans_create(const struct lu_env *env,
322 struct mdd_device *mdd);
323 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
325 int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
326 int rc, struct thandle *handle);
329 struct lu_object *mdd_object_alloc(const struct lu_env *env,
330 const struct lu_object_header *hdr,
331 struct lu_device *d);
332 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
333 const struct lu_fid *pfid, const char *name,
334 __u32 mode, struct lu_fid *fid);
336 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
337 struct thandle *handle);
338 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
339 struct lu_attr *attr, const struct lu_buf *buf, int fl);
340 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
342 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
343 const struct lu_attr *la, int mask, int role);
344 int mdd_permission(const struct lu_env *env,
345 struct md_object *pobj, struct md_object *cobj,
346 struct md_attr *ma, int mask);
347 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
348 int (*func)(void *), void *data, char *name);
349 void mdd_generic_thread_stop(struct mdd_generic_thread *thread);
352 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
354 /* inline functions */
355 static inline int lu_device_is_mdd(struct lu_device *d)
357 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
360 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
362 LASSERT(lu_device_is_mdd(d));
363 return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
366 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
368 return (&d->mdd_md_dev.md_lu_dev);
371 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
373 LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
374 return container_of0(o, struct mdd_object, mod_obj.mo_lu);
377 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
379 return lu2mdd_dev(mdo->mo_lu.lo_dev);
382 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
384 return container_of0(mdo, struct mdd_object, mod_obj);
387 static inline const struct dt_device_operations *
388 mdd_child_ops(struct mdd_device *d)
390 return d->mdd_child->dd_ops;
393 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
395 return &obj->mod_obj.mo_lu;
398 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
400 return container_of0(lu_object_next(mdd2lu_obj(o)),
401 struct dt_object, do_lu);
404 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
406 return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
409 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
411 return mdo2mdd(&obj->mod_obj);
414 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
416 return lu_object_fid(&obj->mod_obj.mo_lu);
419 static inline umode_t mdd_object_type(const struct mdd_object *obj)
421 return lu_object_attr(&obj->mod_obj.mo_lu);
424 static inline int mdd_is_dead_obj(struct mdd_object *obj)
426 return obj && obj->mod_flags & DEAD_OBJ;
429 static inline int mdd_object_exists(struct mdd_object *obj)
431 return lu_object_exists(mdd2lu_obj(obj));
434 static inline int mdd_object_remote(struct mdd_object *obj)
436 return lu_object_remote(mdd2lu_obj(obj));
439 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
441 return lu_object_fid(mdd2lu_obj(obj));
444 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
446 return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
449 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
451 return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
454 #define MAX_ATIME_DIFF 60
456 static inline int mdd_permission_internal(const struct lu_env *env,
457 struct mdd_object *obj,
458 const struct lu_attr *la, int mask)
460 return __mdd_permission_internal(env, obj, la, mask, -1);
463 static inline int mdd_permission_internal_locked(const struct lu_env *env,
464 struct mdd_object *obj,
465 const struct lu_attr *la,
467 enum mdd_object_role role)
469 return __mdd_permission_internal(env, obj, la, mask, role);
472 /* mdd inline func for calling osd_dt_object ops */
473 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
476 struct dt_object *next = mdd_object_child(obj);
477 return dt_attr_get(env, next, la);
480 static inline int mdo_declare_attr_set(const struct lu_env *env,
481 struct mdd_object *obj,
482 const struct lu_attr *la,
483 struct thandle *handle)
485 struct dt_object *next = mdd_object_child(obj);
486 return dt_declare_attr_set(env, next, la, handle);
489 static inline int mdo_attr_set(const struct lu_env *env,
490 struct mdd_object *obj,
491 const struct lu_attr *la,
492 struct thandle *handle)
494 struct dt_object *next = mdd_object_child(obj);
496 if (!mdd_object_exists(obj))
499 return dt_attr_set(env, next, la, handle);
502 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
503 struct lu_buf *buf, const char *name)
505 struct dt_object *next = mdd_object_child(obj);
506 return dt_xattr_get(env, next, buf, name);
509 static inline int mdo_declare_xattr_set(const struct lu_env *env,
510 struct mdd_object *obj,
511 const struct lu_buf *buf,
513 int fl, struct thandle *handle)
515 struct dt_object *next = mdd_object_child(obj);
516 return dt_declare_xattr_set(env, next, buf, name, fl, handle);
519 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
520 const struct lu_buf *buf, const char *name,
521 int fl, struct thandle *handle)
523 struct dt_object *next = mdd_object_child(obj);
525 if (!mdd_object_exists(obj))
528 return dt_xattr_set(env, next, buf, name, fl, handle);
531 static inline int mdo_declare_xattr_del(const struct lu_env *env,
532 struct mdd_object *obj,
534 struct thandle *handle)
536 struct dt_object *next = mdd_object_child(obj);
537 return dt_declare_xattr_del(env, next, name, handle);
540 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
541 const char *name, struct thandle *handle)
543 struct dt_object *next = mdd_object_child(obj);
545 if (!mdd_object_exists(obj))
548 return dt_xattr_del(env, next, name, handle);
552 mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
555 struct dt_object *next = mdd_object_child(obj);
557 if (!mdd_object_exists(obj))
560 return dt_xattr_list(env, next, buf);
564 mdo_invalidate(const struct lu_env *env, struct mdd_object *obj)
566 return dt_invalidate(env, mdd_object_child(obj));
570 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
571 const struct lu_fid *fid, __u32 type,
572 const char *name, struct thandle *handle)
574 struct dt_object *next = mdd_object_child(obj);
578 * if the object doesn't exist yet, then it's supposed to be created
579 * and declaration of the creation should be enough to insert ./..
583 if (dt_try_as_dir(env, next)) {
584 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
587 rec->rec_type = type;
588 rc = dt_declare_insert(env, next, (const struct dt_rec *)rec,
589 (const struct dt_key *)name, handle);
596 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
597 const char *name, struct thandle *handle)
599 struct dt_object *next = mdd_object_child(obj);
601 if (!dt_try_as_dir(env, next))
604 return dt_declare_delete(env, next, (const struct dt_key *)name,
608 static inline int mdo_declare_ref_add(const struct lu_env *env,
609 struct mdd_object *obj,
610 struct thandle *handle)
612 struct dt_object *next = mdd_object_child(obj);
613 return dt_declare_ref_add(env, next, handle);
616 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
617 struct thandle *handle)
619 struct dt_object *next = mdd_object_child(obj);
621 if (!mdd_object_exists(obj))
624 return dt_ref_add(env, next, handle);
627 static inline int mdo_declare_ref_del(const struct lu_env *env,
628 struct mdd_object *obj,
629 struct thandle *handle)
631 struct dt_object *next = mdd_object_child(obj);
632 return dt_declare_ref_del(env, next, handle);
635 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
636 struct thandle *handle)
638 struct dt_object *next = mdd_object_child(obj);
640 if (!mdd_object_exists(obj))
643 return dt_ref_del(env, next, handle);
647 mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
648 struct lu_attr *attr,
649 struct dt_allocation_hint *hint,
650 struct dt_object_format *dof,
651 struct thandle *handle)
653 struct dt_object *next = mdd_object_child(o);
654 return dt_declare_create(env, next, attr, hint, dof, handle);
658 mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
659 struct lu_attr *attr,
660 struct dt_allocation_hint *hint,
661 struct dt_object_format *dof,
662 struct thandle *handle)
664 struct dt_object *next = mdd_object_child(o);
665 return dt_create(env, next, attr, hint, dof, handle);
669 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
670 struct thandle *handle)
672 struct dt_object *next = mdd_object_child(o);
673 return dt_declare_destroy(env, next, handle);
677 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
678 struct thandle *handle)
680 struct dt_object *next = mdd_object_child(o);
681 return dt_destroy(env, next, handle);