Whamcloud - gitweb
d06bbfec66a8d82c2863b9c3969ad1e39571618c
[fs/lustre-release.git] / lustre / include / obd.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 __OBD_H
38 #define __OBD_H
39
40 #if defined(__linux__)
41 #include <linux/obd.h>
42 #elif defined(__APPLE__)
43 #include <darwin/obd.h>
44 #elif defined(__WINNT__)
45 #include <winnt/obd.h>
46 #else
47 #error Unsupported operating system.
48 #endif
49
50 #define IOC_OSC_TYPE         'h'
51 #define IOC_OSC_MIN_NR       20
52 #define IOC_OSC_SET_ACTIVE   _IOWR(IOC_OSC_TYPE, 21, struct obd_device *)
53 #define IOC_OSC_MAX_NR       50
54
55 #define IOC_MDC_TYPE         'i'
56 #define IOC_MDC_MIN_NR       20
57 #define IOC_MDC_MAX_NR       50
58
59 #include <lustre/lustre_idl.h>
60 #include <lustre_lib.h>
61 #include <libcfs/bitmap.h>
62 #ifdef HAVE_SERVER_SUPPORT
63 # include <lu_target.h>
64 # include <obd_target.h>
65 #endif
66 #include <lu_ref.h>
67 #include <lustre_export.h>
68 #include <lustre_fid.h>
69 #include <lustre_fld.h>
70 #include <lustre_capa.h>
71 #include <lvfs.h>
72
73 #define MAX_OBD_DEVICES 8192
74
75 struct osc_async_rc {
76         int     ar_rc;
77         int     ar_force_sync;
78         __u64   ar_min_xid;
79 };
80
81 struct lov_oinfo {                 /* per-stripe data structure */
82         struct ost_id   loi_oi;    /* object ID/Sequence on the target OST */
83         int loi_ost_idx;           /* OST stripe index in lov_tgt_desc->tgts */
84         int loi_ost_gen;           /* generation of this loi_ost_idx */
85
86         unsigned long loi_kms_valid:1;
87         __u64 loi_kms;             /* known minimum size */
88         struct ost_lvb loi_lvb;
89         struct osc_async_rc     loi_ar;
90 };
91
92 static inline void loi_kms_set(struct lov_oinfo *oinfo, __u64 kms)
93 {
94         oinfo->loi_kms = kms;
95         oinfo->loi_kms_valid = 1;
96 }
97
98 static inline void loi_init(struct lov_oinfo *loi)
99 {
100 }
101
102 struct lov_stripe_md {
103         atomic_t        lsm_refc;
104         spinlock_t      lsm_lock;
105         pid_t           lsm_lock_owner; /* debugging */
106
107         /* maximum possible file size, might change as OSTs status changes,
108          * e.g. disconnected, deactivated */
109         __u64            lsm_maxbytes;
110         struct {
111                 /* Public members. */
112                 struct ost_id lw_object_oi; /* lov object id/seq */
113
114                 /* LOV-private members start here -- only for use in lov/. */
115                 __u32 lw_magic;
116                 __u32 lw_stripe_size;      /* size of the stripe */
117                 __u32 lw_pattern;          /* striping pattern (RAID0, RAID1) */
118                 __u16 lw_stripe_count;  /* number of objects being striped over */
119                 __u16 lw_layout_gen;       /* generation of the layout */
120                 char  lw_pool_name[LOV_MAXPOOLNAME]; /* pool name */
121         } lsm_wire;
122
123         struct lov_oinfo *lsm_oinfo[0];
124 };
125
126 #define lsm_oi           lsm_wire.lw_object_oi
127 #define lsm_magic        lsm_wire.lw_magic
128 #define lsm_layout_gen   lsm_wire.lw_layout_gen
129 #define lsm_stripe_size  lsm_wire.lw_stripe_size
130 #define lsm_pattern      lsm_wire.lw_pattern
131 #define lsm_stripe_count lsm_wire.lw_stripe_count
132 #define lsm_pool_name    lsm_wire.lw_pool_name
133
134 static inline bool lsm_is_released(struct lov_stripe_md *lsm)
135 {
136         return !!(lsm->lsm_pattern & LOV_PATTERN_F_RELEASED);
137 }
138
139 static inline bool lsm_has_objects(struct lov_stripe_md *lsm)
140 {
141         if (lsm == NULL)
142                 return false;
143         if (lsm_is_released(lsm))
144                 return false;
145         return true;
146 }
147
148 static inline int lov_stripe_md_size(unsigned int stripe_count)
149 {
150         struct lov_stripe_md lsm;
151
152         return sizeof(lsm) + stripe_count * sizeof(lsm.lsm_oinfo[0]);
153 }
154
155 struct obd_info;
156
157 typedef int (*obd_enqueue_update_f)(void *cookie, int rc);
158
159 /* obd info for a particular level (lov, osc). */
160 struct obd_info {
161         /* Lock policy. It keeps an extent which is specific for a particular
162          * OSC. (e.g. lov_prep_enqueue_set initialises extent of the policy,
163          * and osc_enqueue passes it into ldlm_lock_match & ldlm_cli_enqueue. */
164         ldlm_policy_data_t      oi_policy;
165         /* Flags used for set request specific flags:
166            - while lock handling, the flags obtained on the enqueue
167            request are set here.
168            - while stats, the flags used for control delay/resend.
169            - while setattr, the flags used for distinguish punch operation
170          */
171         __u64                   oi_flags;
172         /* Lock handle specific for every OSC lock. */
173         struct lustre_handle   *oi_lockh;
174         /* lsm data specific for every OSC. */
175         struct lov_stripe_md   *oi_md;
176         /* obdo data specific for every OSC, if needed at all. */
177         struct obdo            *oi_oa;
178         /* statfs data specific for every OSC, if needed at all. */
179         struct obd_statfs      *oi_osfs;
180         /* An update callback which is called to update some data on upper
181          * level. E.g. it is used for update lsm->lsm_oinfo at every recieved
182          * request in osc level for enqueue requests. It is also possible to
183          * update some caller data from LOV layer if needed. */
184         obd_enqueue_update_f    oi_cb_up;
185         /* oss capability, its type is obd_capa in client to avoid copy.
186          * in contrary its type is lustre_capa in OSS. */
187         void                   *oi_capa;
188         /* transfer jobid from ost_sync() to filter_sync()... */
189         char                   *oi_jobid;
190 };
191
192 struct obd_type {
193         struct list_head typ_chain;
194         struct obd_ops *typ_dt_ops;
195         struct md_ops *typ_md_ops;
196         struct proc_dir_entry *typ_procroot;
197         struct proc_dir_entry *typ_procsym;
198         __u32 typ_sym_filter;
199         char *typ_name;
200         int  typ_refcnt;
201         struct lu_device_type *typ_lu;
202         spinlock_t obd_type_lock;
203 };
204
205 struct brw_page {
206         obd_off  off;
207         struct page *pg;
208         int count;
209         obd_flag flag;
210 };
211
212 /* llog contexts */
213 enum llog_ctxt_id {
214         LLOG_CONFIG_ORIG_CTXT  =  0,
215         LLOG_CONFIG_REPL_CTXT,
216         LLOG_MDS_OST_ORIG_CTXT,
217         LLOG_MDS_OST_REPL_CTXT,
218         LLOG_SIZE_ORIG_CTXT,
219         LLOG_SIZE_REPL_CTXT,
220         LLOG_RD1_ORIG_CTXT,
221         LLOG_RD1_REPL_CTXT,
222         LLOG_TEST_ORIG_CTXT,
223         LLOG_TEST_REPL_CTXT,
224         LLOG_LOVEA_ORIG_CTXT,
225         LLOG_LOVEA_REPL_CTXT,
226         LLOG_CHANGELOG_ORIG_CTXT,       /**< changelog generation on mdd */
227         LLOG_CHANGELOG_REPL_CTXT,       /**< changelog access on clients */
228         LLOG_CHANGELOG_USER_ORIG_CTXT,  /**< for multiple changelog consumers */
229         LLOG_AGENT_ORIG_CTXT,           /**< agent requests generation on cdt */
230         LLOG_MAX_CTXTS
231 };
232
233 struct timeout_item {
234         enum timeout_event ti_event;
235         cfs_time_t         ti_timeout;
236         timeout_cb_t       ti_cb;
237         void              *ti_cb_data;
238         cfs_list_t         ti_obd_list;
239         cfs_list_t         ti_chain;
240 };
241
242 #define OBD_MAX_RIF_DEFAULT     8
243 #define OBD_MAX_RIF_MAX         512
244 #define OSC_MAX_RIF_MAX         256
245 #define OSC_MAX_DIRTY_DEFAULT   (OBD_MAX_RIF_DEFAULT * 4)
246 #define OSC_MAX_DIRTY_MB_MAX    2048     /* arbitrary, but < MAX_LONG bytes */
247 #define OSC_DEFAULT_RESENDS     10
248
249 /* possible values for fo_sync_lock_cancel */
250 enum {
251         NEVER_SYNC_ON_CANCEL = 0,
252         BLOCKING_SYNC_ON_CANCEL = 1,
253         ALWAYS_SYNC_ON_CANCEL = 2,
254         NUM_SYNC_ON_CANCEL_STATES
255 };
256
257 struct mdc_rpc_lock;
258 struct obd_import;
259 struct client_obd {
260         struct rw_semaphore  cl_sem;
261         struct obd_uuid          cl_target_uuid;
262         struct obd_import       *cl_import; /* ptlrpc connection state */
263         int                      cl_conn_count;
264         /* max_mds_easize is purely a performance thing so we don't have to
265          * call obd_size_diskmd() all the time. */
266         int                      cl_default_mds_easize;
267         int                      cl_max_mds_easize;
268         int                      cl_default_mds_cookiesize;
269         int                      cl_max_mds_cookiesize;
270
271         enum lustre_sec_part     cl_sp_me;
272         enum lustre_sec_part     cl_sp_to;
273         struct sptlrpc_flavor    cl_flvr_mgc;   /* fixed flavor of mgc->mgs */
274
275         /* the grant values are protected by loi_list_lock below */
276         long                     cl_dirty;         /* all _dirty_ in bytes */
277         long                     cl_dirty_max;     /* allowed w/o rpc */
278         long                     cl_dirty_transit; /* dirty synchronous */
279         long                     cl_avail_grant;   /* bytes of credit for ost */
280         long                     cl_lost_grant;    /* lost credits (trunc) */
281
282         /* since we allocate grant by blocks, we don't know how many grant will
283          * be used to add a page into cache. As a solution, we reserve maximum
284          * grant before trying to dirty a page and unreserve the rest.
285          * See osc_{reserve|unreserve}_grant for details. */
286         long                 cl_reserved_grant;
287         cfs_list_t           cl_cache_waiters; /* waiting for cache/grant */
288         cfs_time_t           cl_next_shrink_grant;   /* jiffies */
289         cfs_list_t           cl_grant_shrink_list;  /* Timeout event list */
290         int                  cl_grant_shrink_interval; /* seconds */
291
292         /* A chunk is an optimal size used by osc_extent to determine
293          * the extent size. A chunk is max(PAGE_CACHE_SIZE, OST block size) */
294         int                  cl_chunkbits;
295         int                  cl_chunk;
296         int                  cl_extent_tax; /* extent overhead, by bytes */
297
298         /* keep track of objects that have lois that contain pages which
299          * have been queued for async brw.  this lock also protects the
300          * lists of osc_client_pages that hang off of the loi */
301         /*
302          * ->cl_loi_list_lock protects consistency of
303          * ->cl_loi_{ready,read,write}_list. ->ap_make_ready() and
304          * ->ap_completion() call-backs are executed under this lock. As we
305          * cannot guarantee that these call-backs never block on all platforms
306          * (as a matter of fact they do block on Mac OS X), type of
307          * ->cl_loi_list_lock is platform dependent: it's a spin-lock on Linux
308          * and blocking mutex on Mac OS X. (Alternative is to make this lock
309          * blocking everywhere, but we don't want to slow down fast-path of
310          * our main platform.)
311          *
312          * Exact type of ->cl_loi_list_lock is defined in arch/obd.h together
313          * with client_obd_list_{un,}lock() and
314          * client_obd_list_lock_{init,done}() functions.
315          *
316          * NB by Jinshan: though field names are still _loi_, but actually
317          * osc_object{}s are in the list.
318          */
319         client_obd_lock_t        cl_loi_list_lock;
320         cfs_list_t               cl_loi_ready_list;
321         cfs_list_t               cl_loi_hp_ready_list;
322         cfs_list_t               cl_loi_write_list;
323         cfs_list_t               cl_loi_read_list;
324         int                      cl_r_in_flight;
325         int                      cl_w_in_flight;
326         /* just a sum of the loi/lop pending numbers to be exported by /proc */
327         atomic_t                 cl_pending_w_pages;
328         atomic_t                 cl_pending_r_pages;
329         __u32                    cl_max_pages_per_rpc;
330         int                      cl_max_rpcs_in_flight;
331         struct obd_histogram     cl_read_rpc_hist;
332         struct obd_histogram     cl_write_rpc_hist;
333         struct obd_histogram     cl_read_page_hist;
334         struct obd_histogram     cl_write_page_hist;
335         struct obd_histogram     cl_read_offset_hist;
336         struct obd_histogram     cl_write_offset_hist;
337
338         /* lru for osc caching pages */
339         struct cl_client_cache  *cl_cache;
340         cfs_list_t               cl_lru_osc; /* member of cl_cache->ccc_lru */
341         atomic_t                *cl_lru_left;
342         atomic_t                 cl_lru_busy;
343         atomic_t                 cl_lru_shrinkers;
344         atomic_t                 cl_lru_in_list;
345         cfs_list_t               cl_lru_list; /* lru page list */
346         client_obd_lock_t        cl_lru_list_lock; /* page list protector */
347         atomic_t                 cl_unstable_count;
348
349         /* number of in flight destroy rpcs is limited to max_rpcs_in_flight */
350         atomic_t             cl_destroy_in_flight;
351         wait_queue_head_t        cl_destroy_waitq;
352
353         struct mdc_rpc_lock     *cl_rpc_lock;
354         struct mdc_rpc_lock     *cl_close_lock;
355
356         /* mgc datastruct */
357         struct mutex             cl_mgc_mutex;
358         struct local_oid_storage *cl_mgc_los;
359         struct dt_object        *cl_mgc_configs_dir;
360         atomic_t             cl_mgc_refcount;
361         struct obd_export       *cl_mgc_mgsexp;
362
363         /* checksumming for data sent over the network */
364         unsigned int             cl_checksum:1; /* 0 = disabled, 1 = enabled */
365         /* supported checksum types that are worked out at connect time */
366         __u32                    cl_supp_cksum_types;
367         /* checksum algorithm to be used */
368         cksum_type_t             cl_cksum_type;
369
370         /* also protected by the poorly named _loi_list_lock lock above */
371         struct osc_async_rc      cl_ar;
372
373         /* used by quotacheck when the servers are older than 2.4 */
374         int                      cl_qchk_stat; /* quotacheck stat of the peer */
375 #define CL_NOT_QUOTACHECKED 1   /* client->cl_qchk_stat init value */
376 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 7, 50, 0)
377 #warning "please consider removing quotacheck compatibility code"
378 #endif
379
380         /* sequence manager */
381         struct lu_client_seq    *cl_seq;
382
383         atomic_t             cl_resends; /* resend count */
384
385         /* ptlrpc work for writeback in ptlrpcd context */
386         void                    *cl_writeback_work;
387         void                    *cl_lru_work;
388         /* hash tables for osc_quota_info */
389         cfs_hash_t              *cl_quota_hash[MAXQUOTAS];
390 };
391 #define obd2cli_tgt(obd) ((char *)(obd)->u.cli.cl_target_uuid.uuid)
392
393 struct obd_id_info {
394         __u32   idx;
395         obd_id  *data;
396 };
397
398 struct echo_client_obd {
399         struct obd_export       *ec_exp;   /* the local connection to osc/lov */
400         spinlock_t              ec_lock;
401         cfs_list_t           ec_objects;
402         cfs_list_t           ec_locks;
403         int                  ec_nstripes;
404         __u64                ec_unique;
405 };
406
407 /* Generic subset of OSTs */
408 struct ost_pool {
409         __u32              *op_array;      /* array of index of
410                                                    lov_obd->lov_tgts */
411         unsigned int        op_count;      /* number of OSTs in the array */
412         unsigned int        op_size;       /* allocated size of lp_array */
413         struct rw_semaphore op_rw_sem;     /* to protect ost_pool use */
414 };
415
416 /* allow statfs data caching for 1 second */
417 #define OBD_STATFS_CACHE_SECONDS 1
418
419 struct lov_tgt_desc {
420         cfs_list_t          ltd_kill;
421         struct obd_uuid     ltd_uuid;
422         struct obd_device  *ltd_obd;
423         struct obd_export  *ltd_exp;
424         __u32               ltd_gen;
425         __u32               ltd_index;   /* index in lov_obd->tgts */
426         unsigned long       ltd_active:1,/* is this target up for requests */
427                             ltd_activate:1,/* should  target be activated */
428                             ltd_reap:1;  /* should this target be deleted */
429 };
430
431 struct lov_obd {
432         struct lov_desc         desc;
433         struct lov_tgt_desc   **lov_tgts;               /* sparse array */
434         struct ost_pool         lov_packed;             /* all OSTs in a packed
435                                                            array */
436         struct mutex            lov_lock;
437         struct obd_connect_data lov_ocd;
438         atomic_t                lov_refcount;
439         __u32                   lov_death_row;  /* tgts scheduled to be deleted */
440         __u32                   lov_tgt_size;   /* size of tgts array */
441         int                     lov_connects;
442         int                     lov_pool_count;
443         cfs_hash_t             *lov_pools_hash_body; /* used for key access */
444         cfs_list_t              lov_pool_list;  /* used for sequential access */
445         cfs_proc_dir_entry_t   *lov_pool_proc_entry;
446         enum lustre_sec_part    lov_sp_me;
447
448         /* Cached LRU and unstable data from upper layer */
449         void                   *lov_cache;
450
451         struct rw_semaphore     lov_notify_lock;
452 };
453
454 struct lmv_tgt_desc {
455         struct obd_uuid         ltd_uuid;
456         struct obd_export       *ltd_exp;
457         int                     ltd_idx;
458         struct mutex            ltd_fid_mutex;
459         unsigned long           ltd_active:1; /* target up for requests */
460 };
461
462 enum placement_policy {
463         PLACEMENT_CHAR_POLICY   = 0,
464         PLACEMENT_NID_POLICY    = 1,
465         PLACEMENT_INVAL_POLICY  = 2,
466         PLACEMENT_MAX_POLICY
467 };
468
469 typedef enum placement_policy placement_policy_t;
470
471 struct lmv_obd {
472         int                     refcount;
473         struct lu_client_fld    lmv_fld;
474         spinlock_t              lmv_lock;
475         placement_policy_t      lmv_placement;
476         struct lmv_desc         desc;
477         struct obd_uuid         cluuid;
478         struct obd_export       *exp;
479
480         struct mutex            init_mutex;
481         int                     connected;
482         int                     max_easize;
483         int                     max_def_easize;
484         int                     max_cookiesize;
485         int                     max_def_cookiesize;
486         int                     server_timeout;
487
488         int                     tgts_size; /* size of tgts array */
489         struct lmv_tgt_desc     **tgts;
490
491         struct obd_connect_data conn_data;
492 };
493
494 struct niobuf_local {
495         __u64           lnb_file_offset;
496         __u32           lnb_page_offset;
497         __u32           len;
498         __u32           flags;
499         struct page     *page;
500         struct dentry   *dentry;
501         int             lnb_grant_used;
502         int             rc;
503 };
504
505 #define LUSTRE_FLD_NAME         "fld"
506 #define LUSTRE_SEQ_NAME         "seq"
507
508 #define LUSTRE_MDD_NAME         "mdd"
509 #define LUSTRE_OSD_LDISKFS_NAME "osd-ldiskfs"
510 #define LUSTRE_OSD_ZFS_NAME     "osd-zfs"
511 #define LUSTRE_VVP_NAME         "vvp"
512 #define LUSTRE_LMV_NAME         "lmv"
513 #define LUSTRE_SLP_NAME         "slp"
514 #define LUSTRE_LOD_NAME         "lod"
515 #define LUSTRE_OSP_NAME         "osp"
516 #define LUSTRE_LWP_NAME         "lwp"
517
518 /* obd device type names */
519  /* FIXME all the references to LUSTRE_MDS_NAME should be swapped with LUSTRE_MDT_NAME */
520 #define LUSTRE_MDS_NAME         "mds"
521 #define LUSTRE_MDT_NAME         "mdt"
522 #define LUSTRE_MDC_NAME         "mdc"
523 #define LUSTRE_OSS_NAME         "ost"       /* FIXME change name to oss */
524 #define LUSTRE_OST_NAME         "obdfilter" /* FIXME change name to ost */
525 #define LUSTRE_OSC_NAME         "osc"
526 #define LUSTRE_LOV_NAME         "lov"
527 #define LUSTRE_MGS_NAME         "mgs"
528 #define LUSTRE_MGC_NAME         "mgc"
529
530 #define LUSTRE_ECHO_NAME        "obdecho"
531 #define LUSTRE_ECHO_CLIENT_NAME "echo_client"
532 #define LUSTRE_QMT_NAME         "qmt"
533
534 /* Constant obd names (post-rename) */
535 #define LUSTRE_MDS_OBDNAME "MDS"
536 #define LUSTRE_OSS_OBDNAME "OSS"
537 #define LUSTRE_MGS_OBDNAME "MGS"
538 #define LUSTRE_MGC_OBDNAME "MGC"
539
540 static inline int is_osp_on_mdt(char *name)
541 {
542         char   *ptr;
543
544         ptr = strrchr(name, '-');
545         if (ptr == NULL) {
546                 CERROR("%s is not a obdname\n", name);
547                 return 0;
548         }
549
550         /* 1.8 OSC/OSP name on MDT is fsname-OSTxxxx-osc */
551         if (strncmp(ptr + 1, "osc", 3) == 0)
552                 return 1;
553
554         if (strncmp(ptr + 1, "MDT", 3) != 0)
555                 return 0;
556
557         while (*(--ptr) != '-' && ptr != name);
558
559         if (ptr == name)
560                 return 0;
561
562         if (strncmp(ptr + 1, LUSTRE_OSP_NAME, strlen(LUSTRE_OSP_NAME)) != 0 &&
563             strncmp(ptr + 1, LUSTRE_OSC_NAME, strlen(LUSTRE_OSC_NAME)) != 0)
564                 return 0;
565
566         return 1;
567 }
568
569 /* Don't conflict with on-wire flags OBD_BRW_WRITE, etc */
570 #define N_LOCAL_TEMP_PAGE 0x10000000
571
572 struct obd_trans_info {
573         __u64                    oti_transno;
574         __u64                    oti_xid;
575         /* Only used on the server side for tracking acks. */
576         struct oti_req_ack_lock {
577                 struct lustre_handle lock;
578                 __u32                mode;
579         }                        oti_ack_locks[4];
580         void                    *oti_handle;
581         struct llog_cookie       oti_onecookie;
582         struct llog_cookie      *oti_logcookies;
583         int                      oti_numcookies;
584         /** synchronous write is needed */
585         unsigned long            oti_sync_write:1;
586
587         /* initial thread handling transaction */
588         struct ptlrpc_thread *   oti_thread;
589         __u32                    oti_conn_cnt;
590         /** VBR: versions */
591         __u64                    oti_pre_version;
592         /** JobID */
593         char                    *oti_jobid;
594
595         struct obd_uuid         *oti_ost_uuid;
596 };
597
598 static inline void oti_init(struct obd_trans_info *oti,
599                             struct ptlrpc_request *req)
600 {
601         if (oti == NULL)
602                 return;
603         memset(oti, 0, sizeof(*oti));
604
605         if (req == NULL)
606                 return;
607
608         oti->oti_xid = req->rq_xid;
609         /** VBR: take versions from request */
610         if (req->rq_reqmsg != NULL &&
611             lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
612                 __u64 *pre_version = lustre_msg_get_versions(req->rq_reqmsg);
613                 oti->oti_pre_version = pre_version ? pre_version[0] : 0;
614                 oti->oti_transno = lustre_msg_get_transno(req->rq_reqmsg);
615         }
616
617         /** called from mds_create_objects */
618         if (req->rq_repmsg != NULL)
619                 oti->oti_transno = lustre_msg_get_transno(req->rq_repmsg);
620         oti->oti_thread = req->rq_svc_thread;
621         if (req->rq_reqmsg != NULL)
622                 oti->oti_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
623 }
624
625 static inline void oti_alloc_cookies(struct obd_trans_info *oti,int num_cookies)
626 {
627         if (!oti)
628                 return;
629
630         if (num_cookies == 1)
631                 oti->oti_logcookies = &oti->oti_onecookie;
632         else
633                 OBD_ALLOC_LARGE(oti->oti_logcookies,
634                                 num_cookies * sizeof(oti->oti_onecookie));
635
636         oti->oti_numcookies = num_cookies;
637 }
638
639 static inline void oti_free_cookies(struct obd_trans_info *oti)
640 {
641         if (!oti || !oti->oti_logcookies)
642                 return;
643
644         if (oti->oti_logcookies == &oti->oti_onecookie)
645                 LASSERT(oti->oti_numcookies == 1);
646         else
647                 OBD_FREE_LARGE(oti->oti_logcookies,
648                                oti->oti_numcookies*sizeof(oti->oti_onecookie));
649         oti->oti_logcookies = NULL;
650         oti->oti_numcookies = 0;
651 }
652
653 /*
654  * Events signalled through obd_notify() upcall-chain.
655  */
656 enum obd_notify_event {
657         /* target added */
658         OBD_NOTIFY_CREATE,
659         /* Device connect start */
660         OBD_NOTIFY_CONNECT,
661         /* Device activated */
662         OBD_NOTIFY_ACTIVE,
663         /* Device deactivated */
664         OBD_NOTIFY_INACTIVE,
665         /* Device disconnected */
666         OBD_NOTIFY_DISCON,
667         /* Connect data for import were changed */
668         OBD_NOTIFY_OCD,
669         /* Sync request */
670         OBD_NOTIFY_SYNC_NONBLOCK,
671         OBD_NOTIFY_SYNC,
672         /* Configuration event */
673         OBD_NOTIFY_CONFIG,
674         /* Administratively deactivate/activate event */
675         OBD_NOTIFY_DEACTIVATE,
676         OBD_NOTIFY_ACTIVATE
677 };
678
679 /* bit-mask flags for config events */
680 enum config_flags {
681         CONFIG_LOG      = 0x1,  /* finished processing config log */
682         CONFIG_SYNC     = 0x2,  /* mdt synced 1 ost */
683         CONFIG_TARGET   = 0x4   /* one target is added */
684 };
685
686 /*
687  * Data structure used to pass obd_notify()-event to non-obd listeners (llite
688  * and liblustre being main examples).
689  */
690 struct obd_notify_upcall {
691         int (*onu_upcall)(struct obd_device *host, struct obd_device *watched,
692                           enum obd_notify_event ev, void *owner, void *data);
693         /* Opaque datum supplied by upper layer listener */
694         void *onu_owner;
695 };
696
697 struct target_recovery_data {
698         svc_handler_t           trd_recovery_handler;
699         pid_t                   trd_processing_task;
700         struct completion       trd_starting;
701         struct completion       trd_finishing;
702 };
703
704 struct obd_llog_group {
705         int                olg_seq;
706         struct llog_ctxt   *olg_ctxts[LLOG_MAX_CTXTS];
707         wait_queue_head_t  olg_waitq;
708         spinlock_t         olg_lock;
709         struct mutex       olg_cat_processing;
710 };
711
712 /* corresponds to one of the obd's */
713 #define OBD_DEVICE_MAGIC        0XAB5CD6EF
714 #define OBD_DEV_BY_DEVNAME      0xffffd0de
715
716 struct obd_device {
717         struct obd_type         *obd_type;
718         __u32                    obd_magic;
719
720         /* common and UUID name of this device */
721         char                     obd_name[MAX_OBD_NAME];
722         struct obd_uuid          obd_uuid;
723         int                      obd_minor;
724         struct lu_device        *obd_lu_dev;
725
726         /* bitfield modification is protected by obd_dev_lock */
727         unsigned long
728                 obd_attached:1,         /* finished attach */
729                 obd_set_up:1,           /* finished setup */
730                 obd_recovering:1,       /* there are recoverable clients */
731                 obd_abort_recovery:1,   /* recovery expired */
732                 obd_version_recov:1,    /* obd uses version checking */
733                 obd_replayable:1,       /* recovery is enabled;
734                                          * inform clients */
735                 obd_no_transno:1,       /* no committed-transno notification */
736                 obd_no_recov:1,         /* fail instead of retry messages */
737                 obd_stopping:1,         /* started cleanup */
738                 obd_starting:1,         /* started setup */
739                 obd_force:1,            /* cleanup with > 0 obd refcount */
740                 obd_fail:1,             /* cleanup with failover */
741                 obd_async_recov:1,      /* allow asynchronous orphan cleanup */
742                 obd_no_conn:1,          /* deny new connections */
743                 obd_inactive:1,         /* device active/inactive
744                                          * (for /proc/status only!!) */
745                 obd_no_ir:1,            /* no imperative recovery. */
746                 obd_process_conf:1,     /* device is processing mgs config */
747                 obd_uses_nid_stats:1;   /* maintain per-client OBD stats */
748
749         /* use separate field as it is set in interrupt to don't mess with
750          * protection of other bits using _bh lock */
751         unsigned long obd_recovery_expired:1;
752         /* uuid-export hash body */
753         cfs_hash_t             *obd_uuid_hash;
754         /* nid-export hash body */
755         cfs_hash_t             *obd_nid_hash;
756         /* nid stats body */
757         cfs_hash_t             *obd_nid_stats_hash;
758         cfs_list_t              obd_nid_stats;
759         atomic_t            obd_refcount;
760         cfs_list_t              obd_exports;
761         cfs_list_t              obd_unlinked_exports;
762         cfs_list_t              obd_delayed_exports;
763         struct list_head        obd_lwp_list;
764         int                     obd_num_exports;
765         spinlock_t              obd_nid_lock;
766         struct ldlm_namespace  *obd_namespace;
767         struct ptlrpc_client    obd_ldlm_client; /* XXX OST/MDS only */
768         /* a spinlock is OK for what we do now, may need a semaphore later */
769         spinlock_t              obd_dev_lock; /* protect OBD bitfield above */
770         struct mutex            obd_dev_mutex;
771         __u64                   obd_last_committed;
772         spinlock_t              obd_osfs_lock;
773         struct obd_statfs       obd_osfs;       /* locked by obd_osfs_lock */
774         __u64                   obd_osfs_age;
775         struct lvfs_run_ctxt    obd_lvfs_ctxt;
776         struct obd_llog_group   obd_olg;        /* default llog group */
777         struct obd_device       *obd_observer;
778         struct rw_semaphore     obd_observer_link_sem;
779         struct obd_notify_upcall obd_upcall;
780         struct obd_export       *obd_self_export;
781         struct obd_export       *obd_lwp_export;
782         /* list of exports in LRU order, for ping evictor, with obd_dev_lock */
783         cfs_list_t              obd_exports_timed;
784         time_t                  obd_eviction_timer; /* for ping evictor */
785
786         int                     obd_max_recoverable_clients;
787         atomic_t                obd_connected_clients;
788         int                     obd_stale_clients;
789         int                     obd_delayed_clients;
790         /* this lock protects all recovery list_heads, timer and
791          * obd_next_recovery_transno value */
792         spinlock_t                       obd_recovery_task_lock;
793         __u64                            obd_next_recovery_transno;
794         int                              obd_replayed_requests;
795         int                              obd_requests_queued_for_recovery;
796         wait_queue_head_t                obd_next_transno_waitq;
797         /* protected by obd_recovery_task_lock */
798         struct timer_list                obd_recovery_timer;
799         time_t                           obd_recovery_start; /* seconds */
800         time_t                           obd_recovery_end; /* seconds, for lprocfs_status */
801         int                              obd_recovery_time_hard;
802         int                              obd_recovery_timeout;
803         int                              obd_recovery_ir_factor;
804
805         /* new recovery stuff from CMD2 */
806         struct target_recovery_data      obd_recovery_data;
807         int                              obd_replayed_locks;
808         atomic_t                     obd_req_replay_clients;
809         atomic_t                     obd_lock_replay_clients;
810         /* all lists are protected by obd_recovery_task_lock */
811         cfs_list_t                       obd_req_replay_queue;
812         cfs_list_t                       obd_lock_replay_queue;
813         cfs_list_t                       obd_final_req_queue;
814
815         union {
816 #ifdef HAVE_SERVER_SUPPORT
817                 struct obd_device_target obt;
818                 struct filter_obd filter;
819                 struct ost_obd ost;
820                 struct echo_obd echo;
821 #endif
822                 struct client_obd cli;
823                 struct echo_client_obd echo_client;
824                 struct lov_obd lov;
825                 struct lmv_obd lmv;
826         } u;
827         /* Fields used by LProcFS */
828         unsigned int           obd_cntr_base;
829         struct lprocfs_stats  *obd_stats;
830
831         unsigned int           obd_md_cntr_base;
832         struct lprocfs_stats  *obd_md_stats;
833
834         struct proc_dir_entry   *obd_proc_entry;
835         struct proc_dir_entry   *obd_proc_exports_entry;
836         struct proc_dir_entry   *obd_svc_procroot;
837         struct lprocfs_stats    *obd_svc_stats;
838         struct lprocfs_seq_vars *obd_vars;
839         atomic_t                obd_evict_inprogress;
840         wait_queue_head_t       obd_evict_inprogress_waitq;
841         cfs_list_t              obd_evict_list; /* protected with pet_lock */
842
843         /**
844          * Ldlm pool part. Save last calculated SLV and Limit.
845          */
846         rwlock_t                obd_pool_lock;
847         int                    obd_pool_limit;
848         __u64                  obd_pool_slv;
849
850         /**
851          * A list of outstanding class_incref()'s against this obd. For
852          * debugging.
853          */
854         struct lu_ref          obd_reference;
855
856         int                    obd_conn_inprogress;
857 };
858
859 #define OBD_LLOG_FL_SENDNOW     0x0001
860 #define OBD_LLOG_FL_EXIT        0x0002
861
862 enum obd_cleanup_stage {
863 /* Special case hack for MDS LOVs */
864         OBD_CLEANUP_EARLY,
865 /* can be directly mapped to .ldto_device_fini() */
866         OBD_CLEANUP_EXPORTS,
867 };
868
869 /* get/set_info keys */
870 #define KEY_ASYNC               "async"
871 #define KEY_BLOCKSIZE_BITS      "blocksize_bits"
872 #define KEY_BLOCKSIZE           "blocksize"
873 #define KEY_CAPA_KEY            "capa_key"
874 #define KEY_CHANGELOG_CLEAR     "changelog_clear"
875 #define KEY_FID2PATH            "fid2path"
876 #define KEY_CHECKSUM            "checksum"
877 #define KEY_CLEAR_FS            "clear_fs"
878 #define KEY_CONN_DATA           "conn_data"
879 #define KEY_EVICT_BY_NID        "evict_by_nid"
880 #define KEY_FIEMAP              "fiemap"
881 #define KEY_FLUSH_CTX           "flush_ctx"
882 #define KEY_GRANT_SHRINK        "grant_shrink"
883 #define KEY_HSM_COPYTOOL_SEND   "hsm_send"
884 #define KEY_INIT_RECOV_BACKUP   "init_recov_bk"
885 #define KEY_INIT_RECOV          "initial_recov"
886 #define KEY_INTERMDS            "inter_mds"
887 #define KEY_LAST_ID             "last_id"
888 #define KEY_LAST_FID            "last_fid"
889 #define KEY_LOCK_TO_STRIPE      "lock_to_stripe"
890 #define KEY_LOVDESC             "lovdesc"
891 #define KEY_LOV_IDX             "lov_idx"
892 #define KEY_MAX_EASIZE          "max_easize"
893 #define KEY_DEFAULT_EASIZE      "default_easize"
894 #define KEY_MAX_COOKIESIZE      "max_cookiesize"
895 #define KEY_DEFAULT_COOKIESIZE  "default_cookiesize"
896 #define KEY_MDS_CONN            "mds_conn"
897 #define KEY_MGSSEC              "mgssec"
898 #define KEY_NEXT_ID             "next_id"
899 #define KEY_READ_ONLY           "read-only"
900 #define KEY_REGISTER_TARGET     "register_target"
901 #define KEY_SET_FS              "set_fs"
902 #define KEY_TGT_COUNT           "tgt_count"
903 /*      KEY_SET_INFO in lustre_idl.h */
904 #define KEY_SPTLRPC_CONF        "sptlrpc_conf"
905 #define KEY_CONNECT_FLAG        "connect_flags"
906 #define KEY_SYNC_LOCK_CANCEL    "sync_lock_cancel"
907
908 #define KEY_CACHE_SET           "cache_set"
909 #define KEY_CACHE_LRU_SHRINK    "cache_lru_shrink"
910 #define KEY_CHANGELOG_INDEX     "changelog_index"
911 #define KEY_OSP_CONNECTED       "osp_connected"
912
913 struct lu_context;
914
915 /* /!\ must be coherent with include/linux/namei.h on patched kernel */
916 #define IT_OPEN     (1 << 0)
917 #define IT_CREAT    (1 << 1)
918 #define IT_READDIR  (1 << 2)
919 #define IT_GETATTR  (1 << 3)
920 #define IT_LOOKUP   (1 << 4)
921 #define IT_UNLINK   (1 << 5)
922 #define IT_TRUNC    (1 << 6)
923 #define IT_GETXATTR (1 << 7)
924 #define IT_EXEC     (1 << 8)
925 #define IT_PIN      (1 << 9)
926 #define IT_LAYOUT   (1 << 10)
927 #define IT_QUOTA_DQACQ (1 << 11)
928 #define IT_QUOTA_CONN  (1 << 12)
929 #define IT_SETXATTR (1 << 13)
930
931 static inline int it_to_lock_mode(struct lookup_intent *it)
932 {
933         /* CREAT needs to be tested before open (both could be set) */
934         if (it->it_op & IT_CREAT)
935                 return LCK_CW;
936         else if (it->it_op & (IT_GETATTR | IT_OPEN | IT_LOOKUP |
937                               IT_LAYOUT))
938                 return LCK_CR;
939         else if (it->it_op &  IT_READDIR)
940                 return LCK_PR;
941         else if (it->it_op &  IT_GETXATTR)
942                 return LCK_PR;
943         else if (it->it_op &  IT_SETXATTR)
944                 return LCK_PW;
945
946         LASSERTF(0, "Invalid it_op: %d\n", it->it_op);
947         return -EINVAL;
948 }
949
950 struct md_op_data {
951         struct lu_fid           op_fid1; /* operation fid1 (usualy parent) */
952         struct lu_fid           op_fid2; /* operation fid2 (usualy child) */
953         struct lu_fid           op_fid3; /* 2 extra fids to find conflicting */
954         struct lu_fid           op_fid4; /* to the operation locks. */
955         mdsno_t                 op_mds;  /* what mds server open will go to */
956         struct lustre_handle    op_handle;
957         obd_time                op_mod_time;
958         const char             *op_name;
959         int                     op_namelen;
960         __u32                   op_mode;
961         struct lmv_stripe_md   *op_mea1;
962         struct lmv_stripe_md   *op_mea2;
963         __u32                   op_suppgids[2];
964         __u32                   op_fsuid;
965         __u32                   op_fsgid;
966         cfs_cap_t               op_cap;
967         void                   *op_data;
968
969         /* iattr fields and blocks. */
970         struct iattr            op_attr;
971 #ifdef __KERNEL__
972         unsigned int            op_attr_flags;
973 #endif
974         __u64                   op_valid;
975         loff_t                  op_attr_blocks;
976
977         /* Size-on-MDS epoch and flags. */
978         __u64                   op_ioepoch;
979         __u32                   op_flags;
980
981         /* Capa fields */
982         struct obd_capa        *op_capa1;
983         struct obd_capa        *op_capa2;
984
985         /* Various operation flags. */
986         enum mds_op_bias        op_bias;
987
988         /* Used by readdir */
989         __u64                   op_hash_offset;
990
991         /* Used by readdir */
992         __u32                   op_npages;
993
994         /* used to transfer info between the stacks of MD client
995          * see enum op_cli_flags */
996         __u32                   op_cli_flags;
997
998         /* File object data version for HSM release, on client */
999         __u64                   op_data_version;
1000         struct lustre_handle    op_lease_handle;
1001 };
1002
1003 #define op_stripe_offset        op_ioepoch
1004 #define op_max_pages            op_valid
1005
1006 struct md_callback {
1007         int (*md_blocking_ast)(struct ldlm_lock *lock,
1008                                struct ldlm_lock_desc *desc,
1009                                void *data, int flag);
1010 };
1011
1012 struct md_enqueue_info;
1013 /* metadata stat-ahead */
1014 typedef int (* md_enqueue_cb_t)(struct ptlrpc_request *req,
1015                                 struct md_enqueue_info *minfo,
1016                                 int rc);
1017
1018 struct md_enqueue_info {
1019         struct md_op_data       mi_data;
1020         struct lookup_intent    mi_it;
1021         struct lustre_handle    mi_lockh;
1022         struct inode           *mi_dir;
1023         md_enqueue_cb_t         mi_cb;
1024         __u64                   mi_cbdata;
1025         unsigned int            mi_generation;
1026 };
1027
1028 struct obd_ops {
1029         struct module *o_owner;
1030         int (*o_iocontrol)(unsigned int cmd, struct obd_export *exp, int len,
1031                            void *karg, void *uarg);
1032         int (*o_get_info)(const struct lu_env *env, struct obd_export *,
1033                           __u32 keylen, void *key, __u32 *vallen, void *val,
1034                           struct lov_stripe_md *lsm);
1035         int (*o_set_info_async)(const struct lu_env *, struct obd_export *,
1036                                 __u32 keylen, void *key,
1037                                 __u32 vallen, void *val,
1038                                 struct ptlrpc_request_set *set);
1039         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
1040         int (*o_detach)(struct obd_device *dev);
1041         int (*o_setup) (struct obd_device *dev, struct lustre_cfg *cfg);
1042         int (*o_precleanup)(struct obd_device *dev,
1043                             enum obd_cleanup_stage cleanup_stage);
1044         int (*o_cleanup)(struct obd_device *dev);
1045         int (*o_process_config)(struct obd_device *dev, obd_count len,
1046                                 void *data);
1047         int (*o_postrecov)(struct obd_device *dev);
1048         int (*o_add_conn)(struct obd_import *imp, struct obd_uuid *uuid,
1049                           int priority);
1050         int (*o_del_conn)(struct obd_import *imp, struct obd_uuid *uuid);
1051         /* connect to the target device with given connection
1052          * data. @ocd->ocd_connect_flags is modified to reflect flags actually
1053          * granted by the target, which are guaranteed to be a subset of flags
1054          * asked for. If @ocd == NULL, use default parameters. */
1055         int (*o_connect)(const struct lu_env *env,
1056                          struct obd_export **exp, struct obd_device *src,
1057                          struct obd_uuid *cluuid, struct obd_connect_data *ocd,
1058                          void *localdata);
1059         int (*o_reconnect)(const struct lu_env *env,
1060                            struct obd_export *exp, struct obd_device *src,
1061                            struct obd_uuid *cluuid,
1062                            struct obd_connect_data *ocd,
1063                            void *localdata);
1064         int (*o_disconnect)(struct obd_export *exp);
1065
1066         /* Initialize/finalize fids infrastructure. */
1067         int (*o_fid_init)(struct obd_device *obd,
1068                           struct obd_export *exp, enum lu_cli_type type);
1069         int (*o_fid_fini)(struct obd_device *obd);
1070
1071         /* Allocate new fid according to passed @hint. */
1072         int (*o_fid_alloc)(struct obd_export *exp, struct lu_fid *fid,
1073                            struct md_op_data *op_data);
1074
1075         /*
1076          * Object with @fid is getting deleted, we may want to do something
1077          * about this.
1078          */
1079         int (*o_statfs)(const struct lu_env *, struct obd_export *exp,
1080                         struct obd_statfs *osfs, __u64 max_age, __u32 flags);
1081         int (*o_statfs_async)(struct obd_export *exp, struct obd_info *oinfo,
1082                               __u64 max_age, struct ptlrpc_request_set *set);
1083         int (*o_packmd)(struct obd_export *exp, struct lov_mds_md **disk_tgt,
1084                         struct lov_stripe_md *mem_src);
1085         int (*o_unpackmd)(struct obd_export *exp,struct lov_stripe_md **mem_tgt,
1086                           struct lov_mds_md *disk_src, int disk_len);
1087         int (*o_create)(const struct lu_env *env, struct obd_export *exp,
1088                         struct obdo *oa, struct lov_stripe_md **ea,
1089                         struct obd_trans_info *oti);
1090         int (*o_create_async)(struct obd_export *exp,  struct obd_info *oinfo,
1091                               struct lov_stripe_md **ea,
1092                               struct obd_trans_info *oti);
1093         int (*o_destroy)(const struct lu_env *env, struct obd_export *exp,
1094                          struct obdo *oa, struct lov_stripe_md *ea,
1095                          struct obd_trans_info *oti, struct obd_export *md_exp,
1096                          void *capa);
1097         int (*o_setattr)(const struct lu_env *, struct obd_export *exp,
1098                          struct obd_info *oinfo, struct obd_trans_info *oti);
1099         int (*o_setattr_async)(struct obd_export *exp, struct obd_info *oinfo,
1100                                struct obd_trans_info *oti,
1101                                struct ptlrpc_request_set *rqset);
1102         int (*o_getattr)(const struct lu_env *env, struct obd_export *exp,
1103                          struct obd_info *oinfo);
1104         int (*o_getattr_async)(struct obd_export *exp, struct obd_info *oinfo,
1105                                struct ptlrpc_request_set *set);
1106         int (*o_preprw)(const struct lu_env *env, int cmd,
1107                         struct obd_export *exp, struct obdo *oa, int objcount,
1108                         struct obd_ioobj *obj, struct niobuf_remote *remote,
1109                         int *nr_pages, struct niobuf_local *local,
1110                         struct obd_trans_info *oti, struct lustre_capa *capa);
1111         int (*o_commitrw)(const struct lu_env *env, int cmd,
1112                           struct obd_export *exp, struct obdo *oa,
1113                           int objcount, struct obd_ioobj *obj,
1114                           struct niobuf_remote *remote, int pages,
1115                           struct niobuf_local *local,
1116                           struct obd_trans_info *oti, int rc);
1117         int (*o_change_cbdata)(struct obd_export *, struct lov_stripe_md *,
1118                                ldlm_iterator_t it, void *data);
1119         int (*o_find_cbdata)(struct obd_export *, struct lov_stripe_md *,
1120                              ldlm_iterator_t it, void *data);
1121         int (*o_init_export)(struct obd_export *exp);
1122         int (*o_destroy_export)(struct obd_export *exp);
1123
1124         /* llog related obd_methods */
1125         int (*o_llog_init)(struct obd_device *obd, struct obd_llog_group *grp,
1126                            struct obd_device *disk_obd, int *idx);
1127         int (*o_llog_finish)(struct obd_device *obd, int count);
1128         int (*o_llog_connect)(struct obd_export *, struct llogd_conn_body *);
1129
1130         /* metadata-only methods */
1131         int (*o_pin)(struct obd_export *, const struct lu_fid *fid,
1132                      struct obd_capa *, struct obd_client_handle *, int flag);
1133         int (*o_unpin)(struct obd_export *, struct obd_client_handle *, int);
1134
1135         int (*o_import_event)(struct obd_device *, struct obd_import *,
1136                               enum obd_import_event);
1137
1138         int (*o_notify)(struct obd_device *obd, struct obd_device *watched,
1139                         enum obd_notify_event ev, void *data);
1140
1141         int (*o_health_check)(const struct lu_env *env, struct obd_device *);
1142         struct obd_uuid *(*o_get_uuid) (struct obd_export *exp);
1143
1144         /* quota methods */
1145         int (*o_quotacheck)(struct obd_device *, struct obd_export *,
1146                             struct obd_quotactl *);
1147         int (*o_quotactl)(struct obd_device *, struct obd_export *,
1148                           struct obd_quotactl *);
1149
1150         int (*o_ping)(const struct lu_env *, struct obd_export *exp);
1151
1152         /* pools methods */
1153         int (*o_pool_new)(struct obd_device *obd, char *poolname);
1154         int (*o_pool_del)(struct obd_device *obd, char *poolname);
1155         int (*o_pool_add)(struct obd_device *obd, char *poolname,
1156                           char *ostname);
1157         int (*o_pool_rem)(struct obd_device *obd, char *poolname,
1158                           char *ostname);
1159         void (*o_getref)(struct obd_device *obd);
1160         void (*o_putref)(struct obd_device *obd);
1161         /*
1162          * NOTE: If adding ops, add another LPROCFS_OBD_OP_INIT() line
1163          * to lprocfs_alloc_obd_stats() in obdclass/lprocfs_status.c.
1164          * Also, add a wrapper function in include/linux/obd_class.h. */
1165 };
1166
1167 /* lmv structures */
1168 struct lustre_md {
1169         struct mdt_body         *body;
1170         struct lov_stripe_md    *lsm;
1171         struct lmv_stripe_md    *lmv;
1172 #ifdef CONFIG_FS_POSIX_ACL
1173         struct posix_acl        *posix_acl;
1174 #endif
1175         struct mdt_remote_perm  *remote_perm;
1176         struct obd_capa         *mds_capa;
1177         struct obd_capa         *oss_capa;
1178         __u64                   lm_flags;
1179 };
1180
1181 struct md_open_data {
1182         struct obd_client_handle        *mod_och;
1183         struct ptlrpc_request           *mod_open_req;
1184         struct ptlrpc_request           *mod_close_req;
1185         atomic_t                         mod_refcount;
1186         bool                             mod_is_create;
1187 };
1188
1189 struct lookup_intent;
1190 struct cl_attr;
1191
1192 struct md_ops {
1193         /* Every operation from MD_STATS_FIRST_OP up to and including
1194          * MD_STATS_LAST_OP will be counted by EXP_MD_OP_INCREMENT()
1195          * and will appear in /proc/fs/lustre/{lmv,mdc}/.../md_stats.
1196          * Operations after MD_STATS_LAST_OP are excluded from stats.
1197          * There are a few reasons for doing this: we prune the 17
1198          * counters which will be of minimal use in understanding
1199          * metadata utilization, we save memory by allocating 15
1200          * instead of 32 counters, we save cycles by not counting.
1201          *
1202          * MD_STATS_FIRST_OP must be the first member of md_ops.
1203          */
1204 #define MD_STATS_FIRST_OP m_close
1205         int (*m_close)(struct obd_export *, struct md_op_data *,
1206                        struct md_open_data *, struct ptlrpc_request **);
1207
1208         int (*m_create)(struct obd_export *, struct md_op_data *,
1209                         const void *, int, int, __u32, __u32, cfs_cap_t,
1210                         __u64, struct ptlrpc_request **);
1211
1212         int (*m_enqueue)(struct obd_export *, struct ldlm_enqueue_info *,
1213                          struct lookup_intent *, struct md_op_data *,
1214                          struct lustre_handle *, void *, int,
1215                          struct ptlrpc_request **, __u64);
1216
1217         int (*m_getattr)(struct obd_export *, struct md_op_data *,
1218                          struct ptlrpc_request **);
1219
1220         int (*m_intent_lock)(struct obd_export *, struct md_op_data *,
1221                              void *, int, struct lookup_intent *, int,
1222                              struct ptlrpc_request **,
1223                              ldlm_blocking_callback, __u64);
1224
1225         int (*m_link)(struct obd_export *, struct md_op_data *,
1226                       struct ptlrpc_request **);
1227
1228         int (*m_rename)(struct obd_export *, struct md_op_data *,
1229                         const char *, int, const char *, int,
1230                         struct ptlrpc_request **);
1231
1232         int (*m_setattr)(struct obd_export *, struct md_op_data *, void *,
1233                          int , void *, int, struct ptlrpc_request **,
1234                          struct md_open_data **mod);
1235
1236         int (*m_fsync)(struct obd_export *, const struct lu_fid *,
1237                        struct obd_capa *, struct ptlrpc_request **);
1238
1239         int (*m_readpage)(struct obd_export *, struct md_op_data *,
1240                           struct page **, struct ptlrpc_request **);
1241
1242         int (*m_read_entry)(struct obd_export *, struct md_op_data *,
1243                             struct md_callback *cb_op, struct lu_dirent **ld,
1244                             struct page **ppage);
1245
1246         int (*m_unlink)(struct obd_export *, struct md_op_data *,
1247                         struct ptlrpc_request **);
1248
1249         int (*m_setxattr)(struct obd_export *, const struct lu_fid *,
1250                           struct obd_capa *, obd_valid, const char *,
1251                           const char *, int, int, int, __u32,
1252                           struct ptlrpc_request **);
1253
1254         int (*m_getxattr)(struct obd_export *, const struct lu_fid *,
1255                           struct obd_capa *, obd_valid, const char *,
1256                           const char *, int, int, int,
1257                           struct ptlrpc_request **);
1258
1259         int (*m_intent_getattr_async)(struct obd_export *,
1260                                       struct md_enqueue_info *,
1261                                       struct ldlm_enqueue_info *);
1262
1263         int (*m_revalidate_lock)(struct obd_export *, struct lookup_intent *,
1264                                  struct lu_fid *, __u64 *bits);
1265 #define MD_STATS_LAST_OP m_revalidate_lock
1266
1267         int (*m_getstatus)(struct obd_export *, struct lu_fid *,
1268                            struct obd_capa **);
1269
1270         int (*m_null_inode)(struct obd_export *, const struct lu_fid *);
1271
1272         int (*m_find_cbdata)(struct obd_export *, const struct lu_fid *,
1273                              ldlm_iterator_t, void *);
1274
1275         int (*m_done_writing)(struct obd_export *, struct md_op_data  *,
1276                               struct md_open_data *);
1277
1278         int (*m_getattr_name)(struct obd_export *, struct md_op_data *,
1279                               struct ptlrpc_request **);
1280
1281         int (*m_is_subdir)(struct obd_export *, const struct lu_fid *,
1282                            const struct lu_fid *,
1283                            struct ptlrpc_request **);
1284
1285         int (*m_init_ea_size)(struct obd_export *, int, int, int, int);
1286
1287         int (*m_get_lustre_md)(struct obd_export *, struct ptlrpc_request *,
1288                                struct obd_export *, struct obd_export *,
1289                                struct lustre_md *);
1290
1291         int (*m_free_lustre_md)(struct obd_export *, struct lustre_md *);
1292
1293         int (*m_merge_attr)(struct obd_export *,
1294                             const struct lmv_stripe_md *lsm,
1295                             struct cl_attr *attr);
1296
1297         int (*m_update_lsm_md)(struct obd_export *, struct lmv_stripe_md *lsm,
1298                                struct mdt_body *, ldlm_blocking_callback);
1299
1300         int (*m_set_open_replay_data)(struct obd_export *,
1301                                       struct obd_client_handle *,
1302                                       struct lookup_intent *);
1303
1304         int (*m_clear_open_replay_data)(struct obd_export *,
1305                                         struct obd_client_handle *);
1306
1307         int (*m_set_lock_data)(struct obd_export *, __u64 *, void *, __u64 *);
1308
1309         ldlm_mode_t (*m_lock_match)(struct obd_export *, __u64,
1310                                     const struct lu_fid *, ldlm_type_t,
1311                                     ldlm_policy_data_t *, ldlm_mode_t,
1312                                     struct lustre_handle *);
1313
1314         int (*m_cancel_unused)(struct obd_export *, const struct lu_fid *,
1315                                ldlm_policy_data_t *, ldlm_mode_t,
1316                                ldlm_cancel_flags_t flags, void *opaque);
1317
1318         int (*m_renew_capa)(struct obd_export *, struct obd_capa *oc,
1319                             renew_capa_cb_t cb);
1320
1321         int (*m_unpack_capa)(struct obd_export *, struct ptlrpc_request *,
1322                              const struct req_msg_field *, struct obd_capa **);
1323
1324         int (*m_get_remote_perm)(struct obd_export *, const struct lu_fid *,
1325                                  struct obd_capa *, __u32,
1326                                  struct ptlrpc_request **);
1327 };
1328
1329 struct lsm_operations {
1330         void (*lsm_free)(struct lov_stripe_md *);
1331         int (*lsm_destroy)(struct lov_stripe_md *, struct obdo *oa,
1332                            struct obd_export *md_exp);
1333         void (*lsm_stripe_by_index)(struct lov_stripe_md *, int *, obd_off *,
1334                                     obd_off *);
1335         void (*lsm_stripe_by_offset)(struct lov_stripe_md *, int *, obd_off *,
1336                                      obd_off *);
1337         int (*lsm_lmm_verify) (struct lov_mds_md *lmm, int lmm_bytes,
1338                                __u16 *stripe_count);
1339         int (*lsm_unpackmd) (struct lov_obd *lov, struct lov_stripe_md *lsm,
1340                              struct lov_mds_md *lmm);
1341 };
1342
1343 extern const struct lsm_operations lsm_v1_ops;
1344 extern const struct lsm_operations lsm_v3_ops;
1345 static inline const struct lsm_operations *lsm_op_find(int magic)
1346 {
1347         switch(magic) {
1348         case LOV_MAGIC_V1:
1349                return &lsm_v1_ops;
1350         case LOV_MAGIC_V3:
1351                return &lsm_v3_ops;
1352         default:
1353                CERROR("Cannot recognize lsm_magic %08x\n", magic);
1354                return NULL;
1355         }
1356 }
1357
1358 /* Requests for obd_extent_calc() */
1359 #define OBD_CALC_STRIPE_START   1
1360 #define OBD_CALC_STRIPE_END     2
1361
1362 static inline struct lustre_capa *oinfo_capa(struct obd_info *oinfo)
1363 {
1364         return oinfo->oi_capa;
1365 }
1366
1367 static inline struct md_open_data *obd_mod_alloc(void)
1368 {
1369         struct md_open_data *mod;
1370         OBD_ALLOC_PTR(mod);
1371         if (mod == NULL)
1372                 return NULL;
1373         atomic_set(&mod->mod_refcount, 1);
1374         return mod;
1375 }
1376
1377 #define obd_mod_get(mod) atomic_inc(&(mod)->mod_refcount)
1378 #define obd_mod_put(mod)                                          \
1379 ({                                                                \
1380         if (atomic_dec_and_test(&(mod)->mod_refcount)) {          \
1381                 if ((mod)->mod_open_req)                          \
1382                         ptlrpc_req_finished((mod)->mod_open_req); \
1383                 OBD_FREE_PTR(mod);                                \
1384         }                                                         \
1385 })
1386
1387 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid);
1388 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent);
1389
1390 /* return 1 if client should be resend request */
1391 static inline int client_should_resend(int resend, struct client_obd *cli)
1392 {
1393         return atomic_read(&cli->cl_resends) ?
1394                atomic_read(&cli->cl_resends) > resend : 1;
1395 }
1396
1397 /**
1398  * Return device name for this device
1399  *
1400  * XXX: lu_device is declared before obd_device, while a pointer pointing
1401  * back to obd_device in lu_device, so this helper function defines here
1402  * instead of in lu_object.h
1403  */
1404 static inline const char *lu_dev_name(const struct lu_device *lu_dev)
1405 {
1406         return lu_dev->ld_obd->obd_name;
1407 }
1408
1409 static inline bool filename_is_volatile(const char *name, int namelen, int *idx)
1410 {
1411         const char      *start;
1412         char            *end;
1413
1414         if (strncmp(name, LUSTRE_VOLATILE_HDR, LUSTRE_VOLATILE_HDR_LEN) != 0)
1415                 return false;
1416
1417         /* caller does not care of idx */
1418         if (idx == NULL)
1419                 return true;
1420
1421         /* volatile file, the MDT can be set from name */
1422         /* name format is LUSTRE_VOLATILE_HDR:[idx]: */
1423         /* if no MDT is specified, use std way */
1424         if (namelen < LUSTRE_VOLATILE_HDR_LEN + 2)
1425                 goto bad_format;
1426         /* test for no MDT idx case */
1427         if ((*(name + LUSTRE_VOLATILE_HDR_LEN) == ':') &&
1428             (*(name + LUSTRE_VOLATILE_HDR_LEN + 1) == ':')) {
1429                 *idx = -1;
1430                 return true;
1431         }
1432         /* we have an idx, read it */
1433         start = name + LUSTRE_VOLATILE_HDR_LEN + 1;
1434         *idx = strtoul(start, &end, 16);
1435         /* error cases:
1436          * no digit, no trailing :, negative value
1437          */
1438         if (((*idx == 0) && (end == start)) ||
1439             (*end != ':') || (*idx < 0))
1440                 goto bad_format;
1441
1442         return true;
1443 bad_format:
1444         /* bad format of mdt idx, we cannot return an error
1445          * to caller so we use hash algo */
1446         CERROR("Bad volatile file name format: %s\n",
1447                name + LUSTRE_VOLATILE_HDR_LEN);
1448         return false;
1449 }
1450
1451 static inline int cli_brw_size(struct obd_device *obd)
1452 {
1453         LASSERT(obd != NULL);
1454         return obd->u.cli.cl_max_pages_per_rpc << PAGE_CACHE_SHIFT;
1455 }
1456
1457 #endif /* __OBD_H */