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