Merge tag 'wberr-v4.14-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton...
[sfrench/cifs-2.6.git] / drivers / staging / lustre / lustre / lov / lov_cl_internal.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015 Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Internal interfaces of LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #ifndef LOV_CL_INTERNAL_H
43 #define LOV_CL_INTERNAL_H
44
45 #include <linux/libcfs/libcfs.h>
46
47 #include <obd.h>
48 #include <cl_object.h>
49 #include "lov_internal.h"
50
51 /** \defgroup lov lov
52  * Logical object volume layer. This layer implements data striping (raid0).
53  *
54  * At the lov layer top-entity (object, page, lock, io) is connected to one or
55  * more sub-entities: top-object, representing a file is connected to a set of
56  * sub-objects, each representing a stripe, file-level top-lock is connected
57  * to a set of per-stripe sub-locks, top-page is connected to a (single)
58  * sub-page, and a top-level IO is connected to a set of (potentially
59  * concurrent) sub-IO's.
60  *
61  * Sub-object, sub-page, and sub-io have well-defined top-object and top-page
62  * respectively, while a single sub-lock can be part of multiple top-locks.
63  *
64  * Reference counting models are different for different types of entities:
65  *
66  *     - top-object keeps a reference to its sub-objects, and destroys them
67  *       when it is destroyed.
68  *
69  *     - top-page keeps a reference to its sub-page, and destroys it when it
70  *       is destroyed.
71  *
72  *     - IO's are not reference counted.
73  *
74  * To implement a connection between top and sub entities, lov layer is split
75  * into two pieces: lov ("upper half"), and lovsub ("bottom half"), both
76  * implementing full set of cl-interfaces. For example, top-object has vvp and
77  * lov layers, and it's sub-object has lovsub and osc layers. lovsub layer is
78  * used to track child-parent relationship.
79  *
80  * @{
81  */
82
83 struct lovsub_device;
84 struct lovsub_object;
85 struct lovsub_lock;
86
87 enum lov_device_flags {
88         LOV_DEV_INITIALIZED = 1 << 0
89 };
90
91 /*
92  * Upper half.
93  */
94
95 struct lov_device {
96         /*
97          * XXX Locking of lov-private data is missing.
98          */
99         struct cl_device          ld_cl;
100         struct lov_obd     *ld_lov;
101         /** size of lov_device::ld_target[] array */
102         __u32                ld_target_nr;
103         struct lovsub_device    **ld_target;
104         __u32                ld_flags;
105 };
106
107 /**
108  * Layout type.
109  */
110 enum lov_layout_type {
111         LLT_EMPTY,      /** empty file without body (mknod + truncate) */
112         LLT_RAID0,      /** striped file */
113         LLT_RELEASED,   /** file with no objects (data in HSM) */
114         LLT_NR
115 };
116
117 static inline char *llt2str(enum lov_layout_type llt)
118 {
119         switch (llt) {
120         case LLT_EMPTY:
121                 return "EMPTY";
122         case LLT_RAID0:
123                 return "RAID0";
124         case LLT_RELEASED:
125                 return "RELEASED";
126         case LLT_NR:
127                 LBUG();
128         }
129         LBUG();
130         return "";
131 }
132
133 /**
134  * lov-specific file state.
135  *
136  * lov object has particular layout type, determining how top-object is built
137  * on top of sub-objects. Layout type can change dynamically. When this
138  * happens, lov_object::lo_type_guard semaphore is taken in exclusive mode,
139  * all state pertaining to the old layout type is destroyed, and new state is
140  * constructed. All object methods take said semaphore in the shared mode,
141  * providing serialization against transition between layout types.
142  *
143  * To avoid multiple `if' or `switch' statements, selecting behavior for the
144  * current layout type, object methods perform double-dispatch, invoking
145  * function corresponding to the current layout type.
146  */
147 struct lov_object {
148         struct cl_object       lo_cl;
149         /**
150          * Serializes object operations with transitions between layout types.
151          *
152          * This semaphore is taken in shared mode by all object methods, and
153          * is taken in exclusive mode when object type is changed.
154          *
155          * \see lov_object::lo_type
156          */
157         struct rw_semaphore     lo_type_guard;
158         /**
159          * Type of an object. Protected by lov_object::lo_type_guard.
160          */
161         enum lov_layout_type    lo_type;
162         /**
163          * True if layout is invalid. This bit is cleared when layout lock
164          * is lost.
165          */
166         bool                    lo_layout_invalid;
167         /**
168          * How many IOs are on going on this object. Layout can be changed
169          * only if there is no active IO.
170          */
171         atomic_t               lo_active_ios;
172         /**
173          * Waitq - wait for no one else is using lo_lsm
174          */
175         wait_queue_head_t              lo_waitq;
176         /**
177          * Layout metadata. NULL if empty layout.
178          */
179         struct lov_stripe_md  *lo_lsm;
180
181         union lov_layout_state {
182                 struct lov_layout_raid0 {
183                         unsigned int           lo_nr;
184                         /**
185                          * When this is true, lov_object::lo_attr contains
186                          * valid up to date attributes for a top-level
187                          * object. This field is reset to 0 when attributes of
188                          * any sub-object change.
189                          */
190                         int                    lo_attr_valid;
191                         /**
192                          * Array of sub-objects. Allocated when top-object is
193                          * created (lov_init_raid0()).
194                          *
195                          * Top-object is a strict master of its sub-objects:
196                          * it is created before them, and outlives its
197                          * children (this later is necessary so that basic
198                          * functions like cl_object_top() always
199                          * work). Top-object keeps a reference on every
200                          * sub-object.
201                          *
202                          * When top-object is destroyed (lov_delete_raid0())
203                          * it releases its reference to a sub-object and waits
204                          * until the latter is finally destroyed.
205                          */
206                         struct lovsub_object **lo_sub;
207                         /**
208                          * protect lo_sub
209                          */
210                         spinlock_t              lo_sub_lock;
211                         /**
212                          * Cached object attribute, built from sub-object
213                          * attributes.
214                          */
215                         struct cl_attr   lo_attr;
216                 } raid0;
217                 struct lov_layout_state_empty {
218                 } empty;
219                 struct lov_layout_state_released {
220                 } released;
221         } u;
222         /**
223          * Thread that acquired lov_object::lo_type_guard in an exclusive
224          * mode.
225          */
226         struct task_struct      *lo_owner;
227 };
228
229 /**
230  * State lov_lock keeps for each sub-lock.
231  */
232 struct lov_lock_sub {
233         /** sub-lock itself */
234         struct cl_lock          sub_lock;
235         /** Set if the sublock has ever been enqueued, meaning it may
236          * hold resources of underlying layers
237          */
238         unsigned int            sub_is_enqueued:1,
239                                 sub_initialized:1;
240         int               sub_stripe;
241 };
242
243 /**
244  * lov-specific lock state.
245  */
246 struct lov_lock {
247         struct cl_lock_slice   lls_cl;
248         /** Number of sub-locks in this lock */
249         int                 lls_nr;
250         /** sublock array */
251         struct lov_lock_sub     lls_sub[0];
252 };
253
254 struct lov_page {
255         struct cl_page_slice    lps_cl;
256         unsigned int            lps_stripe; /* stripe index */
257 };
258
259 /*
260  * Bottom half.
261  */
262
263 struct lovsub_device {
264         struct cl_device   acid_cl;
265         struct cl_device  *acid_next;
266 };
267
268 struct lovsub_object {
269         struct cl_object_header lso_header;
270         struct cl_object        lso_cl;
271         struct lov_object      *lso_super;
272         int                  lso_index;
273 };
274
275 /**
276  * Lock state at lovsub layer.
277  */
278 struct lovsub_lock {
279         struct cl_lock_slice  lss_cl;
280 };
281
282 /**
283  * Describe the environment settings for sublocks.
284  */
285 struct lov_sublock_env {
286         const struct lu_env *lse_env;
287         struct cl_io    *lse_io;
288 };
289
290 struct lovsub_page {
291         struct cl_page_slice lsb_cl;
292 };
293
294 struct lov_thread_info {
295         struct cl_object_conf   lti_stripe_conf;
296         struct lu_fid      lti_fid;
297         struct ost_lvb    lti_lvb;
298         struct cl_2queue        lti_cl2q;
299         struct cl_page_list     lti_plist;
300         wait_queue_entry_t        lti_waiter;
301 };
302
303 /**
304  * State that lov_io maintains for every sub-io.
305  */
306 struct lov_io_sub {
307         u16              sub_stripe;
308         /**
309          * environment's refcheck.
310          *
311          * \see cl_env_get()
312          */
313         u16                      sub_refcheck;
314         /**
315          * true, iff cl_io_init() was successfully executed against
316          * lov_io_sub::sub_io.
317          */
318         u16                      sub_io_initialized:1,
319         /**
320          * True, iff lov_io_sub::sub_io and lov_io_sub::sub_env weren't
321          * allocated, but borrowed from a per-device emergency pool.
322          */
323                                  sub_borrowed:1;
324         /**
325          * Linkage into a list (hanging off lov_io::lis_active) of all
326          * sub-io's active for the current IO iteration.
327          */
328         struct list_head         sub_linkage;
329         /**
330          * sub-io for a stripe. Ideally sub-io's can be stopped and resumed
331          * independently, with lov acting as a scheduler to maximize overall
332          * throughput.
333          */
334         struct cl_io    *sub_io;
335         /**
336          * environment, in which sub-io executes.
337          */
338         struct lu_env *sub_env;
339 };
340
341 /**
342  * IO state private for LOV.
343  */
344 struct lov_io {
345         /** super-class */
346         struct cl_io_slice lis_cl;
347         /**
348          * Pointer to the object slice. This is a duplicate of
349          * lov_io::lis_cl::cis_object.
350          */
351         struct lov_object *lis_object;
352         /**
353          * Original end-of-io position for this IO, set by the upper layer as
354          * cl_io::u::ci_rw::pos + cl_io::u::ci_rw::count. lov remembers this,
355          * changes pos and count to fit IO into a single stripe and uses saved
356          * value to determine when IO iterations have to stop.
357          *
358          * This is used only for CIT_READ and CIT_WRITE io's.
359          */
360         loff_t       lis_io_endpos;
361
362         /**
363          * starting position within a file, for the current io loop iteration
364          * (stripe), used by ci_io_loop().
365          */
366         u64         lis_pos;
367         /**
368          * end position with in a file, for the current stripe io. This is
369          * exclusive (i.e., next offset after last byte affected by io).
370          */
371         u64         lis_endpos;
372
373         int             lis_stripe_count;
374         int             lis_active_subios;
375
376         /**
377          * the index of ls_single_subio in ls_subios array
378          */
379         int             lis_single_subio_index;
380         struct cl_io       lis_single_subio;
381
382         /**
383          * size of ls_subios array, actually the highest stripe #
384          */
385         int             lis_nr_subios;
386         struct lov_io_sub *lis_subs;
387         /**
388          * List of active sub-io's.
389          */
390         struct list_head         lis_active;
391 };
392
393 struct lov_session {
394         struct lov_io     ls_io;
395         struct lov_sublock_env ls_subenv;
396 };
397
398 extern struct lu_device_type lov_device_type;
399 extern struct lu_device_type lovsub_device_type;
400
401 extern struct lu_context_key lov_key;
402 extern struct lu_context_key lov_session_key;
403
404 extern struct kmem_cache *lov_lock_kmem;
405 extern struct kmem_cache *lov_object_kmem;
406 extern struct kmem_cache *lov_thread_kmem;
407 extern struct kmem_cache *lov_session_kmem;
408
409 extern struct kmem_cache *lovsub_lock_kmem;
410 extern struct kmem_cache *lovsub_object_kmem;
411
412 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
413                     const struct lu_object_conf *conf);
414 int lovsub_object_init(const struct lu_env *env, struct lu_object *obj,
415                        const struct lu_object_conf *conf);
416 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
417                   struct cl_lock *lock, const struct cl_io *io);
418 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
419                 struct cl_io *io);
420 int lovsub_lock_init(const struct lu_env *env, struct cl_object *obj,
421                      struct cl_lock *lock, const struct cl_io *io);
422
423 int lov_lock_init_raid0(const struct lu_env *env, struct cl_object *obj,
424                         struct cl_lock *lock, const struct cl_io *io);
425 int lov_lock_init_empty(const struct lu_env *env, struct cl_object *obj,
426                         struct cl_lock *lock, const struct cl_io *io);
427 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
428                       struct cl_io *io);
429 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
430                       struct cl_io *io);
431 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
432                          struct cl_io *io);
433
434 struct lov_io_sub *lov_sub_get(const struct lu_env *env, struct lov_io *lio,
435                                int stripe);
436
437 int lov_page_init(const struct lu_env *env, struct cl_object *ob,
438                   struct cl_page *page, pgoff_t index);
439 int lovsub_page_init(const struct lu_env *env, struct cl_object *ob,
440                      struct cl_page *page, pgoff_t index);
441 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
442                         struct cl_page *page, pgoff_t index);
443 int lov_page_init_raid0(const struct lu_env *env, struct cl_object *obj,
444                         struct cl_page *page, pgoff_t index);
445 struct lu_object *lov_object_alloc(const struct lu_env *env,
446                                    const struct lu_object_header *hdr,
447                                    struct lu_device *dev);
448 struct lu_object *lovsub_object_alloc(const struct lu_env *env,
449                                       const struct lu_object_header *hdr,
450                                       struct lu_device *dev);
451
452 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov);
453 int lov_page_stripe(const struct cl_page *page);
454
455 #define lov_foreach_target(lov, var)                \
456         for (var = 0; var < lov_targets_nr(lov); ++var)
457
458 /*****************************************************************************
459  *
460  * Type conversions.
461  *
462  * Accessors.
463  *
464  */
465
466 static inline struct lov_session *lov_env_session(const struct lu_env *env)
467 {
468         struct lov_session *ses;
469
470         ses = lu_context_key_get(env->le_ses, &lov_session_key);
471         LASSERT(ses);
472         return ses;
473 }
474
475 static inline struct lov_io *lov_env_io(const struct lu_env *env)
476 {
477         return &lov_env_session(env)->ls_io;
478 }
479
480 static inline int lov_is_object(const struct lu_object *obj)
481 {
482         return obj->lo_dev->ld_type == &lov_device_type;
483 }
484
485 static inline int lovsub_is_object(const struct lu_object *obj)
486 {
487         return obj->lo_dev->ld_type == &lovsub_device_type;
488 }
489
490 static inline struct lu_device *lov2lu_dev(struct lov_device *lov)
491 {
492         return &lov->ld_cl.cd_lu_dev;
493 }
494
495 static inline struct lov_device *lu2lov_dev(const struct lu_device *d)
496 {
497         LINVRNT(d->ld_type == &lov_device_type);
498         return container_of0(d, struct lov_device, ld_cl.cd_lu_dev);
499 }
500
501 static inline struct cl_device *lovsub2cl_dev(struct lovsub_device *lovsub)
502 {
503         return &lovsub->acid_cl;
504 }
505
506 static inline struct lu_device *lovsub2lu_dev(struct lovsub_device *lovsub)
507 {
508         return &lovsub2cl_dev(lovsub)->cd_lu_dev;
509 }
510
511 static inline struct lovsub_device *lu2lovsub_dev(const struct lu_device *d)
512 {
513         LINVRNT(d->ld_type == &lovsub_device_type);
514         return container_of0(d, struct lovsub_device, acid_cl.cd_lu_dev);
515 }
516
517 static inline struct lovsub_device *cl2lovsub_dev(const struct cl_device *d)
518 {
519         LINVRNT(d->cd_lu_dev.ld_type == &lovsub_device_type);
520         return container_of0(d, struct lovsub_device, acid_cl);
521 }
522
523 static inline struct lu_object *lov2lu(struct lov_object *lov)
524 {
525         return &lov->lo_cl.co_lu;
526 }
527
528 static inline struct cl_object *lov2cl(struct lov_object *lov)
529 {
530         return &lov->lo_cl;
531 }
532
533 static inline struct lov_object *lu2lov(const struct lu_object *obj)
534 {
535         LINVRNT(lov_is_object(obj));
536         return container_of0(obj, struct lov_object, lo_cl.co_lu);
537 }
538
539 static inline struct lov_object *cl2lov(const struct cl_object *obj)
540 {
541         LINVRNT(lov_is_object(&obj->co_lu));
542         return container_of0(obj, struct lov_object, lo_cl);
543 }
544
545 static inline struct lu_object *lovsub2lu(struct lovsub_object *los)
546 {
547         return &los->lso_cl.co_lu;
548 }
549
550 static inline struct cl_object *lovsub2cl(struct lovsub_object *los)
551 {
552         return &los->lso_cl;
553 }
554
555 static inline struct lovsub_object *cl2lovsub(const struct cl_object *obj)
556 {
557         LINVRNT(lovsub_is_object(&obj->co_lu));
558         return container_of0(obj, struct lovsub_object, lso_cl);
559 }
560
561 static inline struct lovsub_object *lu2lovsub(const struct lu_object *obj)
562 {
563         LINVRNT(lovsub_is_object(obj));
564         return container_of0(obj, struct lovsub_object, lso_cl.co_lu);
565 }
566
567 static inline struct lovsub_lock *
568 cl2lovsub_lock(const struct cl_lock_slice *slice)
569 {
570         LINVRNT(lovsub_is_object(&slice->cls_obj->co_lu));
571         return container_of(slice, struct lovsub_lock, lss_cl);
572 }
573
574 static inline struct lovsub_lock *cl2sub_lock(const struct cl_lock *lock)
575 {
576         const struct cl_lock_slice *slice;
577
578         slice = cl_lock_at(lock, &lovsub_device_type);
579         LASSERT(slice);
580         return cl2lovsub_lock(slice);
581 }
582
583 static inline struct lov_lock *cl2lov_lock(const struct cl_lock_slice *slice)
584 {
585         LINVRNT(lov_is_object(&slice->cls_obj->co_lu));
586         return container_of(slice, struct lov_lock, lls_cl);
587 }
588
589 static inline struct lov_page *cl2lov_page(const struct cl_page_slice *slice)
590 {
591         LINVRNT(lov_is_object(&slice->cpl_obj->co_lu));
592         return container_of0(slice, struct lov_page, lps_cl);
593 }
594
595 static inline struct lovsub_page *
596 cl2lovsub_page(const struct cl_page_slice *slice)
597 {
598         LINVRNT(lovsub_is_object(&slice->cpl_obj->co_lu));
599         return container_of0(slice, struct lovsub_page, lsb_cl);
600 }
601
602 static inline struct lov_io *cl2lov_io(const struct lu_env *env,
603                                        const struct cl_io_slice *ios)
604 {
605         struct lov_io *lio;
606
607         lio = container_of(ios, struct lov_io, lis_cl);
608         LASSERT(lio == lov_env_io(env));
609         return lio;
610 }
611
612 static inline int lov_targets_nr(const struct lov_device *lov)
613 {
614         return lov->ld_lov->desc.ld_tgt_count;
615 }
616
617 static inline struct lov_thread_info *lov_env_info(const struct lu_env *env)
618 {
619         struct lov_thread_info *info;
620
621         info = lu_context_key_get(&env->le_ctx, &lov_key);
622         LASSERT(info);
623         return info;
624 }
625
626 static inline struct lov_layout_raid0 *lov_r0(struct lov_object *lov)
627 {
628         LASSERT(lov->lo_type == LLT_RAID0);
629         LASSERT(lov->lo_lsm->lsm_magic == LOV_MAGIC ||
630                 lov->lo_lsm->lsm_magic == LOV_MAGIC_V3);
631         return &lov->u.raid0;
632 }
633
634 /* lov_pack.c */
635 int lov_getstripe(struct lov_object *obj, struct lov_stripe_md *lsm,
636                   struct lov_user_md __user *lump);
637
638 /** @} lov */
639
640 #endif