Whamcloud - gitweb
57350ea79efab20fafc7fdca582031421a54f3bb
[fs/lustre-release.git] / lustre / osc / osc_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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Whamcloud, 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 /*
37  * This file is part of Lustre, http://www.lustre.org/
38  * Lustre is a trademark of Sun Microsystems, Inc.
39  *
40  * Internal interfaces of OSC layer.
41  *
42  *   Author: Nikita Danilov <nikita.danilov@sun.com>
43  */
44
45 #ifndef OSC_CL_INTERNAL_H
46 #define OSC_CL_INTERNAL_H
47
48 #ifdef __KERNEL__
49 # include <libcfs/libcfs.h>
50 #else
51 # include <liblustre.h>
52 #endif
53
54 #include <obd.h>
55 /* osc_build_res_name() */
56 #include <obd_ost.h>
57 #include <cl_object.h>
58 #include "osc_internal.h"
59
60 /** \defgroup osc osc
61  *  @{
62  */
63
64 /**
65  * State maintained by osc layer for each IO context.
66  */
67 struct osc_io {
68         /** super class */
69         struct cl_io_slice oi_cl;
70         /** true if this io is lockless. */
71         int                oi_lockless;
72
73         struct obdo        oi_oa;
74         struct osc_setattr_cbargs {
75                 int               opc_rc;
76                 cfs_completion_t  opc_sync;
77         } oi_setattr_cbarg;
78 };
79
80 /**
81  * State of transfer for osc.
82  */
83 struct osc_req {
84         struct cl_req_slice    or_cl;
85 };
86
87 /**
88  * State maintained by osc layer for the duration of a system call.
89  */
90 struct osc_session {
91         struct osc_io       os_io;
92 };
93
94 struct osc_thread_info {
95         struct ldlm_res_id      oti_resname;
96         ldlm_policy_data_t      oti_policy;
97         struct cl_lock_descr    oti_descr;
98         struct cl_attr          oti_attr;
99         struct lustre_handle    oti_handle;
100         struct cl_page_list     oti_plist;
101 };
102
103 /**
104  * Manage osc_async_page
105  */
106 struct osc_oap_pages {
107         cfs_list_t      oop_pending;
108         cfs_list_t      oop_urgent;
109         int             oop_num_pending;
110 };
111
112 static inline void osc_oap_pages_init(struct osc_oap_pages *list)
113 {
114         CFS_INIT_LIST_HEAD(&list->oop_pending);
115         CFS_INIT_LIST_HEAD(&list->oop_urgent);
116         list->oop_num_pending = 0;
117 }
118
119 struct osc_object {
120         struct cl_object   oo_cl;
121         struct lov_oinfo  *oo_oinfo;
122         /**
123          * True if locking against this stripe got -EUSERS.
124          */
125         int                oo_contended;
126         cfs_time_t         oo_contention_time;
127 #ifdef INVARIANT_CHECK
128         /**
129          * IO context used for invariant checks in osc_lock_has_pages().
130          */
131         struct cl_io       oo_debug_io;
132         /** Serialization object for osc_object::oo_debug_io. */
133         cfs_mutex_t        oo_debug_mutex;
134 #endif
135         /**
136          * List of pages in transfer.
137          */
138         cfs_list_t         oo_inflight[CRT_NR];
139         /**
140          * Lock, protecting ccc_object::cob_inflight, because a seat-belt is
141          * locked during take-off and landing.
142          */
143         cfs_spinlock_t     oo_seatbelt;
144
145         /**
146          * used by the osc to keep track of what objects to build into rpcs
147          */
148         struct osc_oap_pages oo_read_pages;
149         struct osc_oap_pages oo_write_pages;
150         cfs_list_t           oo_ready_item;
151         cfs_list_t           oo_hp_ready_item;
152         cfs_list_t           oo_write_item;
153         cfs_list_t           oo_read_item;
154 };
155
156 /*
157  * Lock "micro-states" for osc layer.
158  */
159 enum osc_lock_state {
160         OLS_NEW,
161         OLS_ENQUEUED,
162         OLS_UPCALL_RECEIVED,
163         OLS_GRANTED,
164         OLS_RELEASED,
165         OLS_BLOCKED,
166         OLS_CANCELLED
167 };
168
169 /**
170  * osc-private state of cl_lock.
171  *
172  * Interaction with DLM.
173  *
174  * CLIO enqueues all DLM locks through ptlrpcd (that is, in "async" mode).
175  *
176  * Once receive upcall is invoked, osc_lock remembers a handle of DLM lock in
177  * osc_lock::ols_handle and a pointer to that lock in osc_lock::ols_lock.
178  *
179  * This pointer is protected through a reference, acquired by
180  * osc_lock_upcall0(). Also, an additional reference is acquired by
181  * ldlm_lock_addref() call protecting the lock from cancellation, until
182  * osc_lock_unuse() releases it.
183  *
184  * Below is a description of how lock references are acquired and released
185  * inside of DLM.
186  *
187  * - When new lock is created and enqueued to the server (ldlm_cli_enqueue())
188  *      - ldlm_lock_create()
189  *          - ldlm_lock_new(): initializes a lock with 2 references. One for
190  *            the caller (released when reply from the server is received, or on
191  *            error), and another for the hash table.
192  *      - ldlm_lock_addref_internal(): protects the lock from cancellation.
193  *
194  * - When reply is received from the server (osc_enqueue_interpret())
195  *      - ldlm_cli_enqueue_fini()
196  *          - LDLM_LOCK_PUT(): releases caller reference acquired by
197  *            ldlm_lock_new().
198  *          - if (rc != 0)
199  *                ldlm_lock_decref(): error case: matches ldlm_cli_enqueue().
200  *      - ldlm_lock_decref(): for async locks, matches ldlm_cli_enqueue().
201  *
202  * - When lock is being cancelled (ldlm_lock_cancel())
203  *      - ldlm_lock_destroy()
204  *          - LDLM_LOCK_PUT(): releases hash-table reference acquired by
205  *            ldlm_lock_new().
206  *
207  * osc_lock is detached from ldlm_lock by osc_lock_detach() that is called
208  * either when lock is cancelled (osc_lock_blocking()), or when locks is
209  * deleted without cancellation (e.g., from cl_locks_prune()). In the latter
210  * case ldlm lock remains in memory, and can be re-attached to osc_lock in the
211  * future.
212  */
213 struct osc_lock {
214         struct cl_lock_slice     ols_cl;
215         /** underlying DLM lock */
216         struct ldlm_lock        *ols_lock;
217         /** lock value block */
218         struct ost_lvb           ols_lvb;
219         /** DLM flags with which osc_lock::ols_lock was enqueued */
220         int                      ols_flags;
221         /** osc_lock::ols_lock handle */
222         struct lustre_handle     ols_handle;
223         struct ldlm_enqueue_info ols_einfo;
224         enum osc_lock_state      ols_state;
225
226         /**
227          * How many pages are using this lock for io, currently only used by
228          * read-ahead. If non-zero, the underlying dlm lock won't be cancelled
229          * during recovery to avoid deadlock. see bz16774.
230          *
231          * \see osc_page::ops_lock
232          * \see osc_page_addref_lock(), osc_page_putref_lock()
233          */
234         cfs_atomic_t             ols_pageref;
235
236         /**
237          * true, if ldlm_lock_addref() was called against
238          * osc_lock::ols_lock. This is used for sanity checking.
239          *
240          * \see osc_lock::ols_has_ref
241          */
242         unsigned                  ols_hold :1,
243         /**
244          * this is much like osc_lock::ols_hold, except that this bit is
245          * cleared _after_ reference in released in osc_lock_unuse(). This
246          * fine distinction is needed because:
247          *
248          *     - if ldlm lock still has a reference, osc_ast_data_get() needs
249          *       to return associated cl_lock (so that a flag is needed that is
250          *       cleared after ldlm_lock_decref() returned), and
251          *
252          *     - ldlm_lock_decref() can invoke blocking ast (for a
253          *       LDLM_FL_CBPENDING lock), and osc_lock functions like
254          *       osc_lock_cancel() called from there need to know whether to
255          *       release lock reference (so that a flag is needed that is
256          *       cleared before ldlm_lock_decref() is called).
257          */
258                                  ols_has_ref:1,
259         /**
260          * inherit the lockless attribute from top level cl_io.
261          * If true, osc_lock_enqueue is able to tolerate the -EUSERS error.
262          */
263                                  ols_locklessable:1,
264         /**
265          * set by osc_lock_use() to wait until blocking AST enters into
266          * osc_ldlm_blocking_ast0(), so that cl_lock mutex can be used for
267          * further synchronization.
268          */
269                                  ols_ast_wait:1,
270         /**
271          * If the data of this lock has been flushed to server side.
272          */
273                                  ols_flush:1,
274         /**
275          * if set, the osc_lock is a glimpse lock. For glimpse locks, we treat
276          * the EVAVAIL error as torerable, this will make upper logic happy
277          * to wait all glimpse locks to each OSTs to be completed.
278          * Glimpse lock converts to normal lock if the server lock is
279          * granted.
280          * Glimpse lock should be destroyed immediately after use.
281          */
282                                  ols_glimpse:1,
283         /**
284          * For async glimpse lock.
285          */
286                                  ols_agl:1;
287         /**
288          * IO that owns this lock. This field is used for a dead-lock
289          * avoidance by osc_lock_enqueue_wait().
290          *
291          * XXX: unfortunately, the owner of a osc_lock is not unique, 
292          * the lock may have multiple users, if the lock is granted and
293          * then matched.
294          */
295         struct osc_io           *ols_owner;
296 };
297
298
299 /**
300  * Page state private for osc layer.
301  */
302 struct osc_page {
303         struct cl_page_slice  ops_cl;
304         /**
305          * Page queues used by osc to detect when RPC can be formed.
306          */
307         struct osc_async_page ops_oap;
308         /**
309          * An offset within page from which next transfer starts. This is used
310          * by cl_page_clip() to submit partial page transfers.
311          */
312         int                   ops_from;
313         /**
314          * An offset within page at which next transfer ends.
315          *
316          * \see osc_page::ops_from.
317          */
318         int                   ops_to;
319         /**
320          * Boolean, true iff page is under transfer. Used for sanity checking.
321          */
322         unsigned              ops_transfer_pinned:1,
323         /**
324          * True for a `temporary page' created by read-ahead code, probably
325          * outside of any DLM lock.
326          */
327                               ops_temp:1,
328         /**
329          * Set if the page must be transferred with OBD_BRW_SRVLOCK.
330          */
331                               ops_srvlock:1;
332         /**
333          * Linkage into a per-osc_object list of pages in flight. For
334          * debugging.
335          */
336         cfs_list_t            ops_inflight;
337         /**
338          * Thread that submitted this page for transfer. For debugging.
339          */
340         cfs_task_t           *ops_submitter;
341         /**
342          * Submit time - the time when the page is starting RPC. For debugging.
343          */
344         cfs_time_t            ops_submit_time;
345
346         /**
347          * A lock of which we hold a reference covers this page. Only used by
348          * read-ahead: for a readahead page, we hold it's covering lock to
349          * prevent it from being canceled during recovery.
350          *
351          * \see osc_lock::ols_pageref
352          * \see osc_page_addref_lock(), osc_page_putref_lock().
353          */
354         struct cl_lock       *ops_lock;
355 };
356
357 extern cfs_mem_cache_t *osc_page_kmem;
358 extern cfs_mem_cache_t *osc_lock_kmem;
359 extern cfs_mem_cache_t *osc_object_kmem;
360 extern cfs_mem_cache_t *osc_thread_kmem;
361 extern cfs_mem_cache_t *osc_session_kmem;
362 extern cfs_mem_cache_t *osc_req_kmem;
363
364 extern struct lu_device_type osc_device_type;
365 extern struct lu_context_key osc_key;
366 extern struct lu_context_key osc_session_key;
367
368 #define OSC_FLAGS (ASYNC_URGENT|ASYNC_READY)
369
370 int osc_lock_init(const struct lu_env *env,
371                   struct cl_object *obj, struct cl_lock *lock,
372                   const struct cl_io *io);
373 int osc_io_init  (const struct lu_env *env,
374                   struct cl_object *obj, struct cl_io *io);
375 int osc_req_init (const struct lu_env *env, struct cl_device *dev,
376                   struct cl_req *req);
377 struct lu_object *osc_object_alloc(const struct lu_env *env,
378                                    const struct lu_object_header *hdr,
379                                    struct lu_device *dev);
380 struct cl_page   *osc_page_init   (const struct lu_env *env,
381                                    struct cl_object *obj,
382                                    struct cl_page *page, cfs_page_t *vmpage);
383
384 void osc_lock_build_res(const struct lu_env *env, const struct osc_object *obj,
385                         struct ldlm_res_id *resname);
386 void osc_index2policy  (ldlm_policy_data_t *policy, const struct cl_object *obj,
387                         pgoff_t start, pgoff_t end);
388 int  osc_lvb_print     (const struct lu_env *env, void *cookie,
389                         lu_printer_t p, const struct ost_lvb *lvb);
390
391 void osc_io_submit_page(const struct lu_env *env,
392                         struct osc_io *oio, struct osc_page *opg,
393                         enum cl_req_type crt);
394 void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
395                        struct obdo *oa, struct osc_async_page *oap,
396                        int sent, int rc);
397 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops);
398 int osc_set_async_flags(struct osc_object *obj, struct osc_page *opg,
399                         obd_flag async_flags);
400 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
401                         cfs_page_t *page, loff_t offset);
402 int osc_queue_async_io(const struct lu_env *env, struct osc_page *ops);
403 int osc_teardown_async_page(struct osc_object *obj,
404                             struct osc_page *ops);
405 int osc_queue_sync_page(const struct lu_env *env, struct osc_page *ops,
406                         int cmd, int brw_flags);
407 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
408                    struct osc_object *osc, pdl_policy_t pol);
409
410 void osc_object_set_contended  (struct osc_object *obj);
411 void osc_object_clear_contended(struct osc_object *obj);
412 int  osc_object_is_contended   (struct osc_object *obj);
413
414 int  osc_lock_is_lockless      (const struct osc_lock *olck);
415
416 /*****************************************************************************
417  *
418  * Accessors.
419  *
420  */
421
422 static inline struct osc_thread_info *osc_env_info(const struct lu_env *env)
423 {
424         struct osc_thread_info *info;
425
426         info = lu_context_key_get(&env->le_ctx, &osc_key);
427         LASSERT(info != NULL);
428         return info;
429 }
430
431 static inline struct osc_session *osc_env_session(const struct lu_env *env)
432 {
433         struct osc_session *ses;
434
435         ses = lu_context_key_get(env->le_ses, &osc_session_key);
436         LASSERT(ses != NULL);
437         return ses;
438 }
439
440 static inline struct osc_io *osc_env_io(const struct lu_env *env)
441 {
442         return &osc_env_session(env)->os_io;
443 }
444
445 static inline int osc_is_object(const struct lu_object *obj)
446 {
447         return obj->lo_dev->ld_type == &osc_device_type;
448 }
449
450 static inline struct osc_device *lu2osc_dev(const struct lu_device *d)
451 {
452         LINVRNT(d->ld_type == &osc_device_type);
453         return container_of0(d, struct osc_device, od_cl.cd_lu_dev);
454 }
455
456 static inline struct obd_export *osc_export(const struct osc_object *obj)
457 {
458         return lu2osc_dev(obj->oo_cl.co_lu.lo_dev)->od_exp;
459 }
460
461 static inline struct osc_object *cl2osc(const struct cl_object *obj)
462 {
463         LINVRNT(osc_is_object(&obj->co_lu));
464         return container_of0(obj, struct osc_object, oo_cl);
465 }
466
467 static inline ldlm_mode_t osc_cl_lock2ldlm(enum cl_lock_mode mode)
468 {
469         LASSERT(mode == CLM_READ || mode == CLM_WRITE || mode == CLM_GROUP);
470         if (mode == CLM_READ)
471                 return LCK_PR;
472         else if (mode == CLM_WRITE)
473                 return LCK_PW;
474         else
475                 return LCK_GROUP;
476 }
477
478 static inline enum cl_lock_mode osc_ldlm2cl_lock(ldlm_mode_t mode)
479 {
480         LASSERT(mode == LCK_PR || mode == LCK_PW || mode == LCK_GROUP);
481         if (mode == LCK_PR)
482                 return CLM_READ;
483         else if (mode == LCK_PW)
484                 return CLM_WRITE;
485         else
486                 return CLM_GROUP;
487 }
488
489 static inline struct osc_page *cl2osc_page(const struct cl_page_slice *slice)
490 {
491         LINVRNT(osc_is_object(&slice->cpl_obj->co_lu));
492         return container_of0(slice, struct osc_page, ops_cl);
493 }
494
495 static inline struct osc_lock *cl2osc_lock(const struct cl_lock_slice *slice)
496 {
497         LINVRNT(osc_is_object(&slice->cls_obj->co_lu));
498         return container_of0(slice, struct osc_lock, ols_cl);
499 }
500
501 static inline struct osc_lock *osc_lock_at(const struct cl_lock *lock)
502 {
503         return cl2osc_lock(cl_lock_at(lock, &osc_device_type));
504 }
505
506 static inline int osc_io_srvlock(struct osc_io *oio)
507 {
508         return (oio->oi_lockless && !oio->oi_cl.cis_io->ci_no_srvlock);
509 }
510
511 /** @} osc */
512
513 #endif /* OSC_CL_INTERNAL_H */