ocfs2.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * ocfs2.h
  6. *
  7. * Defines macros and structures used in OCFS2
  8. *
  9. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  10. */
  11. #ifndef OCFS2_H
  12. #define OCFS2_H
  13. #include <linux/spinlock.h>
  14. #include <linux/sched.h>
  15. #include <linux/wait.h>
  16. #include <linux/list.h>
  17. #include <linux/llist.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/kref.h>
  21. #include <linux/mutex.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/jbd2.h>
  24. /* For union ocfs2_dlm_lksb */
  25. #include "stackglue.h"
  26. #include "ocfs2_fs.h"
  27. #include "ocfs2_lockid.h"
  28. #include "ocfs2_ioctl.h"
  29. /* For struct ocfs2_blockcheck_stats */
  30. #include "blockcheck.h"
  31. #include "reservations.h"
  32. #include "filecheck.h"
  33. /* Caching of metadata buffers */
  34. /* Most user visible OCFS2 inodes will have very few pieces of
  35. * metadata, but larger files (including bitmaps, etc) must be taken
  36. * into account when designing an access scheme. We allow a small
  37. * amount of inlined blocks to be stored on an array and grow the
  38. * structure into a rb tree when necessary. */
  39. #define OCFS2_CACHE_INFO_MAX_ARRAY 2
  40. /* Flags for ocfs2_caching_info */
  41. enum ocfs2_caching_info_flags {
  42. /* Indicates that the metadata cache is using the inline array */
  43. OCFS2_CACHE_FL_INLINE = 1<<1,
  44. };
  45. struct ocfs2_caching_operations;
  46. struct ocfs2_caching_info {
  47. /*
  48. * The parent structure provides the locks, but because the
  49. * parent structure can differ, it provides locking operations
  50. * to struct ocfs2_caching_info.
  51. */
  52. const struct ocfs2_caching_operations *ci_ops;
  53. /* next two are protected by trans_inc_lock */
  54. /* which transaction were we created on? Zero if none. */
  55. unsigned long ci_created_trans;
  56. /* last transaction we were a part of. */
  57. unsigned long ci_last_trans;
  58. /* Cache structures */
  59. unsigned int ci_flags;
  60. unsigned int ci_num_cached;
  61. union {
  62. sector_t ci_array[OCFS2_CACHE_INFO_MAX_ARRAY];
  63. struct rb_root ci_tree;
  64. } ci_cache;
  65. };
  66. /*
  67. * Need this prototype here instead of in uptodate.h because journal.h
  68. * uses it.
  69. */
  70. struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci);
  71. /* this limits us to 256 nodes
  72. * if we need more, we can do a kmalloc for the map */
  73. #define OCFS2_NODE_MAP_MAX_NODES 256
  74. struct ocfs2_node_map {
  75. u16 num_nodes;
  76. unsigned long map[BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES)];
  77. };
  78. enum ocfs2_ast_action {
  79. OCFS2_AST_INVALID = 0,
  80. OCFS2_AST_ATTACH,
  81. OCFS2_AST_CONVERT,
  82. OCFS2_AST_DOWNCONVERT,
  83. };
  84. /* actions for an unlockast function to take. */
  85. enum ocfs2_unlock_action {
  86. OCFS2_UNLOCK_INVALID = 0,
  87. OCFS2_UNLOCK_CANCEL_CONVERT,
  88. OCFS2_UNLOCK_DROP_LOCK,
  89. };
  90. /* ocfs2_lock_res->l_flags flags. */
  91. #define OCFS2_LOCK_ATTACHED (0x00000001) /* we have initialized
  92. * the lvb */
  93. #define OCFS2_LOCK_BUSY (0x00000002) /* we are currently in
  94. * dlm_lock */
  95. #define OCFS2_LOCK_BLOCKED (0x00000004) /* blocked waiting to
  96. * downconvert*/
  97. #define OCFS2_LOCK_LOCAL (0x00000008) /* newly created inode */
  98. #define OCFS2_LOCK_NEEDS_REFRESH (0x00000010)
  99. #define OCFS2_LOCK_REFRESHING (0x00000020)
  100. #define OCFS2_LOCK_INITIALIZED (0x00000040) /* track initialization
  101. * for shutdown paths */
  102. #define OCFS2_LOCK_FREEING (0x00000080) /* help dlmglue track
  103. * when to skip queueing
  104. * a lock because it's
  105. * about to be
  106. * dropped. */
  107. #define OCFS2_LOCK_QUEUED (0x00000100) /* queued for downconvert */
  108. #define OCFS2_LOCK_NOCACHE (0x00000200) /* don't use a holder count */
  109. #define OCFS2_LOCK_PENDING (0x00000400) /* This lockres is pending a
  110. call to dlm_lock. Only
  111. exists with BUSY set. */
  112. #define OCFS2_LOCK_UPCONVERT_FINISHING (0x00000800) /* blocks the dc thread
  113. * from downconverting
  114. * before the upconvert
  115. * has completed */
  116. #define OCFS2_LOCK_NONBLOCK_FINISHED (0x00001000) /* NONBLOCK cluster
  117. * lock has already
  118. * returned, do not block
  119. * dc thread from
  120. * downconverting */
  121. struct ocfs2_lock_res_ops;
  122. typedef void (*ocfs2_lock_callback)(int status, unsigned long data);
  123. #ifdef CONFIG_OCFS2_FS_STATS
  124. struct ocfs2_lock_stats {
  125. u64 ls_total; /* Total wait in NSEC */
  126. u32 ls_gets; /* Num acquires */
  127. u32 ls_fail; /* Num failed acquires */
  128. /* Storing max wait in usecs saves 24 bytes per inode */
  129. u32 ls_max; /* Max wait in USEC */
  130. u64 ls_last; /* Last unlock time in USEC */
  131. };
  132. #endif
  133. struct ocfs2_lock_res {
  134. void *l_priv;
  135. struct ocfs2_lock_res_ops *l_ops;
  136. struct list_head l_blocked_list;
  137. struct list_head l_mask_waiters;
  138. struct list_head l_holders;
  139. unsigned long l_flags;
  140. char l_name[OCFS2_LOCK_ID_MAX_LEN];
  141. unsigned int l_ro_holders;
  142. unsigned int l_ex_holders;
  143. signed char l_level;
  144. signed char l_requested;
  145. signed char l_blocking;
  146. /* Data packed - type enum ocfs2_lock_type */
  147. unsigned char l_type;
  148. /* used from AST/BAST funcs. */
  149. /* Data packed - enum type ocfs2_ast_action */
  150. unsigned char l_action;
  151. /* Data packed - enum type ocfs2_unlock_action */
  152. unsigned char l_unlock_action;
  153. unsigned int l_pending_gen;
  154. spinlock_t l_lock;
  155. struct ocfs2_dlm_lksb l_lksb;
  156. wait_queue_head_t l_event;
  157. struct list_head l_debug_list;
  158. #ifdef CONFIG_OCFS2_FS_STATS
  159. struct ocfs2_lock_stats l_lock_prmode; /* PR mode stats */
  160. u32 l_lock_refresh; /* Disk refreshes */
  161. u64 l_lock_wait; /* First lock wait time */
  162. struct ocfs2_lock_stats l_lock_exmode; /* EX mode stats */
  163. #endif
  164. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  165. struct lockdep_map l_lockdep_map;
  166. #endif
  167. };
  168. enum ocfs2_orphan_reco_type {
  169. ORPHAN_NO_NEED_TRUNCATE = 0,
  170. ORPHAN_NEED_TRUNCATE,
  171. };
  172. enum ocfs2_orphan_scan_state {
  173. ORPHAN_SCAN_ACTIVE,
  174. ORPHAN_SCAN_INACTIVE
  175. };
  176. struct ocfs2_orphan_scan {
  177. struct mutex os_lock;
  178. struct ocfs2_super *os_osb;
  179. struct ocfs2_lock_res os_lockres; /* lock to synchronize scans */
  180. struct delayed_work os_orphan_scan_work;
  181. time64_t os_scantime; /* time this node ran the scan */
  182. u32 os_count; /* tracks node specific scans */
  183. u32 os_seqno; /* tracks cluster wide scans */
  184. atomic_t os_state; /* ACTIVE or INACTIVE */
  185. };
  186. struct ocfs2_dlm_debug {
  187. struct kref d_refcnt;
  188. u32 d_filter_secs;
  189. struct list_head d_lockres_tracking;
  190. };
  191. enum ocfs2_vol_state
  192. {
  193. VOLUME_INIT = 0,
  194. VOLUME_MOUNTED,
  195. VOLUME_MOUNTED_QUOTAS,
  196. VOLUME_DISMOUNTED,
  197. VOLUME_DISABLED
  198. };
  199. struct ocfs2_alloc_stats
  200. {
  201. atomic_t moves;
  202. atomic_t local_data;
  203. atomic_t bitmap_data;
  204. atomic_t bg_allocs;
  205. atomic_t bg_extends;
  206. };
  207. enum ocfs2_local_alloc_state
  208. {
  209. OCFS2_LA_UNUSED = 0, /* Local alloc will never be used for
  210. * this mountpoint. */
  211. OCFS2_LA_ENABLED, /* Local alloc is in use. */
  212. OCFS2_LA_THROTTLED, /* Local alloc is in use, but number
  213. * of bits has been reduced. */
  214. OCFS2_LA_DISABLED /* Local alloc has temporarily been
  215. * disabled. */
  216. };
  217. enum ocfs2_mount_options
  218. {
  219. OCFS2_MOUNT_HB_LOCAL = 1 << 0, /* Local heartbeat */
  220. OCFS2_MOUNT_BARRIER = 1 << 1, /* Use block barriers */
  221. OCFS2_MOUNT_NOINTR = 1 << 2, /* Don't catch signals */
  222. OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */
  223. OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */
  224. OCFS2_MOUNT_LOCALFLOCKS = 1 << 5, /* No cluster aware user file locks */
  225. OCFS2_MOUNT_NOUSERXATTR = 1 << 6, /* No user xattr */
  226. OCFS2_MOUNT_INODE64 = 1 << 7, /* Allow inode numbers > 2^32 */
  227. OCFS2_MOUNT_POSIX_ACL = 1 << 8, /* Force POSIX access control lists */
  228. OCFS2_MOUNT_NO_POSIX_ACL = 1 << 9, /* Disable POSIX access
  229. control lists */
  230. OCFS2_MOUNT_USRQUOTA = 1 << 10, /* We support user quotas */
  231. OCFS2_MOUNT_GRPQUOTA = 1 << 11, /* We support group quotas */
  232. OCFS2_MOUNT_COHERENCY_BUFFERED = 1 << 12, /* Allow concurrent O_DIRECT
  233. writes */
  234. OCFS2_MOUNT_HB_NONE = 1 << 13, /* No heartbeat */
  235. OCFS2_MOUNT_HB_GLOBAL = 1 << 14, /* Global heartbeat */
  236. OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT = 1 << 15, /* Journal Async Commit */
  237. OCFS2_MOUNT_ERRORS_CONT = 1 << 16, /* Return EIO to the calling process on error */
  238. OCFS2_MOUNT_ERRORS_ROFS = 1 << 17, /* Change filesystem to read-only on error */
  239. OCFS2_MOUNT_NOCLUSTER = 1 << 18, /* No cluster aware filesystem mount */
  240. };
  241. #define OCFS2_OSB_SOFT_RO 0x0001
  242. #define OCFS2_OSB_HARD_RO 0x0002
  243. #define OCFS2_OSB_ERROR_FS 0x0004
  244. #define OCFS2_DEFAULT_ATIME_QUANTUM 60
  245. struct ocfs2_journal;
  246. struct ocfs2_slot_info;
  247. struct ocfs2_recovery_map;
  248. struct ocfs2_replay_map;
  249. struct ocfs2_quota_recovery;
  250. struct ocfs2_super
  251. {
  252. struct task_struct *commit_task;
  253. struct super_block *sb;
  254. struct inode *root_inode;
  255. struct inode *sys_root_inode;
  256. struct inode *global_system_inodes[NUM_GLOBAL_SYSTEM_INODES];
  257. struct inode **local_system_inodes;
  258. struct ocfs2_slot_info *slot_info;
  259. u32 *slot_recovery_generations;
  260. spinlock_t node_map_lock;
  261. u64 root_blkno;
  262. u64 system_dir_blkno;
  263. u64 bitmap_blkno;
  264. u32 bitmap_cpg;
  265. char *uuid_str;
  266. u32 uuid_hash;
  267. u8 *vol_label;
  268. u64 first_cluster_group_blkno;
  269. u32 fs_generation;
  270. u32 s_feature_compat;
  271. u32 s_feature_incompat;
  272. u32 s_feature_ro_compat;
  273. /* Protects s_next_generation, osb_flags and s_inode_steal_slot.
  274. * Could protect more on osb as it's very short lived.
  275. */
  276. spinlock_t osb_lock;
  277. u32 s_next_generation;
  278. unsigned long osb_flags;
  279. u16 s_inode_steal_slot;
  280. u16 s_meta_steal_slot;
  281. atomic_t s_num_inodes_stolen;
  282. atomic_t s_num_meta_stolen;
  283. unsigned long s_mount_opt;
  284. unsigned int s_atime_quantum;
  285. unsigned int max_slots;
  286. unsigned int node_num;
  287. int slot_num;
  288. int preferred_slot;
  289. int s_sectsize_bits;
  290. int s_clustersize;
  291. int s_clustersize_bits;
  292. unsigned int s_xattr_inline_size;
  293. atomic_t vol_state;
  294. struct mutex recovery_lock;
  295. struct ocfs2_recovery_map *recovery_map;
  296. struct ocfs2_replay_map *replay_map;
  297. struct task_struct *recovery_thread_task;
  298. int disable_recovery;
  299. wait_queue_head_t checkpoint_event;
  300. struct ocfs2_journal *journal;
  301. unsigned long osb_commit_interval;
  302. struct delayed_work la_enable_wq;
  303. /*
  304. * Must hold local alloc i_mutex and osb->osb_lock to change
  305. * local_alloc_bits. Reads can be done under either lock.
  306. */
  307. unsigned int local_alloc_bits;
  308. unsigned int local_alloc_default_bits;
  309. /* osb_clusters_at_boot can become stale! Do not trust it to
  310. * be up to date. */
  311. unsigned int osb_clusters_at_boot;
  312. enum ocfs2_local_alloc_state local_alloc_state; /* protected
  313. * by osb_lock */
  314. struct buffer_head *local_alloc_bh;
  315. u64 la_last_gd;
  316. struct ocfs2_reservation_map osb_la_resmap;
  317. unsigned int osb_resv_level;
  318. unsigned int osb_dir_resv_level;
  319. /* Next two fields are for local node slot recovery during
  320. * mount. */
  321. struct ocfs2_dinode *local_alloc_copy;
  322. struct ocfs2_quota_recovery *quota_rec;
  323. struct ocfs2_blockcheck_stats osb_ecc_stats;
  324. struct ocfs2_alloc_stats alloc_stats;
  325. char dev_str[20]; /* "major,minor" of the device */
  326. u8 osb_stackflags;
  327. char osb_cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
  328. char osb_cluster_name[OCFS2_CLUSTER_NAME_LEN + 1];
  329. struct ocfs2_cluster_connection *cconn;
  330. struct ocfs2_lock_res osb_super_lockres;
  331. struct ocfs2_lock_res osb_rename_lockres;
  332. struct ocfs2_lock_res osb_nfs_sync_lockres;
  333. struct rw_semaphore nfs_sync_rwlock;
  334. struct ocfs2_lock_res osb_trim_fs_lockres;
  335. struct mutex obs_trim_fs_mutex;
  336. struct ocfs2_dlm_debug *osb_dlm_debug;
  337. struct dentry *osb_debug_root;
  338. wait_queue_head_t recovery_event;
  339. spinlock_t dc_task_lock;
  340. struct task_struct *dc_task;
  341. wait_queue_head_t dc_event;
  342. unsigned long dc_wake_sequence;
  343. unsigned long dc_work_sequence;
  344. /*
  345. * Any thread can add locks to the list, but the downconvert
  346. * thread is the only one allowed to remove locks. Any change
  347. * to this rule requires updating
  348. * ocfs2_downconvert_thread_do_work().
  349. */
  350. struct list_head blocked_lock_list;
  351. unsigned long blocked_lock_count;
  352. /* List of dquot structures to drop last reference to */
  353. struct llist_head dquot_drop_list;
  354. struct work_struct dquot_drop_work;
  355. wait_queue_head_t osb_mount_event;
  356. /* Truncate log info */
  357. struct inode *osb_tl_inode;
  358. struct buffer_head *osb_tl_bh;
  359. struct delayed_work osb_truncate_log_wq;
  360. atomic_t osb_tl_disable;
  361. /*
  362. * How many clusters in our truncate log.
  363. * It must be protected by osb_tl_inode->i_mutex.
  364. */
  365. unsigned int truncated_clusters;
  366. struct ocfs2_node_map osb_recovering_orphan_dirs;
  367. unsigned int *osb_orphan_wipes;
  368. wait_queue_head_t osb_wipe_event;
  369. struct ocfs2_orphan_scan osb_orphan_scan;
  370. /* used to protect metaecc calculation check of xattr. */
  371. spinlock_t osb_xattr_lock;
  372. unsigned int osb_dx_mask;
  373. u32 osb_dx_seed[4];
  374. /* the group we used to allocate inodes. */
  375. u64 osb_inode_alloc_group;
  376. /* rb tree root for refcount lock. */
  377. struct rb_root osb_rf_lock_tree;
  378. struct ocfs2_refcount_tree *osb_ref_tree_lru;
  379. struct mutex system_file_mutex;
  380. /*
  381. * OCFS2 needs to schedule several different types of work which
  382. * require cluster locking, disk I/O, recovery waits, etc. Since these
  383. * types of work tend to be heavy we avoid using the kernel events
  384. * workqueue and schedule on our own.
  385. */
  386. struct workqueue_struct *ocfs2_wq;
  387. /* sysfs directory per partition */
  388. struct kset *osb_dev_kset;
  389. /* file check related stuff */
  390. struct ocfs2_filecheck_sysfs_entry osb_fc_ent;
  391. };
  392. #define OCFS2_SB(sb) ((struct ocfs2_super *)(sb)->s_fs_info)
  393. /* Useful typedef for passing around journal access functions */
  394. typedef int (*ocfs2_journal_access_func)(handle_t *handle,
  395. struct ocfs2_caching_info *ci,
  396. struct buffer_head *bh, int type);
  397. static inline int ocfs2_should_order_data(struct inode *inode)
  398. {
  399. if (!S_ISREG(inode->i_mode))
  400. return 0;
  401. if (OCFS2_SB(inode->i_sb)->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)
  402. return 0;
  403. return 1;
  404. }
  405. static inline int ocfs2_sparse_alloc(struct ocfs2_super *osb)
  406. {
  407. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC)
  408. return 1;
  409. return 0;
  410. }
  411. static inline int ocfs2_writes_unwritten_extents(struct ocfs2_super *osb)
  412. {
  413. /*
  414. * Support for sparse files is a pre-requisite
  415. */
  416. if (!ocfs2_sparse_alloc(osb))
  417. return 0;
  418. if (osb->s_feature_ro_compat & OCFS2_FEATURE_RO_COMPAT_UNWRITTEN)
  419. return 1;
  420. return 0;
  421. }
  422. static inline int ocfs2_supports_append_dio(struct ocfs2_super *osb)
  423. {
  424. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_APPEND_DIO)
  425. return 1;
  426. return 0;
  427. }
  428. static inline int ocfs2_supports_inline_data(struct ocfs2_super *osb)
  429. {
  430. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_INLINE_DATA)
  431. return 1;
  432. return 0;
  433. }
  434. static inline int ocfs2_supports_xattr(struct ocfs2_super *osb)
  435. {
  436. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR)
  437. return 1;
  438. return 0;
  439. }
  440. static inline int ocfs2_meta_ecc(struct ocfs2_super *osb)
  441. {
  442. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_META_ECC)
  443. return 1;
  444. return 0;
  445. }
  446. static inline int ocfs2_supports_indexed_dirs(struct ocfs2_super *osb)
  447. {
  448. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_INDEXED_DIRS)
  449. return 1;
  450. return 0;
  451. }
  452. static inline int ocfs2_supports_discontig_bg(struct ocfs2_super *osb)
  453. {
  454. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG)
  455. return 1;
  456. return 0;
  457. }
  458. static inline unsigned int ocfs2_link_max(struct ocfs2_super *osb)
  459. {
  460. if (ocfs2_supports_indexed_dirs(osb))
  461. return OCFS2_DX_LINK_MAX;
  462. return OCFS2_LINK_MAX;
  463. }
  464. static inline unsigned int ocfs2_read_links_count(struct ocfs2_dinode *di)
  465. {
  466. u32 nlink = le16_to_cpu(di->i_links_count);
  467. u32 hi = le16_to_cpu(di->i_links_count_hi);
  468. if (di->i_dyn_features & cpu_to_le16(OCFS2_INDEXED_DIR_FL))
  469. nlink |= (hi << OCFS2_LINKS_HI_SHIFT);
  470. return nlink;
  471. }
  472. static inline void ocfs2_set_links_count(struct ocfs2_dinode *di, u32 nlink)
  473. {
  474. u16 lo, hi;
  475. lo = nlink;
  476. hi = nlink >> OCFS2_LINKS_HI_SHIFT;
  477. di->i_links_count = cpu_to_le16(lo);
  478. di->i_links_count_hi = cpu_to_le16(hi);
  479. }
  480. static inline void ocfs2_add_links_count(struct ocfs2_dinode *di, int n)
  481. {
  482. u32 links = ocfs2_read_links_count(di);
  483. links += n;
  484. ocfs2_set_links_count(di, links);
  485. }
  486. static inline int ocfs2_refcount_tree(struct ocfs2_super *osb)
  487. {
  488. if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE)
  489. return 1;
  490. return 0;
  491. }
  492. /* set / clear functions because cluster events can make these happen
  493. * in parallel so we want the transitions to be atomic. this also
  494. * means that any future flags osb_flags must be protected by spinlock
  495. * too! */
  496. static inline void ocfs2_set_osb_flag(struct ocfs2_super *osb,
  497. unsigned long flag)
  498. {
  499. spin_lock(&osb->osb_lock);
  500. osb->osb_flags |= flag;
  501. spin_unlock(&osb->osb_lock);
  502. }
  503. static inline void ocfs2_set_ro_flag(struct ocfs2_super *osb,
  504. int hard)
  505. {
  506. spin_lock(&osb->osb_lock);
  507. osb->osb_flags &= ~(OCFS2_OSB_SOFT_RO|OCFS2_OSB_HARD_RO);
  508. if (hard)
  509. osb->osb_flags |= OCFS2_OSB_HARD_RO;
  510. else
  511. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  512. spin_unlock(&osb->osb_lock);
  513. }
  514. static inline int ocfs2_is_hard_readonly(struct ocfs2_super *osb)
  515. {
  516. int ret;
  517. spin_lock(&osb->osb_lock);
  518. ret = osb->osb_flags & OCFS2_OSB_HARD_RO;
  519. spin_unlock(&osb->osb_lock);
  520. return ret;
  521. }
  522. static inline int ocfs2_is_soft_readonly(struct ocfs2_super *osb)
  523. {
  524. int ret;
  525. spin_lock(&osb->osb_lock);
  526. ret = osb->osb_flags & OCFS2_OSB_SOFT_RO;
  527. spin_unlock(&osb->osb_lock);
  528. return ret;
  529. }
  530. static inline int ocfs2_clusterinfo_valid(struct ocfs2_super *osb)
  531. {
  532. return (osb->s_feature_incompat &
  533. (OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK |
  534. OCFS2_FEATURE_INCOMPAT_CLUSTERINFO));
  535. }
  536. static inline int ocfs2_userspace_stack(struct ocfs2_super *osb)
  537. {
  538. if (ocfs2_clusterinfo_valid(osb) &&
  539. memcmp(osb->osb_cluster_stack, OCFS2_CLASSIC_CLUSTER_STACK,
  540. OCFS2_STACK_LABEL_LEN))
  541. return 1;
  542. return 0;
  543. }
  544. static inline int ocfs2_o2cb_stack(struct ocfs2_super *osb)
  545. {
  546. if (ocfs2_clusterinfo_valid(osb) &&
  547. !memcmp(osb->osb_cluster_stack, OCFS2_CLASSIC_CLUSTER_STACK,
  548. OCFS2_STACK_LABEL_LEN))
  549. return 1;
  550. return 0;
  551. }
  552. static inline int ocfs2_cluster_o2cb_global_heartbeat(struct ocfs2_super *osb)
  553. {
  554. return ocfs2_o2cb_stack(osb) &&
  555. (osb->osb_stackflags & OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT);
  556. }
  557. static inline int ocfs2_mount_local(struct ocfs2_super *osb)
  558. {
  559. return ((osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT)
  560. || (osb->s_mount_opt & OCFS2_MOUNT_NOCLUSTER));
  561. }
  562. static inline int ocfs2_uses_extended_slot_map(struct ocfs2_super *osb)
  563. {
  564. return (osb->s_feature_incompat &
  565. OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP);
  566. }
  567. #define OCFS2_IS_VALID_DINODE(ptr) \
  568. (!strcmp((ptr)->i_signature, OCFS2_INODE_SIGNATURE))
  569. #define OCFS2_IS_VALID_EXTENT_BLOCK(ptr) \
  570. (!strcmp((ptr)->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE))
  571. #define OCFS2_IS_VALID_GROUP_DESC(ptr) \
  572. (!strcmp((ptr)->bg_signature, OCFS2_GROUP_DESC_SIGNATURE))
  573. #define OCFS2_IS_VALID_XATTR_BLOCK(ptr) \
  574. (!strcmp((ptr)->xb_signature, OCFS2_XATTR_BLOCK_SIGNATURE))
  575. #define OCFS2_IS_VALID_DIR_TRAILER(ptr) \
  576. (!strcmp((ptr)->db_signature, OCFS2_DIR_TRAILER_SIGNATURE))
  577. #define OCFS2_IS_VALID_DX_ROOT(ptr) \
  578. (!strcmp((ptr)->dr_signature, OCFS2_DX_ROOT_SIGNATURE))
  579. #define OCFS2_IS_VALID_DX_LEAF(ptr) \
  580. (!strcmp((ptr)->dl_signature, OCFS2_DX_LEAF_SIGNATURE))
  581. #define OCFS2_IS_VALID_REFCOUNT_BLOCK(ptr) \
  582. (!strcmp((ptr)->rf_signature, OCFS2_REFCOUNT_BLOCK_SIGNATURE))
  583. static inline unsigned long ino_from_blkno(struct super_block *sb,
  584. u64 blkno)
  585. {
  586. return (unsigned long)(blkno & (u64)ULONG_MAX);
  587. }
  588. static inline u64 ocfs2_clusters_to_blocks(struct super_block *sb,
  589. u32 clusters)
  590. {
  591. int c_to_b_bits = OCFS2_SB(sb)->s_clustersize_bits -
  592. sb->s_blocksize_bits;
  593. return (u64)clusters << c_to_b_bits;
  594. }
  595. static inline u32 ocfs2_clusters_for_blocks(struct super_block *sb,
  596. u64 blocks)
  597. {
  598. int b_to_c_bits = OCFS2_SB(sb)->s_clustersize_bits -
  599. sb->s_blocksize_bits;
  600. blocks += (1 << b_to_c_bits) - 1;
  601. return (u32)(blocks >> b_to_c_bits);
  602. }
  603. static inline u32 ocfs2_blocks_to_clusters(struct super_block *sb,
  604. u64 blocks)
  605. {
  606. int b_to_c_bits = OCFS2_SB(sb)->s_clustersize_bits -
  607. sb->s_blocksize_bits;
  608. return (u32)(blocks >> b_to_c_bits);
  609. }
  610. static inline unsigned int ocfs2_clusters_for_bytes(struct super_block *sb,
  611. u64 bytes)
  612. {
  613. int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
  614. unsigned int clusters;
  615. bytes += OCFS2_SB(sb)->s_clustersize - 1;
  616. /* OCFS2 just cannot have enough clusters to overflow this */
  617. clusters = (unsigned int)(bytes >> cl_bits);
  618. return clusters;
  619. }
  620. static inline unsigned int ocfs2_bytes_to_clusters(struct super_block *sb,
  621. u64 bytes)
  622. {
  623. int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
  624. unsigned int clusters;
  625. clusters = (unsigned int)(bytes >> cl_bits);
  626. return clusters;
  627. }
  628. static inline u64 ocfs2_blocks_for_bytes(struct super_block *sb,
  629. u64 bytes)
  630. {
  631. bytes += sb->s_blocksize - 1;
  632. return bytes >> sb->s_blocksize_bits;
  633. }
  634. static inline u64 ocfs2_clusters_to_bytes(struct super_block *sb,
  635. u32 clusters)
  636. {
  637. return (u64)clusters << OCFS2_SB(sb)->s_clustersize_bits;
  638. }
  639. static inline u64 ocfs2_block_to_cluster_start(struct super_block *sb,
  640. u64 blocks)
  641. {
  642. int bits = OCFS2_SB(sb)->s_clustersize_bits - sb->s_blocksize_bits;
  643. unsigned int clusters;
  644. clusters = ocfs2_blocks_to_clusters(sb, blocks);
  645. return (u64)clusters << bits;
  646. }
  647. static inline u64 ocfs2_align_bytes_to_clusters(struct super_block *sb,
  648. u64 bytes)
  649. {
  650. int cl_bits = OCFS2_SB(sb)->s_clustersize_bits;
  651. unsigned int clusters;
  652. clusters = ocfs2_clusters_for_bytes(sb, bytes);
  653. return (u64)clusters << cl_bits;
  654. }
  655. static inline u64 ocfs2_align_bytes_to_blocks(struct super_block *sb,
  656. u64 bytes)
  657. {
  658. u64 blocks;
  659. blocks = ocfs2_blocks_for_bytes(sb, bytes);
  660. return blocks << sb->s_blocksize_bits;
  661. }
  662. static inline unsigned long ocfs2_align_bytes_to_sectors(u64 bytes)
  663. {
  664. return (unsigned long)((bytes + 511) >> 9);
  665. }
  666. static inline unsigned int ocfs2_page_index_to_clusters(struct super_block *sb,
  667. unsigned long pg_index)
  668. {
  669. u32 clusters = pg_index;
  670. unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
  671. if (unlikely(PAGE_SHIFT > cbits))
  672. clusters = pg_index << (PAGE_SHIFT - cbits);
  673. else if (PAGE_SHIFT < cbits)
  674. clusters = pg_index >> (cbits - PAGE_SHIFT);
  675. return clusters;
  676. }
  677. /*
  678. * Find the 1st page index which covers the given clusters.
  679. */
  680. static inline pgoff_t ocfs2_align_clusters_to_page_index(struct super_block *sb,
  681. u32 clusters)
  682. {
  683. unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
  684. pgoff_t index = clusters;
  685. if (PAGE_SHIFT > cbits) {
  686. index = (pgoff_t)clusters >> (PAGE_SHIFT - cbits);
  687. } else if (PAGE_SHIFT < cbits) {
  688. index = (pgoff_t)clusters << (cbits - PAGE_SHIFT);
  689. }
  690. return index;
  691. }
  692. static inline unsigned int ocfs2_pages_per_cluster(struct super_block *sb)
  693. {
  694. unsigned int cbits = OCFS2_SB(sb)->s_clustersize_bits;
  695. unsigned int pages_per_cluster = 1;
  696. if (PAGE_SHIFT < cbits)
  697. pages_per_cluster = 1 << (cbits - PAGE_SHIFT);
  698. return pages_per_cluster;
  699. }
  700. static inline unsigned int ocfs2_megabytes_to_clusters(struct super_block *sb,
  701. unsigned int megs)
  702. {
  703. BUILD_BUG_ON(OCFS2_MAX_CLUSTERSIZE > 1048576);
  704. return megs << (20 - OCFS2_SB(sb)->s_clustersize_bits);
  705. }
  706. static inline unsigned int ocfs2_clusters_to_megabytes(struct super_block *sb,
  707. unsigned int clusters)
  708. {
  709. return clusters >> (20 - OCFS2_SB(sb)->s_clustersize_bits);
  710. }
  711. static inline void _ocfs2_set_bit(unsigned int bit, unsigned long *bitmap)
  712. {
  713. __set_bit_le(bit, bitmap);
  714. }
  715. #define ocfs2_set_bit(bit, addr) _ocfs2_set_bit((bit), (unsigned long *)(addr))
  716. static inline void _ocfs2_clear_bit(unsigned int bit, unsigned long *bitmap)
  717. {
  718. __clear_bit_le(bit, bitmap);
  719. }
  720. #define ocfs2_clear_bit(bit, addr) _ocfs2_clear_bit((bit), (unsigned long *)(addr))
  721. #define ocfs2_test_bit test_bit_le
  722. #define ocfs2_find_next_zero_bit find_next_zero_bit_le
  723. #define ocfs2_find_next_bit find_next_bit_le
  724. static inline void *correct_addr_and_bit_unaligned(int *bit, void *addr)
  725. {
  726. #if BITS_PER_LONG == 64
  727. *bit += ((unsigned long) addr & 7UL) << 3;
  728. addr = (void *) ((unsigned long) addr & ~7UL);
  729. #elif BITS_PER_LONG == 32
  730. *bit += ((unsigned long) addr & 3UL) << 3;
  731. addr = (void *) ((unsigned long) addr & ~3UL);
  732. #else
  733. #error "how many bits you are?!"
  734. #endif
  735. return addr;
  736. }
  737. static inline void ocfs2_set_bit_unaligned(int bit, void *bitmap)
  738. {
  739. bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
  740. ocfs2_set_bit(bit, bitmap);
  741. }
  742. static inline void ocfs2_clear_bit_unaligned(int bit, void *bitmap)
  743. {
  744. bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
  745. ocfs2_clear_bit(bit, bitmap);
  746. }
  747. static inline int ocfs2_test_bit_unaligned(int bit, void *bitmap)
  748. {
  749. bitmap = correct_addr_and_bit_unaligned(&bit, bitmap);
  750. return ocfs2_test_bit(bit, bitmap);
  751. }
  752. static inline int ocfs2_find_next_zero_bit_unaligned(void *bitmap, int max,
  753. int start)
  754. {
  755. int fix = 0, ret, tmpmax;
  756. bitmap = correct_addr_and_bit_unaligned(&fix, bitmap);
  757. tmpmax = max + fix;
  758. start += fix;
  759. ret = ocfs2_find_next_zero_bit(bitmap, tmpmax, start) - fix;
  760. if (ret > max)
  761. return max;
  762. return ret;
  763. }
  764. #endif /* OCFS2_H */