1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * NILFS local header file. 4 * 5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 6 * 7 * Written by Koji Sato and Ryusuke Konishi. 8 */ 9 10 #ifndef _NILFS_H 11 #define _NILFS_H 12 13 #include <linux/kernel.h> 14 #include <linux/buffer_head.h> 15 #include <linux/spinlock.h> 16 #include <linux/blkdev.h> 17 #include <linux/fs_struct.h> 18 #include <linux/nilfs2_api.h> 19 #include <linux/nilfs2_ondisk.h> 20 #include "the_nilfs.h" 21 #include "bmap.h" 22 23 /** 24 * struct nilfs_inode_info - nilfs inode data in memory 25 * @i_flags: inode flags 26 * @i_type: inode type (combination of flags that inidicate usage) 27 * @i_state: dynamic state flags 28 * @i_bmap: pointer on i_bmap_data 29 * @i_bmap_data: raw block mapping 30 * @i_xattr: <TODO> 31 * @i_dir_start_lookup: page index of last successful search 32 * @i_cno: checkpoint number for GC inode 33 * @i_assoc_inode: associated inode (B-tree node cache holder or back pointer) 34 * @i_dirty: list for connecting dirty files 35 * @xattr_sem: semaphore for extended attributes processing 36 * @i_bh: buffer contains disk inode 37 * @i_root: root object of the current filesystem tree 38 * @vfs_inode: VFS inode object 39 */ 40 struct nilfs_inode_info { 41 __u32 i_flags; 42 unsigned int i_type; 43 unsigned long i_state; /* Dynamic state flags */ 44 struct nilfs_bmap *i_bmap; 45 struct nilfs_bmap i_bmap_data; 46 __u64 i_xattr; /* sector_t ??? */ 47 __u32 i_dir_start_lookup; 48 __u64 i_cno; /* check point number for GC inode */ 49 struct inode *i_assoc_inode; 50 struct list_head i_dirty; /* List for connecting dirty files */ 51 52 #ifdef CONFIG_NILFS_XATTR 53 /* 54 * Extended attributes can be read independently of the main file 55 * data. Taking i_sem even when reading would cause contention 56 * between readers of EAs and writers of regular file data, so 57 * instead we synchronize on xattr_sem when reading or changing 58 * EAs. 59 */ 60 struct rw_semaphore xattr_sem; 61 #endif 62 struct buffer_head *i_bh; /* 63 * i_bh contains a new or dirty 64 * disk inode. 65 */ 66 struct nilfs_root *i_root; 67 struct inode vfs_inode; 68 }; 69 70 static inline struct nilfs_inode_info *NILFS_I(const struct inode *inode) 71 { 72 return container_of(inode, struct nilfs_inode_info, vfs_inode); 73 } 74 75 static inline struct nilfs_inode_info * 76 NILFS_BMAP_I(const struct nilfs_bmap *bmap) 77 { 78 return container_of(bmap, struct nilfs_inode_info, i_bmap_data); 79 } 80 81 /* 82 * Dynamic state flags of NILFS on-memory inode (i_state) 83 */ 84 enum { 85 NILFS_I_NEW = 0, /* Inode is newly created */ 86 NILFS_I_DIRTY, /* The file is dirty */ 87 NILFS_I_QUEUED, /* inode is in dirty_files list */ 88 NILFS_I_BUSY, /* 89 * Inode is grabbed by a segment 90 * constructor 91 */ 92 NILFS_I_COLLECTED, /* All dirty blocks are collected */ 93 NILFS_I_UPDATED, /* The file has been written back */ 94 NILFS_I_INODE_SYNC, /* dsync is not allowed for inode */ 95 NILFS_I_BMAP, /* has bmap and btnode_cache */ 96 }; 97 98 /* 99 * Flags to identify the usage of on-memory inodes (i_type) 100 */ 101 enum { 102 NILFS_I_TYPE_NORMAL = 0, 103 NILFS_I_TYPE_GC = 0x0001, /* For data caching during GC */ 104 NILFS_I_TYPE_BTNC = 0x0002, /* For btree node cache */ 105 NILFS_I_TYPE_SHADOW = 0x0004, /* For shadowed page cache */ 106 }; 107 108 /* 109 * commit flags for nilfs_commit_super and nilfs_sync_super 110 */ 111 enum { 112 NILFS_SB_COMMIT = 0, /* Commit a super block alternately */ 113 NILFS_SB_COMMIT_ALL /* Commit both super blocks */ 114 }; 115 116 /** 117 * define NILFS_MAX_VOLUME_NAME - maximum number of characters (bytes) in a 118 * file system volume name 119 * 120 * Defined by the size of the volume name field in the on-disk superblocks. 121 * This volume name does not include the terminating NULL byte if the string 122 * length matches the field size, so use (NILFS_MAX_VOLUME_NAME + 1) for the 123 * size of the buffer that requires a NULL byte termination. 124 */ 125 #define NILFS_MAX_VOLUME_NAME \ 126 sizeof_field(struct nilfs_super_block, s_volume_name) 127 128 /* 129 * Macros to check inode numbers 130 */ 131 #define NILFS_MDT_INO_BITS \ 132 (BIT(NILFS_DAT_INO) | BIT(NILFS_CPFILE_INO) | \ 133 BIT(NILFS_SUFILE_INO) | BIT(NILFS_IFILE_INO) | \ 134 BIT(NILFS_ATIME_INO) | BIT(NILFS_SKETCH_INO)) 135 136 #define NILFS_SYS_INO_BITS (BIT(NILFS_ROOT_INO) | NILFS_MDT_INO_BITS) 137 138 #define NILFS_FIRST_INO(sb) (((struct the_nilfs *)sb->s_fs_info)->ns_first_ino) 139 140 #define NILFS_MDT_INODE(sb, ino) \ 141 ((ino) < NILFS_USER_INO && (NILFS_MDT_INO_BITS & BIT(ino))) 142 #define NILFS_VALID_INODE(sb, ino) \ 143 ((ino) >= NILFS_FIRST_INO(sb) || \ 144 ((ino) < NILFS_USER_INO && (NILFS_SYS_INO_BITS & BIT(ino)))) 145 146 #define NILFS_PRIVATE_INODE(ino) ({ \ 147 ino_t __ino = (ino); \ 148 ((__ino) < NILFS_USER_INO && (__ino) != NILFS_ROOT_INO && \ 149 (__ino) != NILFS_SKETCH_INO); }) 150 151 /** 152 * struct nilfs_transaction_info: context information for synchronization 153 * @ti_magic: Magic number 154 * @ti_save: Backup of journal_info field of task_struct 155 * @ti_flags: Flags 156 * @ti_count: Nest level 157 */ 158 struct nilfs_transaction_info { 159 u32 ti_magic; 160 void *ti_save; 161 /* 162 * This should never be used. If it happens, 163 * one of other filesystems has a bug. 164 */ 165 unsigned short ti_flags; 166 unsigned short ti_count; 167 }; 168 169 /* ti_magic */ 170 #define NILFS_TI_MAGIC 0xd9e392fb 171 172 /* ti_flags */ 173 #define NILFS_TI_DYNAMIC_ALLOC 0x0001 /* Allocated from slab */ 174 #define NILFS_TI_SYNC 0x0002 /* 175 * Force to construct segment at the 176 * end of transaction. 177 */ 178 #define NILFS_TI_GC 0x0004 /* GC context */ 179 #define NILFS_TI_COMMIT 0x0008 /* Change happened or not */ 180 #define NILFS_TI_WRITER 0x0010 /* Constructor context */ 181 182 183 int nilfs_transaction_begin(struct super_block *, 184 struct nilfs_transaction_info *, int); 185 int nilfs_transaction_commit(struct super_block *); 186 void nilfs_transaction_abort(struct super_block *); 187 188 static inline void nilfs_set_transaction_flag(unsigned int flag) 189 { 190 struct nilfs_transaction_info *ti = current->journal_info; 191 192 ti->ti_flags |= flag; 193 } 194 195 static inline int nilfs_test_transaction_flag(unsigned int flag) 196 { 197 struct nilfs_transaction_info *ti = current->journal_info; 198 199 if (ti == NULL || ti->ti_magic != NILFS_TI_MAGIC) 200 return 0; 201 return !!(ti->ti_flags & flag); 202 } 203 204 static inline int nilfs_doing_gc(void) 205 { 206 return nilfs_test_transaction_flag(NILFS_TI_GC); 207 } 208 209 static inline int nilfs_doing_construction(void) 210 { 211 return nilfs_test_transaction_flag(NILFS_TI_WRITER); 212 } 213 214 /* 215 * function prototype 216 */ 217 #ifdef CONFIG_NILFS_POSIX_ACL 218 #error "NILFS: not yet supported POSIX ACL" 219 extern int nilfs_acl_chmod(struct inode *); 220 extern int nilfs_init_acl(struct inode *, struct inode *); 221 #else 222 static inline int nilfs_acl_chmod(struct inode *inode) 223 { 224 return 0; 225 } 226 227 static inline int nilfs_init_acl(struct inode *inode, struct inode *dir) 228 { 229 if (S_ISLNK(inode->i_mode)) 230 return 0; 231 232 inode->i_mode &= ~current_umask(); 233 return 0; 234 } 235 #endif 236 237 #define NILFS_ATIME_DISABLE 238 239 /* Flags that should be inherited by new inodes from their parent. */ 240 #define NILFS_FL_INHERITED \ 241 (FS_SECRM_FL | FS_UNRM_FL | FS_COMPR_FL | FS_SYNC_FL | \ 242 FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL |\ 243 FS_COMPRBLK_FL | FS_NOCOMP_FL | FS_NOTAIL_FL | FS_DIRSYNC_FL) 244 245 /* Mask out flags that are inappropriate for the given type of inode. */ 246 static inline __u32 nilfs_mask_flags(umode_t mode, __u32 flags) 247 { 248 if (S_ISDIR(mode)) 249 return flags; 250 else if (S_ISREG(mode)) 251 return flags & ~(FS_DIRSYNC_FL | FS_TOPDIR_FL); 252 else 253 return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 254 } 255 256 /* dir.c */ 257 int nilfs_add_link(struct dentry *, struct inode *); 258 int nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr, ino_t *ino); 259 int nilfs_make_empty(struct inode *, struct inode *); 260 struct nilfs_dir_entry *nilfs_find_entry(struct inode *, const struct qstr *, 261 struct folio **); 262 int nilfs_delete_entry(struct nilfs_dir_entry *, struct folio *); 263 int nilfs_empty_dir(struct inode *); 264 struct nilfs_dir_entry *nilfs_dotdot(struct inode *, struct folio **); 265 int nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de, 266 struct folio *folio, struct inode *inode); 267 268 /* file.c */ 269 extern int nilfs_sync_file(struct file *, loff_t, loff_t, int); 270 271 /* ioctl.c */ 272 int nilfs_fileattr_get(struct dentry *dentry, struct file_kattr *m); 273 int nilfs_fileattr_set(struct mnt_idmap *idmap, 274 struct dentry *dentry, struct file_kattr *fa); 275 long nilfs_ioctl(struct file *, unsigned int, unsigned long); 276 long nilfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 277 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *, struct nilfs_argv *, 278 void **); 279 280 /* inode.c */ 281 void nilfs_inode_add_blocks(struct inode *inode, int n); 282 void nilfs_inode_sub_blocks(struct inode *inode, int n); 283 extern struct inode *nilfs_new_inode(struct inode *, umode_t); 284 extern int nilfs_get_block(struct inode *, sector_t, struct buffer_head *, int); 285 extern void nilfs_set_inode_flags(struct inode *); 286 extern int nilfs_read_inode_common(struct inode *, struct nilfs_inode *); 287 void nilfs_write_inode_common(struct inode *inode, 288 struct nilfs_inode *raw_inode); 289 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 290 unsigned long ino); 291 struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 292 unsigned long ino); 293 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 294 unsigned long ino); 295 extern struct inode *nilfs_iget_for_gc(struct super_block *sb, 296 unsigned long ino, __u64 cno); 297 int nilfs_attach_btree_node_cache(struct inode *inode); 298 void nilfs_detach_btree_node_cache(struct inode *inode); 299 struct inode *nilfs_iget_for_shadow(struct inode *inode); 300 extern void nilfs_update_inode(struct inode *, struct buffer_head *, int); 301 extern void nilfs_truncate(struct inode *); 302 extern void nilfs_evict_inode(struct inode *); 303 extern int nilfs_setattr(struct mnt_idmap *, struct dentry *, 304 struct iattr *); 305 extern void nilfs_write_failed(struct address_space *mapping, loff_t to); 306 int nilfs_permission(struct mnt_idmap *idmap, struct inode *inode, 307 int mask); 308 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh); 309 extern int nilfs_inode_dirty(struct inode *); 310 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty); 311 extern int __nilfs_mark_inode_dirty(struct inode *, int); 312 extern void nilfs_dirty_inode(struct inode *, int flags); 313 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 314 __u64 start, __u64 len); 315 static inline int nilfs_mark_inode_dirty(struct inode *inode) 316 { 317 return __nilfs_mark_inode_dirty(inode, I_DIRTY); 318 } 319 static inline int nilfs_mark_inode_dirty_sync(struct inode *inode) 320 { 321 return __nilfs_mark_inode_dirty(inode, I_DIRTY_SYNC); 322 } 323 324 /* super.c */ 325 extern struct inode *nilfs_alloc_inode(struct super_block *); 326 327 __printf(2, 3) 328 void __nilfs_msg(struct super_block *sb, const char *fmt, ...); 329 extern __printf(3, 4) 330 void __nilfs_error(struct super_block *sb, const char *function, 331 const char *fmt, ...); 332 333 #ifdef CONFIG_PRINTK 334 335 #define nilfs_msg(sb, level, fmt, ...) \ 336 __nilfs_msg(sb, level fmt, ##__VA_ARGS__) 337 #define nilfs_error(sb, fmt, ...) \ 338 __nilfs_error(sb, __func__, fmt, ##__VA_ARGS__) 339 340 #else 341 342 #define nilfs_msg(sb, level, fmt, ...) \ 343 do { \ 344 no_printk(level fmt, ##__VA_ARGS__); \ 345 (void)(sb); \ 346 } while (0) 347 #define nilfs_error(sb, fmt, ...) \ 348 do { \ 349 no_printk(fmt, ##__VA_ARGS__); \ 350 __nilfs_error(sb, "", " "); \ 351 } while (0) 352 353 #endif /* CONFIG_PRINTK */ 354 355 #define nilfs_crit(sb, fmt, ...) \ 356 nilfs_msg(sb, KERN_CRIT, fmt, ##__VA_ARGS__) 357 #define nilfs_err(sb, fmt, ...) \ 358 nilfs_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__) 359 #define nilfs_warn(sb, fmt, ...) \ 360 nilfs_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__) 361 #define nilfs_info(sb, fmt, ...) \ 362 nilfs_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__) 363 364 extern struct nilfs_super_block * 365 nilfs_read_super_block(struct super_block *, u64, int, struct buffer_head **); 366 extern int nilfs_store_magic(struct super_block *sb, 367 struct nilfs_super_block *sbp); 368 extern int nilfs_check_feature_compatibility(struct super_block *, 369 struct nilfs_super_block *); 370 extern void nilfs_set_log_cursor(struct nilfs_super_block *, 371 struct the_nilfs *); 372 struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb, 373 int flip); 374 int nilfs_commit_super(struct super_block *sb, int flag); 375 int nilfs_cleanup_super(struct super_block *sb); 376 int nilfs_resize_fs(struct super_block *sb, __u64 newsize); 377 int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt, 378 struct nilfs_root **root); 379 int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno); 380 381 /* gcinode.c */ 382 int nilfs_gccache_submit_read_data(struct inode *, sector_t, sector_t, __u64, 383 struct buffer_head **); 384 int nilfs_gccache_submit_read_node(struct inode *, sector_t, __u64, 385 struct buffer_head **); 386 int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *); 387 int nilfs_init_gcinode(struct inode *inode); 388 void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs); 389 390 /* sysfs.c */ 391 int __init nilfs_sysfs_init(void); 392 void nilfs_sysfs_exit(void); 393 int nilfs_sysfs_create_device_group(struct super_block *); 394 void nilfs_sysfs_delete_device_group(struct the_nilfs *); 395 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *); 396 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *); 397 398 /* 399 * Inodes and files operations 400 */ 401 extern const struct file_operations nilfs_dir_operations; 402 extern const struct inode_operations nilfs_file_inode_operations; 403 extern const struct file_operations nilfs_file_operations; 404 extern const struct address_space_operations nilfs_aops; 405 extern const struct address_space_operations nilfs_buffer_cache_aops; 406 extern const struct inode_operations nilfs_dir_inode_operations; 407 extern const struct inode_operations nilfs_special_inode_operations; 408 extern const struct inode_operations nilfs_symlink_inode_operations; 409 410 /* 411 * filesystem type 412 */ 413 extern struct file_system_type nilfs_fs_type; 414 415 416 #endif /* _NILFS_H */ 417