Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / ldlm / ldlm_internal.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2011, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  */
13
14 extern int ldlm_srv_namespace_nr;
15 extern int ldlm_cli_namespace_nr;
16 extern struct mutex ldlm_srv_namespace_lock;
17 extern struct list_head ldlm_srv_namespace_list;
18 extern struct mutex ldlm_cli_namespace_lock;
19 extern struct list_head ldlm_cli_active_namespace_list;
20 extern struct list_head ldlm_cli_inactive_namespace_list;
21 extern unsigned int ldlm_cancel_unused_locks_before_replay;
22 extern struct kmem_cache *ldlm_glimpse_work_kmem;
23
24 static inline int ldlm_namespace_nr_read(enum ldlm_side client)
25 {
26         return client == LDLM_NAMESPACE_SERVER ?
27                 ldlm_srv_namespace_nr : ldlm_cli_namespace_nr;
28 }
29
30 static inline void ldlm_namespace_nr_inc(enum ldlm_side client)
31 {
32         if (client == LDLM_NAMESPACE_SERVER)
33                 ldlm_srv_namespace_nr++;
34         else
35                 ldlm_cli_namespace_nr++;
36 }
37
38 static inline void ldlm_namespace_nr_dec(enum ldlm_side client)
39 {
40         if (client == LDLM_NAMESPACE_SERVER)
41                 ldlm_srv_namespace_nr--;
42         else
43                 ldlm_cli_namespace_nr--;
44 }
45
46 static inline struct list_head *ldlm_namespace_list(enum ldlm_side client)
47 {
48         return client == LDLM_NAMESPACE_SERVER ?
49                 &ldlm_srv_namespace_list : &ldlm_cli_active_namespace_list;
50 }
51
52 static inline
53 struct list_head *ldlm_namespace_inactive_list(enum ldlm_side client)
54 {
55         return client == LDLM_NAMESPACE_SERVER ?
56                 &ldlm_srv_namespace_list : &ldlm_cli_inactive_namespace_list;
57 }
58
59 static inline struct mutex *ldlm_namespace_lock(enum ldlm_side client)
60 {
61         return client == LDLM_NAMESPACE_SERVER ?
62                 &ldlm_srv_namespace_lock : &ldlm_cli_namespace_lock;
63 }
64
65 /* ns_bref is the number of resources in this namespace */
66 static inline int ldlm_ns_empty(struct ldlm_namespace *ns)
67 {
68         return atomic_read(&ns->ns_bref) == 0;
69 }
70
71 void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *l,
72                                           enum ldlm_side);
73 void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *l,
74                                             enum ldlm_side);
75 struct ldlm_namespace *ldlm_namespace_first_locked(enum ldlm_side);
76
77 /* ldlm_request.c */
78 int ldlm_cancel_lru(struct ldlm_namespace *ns, int min,
79                     enum ldlm_cancel_flags cancel_flags,
80                     enum ldlm_lru_flags lru_flags);
81 int ldlm_cancel_lru_local(struct ldlm_namespace *ns,
82                           struct list_head *cancels, int min, int max,
83                           enum ldlm_cancel_flags cancel_flags,
84                           enum ldlm_lru_flags lru_flags);
85 extern unsigned int ldlm_enqueue_min;
86 /* ldlm_resource.c */
87 extern struct kmem_cache *ldlm_resource_slab;
88 extern struct kmem_cache *ldlm_lock_slab;
89 extern struct kmem_cache *ldlm_inodebits_slab;
90 extern struct kmem_cache *ldlm_interval_tree_slab;
91
92 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
93                                      struct ldlm_lock *new);
94 void ldlm_resource_insert_lock_before(struct ldlm_lock *original,
95                                       struct ldlm_lock *new);
96
97 /* ldlm_lock.c */
98
99 typedef enum {
100         LDLM_WORK_BL_AST,
101         LDLM_WORK_CP_AST,
102         LDLM_WORK_REVOKE_AST,
103         LDLM_WORK_GL_AST
104 } ldlm_desc_ast_t;
105
106 void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock);
107 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list);
108 int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
109                   enum req_location loc, void *data, int size);
110 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
111                                    const struct ldlm_res_id *res,
112                                    enum ldlm_type type, enum ldlm_mode mode,
113                                    const struct ldlm_callback_suite *cbs,
114                                    void *data, __u32 lvb_len,
115                                    enum lvb_type lvb_type);
116 enum ldlm_error ldlm_lock_enqueue(const struct lu_env *env,
117                                   struct ldlm_namespace *l,
118                                   struct ldlm_lock **lock,
119                                   void *cookie, __u64 *flags);
120 void ldlm_lock_addref_internal(struct ldlm_lock *l, enum ldlm_mode mode);
121 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *l, enum ldlm_mode mode);
122 void ldlm_lock_decref_internal(struct ldlm_lock *l, enum ldlm_mode mode);
123 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *l,
124                                       enum ldlm_mode mode);
125 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
126                             struct list_head *work_list);
127 #ifdef HAVE_SERVER_SUPPORT
128 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
129                          struct list_head *work_list,
130                          enum ldlm_process_intention intention,
131                          enum mds_ibits_locks hint);
132 int ldlm_handle_conflict_lock(struct ldlm_lock *lock, __u64 *flags,
133                               struct list_head *rpc_list);
134 void ldlm_discard_bl_list(struct list_head *bl_list);
135 void ldlm_clear_blocking_lock(struct ldlm_lock *lock);
136 void ldlm_clear_blocking_data(struct ldlm_lock *lock);
137 #endif
138 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
139                       ldlm_desc_ast_t ast_type);
140 int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq);
141 int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, ktime_t last_use);
142 #define ldlm_lock_remove_from_lru(lock) \
143                 ldlm_lock_remove_from_lru_check(lock, ktime_set(0, 0))
144 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock);
145 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock);
146 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock);
147 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock);
148
149 int ldlm_export_cancel_blocked_locks(struct obd_export *exp);
150 int ldlm_export_cancel_locks(struct obd_export *exp);
151 void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock);
152
153 /* ldlm_lockd.c */
154 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
155                            struct ldlm_lock *lock);
156 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns,
157                            struct ldlm_lock_desc *ld,
158                            struct list_head *cancels, int count,
159                            enum ldlm_cancel_flags cancel_flags);
160 int ldlm_bl_to_thread_ns(struct ldlm_namespace *ns);
161 int ldlm_bl_thread_wakeup(void);
162
163 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
164                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock);
165 void ldlm_bl_desc2lock(const struct ldlm_lock_desc *ld, struct ldlm_lock *lock);
166
167 #ifdef HAVE_SERVER_SUPPORT
168 /* ldlm_plain.c */
169 int ldlm_process_plain_lock(struct ldlm_lock *lock, __u64 *flags,
170                             enum ldlm_process_intention intention,
171                             enum ldlm_error *err, struct list_head *work_list);
172
173 /* ldlm_inodebits.c */
174 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
175                                 enum ldlm_process_intention intention,
176                                 enum ldlm_error *err,
177                                 struct list_head *work_list);
178 int ldlm_reprocess_inodebits_queue(struct ldlm_resource *res,
179                                    struct list_head *queue,
180                                    struct list_head *work_list,
181                                    enum ldlm_process_intention intention,
182                                    enum mds_ibits_locks hint);
183 /* ldlm_extent.c */
184 int ldlm_process_extent_lock(struct ldlm_lock *lock, __u64 *flags,
185                              enum ldlm_process_intention intention,
186                              enum ldlm_error *err, struct list_head *work_list);
187 #endif
188 void ldlm_extent_add_lock(struct ldlm_resource *res, struct ldlm_lock *lock);
189 void ldlm_extent_unlink_lock(struct ldlm_lock *lock);
190 void ldlm_extent_search(struct interval_tree_root *root,
191                         u64 start, u64 end,
192                         bool (*matches)(struct ldlm_lock *lock, void *data),
193                         void *data);
194
195 int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock);
196 void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
197                              struct ldlm_lock *lock, bool tail);
198 void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock);
199
200 /* ldlm_flock.c */
201 int ldlm_process_flock_lock(struct ldlm_lock *req, __u64 *flags,
202                             enum ldlm_process_intention intention,
203                             enum ldlm_error *err, struct list_head *work_list);
204 int ldlm_init_flock_export(struct obd_export *exp);
205 void ldlm_destroy_flock_export(struct obd_export *exp);
206 void ldlm_flock_add_lock(struct ldlm_resource *req, struct list_head *head,
207                          struct ldlm_lock *lock);
208 void ldlm_flock_unlink_lock(struct ldlm_lock *lock);
209
210 /* l_lock.c */
211 void l_check_ns_lock(struct ldlm_namespace *ns);
212 void l_check_no_ns_lock(struct ldlm_namespace *ns);
213
214 extern struct dentry *ldlm_svc_debugfs_dir;
215
216 struct ldlm_state {
217         struct ptlrpc_service *ldlm_cb_service;
218         struct ptlrpc_service *ldlm_cancel_service;
219         struct ptlrpc_client *ldlm_client;
220         struct ldlm_bl_pool *ldlm_bl_pool;
221 };
222
223 int ldlm_init(void);
224 void ldlm_exit(void);
225
226 enum ldlm_policy_res {
227         LDLM_POLICY_CANCEL_LOCK,
228         LDLM_POLICY_KEEP_LOCK,
229         LDLM_POLICY_SKIP_LOCK
230 };
231
232 #define LDLM_POOL_SYSFS_PRINT_int(v) sprintf(buf, "%d\n", v)
233 #define LDLM_POOL_SYSFS_SET_int(a, b) { a = b; }
234 #define LDLM_POOL_SYSFS_PRINT_u64(v) sprintf(buf, "%lld\n", v)
235 #define LDLM_POOL_SYSFS_SET_u64(a, b) { a = b; }
236 #define LDLM_POOL_SYSFS_PRINT_atomic(v) sprintf(buf, "%d\n", atomic_read(&v))
237 #define LDLM_POOL_SYSFS_SET_atomic(a, b) atomic_set(&a, b)
238
239 #define LDLM_POOL_SYSFS_READER_SHOW(var, type)                             \
240         static ssize_t var##_show(struct kobject *kobj,                    \
241                                   struct attribute *attr,                  \
242                                   char *buf)                               \
243         {                                                                  \
244                 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,\
245                                                     pl_kobj);              \
246                 type tmp;                                                  \
247                                                                            \
248                 spin_lock(&pl->pl_lock);                                   \
249                 tmp = pl->pl_##var;                                        \
250                 spin_unlock(&pl->pl_lock);                                 \
251                                                                            \
252                 return LDLM_POOL_SYSFS_PRINT_##type(tmp);                  \
253         }                                                                  \
254         struct __##var##__dummy_read { ; } /* semicolon catcher */
255
256 #define LDLM_POOL_SYSFS_WRITER_STORE(var, type)                            \
257         static ssize_t var##_store(struct kobject *kobj,                   \
258                                    struct attribute *attr,                 \
259                                    const char *buffer,                     \
260                                    size_t count)                           \
261         {                                                                  \
262                 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,\
263                                                     pl_kobj);              \
264                 unsigned long tmp;                                         \
265                 int rc;                                                    \
266                                                                            \
267                 rc = kstrtoul(buffer, 10, &tmp);                           \
268                 if (rc < 0) {                                              \
269                         return rc;                                         \
270                 }                                                          \
271                                                                            \
272                 spin_lock(&pl->pl_lock);                                   \
273                 LDLM_POOL_SYSFS_SET_##type(pl->pl_##var, tmp);             \
274                 spin_unlock(&pl->pl_lock);                                 \
275                                                                            \
276                 return count;                                              \
277         }                                                                  \
278         struct __##var##__dummy_write {; } /* semicolon catcher */
279
280 #define LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(var, type)                      \
281         static ssize_t var##_show(struct kobject *kobj,                    \
282                                   struct attribute *attr,                  \
283                                   char *buf)                               \
284         {                                                                  \
285                 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,\
286                                                     pl_kobj);              \
287                                                                            \
288                 return LDLM_POOL_SYSFS_PRINT_##type(pl->pl_##var);         \
289         }                                                                  \
290         struct __##var##__dummy_read {; } /* semicolon catcher */
291
292 #define LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(var, type)                     \
293         static ssize_t var##_store(struct kobject *kobj,                   \
294                                    struct attribute *attr,                 \
295                                    const char *buffer,                     \
296                                    size_t count)                           \
297         {                                                                  \
298                 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,\
299                                                     pl_kobj);              \
300                 unsigned long tmp;                                         \
301                 int rc;                                                    \
302                                                                            \
303                 rc = kstrtoul(buffer, 10, &tmp);                           \
304                 if (rc < 0) {                                              \
305                         return rc;                                         \
306                 }                                                          \
307                                                                            \
308                 LDLM_POOL_SYSFS_SET_##type(pl->pl_##var, tmp);             \
309                                                                            \
310                 return count;                                              \
311         }                                                                  \
312         struct __##var##__dummy_write {; } /* semicolon catcher */
313
314 static inline void
315 ldlm_add_var(struct ldebugfs_vars *vars, struct dentry *debugfs_entry,
316              const char *name, void *data, const struct file_operations *ops)
317 {
318         vars->name = name;
319         vars->data = data;
320         vars->fops = ops;
321         ldebugfs_add_vars(debugfs_entry, vars, NULL);
322 }
323
324 static inline int is_granted_or_cancelled(struct ldlm_lock *lock)
325 {
326         int ret = 0;
327
328         lock_res_and_lock(lock);
329         ret = is_granted_or_cancelled_nolock(lock);
330         unlock_res_and_lock(lock);
331
332         return ret;
333 }
334
335 static inline bool is_bl_done(struct ldlm_lock *lock)
336 {
337         bool bl_done = true;
338
339         if (!ldlm_is_bl_done(lock)) {
340                 lock_res_and_lock(lock);
341                 bl_done = ldlm_is_bl_done(lock);
342                 unlock_res_and_lock(lock);
343         }
344
345         return bl_done;
346 }
347
348 static inline bool is_lock_converted(struct ldlm_lock *lock)
349 {
350         bool ret = 0;
351
352         lock_res_and_lock(lock);
353         ret = (lock->l_policy_data.l_inodebits.cancel_bits ==
354                MDS_INODELOCK_NONE);
355         unlock_res_and_lock(lock);
356
357         return ret;
358 }
359
360 typedef void (*ldlm_policy_wire_to_local_t)(const union ldlm_wire_policy_data *,
361                                             union ldlm_policy_data *);
362 typedef void (*ldlm_policy_local_to_wire_t)(const union ldlm_policy_data *,
363                                             union ldlm_wire_policy_data *);
364 void ldlm_plain_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
365                                      union ldlm_policy_data *lpolicy);
366 void ldlm_plain_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
367                                      union ldlm_wire_policy_data *wpolicy);
368 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
369                                      union ldlm_policy_data *lpolicy);
370 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
371                                      union ldlm_wire_policy_data *wpolicy);
372 void ldlm_extent_policy_wire_to_local(const union ldlm_wire_policy_data *wpol,
373                                       union ldlm_policy_data *lpolicy);
374 void ldlm_extent_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
375                                       union ldlm_wire_policy_data *wpolicy);
376 void ldlm_flock_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
377                                      union ldlm_policy_data *lpolicy);
378 void ldlm_flock_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
379                                      union ldlm_wire_policy_data *wpolicy);
380
381 /* ldlm_reclaim.c */
382 #ifdef HAVE_SERVER_SUPPORT
383 extern u64 ldlm_reclaim_threshold;
384 extern u64 ldlm_lock_limit;
385 extern u64 ldlm_reclaim_threshold_mb;
386 extern u64 ldlm_lock_limit_mb;
387 extern struct percpu_counter ldlm_granted_total;
388 #endif
389 extern unsigned int ldlm_dump_granted_max;
390 int ldlm_reclaim_setup(void);
391 void ldlm_reclaim_cleanup(void);
392 void ldlm_reclaim_add(struct ldlm_lock *lock);
393 void ldlm_reclaim_del(struct ldlm_lock *lock);
394 bool ldlm_reclaim_full(void);
395
396 static inline bool ldlm_res_eq(const struct ldlm_res_id *res0,
397                                const struct ldlm_res_id *res1)
398 {
399         return memcmp(res0, res1, sizeof(*res0)) == 0;
400 }
401
402 /* exports for testing */
403 struct ldlm_lock *ldlm_lock_new_testing(struct ldlm_resource *resource);