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