Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #ifndef __OBD_H
33 #define __OBD_H
34
35 #include <linux/fs.h>
36 #include <linux/posix_acl.h>
37 #include <linux/kobject.h>
38 #include <linux/spinlock.h>
39 #include <linux/sysfs.h>
40 #include <libcfs/linux/xarray.h>
41
42 #include <uapi/linux/lustre/lustre_idl.h>
43 #include <lustre_lib.h>
44 #include <lu_ref.h>
45 #include <lustre_export.h>
46 #include <lustre_fid.h>
47 #include <lustre_fld.h>
48 #include <lustre_handles.h>
49 #include <lustre_intent.h>
50 #include <lvfs.h>
51
52 struct osc_async_rc {
53         int     ar_rc;
54         int     ar_force_sync;
55         __u64   ar_min_xid;
56 };
57
58 struct lov_oinfo {                 /* per-stripe data structure */
59         struct ost_id   loi_oi;    /* object ID/Sequence on the target OST */
60         int loi_ost_idx;           /* OST stripe index in lov_tgt_desc->tgts */
61         int loi_ost_gen;           /* generation of this loi_ost_idx */
62
63         unsigned long loi_kms_valid:1;
64         __u64 loi_kms;             /* known minimum size */
65         struct ost_lvb loi_lvb;
66         struct osc_async_rc     loi_ar;
67 };
68
69 void lov_fix_ea_for_replay(void *lovea);
70
71 static inline void loi_kms_set(struct lov_oinfo *oinfo, __u64 kms)
72 {
73         oinfo->loi_kms = kms;
74         oinfo->loi_kms_valid = 1;
75 }
76
77 struct lov_stripe_md;
78 struct obd_info;
79
80 typedef int (*obd_enqueue_update_f)(void *cookie, int rc);
81
82 /* obd info for a particular level (lov, osc). */
83 struct obd_info {
84         /* OBD_STATFS_* flags */
85         __u64                   oi_flags;
86         struct obd_device      *oi_obd;
87         struct lu_tgt_desc     *oi_tgt;
88         /* statfs data specific for every OSC, if needed at all. */
89         struct obd_statfs      *oi_osfs;
90         /* An update callback which is called to update some data on upper
91          * level. E.g. it is used for update lsm->lsm_oinfo at every received
92          * request in osc level for enqueue requests. It is also possible to
93          * update some caller data from LOV layer if needed. */
94         obd_enqueue_update_f    oi_cb_up;
95 };
96
97 struct obd_type {
98         const struct obd_ops    *typ_dt_ops;
99         const struct md_ops     *typ_md_ops;
100         struct proc_dir_entry   *typ_procroot;
101         struct dentry           *typ_debugfs_entry;
102 #ifdef HAVE_SERVER_SUPPORT
103         bool                     typ_sym_filter;
104 #endif
105         atomic_t                 typ_refcnt;
106         struct lu_device_type   *typ_lu;
107         struct kobject           typ_kobj;
108 };
109 #define typ_name typ_kobj.name
110 #define OBD_LU_TYPE_SETUP ((void *)0x01UL)
111
112 struct brw_page {
113         u64              bp_off;
114         struct page     *bp_page;
115         u32              bp_count;
116         u32              bp_flag;
117         /* used for encryption: difference with offset in clear text page */
118         u16              bp_off_diff;
119         /* used for encryption: difference with count in clear text page */
120         u16              bp_count_diff;
121 } __attribute__((packed));
122
123 struct timeout_item {
124         enum timeout_event ti_event;
125         time64_t           ti_timeout;
126         timeout_cb_t       ti_cb;
127         void              *ti_cb_data;
128         struct list_head   ti_obd_list;
129         struct list_head   ti_chain;
130 };
131
132 #define OBD_MAX_RIF_DEFAULT     8
133 #define OBD_MAX_RIF_MAX         512
134 #define OSC_MAX_RIF_MAX         256
135 #define OSC_MAX_DIRTY_DEFAULT   2000     /* Arbitrary large value */
136 #define OSC_MAX_DIRTY_MB_MAX    2048     /* arbitrary, but < MAX_LONG bytes */
137 #define OSC_DEFAULT_RESENDS     10
138
139 /* possible values for lut_sync_lock_cancel */
140 enum tgt_sync_lock_cancel {
141         SYNC_LOCK_CANCEL_NEVER    = 0,
142         SYNC_LOCK_CANCEL_BLOCKING = 1,
143         SYNC_LOCK_CANCEL_ALWAYS   = 2,
144 };
145
146 /*
147  * Limit reply buffer size for striping data to one x86_64 page. This
148  * value is chosen to fit the striping data for common use cases while
149  * staying well below the limit at which the buffer must be backed by
150  * vmalloc(). Excessive use of vmalloc() may cause spinlock contention
151  * on the MDS.
152  */
153 #define OBD_MAX_DEFAULT_EA_SIZE 4096
154
155 /*
156  * Lustre can handle larger xattrs internally, but we must respect the Linux
157  * VFS limitation or tools like tar cannot interact with Lustre volumes
158  * correctly.
159  */
160 #define OBD_MAX_EA_SIZE         XATTR_SIZE_MAX
161
162
163 enum obd_cl_sem_lock_class {
164         OBD_CLI_SEM_NORMAL,
165         OBD_CLI_SEM_MGC,
166         OBD_CLI_SEM_MDCOSC,
167 };
168
169 struct obd_import;
170 struct client_obd {
171         struct rw_semaphore      cl_sem;
172         struct obd_uuid          cl_target_uuid;
173         struct obd_import       *cl_import; /* ptlrpc connection state */
174         size_t                   cl_conn_count;
175
176         /* Cache maximum and default values for easize. This is
177          * strictly a performance optimization to minimize calls to
178          * obd_size_diskmd(). The default values are used to calculate the
179          * initial size of a request buffer. The ptlrpc layer will resize the
180          * buffer as needed to accommodate a larger reply from the
181          * server. The default values should be small enough to avoid wasted
182          * memory and excessive use of vmalloc(), yet large enough to avoid
183          * reallocating the buffer in the common use case. */
184
185         /* Default EA size for striping attributes. It is initialized at
186          * mount-time based on the default stripe width of the filesystem,
187          * then it tracks the largest observed EA size advertised by
188          * the MDT, up to a maximum value of OBD_MAX_DEFAULT_EA_SIZE. */
189         __u32                    cl_default_mds_easize;
190
191         /* Maximum possible EA size computed at mount-time based on
192          * the number of OSTs in the filesystem. May be increased at
193          * run-time if a larger observed size is advertised by the MDT. */
194         __u32                    cl_max_mds_easize;
195
196         /* Data-on-MDT specific value to set larger reply buffer for possible
197          * data read along with open/stat requests. By default it tries to use
198          * unused space in reply buffer.
199          * This value is used to ensure that reply buffer has at least as
200          * much free space as value indicates. That free space is gained from
201          * LOV EA buffer which is small for DoM files and on big systems can
202          * provide up to 32KB of extra space in reply buffer.
203          * Default value is 8K now.
204          */
205         __u32                    cl_dom_min_inline_repsize;
206
207         unsigned int             cl_checksum:1, /* 0 = disabled, 1 = enabled */
208                                  cl_checksum_dump:1, /* same */
209                                  cl_ocd_grant_param:1,
210                                  cl_lsom_update:1, /* send LSOM updates */
211                                  cl_root_squash:1, /* if root squash enabled*/
212                                  /* check prj quota for root */
213                                  cl_root_prjquota:1;
214         enum lustre_sec_part     cl_sp_me;
215         enum lustre_sec_part     cl_sp_to;
216         struct sptlrpc_flavor    cl_flvr_mgc; /* fixed flavor of mgc->mgs */
217
218         /* the grant values are protected by loi_list_lock below */
219         unsigned long            cl_dirty_pages;      /* all _dirty_ in pages */
220         unsigned long            cl_dirty_max_pages;  /* allowed w/o rpc */
221         unsigned long            cl_avail_grant;   /* bytes of credit for ost */
222         unsigned long            cl_lost_grant;    /* lost credits (trunc) */
223         /* grant consumed for dirty pages */
224         unsigned long            cl_dirty_grant;
225
226         /* since we allocate grant by blocks, we don't know how many grant will
227          * be used to add a page into cache. As a solution, we reserve maximum
228          * grant before trying to dirty a page and unreserve the rest.
229          * See osc_{reserve|unreserve}_grant for details. */
230         long                    cl_reserved_grant;
231         wait_queue_head_t       cl_cache_waiters; /* waiting for cache/grant */
232         time64_t                cl_next_shrink_grant;   /* seconds */
233         struct list_head        cl_grant_chain;
234         time64_t                cl_grant_shrink_interval; /* seconds */
235
236         /* A chunk is an optimal size used by osc_extent to determine
237          * the extent size. A chunk is max(PAGE_SIZE, OST block size) */
238         int                     cl_chunkbits;
239         /* extent insertion metadata overhead to be accounted in grant,
240          * in bytes */
241         unsigned int            cl_grant_extent_tax;
242         /* maximum extent size, in number of pages */
243         unsigned int            cl_max_extent_pages;
244
245         /* keep track of objects that have lois that contain pages which
246          * have been queued for async brw.  this lock also protects the
247          * lists of osc_client_pages that hang off of the loi */
248         /*
249          * ->cl_loi_list_lock protects consistency of
250          * ->cl_loi_{ready,read,write}_list. ->ap_make_ready() and
251          * ->ap_completion() call-backs are executed under this lock. As we
252          * cannot guarantee that these call-backs never block on all platforms
253          * (as a matter of fact they do block on Mac OS X), type of
254          * ->cl_loi_list_lock is platform dependent: it's a spin-lock on Linux
255          * and blocking mutex on Mac OS X. (Alternative is to make this lock
256          * blocking everywhere, but we don't want to slow down fast-path of
257          * our main platform.)
258          *
259          * NB by Jinshan: though field names are still _loi_, but actually
260          * osc_object{}s are in the list.
261          */
262         spinlock_t              cl_loi_list_lock;
263         struct list_head        cl_loi_ready_list;
264         struct list_head        cl_loi_hp_ready_list;
265         struct list_head        cl_loi_write_list;
266         struct list_head        cl_loi_read_list;
267         __u32                   cl_r_in_flight;
268         __u32                   cl_w_in_flight;
269         /* just a sum of the loi/lop pending numbers to be exported by /proc */
270         atomic_t                cl_pending_w_pages;
271         atomic_t                cl_pending_r_pages;
272         u32                     cl_max_pages_per_rpc;
273         u32                     cl_max_rpcs_in_flight;
274         u32                     cl_max_short_io_bytes;
275         ktime_t                 cl_stats_init;
276         struct obd_histogram    cl_read_rpc_hist;
277         struct obd_histogram    cl_write_rpc_hist;
278         struct obd_histogram    cl_read_page_hist;
279         struct obd_histogram    cl_write_page_hist;
280         struct obd_histogram    cl_read_offset_hist;
281         struct obd_histogram    cl_write_offset_hist;
282         ktime_t                 cl_batch_stats_init;
283         struct obd_histogram    cl_batch_rpc_hist;
284
285         /** LRU for osc caching pages */
286         struct cl_client_cache  *cl_cache;
287         /** member of cl_cache->ccc_lru */
288         struct list_head         cl_lru_osc;
289         /** # of available LRU slots left in the per-OSC cache.
290          * Available LRU slots are shared by all OSCs of the same file system,
291          * therefore this is a pointer to cl_client_cache::ccc_lru_left. */
292         atomic_long_t           *cl_lru_left;
293         /** # of busy LRU pages. A page is considered busy if it's in writeback
294          * queue, or in transfer. Busy pages can't be discarded so they are not
295          * in LRU cache. */
296         atomic_long_t            cl_lru_busy;
297         /** # of LRU pages in the cache for this client_obd */
298         atomic_long_t            cl_lru_in_list;
299         /** # of threads are shrinking LRU cache. To avoid contention, it's not
300          * allowed to have multiple threads shrinking LRU cache. */
301         atomic_t                 cl_lru_shrinkers;
302         /** The time when this LRU cache was last used. */
303         time64_t                 cl_lru_last_used;
304         /** stats: how many reclaims have happened for this client_obd.
305          * reclaim and shrink - shrink is async, voluntarily rebalancing;
306          * reclaim is sync, initiated by IO thread when the LRU slots are
307          * in shortage. */
308         __u64                    cl_lru_reclaim;
309         /** List of LRU pages for this client_obd */
310         struct list_head         cl_lru_list;
311         /** Lock for LRU page list */
312         spinlock_t               cl_lru_list_lock;
313         /** # of unstable pages in this client_obd.
314          * An unstable page is a page state that WRITE RPC has finished but
315          * the transaction has NOT yet committed. */
316         atomic_long_t            cl_unstable_count;
317         /** Link to osc_shrinker_list */
318         struct list_head         cl_shrink_list;
319
320         /* number of in flight destroy rpcs is limited to max_rpcs_in_flight */
321         atomic_t                 cl_destroy_in_flight;
322         wait_queue_head_t        cl_destroy_waitq;
323
324         /* modify rpcs in flight
325          * currently used for metadata only */
326         __u16                    cl_max_mod_rpcs_in_flight;
327         __u16                    cl_mod_rpcs_in_flight;
328         __u16                    cl_close_rpcs_in_flight;
329         wait_queue_head_t        cl_mod_rpcs_waitq;
330         unsigned long           *cl_mod_tag_bitmap;
331         ktime_t                  cl_mod_rpcs_init;
332         struct obd_histogram     cl_mod_rpcs_hist;
333
334         /* mgc datastruct */
335         struct mutex              cl_mgc_mutex;
336         struct local_oid_storage *cl_mgc_los;
337         struct dt_object         *cl_mgc_configs_dir;
338         struct obd_export        *cl_mgc_mgsexp;
339         atomic_t                  cl_mgc_refcount;
340         /* in-flight control list and total RPCs counter */
341         struct list_head         cl_flight_waiters;
342         __u32                    cl_rpcs_in_flight;
343
344         /* supported checksum types that are worked out at connect time */
345         __u32                    cl_supp_cksum_types;
346         /* checksum algorithm to be used */
347         enum cksum_types         cl_cksum_type;
348         /* preferred checksum algorithm to be used */
349         enum cksum_types         cl_preferred_cksum_type;
350
351         /* also protected by the poorly named _loi_list_lock lock above */
352         struct osc_async_rc      cl_ar;
353
354         /* sequence manager */
355         struct lu_client_seq    *cl_seq;
356         struct rw_semaphore      cl_seq_rwsem;
357
358         atomic_t                 cl_resends; /* resend count */
359
360         /* ptlrpc work for writeback in ptlrpcd context */
361         void                    *cl_writeback_work;
362         void                    *cl_lru_work;
363         struct mutex              cl_quota_mutex;
364         /* quota IDs/types that have exceeded quota */
365         struct xarray            cl_quota_exceeded_ids;
366         /* the xid of the request updating the hash tables */
367         __u64                    cl_quota_last_xid;
368         /* Links to the global list of registered changelog devices */
369         struct list_head         cl_chg_dev_linkage;
370 };
371 #define obd2cli_tgt(obd) ((char *)(obd)->u.cli.cl_target_uuid.uuid)
372
373 struct obd_id_info {
374         u32      idx;
375         u64     *data;
376 };
377
378 struct echo_client_obd {
379         struct obd_export      *ec_exp; /* the local connection to osc/lov */
380         spinlock_t              ec_lock;
381         struct list_head        ec_objects;
382         struct list_head        ec_locks;
383         __u64                   ec_unique;
384 };
385
386 /* allow statfs data caching for 1 second */
387 #define OBD_STATFS_CACHE_SECONDS 1
388 /* arbitrary maximum. larger would be useless, allows catching bogus input */
389 #define OBD_STATFS_CACHE_MAX_AGE 3600 /* seconds */
390
391 #define lov_tgt_desc lu_tgt_desc
392
393 struct lov_md_tgt_desc {
394         struct obd_device *lmtd_mdc;
395         __u32              lmtd_index;
396 };
397
398 struct lov_obd {
399         struct lov_desc         desc;
400         struct lov_tgt_desc   **lov_tgts;               /* sparse array */
401         struct lu_tgt_pool      lov_packed;             /* all OSTs in a packed
402                                                            array */
403         struct mutex            lov_lock;
404         struct obd_connect_data lov_ocd;
405         atomic_t                lov_refcount;
406         __u32                   lov_death_row;  /* tgts scheduled to be deleted */
407         __u32                   lov_tgt_size;   /* size of tgts array */
408         int                     lov_connects;
409         int                     lov_pool_count;
410         struct rhashtable       lov_pools_hash_body; /* used for key access */
411         struct list_head        lov_pool_list;  /* used for sequential access */
412         struct proc_dir_entry  *lov_pool_proc_entry;
413         enum lustre_sec_part    lov_sp_me;
414
415         /* Cached LRU and unstable data from upper layer */
416         struct cl_client_cache *lov_cache;
417
418         struct rw_semaphore     lov_notify_lock;
419         /* Data-on-MDT: MDC array */
420         struct lov_md_tgt_desc  *lov_mdc_tgts;
421
422         struct kobject          *lov_tgts_kobj;
423 };
424
425 #define lmv_tgt_desc lu_tgt_desc
426
427 struct qos_exclude_prefix {
428         struct list_head        qep_list;
429         struct rhash_head       qep_hash;
430         char                    qep_name[NAME_MAX + 1];
431 };
432
433 struct lmv_obd {
434         struct lu_client_fld    lmv_fld;
435         spinlock_t              lmv_lock;
436
437         int                     connected;
438         int                     max_easize;
439         int                     max_def_easize;
440         u32                     lmv_statfs_start;
441
442         struct lu_tgt_descs     lmv_mdt_descs;
443
444         struct obd_connect_data conn_data;
445         struct kobject          *lmv_tgts_kobj;
446         void                    *lmv_cache;
447
448         time64_t                lmv_setup_time;
449         __u32                   lmv_qos_rr_index; /* next round-robin MDT idx */
450         struct rhashtable       lmv_qos_exclude_hash;
451         struct list_head        lmv_qos_exclude_list;
452 };
453
454 #define lmv_mdt_count   lmv_mdt_descs.ltd_lmv_desc.ld_tgt_count
455 #define lmv_qos         lmv_mdt_descs.ltd_qos
456
457 /* Minimum sector size is 512 */
458 #define MAX_GUARD_NUMBER (PAGE_SIZE / 512)
459
460 struct niobuf_local {
461         __u64           lnb_file_offset;
462         __u32           lnb_page_offset;
463         __u32           lnb_len;
464         __u32           lnb_flags;
465         int             lnb_rc;
466         struct page     *lnb_page;
467         void            *lnb_data;
468         __be16          lnb_guards[MAX_GUARD_NUMBER];
469         __u16           lnb_guard_rpc:1;
470         __u16           lnb_guard_disk:1;
471         /* separate unlock for read path to allow shared access */
472         __u16           lnb_locked:1;
473 };
474
475 struct tgt_thread_big_cache {
476         struct niobuf_local     local[PTLRPC_MAX_BRW_PAGES];
477 };
478
479 #define LUSTRE_FLD_NAME         "fld"
480 #define LUSTRE_SEQ_NAME         "seq"
481
482 #define LUSTRE_MDD_NAME         "mdd"
483 #define LUSTRE_OSD_LDISKFS_NAME "osd-ldiskfs"
484 #define LUSTRE_OSD_ZFS_NAME     "osd-zfs"
485 #define LUSTRE_VVP_NAME         "vvp"
486 #define LUSTRE_LMV_NAME         "lmv"
487 #define LUSTRE_SLP_NAME         "slp"
488 #define LUSTRE_LOD_NAME         "lod"
489 #define LUSTRE_OSP_NAME         "osp"
490 #define LUSTRE_LWP_NAME         "lwp"
491
492 /* obd device type names */
493  /* FIXME all the references to LUSTRE_MDS_NAME should be swapped with LUSTRE_MDT_NAME */
494 #define LUSTRE_MDS_NAME         "mds"
495 #define LUSTRE_MDT_NAME         "mdt"
496 #define LUSTRE_MDC_NAME         "mdc"
497 #define LUSTRE_OSS_NAME         "ost"       /* FIXME change name to oss */
498 #define LUSTRE_OST_NAME         "obdfilter" /* FIXME change name to ost */
499 #define LUSTRE_OSC_NAME         "osc"
500 #define LUSTRE_LOV_NAME         "lov"
501 #define LUSTRE_MGS_NAME         "mgs"
502 #define LUSTRE_MGC_NAME         "mgc"
503
504 #define LUSTRE_ECHO_NAME        "obdecho"
505 #define LUSTRE_ECHO_CLIENT_NAME "echo_client"
506 #define LUSTRE_QMT_NAME         "qmt"
507
508 /* Constant obd names (post-rename) */
509 #define LUSTRE_MDS_OBDNAME "MDS"
510 #define LUSTRE_OSS_OBDNAME "OSS"
511 #define LUSTRE_MGS_OBDNAME "MGS"
512 #define LUSTRE_MGC_OBDNAME "MGC"
513
514 static inline int is_lwp_on_mdt(char *name)
515 {
516         char   *ptr;
517
518         ptr = strrchr(name, '-');
519         if (ptr == NULL) {
520                 CERROR("%s is not a obdname\n", name);
521                 return 0;
522         }
523
524         /* LWP name on MDT is fsname-MDTxxxx-lwp-MDTxxxx */
525
526         if (strncmp(ptr + 1, "MDT", 3) != 0)
527                 return 0;
528
529         while (*(--ptr) != '-' && ptr != name);
530
531         if (ptr == name)
532                 return 0;
533
534         if (strncmp(ptr + 1, LUSTRE_LWP_NAME, strlen(LUSTRE_LWP_NAME)) != 0)
535                 return 0;
536
537         return 1;
538 }
539
540 static inline int is_lwp_on_ost(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         /* LWP name on OST is fsname-MDTxxxx-lwp-OSTxxxx */
551
552         if (strncmp(ptr + 1, "OST", 3) != 0)
553                 return 0;
554
555         while (*(--ptr) != '-' && ptr != name);
556
557         if (ptr == name)
558                 return 0;
559
560         if (strncmp(ptr + 1, LUSTRE_LWP_NAME, strlen(LUSTRE_LWP_NAME)) != 0)
561                 return 0;
562
563         return 1;
564 }
565
566 /*
567  * Events signalled through obd_notify() upcall-chain.
568  */
569 enum obd_notify_event {
570         /* Device connect start */
571         OBD_NOTIFY_CONNECT,
572         /* Device activated */
573         OBD_NOTIFY_ACTIVE,
574         /* Device deactivated */
575         OBD_NOTIFY_INACTIVE,
576         /* Connect data for import were changed */
577         OBD_NOTIFY_OCD,
578         /* Administratively deactivate/activate event */
579         OBD_NOTIFY_DEACTIVATE,
580         OBD_NOTIFY_ACTIVATE
581 };
582
583 /*
584  * Data structure used to pass obd_notify()-event to non-obd listeners (llite
585  * being main example).
586  */
587 struct obd_notify_upcall {
588         int (*onu_upcall)(struct obd_device *host, struct obd_device *watched,
589                           enum obd_notify_event ev, void *owner);
590         /* Opaque datum supplied by upper layer listener */
591         void *onu_owner;
592 };
593
594 struct target_recovery_data {
595         svc_handler_t           trd_recovery_handler;
596         pid_t                   trd_processing_task;
597         struct completion       trd_starting;
598         struct completion       trd_finishing;
599 };
600
601 struct obd_llog_group {
602         struct llog_ctxt   *olg_ctxts[LLOG_MAX_CTXTS];
603         wait_queue_head_t  olg_waitq;
604         spinlock_t         olg_lock;
605 };
606
607 /* corresponds to one of the obd's */
608 #define OBD_DEVICE_MAGIC        0XAB5CD6EF
609
610 struct obd_device {
611         struct obd_type                 *obd_type;
612         __u32                            obd_magic; /* OBD_DEVICE_MAGIC */
613         int                              obd_minor; /* device number: lctl dl */
614         struct lu_device                *obd_lu_dev;
615
616         /* common and UUID name of this device */
617         struct obd_uuid                  obd_uuid;
618         char                             obd_name[MAX_OBD_NAME];
619
620         /* bitfield modification is protected by obd_dev_lock */
621         unsigned long
622                 obd_attached:1,         /* finished attach */
623                 obd_set_up:1,           /* finished setup */
624                 obd_recovering:1,       /* there are recoverable clients */
625                 obd_abort_recovery:1,   /* abort client and MDT recovery */
626                 obd_abort_mdt_recovery:1, /* abort recovery between MDTs */
627                 obd_version_recov:1,    /* obd uses version checking */
628                 obd_replayable:1,       /* recovery enabled; inform clients */
629                 obd_no_recov:1,         /* fail instead of retry messages */
630                 obd_stopping:1,         /* started cleanup */
631                 obd_starting:1,         /* started setup */
632                 obd_force:1,            /* cleanup with > 0 obd refcount */
633                 obd_fail:1,             /* cleanup with failover */
634                 obd_no_conn:1,          /* deny new connections */
635                 obd_inactive:1,         /* device active/inactive
636                                          * (for /proc/status only!!) */
637                 obd_no_ir:1,            /* no imperative recovery. */
638                 obd_process_conf:1,     /* device is processing mgs config */
639                 obd_checksum_dump:1,    /* dump pages upon cksum error */
640                 obd_dynamic_nids:1,     /* Allow dynamic NIDs on device */
641                 obd_read_only:1;        /* device is read-only */
642 #ifdef HAVE_SERVER_SUPPORT
643         /* no committed-transno notification */
644         unsigned long                   obd_no_transno:1;
645 #endif
646
647         /* use separate field as it is set in interrupt to don't mess with
648          * protection of other bits using _bh lock */
649         unsigned long obd_recovery_expired:1;
650         /* uuid-export hash body */
651         struct rhashtable               obd_uuid_hash;
652         /* nid-export hash body */
653         struct rhltable                 obd_nid_hash;
654         /* nid stats body */
655         struct cfs_hash             *obd_nid_stats_hash;
656         /* client_generation-export hash body */
657         struct cfs_hash             *obd_gen_hash;
658         struct list_head        obd_nid_stats;
659         struct list_head        obd_exports;
660         struct list_head        obd_unlinked_exports;
661         struct list_head        obd_delayed_exports;
662         struct list_head        obd_lwp_list;
663         struct kref             obd_refcount;
664         int                     obd_num_exports;
665         int                     obd_grant_check_threshold;
666         spinlock_t              obd_nid_lock;
667         struct ldlm_namespace  *obd_namespace;
668         struct ptlrpc_client    obd_ldlm_client; /* XXX OST/MDS only */
669         /* a spinlock is OK for what we do now, may need a semaphore later */
670         spinlock_t              obd_dev_lock; /* protect OBD bitfield above */
671         spinlock_t              obd_osfs_lock;
672         struct obd_statfs       obd_osfs;       /* locked by obd_osfs_lock */
673         time64_t                obd_osfs_age;
674         __u64                   obd_last_committed;
675         struct mutex            obd_dev_mutex;
676         struct lvfs_run_ctxt    obd_lvfs_ctxt;
677         struct obd_llog_group   obd_olg;        /* default llog group */
678         struct obd_device       *obd_observer;
679         struct rw_semaphore     obd_observer_link_sem;
680         struct obd_notify_upcall obd_upcall;
681         struct obd_export       *obd_self_export;
682         struct obd_export       *obd_lwp_export;
683         /* list of exports in LRU order, for ping evictor, with obd_dev_lock */
684         struct list_head        obd_exports_timed;
685         time64_t                obd_eviction_timer;     /* for ping evictor */
686
687         atomic_t                obd_max_recoverable_clients;
688         atomic_t                obd_connected_clients;
689         int                     obd_stale_clients;
690         /* this lock protects all recovery list_heads, timer and
691          * obd_next_recovery_transno value */
692         spinlock_t              obd_recovery_task_lock;
693         __u64                   obd_next_recovery_transno;
694         int                     obd_replayed_requests;
695         int                     obd_requests_queued_for_recovery;
696         wait_queue_head_t       obd_next_transno_waitq;
697         /* protected by obd_recovery_task_lock */
698         struct hrtimer          obd_recovery_timer;
699         /* seconds */
700         time64_t                obd_recovery_start;
701         /* seconds, for lprocfs_status */
702         time64_t                obd_recovery_end;
703         /* To tell timeouts from time stamps Lustre uses timeout_t
704          * instead of time64_t.
705          */
706         timeout_t                       obd_recovery_time_hard;
707         timeout_t                       obd_recovery_timeout;
708         int                             obd_recovery_ir_factor;
709
710         /* new recovery stuff from CMD2 */
711         int                             obd_replayed_locks;
712         atomic_t                        obd_req_replay_clients;
713         atomic_t                        obd_lock_replay_clients;
714         struct target_recovery_data     obd_recovery_data;
715
716         /* all lists are protected by obd_recovery_task_lock */
717         struct list_head                obd_req_replay_queue;
718         struct list_head                obd_lock_replay_queue;
719         struct list_head                obd_final_req_queue;
720
721         union {
722                 struct client_obd cli;
723                 struct echo_client_obd echo_client;
724                 struct lov_obd lov;
725                 struct lmv_obd lmv;
726         } u;
727
728         /* Fields used by LProcFS */
729         struct lprocfs_stats            *obd_stats;
730
731         struct lprocfs_stats            *obd_md_stats;
732
733         struct dentry                   *obd_debugfs_entry;
734         struct dentry                   *obd_debugfs_gss_dir;
735         struct proc_dir_entry   *obd_proc_entry;
736         struct proc_dir_entry   *obd_proc_exports_entry;
737         struct dentry                   *obd_svc_debugfs_entry;
738         struct lprocfs_stats    *obd_svc_stats;
739         const struct attribute         **obd_attrs;
740         struct lprocfs_vars     *obd_vars;
741         struct ldebugfs_vars    *obd_debugfs_vars;
742         struct list_head        obd_evict_list; /* protected with pet_lock */
743         atomic_t                obd_eviction_count;
744
745         /**
746          * LDLM pool part. Save last calculated SLV and Limit.
747          */
748         rwlock_t                        obd_pool_lock;
749         __u64                           obd_pool_slv;
750         int                             obd_pool_limit;
751
752         atomic_t                        obd_conn_inprogress;
753
754         /**
755          * List of outstanding class_incref()'s fo this OBD. For debugging. */
756         struct lu_ref                   obd_reference;
757
758         struct kset                     obd_kset; /* sysfs object collection */
759         struct kobj_type                obd_ktype;
760         struct completion               obd_kobj_unregister;
761
762         /* adaptive timeout parameters */
763         unsigned int                    obd_at_min;
764         unsigned int                    obd_at_max;
765         unsigned int                    obd_at_history;
766         unsigned int                    obd_ldlm_enqueue_min;
767 };
768
769 #define obd_get_at_min(obd) ({ \
770         struct obd_device *_obd = obd; \
771         _obd && _obd->obd_at_min ? _obd->obd_at_min : at_min; \
772 })
773 #define obd_get_at_max(obd) ({\
774         struct obd_device *_obd = obd; \
775         _obd && _obd->obd_at_max ? _obd->obd_at_max : at_max; \
776 })
777 #define obd_get_at_history(obd) ({ \
778         struct obd_device *_obd = obd; \
779         _obd && _obd->obd_at_history ? _obd->obd_at_history : at_history; \
780 })
781 extern unsigned int ldlm_enqueue_min;
782 #define obd_get_ldlm_enqueue_min(obd) ({ \
783         struct obd_device *_obd = obd; \
784         _obd && _obd->obd_ldlm_enqueue_min ? _obd->obd_ldlm_enqueue_min : \
785                                              ldlm_enqueue_min; \
786 })
787 #define obd_at_off(obd) (obd_get_at_max(obd) == 0)
788
789 #define obd_at_get(obd, at) ({ \
790         timeout_t t1 = obd_get_at_min(obd); \
791         max_t(timeout_t, (at)->at_current_timeout, t1); \
792 })
793
794 int obd_uuid_add(struct obd_device *obd, struct obd_export *export);
795 void obd_uuid_del(struct obd_device *obd, struct obd_export *export);
796 #ifdef HAVE_SERVER_SUPPORT
797 struct obd_export *obd_uuid_lookup(struct obd_device *obd,
798                                    struct obd_uuid *uuid);
799
800 int obd_nid_export_for_each(struct obd_device *obd, struct lnet_nid *nid,
801                             int cb(struct obd_export *exp, void *data),
802                             void *data);
803 int obd_nid_add(struct obd_device *obd, struct obd_export *exp);
804 void obd_nid_del(struct obd_device *obd, struct obd_export *exp);
805
806 /* both client and MDT recovery are aborted, or MDT is stopping  */
807 static inline bool obd_recovery_abort(struct obd_device *obd)
808 {
809         return obd->obd_stopping || obd->obd_abort_recovery;
810 }
811
812 /* MDT recovery is aborted, or MDT is stopping */
813 static inline bool obd_mdt_recovery_abort(struct obd_device *obd)
814 {
815         return obd->obd_stopping || obd->obd_abort_recovery ||
816                obd->obd_abort_mdt_recovery;
817 }
818 #endif
819
820 /* get/set_info keys */
821 #define KEY_ASYNC               "async"
822 #define KEY_CHANGELOG_CLEAR     "changelog_clear"
823 #define KEY_FID2PATH            "fid2path"
824 #define KEY_CHECKSUM            "checksum"
825 #define KEY_CLEAR_FS            "clear_fs"
826 #define KEY_CONN_DATA           "conn_data"
827 #define KEY_EVICT_BY_NID        "evict_by_nid"
828 #define KEY_FIEMAP              "fiemap"
829 #define KEY_FLUSH_CTX           "flush_ctx"
830 #define KEY_GRANT_SHRINK        "grant_shrink"
831 #define KEY_HSM_COPYTOOL_SEND   "hsm_send"
832 #define KEY_INIT_RECOV_BACKUP   "init_recov_bk"
833 #define KEY_INTERMDS            "inter_mds"
834 #define KEY_LAST_ID             "last_id"
835 #define KEY_LAST_FID            "last_fid"
836 #define KEY_MAX_EASIZE          "max_easize"
837 #define KEY_DEFAULT_EASIZE      "default_easize"
838 #define KEY_MGSSEC              "mgssec"
839 #define KEY_READ_ONLY           "read-only"
840 #define KEY_REGISTER_TARGET     "register_target"
841 #define KEY_SET_FS              "set_fs"
842 #define KEY_TGT_COUNT           "tgt_count"
843 /*      KEY_SET_INFO in lustre_idl.h */
844 #define KEY_SPTLRPC_CONF        "sptlrpc_conf"
845
846 #define KEY_CACHE_LRU_SHRINK    "cache_lru_shrink"
847 #define KEY_OSP_CONNECTED       "osp_connected"
848
849 /* Flags for op_xvalid */
850 enum op_xvalid {
851         OP_XVALID_CTIME_SET     = BIT(0),       /* 0x0001 */
852         OP_XVALID_BLOCKS        = BIT(1),       /* 0x0002 */
853         OP_XVALID_OWNEROVERRIDE = BIT(2),       /* 0x0004 */
854         OP_XVALID_FLAGS         = BIT(3),       /* 0x0008 */
855         OP_XVALID_PROJID        = BIT(4),       /* 0x0010 */
856         OP_XVALID_LAZYSIZE      = BIT(5),       /* 0x0020 */
857         OP_XVALID_LAZYBLOCKS    = BIT(6),       /* 0x0040 */
858 };
859
860 struct lu_context;
861
862 static inline int it_to_lock_mode(struct lookup_intent *it)
863 {
864         /* CREAT needs to be tested before open (both could be set) */
865         if (it->it_op & IT_CREAT)
866                 return LCK_CW;
867         else if (it->it_op & (IT_GETATTR | IT_OPEN | IT_LOOKUP))
868                 return LCK_CR;
869         else if (it->it_op & IT_LAYOUT)
870                 return (it->it_flags & FMODE_WRITE) ? LCK_EX : LCK_CR;
871         else if (it->it_op &  IT_READDIR)
872                 return LCK_PR;
873         else if (it->it_op &  IT_GETXATTR)
874                 return LCK_PR;
875
876         LASSERTF(0, "Invalid it_op: %d\n", it->it_op);
877         return -EINVAL;
878 }
879
880 enum md_op_flags {
881         MF_MDC_CANCEL_FID1      = BIT(0),
882         MF_MDC_CANCEL_FID2      = BIT(1),
883         MF_MDC_CANCEL_FID3      = BIT(2),
884         MF_MDC_CANCEL_FID4      = BIT(3),
885         MF_GET_MDT_IDX          = BIT(4),
886         MF_GETATTR_BY_FID       = BIT(5),
887         MF_QOS_MKDIR            = BIT(6),
888         MF_RR_MKDIR             = BIT(7),
889         MF_OPNAME_KMALLOCED     = BIT(8),
890 };
891
892 enum md_cli_flags {
893         CLI_SET_MEA     = BIT(0),
894         CLI_RM_ENTRY    = BIT(1),
895         CLI_HASH64      = BIT(2),
896         CLI_API32       = BIT(3),
897         CLI_MIGRATE     = BIT(4),
898         CLI_DIRTY_DATA  = BIT(5),
899         CLI_NO_SLOT     = BIT(6),
900 };
901
902 enum md_op_code {
903         LUSTRE_OPC_MKDIR = 1,
904         LUSTRE_OPC_SYMLINK,
905         LUSTRE_OPC_MKNOD,
906         LUSTRE_OPC_CREATE,
907         LUSTRE_OPC_ANY,
908         LUSTRE_OPC_LOOKUP,
909         LUSTRE_OPC_OPEN,
910 };
911
912 /**
913  * GETXATTR is not included as only a couple of fields in the reply body
914  * is filled, but not FID which is needed for common intent handling in
915  * mdc_finish_intent_lock()
916  */
917 static inline bool it_has_reply_body(const struct lookup_intent *it)
918 {
919         return it->it_op & (IT_OPEN | IT_LOOKUP | IT_GETATTR);
920 }
921
922 struct md_op_data {
923         struct lu_fid           op_fid1; /* operation fid1 (usualy parent) */
924         struct lu_fid           op_fid2; /* operation fid2 (usualy child) */
925         struct lu_fid           op_fid3; /* 2 extra fids to find conflicting */
926         struct lu_fid           op_fid4; /* to the operation locks. */
927         u32                     op_mds;  /* what mds server open will go to */
928         __u32                   op_mode;
929         enum md_op_code         op_code;
930         struct lustre_handle    op_open_handle;
931         s64                     op_mod_time;
932         const char              *op_name;
933         size_t                  op_namelen;
934         struct lmv_stripe_object *op_lso1;
935         struct lmv_stripe_object *op_lso2;
936         struct lmv_stripe_object *op_default_lso1; /* default LMV */
937         __u32                   op_suppgids[2];
938         __u32                   op_fsuid;
939         __u32                   op_fsgid;
940         kernel_cap_t            op_cap;
941         void                    *op_data;
942         size_t                  op_data_size;
943
944         /* iattr fields and blocks. */
945         struct iattr            op_attr;
946         enum op_xvalid          op_xvalid;      /* eXtra validity flags */
947         loff_t                  op_attr_blocks;
948         u64                     op_valid;       /* OBD_MD_* */
949         unsigned int            op_attr_flags;  /* LUSTRE_{SYNC,..}_FL */
950
951         enum md_op_flags        op_flags;
952
953         /* Various operation flags. */
954         enum mds_op_bias        op_bias;
955
956         /* used to transfer info between the stacks of MD client
957          * see enum op_cli_flags */
958         enum md_cli_flags       op_cli_flags;
959
960         /* File object data version for HSM release or migrate, on client */
961         __u64                   op_data_version;
962         __u64                   op_data_version2;
963         struct lustre_handle    op_lease_handle;
964
965         /* File security context, for creates/metadata ops */
966         const char             *op_file_secctx_name;
967         __u32                   op_file_secctx_name_size;
968         void                   *op_file_secctx;
969         __u32                   op_file_secctx_size;
970         int                     op_file_secctx_slot;
971
972         /* File encryption context, for creates/metadata ops */
973         void                   *op_file_encctx;
974         __u32                   op_file_encctx_size;
975
976         __u32                   op_projid;
977
978         union {
979                 /* Used by readdir */
980                 unsigned int    op_max_pages;
981                 /* mkdir */
982                 unsigned short  op_dir_depth;
983         };
984
985         __u16                   op_mirror_id;
986
987         /*
988          * used to access dir that is changing layout: if it's set, access
989          * dir by new layout, otherwise old layout.
990          * By default it's not set, because new files are created under new
991          * layout, if we can't find file with name under both old and new
992          * layout, we are sure file with name doesn't exist, but in reverse
993          * order there may be a race with creation by others.
994          */
995         bool                    op_new_layout;
996         /* used to access dir with bash hash */
997         __u32                   op_stripe_index;
998         /* Archive ID for PCC attach */
999         __u32                   op_archive_id;
1000 };
1001
1002 struct md_readdir_info {
1003         int (*mr_blocking_ast)(struct ldlm_lock *lock,
1004                                struct ldlm_lock_desc *desc,
1005                                void *data, int flag);
1006         /* if striped directory is partially read, the result is stored here */
1007         int mr_partial_readdir_rc;
1008 };
1009
1010 struct md_op_item;
1011 typedef int (*md_op_item_cb_t)(struct md_op_item *item, int rc);
1012
1013 enum md_item_opcode {
1014         MD_OP_NONE      = 0,
1015         MD_OP_GETATTR   = 1,
1016         MD_OP_MAX,
1017 };
1018
1019 struct md_op_item {
1020         enum md_item_opcode              mop_opc;
1021         struct md_op_data                mop_data;
1022         struct lookup_intent             mop_it;
1023         struct lustre_handle             mop_lockh;
1024         struct ldlm_enqueue_info         mop_einfo;
1025         md_op_item_cb_t                  mop_cb;
1026         void                            *mop_cbdata;
1027         struct inode                    *mop_dir;
1028         struct req_capsule              *mop_pill;
1029         struct work_struct               mop_work;
1030         __u64                            mop_lock_flags;
1031         unsigned int                     mop_subpill_allocated:1;
1032 };
1033
1034 enum lu_batch_flags {
1035         BATCH_FL_NONE   = 0x0,
1036         /* All requests in a batch are read-only. */
1037         BATCH_FL_RDONLY = 0x1,
1038         /* Will create PTLRPC request set for the batch. */
1039         BATCH_FL_RQSET  = 0x2,
1040         /* Whether need sync commit. */
1041         BATCH_FL_SYNC   = 0x4,
1042 };
1043
1044 struct lu_batch {
1045         struct ptlrpc_request_set       *lbt_rqset;
1046         __s32                            lbt_result;
1047         __u32                            lbt_flags;
1048         /* Max batched SUB requests count in a batch. */
1049         __u32                            lbt_max_count;
1050 };
1051
1052 struct batch_update_head {
1053         struct obd_export       *buh_exp;
1054         struct lu_batch         *buh_batch;
1055         int                      buh_flags;
1056         __u32                    buh_count;
1057         __u32                    buh_update_count;
1058         __u32                    buh_buf_count;
1059         __u32                    buh_reqsize;
1060         __u32                    buh_repsize;
1061         __u32                    buh_batchid;
1062         struct list_head         buh_buf_list;
1063         struct list_head         buh_cb_list;
1064 };
1065
1066 struct object_update_callback;
1067 typedef int (*object_update_interpret_t)(struct ptlrpc_request *req,
1068                                          struct lustre_msg *repmsg,
1069                                          struct object_update_callback *ouc,
1070                                          int rc);
1071
1072 struct object_update_callback {
1073         struct list_head                 ouc_item;
1074         object_update_interpret_t        ouc_interpret;
1075         struct batch_update_head        *ouc_head;
1076         void                            *ouc_data;
1077 };
1078
1079 typedef int (*md_update_pack_t)(struct batch_update_head *head,
1080                                 struct lustre_msg *reqmsg,
1081                                 size_t *max_pack_size,
1082                                 struct md_op_item *item);
1083
1084 struct cli_batch {
1085         struct lu_batch                   cbh_super;
1086         struct batch_update_head         *cbh_head;
1087 };
1088
1089 struct lu_batch *cli_batch_create(struct obd_export *exp,
1090                                   enum lu_batch_flags flags, __u32 max_count);
1091 int cli_batch_stop(struct obd_export *exp, struct lu_batch *bh);
1092 int cli_batch_flush(struct obd_export *exp, struct lu_batch *bh, bool wait);
1093 int cli_batch_add(struct obd_export *exp, struct lu_batch *bh,
1094                   struct md_op_item *item, md_update_pack_t packer,
1095                   object_update_interpret_t interpreter);
1096
1097 struct obd_ops {
1098         struct module *o_owner;
1099         int (*o_iocontrol)(unsigned int cmd, struct obd_export *exp, int len,
1100                            void *karg, void __user *uarg);
1101         int (*o_get_info)(const struct lu_env *env, struct obd_export *,
1102                           __u32 keylen, void *key, __u32 *vallen, void *val);
1103         int (*o_set_info_async)(const struct lu_env *, struct obd_export *,
1104                                 __u32 keylen, void *key,
1105                                 __u32 vallen, void *val,
1106                                 struct ptlrpc_request_set *set);
1107         int (*o_setup) (struct obd_device *obd, struct lustre_cfg *cfg);
1108         int (*o_precleanup)(struct obd_device *obd);
1109         int (*o_cleanup)(struct obd_device *obd);
1110         int (*o_process_config)(struct obd_device *obd, size_t len, void *data);
1111         int (*o_postrecov)(struct obd_device *obd);
1112         int (*o_add_conn)(struct obd_import *imp, struct obd_uuid *uuid,
1113                           int priority);
1114         int (*o_del_conn)(struct obd_import *imp, struct obd_uuid *uuid);
1115         /* connect to the target device with given connection
1116          * data. @ocd->ocd_connect_flags is modified to reflect flags actually
1117          * granted by the target, which are guaranteed to be a subset of flags
1118          * asked for. If @ocd == NULL, use default parameters.
1119          * On the client, localdata is a struct cl_client_cache *.
1120          * On the server, localdata is a struct lnet_nid *.
1121          */
1122         int (*o_connect)(const struct lu_env *env,
1123                          struct obd_export **exp, struct obd_device *src,
1124                          struct obd_uuid *cluuid, struct obd_connect_data *ocd,
1125                          void *localdata);
1126         int (*o_reconnect)(const struct lu_env *env,
1127                            struct obd_export *exp, struct obd_device *src,
1128                            struct obd_uuid *cluuid,
1129                            struct obd_connect_data *ocd,
1130                            void *localdata);
1131         int (*o_disconnect)(struct obd_export *exp);
1132
1133         /* Initialize/finalize fids infrastructure. */
1134         int (*o_fid_init)(struct obd_device *obd,
1135                           struct obd_export *exp, enum lu_cli_type type);
1136         int (*o_fid_fini)(struct obd_device *obd);
1137
1138         /* Allocate new fid according to passed @hint. */
1139         int (*o_fid_alloc)(const struct lu_env *env, struct obd_export *exp,
1140                            struct lu_fid *fid, struct md_op_data *op_data);
1141
1142         /*
1143          * Object with @fid is getting deleted, we may want to do something
1144          * about this.
1145          */
1146         int (*o_statfs)(const struct lu_env *, struct obd_export *exp,
1147                         struct obd_statfs *osfs, time64_t max_age, __u32 flags);
1148         int (*o_statfs_async)(struct obd_export *exp, struct obd_info *oinfo,
1149                               time64_t max_age, struct ptlrpc_request_set *set);
1150         int (*o_create)(const struct lu_env *env, struct obd_export *exp,
1151                         struct obdo *oa);
1152         int (*o_destroy)(const struct lu_env *env, struct obd_export *exp,
1153                          struct obdo *oa);
1154         int (*o_setattr)(const struct lu_env *, struct obd_export *exp,
1155                          struct obdo *oa);
1156         int (*o_getattr)(const struct lu_env *env, struct obd_export *exp,
1157                          struct obdo *oa);
1158         int (*o_preprw)(const struct lu_env *env, int cmd,
1159                         struct obd_export *exp, struct obdo *oa, int objcount,
1160                         struct obd_ioobj *obj, struct niobuf_remote *remote,
1161                         int *nr_pages, struct niobuf_local *local);
1162         int (*o_commitrw)(const struct lu_env *env, int cmd,
1163                           struct obd_export *exp, struct obdo *oa,
1164                           int objcount, struct obd_ioobj *obj,
1165                           struct niobuf_remote *remote, int pages,
1166                           struct niobuf_local *local, int rc, int nob,
1167                           ktime_t kstart);
1168         int (*o_init_export)(struct obd_export *exp);
1169         int (*o_destroy_export)(struct obd_export *exp);
1170
1171         int (*o_import_event)(struct obd_device *, struct obd_import *,
1172                               enum obd_import_event);
1173
1174         int (*o_notify)(struct obd_device *obd, struct obd_device *watched,
1175                         enum obd_notify_event ev);
1176
1177         int (*o_health_check)(const struct lu_env *env, struct obd_device *);
1178         struct obd_uuid *(*o_get_uuid) (struct obd_export *exp);
1179
1180         /* quota methods */
1181         int (*o_quotactl)(struct obd_device *, struct obd_export *,
1182                           struct obd_quotactl *);
1183
1184         /* pools methods */
1185         int (*o_pool_new)(struct obd_device *obd, char *poolname);
1186         int (*o_pool_del)(struct obd_device *obd, char *poolname);
1187         int (*o_pool_add)(struct obd_device *obd, char *poolname,
1188                           char *ostname);
1189         int (*o_pool_rem)(struct obd_device *obd, char *poolname,
1190                           char *ostname);
1191 };
1192
1193 /* lmv structures */
1194 struct lustre_md {
1195         struct mdt_body                 *body;
1196         struct lu_buf                   layout;
1197         struct lmv_stripe_object        *lsm_obj;
1198         struct lmv_stripe_object        *def_lsm_obj;
1199 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1200         struct posix_acl                *posix_acl;
1201 #endif
1202 };
1203
1204 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1205 static inline void lmd_clear_acl(struct lustre_md *md)
1206 {
1207         if (md->posix_acl) {
1208                 posix_acl_release(md->posix_acl);
1209                 md->posix_acl = NULL;
1210         }
1211 }
1212
1213 #define OBD_CONNECT_ACL_FLAGS  \
1214         (OBD_CONNECT_ACL | OBD_CONNECT_UMASK | OBD_CONNECT_LARGE_ACL)
1215 #else
1216 static inline void lmd_clear_acl(struct lustre_md *md)
1217 {
1218 }
1219
1220 #define OBD_CONNECT_ACL_FLAGS  (0)
1221 #endif
1222
1223 struct md_open_data {
1224         struct obd_client_handle        *mod_och;
1225         struct ptlrpc_request           *mod_open_req;
1226         struct ptlrpc_request           *mod_close_req;
1227         atomic_t                         mod_refcount;
1228         bool                             mod_is_create;
1229 };
1230
1231 struct obd_client_handle {
1232         struct lustre_handle     och_open_handle;
1233         struct lu_fid            och_fid;
1234         struct md_open_data     *och_mod;
1235         struct lustre_handle     och_lease_handle; /* open lock for lease */
1236         __u32                    och_magic;
1237         int                      och_flags;
1238 };
1239
1240 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
1241
1242 struct lookup_intent;
1243 struct cl_attr;
1244
1245 struct md_ops {
1246         int (*m_close)(struct obd_export *, struct md_op_data *,
1247                        struct md_open_data *, struct ptlrpc_request **);
1248
1249         int (*m_create)(struct obd_export *, struct md_op_data *,
1250                         const void *, size_t, umode_t, uid_t, gid_t,
1251                         kernel_cap_t, __u64, struct ptlrpc_request **);
1252
1253         int (*m_enqueue)(struct obd_export *, struct ldlm_enqueue_info *,
1254                          const union ldlm_policy_data *, struct md_op_data *,
1255                          struct lustre_handle *, __u64);
1256
1257         int (*m_getattr)(struct obd_export *, struct md_op_data *,
1258                          struct ptlrpc_request **);
1259
1260         int (*m_intent_lock)(struct obd_export *, struct md_op_data *,
1261                              struct lookup_intent *,
1262                              struct ptlrpc_request **,
1263                              ldlm_blocking_callback, __u64);
1264
1265         int (*m_link)(struct obd_export *, struct md_op_data *,
1266                       struct ptlrpc_request **);
1267
1268         int (*m_rename)(struct obd_export *, struct md_op_data *,
1269                         const char *, size_t, const char *, size_t,
1270                         struct ptlrpc_request **);
1271
1272         int (*m_setattr)(struct obd_export *, struct md_op_data *, void *,
1273                          size_t , struct ptlrpc_request **);
1274
1275         int (*m_fsync)(struct obd_export *, const struct lu_fid *,
1276                        struct ptlrpc_request **);
1277
1278         int (*m_read_page)(struct obd_export *, struct md_op_data *,
1279                            struct md_readdir_info *mrinfo, __u64 hash_offset,
1280                            struct page **ppage);
1281
1282         int (*m_unlink)(struct obd_export *, struct md_op_data *,
1283                         struct ptlrpc_request **);
1284
1285         int (*m_setxattr)(struct obd_export *, const struct lu_fid *,
1286                           u64, const char *, const void *, size_t, unsigned int,
1287                           u32, struct ptlrpc_request **);
1288
1289         int (*m_getxattr)(struct obd_export *, const struct lu_fid *,
1290                           u64, const char *, size_t, struct ptlrpc_request **);
1291
1292         int (*m_intent_getattr_async)(struct obd_export *,
1293                                       struct md_op_item *);
1294
1295         int (*m_revalidate_lock)(struct obd_export *, struct lookup_intent *,
1296                                  struct lu_fid *, __u64 *bits);
1297
1298         int (*m_file_resync)(struct obd_export *, struct md_op_data *);
1299
1300         int (*m_get_root)(struct obd_export *, const char *, struct lu_fid *);
1301         int (*m_null_inode)(struct obd_export *, const struct lu_fid *);
1302
1303         int (*m_getattr_name)(struct obd_export *, struct md_op_data *,
1304                               struct ptlrpc_request **);
1305
1306         int (*m_init_ea_size)(struct obd_export *, __u32, __u32);
1307
1308         int (*m_get_lustre_md)(struct obd_export *, struct req_capsule *,
1309                                struct obd_export *, struct obd_export *,
1310                                struct lustre_md *);
1311
1312         int (*m_put_lustre_md)(struct obd_export *, struct lustre_md *);
1313
1314         int (*m_merge_attr)(struct obd_export *,
1315                             const struct lmv_stripe_object *,
1316                             struct cl_attr *attr, ldlm_blocking_callback);
1317
1318         int (*m_set_open_replay_data)(struct obd_export *,
1319                                       struct obd_client_handle *,
1320                                       struct lookup_intent *);
1321
1322         int (*m_clear_open_replay_data)(struct obd_export *,
1323                                         struct obd_client_handle *);
1324
1325         int (*m_set_lock_data)(struct obd_export *,
1326                                const struct lustre_handle *, void *, __u64 *);
1327
1328         enum ldlm_mode (*m_lock_match)(struct obd_export *, __u64,
1329                                        const struct lu_fid *, enum ldlm_type,
1330                                        union ldlm_policy_data *, enum ldlm_mode,
1331                                        struct lustre_handle *);
1332
1333         int (*m_cancel_unused)(struct obd_export *, const struct lu_fid *,
1334                                union ldlm_policy_data *, enum ldlm_mode,
1335                                enum ldlm_cancel_flags flags, void *opaque);
1336
1337         int (*m_get_fid_from_lsm)(struct obd_export *,
1338                                   const struct lmv_stripe_object *,
1339                                   const char *name, int namelen,
1340                                   struct lu_fid *fid);
1341         int (*m_stripe_object_create)(struct obd_export *exp,
1342                                       struct lmv_stripe_object **plso,
1343                                       const union lmv_mds_md *lmv,
1344                                       size_t lmv_size);
1345         int (*m_rmfid)(struct obd_export *exp, struct fid_array *fa, int *rcs,
1346                        struct ptlrpc_request_set *set);
1347         struct lu_batch *(*m_batch_create)(struct obd_export *exp,
1348                                            enum lu_batch_flags flags,
1349                                            __u32 max_count);
1350         int (*m_batch_stop)(struct obd_export *exp, struct lu_batch *bh);
1351         int (*m_batch_flush)(struct obd_export *exp, struct lu_batch *bh,
1352                              bool wait);
1353         int (*m_batch_add)(struct obd_export *exp, struct lu_batch *bh,
1354                            struct md_op_item *item);
1355 };
1356
1357 static inline struct md_open_data *obd_mod_alloc(void)
1358 {
1359         struct md_open_data *mod;
1360         OBD_ALLOC_PTR(mod);
1361         if (mod == NULL)
1362                 return NULL;
1363         atomic_set(&mod->mod_refcount, 1);
1364         return mod;
1365 }
1366
1367 #define obd_mod_get(mod) atomic_inc(&(mod)->mod_refcount)
1368 #define obd_mod_put(mod)                                          \
1369 ({                                                                \
1370         if (atomic_dec_and_test(&(mod)->mod_refcount)) {          \
1371                 if ((mod)->mod_open_req)                          \
1372                         ptlrpc_req_finished((mod)->mod_open_req); \
1373                 if ((mod)->mod_close_req)                         \
1374                         ptlrpc_req_finished((mod)->mod_close_req);\
1375                 OBD_FREE_PTR(mod);                                \
1376         }                                                         \
1377 })
1378
1379 void obdo_from_inode(struct obdo *dst, struct inode *src, u64 valid);
1380 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent);
1381 void obdo_set_o_projid(struct obdo *dst, u32 projid);
1382
1383 /* return 1 if client should be resend request */
1384 static inline int client_should_resend(int resend, struct client_obd *cli)
1385 {
1386         return atomic_read(&cli->cl_resends) ?
1387                atomic_read(&cli->cl_resends) > resend : 1;
1388 }
1389
1390 /**
1391  * Return device name for this device
1392  *
1393  * XXX: lu_device is declared before obd_device, while a pointer pointing
1394  * back to obd_device in lu_device, so this helper function defines here
1395  * instead of in lu_object.h
1396  */
1397 static inline const char *lu_dev_name(const struct lu_device *lu_dev)
1398 {
1399         return lu_dev->ld_obd->obd_name;
1400 }
1401
1402 static inline bool filename_is_volatile(const char *name, size_t namelen,
1403                                         int *idx)
1404 {
1405         const char *start;
1406         int rnd, fd, rc;
1407
1408         if (strncmp(name, LUSTRE_VOLATILE_HDR, LUSTRE_VOLATILE_HDR_LEN) != 0)
1409                 return false;
1410
1411         /* caller does not care of idx */
1412         if (idx == NULL)
1413                 return true;
1414
1415         /* volatile file, the MDT can be set from name */
1416         /* name format is LUSTRE_VOLATILE_HDR:[idx]: */
1417         /* if no MDT is specified, use std way */
1418         if (namelen < LUSTRE_VOLATILE_HDR_LEN + 2)
1419                 goto bad_format;
1420         /* test for no MDT idx case */
1421         if ((*(name + LUSTRE_VOLATILE_HDR_LEN) == ':') &&
1422             (*(name + LUSTRE_VOLATILE_HDR_LEN + 1) == ':')) {
1423                 *idx = -1;
1424                 return true;
1425         }
1426         /* we have an idx, read it */
1427         start = name + LUSTRE_VOLATILE_HDR_LEN + 1;
1428         rc = sscanf(start, "%4x:%4x:fd=%2d", idx, &rnd, &fd);
1429         /* error cases: no digit or negative value
1430          * rc will never be larger then 3
1431          */
1432         if (rc <= 0 || *idx < 0)
1433                 goto bad_format;
1434
1435         return true;
1436 bad_format:
1437         /* bad format of mdt idx, we cannot return an error
1438          * to caller so we use hash algo */
1439         CERROR("Bad volatile file name format: %s\n",
1440                name + LUSTRE_VOLATILE_HDR_LEN);
1441         return false;
1442 }
1443
1444 static inline int cli_brw_size(struct obd_device *obd)
1445 {
1446         LASSERT(obd != NULL);
1447         return obd->u.cli.cl_max_pages_per_rpc << PAGE_SHIFT;
1448 }
1449
1450 /*
1451  * When RPC size or the max RPCs in flight is increased, the max dirty pages
1452  * of the client should be increased accordingly to avoid sending fragmented
1453  * RPCs over the network when the client runs out of the maximum dirty space
1454  * when so many RPCs are being generated.
1455  */
1456 static inline void client_adjust_max_dirty(struct client_obd *cli)
1457 {
1458          /* initializing */
1459         if (cli->cl_dirty_max_pages <= 0) {
1460                 cli->cl_dirty_max_pages =
1461                         (OSC_MAX_DIRTY_DEFAULT * 1024 * 1024) >> PAGE_SHIFT;
1462         } else {
1463                 unsigned long dirty_max = cli->cl_max_rpcs_in_flight *
1464                                           cli->cl_max_pages_per_rpc;
1465
1466                 if (dirty_max > cli->cl_dirty_max_pages)
1467                         cli->cl_dirty_max_pages = dirty_max;
1468         }
1469
1470         if (cli->cl_dirty_max_pages > cfs_totalram_pages() / 8)
1471                 cli->cl_dirty_max_pages = cfs_totalram_pages() / 8;
1472
1473         /* This value is exported to userspace through the max_dirty_mb
1474          * parameter.  So we round up the number of pages to make it a round
1475          * number of MBs. */
1476         cli->cl_dirty_max_pages = round_up(cli->cl_dirty_max_pages,
1477                                            1 << (20 - PAGE_SHIFT));
1478 }
1479
1480 /* Must be used for page cache pages only,
1481  * not safe otherwise (e.g. direct IO pages)
1482  */
1483 static inline struct inode *page2inode(struct page *page)
1484 {
1485         if (page->mapping) {
1486                 if (PageAnon(page))
1487                         return NULL;
1488                 else
1489                         return page->mapping->host;
1490         } else {
1491                 return NULL;
1492         }
1493 }
1494
1495 #endif /* __OBD_H */