Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * ocfs2.h
0004  *
0005  * Defines macros and structures used in OCFS2
0006  *
0007  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
0008  */
0009 
0010 #ifndef OCFS2_H
0011 #define OCFS2_H
0012 
0013 #include <linux/spinlock.h>
0014 #include <linux/sched.h>
0015 #include <linux/wait.h>
0016 #include <linux/list.h>
0017 #include <linux/llist.h>
0018 #include <linux/rbtree.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/kref.h>
0021 #include <linux/mutex.h>
0022 #include <linux/lockdep.h>
0023 #include <linux/jbd2.h>
0024 
0025 /* For union ocfs2_dlm_lksb */
0026 #include "stackglue.h"
0027 
0028 #include "ocfs2_fs.h"
0029 #include "ocfs2_lockid.h"
0030 #include "ocfs2_ioctl.h"
0031 
0032 /* For struct ocfs2_blockcheck_stats */
0033 #include "blockcheck.h"
0034 
0035 #include "reservations.h"
0036 
0037 #include "filecheck.h"
0038 
0039 /* Caching of metadata buffers */
0040 
0041 /* Most user visible OCFS2 inodes will have very few pieces of
0042  * metadata, but larger files (including bitmaps, etc) must be taken
0043  * into account when designing an access scheme. We allow a small
0044  * amount of inlined blocks to be stored on an array and grow the
0045  * structure into a rb tree when necessary. */
0046 #define OCFS2_CACHE_INFO_MAX_ARRAY 2
0047 
0048 /* Flags for ocfs2_caching_info */
0049 
0050 enum ocfs2_caching_info_flags {
0051     /* Indicates that the metadata cache is using the inline array */
0052     OCFS2_CACHE_FL_INLINE   = 1<<1,
0053 };
0054 
0055 struct ocfs2_caching_operations;
0056 struct ocfs2_caching_info {
0057     /*
0058      * The parent structure provides the locks, but because the
0059      * parent structure can differ, it provides locking operations
0060      * to struct ocfs2_caching_info.
0061      */
0062     const struct ocfs2_caching_operations *ci_ops;
0063 
0064     /* next two are protected by trans_inc_lock */
0065     /* which transaction were we created on? Zero if none. */
0066     unsigned long       ci_created_trans;
0067     /* last transaction we were a part of. */
0068     unsigned long       ci_last_trans;
0069 
0070     /* Cache structures */
0071     unsigned int        ci_flags;
0072     unsigned int        ci_num_cached;
0073     union {
0074     sector_t    ci_array[OCFS2_CACHE_INFO_MAX_ARRAY];
0075         struct rb_root  ci_tree;
0076     } ci_cache;
0077 };
0078 /*
0079  * Need this prototype here instead of in uptodate.h because journal.h
0080  * uses it.
0081  */
0082 struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci);
0083 
0084 /* this limits us to 256 nodes
0085  * if we need more, we can do a kmalloc for the map */
0086 #define OCFS2_NODE_MAP_MAX_NODES    256
0087 struct ocfs2_node_map {
0088     u16 num_nodes;
0089     unsigned long map[BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES)];
0090 };
0091 
0092 enum ocfs2_ast_action {
0093     OCFS2_AST_INVALID = 0,
0094     OCFS2_AST_ATTACH,
0095     OCFS2_AST_CONVERT,
0096     OCFS2_AST_DOWNCONVERT,
0097 };
0098 
0099 /* actions for an unlockast function to take. */
0100 enum ocfs2_unlock_action {
0101     OCFS2_UNLOCK_INVALID = 0,
0102     OCFS2_UNLOCK_CANCEL_CONVERT,
0103     OCFS2_UNLOCK_DROP_LOCK,
0104 };
0105 
0106 /* ocfs2_lock_res->l_flags flags. */
0107 #define OCFS2_LOCK_ATTACHED      (0x00000001) /* we have initialized
0108                            * the lvb */
0109 #define OCFS2_LOCK_BUSY          (0x00000002) /* we are currently in
0110                            * dlm_lock */
0111 #define OCFS2_LOCK_BLOCKED       (0x00000004) /* blocked waiting to
0112                            * downconvert*/
0113 #define OCFS2_LOCK_LOCAL         (0x00000008) /* newly created inode */
0114 #define OCFS2_LOCK_NEEDS_REFRESH (0x00000010)
0115 #define OCFS2_LOCK_REFRESHING    (0x00000020)
0116 #define OCFS2_LOCK_INITIALIZED   (0x00000040) /* track initialization
0117                            * for shutdown paths */
0118 #define OCFS2_LOCK_FREEING       (0x00000080) /* help dlmglue track
0119                            * when to skip queueing
0120                            * a lock because it's
0121                            * about to be
0122                            * dropped. */
0123 #define OCFS2_LOCK_QUEUED        (0x00000100) /* queued for downconvert */
0124 #define OCFS2_LOCK_NOCACHE       (0x00000200) /* don't use a holder count */
0125 #define OCFS2_LOCK_PENDING       (0x00000400) /* This lockres is pending a
0126                          call to dlm_lock.  Only
0127                          exists with BUSY set. */
0128 #define OCFS2_LOCK_UPCONVERT_FINISHING (0x00000800) /* blocks the dc thread
0129                              * from downconverting
0130                              * before the upconvert
0131                              * has completed */
0132 
0133 #define OCFS2_LOCK_NONBLOCK_FINISHED (0x00001000) /* NONBLOCK cluster
0134                            * lock has already
0135                            * returned, do not block
0136                            * dc thread from
0137                            * downconverting */
0138 
0139 struct ocfs2_lock_res_ops;
0140 
0141 typedef void (*ocfs2_lock_callback)(int status, unsigned long data);
0142 
0143 #ifdef CONFIG_OCFS2_FS_STATS
0144 struct ocfs2_lock_stats {
0145     u64     ls_total;   /* Total wait in NSEC */
0146     u32     ls_gets;    /* Num acquires */
0147     u32     ls_fail;    /* Num failed acquires */
0148 
0149     /* Storing max wait in usecs saves 24 bytes per inode */
0150     u32     ls_max;     /* Max wait in USEC */
0151     u64     ls_last;    /* Last unlock time in USEC */
0152 };
0153 #endif
0154 
0155 struct ocfs2_lock_res {
0156     void                    *l_priv;
0157     struct ocfs2_lock_res_ops *l_ops;
0158 
0159 
0160     struct list_head         l_blocked_list;
0161     struct list_head         l_mask_waiters;
0162     struct list_head     l_holders;
0163 
0164     unsigned long        l_flags;
0165     char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
0166     unsigned int             l_ro_holders;
0167     unsigned int             l_ex_holders;
0168     signed char      l_level;
0169     signed char      l_requested;
0170     signed char      l_blocking;
0171 
0172     /* Data packed - type enum ocfs2_lock_type */
0173     unsigned char            l_type;
0174 
0175     /* used from AST/BAST funcs. */
0176     /* Data packed - enum type ocfs2_ast_action */
0177     unsigned char            l_action;
0178     /* Data packed - enum type ocfs2_unlock_action */
0179     unsigned char            l_unlock_action;
0180     unsigned int             l_pending_gen;
0181 
0182     spinlock_t               l_lock;
0183 
0184     struct ocfs2_dlm_lksb    l_lksb;
0185 
0186     wait_queue_head_t        l_event;
0187 
0188     struct list_head         l_debug_list;
0189 
0190 #ifdef CONFIG_OCFS2_FS_STATS
0191     struct ocfs2_lock_stats  l_lock_prmode;     /* PR mode stats */
0192     u32                      l_lock_refresh;    /* Disk refreshes */
0193     u64                      l_lock_wait;   /* First lock wait time */
0194     struct ocfs2_lock_stats  l_lock_exmode;     /* EX mode stats */
0195 #endif
0196 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0197     struct lockdep_map   l_lockdep_map;
0198 #endif
0199 };
0200 
0201 enum ocfs2_orphan_reco_type {
0202     ORPHAN_NO_NEED_TRUNCATE = 0,
0203     ORPHAN_NEED_TRUNCATE,
0204 };
0205 
0206 enum ocfs2_orphan_scan_state {
0207     ORPHAN_SCAN_ACTIVE,
0208     ORPHAN_SCAN_INACTIVE
0209 };
0210 
0211 struct ocfs2_orphan_scan {
0212     struct mutex        os_lock;
0213     struct ocfs2_super  *os_osb;
0214     struct ocfs2_lock_res   os_lockres;     /* lock to synchronize scans */
0215     struct delayed_work     os_orphan_scan_work;
0216     time64_t        os_scantime;  /* time this node ran the scan */
0217     u32         os_count;      /* tracks node specific scans */
0218     u32             os_seqno;       /* tracks cluster wide scans */
0219     atomic_t        os_state;              /* ACTIVE or INACTIVE */
0220 };
0221 
0222 struct ocfs2_dlm_debug {
0223     struct kref d_refcnt;
0224     u32 d_filter_secs;
0225     struct list_head d_lockres_tracking;
0226 };
0227 
0228 enum ocfs2_vol_state
0229 {
0230     VOLUME_INIT = 0,
0231     VOLUME_MOUNTED,
0232     VOLUME_MOUNTED_QUOTAS,
0233     VOLUME_DISMOUNTED,
0234     VOLUME_DISABLED
0235 };
0236 
0237 struct ocfs2_alloc_stats
0238 {
0239     atomic_t moves;
0240     atomic_t local_data;
0241     atomic_t bitmap_data;
0242     atomic_t bg_allocs;
0243     atomic_t bg_extends;
0244 };
0245 
0246 enum ocfs2_local_alloc_state
0247 {
0248     OCFS2_LA_UNUSED = 0,    /* Local alloc will never be used for
0249                  * this mountpoint. */
0250     OCFS2_LA_ENABLED,   /* Local alloc is in use. */
0251     OCFS2_LA_THROTTLED, /* Local alloc is in use, but number
0252                  * of bits has been reduced. */
0253     OCFS2_LA_DISABLED   /* Local alloc has temporarily been
0254                  * disabled. */
0255 };
0256 
0257 enum ocfs2_mount_options
0258 {
0259     OCFS2_MOUNT_HB_LOCAL = 1 << 0, /* Local heartbeat */
0260     OCFS2_MOUNT_BARRIER = 1 << 1,   /* Use block barriers */
0261     OCFS2_MOUNT_NOINTR  = 1 << 2,   /* Don't catch signals */
0262     OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */
0263     OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */
0264     OCFS2_MOUNT_LOCALFLOCKS = 1 << 5, /* No cluster aware user file locks */
0265     OCFS2_MOUNT_NOUSERXATTR = 1 << 6, /* No user xattr */
0266     OCFS2_MOUNT_INODE64 = 1 << 7,   /* Allow inode numbers > 2^32 */
0267     OCFS2_MOUNT_POSIX_ACL = 1 << 8, /* Force POSIX access control lists */
0268     OCFS2_MOUNT_NO_POSIX_ACL = 1 << 9,  /* Disable POSIX access
0269                            control lists */
0270     OCFS2_MOUNT_USRQUOTA = 1 << 10, /* We support user quotas */
0271     OCFS2_MOUNT_GRPQUOTA = 1 << 11, /* We support group quotas */
0272     OCFS2_MOUNT_COHERENCY_BUFFERED = 1 << 12, /* Allow concurrent O_DIRECT
0273                              writes */
0274     OCFS2_MOUNT_HB_NONE = 1 << 13, /* No heartbeat */
0275     OCFS2_MOUNT_HB_GLOBAL = 1 << 14, /* Global heartbeat */
0276 
0277     OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT = 1 << 15,  /* Journal Async Commit */
0278     OCFS2_MOUNT_ERRORS_CONT = 1 << 16, /* Return EIO to the calling process on error */
0279     OCFS2_MOUNT_ERRORS_ROFS = 1 << 17, /* Change filesystem to read-only on error */
0280 };
0281 
0282 #define OCFS2_OSB_SOFT_RO   0x0001
0283 #define OCFS2_OSB_HARD_RO   0x0002
0284 #define OCFS2_OSB_ERROR_FS  0x0004
0285 #define OCFS2_DEFAULT_ATIME_QUANTUM 60
0286 
0287 struct ocfs2_journal;
0288 struct ocfs2_slot_info;
0289 struct ocfs2_recovery_map;
0290 struct ocfs2_replay_map;
0291 struct ocfs2_quota_recovery;
0292 struct ocfs2_super
0293 {
0294     struct task_struct *commit_task;
0295     struct super_block *sb;
0296     struct inode *root_inode;
0297     struct inode *sys_root_inode;
0298     struct inode *global_system_inodes[NUM_GLOBAL_SYSTEM_INODES];
0299     struct inode **local_system_inodes;
0300 
0301     struct ocfs2_slot_info *slot_info;
0302 
0303     u32 *slot_recovery_generations;
0304 
0305     spinlock_t node_map_lock;
0306 
0307     u64 root_blkno;
0308     u64 system_dir_blkno;
0309     u64 bitmap_blkno;
0310     u32 bitmap_cpg;
0311     char *uuid_str;
0312     u32 uuid_hash;
0313     u8 *vol_label;
0314     u64 first_cluster_group_blkno;
0315     u32 fs_generation;
0316 
0317     u32 s_feature_compat;
0318     u32 s_feature_incompat;
0319     u32 s_feature_ro_compat;
0320 
0321     /* Protects s_next_generation, osb_flags and s_inode_steal_slot.
0322      * Could protect more on osb as it's very short lived.
0323      */
0324     spinlock_t osb_lock;
0325     u32 s_next_generation;
0326     unsigned long osb_flags;
0327     u16 s_inode_steal_slot;
0328     u16 s_meta_steal_slot;
0329     atomic_t s_num_inodes_stolen;
0330     atomic_t s_num_meta_stolen;
0331 
0332     unsigned long s_mount_opt;
0333     unsigned int s_atime_quantum;
0334 
0335     unsigned int max_slots;
0336     unsigned int node_num;
0337     int slot_num;
0338     int preferred_slot;
0339     int s_sectsize_bits;
0340     int s_clustersize;
0341     int s_clustersize_bits;
0342     unsigned int s_xattr_inline_size;
0343 
0344     atomic_t vol_state;
0345     struct mutex recovery_lock;
0346     struct ocfs2_recovery_map *recovery_map;
0347     struct ocfs2_replay_map *replay_map;
0348     struct task_struct *recovery_thread_task;
0349     int disable_recovery;
0350     wait_queue_head_t checkpoint_event;
0351     struct ocfs2_journal *journal;
0352     unsigned long osb_commit_interval;
0353 
0354     struct delayed_work     la_enable_wq;
0355 
0356     /*
0357      * Must hold local alloc i_rwsem and osb->osb_lock to change
0358      * local_alloc_bits. Reads can be done under either lock.
0359      */
0360     unsigned int local_alloc_bits;
0361     unsigned int local_alloc_default_bits;
0362     /* osb_clusters_at_boot can become stale! Do not trust it to
0363      * be up to date. */
0364     unsigned int osb_clusters_at_boot;
0365 
0366     enum ocfs2_local_alloc_state local_alloc_state; /* protected
0367                              * by osb_lock */
0368 
0369     struct buffer_head *local_alloc_bh;
0370 
0371     u64 la_last_gd;
0372 
0373     struct ocfs2_reservation_map    osb_la_resmap;
0374 
0375     unsigned int    osb_resv_level;
0376     unsigned int    osb_dir_resv_level;
0377 
0378     /* Next two fields are for local node slot recovery during
0379      * mount. */
0380     struct ocfs2_dinode *local_alloc_copy;
0381     struct ocfs2_quota_recovery *quota_rec;
0382 
0383     struct ocfs2_blockcheck_stats osb_ecc_stats;
0384     struct ocfs2_alloc_stats alloc_stats;
0385     char dev_str[20];       /* "major,minor" of the device */
0386 
0387     u8 osb_stackflags;
0388 
0389     char osb_cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
0390     char osb_cluster_name[OCFS2_CLUSTER_NAME_LEN + 1];
0391     struct ocfs2_cluster_connection *cconn;
0392     struct ocfs2_lock_res osb_super_lockres;
0393     struct ocfs2_lock_res osb_rename_lockres;
0394     struct ocfs2_lock_res osb_nfs_sync_lockres;
0395     struct rw_semaphore nfs_sync_rwlock;
0396     struct ocfs2_lock_res osb_trim_fs_lockres;
0397     struct mutex obs_trim_fs_mutex;
0398     struct ocfs2_dlm_debug *osb_dlm_debug;
0399 
0400     struct dentry *osb_debug_root;
0401 
0402     wait_queue_head_t recovery_event;
0403 
0404     spinlock_t dc_task_lock;
0405     struct task_struct *dc_task;
0406     wait_queue_head_t dc_event;
0407     unsigned long dc_wake_sequence;
0408     unsigned long dc_work_sequence;
0409 
0410     /*
0411      * Any thread can add locks to the list, but the downconvert
0412      * thread is the only one allowed to remove locks. Any change
0413      * to this rule requires updating
0414      * ocfs2_downconvert_thread_do_work().
0415      */
0416     struct list_head blocked_lock_list;
0417     unsigned long blocked_lock_count;
0418 
0419     /* List of dquot structures to drop last reference to */
0420     struct llist_head dquot_drop_list;
0421     struct work_struct dquot_drop_work;
0422 
0423     wait_queue_head_t       osb_mount_event;
0424 
0425     /* Truncate log info */
0426     struct inode            *osb_tl_inode;
0427     struct buffer_head      *osb_tl_bh;
0428     struct delayed_work     osb_truncate_log_wq;
0429     atomic_t            osb_tl_disable;
0430     /*
0431      * How many clusters in our truncate log.
0432      * It must be protected by osb_tl_inode->i_rwsem.
0433      */
0434     unsigned int truncated_clusters;
0435 
0436     struct ocfs2_node_map       osb_recovering_orphan_dirs;
0437     unsigned int            *osb_orphan_wipes;
0438     wait_queue_head_t       osb_wipe_event;
0439 
0440     struct ocfs2_orphan_scan    osb_orphan_scan;
0441 
0442     /* used to protect metaecc calculation check of xattr. */
0443     spinlock_t osb_xattr_lock;
0444 
0445     unsigned int            osb_dx_mask;
0446     u32             osb_dx_seed[4];
0447 
0448     /* the group we used to allocate inodes. */
0449     u64             osb_inode_alloc_group;
0450 
0451     /* rb tree root for refcount lock. */
0452     struct rb_root  osb_rf_lock_tree;
0453     struct ocfs2_refcount_tree *osb_ref_tree_lru;
0454 
0455     struct mutex system_file_mutex;
0456 
0457     /*
0458      * OCFS2 needs to schedule several different types of work which
0459      * require cluster locking, disk I/O, recovery waits, etc. Since these
0460      * types of work tend to be heavy we avoid using the kernel events
0461      * workqueue and schedule on our own.
0462      */
0463     struct workqueue_struct *ocfs2_wq;
0464 
0465     /* sysfs directory per partition */
0466     struct kset *osb_dev_kset;
0467 
0468     /* file check related stuff */
0469     struct ocfs2_filecheck_sysfs_entry osb_fc_ent;
0470 };
0471 
0472 #define OCFS2_SB(sb)        ((struct ocfs2_super *)(sb)->s_fs_info)
0473 
0474 /* Useful typedef for passing around journal access functions */
0475 typedef int (*ocfs2_journal_access_func)(handle_t *handle,
0476                      struct ocfs2_caching_info *ci,
0477                      struct buffer_head *bh, int type);
0478 
0479 static inline int ocfs2_should_order_data(struct inode *inode)
0480 {
0481     if (!S_ISREG(inode->i_mode))
0482         return 0;
0483     if (OCFS2_SB(inode->i_sb)->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)
0484         return 0;
0485     return 1;
0486 }
0487 
0488 static inline int ocfs2_sparse_alloc(struct ocfs2_super *osb)
0489 {
0490     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC)
0491         return 1;
0492     return 0;
0493 }
0494 
0495 static inline int ocfs2_writes_unwritten_extents(struct ocfs2_super *osb)
0496 {
0497     /*
0498      * Support for sparse files is a pre-requisite
0499      */
0500     if (!ocfs2_sparse_alloc(osb))
0501         return 0;
0502 
0503     if (osb->s_feature_ro_compat & OCFS2_FEATURE_RO_COMPAT_UNWRITTEN)
0504         return 1;
0505     return 0;
0506 }
0507 
0508 static inline int ocfs2_supports_append_dio(struct ocfs2_super *osb)
0509 {
0510     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_APPEND_DIO)
0511         return 1;
0512     return 0;
0513 }
0514 
0515 
0516 static inline int ocfs2_supports_inline_data(struct ocfs2_super *osb)
0517 {
0518     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_INLINE_DATA)
0519         return 1;
0520     return 0;
0521 }
0522 
0523 static inline int ocfs2_supports_xattr(struct ocfs2_super *osb)
0524 {
0525     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR)
0526         return 1;
0527     return 0;
0528 }
0529 
0530 static inline int ocfs2_meta_ecc(struct ocfs2_super *osb)
0531 {
0532     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_META_ECC)
0533         return 1;
0534     return 0;
0535 }
0536 
0537 static inline int ocfs2_supports_indexed_dirs(struct ocfs2_super *osb)
0538 {
0539     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_INDEXED_DIRS)
0540         return 1;
0541     return 0;
0542 }
0543 
0544 static inline int ocfs2_supports_discontig_bg(struct ocfs2_super *osb)
0545 {
0546     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG)
0547         return 1;
0548     return 0;
0549 }
0550 
0551 static inline unsigned int ocfs2_link_max(struct ocfs2_super *osb)
0552 {
0553     if (ocfs2_supports_indexed_dirs(osb))
0554         return OCFS2_DX_LINK_MAX;
0555     return OCFS2_LINK_MAX;
0556 }
0557 
0558 static inline unsigned int ocfs2_read_links_count(struct ocfs2_dinode *di)
0559 {
0560     u32 nlink = le16_to_cpu(di->i_links_count);
0561     u32 hi = le16_to_cpu(di->i_links_count_hi);
0562 
0563     if (di->i_dyn_features & cpu_to_le16(OCFS2_INDEXED_DIR_FL))
0564         nlink |= (hi << OCFS2_LINKS_HI_SHIFT);
0565 
0566     return nlink;
0567 }
0568 
0569 static inline void ocfs2_set_links_count(struct ocfs2_dinode *di, u32 nlink)
0570 {
0571     u16 lo, hi;
0572 
0573     lo = nlink;
0574     hi = nlink >> OCFS2_LINKS_HI_SHIFT;
0575 
0576     di->i_links_count = cpu_to_le16(lo);
0577     di->i_links_count_hi = cpu_to_le16(hi);
0578 }
0579 
0580 static inline void ocfs2_add_links_count(struct ocfs2_dinode *di, int n)
0581 {
0582     u32 links = ocfs2_read_links_count(di);
0583 
0584     links += n;
0585 
0586     ocfs2_set_links_count(di, links);
0587 }
0588 
0589 static inline int ocfs2_refcount_tree(struct ocfs2_super *osb)
0590 {
0591     if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE)
0592         return 1;
0593     return 0;
0594 }
0595 
0596 /* set / clear functions because cluster events can make these happen
0597  * in parallel so we want the transitions to be atomic. this also
0598  * means that any future flags osb_flags must be protected by spinlock
0599  * too! */
0600 static inline void ocfs2_set_osb_flag(struct ocfs2_super *osb,
0601                       unsigned long flag)
0602 {
0603     spin_lock(&osb->osb_lock);
0604     osb->osb_flags |= flag;
0605     spin_unlock(&osb->osb_lock);
0606 }
0607 
0608 static inline void ocfs2_set_ro_flag(struct ocfs2_super *osb,
0609                      int hard)
0610 {
0611     spin_lock(&osb->osb_lock);
0612     osb->osb_flags &= ~(OCFS2_OSB_SOFT_RO|OCFS2_OSB_HARD_RO);
0613     if (hard)
0614         osb->osb_flags |= OCFS2_OSB_HARD_RO;
0615     else
0616         osb->osb_flags |= OCFS2_OSB_SOFT_RO;
0617     spin_unlock(&osb->osb_lock);
0618 }
0619 
0620 static inline int ocfs2_is_hard_readonly(struct ocfs2_super *osb)
0621 {
0622     int ret;
0623 
0624     spin_lock(&osb->osb_lock);
0625     ret = osb->osb_flags & OCFS2_OSB_HARD_RO;
0626     spin_unlock(&osb->osb_lock);
0627 
0628     return ret;
0629 }
0630 
0631 static inline int ocfs2_is_soft_readonly(struct ocfs2_super *osb)
0632 {
0633     int ret;
0634 
0635     spin_lock(&osb->osb_lock);
0636     ret = osb->osb_flags & OCFS2_OSB_SOFT_RO;
0637     spin_unlock(&osb->osb_lock);
0638 
0639     return ret;
0640 }
0641 
0642 static inline int ocfs2_clusterinfo_valid(struct ocfs2_super *osb)
0643 {
0644     return (osb->s_feature_incompat &
0645         (OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK |
0646          OCFS2_FEATURE_INCOMPAT_CLUSTERINFO));
0647 }
0648 
0649 static inline int ocfs2_userspace_stack(struct ocfs2_super *osb)
0650 {
0651     if (ocfs2_clusterinfo_valid(osb) &&
0652         memcmp(osb->osb_cluster_stack, OCFS2_CLASSIC_CLUSTER_STACK,
0653            OCFS2_STACK_LABEL_LEN))
0654         return 1;
0655     return 0;
0656 }
0657 
0658 static inline int ocfs2_o2cb_stack(struct ocfs2_super *osb)
0659 {
0660     if (ocfs2_clusterinfo_valid(osb) &&
0661         !memcmp(osb->osb_cluster_stack, OCFS2_CLASSIC_CLUSTER_STACK,
0662            OCFS2_STACK_LABEL_LEN))
0663         return 1;
0664     return 0;
0665 }
0666 
0667 static inline int ocfs2_cluster_o2cb_global_heartbeat(struct ocfs2_super *osb)
0668 {
0669     return ocfs2_o2cb_stack(osb) &&
0670         (osb->osb_stackflags & OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT);
0671 }
0672 
0673 static inline int ocfs2_mount_local(struct ocfs2_super *osb)
0674 {
0675     return (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT);
0676 }
0677 
0678 static inline int ocfs2_uses_extended_slot_map(struct ocfs2_super *osb)
0679 {
0680     return (osb->s_feature_incompat &
0681         OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP);
0682 }
0683 
0684 
0685 #define OCFS2_IS_VALID_DINODE(ptr)                  \
0686     (!strcmp((ptr)->i_signature, OCFS2_INODE_SIGNATURE))
0687 
0688 #define OCFS2_IS_VALID_EXTENT_BLOCK(ptr)                \
0689     (!strcmp((ptr)->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE))
0690 
0691 #define OCFS2_IS_VALID_GROUP_DESC(ptr)                  \
0692     (!strcmp((ptr)->bg_signature, OCFS2_GROUP_DESC_SIGNATURE))
0693 
0694 
0695 #define OCFS2_IS_VALID_XATTR_BLOCK(ptr)                 \
0696     (!strcmp((ptr)->xb_signature, OCFS2_XATTR_BLOCK_SIGNATURE))
0697 
0698 #define OCFS2_IS_VALID_DIR_TRAILER(ptr)                 \
0699     (!strcmp((ptr)->db_signature, OCFS2_DIR_TRAILER_SIGNATURE))
0700 
0701 #define OCFS2_IS_VALID_DX_ROOT(ptr)                 \
0702     (!strcmp((ptr)->dr_signature, OCFS2_DX_ROOT_SIGNATURE))
0703 
0704 #define OCFS2_IS_VALID_DX_LEAF(ptr)                 \
0705     (!strcmp((ptr)->dl_signature, OCFS2_DX_LEAF_SIGNATURE))
0706 
0707 #define OCFS2_IS_VALID_REFCOUNT_BLOCK(ptr)              \
0708     (!strcmp((ptr)->rf_signature, OCFS2_REFCOUNT_BLOCK_SIGNATURE))
0709 
0710 static inline unsigned long ino_from_blkno(struct super_block *sb,
0711                        u64 blkno)
0712 {
0713     return (unsigned long)(blkno & (u64)ULONG_MAX);
0714 }
0715 
0716 static inline u64 ocfs2_clusters_to_blocks(struct super_block *sb,
0717                        u32 clusters)
0718 {
0719     int c_to_b_bits = OCFS2_SB(sb)->s_clustersize_bits -
0720         sb->s_blocksize_bits;
0721 
0722     return (u64)clusters << c_to_b_bits;
0723 }
0724 
0725 static inline u32 ocfs2_clusters_for_blocks(struct super_block *sb,
0726         u64 blocks)
0727 {
0728     int b_to_c_bits = OCFS2_SB(sb)->s_clustersize_bits -
0729             sb->s_blocksize_bits;
0730 
0731     blocks += (1 << b_to_c_bits) - 1;
0732     return (u32)(blocks >> b_to_c_bits);
0733 }
0734 
0735 static inline u32 ocfs2_blocks_to_clusters(struct super_block *sb,
0736                        u64 blocks)
0737 {
0738     int b_to_c_bits = OCFS2_SB(sb)->s_clustersize_bits -
0739         sb->s_blocksize_bits;
0740 
0741     return (u32)(blocks >> b_to_c_bits);
0742 }
0743 
0744 static inline unsigned int ocfs2_clusters_for_bytes(struct super_block *sb,
0745                             u64 bytes)
0746 {
0747     int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
0748     unsigned int clusters;
0749 
0750     bytes += OCFS2_SB(sb)->s_clustersize - 1;
0751     /* OCFS2 just cannot have enough clusters to overflow this */
0752     clusters = (unsigned int)(bytes >> cl_bits);
0753 
0754     return clusters;
0755 }
0756 
0757 static inline unsigned int ocfs2_bytes_to_clusters(struct super_block *sb,
0758         u64 bytes)
0759 {
0760     int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
0761     unsigned int clusters;
0762 
0763     clusters = (unsigned int)(bytes >> cl_bits);
0764     return clusters;
0765 }
0766 
0767 static inline u64 ocfs2_blocks_for_bytes(struct super_block *sb,
0768                      u64 bytes)
0769 {
0770     bytes += sb->s_blocksize - 1;
0771     return bytes >> sb->s_blocksize_bits;
0772 }
0773 
0774 static inline u64 ocfs2_clusters_to_bytes(struct super_block *sb,
0775                       u32 clusters)
0776 {
0777     return (u64)clusters << OCFS2_SB(sb)->s_clustersize_bits;
0778 }
0779 
0780 static inline u64 ocfs2_block_to_cluster_start(struct super_block *sb,
0781                            u64 blocks)
0782 {
0783     int bits = OCFS2_SB(sb)->s_clustersize_bits - sb->s_blocksize_bits;
0784     unsigned int clusters;
0785 
0786     clusters = ocfs2_blocks_to_clusters(sb, blocks);
0787     return (u64)clusters << bits;
0788 }
0789 
0790 static inline u64 ocfs2_align_bytes_to_clusters(struct super_block *sb,
0791                         u64 bytes)
0792 {
0793     int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
0794     unsigned int clusters;
0795 
0796     clusters = ocfs2_clusters_for_bytes(sb, bytes);
0797     return (u64)clusters << cl_bits;
0798 }
0799 
0800 static inline u64 ocfs2_align_bytes_to_blocks(struct super_block *sb,
0801                           u64 bytes)
0802 {
0803     u64 blocks;
0804 
0805         blocks = ocfs2_blocks_for_bytes(sb, bytes);
0806     return blocks << sb->s_blocksize_bits;
0807 }
0808 
0809 static inline unsigned long ocfs2_align_bytes_to_sectors(u64 bytes)
0810 {
0811     return (unsigned long)((bytes + 511) >> 9);
0812 }
0813 
0814 static inline unsigned int ocfs2_page_index_to_clusters(struct super_block *sb,
0815                             unsigned long pg_index)
0816 {
0817     u32 clusters = pg_index;
0818     unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
0819 
0820     if (unlikely(PAGE_SHIFT > cbits))
0821         clusters = pg_index << (PAGE_SHIFT - cbits);
0822     else if (PAGE_SHIFT < cbits)
0823         clusters = pg_index >> (cbits - PAGE_SHIFT);
0824 
0825     return clusters;
0826 }
0827 
0828 /*
0829  * Find the 1st page index which covers the given clusters.
0830  */
0831 static inline pgoff_t ocfs2_align_clusters_to_page_index(struct super_block *sb,
0832                             u32 clusters)
0833 {
0834     unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
0835         pgoff_t index = clusters;
0836 
0837     if (PAGE_SHIFT > cbits) {
0838         index = (pgoff_t)clusters >> (PAGE_SHIFT - cbits);
0839     } else if (PAGE_SHIFT < cbits) {
0840         index = (pgoff_t)clusters << (cbits - PAGE_SHIFT);
0841     }
0842 
0843     return index;
0844 }
0845 
0846 static inline unsigned int ocfs2_pages_per_cluster(struct super_block *sb)
0847 {
0848     unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
0849     unsigned int pages_per_cluster = 1;
0850 
0851     if (PAGE_SHIFT < cbits)
0852         pages_per_cluster = 1 << (cbits - PAGE_SHIFT);
0853 
0854     return pages_per_cluster;
0855 }
0856 
0857 static inline unsigned int ocfs2_megabytes_to_clusters(struct super_block *sb,
0858                                unsigned int megs)
0859 {
0860     BUILD_BUG_ON(OCFS2_MAX_CLUSTERSIZE > 1048576);
0861 
0862     return megs << (20 - OCFS2_SB(sb)->s_clustersize_bits);
0863 }
0864 
0865 static inline unsigned int ocfs2_clusters_to_megabytes(struct super_block *sb,
0866                                unsigned int clusters)
0867 {
0868     return clusters >> (20 - OCFS2_SB(sb)->s_clustersize_bits);
0869 }
0870 
0871 static inline void _ocfs2_set_bit(unsigned int bit, unsigned long *bitmap)
0872 {
0873     __set_bit_le(bit, bitmap);
0874 }
0875 #define ocfs2_set_bit(bit, addr) _ocfs2_set_bit((bit), (unsigned long *)(addr))
0876 
0877 static inline void _ocfs2_clear_bit(unsigned int bit, unsigned long *bitmap)
0878 {
0879     __clear_bit_le(bit, bitmap);
0880 }
0881 #define ocfs2_clear_bit(bit, addr) _ocfs2_clear_bit((bit), (unsigned long *)(addr))
0882 
0883 #define ocfs2_test_bit test_bit_le
0884 #define ocfs2_find_next_zero_bit find_next_zero_bit_le
0885 #define ocfs2_find_next_bit find_next_bit_le
0886 
0887 static inline void *correct_addr_and_bit_unaligned(int *bit, void *addr)
0888 {
0889 #if BITS_PER_LONG == 64
0890     *bit += ((unsigned long) addr & 7UL) << 3;
0891     addr = (void *) ((unsigned long) addr & ~7UL);
0892 #elif BITS_PER_LONG == 32
0893     *bit += ((unsigned long) addr & 3UL) << 3;
0894     addr = (void *) ((unsigned long) addr & ~3UL);
0895 #else
0896 #error "how many bits you are?!"
0897 #endif
0898     return addr;
0899 }
0900 
0901 static inline void ocfs2_set_bit_unaligned(int bit, void *bitmap)
0902 {
0903     bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
0904     ocfs2_set_bit(bit, bitmap);
0905 }
0906 
0907 static inline void ocfs2_clear_bit_unaligned(int bit, void *bitmap)
0908 {
0909     bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
0910     ocfs2_clear_bit(bit, bitmap);
0911 }
0912 
0913 static inline int ocfs2_test_bit_unaligned(int bit, void *bitmap)
0914 {
0915     bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
0916     return ocfs2_test_bit(bit, bitmap);
0917 }
0918 
0919 static inline int ocfs2_find_next_zero_bit_unaligned(void *bitmap, int max,
0920                             int start)
0921 {
0922     int fix = 0, ret, tmpmax;
0923     bitmap = correct_addr_and_bit_unaligned(&fix, bitmap);
0924     tmpmax = max + fix;
0925     start += fix;
0926 
0927     ret = ocfs2_find_next_zero_bit(bitmap, tmpmax, start) - fix;
0928     if (ret > max)
0929         return max;
0930     return ret;
0931 }
0932 
0933 #endif  /* OCFS2_H */
0934