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