1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2012, 2017, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
13 * Internal interfaces of LOV layer.
15 * Author: Nikita Danilov <nikita.danilov@sun.com>
16 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
19 #ifndef LOV_CL_INTERNAL_H
20 #define LOV_CL_INTERNAL_H
22 #include <libcfs/libcfs.h>
24 #include <cl_object.h>
25 #include "lov_internal.h"
28 * Logical object volume layer. This layer implements data striping (raid0).
30 * At the lov layer top-entity (object, lock, io) is connected to one or
31 * more sub-entities: top-object, representing a file is connected to a set of
32 * sub-objects, each representing a stripe, file-level top-lock is connected
33 * to a set of per-stripe sub-locks, and a top-level IO is connected to a set of
34 * (potentially concurrent) sub-IO's.
36 * Sub-object and sub-io have well-defined top-object and top-io
37 * respectively, while a single sub-lock can be part of multiple top-locks.
39 * Reference counting models are different for different types of entities:
41 * - top-object keeps a reference to its sub-objects, and destroys them
42 * when it is destroyed.
44 * - IO's are not reference counted.
46 * To implement a connection between top and sub entities, lov layer is split
47 * into two pieces: lov ("upper half"), and lovsub ("bottom half"), both
48 * implementing full set of cl-interfaces. For example, top-object has vvp and
49 * lov layers, and it's sub-object has lovsub and osc layers. lovsub layer is
50 * used to track child-parent relationship.
58 enum lov_device_flags {
59 LOV_DEV_INITIALIZED = BIT(0),
66 /* Data-on-MDT array item in lov_device::ld_md_tgts[] */
67 struct lovdom_device {
68 struct cl_device *ldm_mdc;
74 * XXX Locking of lov-private data is missing.
76 struct cl_device ld_cl;
77 struct lov_obd *ld_lov;
78 /* size of lov_device::ld_target[] array */
80 struct lovsub_device **ld_target;
83 /* Data-on-MDT devices */
85 struct lovdom_device *ld_md_tgts;
86 struct obd_device *ld_lmv;
87 /* LU site for subdevices */
88 struct lu_site ld_site;
94 enum lov_layout_type {
95 LLT_EMPTY, /* empty file without body (mknod + truncate) */
96 LLT_RELEASED, /* file with no objects (data in HSM) */
97 LLT_COMP, /* support composite layout */
98 LLT_FOREIGN, /* foreign layout */
102 static inline char *llt2str(enum lov_layout_type llt)
121 * Return lov_layout_entry_type associated with a given composite layout
124 static inline __u32 lov_entry_type(struct lov_stripe_md_entry *lsme)
126 if ((lov_pattern(lsme->lsme_pattern) & LOV_PATTERN_RAID0) ||
127 (lov_pattern(lsme->lsme_pattern) & LOV_PATTERN_MDT) ||
128 (lov_pattern(lsme->lsme_pattern) == LOV_PATTERN_FOREIGN))
129 return lov_pattern(lsme->lsme_pattern &
130 ~(LOV_PATTERN_OVERSTRIPING | LOV_PATTERN_COMPRESS));
134 struct lov_layout_entry;
138 struct lov_comp_layout_entry_ops {
139 int (*lco_init)(const struct lu_env *env, struct lov_device *dev,
140 struct lov_object *lov, unsigned int index,
141 const struct cl_object_conf *conf,
142 struct lov_layout_entry *lle);
143 void (*lco_fini)(const struct lu_env *env,
144 struct lov_layout_entry *lle);
145 int (*lco_getattr)(const struct lu_env *env, struct lov_object *obj,
146 unsigned int index, struct lov_layout_entry *lle,
147 struct cl_attr **attr);
150 struct lov_layout_raid0 {
153 * When this is true, lov_object::lo_attr contains
154 * valid up to date attributes for a top-level
155 * object. This field is reset to 0 when attributes of
156 * any sub-object change.
160 * Array of sub-objects. Allocated when top-object is
161 * created (lov_init_raid0()).
163 * Top-object is a strict master of its sub-objects:
164 * it is created before them, and outlives its
165 * children (this later is necessary so that basic
166 * functions like cl_object_top() always
167 * work). Top-object keeps a reference on every
170 * When top-object is destroyed (lov_delete_raid0())
171 * it releases its reference to a sub-object and waits
172 * until the latter is finally destroyed.
174 struct lovsub_object **lo_sub;
178 spinlock_t lo_sub_lock;
180 * Cached object attribute, built from sub-object
183 struct cl_attr lo_attr;
186 struct lov_layout_dom {
187 /* keep this always at first place so DOM layout entry
188 * can be addressed also as RAID0 after initialization.
190 struct lov_layout_raid0 lo_dom_r0;
191 struct lovsub_object *lo_dom;
192 struct lov_oinfo *lo_loi;
193 unsigned short lo_mdt_idx;
196 struct lov_layout_entry {
198 unsigned int lle_valid:1;
199 unsigned int lle_preference;
200 struct lu_extent *lle_extent;
201 struct lov_stripe_md_entry *lle_lsme;
202 struct lov_comp_layout_entry_ops *lle_comp_ops;
204 struct lov_layout_raid0 lle_raid0;
205 struct lov_layout_dom lle_dom;
209 struct lov_mirror_entry {
210 unsigned short lre_mirror_id;
211 unsigned short lre_stale:1, /* set if any components is stale */
212 /* set if one of components in this mirror is valid */
214 lre_foreign:1; /* set if it is a foreign component */
215 int lre_preference; /* overall preference of this mirror */
217 unsigned short lre_start; /* idx(lo_entries) start idx (mirror) */
218 unsigned short lre_end; /* end index of this mirror */
221 enum lov_object_flags {
222 /* Layout is invalid, set when layout lock is lost */
223 LO_LAYOUT_INVALID = 0x1,
224 LO_NEED_INODE_LOCK = 0x2,
228 * lov-specific file state.
230 * lov object has particular layout type, determining how top-object is built
231 * on top of sub-objects. Layout type can change dynamically. When this
232 * happens, lov_object::lo_type_guard semaphore is taken in exclusive mode,
233 * all state pertaining to the old layout type is destroyed, and new state is
234 * constructed. All object methods take said semaphore in the shared mode,
235 * providing serialization against transition between layout types.
237 * To avoid multiple `if' or `switch' statements, selecting behavior for the
238 * current layout type, object methods perform double-dispatch, invoking
239 * function corresponding to the current layout type.
242 struct cl_object lo_cl;
244 * Serializes object operations with transitions between layout types.
246 * This semaphore is taken in shared mode by all object methods, and
247 * is taken in exclusive mode when object type is changed.
249 * \see lov_object::lo_type
251 struct rw_semaphore lo_type_guard;
253 * Type of an object. Protected by lov_object::lo_type_guard.
255 enum lov_layout_type lo_type;
259 unsigned long lo_obj_flags;
261 * How many IOs are on going on this object. Layout can be changed
262 * only if there is no active IO.
264 atomic_t lo_active_ios;
266 * Waitq - wait for no one else is using lo_lsm
268 wait_queue_head_t lo_waitq;
270 * Layout metadata. NULL if empty layout.
272 struct lov_stripe_md *lo_lsm;
274 union lov_layout_state {
275 struct lov_layout_state_empty {
277 struct lov_layout_state_released {
279 struct lov_layout_composite {
280 /* flags of lov_comp_md_v1::lcm_flags. Mainly used
284 /* For FLR: index of preferred mirror to read.
285 * Preferred mirror is initialized by the preferred
286 * bit of lsme. It can be changed when the preferred
289 int lo_preferred_mirror;
290 /* For FLR: Number of (valid) mirrors. */
291 unsigned int lo_mirror_count;
292 struct lov_mirror_entry *lo_mirrors;
293 /* Current entry count of lo_entries, include
296 unsigned int lo_entry_count;
297 struct lov_layout_entry *lo_entries;
300 /* Thread that acquired lov_object::lo_type_guard in exclusive mode. */
301 struct task_struct *lo_owner;
304 static inline const struct lu_fid *lov_object_fid(const struct lov_object *lov)
306 return lu_object_fid(&lov->lo_cl.co_lu);
309 static inline struct lov_stripe_md_entry *lov_lse(struct lov_object *lov, int i)
311 LASSERT(lov->lo_lsm != NULL);
312 LASSERT(i < lov->lo_lsm->lsm_entry_count);
314 return lov->lo_lsm->lsm_entries[i];
317 static inline unsigned int lov_flr_state(const struct lov_object *lov)
319 if (lov->lo_type != LLT_COMP)
322 return lov->u.composite.lo_flags & LCM_FL_FLR_MASK;
325 static inline bool lov_is_flr(const struct lov_object *lov)
327 return lov_flr_state(lov) != LCM_FL_NONE;
330 static inline struct lov_layout_entry *lov_entry(struct lov_object *lov, int i)
332 LASSERT(lov->lo_type == LLT_COMP);
333 LASSERTF(i < lov->u.composite.lo_entry_count,
334 DFID" entry %d, entry_count %d\n",
335 PFID(lov_object_fid(lov)),
336 i, lov->u.composite.lo_entry_count);
338 return &lov->u.composite.lo_entries[i];
341 static inline struct lov_layout_raid0 *lov_r0(struct lov_object *lov, int i)
343 return &lov_entry(lov, i)->lle_raid0;
346 #define lov_for_layout_entry(lov, entry, start, end) \
347 if (lov->u.composite.lo_entries && \
348 lov->u.composite.lo_entry_count > 0) \
349 for (entry = lov_entry(lov, start); \
350 entry <= lov_entry(lov, end); entry++)
352 #define lov_foreach_layout_entry(lov, entry) \
353 lov_for_layout_entry(lov, entry, 0, \
354 (lov)->u.composite.lo_entry_count - 1)
356 #define lov_foreach_mirror_layout_entry(lov, entry, lre) \
357 lov_for_layout_entry(lov, entry, (lre)->lre_start, (lre)->lre_end)
359 static inline struct lov_mirror_entry *
360 lov_mirror_entry(struct lov_object *lov, int i)
362 LASSERTF(i < lov->u.composite.lo_mirror_count,
363 DFID" entry %d, mirror_count %d\n",
364 PFID(lov_object_fid(lov)),
365 i, lov->u.composite.lo_mirror_count);
367 return &lov->u.composite.lo_mirrors[i];
370 #define lov_foreach_mirror_entry(lov, lre) \
371 for (lre = lov_mirror_entry(lov, 0); \
372 lre <= lov_mirror_entry(lov, \
373 lov->u.composite.lo_mirror_count - 1); \
376 static inline unsigned
377 lov_layout_entry_index(struct lov_object *lov, struct lov_layout_entry *entry)
379 struct lov_layout_entry *first = &lov->u.composite.lo_entries[0];
380 unsigned int index = (unsigned int)(entry - first);
382 LASSERT(entry >= first);
383 LASSERT(index < lov->u.composite.lo_entry_count);
388 /* State lov_lock keeps for each sub-lock. */
389 struct lov_lock_sub {
390 /* sub-lock itself */
391 struct cl_lock sub_lock;
392 /* Set if the sublock has ever been enqueued, meaning it may
393 * hold resources of underlying layers
395 unsigned int sub_is_enqueued:1,
400 /* lov-specific lock state. */
402 struct cl_lock_slice lls_cl;
403 /* Number of sub-locks in this lock */
406 struct lov_lock_sub lls_sub[];
410 struct lovsub_device {
411 struct cl_device acid_cl;
412 struct cl_device *acid_next;
415 struct lovsub_object {
416 struct cl_object_header lso_header;
417 struct cl_object lso_cl;
418 struct lov_object *lso_super;
422 /* Describe the environment settings for sublocks. */
423 struct lov_sublock_env {
424 const struct lu_env *lse_env;
425 struct cl_io *lse_io;
428 struct lov_thread_info {
429 struct cl_object_conf lti_stripe_conf;
430 struct lu_fid lti_fid;
431 struct ost_lvb lti_lvb;
432 struct cl_2queue lti_cl2q;
433 struct cl_page_list lti_plist;
436 /* State that lov_io maintains for every sub-io. */
438 /* Linkage into a list (hanging off lov_io::lis_subios) */
439 struct list_head sub_list;
440 /* Linkage into a list (hanging off lov_io::lis_active) of all
441 * sub-io's active for the current IO iteration.
443 struct list_head sub_linkage;
444 unsigned int sub_subio_index;
445 /* sub-io for a stripe. Ideally sub-io's can be stopped and resumed
446 * independently, with lov acting as a scheduler to maximize overall
450 /* environment, in which sub-io executes. */
451 struct lu_env *sub_env;
452 /* environment's refcheck. (cl_env_get()) */
456 /* IO state private for LOV. */
457 #define LIS_CACHE_ENTRY_NONE -ENOENT
460 struct cl_io_slice lis_cl;
462 /* FLR: index to lo_mirrors. Valid only if lov_is_flr() returns true.
464 * The mirror index of this io. Preserved over cl_io_init()
465 * if io->ci_ndelay_tried is greater than zero.
467 int lis_mirror_index;
468 /* FLR: the layout gen when lis_mirror_index was cached. The
469 * mirror index makes sense only when the layout gen doesn't
472 int lis_mirror_layout_gen;
474 /* fields below this will be initialized in lov_io_init(). */
475 unsigned int lis_preserved;
477 /* Pointer to obj slice. Duplicate of lov_io::lis_cl::cis_object. */
478 struct lov_object *lis_object;
480 * Original end-of-io position for this IO, set by the upper layer as
481 * cl_io::u::ci_rw::pos + cl_io::u::ci_rw::count. lov remembers this,
482 * changes pos and count to fit IO into a single stripe and uses saved
483 * value to determine when IO iterations have to stop.
485 * This is used only for CIT_READ and CIT_WRITE io's.
487 loff_t lis_io_endpos;
489 /* Record stripe index before the truncate size, used for setting OST
490 * obj size for truncate. LU-14128. lis_trunc_stripe_index[i] refers to
491 * lov_object.u.composite.lo_entries[i].
493 int *lis_trunc_stripe_index;
495 /* starting position within a file, for the current io loop iteration
496 * (stripe), used by ci_io_loop().
499 /* end position with in a file, for the current stripe io. This is
500 * exclusive (i.e., next offset after last byte affected by io).
505 /* the index of ls_single_subio in ls_subios array */
506 int lis_single_subio_index;
507 struct lov_io_sub lis_single_subio;
509 /* List of active sub-io's. Active sub-io's are under the range
510 * of [lis_pos, lis_endpos).
512 struct list_head lis_active;
513 /* All sub-io's created in this lov_io. */
514 struct list_head lis_subios;
515 /* Cached results from stripe & offset calculations for page init */
516 int lis_cached_entry;
517 int lis_cached_stripe;
518 loff_t lis_cached_off;
519 loff_t lis_cached_suboff;
520 struct lov_io_sub *lis_cached_sub;
525 struct lov_sublock_env ls_subenv;
528 extern struct lu_device_type lov_device_type;
529 extern struct lu_device_type lovsub_device_type;
531 extern struct lu_context_key lov_key;
532 extern struct lu_context_key lov_session_key;
534 extern struct kmem_cache *lov_lock_kmem;
535 extern struct kmem_cache *lov_object_kmem;
536 extern struct kmem_cache *lov_thread_kmem;
537 extern struct kmem_cache *lov_session_kmem;
539 extern struct kmem_cache *lovsub_object_kmem;
541 int lov_lock_init_composite(const struct lu_env *env, struct cl_object *obj,
542 struct cl_lock *lock, const struct cl_io *io);
543 int lov_lock_init_empty(const struct lu_env *env, struct cl_object *obj,
544 struct cl_lock *lock, const struct cl_io *io);
545 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
547 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
549 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
552 struct lov_io_sub *lov_sub_get(const struct lu_env *env, struct lov_io *lio,
556 CP_LOV_INDEX_EMPTY = -1U,
559 static inline bool lov_pages_is_empty(struct cl_dio_pages *cdp)
561 return cdp->cdp_lov_index == CP_LOV_INDEX_EMPTY;
564 static inline bool lov_page_is_empty(const struct cl_page *cp)
566 return cp->cp_lov_index == CP_LOV_INDEX_EMPTY;
570 int lov_dio_pages_init_empty(const struct lu_env *env, struct cl_object *obj,
571 struct cl_dio_pages *cdp, pgoff_t index);
572 int lov_dio_pages_init_composite(const struct lu_env *env,
573 struct cl_object *obj,
574 struct cl_dio_pages *cdp, pgoff_t index);
575 int lov_dio_pages_init_foreign(const struct lu_env *env, struct cl_object *obj,
576 struct cl_dio_pages *cdp, pgoff_t index);
577 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
578 struct cl_page *page, pgoff_t index);
579 int lov_page_init_composite(const struct lu_env *env, struct cl_object *obj,
580 struct cl_page *page, pgoff_t index);
581 int lov_page_init_foreign(const struct lu_env *env, struct cl_object *obj,
582 struct cl_page *page, pgoff_t index);
583 struct lu_object *lov_object_alloc(const struct lu_env *env,
584 const struct lu_object_header *hdr,
585 struct lu_device *dev);
587 struct lu_object *lovsub_object_alloc(const struct lu_env *env,
588 const struct lu_object_header *hdr,
589 struct lu_device *dev);
591 int lov_io_layout_at(struct lov_io *lio, __u64 offset);
592 bool lov_io_layout_at_confirm(struct lov_io *lio, int entry, __u64 offset);
594 static inline struct lu_extent *lov_io_extent(struct lov_io *io, int i)
596 return &lov_lse(io->lis_object, i)->lsme_extent;
599 /* For layout entries within @ext. */
600 #define lov_foreach_io_layout(ind, lio, ext) \
601 for (ind = lov_io_layout_at(lio, (ext)->e_start); \
603 lu_extent_is_overlapped(lov_io_extent(lio, ind), ext); \
604 ind = lov_io_layout_at(lio, lov_io_extent(lio, ind)->e_end))
611 static inline struct lov_session *lov_env_session(const struct lu_env *env)
613 struct lov_session *ses;
615 ses = lu_context_key_get(env->le_ses, &lov_session_key);
616 LASSERT(ses != NULL);
620 static inline struct lov_io *lov_env_io(const struct lu_env *env)
622 return &lov_env_session(env)->ls_io;
625 static inline int lov_is_object(const struct lu_object *obj)
627 return obj->lo_dev->ld_type == &lov_device_type;
630 static inline int lovsub_is_object(const struct lu_object *obj)
632 return obj->lo_dev->ld_type == &lovsub_device_type;
635 static inline struct lu_device *lov2lu_dev(struct lov_device *lov)
637 return &lov->ld_cl.cd_lu_dev;
640 static inline struct lov_device *lu2lov_dev(const struct lu_device *d)
642 LINVRNT(d->ld_type == &lov_device_type);
643 return container_of(d, struct lov_device, ld_cl.cd_lu_dev);
646 static inline struct cl_device *lovsub2cl_dev(struct lovsub_device *lovsub)
648 return &lovsub->acid_cl;
651 static inline struct lu_device *lovsub2lu_dev(struct lovsub_device *lovsub)
653 return &lovsub2cl_dev(lovsub)->cd_lu_dev;
656 static inline struct lovsub_device *lu2lovsub_dev(const struct lu_device *d)
658 LINVRNT(d->ld_type == &lovsub_device_type);
659 return container_of(d, struct lovsub_device, acid_cl.cd_lu_dev);
662 static inline struct lovsub_device *cl2lovsub_dev(const struct cl_device *d)
664 LINVRNT(d->cd_lu_dev.ld_type == &lovsub_device_type);
665 return container_of(d, struct lovsub_device, acid_cl);
668 static inline struct lu_object *lov2lu(struct lov_object *lov)
670 return &lov->lo_cl.co_lu;
673 static inline struct cl_object *lov2cl(struct lov_object *lov)
678 static inline struct lov_object *lu2lov(const struct lu_object *obj)
680 LINVRNT(lov_is_object(obj));
681 return container_of(obj, struct lov_object, lo_cl.co_lu);
684 static inline struct lov_object *cl2lov(const struct cl_object *obj)
686 LINVRNT(lov_is_object(&obj->co_lu));
687 return container_of(obj, struct lov_object, lo_cl);
690 static inline struct lu_object *lovsub2lu(struct lovsub_object *los)
692 return &los->lso_cl.co_lu;
695 static inline struct cl_object *lovsub2cl(struct lovsub_object *los)
700 static inline struct lovsub_object *cl2lovsub(const struct cl_object *obj)
702 LINVRNT(lovsub_is_object(&obj->co_lu));
703 return container_of(obj, struct lovsub_object, lso_cl);
706 static inline struct lovsub_object *lu2lovsub(const struct lu_object *obj)
708 LINVRNT(lovsub_is_object(obj));
709 return container_of(obj, struct lovsub_object, lso_cl.co_lu);
712 static inline struct lov_lock *cl2lov_lock(const struct cl_lock_slice *slice)
714 LINVRNT(lov_is_object(&slice->cls_obj->co_lu));
715 return container_of(slice, struct lov_lock, lls_cl);
718 static inline struct lov_io *cl2lov_io(const struct lu_env *env,
719 const struct cl_io_slice *ios)
723 lio = container_of(ios, struct lov_io, lis_cl);
724 LASSERT(lio == lov_env_io(env));
728 static inline struct lov_thread_info *lov_env_info(const struct lu_env *env)
730 struct lov_thread_info *info;
732 info = lu_context_key_get(&env->le_ctx, &lov_key);
733 LASSERT(info != NULL);
738 int lov_getstripe(const struct lu_env *env, struct lov_object *obj,
739 struct lov_stripe_md *lsm, struct lov_user_md __user *lump,