Whamcloud - gitweb
LU-3259 clio: cl_lock simplification
[fs/lustre-release.git] / lustre / include / lclient.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) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Definitions shared between vvp and liblustre, and other clients in the
37  * future.
38  *
39  *   Author: Oleg Drokin <oleg.drokin@sun.com>
40  *   Author: Nikita Danilov <nikita.danilov@sun.com>
41  */
42
43 #ifndef LCLIENT_H
44 #define LCLIENT_H
45
46 #include <lustre/lustre_idl.h>
47 #include <cl_object.h>
48
49 enum obd_notify_event;
50 struct inode;
51 struct lov_stripe_md;
52 struct lustre_md;
53 struct obd_capa;
54 struct obd_device;
55 struct obd_export;
56 struct page;
57
58 blkcnt_t dirty_cnt(struct inode *inode);
59
60 int cl_glimpse_size0(struct inode *inode, int agl);
61 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
62                     struct inode *inode, struct cl_object *clob, int agl);
63
64 static inline int cl_glimpse_size(struct inode *inode)
65 {
66         return cl_glimpse_size0(inode, 0);
67 }
68
69 static inline int cl_agl(struct inode *inode)
70 {
71         return cl_glimpse_size0(inode, 1);
72 }
73
74 /**
75  * Locking policy for setattr.
76  */
77 enum ccc_setattr_lock_type {
78         /** Locking is done by server */
79         SETATTR_NOLOCK,
80         /** Extent lock is enqueued */
81         SETATTR_EXTENT_LOCK,
82         /** Existing local extent lock is used */
83         SETATTR_MATCH_LOCK
84 };
85
86
87 /**
88  * IO state private to vvp or slp layers.
89  */
90 struct ccc_io {
91         /** super class */
92         struct cl_io_slice     cui_cl;
93         struct cl_io_lock_link cui_link;
94         /**
95          * I/O vector information to or from which read/write is going.
96          */
97         struct iovec *cui_iov;
98         unsigned long cui_nrsegs;
99         /**
100          * Total iov count for left IO.
101          */
102         unsigned long cui_tot_nrsegs;
103         /**
104          * Old length for iov that was truncated partially.
105          */
106         size_t cui_iov_olen;
107         /**
108          * Total size for the left IO.
109          */
110         size_t cui_tot_count;
111
112         union {
113                 struct {
114                         enum ccc_setattr_lock_type cui_local_lock;
115                 } setattr;
116                 struct {
117                         struct cl_page_list cui_queue;
118                         unsigned long cui_written;
119                         int cui_from;
120                         int cui_to;
121                 } write;
122         } u;
123         /**
124          * Layout version when this IO is initialized
125          */
126         __u32                cui_layout_gen;
127         /**
128          * File descriptor against which IO is done.
129          */
130         struct ll_file_data *cui_fd;
131         struct kiocb *cui_iocb;
132 };
133
134 /**
135  * True, if \a io is a normal io, False for other splice_{read,write}.
136  * must be impementated in arch specific code.
137  */
138 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io);
139
140 extern struct lu_context_key ccc_key;
141 extern struct lu_context_key ccc_session_key;
142
143 struct ccc_thread_info {
144         struct cl_lock          cti_lock;
145         struct cl_lock_descr    cti_descr;
146         struct cl_io            cti_io;
147         struct cl_attr          cti_attr;
148 };
149
150 static inline struct ccc_thread_info *ccc_env_info(const struct lu_env *env)
151 {
152         struct ccc_thread_info      *info;
153
154         info = lu_context_key_get(&env->le_ctx, &ccc_key);
155         LASSERT(info != NULL);
156         return info;
157 }
158
159 static inline struct cl_lock *ccc_env_lock(const struct lu_env *env)
160 {
161         struct cl_lock *lock = &ccc_env_info(env)->cti_lock;
162         memset(lock, 0, sizeof *lock);
163         return lock;
164 }
165
166 static inline struct cl_attr *ccc_env_thread_attr(const struct lu_env *env)
167 {
168         struct cl_attr *attr = &ccc_env_info(env)->cti_attr;
169         memset(attr, 0, sizeof(*attr));
170         return attr;
171 }
172
173 static inline struct cl_io *ccc_env_thread_io(const struct lu_env *env)
174 {
175         struct cl_io *io = &ccc_env_info(env)->cti_io;
176         memset(io, 0, sizeof(*io));
177         return io;
178 }
179
180 struct ccc_session {
181         struct ccc_io cs_ios;
182 };
183
184 static inline struct ccc_session *ccc_env_session(const struct lu_env *env)
185 {
186         struct ccc_session *ses;
187
188         ses = lu_context_key_get(env->le_ses, &ccc_session_key);
189         LASSERT(ses != NULL);
190         return ses;
191 }
192
193 static inline struct ccc_io *ccc_env_io(const struct lu_env *env)
194 {
195         return &ccc_env_session(env)->cs_ios;
196 }
197
198 /**
199  * ccc-private object state.
200  */
201 struct ccc_object {
202         struct cl_object_header cob_header;
203         struct cl_object        cob_cl;
204         struct inode           *cob_inode;
205
206         /**
207          * A list of dirty pages pending IO in the cache. Used by
208          * SOM. Protected by ll_inode_info::lli_lock.
209          *
210          * \see ccc_page::cpg_pending_linkage
211          */
212         struct list_head        cob_pending_list;
213
214         /**
215          * Access this counter is protected by inode->i_sem. Now that
216          * the lifetime of transient pages must be covered by inode sem,
217          * we don't need to hold any lock..
218          */
219         int                     cob_transient_pages;
220         /**
221          * Number of outstanding mmaps on this file.
222          *
223          * \see ll_vm_open(), ll_vm_close().
224          */
225         atomic_t                cob_mmap_cnt;
226
227         /**
228          * various flags
229          * cob_discard_page_warned
230          *     if pages belonging to this object are discarded when a client
231          * is evicted, some debug info will be printed, this flag will be set
232          * during processing the first discarded page, then avoid flooding
233          * debug message for lots of discarded pages.
234          *
235          * \see ll_dirty_page_discard_warn.
236          */
237         unsigned int            cob_discard_page_warned:1;
238 };
239
240 /**
241  * ccc-private page state.
242  */
243 struct ccc_page {
244         struct cl_page_slice cpg_cl;
245         unsigned        cpg_defer_uptodate:1,
246                         cpg_ra_used:1,
247                         cpg_write_queued:1;
248         /**
249          * Non-empty iff this page is already counted in
250          * ccc_object::cob_pending_list. Protected by
251          * ccc_object::cob_pending_guard. This list is only used as a flag,
252          * that is, never iterated through, only checked for list_empty(), but
253          * having a list is useful for debugging.
254          */
255         struct list_head cpg_pending_linkage;
256         /** VM page */
257         struct page     *cpg_page;
258 };
259
260 static inline struct ccc_page *cl2ccc_page(const struct cl_page_slice *slice)
261 {
262         return container_of(slice, struct ccc_page, cpg_cl);
263 }
264
265 static inline pgoff_t ccc_index(struct ccc_page *ccc)
266 {
267         return ccc->cpg_cl.cpl_index;
268 }
269
270 struct cl_page    *ccc_vmpage_page_transient(struct page *vmpage);
271
272 struct ccc_device {
273         struct cl_device    cdv_cl;
274         struct super_block *cdv_sb;
275         struct cl_device   *cdv_next;
276 };
277
278 struct ccc_lock {
279         struct cl_lock_slice clk_cl;
280 };
281
282 struct ccc_req {
283         struct cl_req_slice  crq_cl;
284 };
285
286 void *ccc_key_init        (const struct lu_context *ctx,
287                            struct lu_context_key *key);
288 void  ccc_key_fini        (const struct lu_context *ctx,
289                            struct lu_context_key *key, void *data);
290 void *ccc_session_key_init(const struct lu_context *ctx,
291                            struct lu_context_key *key);
292 void  ccc_session_key_fini(const struct lu_context *ctx,
293                            struct lu_context_key *key, void *data);
294
295 int              ccc_device_init  (const struct lu_env *env,
296                                    struct lu_device *d,
297                                    const char *name, struct lu_device *next);
298 struct lu_device *ccc_device_fini (const struct lu_env *env,
299                                    struct lu_device *d);
300 struct lu_device *ccc_device_alloc(const struct lu_env *env,
301                                    struct lu_device_type *t,
302                                    struct lustre_cfg *cfg,
303                                    const struct lu_device_operations *luops,
304                                    const struct cl_device_operations *clops);
305 struct lu_device *ccc_device_free (const struct lu_env *env,
306                                    struct lu_device *d);
307 struct lu_object *ccc_object_alloc(const struct lu_env *env,
308                                    const struct lu_object_header *hdr,
309                                    struct lu_device *dev,
310                                    const struct cl_object_operations *clops,
311                                    const struct lu_object_operations *luops);
312
313 int ccc_req_init(const struct lu_env *env, struct cl_device *dev,
314                  struct cl_req *req);
315 void ccc_umount(const struct lu_env *env, struct cl_device *dev);
316 int ccc_global_init(struct lu_device_type *device_type);
317 void ccc_global_fini(struct lu_device_type *device_type);
318 int ccc_object_init0(const struct lu_env *env,struct ccc_object *vob,
319                      const struct cl_object_conf *conf);
320 int ccc_object_init(const struct lu_env *env, struct lu_object *obj,
321                     const struct lu_object_conf *conf);
322 void ccc_object_free(const struct lu_env *env, struct lu_object *obj);
323 int ccc_lock_init(const struct lu_env *env, struct cl_object *obj,
324                   struct cl_lock *lock, const struct cl_io *io,
325                   const struct cl_lock_operations *lkops);
326 int ccc_attr_set(const struct lu_env *env, struct cl_object *obj,
327                  const struct cl_attr *attr, unsigned valid);
328 int ccc_object_glimpse(const struct lu_env *env,
329                        const struct cl_object *obj, struct ost_lvb *lvb);
330 int ccc_conf_set(const struct lu_env *env, struct cl_object *obj,
331                  const struct cl_object_conf *conf);
332 int ccc_fail(const struct lu_env *env, const struct cl_page_slice *slice);
333 void ccc_transient_page_verify(const struct cl_page *page);
334 int  ccc_transient_page_own(const struct lu_env *env,
335                             const struct cl_page_slice *slice,
336                             struct cl_io *io, int nonblock);
337 void ccc_transient_page_assume(const struct lu_env *env,
338                                const struct cl_page_slice *slice,
339                                struct cl_io *io);
340 void ccc_transient_page_unassume(const struct lu_env *env,
341                                  const struct cl_page_slice *slice,
342                                  struct cl_io *io);
343 void ccc_transient_page_disown(const struct lu_env *env,
344                                const struct cl_page_slice *slice,
345                                struct cl_io *io);
346 void ccc_transient_page_discard(const struct lu_env *env,
347                                 const struct cl_page_slice *slice,
348                                 struct cl_io *io);
349 int ccc_transient_page_prep(const struct lu_env *env,
350                             const struct cl_page_slice *slice,
351                             struct cl_io *io);
352 void ccc_lock_delete(const struct lu_env *env,
353                      const struct cl_lock_slice *slice);
354 void ccc_lock_fini(const struct lu_env *env,struct cl_lock_slice *slice);
355 int ccc_lock_enqueue(const struct lu_env *env,const struct cl_lock_slice *slice,
356                      struct cl_io *io, struct cl_sync_io *anchor);
357 void ccc_io_fini(const struct lu_env *env, const struct cl_io_slice *ios);
358 int ccc_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
359                           __u32 enqflags, enum cl_lock_mode mode,
360                           pgoff_t start, pgoff_t end);
361 int ccc_io_one_lock(const struct lu_env *env, struct cl_io *io,
362                     __u32 enqflags, enum cl_lock_mode mode,
363                     loff_t start, loff_t end);
364 void ccc_io_end(const struct lu_env *env, const struct cl_io_slice *ios);
365 void ccc_io_advance(const struct lu_env *env, const struct cl_io_slice *ios,
366                     size_t nob);
367 void ccc_io_update_iov(const struct lu_env *env, struct ccc_io *cio,
368                        struct cl_io *io);
369 int ccc_prep_size(const struct lu_env *env, struct cl_object *obj,
370                   struct cl_io *io, loff_t start, size_t count, int *exceed);
371 void ccc_req_completion(const struct lu_env *env,
372                         const struct cl_req_slice *slice, int ioret);
373 void ccc_req_attr_set(const struct lu_env *env,const struct cl_req_slice *slice,
374                       const struct cl_object *obj,
375                       struct cl_req_attr *oa, obd_valid flags);
376
377 struct lu_device   *ccc2lu_dev      (struct ccc_device *vdv);
378 struct lu_object   *ccc2lu          (struct ccc_object *vob);
379 struct ccc_device  *lu2ccc_dev      (const struct lu_device *d);
380 struct ccc_device  *cl2ccc_dev      (const struct cl_device *d);
381 struct ccc_object  *lu2ccc          (const struct lu_object *obj);
382 struct ccc_object  *cl2ccc          (const struct cl_object *obj);
383 struct ccc_lock    *cl2ccc_lock     (const struct cl_lock_slice *slice);
384 struct ccc_io      *cl2ccc_io       (const struct lu_env *env,
385                                      const struct cl_io_slice *slice);
386 struct ccc_req     *cl2ccc_req      (const struct cl_req_slice *slice);
387 struct page         *cl2vm_page      (const struct cl_page_slice *slice);
388 struct inode       *ccc_object_inode(const struct cl_object *obj);
389 struct ccc_object  *cl_inode2ccc    (struct inode *inode);
390
391 int cl_setattr_ost(struct inode *inode, const struct iattr *attr,
392                    struct obd_capa *capa);
393
394 struct cl_page *ccc_vmpage_page_transient(struct page *vmpage);
395 int ccc_object_invariant(const struct cl_object *obj);
396 int cl_file_inode_init(struct inode *inode, struct lustre_md *md);
397 void cl_inode_fini(struct inode *inode);
398 int cl_local_size(struct inode *inode);
399
400 __u16 ll_dirent_type_get(struct lu_dirent *ent);
401 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32);
402 __u32 cl_fid_build_gen(const struct lu_fid *fid);
403
404 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
405 # define CLOBINVRNT(env, clob, expr)                                    \
406   do {                                                                  \
407           if (unlikely(!(expr))) {                                      \
408                   LU_OBJECT_DEBUG(D_ERROR, (env), &(clob)->co_lu, #expr "\n"); \
409                   LINVRNT(0);                                           \
410           }                                                             \
411   } while (0)
412 #else /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
413 # define CLOBINVRNT(env, clob, expr)                                    \
414          ((void)sizeof(env), (void)sizeof(clob), (void)sizeof !!(expr))
415 #endif /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
416
417 int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp);
418 int cl_ocd_update(struct obd_device *host,
419                   struct obd_device *watched,
420                   enum obd_notify_event ev, void *owner, void *data);
421
422 struct ccc_grouplock {
423         struct lu_env   *cg_env;
424         struct cl_io    *cg_io;
425         struct cl_lock  *cg_lock;
426         unsigned long    cg_gid;
427 };
428
429 int  cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
430                       struct ccc_grouplock *cg);
431 void cl_put_grouplock(struct ccc_grouplock *cg);
432
433 /**
434  * New interfaces to get and put lov_stripe_md from lov layer. This violates
435  * layering because lov_stripe_md is supposed to be a private data in lov.
436  *
437  * NB: If you find you have to use these interfaces for your new code, please
438  * think about it again. These interfaces may be removed in the future for
439  * better layering. */
440 struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj);
441 void lov_lsm_put(struct cl_object *clobj, struct lov_stripe_md *lsm);
442 int lov_read_and_clear_async_rc(struct cl_object *clob);
443
444 struct lov_stripe_md *ccc_inode_lsm_get(struct inode *inode);
445 void ccc_inode_lsm_put(struct inode *inode, struct lov_stripe_md *lsm);
446
447 /**
448  * Data structure managing a client's cached pages. A count of
449  * "unstable" pages is maintained, and an LRU of clean pages is
450  * maintained. "unstable" pages are pages pinned by the ptlrpc
451  * layer for recovery purposes.
452  */
453 struct cl_client_cache {
454         /**
455          * # of users (OSCs)
456          */
457         atomic_t                ccc_users;
458         /**
459          * # of threads are doing shrinking
460          */
461         unsigned int            ccc_lru_shrinkers;
462         /**
463          * # of LRU entries available
464          */
465         atomic_long_t           ccc_lru_left;
466         /**
467          * List of entities(OSCs) for this LRU cache
468          */
469         struct list_head        ccc_lru;
470         /**
471          * Max # of LRU entries
472          */
473         unsigned long           ccc_lru_max;
474         /**
475          * Lock to protect ccc_lru list
476          */
477         spinlock_t              ccc_lru_lock;
478         /**
479          * Set if unstable check is enabled
480          */
481         unsigned int            ccc_unstable_check:1;
482         /**
483          * # of unstable pages for this mount point
484          */
485         atomic_long_t           ccc_unstable_nr;
486         /**
487          * Waitq for awaiting unstable pages to reach zero.
488          * Used at umounting time and signaled on BRW commit
489          */
490         wait_queue_head_t       ccc_unstable_waitq;
491 };
492
493 enum {
494         LUSTRE_OPC_MKDIR    = 0,
495         LUSTRE_OPC_SYMLINK  = 1,
496         LUSTRE_OPC_MKNOD    = 2,
497         LUSTRE_OPC_CREATE   = 3,
498         LUSTRE_OPC_ANY      = 5
499 };
500
501 enum op_cli_flags {
502         CLI_SET_MEA     = 1 << 0,
503         CLI_RM_ENTRY    = 1 << 1,
504         CLI_HASH64      = 1 << 2,
505         CLI_API32       = 1 << 3,
506         CLI_MIGRATE     = 1 << 4,
507 };
508
509 #endif /*LCLIENT_H */