1
/* -*- mode: c; c-basic-offset: 8; -*-
2
* vim: noexpandtab sw=8 ts=8 sts=0:
6
* Filesystem object routines for the OCFS2 userspace library.
8
* Copyright (C) 2004 Oracle. All rights reserved.
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU General Public
12
* License, version 2, as published by the Free Software Foundation.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* General Public License for more details.
19
* You should have received a copy of the GNU General Public
20
* License along with this program; if not, write to the
21
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
* Boston, MA 021110-1307, USA.
24
* Authors: Joel Becker
31
# define _XOPEN_SOURCE 600
33
#ifndef _LARGEFILE64_SOURCE
34
# define _LARGEFILE64_SOURCE
46
#include <linux/types.h>
48
#include <et/com_err.h>
50
#include <ocfs2-kernel/kernel-list.h>
51
#include <ocfs2-kernel/sparse_endian_types.h>
52
#include <ocfs2-kernel/ocfs2_fs.h>
53
#include <o2dlm/o2dlm.h>
54
#include <o2cb/o2cb.h>
55
#include <ocfs2/ocfs2_err.h>
56
#include <ocfs2/jbd2.h>
57
#include <ocfs2-kernel/ocfs2_lockid.h>
59
#define OCFS2_LIB_FEATURE_INCOMPAT_SUPP (OCFS2_FEATURE_INCOMPAT_SUPP | \
60
OCFS2_FEATURE_INCOMPAT_HEARTBEAT_DEV | \
61
OCFS2_FEATURE_INCOMPAT_RESIZE_INPROG | \
62
OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT | \
63
OCFS2_FEATURE_INCOMPAT_INLINE_DATA | \
64
OCFS2_FEATURE_INCOMPAT_TUNEFS_INPROG)
66
#define OCFS2_LIB_FEATURE_RO_COMPAT_SUPP OCFS2_FEATURE_RO_COMPAT_SUPP
68
#define OCFS2_LIB_FEATURE_COMPAT_SUPP OCFS2_FEATURE_COMPAT_SUPP
70
#define OCFS2_LIB_ABORTED_TUNEFS_SUPP OCFS2_TUNEFS_INPROG_REMOVE_SLOT
73
/* define OCFS2_SB for ocfs2-tools */
74
#define OCFS2_SB(sb) (sb)
76
/* Flags for the ocfs2_filesys structure */
77
#define OCFS2_FLAG_RO 0x00
78
#define OCFS2_FLAG_RW 0x01
79
#define OCFS2_FLAG_CHANGED 0x02
80
#define OCFS2_FLAG_DIRTY 0x04
81
#define OCFS2_FLAG_SWAP_BYTES 0x08
82
#define OCFS2_FLAG_BUFFERED 0x10
83
#define OCFS2_FLAG_NO_REV_CHECK 0x20 /* Do not check the OCFS
86
#define OCFS2_FLAG_HEARTBEAT_DEV_OK 0x40
87
#define OCFS2_FLAG_STRICT_COMPAT_CHECK 0x80
88
#define OCFS2_FLAG_IMAGE_FILE 0x0100
90
/* Return flags for the directory iterator functions */
91
#define OCFS2_DIRENT_CHANGED 0x01
92
#define OCFS2_DIRENT_ABORT 0x02
93
#define OCFS2_DIRENT_ERROR 0x04
95
/* Directory iterator flags */
96
#define OCFS2_DIRENT_FLAG_INCLUDE_EMPTY 0x01
97
#define OCFS2_DIRENT_FLAG_INCLUDE_REMOVED 0x02
98
#define OCFS2_DIRENT_FLAG_EXCLUDE_DOTS 0x04
99
#define OCFS2_DIRENT_FLAG_INCLUDE_TRAILER 0x08
101
/* Return flags for the chain iterator functions */
102
#define OCFS2_CHAIN_CHANGED 0x01
103
#define OCFS2_CHAIN_ABORT 0x02
104
#define OCFS2_CHAIN_ERROR 0x04
106
/* Directory constants */
107
#define OCFS2_DIRENT_DOT_FILE 1
108
#define OCFS2_DIRENT_DOT_DOT_FILE 2
109
#define OCFS2_DIRENT_OTHER_FILE 3
110
#define OCFS2_DIRENT_DELETED_FILE 4
112
/* Directory scan flags */
113
#define OCFS2_DIR_SCAN_FLAG_EXCLUDE_DOTS 0x01
115
/* Check if mounted flags */
116
#define OCFS2_MF_MOUNTED 1
117
#define OCFS2_MF_ISROOT 2
118
#define OCFS2_MF_READONLY 4
119
#define OCFS2_MF_SWAP 8
120
#define OCFS2_MF_BUSY 16
121
#define OCFS2_MF_MOUNTED_CLUSTER 32
123
/* check_heartbeats progress states */
124
#define OCFS2_CHB_START 1
125
#define OCFS2_CHB_WAITING 2
126
#define OCFS2_CHB_COMPLETE 3
128
typedef void (*ocfs2_chb_notify)(int state, char *progress, void *data);
130
typedef struct _ocfs2_filesys ocfs2_filesys;
131
typedef struct _ocfs2_cached_inode ocfs2_cached_inode;
132
typedef struct _io_channel io_channel;
133
typedef struct _ocfs2_inode_scan ocfs2_inode_scan;
134
typedef struct _ocfs2_dir_scan ocfs2_dir_scan;
135
typedef struct _ocfs2_bitmap ocfs2_bitmap;
136
typedef struct _ocfs2_devices ocfs2_devices;
138
struct _ocfs2_filesys {
142
struct ocfs2_dinode *fs_super;
143
struct ocfs2_dinode *fs_orig_super;
144
unsigned int fs_blocksize;
145
unsigned int fs_clustersize;
146
uint32_t fs_clusters;
149
uint64_t fs_root_blkno;
150
uint64_t fs_sysdir_blkno;
151
uint64_t fs_first_cg_blkno;
152
char uuid_str[OCFS2_VOL_UUID_LEN * 2 + 1];
155
ocfs2_cached_inode *fs_cluster_alloc;
156
ocfs2_cached_inode **fs_inode_allocs;
157
ocfs2_cached_inode *fs_system_inode_alloc;
158
ocfs2_cached_inode **fs_eb_allocs;
159
ocfs2_cached_inode *fs_system_eb_alloc;
161
struct o2dlm_ctxt *fs_dlm_ctxt;
162
struct ocfs2_image_state *ost;
164
/* Reserved for the use of the calling application. */
168
struct _ocfs2_cached_inode {
169
struct _ocfs2_filesys *ci_fs;
171
struct ocfs2_dinode *ci_inode;
172
ocfs2_bitmap *ci_chains;
175
struct ocfs2_slot_data {
177
unsigned int sd_node_num;
180
struct ocfs2_slot_map_data {
182
struct ocfs2_slot_data *md_slots;
185
struct _ocfs2_devices {
186
struct list_head list;
187
char dev_name[PATH_MAX];
191
int fs_type; /* 0=unknown, 1=ocfs, 2=ocfs2 */
193
uint32_t maj_num; /* major number of the device */
194
uint32_t min_num; /* minor number of the device */
195
errcode_t errcode; /* error encountered reading device */
197
struct ocfs2_slot_map_data *map; /* Mounted nodes, must be freed */
200
typedef struct _ocfs2_fs_options ocfs2_fs_options;
202
struct _ocfs2_fs_options {
204
uint32_t opt_incompat;
205
uint32_t opt_ro_compat;
208
errcode_t ocfs2_malloc(unsigned long size, void *ptr);
209
errcode_t ocfs2_malloc0(unsigned long size, void *ptr);
210
errcode_t ocfs2_free(void *ptr);
211
errcode_t ocfs2_realloc(unsigned long size, void *ptr);
212
errcode_t ocfs2_realloc0(unsigned long size, void *ptr,
213
unsigned long old_size);
214
errcode_t ocfs2_malloc_blocks(io_channel *channel, int num_blocks,
216
errcode_t ocfs2_malloc_block(io_channel *channel, void *ptr);
218
errcode_t io_open(const char *name, int flags, io_channel **channel);
219
errcode_t io_close(io_channel *channel);
220
int io_get_error(io_channel *channel);
221
errcode_t io_set_blksize(io_channel *channel, int blksize);
222
int io_get_blksize(io_channel *channel);
223
int io_get_fd(io_channel *channel);
225
/* use ocfs2_read_blocks if your application might handle o2image file */
226
errcode_t io_read_block(io_channel *channel, int64_t blkno, int count,
228
errcode_t io_write_block(io_channel *channel, int64_t blkno, int count,
230
errcode_t io_init_cache(io_channel *channel, size_t nr_blocks);
231
errcode_t io_init_cache_size(io_channel *channel, size_t bytes);
232
void io_destroy_cache(io_channel *channel);
234
errcode_t ocfs2_read_super(ocfs2_filesys *fs, uint64_t superblock, char *sb);
235
/* Writes the main superblock at OCFS2_SUPER_BLOCK_BLKNO */
236
errcode_t ocfs2_write_primary_super(ocfs2_filesys *fs);
237
/* Writes the primary and backups if enabled */
238
errcode_t ocfs2_write_super(ocfs2_filesys *fs);
241
* ocfs2_read_blocks is a wraper around io_read_block. If device is an image-
242
* file it translates disk offset to image offset
244
errcode_t ocfs2_read_blocks(ocfs2_filesys *fs, int64_t blkno, int count,
246
int ocfs2_mount_local(ocfs2_filesys *fs);
247
errcode_t ocfs2_open(const char *name, int flags,
248
unsigned int superblock, unsigned int blksize,
249
ocfs2_filesys **ret_fs);
250
errcode_t ocfs2_flush(ocfs2_filesys *fs);
251
errcode_t ocfs2_close(ocfs2_filesys *fs);
252
void ocfs2_freefs(ocfs2_filesys *fs);
254
void ocfs2_swap_inode_from_cpu(struct ocfs2_dinode *di, size_t blocksize);
255
void ocfs2_swap_inode_to_cpu(struct ocfs2_dinode *di, size_t blocksize);
256
errcode_t ocfs2_read_inode(ocfs2_filesys *fs, uint64_t blkno,
258
errcode_t ocfs2_write_inode(ocfs2_filesys *fs, uint64_t blkno,
260
errcode_t ocfs2_check_directory(ocfs2_filesys *fs, uint64_t dir);
262
errcode_t ocfs2_read_cached_inode(ocfs2_filesys *fs, uint64_t blkno,
263
ocfs2_cached_inode **ret_ci);
264
errcode_t ocfs2_write_cached_inode(ocfs2_filesys *fs,
265
ocfs2_cached_inode *cinode);
266
errcode_t ocfs2_free_cached_inode(ocfs2_filesys *fs,
267
ocfs2_cached_inode *cinode);
269
void ocfs2_swap_extent_list_from_cpu(struct ocfs2_extent_list *el);
270
void ocfs2_swap_extent_list_to_cpu(struct ocfs2_extent_list *el);
271
errcode_t ocfs2_extent_map_get_blocks(ocfs2_cached_inode *cinode,
272
uint64_t v_blkno, int count,
275
uint16_t *extent_flags);
276
errcode_t ocfs2_get_clusters(ocfs2_cached_inode *cinode,
279
uint32_t *num_clusters,
280
uint16_t *extent_flags);
281
int ocfs2_find_leaf(ocfs2_filesys *fs, struct ocfs2_dinode *di,
282
uint32_t cpos, char **leaf_buf);
283
int ocfs2_search_extent_list(struct ocfs2_extent_list *el, uint32_t v_cluster);
284
void ocfs2_swap_journal_superblock(journal_superblock_t *jsb);
285
errcode_t ocfs2_init_journal_superblock(ocfs2_filesys *fs, char *buf,
286
int buflen, uint32_t jrnl_size);
287
errcode_t ocfs2_read_journal_superblock(ocfs2_filesys *fs, uint64_t blkno,
289
errcode_t ocfs2_write_journal_superblock(ocfs2_filesys *fs, uint64_t blkno,
291
errcode_t ocfs2_make_journal(ocfs2_filesys *fs, uint64_t blkno,
292
uint32_t clusters, ocfs2_fs_options *features);
293
errcode_t ocfs2_journal_clear_features(journal_superblock_t *jsb,
294
ocfs2_fs_options *features);
295
errcode_t ocfs2_journal_set_features(journal_superblock_t *jsb,
296
ocfs2_fs_options *features);
297
extern size_t ocfs2_journal_tag_bytes(journal_superblock_t *jsb);
298
extern uint64_t ocfs2_journal_tag_block(journal_block_tag_t *tag,
301
void ocfs2_swap_extent_block_to_cpu(struct ocfs2_extent_block *eb);
302
void ocfs2_swap_extent_block_from_cpu(struct ocfs2_extent_block *eb);
303
errcode_t ocfs2_read_extent_block(ocfs2_filesys *fs, uint64_t blkno,
305
errcode_t ocfs2_read_extent_block_nocheck(ocfs2_filesys *fs, uint64_t blkno,
307
errcode_t ocfs2_write_extent_block(ocfs2_filesys *fs, uint64_t blkno,
309
errcode_t ocfs2_swap_dir_entries_from_cpu(void *buf, uint64_t bytes);
310
errcode_t ocfs2_swap_dir_entries_to_cpu(void *buf, uint64_t bytes);
311
void ocfs2_swap_dir_trailer(struct ocfs2_dir_block_trailer *trailer);
312
errcode_t ocfs2_read_dir_block(ocfs2_filesys *fs, struct ocfs2_dinode *di,
313
uint64_t block, void *buf);
314
errcode_t ocfs2_write_dir_block(ocfs2_filesys *fs, struct ocfs2_dinode *di,
315
uint64_t block, void *buf);
316
unsigned int ocfs2_dir_trailer_blk_off(ocfs2_filesys *fs);
317
struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_block(ocfs2_filesys *fs,
319
int ocfs2_supports_dir_trailer(ocfs2_filesys *fs);
320
int ocfs2_dir_has_trailer(ocfs2_filesys *fs, struct ocfs2_dinode *di);
321
int ocfs2_skip_dir_trailer(ocfs2_filesys *fs, struct ocfs2_dinode *di,
322
struct ocfs2_dir_entry *de, unsigned long offset);
323
void ocfs2_init_dir_trailer(ocfs2_filesys *fs, struct ocfs2_dinode *di,
324
uint64_t blkno, void *buf);
326
errcode_t ocfs2_dir_iterate2(ocfs2_filesys *fs,
330
int (*func)(uint64_t dir,
332
struct ocfs2_dir_entry *dirent,
338
extern errcode_t ocfs2_dir_iterate(ocfs2_filesys *fs,
342
int (*func)(struct ocfs2_dir_entry *dirent,
349
errcode_t ocfs2_lookup(ocfs2_filesys *fs, uint64_t dir,
350
const char *name, int namelen, char *buf,
353
errcode_t ocfs2_lookup_system_inode(ocfs2_filesys *fs, int type,
354
int slot_num, uint64_t *blkno);
356
errcode_t ocfs2_link(ocfs2_filesys *fs, uint64_t dir, const char *name,
357
uint64_t ino, int flags);
359
errcode_t ocfs2_unlink(ocfs2_filesys *fs, uint64_t dir,
360
const char *name, uint64_t ino, int flags);
362
errcode_t ocfs2_open_inode_scan(ocfs2_filesys *fs,
363
ocfs2_inode_scan **ret_scan);
364
void ocfs2_close_inode_scan(ocfs2_inode_scan *scan);
365
errcode_t ocfs2_get_next_inode(ocfs2_inode_scan *scan,
366
uint64_t *blkno, char *inode);
368
errcode_t ocfs2_open_dir_scan(ocfs2_filesys *fs, uint64_t dir, int flags,
369
ocfs2_dir_scan **ret_scan);
370
void ocfs2_close_dir_scan(ocfs2_dir_scan *scan);
371
errcode_t ocfs2_get_next_dir_entry(ocfs2_dir_scan *scan,
372
struct ocfs2_dir_entry *dirent);
374
errcode_t ocfs2_cluster_bitmap_new(ocfs2_filesys *fs,
375
const char *description,
376
ocfs2_bitmap **ret_bitmap);
377
errcode_t ocfs2_block_bitmap_new(ocfs2_filesys *fs,
378
const char *description,
379
ocfs2_bitmap **ret_bitmap);
380
void ocfs2_bitmap_free(ocfs2_bitmap *bitmap);
381
errcode_t ocfs2_bitmap_set(ocfs2_bitmap *bitmap, uint64_t bitno,
383
errcode_t ocfs2_bitmap_clear(ocfs2_bitmap *bitmap, uint64_t bitno,
385
errcode_t ocfs2_bitmap_test(ocfs2_bitmap *bitmap, uint64_t bitno,
387
errcode_t ocfs2_bitmap_find_next_set(ocfs2_bitmap *bitmap,
388
uint64_t start, uint64_t *found);
389
errcode_t ocfs2_bitmap_find_next_clear(ocfs2_bitmap *bitmap,
390
uint64_t start, uint64_t *found);
391
errcode_t ocfs2_bitmap_read(ocfs2_bitmap *bitmap);
392
errcode_t ocfs2_bitmap_write(ocfs2_bitmap *bitmap);
393
uint64_t ocfs2_bitmap_get_set_bits(ocfs2_bitmap *bitmap);
394
errcode_t ocfs2_bitmap_alloc_range(ocfs2_bitmap *bitmap, uint64_t min,
395
uint64_t len, uint64_t *first_bit,
396
uint64_t *bits_found);
397
errcode_t ocfs2_bitmap_clear_range(ocfs2_bitmap *bitmap, uint64_t len,
400
errcode_t ocfs2_get_device_size(const char *file, int blocksize,
401
uint64_t *retblocks);
403
errcode_t ocfs2_get_device_sectsize(const char *file, int *sectsize);
405
errcode_t ocfs2_check_if_mounted(const char *file, int *mount_flags);
406
errcode_t ocfs2_check_mount_point(const char *device, int *mount_flags,
407
char *mtpt, int mtlen);
409
errcode_t ocfs2_read_whole_file(ocfs2_filesys *fs, uint64_t blkno,
410
char **buf, int *len);
412
void ocfs2_swap_disk_heartbeat_block(struct o2hb_disk_heartbeat_block *hb);
413
errcode_t ocfs2_check_heartbeat(char *device, int *mount_flags,
414
struct list_head *nodes_list);
416
errcode_t ocfs2_check_heartbeats(struct list_head *dev_list, int ignore_local);
418
errcode_t ocfs2_get_ocfs1_label(char *device, uint8_t *label, uint16_t label_len,
419
uint8_t *uuid, uint16_t uuid_len);
421
void ocfs2_swap_group_desc(struct ocfs2_group_desc *gd);
422
errcode_t ocfs2_read_group_desc(ocfs2_filesys *fs, uint64_t blkno,
425
errcode_t ocfs2_write_group_desc(ocfs2_filesys *fs, uint64_t blkno,
428
errcode_t ocfs2_chain_iterate(ocfs2_filesys *fs,
430
int (*func)(ocfs2_filesys *fs,
436
errcode_t ocfs2_load_chain_allocator(ocfs2_filesys *fs,
437
ocfs2_cached_inode *cinode);
438
errcode_t ocfs2_write_chain_allocator(ocfs2_filesys *fs,
439
ocfs2_cached_inode *cinode);
440
errcode_t ocfs2_chain_alloc(ocfs2_filesys *fs,
441
ocfs2_cached_inode *cinode,
444
errcode_t ocfs2_chain_free(ocfs2_filesys *fs,
445
ocfs2_cached_inode *cinode,
447
errcode_t ocfs2_chain_alloc_range(ocfs2_filesys *fs,
448
ocfs2_cached_inode *cinode,
452
uint64_t *bits_found);
453
errcode_t ocfs2_chain_free_range(ocfs2_filesys *fs,
454
ocfs2_cached_inode *cinode,
457
errcode_t ocfs2_chain_test(ocfs2_filesys *fs,
458
ocfs2_cached_inode *cinode,
461
errcode_t ocfs2_chain_force_val(ocfs2_filesys *fs,
462
ocfs2_cached_inode *cinode,
466
errcode_t ocfs2_chain_add_group(ocfs2_filesys *fs,
467
ocfs2_cached_inode *cinode);
469
errcode_t ocfs2_init_dir(ocfs2_filesys *fs,
471
uint64_t parent_dir);
473
errcode_t ocfs2_expand_dir(ocfs2_filesys *fs,
476
errcode_t ocfs2_test_inode_allocated(ocfs2_filesys *fs, uint64_t blkno,
478
void ocfs2_init_group_desc(ocfs2_filesys *fs,
479
struct ocfs2_group_desc *gd,
480
uint64_t blkno, uint32_t generation,
481
uint64_t parent_inode, uint16_t bits,
484
errcode_t ocfs2_new_dir_block(ocfs2_filesys *fs, uint64_t dir_ino,
485
uint64_t parent_ino, char **block);
487
errcode_t ocfs2_insert_extent(ocfs2_filesys *fs, uint64_t ino, uint32_t cpos,
488
uint64_t c_blkno, uint32_t clusters,
490
errcode_t ocfs2_cached_inode_insert_extent(ocfs2_cached_inode *ci,
491
uint32_t cpos, uint64_t c_blkno,
492
uint32_t clusters, uint16_t flag);
494
void ocfs2_dinode_new_extent_list(ocfs2_filesys *fs, struct ocfs2_dinode *di);
495
void ocfs2_set_inode_data_inline(ocfs2_filesys *fs, struct ocfs2_dinode *di);
496
errcode_t ocfs2_convert_inline_data_to_extents(ocfs2_cached_inode *ci);
497
errcode_t ocfs2_new_inode(ocfs2_filesys *fs, uint64_t *ino, int mode);
498
errcode_t ocfs2_new_system_inode(ocfs2_filesys *fs, uint64_t *ino, int mode, int flags);
499
errcode_t ocfs2_delete_inode(ocfs2_filesys *fs, uint64_t ino);
500
errcode_t ocfs2_new_extent_block(ocfs2_filesys *fs, uint64_t *blkno);
501
errcode_t ocfs2_delete_extent_block(ocfs2_filesys *fs, uint64_t blkno);
503
* Allocate the blocks and insert them to the file.
504
* only i_clusters of dinode will be updated accordingly, i_size not changed.
506
errcode_t ocfs2_extend_allocation(ocfs2_filesys *fs, uint64_t ino,
507
uint32_t new_clusters);
508
/* Extend the file to the new size. No clusters will be allocated. */
509
errcode_t ocfs2_extend_file(ocfs2_filesys *fs, uint64_t ino, uint64_t new_size);
511
int ocfs2_mark_extent_written(ocfs2_filesys *fs, struct ocfs2_dinode *di,
512
uint32_t cpos, uint32_t len, uint64_t p_blkno);
513
/* Reserve spaces at "offset" with a "len" in the files. */
514
errcode_t ocfs2_allocate_unwritten_extents(ocfs2_filesys *fs, uint64_t ino,
515
uint64_t offset, uint64_t len);
517
errcode_t ocfs2_truncate(ocfs2_filesys *fs, uint64_t ino, uint64_t new_i_size);
518
errcode_t ocfs2_zero_tail_and_truncate(ocfs2_filesys *fs,
519
ocfs2_cached_inode *ci,
521
uint32_t *new_clusters);
522
errcode_t ocfs2_new_clusters(ocfs2_filesys *fs,
525
uint64_t *start_blkno,
526
uint32_t *clusters_found);
527
errcode_t ocfs2_test_cluster_allocated(ocfs2_filesys *fs, uint32_t cpos,
529
errcode_t ocfs2_new_specific_cluster(ocfs2_filesys *fs, uint32_t cpos);
530
errcode_t ocfs2_free_clusters(ocfs2_filesys *fs,
532
uint64_t start_blkno);
533
errcode_t ocfs2_test_clusters(ocfs2_filesys *fs,
535
uint64_t start_blkno,
539
errcode_t ocfs2_lookup(ocfs2_filesys *fs, uint64_t dir, const char *name,
540
int namelen, char *buf, uint64_t *inode);
542
errcode_t ocfs2_namei(ocfs2_filesys *fs, uint64_t root, uint64_t cwd,
543
const char *name, uint64_t *inode);
545
errcode_t ocfs2_namei_follow(ocfs2_filesys *fs, uint64_t root, uint64_t cwd,
546
const char *name, uint64_t *inode);
548
errcode_t ocfs2_follow_link(ocfs2_filesys *fs, uint64_t root, uint64_t cwd,
549
uint64_t inode, uint64_t *res_inode);
551
errcode_t ocfs2_file_read(ocfs2_cached_inode *ci, void *buf, uint32_t count,
552
uint64_t offset, uint32_t *got);
554
errcode_t ocfs2_file_write(ocfs2_cached_inode *ci, void *buf, uint32_t count,
555
uint64_t offset, uint32_t *wrote);
557
errcode_t ocfs2_fill_cluster_desc(ocfs2_filesys *fs,
558
struct o2cb_cluster_desc *desc);
559
errcode_t ocfs2_set_cluster_desc(ocfs2_filesys *fs,
560
struct o2cb_cluster_desc *desc);
561
errcode_t ocfs2_fill_heartbeat_desc(ocfs2_filesys *fs,
562
struct o2cb_region_desc *desc);
564
errcode_t ocfs2_lock_down_cluster(ocfs2_filesys *fs);
566
errcode_t ocfs2_release_cluster(ocfs2_filesys *fs);
568
errcode_t ocfs2_initialize_dlm(ocfs2_filesys *fs, const char *service);
570
errcode_t ocfs2_shutdown_dlm(ocfs2_filesys *fs, const char *service);
572
errcode_t ocfs2_super_lock(ocfs2_filesys *fs);
574
errcode_t ocfs2_super_unlock(ocfs2_filesys *fs);
576
errcode_t ocfs2_meta_lock(ocfs2_filesys *fs, ocfs2_cached_inode *inode,
577
enum o2dlm_lock_level level, int flags);
579
errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs, ocfs2_cached_inode *ci);
582
void ocfs2_swap_slot_map(struct ocfs2_slot_map *sm, int num_slots);
583
void ocfs2_swap_slot_map_extended(struct ocfs2_slot_map_extended *se,
585
errcode_t ocfs2_read_slot_map(ocfs2_filesys *fs,
587
struct ocfs2_slot_map **map_ret);
588
errcode_t ocfs2_read_slot_map_extended(ocfs2_filesys *fs,
590
struct ocfs2_slot_map_extended **map_ret);
591
errcode_t ocfs2_write_slot_map(ocfs2_filesys *fs,
593
struct ocfs2_slot_map *sm);
594
errcode_t ocfs2_write_slot_map_extended(ocfs2_filesys *fs,
596
struct ocfs2_slot_map_extended *se);
598
/* High level functions for metadata ecc */
599
void ocfs2_compute_meta_ecc(ocfs2_filesys *fs, void *data,
600
struct ocfs2_block_check *bc);
601
errcode_t ocfs2_validate_meta_ecc(ocfs2_filesys *fs, void *data,
602
struct ocfs2_block_check *bc);
603
/* Low level checksum compute functions. Use the high-level ones. */
604
extern void ocfs2_block_check_compute(void *data, size_t blocksize,
605
struct ocfs2_block_check *bc);
606
extern errcode_t ocfs2_block_check_validate(void *data, size_t blocksize,
607
struct ocfs2_block_check *bc);
610
errcode_t ocfs2_format_slot_map(ocfs2_filesys *fs);
611
errcode_t ocfs2_load_slot_map(ocfs2_filesys *fs,
612
struct ocfs2_slot_map_data **data_ret);
613
errcode_t ocfs2_store_slot_map(ocfs2_filesys *fs,
614
struct ocfs2_slot_map_data *md);
616
enum ocfs2_lock_type ocfs2_get_lock_type(char c);
618
char *ocfs2_get_lock_type_string(enum ocfs2_lock_type type);
620
errcode_t ocfs2_encode_lockres(enum ocfs2_lock_type type, uint64_t blkno,
621
uint32_t generation, uint64_t parent,
624
errcode_t ocfs2_decode_lockres(char *lockres, enum ocfs2_lock_type *type,
625
uint64_t *blkno, uint32_t *generation,
628
errcode_t ocfs2_printable_lockres(char *lockres, char *name, int len);
630
/* write the superblock at the specific block. */
631
errcode_t ocfs2_write_backup_super(ocfs2_filesys *fs, uint64_t blkno);
633
/* Get the blkno according to the file system info.
634
* The unused ones, depending on the volume size, are zeroed.
635
* Return the length of the block array.
637
int ocfs2_get_backup_super_offsets(ocfs2_filesys *fs,
638
uint64_t *blocks, size_t len);
640
/* This function will get the superblock pointed to by fs and copy it to
641
* the blocks. But first it will ensure all the appropriate clusters are free.
642
* If not, it will error out with ENOSPC. If free, it will set bits for all
643
* the clusters, zero the clusters and write the backup sb.
644
* In case of updating, it will override the backup blocks with the newest
645
* superblock information.
647
errcode_t ocfs2_set_backup_super_list(ocfs2_filesys *fs,
648
uint64_t *blocks, size_t len);
649
/* Conversely, this clears all the allocator bits associated with the
650
* specified backup superblocks */
651
errcode_t ocfs2_clear_backup_super_list(ocfs2_filesys *fs,
652
uint64_t *blocks, size_t len);
654
/* Refresh the backup superblock information */
655
errcode_t ocfs2_refresh_backup_supers(ocfs2_filesys *fs);
656
/* Refresh a specific list of backup superblocks */
657
errcode_t ocfs2_refresh_backup_super_list(ocfs2_filesys *fs,
658
uint64_t *blocks, size_t len);
660
errcode_t ocfs2_read_backup_super(ocfs2_filesys *fs, int backup, char *sbbuf);
662
/* get the virtual offset of the last allocated cluster. */
663
errcode_t ocfs2_get_last_cluster_offset(ocfs2_filesys *fs,
664
struct ocfs2_dinode *di,
665
uint32_t *v_cluster);
667
/* Filesystem features */
668
enum ocfs2_feature_levels {
669
OCFS2_FEATURE_LEVEL_DEFAULT = 0,
670
OCFS2_FEATURE_LEVEL_MAX_COMPAT,
671
OCFS2_FEATURE_LEVEL_MAX_FEATURES,
674
errcode_t ocfs2_snprint_feature_flags(char *str, size_t size,
675
ocfs2_fs_options *flags);
676
errcode_t ocfs2_snprint_tunefs_flags(char *str, size_t size, uint16_t flags);
677
errcode_t ocfs2_parse_feature(const char *opts,
678
ocfs2_fs_options *feature_flags,
679
ocfs2_fs_options *reverse_flags);
681
errcode_t ocfs2_parse_feature_level(const char *typestr,
682
enum ocfs2_feature_levels *level);
684
errcode_t ocfs2_merge_feature_flags_with_level(ocfs2_fs_options *dest,
686
ocfs2_fs_options *feature_set,
687
ocfs2_fs_options *reverse_set);
690
* Get a callback with each feature in feature_set in order. This will
691
* calculate the dependencies of each feature in feature_set, then call func
692
* once per feature, with only that feature passed to func.
694
void ocfs2_feature_foreach(ocfs2_fs_options *feature_set,
695
int (*func)(ocfs2_fs_options *feature,
698
/* The reverse function. It will call the features in reverse order. */
699
void ocfs2_feature_reverse_foreach(ocfs2_fs_options *reverse_set,
700
int (*func)(ocfs2_fs_options *feature,
705
/* These are deprecated names - don't use them */
706
int ocfs2_get_backup_super_offset(ocfs2_filesys *fs,
707
uint64_t *blocks, size_t len);
708
errcode_t ocfs2_refresh_backup_super(ocfs2_filesys *fs,
709
uint64_t *blocks, size_t len);
710
errcode_t ocfs2_set_backup_super(ocfs2_filesys *fs,
711
uint64_t *blocks, size_t len);
716
* ${foo}_to_${bar} is a floor function. blocks_to_clusters will
717
* returns the cluster that contains a block, not the number of clusters
718
* that hold a given number of blocks.
720
* ${foo}_in_${bar} is a ceiling function. clusters_in_blocks will give
721
* the number of clusters needed to hold a given number of blocks.
723
* These functions return UINTxx_MAX when they overflow, but UINTxx_MAX
724
* cannot be used to check overflow. UINTxx_MAX is a valid value in much
725
* of ocfs2. The caller is responsible for preventing overflow before
726
* using these functions.
729
static inline uint64_t ocfs2_clusters_to_blocks(ocfs2_filesys *fs,
733
OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits -
734
OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
736
return (uint64_t)clusters << c_to_b_bits;
739
static inline uint32_t ocfs2_blocks_to_clusters(ocfs2_filesys *fs,
743
OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits -
744
OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
745
uint64_t ret = blocks >> b_to_c_bits;
747
if (ret > UINT32_MAX)
750
return (uint32_t)ret;
753
static inline uint64_t ocfs2_clusters_to_bytes(ocfs2_filesys *fs,
756
uint64_t ret = clusters;
758
ret = ret << OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits;
765
static inline uint32_t ocfs2_bytes_to_clusters(ocfs2_filesys *fs,
769
bytes >> OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits;
771
if (ret > UINT32_MAX)
774
return (uint32_t)ret;
777
static inline uint64_t ocfs2_blocks_to_bytes(ocfs2_filesys *fs,
781
blocks << OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
789
static inline uint64_t ocfs2_bytes_to_blocks(ocfs2_filesys *fs,
792
return bytes >> OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
795
static inline uint32_t ocfs2_clusters_in_blocks(ocfs2_filesys *fs,
798
int c_to_b_bits = OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits -
799
OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
800
uint64_t ret = blocks + ((1 << c_to_b_bits) - 1);
802
if (ret < blocks) /* deal with wrapping */
805
ret = ret >> c_to_b_bits;
806
if (ret > UINT32_MAX)
809
return (uint32_t)ret;
812
static inline uint32_t ocfs2_clusters_in_bytes(ocfs2_filesys *fs,
815
uint64_t ret = bytes + fs->fs_clustersize - 1;
817
if (ret < bytes) /* deal with wrapping */
820
ret = ret >> OCFS2_RAW_SB(fs->fs_super)->s_clustersize_bits;
821
if (ret > UINT32_MAX)
824
return (uint32_t)ret;
827
static inline uint64_t ocfs2_blocks_in_bytes(ocfs2_filesys *fs,
830
uint64_t ret = bytes + fs->fs_blocksize - 1;
832
if (ret < bytes) /* deal with wrapping */
835
return ret >> OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
839
/* given a cluster offset, calculate which block group it belongs to
840
* and return that block offset. */
841
static inline uint64_t ocfs2_which_cluster_group(ocfs2_filesys *fs,
845
struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
848
group_no = cluster / cpg;
850
return sb->s_first_cluster_group;
851
return ocfs2_clusters_to_blocks(fs, group_no * cpg);
854
static inline int ocfs2_block_out_of_range(ocfs2_filesys *fs, uint64_t block)
856
return (block < OCFS2_SUPER_BLOCK_BLKNO) || (block > fs->fs_blocks);
859
struct ocfs2_cluster_group_sizes {
861
uint16_t cgs_tail_group_bits;
862
uint32_t cgs_cluster_groups;
864
static inline void ocfs2_calc_cluster_groups(uint64_t clusters,
866
struct ocfs2_cluster_group_sizes *cgs)
868
uint16_t max_bits = 8 * ocfs2_group_bitmap_size(blocksize);
870
cgs->cgs_cpg = max_bits;
871
if (max_bits > clusters)
872
cgs->cgs_cpg = clusters;
874
cgs->cgs_cluster_groups = (clusters + cgs->cgs_cpg - 1) /
877
cgs->cgs_tail_group_bits = clusters % cgs->cgs_cpg;
878
if (cgs->cgs_tail_group_bits == 0)
879
cgs->cgs_tail_group_bits = cgs->cgs_cpg;
883
* This is only valid for leaf nodes, which are the only ones that can
884
* have empty extents anyway.
886
static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
888
return !rec->e_leaf_clusters;
892
* Helper function to look at the # of clusters in an extent record.
894
static inline uint32_t ocfs2_rec_clusters(uint16_t tree_depth,
895
struct ocfs2_extent_rec *rec)
898
* Cluster count in extent records is slightly different
899
* between interior nodes and leaf nodes. This is to support
900
* unwritten extents which need a flags field in leaf node
901
* records, thus shrinking the available space for a clusters
905
return rec->e_int_clusters;
907
return rec->e_leaf_clusters;
910
static inline void ocfs2_set_rec_clusters(uint16_t tree_depth,
911
struct ocfs2_extent_rec *rec,
915
rec->e_int_clusters = clusters;
917
rec->e_leaf_clusters = clusters;
920
static inline int ocfs2_sparse_alloc(struct ocfs2_super_block *osb)
922
if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC)
927
static inline int ocfs2_userspace_stack(struct ocfs2_super_block *osb)
929
if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK)
934
static inline int ocfs2_writes_unwritten_extents(struct ocfs2_super_block *osb)
937
* Support for sparse files is a pre-requisite
939
if (!ocfs2_sparse_alloc(osb))
942
if (osb->s_feature_ro_compat & OCFS2_FEATURE_RO_COMPAT_UNWRITTEN)
947
static inline int ocfs2_uses_extended_slot_map(struct ocfs2_super_block *osb)
949
if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP)
954
static inline int ocfs2_support_inline_data(struct ocfs2_super_block *osb)
956
if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_INLINE_DATA)
961
static inline int ocfs2_meta_ecc(struct ocfs2_super_block *osb)
963
if (OCFS2_HAS_INCOMPAT_FEATURE(osb,
964
OCFS2_FEATURE_INCOMPAT_META_ECC))
969
static inline int ocfs2_support_xattr(struct ocfs2_super_block *osb)
971
if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR)
977
* shamelessly lifted from the kernel
979
* min()/max() macros that also do
980
* strict type-checking.. See the
981
* "unnecessary" pointer comparison.
983
#define ocfs2_min(x,y) ({ \
984
const typeof(x) _x = (x); \
985
const typeof(y) _y = (y); \
986
(void) (&_x == &_y); \
987
_x < _y ? _x : _y; })
989
#define ocfs2_max(x,y) ({ \
990
const typeof(x) _x = (x); \
991
const typeof(y) _y = (y); \
992
(void) (&_x == &_y); \
993
_x > _y ? _x : _y; })
995
/* lifted from the kernel. include/linux/kernel.h */
996
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
999
* DEPRECATED: Extent/block iterate functions.
1001
* Do not use these for reading/writing regular files - they don't properly
1002
* handle holes or inline data.
1005
/* Return flags for the extent iterator functions */
1006
#define OCFS2_EXTENT_CHANGED 0x01
1007
#define OCFS2_EXTENT_ABORT 0x02
1008
#define OCFS2_EXTENT_ERROR 0x04
1011
* Extent iterate flags
1013
* OCFS2_EXTENT_FLAG_APPEND indicates that the iterator function should
1014
* be called on extents past the leaf next_free_rec. This is used by
1015
* ocfs2_expand_dir() to add a new extent to a directory (via
1016
* OCFS2_BLOCK_FLAG_APPEND and the block iteration functions).
1018
* OCFS2_EXTENT_FLAG_DEPTH_TRAVERSE indicates that the iterator
1019
* function for tree_depth > 0 records (ocfs2_extent_blocks, iow)
1020
* should be called after all of the extents contained in the
1021
* extent_block are processed. This is useful if you are going to be
1022
* deallocating extents.
1024
* OCFS2_EXTENT_FLAG_DATA_ONLY indicates that the iterator function
1025
* should be called for data extents (depth == 0) only.
1027
#define OCFS2_EXTENT_FLAG_APPEND 0x01
1028
#define OCFS2_EXTENT_FLAG_DEPTH_TRAVERSE 0x02
1029
#define OCFS2_EXTENT_FLAG_DATA_ONLY 0x04
1032
/* Return flags for the block iterator functions */
1033
#define OCFS2_BLOCK_CHANGED 0x01
1034
#define OCFS2_BLOCK_ABORT 0x02
1035
#define OCFS2_BLOCK_ERROR 0x04
1038
* Block iterate flags
1040
* In OCFS2, block iteration runs through the blocks contained in an
1041
* inode's data extents. As such, "DATA_ONLY" and "DEPTH_TRAVERSE"
1042
* can't really apply.
1044
* OCFS2_BLOCK_FLAG_APPEND is as OCFS2_EXTENT_FLAG_APPEND, except on a
1045
* blocksize basis. This may mean that the underlying extent already
1046
* contains the space for a new block, and i_size is updated
1049
#define OCFS2_BLOCK_FLAG_APPEND 0x01
1051
errcode_t ocfs2_extent_iterate(ocfs2_filesys *fs,
1055
int (*func)(ocfs2_filesys *fs,
1056
struct ocfs2_extent_rec *rec,
1063
errcode_t ocfs2_extent_iterate_inode(ocfs2_filesys *fs,
1064
struct ocfs2_dinode *inode,
1067
int (*func)(ocfs2_filesys *fs,
1068
struct ocfs2_extent_rec *rec,
1075
errcode_t ocfs2_block_iterate(ocfs2_filesys *fs,
1078
int (*func)(ocfs2_filesys *fs,
1084
errcode_t ocfs2_block_iterate_inode(ocfs2_filesys *fs,
1085
struct ocfs2_dinode *inode,
1087
int (*func)(ocfs2_filesys *fs,
1094
uint32_t ocfs2_xattr_uuid_hash(unsigned char *uuid);
1095
uint32_t ocfs2_xattr_name_hash(uint32_t uuid_hash, const char *name,
1097
int ocfs2_xattr_find_leaf(ocfs2_filesys *fs, struct ocfs2_xattr_block *xb,
1098
uint32_t cpos, char **leaf_buf);
1099
uint16_t ocfs2_xattr_buckets_per_cluster(ocfs2_filesys *fs);
1100
uint16_t ocfs2_blocks_per_xattr_bucket(ocfs2_filesys *fs);
1101
void ocfs2_swap_xattrs_to_cpu(struct ocfs2_xattr_header *xh);
1102
void ocfs2_swap_xattrs_from_cpu(struct ocfs2_xattr_header *xh);
1103
void ocfs2_swap_xattr_block_to_cpu(struct ocfs2_xattr_block *xb);
1104
void ocfs2_swap_xattr_block_from_cpu(struct ocfs2_xattr_block *xb);
1105
errcode_t ocfs2_read_xattr_block(ocfs2_filesys *fs,
1108
errcode_t ocfs2_write_xattr_block(ocfs2_filesys *fs,
1111
errcode_t ocfs2_xattr_get_rec(ocfs2_filesys *fs,
1112
struct ocfs2_xattr_block *xb,
1116
uint32_t *num_clusters);
1117
uint16_t ocfs2_xattr_value_real_size(uint16_t name_len, uint16_t value_len);
1118
uint16_t ocfs2_xattr_min_offset(struct ocfs2_xattr_header *xh, uint16_t size);
1119
uint16_t ocfs2_xattr_name_value_len(struct ocfs2_xattr_header *xh);
1120
errcode_t ocfs2_read_xattr_bucket(ocfs2_filesys *fs,
1123
errcode_t ocfs2_write_xattr_bucket(ocfs2_filesys *fs,
1126
errcode_t ocfs2_xattr_value_truncate(ocfs2_filesys *fs,
1127
struct ocfs2_xattr_value_root *xv);
1128
errcode_t ocfs2_xattr_tree_truncate(ocfs2_filesys *fs,
1129
struct ocfs2_xattr_tree_root *xt);
1130
errcode_t ocfs2_extent_iterate_xattr(ocfs2_filesys *fs,
1131
struct ocfs2_extent_list *el,
1132
uint64_t last_eb_blk,
1134
int (*func)(ocfs2_filesys *fs,
1135
struct ocfs2_extent_rec *rec,
1143
errcode_t ocfs2_delete_xattr_block(ocfs2_filesys *fs, uint64_t blkno);
1145
#endif /* _FILESYS_H */