Whamcloud - gitweb
LU-3963 libcfs: convert md[d/t]/mg[c/s] to linux atomic primitives
[fs/lustre-release.git] / lustre / mgs / mgs_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) 2007, 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
37 #ifndef _MGS_INTERNAL_H
38 #define _MGS_INTERNAL_H
39
40 #include <libcfs/libcfs.h>
41 #include <lustre_log.h>
42 #include <lustre_export.h>
43 #include <lustre_nodemap.h>
44 #include <dt_object.h>
45
46 #define MGSSELF_NAME    "_mgs"
47
48 #define MGS_SERVICE_WATCHDOG_FACTOR 2
49
50 /* -- imperative recovery control data structures -- */
51 /**
52  * restarting targets.
53  */
54 struct mgs_nidtbl;
55 struct mgs_nidtbl_target {
56         cfs_list_t              mnt_list;
57         struct mgs_nidtbl      *mnt_fs;
58         u64                     mnt_version;
59         int                     mnt_type; /* OST or MDT */
60         cfs_time_t              mnt_last_active;
61         struct mgs_target_info  mnt_mti;
62 };
63
64 enum {
65         IR_FULL = 0,
66         IR_STARTUP,
67         IR_DISABLED,
68         IR_PARTIAL
69 };
70
71 #define IR_STRINGS { "full", "startup", "disabled", "partial" }
72
73 /**
74  */
75 struct fs_db;
76
77 /**
78  * maintain fs client nodes of mgs.
79  */
80 struct mgs_fsc {
81         struct fs_db      *mfc_fsdb;
82         /**
83          * Where the fs client comes from.
84          */
85         struct obd_export *mfc_export;
86         /**
87          * list of fs clients from the same export,
88          * protected by mgs_export_data->med_lock
89          */
90         cfs_list_t         mfc_export_list;
91         /**
92          * list of fs clients in the same fsdb, protected by fsdb->fsdb_mutex
93          */
94         cfs_list_t        mfc_fsdb_list;
95         unsigned          mfc_ir_capable:1;
96 };
97
98 struct mgs_nidtbl {
99         struct fs_db *mn_fsdb;
100         struct file  *mn_version_file;
101         struct mutex    mn_lock;
102         u64           mn_version;
103         int           mn_nr_targets;
104         cfs_list_t    mn_targets;
105 };
106
107 struct mgs_tgt_srpc_conf {
108         struct mgs_tgt_srpc_conf  *mtsc_next;
109         char                      *mtsc_tgt;
110         struct sptlrpc_rule_set    mtsc_rset;
111 };
112
113 #define INDEX_MAP_SIZE  8192     /* covers indicies to FFFF */
114
115 #define FSDB_LOG_EMPTY          (0)  /* missing client log */
116 #define FSDB_OLDLOG14           (1)  /* log starts in old (1.4) style */
117 #define FSDB_REVOKING_LOCK      (2)  /* DLM lock is being revoked */
118 #define FSDB_MGS_SELF           (3)  /* for '_mgs', used by sptlrpc */
119 #define FSDB_OSCNAME18          (4)  /* old 1.8 style OSC naming */
120 #define FSDB_UDESC              (5)  /* sptlrpc user desc, will be obsolete */
121 #define FSDB_REVOKING_PARAMS    (6)  /* DLM lock is being revoked */
122
123 struct fs_db {
124         char              fsdb_name[9];
125         cfs_list_t        fsdb_list;           /* list of databases */
126         struct mutex      fsdb_mutex;
127         void             *fsdb_ost_index_map;  /* bitmap of used indicies */
128         void             *fsdb_mdt_index_map;  /* bitmap of used indicies */
129         int               fsdb_mdt_count;
130         char             *fsdb_clilov;       /* COMPAT_146 client lov name */
131         char             *fsdb_clilmv;
132         unsigned long     fsdb_flags;
133         __u32             fsdb_gen;
134
135         /* in-memory copy of the srpc rules, guarded by fsdb_lock */
136         struct sptlrpc_rule_set   fsdb_srpc_gen;
137         struct mgs_tgt_srpc_conf *fsdb_srpc_tgt;
138
139         /* list of fs clients, mgs_fsc. protected by mgs_mutex */
140         cfs_list_t           fsdb_clients;
141         int                  fsdb_nonir_clients;
142         int                  fsdb_ir_state;
143
144         /* Target NIDs Table */
145         struct mgs_nidtbl    fsdb_nidtbl;
146
147         /* async thread to notify clients */
148         struct mgs_device    *fsdb_mgs;
149         wait_queue_head_t    fsdb_notify_waitq;
150         struct completion    fsdb_notify_comp;
151         cfs_time_t           fsdb_notify_start;
152         atomic_t         fsdb_notify_phase;
153         volatile unsigned int fsdb_notify_async:1,
154                              fsdb_notify_stop:1;
155         /* statistic data */
156         unsigned int         fsdb_notify_total;
157         unsigned int         fsdb_notify_max;
158         unsigned int         fsdb_notify_count;
159 };
160
161 struct mgs_device {
162         struct dt_device                 mgs_dt_dev;
163         struct ptlrpc_service           *mgs_service;
164         struct dt_device                *mgs_bottom;
165         struct obd_export               *mgs_bottom_exp;
166         struct dt_object                *mgs_configs_dir;
167         struct dt_object                *mgs_nidtbl_dir;
168         cfs_list_t                       mgs_fs_db_list;
169         spinlock_t                       mgs_lock; /* covers mgs_fs_db_list */
170         cfs_proc_dir_entry_t            *mgs_proc_live;
171         cfs_proc_dir_entry_t            *mgs_proc_osd;
172         cfs_proc_dir_entry_t            *mgs_proc_fstype;
173         cfs_proc_dir_entry_t            *mgs_proc_mntdev;
174         cfs_time_t                       mgs_start_time;
175         struct obd_device               *mgs_obd;
176         struct local_oid_storage        *mgs_los;
177         struct mutex                     mgs_mutex;
178         struct lu_target                 mgs_lut;
179 };
180
181 /* this is a top object */
182 struct mgs_object {
183         struct lu_object_header mgo_header;
184         struct dt_object        mgo_obj;
185         int                     mgo_no_attrs;
186         int                     mgo_reserved;
187 };
188
189 int mgs_init_fsdb_list(struct mgs_device *mgs);
190 int mgs_cleanup_fsdb_list(struct mgs_device *mgs);
191 int mgs_find_or_make_fsdb(const struct lu_env *env, struct mgs_device *mgs, char *name,
192                           struct fs_db **dbh);
193 struct fs_db *mgs_find_fsdb(struct mgs_device *mgs, char *fsname);
194 int mgs_get_fsdb_srpc_from_llog(const struct lu_env *env, struct mgs_device *mgs, struct fs_db *fsdb);
195 int mgs_check_index(const struct lu_env *env, struct mgs_device *mgs, struct mgs_target_info *mti);
196 int mgs_check_failnid(const struct lu_env *env, struct mgs_device *mgs, struct mgs_target_info *mti);
197 int mgs_write_log_target(const struct lu_env *env, struct mgs_device *mgs, struct mgs_target_info *mti,
198                          struct fs_db *fsdb);
199 int mgs_replace_nids(const struct lu_env *env, struct mgs_device *mgs, char *devname, char *nids);
200 int mgs_erase_log(const struct lu_env *env, struct mgs_device *mgs,
201                   char *name);
202 int mgs_erase_logs(const struct lu_env *env, struct mgs_device *mgs,
203                    char *fsname);
204 int mgs_setparam(const struct lu_env *env, struct mgs_device *mgs,
205                  struct lustre_cfg *lcfg, char *fsname);
206 int mgs_list_logs(const struct lu_env *env, struct mgs_device *mgs,
207                   struct obd_ioctl_data *data);
208 int mgs_pool_cmd(const struct lu_env *env, struct mgs_device *mgs,
209                  enum lcfg_command_type cmd, char *poolname, char *fsname,
210                  char *ostname);
211 int mgs_nodemap_cmd(const struct lu_env *env, struct mgs_device *mgs,
212                     enum lcfg_command_type cmd, const char *nodemap_name,
213                     const char *param);
214
215 /* mgs_handler.c */
216 int  mgs_get_lock(struct obd_device *obd, struct ldlm_res_id *res,
217                   struct lustre_handle *lockh);
218 int  mgs_put_lock(struct lustre_handle *lockh);
219 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type);
220
221 /* mgs_nids.c */
222 int  mgs_ir_update(const struct lu_env *env, struct mgs_device *mgs,
223                    struct mgs_target_info *mti);
224 int mgs_ir_init_fs(const struct lu_env *env, struct mgs_device *mgs,
225                    struct fs_db *fsdb);
226 void mgs_ir_fini_fs(struct mgs_device *mgs, struct fs_db *fsdb);
227 void mgs_ir_notify_complete(struct fs_db *fsdb);
228 int  mgs_get_ir_logs(struct ptlrpc_request *req);
229 int  lprocfs_wr_ir_state(struct file *file, const char *buffer,
230                            unsigned long count, void *data);
231 int  lprocfs_rd_ir_state(struct seq_file *seq, void *data);
232 int  lprocfs_wr_ir_timeout(struct file *file, const char *buffer,
233                            unsigned long count, void *data);
234 int  lprocfs_rd_ir_timeout(char *page, char **start, off_t off, int count,
235                            int *eof, void *data);
236 void mgs_fsc_cleanup(struct obd_export *exp);
237 void mgs_fsc_cleanup_by_fsdb(struct fs_db *fsdb);
238 int  mgs_fsc_attach(const struct lu_env *env, struct obd_export *exp,
239                     char *fsname);
240
241 /* mgs_fs.c */
242 int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
243                           void *localdata);
244 int mgs_client_free(struct obd_export *exp);
245 int mgs_fs_setup(const struct lu_env *env, struct mgs_device *m);
246 int mgs_fs_cleanup(const struct lu_env *env, struct mgs_device *m);
247
248 #ifdef LPROCFS
249 int lproc_mgs_setup(struct mgs_device *mgs, const char *osd_name);
250 void lproc_mgs_cleanup(struct mgs_device *mgs);
251 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb);
252 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb);
253 void lprocfs_mgs_init_vars(struct lprocfs_static_vars *lvars);
254 #else
255 static inline int lproc_mgs_setup(struct mgs_device *mgs, const char *osd_name)
256 {return 0;}
257 static inline void lproc_mgs_cleanup(struct mgs_device *mgs)
258 {}
259 static inline int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
260 {return 0;}
261 static inline int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
262 {return 0;}
263 static void lprocfs_mgs_init_vars(struct lprocfs_static_vars *lvars)
264 {
265         memset(lvars, 0, sizeof(*lvars));
266 }
267 #endif
268
269 /* mgs/lproc_mgs.c */
270 enum {
271         LPROC_MGS_CONNECT = 0,
272         LPROC_MGS_DISCONNECT,
273         LPROC_MGS_EXCEPTION,
274         LPROC_MGS_TARGET_REG,
275         LPROC_MGS_TARGET_DEL,
276         LPROC_MGS_LAST
277 };
278 void mgs_counter_incr(struct obd_export *exp, int opcode);
279 void mgs_stats_counter_init(struct lprocfs_stats *stats);
280
281 struct temp_comp {
282         struct mgs_target_info  *comp_tmti;
283         struct mgs_target_info  *comp_mti;
284         struct fs_db            *comp_fsdb;
285         struct obd_device       *comp_obd;
286 };
287
288 struct mgs_thread_info {
289         struct lustre_cfg_bufs  mgi_bufs;
290         char                    mgi_fsname[MTI_NAME_MAXLEN];
291         struct cfg_marker       mgi_marker;
292         struct temp_comp        mgi_comp;
293 };
294
295 extern struct lu_context_key mgs_thread_key;
296
297 static inline struct mgs_thread_info *mgs_env_info(const struct lu_env *env)
298 {
299         struct mgs_thread_info  *info;
300         int                     rc;
301
302         info = lu_context_key_get(&env->le_ctx, &mgs_thread_key);
303         if (info == NULL) {
304                 rc = lu_env_refill((struct lu_env *)env);
305                 if (rc != 0)
306                         return ERR_PTR(rc);
307                 info = lu_context_key_get(&env->le_ctx, &mgs_thread_key);
308         }
309         LASSERT(info != NULL);
310         return info;
311 }
312
313 extern const struct lu_device_operations mgs_lu_ops;
314
315 static inline int lu_device_is_mgs(struct lu_device *d)
316 {
317         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mgs_lu_ops);
318 }
319
320 static inline struct mgs_device* lu2mgs_dev(struct lu_device *d)
321 {
322         LASSERT(lu_device_is_mgs(d));
323         return container_of0(d, struct mgs_device, mgs_dt_dev.dd_lu_dev);
324 }
325
326 static inline struct mgs_device *exp2mgs_dev(struct obd_export *exp)
327 {
328         return lu2mgs_dev(exp->exp_obd->obd_lu_dev);
329 }
330
331 static inline struct lu_device *mgs2lu_dev(struct mgs_device *d)
332 {
333         return (&d->mgs_dt_dev.dd_lu_dev);
334 }
335
336 static inline struct mgs_device *dt2mgs_dev(struct dt_device *d)
337 {
338         LASSERT(lu_device_is_mgs(&d->dd_lu_dev));
339         return container_of0(d, struct mgs_device, mgs_dt_dev);
340 }
341
342 static inline struct mgs_object *lu2mgs_obj(struct lu_object *o)
343 {
344         LASSERT(ergo(o != NULL, lu_device_is_mgs(o->lo_dev)));
345         return container_of0(o, struct mgs_object, mgo_obj.do_lu);
346 }
347
348 static inline struct lu_object *mgs2lu_obj(struct mgs_object *obj)
349 {
350         return &obj->mgo_obj.do_lu;
351 }
352
353 static inline struct mgs_object *mgs_obj(const struct lu_object *o)
354 {
355         LASSERT(lu_device_is_mgs(o->lo_dev));
356         return container_of0(o, struct mgs_object, mgo_obj.do_lu);
357 }
358
359 static inline struct mgs_object *dt2mgs_obj(const struct dt_object *d)
360 {
361         return mgs_obj(&d->do_lu);
362 }
363
364 static inline struct dt_object* mgs_object_child(struct mgs_object *o)
365 {
366         return container_of0(lu_object_next(mgs2lu_obj(o)),
367                              struct dt_object, do_lu);
368 }
369
370 static inline struct dt_object *dt_object_child(struct dt_object *o)
371 {
372         return container_of0(lu_object_next(&(o)->do_lu),
373                              struct dt_object, do_lu);
374 }
375 struct mgs_direntry {
376         cfs_list_t  list;
377         char       *name;
378         int         len;
379 };
380
381 static inline void mgs_direntry_free(struct mgs_direntry *de)
382 {
383         if (de) {
384                 LASSERT(de->len);
385                 OBD_FREE(de->name, de->len);
386                 OBD_FREE_PTR(de);
387         }
388 }
389
390 static inline struct mgs_direntry *mgs_direntry_alloc(int len)
391 {
392         struct mgs_direntry *de;
393
394         OBD_ALLOC_PTR(de);
395         if (de == NULL)
396                 return NULL;
397
398         OBD_ALLOC(de->name, len);
399         if (de->name == NULL) {
400                 OBD_FREE_PTR(de);
401                 return NULL;
402         }
403
404         de->len = len;
405
406         return de;
407 }
408
409 /* mgs_llog.c */
410 int class_dentry_readdir(const struct lu_env *env, struct mgs_device *mgs,
411                          cfs_list_t *list);
412
413 #endif /* _MGS_INTERNAL_H */