Whamcloud - gitweb
2aca2d2efa2d68f70f8104b4a1c13dcd156e70db
[fs/lustre-release.git] / lustre / ofd / ofd_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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 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 #ifndef _OFD_INTERNAL_H
38 #define _OFD_INTERNAL_H
39
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <dt_object.h>
43 #include <lustre_fid.h>
44 #include <obd_ost.h>
45 #include <lustre_capa.h>
46
47 #define OFD_INIT_OBJID  0
48 #define OFD_ROCOMPAT_SUPP (0)
49 #define OFD_INCOMPAT_SUPP (OBD_INCOMPAT_GROUPS | OBD_INCOMPAT_OST | \
50                            OBD_INCOMPAT_COMMON_LR)
51 #define OFD_MAX_GROUPS  256
52
53 /* Limit the returned fields marked valid to those that we actually might set */
54 #define OFD_VALID_FLAGS (LA_TYPE | LA_MODE | LA_SIZE | LA_BLOCKS | \
55                          LA_BLKSIZE | LA_ATIME | LA_MTIME | LA_CTIME)
56
57 /* per-client-per-object persistent state (LRU) */
58 struct ofd_mod_data {
59         cfs_list_t      fmd_list;        /* linked to fed_mod_list */
60         struct lu_fid   fmd_fid;         /* FID being written to */
61         __u64           fmd_mactime_xid; /* xid highest {m,a,c}time setattr */
62         cfs_time_t      fmd_expire;      /* time when the fmd should expire */
63         int             fmd_refcount;    /* reference counter - list holds 1 */
64 };
65
66 #define OFD_FMD_MAX_NUM_DEFAULT 128
67 #define OFD_FMD_MAX_AGE_DEFAULT ((obd_timeout + 10) * CFS_HZ)
68
69 enum {
70         LPROC_OFD_READ_BYTES = 0,
71         LPROC_OFD_WRITE_BYTES = 1,
72         LPROC_OFD_LAST,
73 };
74
75 struct ofd_device {
76         struct dt_device         ofd_dt_dev;
77         struct dt_device        *ofd_osd;
78         struct obd_export       *ofd_osd_exp;
79         struct dt_device_param   ofd_dt_conf;
80         /* DLM name-space for meta-data locks maintained by this server */
81         struct ldlm_namespace   *ofd_namespace;
82
83         /* transaction callbacks */
84         struct dt_txn_callback   ofd_txn_cb;
85
86         /* last_rcvd file */
87         struct lu_target         ofd_lut;
88         struct dt_object        *ofd_last_group_file;
89         struct dt_object        *ofd_health_check_file;
90
91         int                      ofd_subdir_count;
92
93         int                      ofd_max_group;
94         obd_id                   ofd_last_objids[OFD_MAX_GROUPS];
95         cfs_mutex_t              ofd_create_locks[OFD_MAX_GROUPS];
96         struct dt_object        *ofd_lastid_obj[OFD_MAX_GROUPS];
97         cfs_spinlock_t           ofd_objid_lock;
98         unsigned long            ofd_destroys_in_progress;
99
100         /* protect all statfs-related counters */
101         cfs_spinlock_t           ofd_osfs_lock;
102         /* statfs optimization: we cache a bit  */
103         struct obd_statfs        ofd_osfs;
104         __u64                    ofd_osfs_age;
105         int                      ofd_blockbits;
106         /* writes which might be be accounted twice in ofd_osfs.os_bavail */
107         obd_size                 ofd_osfs_unstable;
108
109         /* counters used during statfs update, protected by ofd_osfs_lock.
110          * record when some statfs refresh are in progress */
111         int                      ofd_statfs_inflight;
112         /* track writes completed while statfs refresh is underway.
113          * tracking is only effective when ofd_statfs_inflight > 1 */
114         obd_size                 ofd_osfs_inflight;
115
116         /* grants: all values in bytes */
117         /* grant lock to protect all grant counters */
118         cfs_spinlock_t           ofd_grant_lock;
119         /* total amount of dirty data reported by clients in incoming obdo */
120         obd_size                 ofd_tot_dirty;
121         /* sum of filesystem space granted to clients for async writes */
122         obd_size                 ofd_tot_granted;
123         /* grant used by I/Os in progress (between prepare and commit) */
124         obd_size                 ofd_tot_pending;
125         /* free space threshold over which we stop granting space to clients
126          * ofd_grant_ratio is stored as a fixed-point fraction using
127          * OFD_GRANT_RATIO_SHIFT of the remaining free space, not in percentage
128          * values */
129         int                      ofd_grant_ratio;
130         /* number of clients using grants */
131         int                      ofd_tot_granted_clients;
132
133         /* ofd mod data: ofd_device wide values */
134         int                      ofd_fmd_max_num; /* per ofd ofd_mod_data */
135         cfs_duration_t           ofd_fmd_max_age; /* time to fmd expiry */
136
137         cfs_spinlock_t           ofd_flags_lock;
138         unsigned long            ofd_raid_degraded:1,
139                                  /* sync journal on writes */
140                                  ofd_syncjournal:1,
141                                  /* sync on lock cancel */
142                                  ofd_sync_lock_cancel:2,
143                                  /* shall we grant space to clients not
144                                   * supporting OBD_CONNECT_GRANT_PARAM? */
145                                  ofd_grant_compat_disable:1;
146 };
147
148 static inline struct ofd_device *ofd_dev(struct lu_device *d)
149 {
150         return container_of0(d, struct ofd_device, ofd_dt_dev.dd_lu_dev);
151 }
152
153 static inline struct obd_device *ofd_obd(struct ofd_device *ofd)
154 {
155         return ofd->ofd_dt_dev.dd_lu_dev.ld_obd;
156 }
157
158 static inline struct ofd_device *ofd_exp(struct obd_export *exp)
159 {
160         return ofd_dev(exp->exp_obd->obd_lu_dev);
161 }
162
163 static inline char *ofd_name(struct ofd_device *ofd)
164 {
165         return ofd->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name;
166 }
167
168 struct ofd_object {
169         struct lu_object_header ofo_header;
170         struct dt_object        ofo_obj;
171         int                     ofo_ff_exists;
172 };
173
174 static inline struct ofd_object *ofd_obj(struct lu_object *o)
175 {
176         return container_of0(o, struct ofd_object, ofo_obj.do_lu);
177 }
178
179 static inline int ofd_object_exists(struct ofd_object *obj)
180 {
181         LASSERT(obj != NULL);
182         if (lu_object_is_dying(obj->ofo_obj.do_lu.lo_header))
183                 return 0;
184         return lu_object_exists(&obj->ofo_obj.do_lu);
185 }
186
187 static inline struct dt_object *fo2dt(struct ofd_object *obj)
188 {
189         return &obj->ofo_obj;
190 }
191
192 static inline struct dt_object *ofd_object_child(struct ofd_object *_obj)
193 {
194         struct lu_object *lu = &(_obj)->ofo_obj.do_lu;
195
196         return container_of0(lu_object_next(lu), struct dt_object, do_lu);
197 }
198
199 static inline struct ofd_device *ofd_obj2dev(const struct ofd_object *fo)
200 {
201         return ofd_dev(fo->ofo_obj.do_lu.lo_dev);
202 }
203
204 static inline struct lustre_capa *ofd_object_capa(const struct lu_env *env,
205                                                   const struct ofd_object *obj)
206 {
207         /* TODO: see mdd_object_capa() */
208         return BYPASS_CAPA;
209 }
210
211 static inline void ofd_read_lock(const struct lu_env *env,
212                                  struct ofd_object *fo)
213 {
214         struct dt_object  *next = ofd_object_child(fo);
215
216         next->do_ops->do_read_lock(env, next, 0);
217 }
218
219 static inline void ofd_read_unlock(const struct lu_env *env,
220                                    struct ofd_object *fo)
221 {
222         struct dt_object  *next = ofd_object_child(fo);
223
224         next->do_ops->do_read_unlock(env, next);
225 }
226
227 static inline void ofd_write_lock(const struct lu_env *env,
228                                   struct ofd_object *fo)
229 {
230         struct dt_object *next = ofd_object_child(fo);
231
232         next->do_ops->do_write_lock(env, next, 0);
233 }
234
235 static inline void ofd_write_unlock(const struct lu_env *env,
236                                     struct ofd_object *fo)
237 {
238         struct dt_object  *next = ofd_object_child(fo);
239
240         next->do_ops->do_write_unlock(env, next);
241 }
242
243 /*
244  * Common data shared by obdofd-level handlers. This is allocated per-thread
245  * to reduce stack consumption.
246  */
247 struct ofd_thread_info {
248         const struct lu_env             *fti_env;
249
250         struct obd_export               *fti_exp;
251         __u64                            fti_xid;
252         __u64                            fti_transno;
253         __u64                            fti_pre_version;
254         __u32                            fti_has_trans:1, /* has txn already */
255                                          fti_mult_trans:1;
256
257         struct lu_fid                    fti_fid;
258         struct lu_attr                   fti_attr;
259         struct lu_attr                   fti_attr2;
260         struct ldlm_res_id               fti_resid;
261         struct filter_fid                fti_mds_fid;
262         struct filter_fid                fti_mds_fid2;
263         struct ost_id                    fti_ostid;
264         struct ofd_object               *fti_obj;
265         union {
266                 char                     name[64]; /* for ofd_init0() */
267                 struct obd_statfs        osfs;    /* for obdofd_statfs() */
268         } fti_u;
269
270         /* Ops object filename */
271         struct lu_name                   fti_name;
272         struct dt_object_format          fti_dof;
273         struct lu_buf                    fti_buf;
274         loff_t                           fti_off;
275
276         /* Space used by the I/O, used by grant code */
277         unsigned long                    fti_used;
278 };
279
280 extern void target_recovery_fini(struct obd_device *obd);
281 extern void target_recovery_init(struct lu_target *lut, svc_handler_t handler);
282
283 /* ofd_capa.c */
284 int ofd_update_capa_key(struct ofd_device *ofd, struct lustre_capa_key *key);
285 int ofd_auth_capa(struct obd_export *exp, struct lu_fid *fid, obd_seq seq,
286                   struct lustre_capa *capa, __u64 opc);
287 void ofd_free_capa_keys(struct ofd_device *ofd);
288
289 /* ofd_dev.c */
290 extern struct lu_context_key ofd_thread_key;
291
292 /* ofd_obd.c */
293 extern struct obd_ops ofd_obd_ops;
294 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
295                         struct obd_statfs *osfs, __u64 max_age,
296                         int *from_cache);
297
298 /* ofd_fs.c */
299 obd_id ofd_last_id(struct ofd_device *ofd, obd_seq seq);
300 void ofd_last_id_set(struct ofd_device *ofd, obd_id id, obd_seq seq);
301 int ofd_last_id_write(const struct lu_env *env, struct ofd_device *ofd,
302                       obd_seq seq);
303 int ofd_group_load(const struct lu_env *env, struct ofd_device *ofd, int);
304 int ofd_fs_setup(const struct lu_env *env, struct ofd_device *ofd,
305                  struct obd_device *obd);
306 void ofd_fs_cleanup(const struct lu_env *env, struct ofd_device *ofd);
307
308 /* ofd_io.c */
309 int ofd_preprw(const struct lu_env *env,int cmd, struct obd_export *exp,
310                struct obdo *oa, int objcount, struct obd_ioobj *obj,
311                struct niobuf_remote *rnb, int *nr_local,
312                struct niobuf_local *lnb, struct obd_trans_info *oti,
313                struct lustre_capa *capa);
314 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
315                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
316                  struct niobuf_remote *rnb, int npages,
317                  struct niobuf_local *lnb, struct obd_trans_info *oti,
318                  int old_rc);
319
320 /* ofd_trans.c */
321 struct thandle *ofd_trans_create(const struct lu_env *env,
322                                  struct ofd_device *ofd);
323 int ofd_trans_start(const struct lu_env *env,
324                     struct ofd_device *ofd, struct ofd_object *fo,
325                     struct thandle *th);
326 void ofd_trans_stop(const struct lu_env *env, struct ofd_device *ofd,
327                     struct thandle *th, int rc);
328 int ofd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
329                     void *cookie);
330
331 /* lproc_ofd.c */
332 void lprocfs_ofd_init_vars(struct lprocfs_static_vars *lvars);
333 int lproc_ofd_attach_seqstat(struct obd_device *dev);
334 extern struct file_operations ofd_per_nid_stats_fops;
335
336 /* ofd_objects.c */
337 struct ofd_object *ofd_object_find(const struct lu_env *env,
338                                    struct ofd_device *ofd,
339                                    const struct lu_fid *fid);
340 struct ofd_object *ofd_object_find_or_create(const struct lu_env *env,
341                                              struct ofd_device *ofd,
342                                              const struct lu_fid *fid,
343                                              struct lu_attr *attr);
344 int ofd_object_ff_check(const struct lu_env *env, struct ofd_object *fo);
345 int ofd_precreate_object(const struct lu_env *env, struct ofd_device *ofd,
346                          obd_id id, obd_seq seq);
347
348 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo);
349 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
350                  struct lu_attr *la, struct filter_fid *ff);
351 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
352                      __u64 start, __u64 end, struct lu_attr *la,
353                      struct filter_fid *ff);
354 int ofd_object_destroy(const struct lu_env *, struct ofd_object *, int);
355 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
356                  struct lu_attr *la);
357 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
358                          struct lu_attr *la, int is_setattr);
359
360 /* ofd_grants.c */
361 #define OFD_GRANT_RATIO_SHIFT 8
362 static inline __u64 ofd_grant_reserved(struct ofd_device *ofd, obd_size bavail)
363 {
364         return (bavail * ofd->ofd_grant_ratio) >> OFD_GRANT_RATIO_SHIFT;
365 }
366
367 static inline int ofd_grant_ratio_conv(int percentage)
368 {
369         return (percentage << OFD_GRANT_RATIO_SHIFT) / 100;
370 }
371
372 static inline int ofd_grant_param_supp(struct obd_export *exp)
373 {
374         return !!(exp->exp_connect_flags & OBD_CONNECT_GRANT_PARAM);
375 }
376
377 /* Blocksize used for client not supporting OBD_CONNECT_GRANT_PARAM.
378  * That's 4KB=2^12 which is the biggest block size known to work whatever
379  * the client's page size is. */
380 #define COMPAT_BSIZE_SHIFT 12
381 static inline int ofd_grant_compat(struct obd_export *exp,
382                                    struct ofd_device *ofd)
383 {
384         /* Clients which don't support OBD_CONNECT_GRANT_PARAM cannot handle
385          * a block size > page size and consume CFS_PAGE_SIZE of grant when
386          * dirtying a page regardless of the block size */
387         return !!(ofd_obd(ofd)->obd_self_export != exp &&
388                   ofd->ofd_blockbits > COMPAT_BSIZE_SHIFT &&
389                   !ofd_grant_param_supp(exp));
390 }
391
392 static inline int ofd_grant_prohibit(struct obd_export *exp,
393                                      struct ofd_device *ofd)
394 {
395         /* When ofd_grant_compat_disable is set, we don't grant any space to
396          * clients not supporting OBD_CONNECT_GRANT_PARAM.
397          * Otherwise, space granted to such a client is inflated since it
398          * consumes CFS_PAGE_SIZE of grant space per block */
399         return !!(ofd_grant_compat(exp, ofd) && ofd->ofd_grant_compat_disable);
400 }
401
402 void ofd_grant_sanity_check(struct obd_device *obd, const char *func);
403 long ofd_grant_connect(const struct lu_env *env, struct obd_export *exp,
404                        obd_size want);
405 void ofd_grant_discard(struct obd_export *exp);
406 void ofd_grant_prepare_read(const struct lu_env *env, struct obd_export *exp,
407                             struct obdo *oa);
408 void ofd_grant_prepare_write(const struct lu_env *env, struct obd_export *exp,
409                              struct obdo *oa, struct niobuf_remote *rnb,
410                              int niocount);
411 void ofd_grant_commit(const struct lu_env *env, struct obd_export *exp, int rc);
412 int ofd_grant_create(const struct lu_env *env, struct obd_export *exp, int *nr);
413
414 /* ofd_fmd.c */
415 int ofd_fmd_init(void);
416 void ofd_fmd_exit(void);
417 struct ofd_mod_data *ofd_fmd_find(struct obd_export *exp,
418                                   struct lu_fid *fid);
419 struct ofd_mod_data *ofd_fmd_get(struct obd_export *exp,
420                                  struct lu_fid *fid);
421 void ofd_fmd_put(struct obd_export *exp, struct ofd_mod_data *fmd);
422 void ofd_fmd_expire(struct obd_export *exp);
423 void ofd_fmd_cleanup(struct obd_export *exp);
424 #ifdef DO_FMD_DROP
425 void ofd_fmd_drop(struct obd_export *exp, struct lu_fid *fid);
426 #else
427 #define ofd_fmd_drop(exp, fid) do {} while (0)
428 #endif
429
430 /* ofd_lvb.c */
431 extern struct ldlm_valblock_ops ofd_lvbo;
432
433 /* ofd_dlm.c */
434 int ofd_intent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
435                       void *req_cookie, ldlm_mode_t mode, int flags,
436                       void *data);
437
438 static inline struct ofd_thread_info * ofd_info(const struct lu_env *env)
439 {
440         struct ofd_thread_info *info;
441
442         info = lu_context_key_get(&env->le_ctx, &ofd_thread_key);
443         LASSERT(info);
444         LASSERT(info->fti_env);
445         LASSERT(info->fti_env == env);
446         return info;
447 }
448
449 static inline struct ofd_thread_info * ofd_info_init(const struct lu_env *env,
450                                                      struct obd_export *exp)
451 {
452         struct ofd_thread_info *info;
453
454         info = lu_context_key_get(&env->le_ctx, &ofd_thread_key);
455         LASSERT(info);
456         LASSERT(info->fti_exp == NULL);
457         LASSERT(info->fti_env == NULL);
458         LASSERT(info->fti_attr.la_valid == 0);
459
460         info->fti_env = env;
461         info->fti_exp = exp;
462         info->fti_pre_version = 0;
463         info->fti_transno = 0;
464         info->fti_has_trans = 0;
465         return info;
466 }
467
468 /* The same as osc_build_res_name() */
469 static inline void ofd_build_resid(const struct lu_fid *fid,
470                                    struct ldlm_res_id *resname)
471 {
472         if (fid_is_idif(fid)) {
473                 /* get id/seq like ostid_idif_pack() does */
474                 osc_build_res_name(fid_idif_id(fid_seq(fid), fid_oid(fid),
475                                                fid_ver(fid)),
476                                    FID_SEQ_OST_MDT0, resname);
477         } else {
478                 /* In the future, where OSTs have FID sequences allocated. */
479                 fid_build_reg_res_name(fid, resname);
480         }
481 }
482
483 static inline void ofd_fid_from_resid(struct lu_fid *fid,
484                                       const struct ldlm_res_id *name)
485 {
486         /* if seq is FID_SEQ_OST_MDT0 then we have IDIF and resid was built
487          * using osc_build_res_name function. */
488         if (fid_seq_is_mdt0(name->name[LUSTRE_RES_ID_VER_OID_OFF])) {
489                 struct ost_id ostid;
490
491                 ostid.oi_id = name->name[LUSTRE_RES_ID_SEQ_OFF];
492                 ostid.oi_seq = name->name[LUSTRE_RES_ID_VER_OID_OFF];
493                 fid_ostid_unpack(fid, &ostid, 0);
494         } else {
495                 fid->f_seq = name->name[LUSTRE_RES_ID_SEQ_OFF];
496                 fid->f_oid = (__u32)name->name[LUSTRE_RES_ID_VER_OID_OFF];
497                 fid->f_ver = name->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32;
498         }
499 }
500
501 static inline void ofd_oti2info(struct ofd_thread_info *info,
502                                 struct obd_trans_info *oti)
503 {
504         info->fti_xid = oti->oti_xid;
505         info->fti_transno = oti->oti_transno;
506         info->fti_pre_version = oti->oti_pre_version;
507 }
508
509 static inline void ofd_info2oti(struct ofd_thread_info *info,
510                                 struct obd_trans_info *oti)
511 {
512         oti->oti_xid = info->fti_xid;
513         LASSERTF(ergo(oti->oti_transno > 0,
514                       oti->oti_transno == info->fti_transno),
515                  "Overwrite replay transno "LPX64" by "LPX64"\n",
516                  oti->oti_transno, info->fti_transno);
517         oti->oti_transno = info->fti_transno;
518         oti->oti_pre_version = info->fti_pre_version;
519 }
520
521 /* sync on lock cancel is useless when we force a journal flush,
522  * and if we enable async journal commit, we should also turn on
523  * sync on lock cancel if it is not enabled already. */
524 static inline void ofd_slc_set(struct ofd_device *ofd)
525 {
526         if (ofd->ofd_syncjournal == 1)
527                 ofd->ofd_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
528         else if (ofd->ofd_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
529                 ofd->ofd_sync_lock_cancel = ALWAYS_SYNC_ON_CANCEL;
530 }
531
532 static inline void ofd_prepare_fidea(struct filter_fid *ff, struct obdo *oa)
533 {
534         if (!(oa->o_valid & OBD_MD_FLGROUP))
535                 oa->o_seq = 0;
536         /* packing fid and converting it to LE for storing into EA.
537          * Here ->o_stripe_idx should be filled by LOV and rest of
538          * fields - by client. */
539         ff->ff_parent.f_seq = cpu_to_le64(oa->o_parent_seq);
540         ff->ff_parent.f_oid = cpu_to_le32(oa->o_parent_oid);
541         /* XXX: we are ignoring o_parent_ver here, since this should
542          *      be the same for all objects in this fileset. */
543         ff->ff_parent.f_ver = cpu_to_le32(oa->o_stripe_idx);
544         ff->ff_objid = cpu_to_le64(oa->o_id);
545         ff->ff_seq = cpu_to_le64(oa->o_seq);
546 }
547
548 /* niobuf_remote has no rnb_ prefix in master */
549 #define rnb_offset offset
550 #define rnb_flags  flags
551 #define rnb_len    len
552 /* the same for niobuf_local */
553 #define lnb_flags flags
554 #define lnb_rc    rc
555
556 #endif /* _OFD_INTERNAL_H */