1 /* $NetBSD: nilfs_subr.h,v 1.4 2015/03/29 14:12:28 riastradh Exp $ */
2 
3 /*
4  * Copyright (c) 2008, 2009 Reinoud Zandijk
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #ifndef _FS_NILFS_NILFS_SUBR_H_
30 #define _FS_NILFS_NILFS_SUBR_H_
31 
32 /* handies */
33 #define	VFSTONILFS(mp)	((struct nilfs_mount *)mp->mnt_data)
34 
35 /* basic calculators */
36 uint64_t nilfs_get_segnum_of_block(struct nilfs_device *nilfsdev, uint64_t blocknr);
37 void nilfs_get_segment_range(struct nilfs_device *nilfsdev, uint64_t segnum,
38 	uint64_t *seg_start, uint64_t *seg_end);
39 void nilfs_calc_mdt_consts(struct nilfs_device *nilfsdev,
40 	struct nilfs_mdt *mdt, int entry_size);
41 uint32_t crc32_le(uint32_t crc, const uint8_t *buf, size_t len);
42 
43 /* log reading / volume helpers */
44 int nilfs_get_segment_log(struct nilfs_device *nilfsdev, uint64_t *blocknr,
45 	uint64_t *offset, struct buf **bpp, int len, void *blob);
46 void nilfs_search_super_root(struct nilfs_device *nilfsdev);
47 
48 /* reading */
49 int nilfs_bread(struct nilfs_node *node, uint64_t blocknr,
50 	int flags, struct buf **bpp);
51 
52 /* btree operations */
53 int nilfs_btree_nlookup(struct nilfs_node *node, uint64_t from, uint64_t blks, uint64_t *l2vmap);
54 
55 /* vtop operations */
56 void nilfs_mdt_trans(struct nilfs_mdt *mdt, uint64_t index, uint64_t *blocknr, uint32_t *entry_in_block);
57 int nilfs_nvtop(struct nilfs_node *node, uint64_t blks, uint64_t *l2vmap, uint64_t *v2pmap);
58 
59 /* node action implementators */
60 int nilfs_get_node_raw(struct nilfs_device *nilfsdev, struct nilfs_mount *ump, uint64_t ino, struct nilfs_inode *inode, struct nilfs_node **nodep);
61 void nilfs_dispose_node(struct nilfs_node **node);
62 
63 int nilfs_grow_node(struct nilfs_node *node, uint64_t new_size);
64 int nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size);
65 void nilfs_itimes(struct nilfs_node *nilfs_node, struct timespec *acc,
66 	struct timespec *mod, struct timespec *birth);
67 int  nilfs_update(struct vnode *node, struct timespec *acc,
68 	struct timespec *mod, struct timespec *birth, int updflags);
69 
70 /* return vpp? */
71 int nilfs_lookup_name_in_dir(struct vnode *dvp, const char *name, int namelen, uint64_t *ino, int *found);
72 int nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp);
73 void nilfs_delete_node(struct nilfs_node *nilfs_node);
74 
75 int nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred);
76 int nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct componentname *cnp);
77 int nilfs_dir_attach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct vattr *vap, struct componentname *cnp);
78 
79 
80 /* vnode operations */
81 int nilfs_inactive(void *v);
82 int nilfs_reclaim(void *v);
83 int nilfs_readdir(void *v);
84 int nilfs_getattr(void *v);
85 int nilfs_setattr(void *v);
86 int nilfs_pathconf(void *v);
87 int nilfs_open(void *v);
88 int nilfs_close(void *v);
89 int nilfs_access(void *v);
90 int nilfs_read(void *v);
91 int nilfs_write(void *v);
92 int nilfs_trivial_bmap(void *v);
93 int nilfs_vfsstrategy(void *v);
94 int nilfs_lookup(void *v);
95 int nilfs_create(void *v);
96 int nilfs_mknod(void *v);
97 int nilfs_link(void *);
98 int nilfs_symlink(void *v);
99 int nilfs_readlink(void *v);
100 int nilfs_rename(void *v);
101 int nilfs_remove(void *v);
102 int nilfs_mkdir(void *v);
103 int nilfs_rmdir(void *v);
104 int nilfs_fsync(void *v);
105 int nilfs_advlock(void *v);
106 
107 #endif	/* !_FS_NILFS_NILFS_SUBR_H_ */
108 
109 #if 0
110 /* device information updating */
111 int nilfs_update_trackinfo(struct nilfs_mount *ump, struct mmc_trackinfo *trackinfo);
112 int nilfs_update_discinfo(struct nilfs_mount *ump);
113 int nilfs_search_tracks(struct nilfs_mount *ump, struct nilfs_args *args,
114 		  int *first_tracknr, int *last_tracknr);
115 int nilfs_search_writing_tracks(struct nilfs_mount *ump);
116 int nilfs_setup_writeparams(struct nilfs_mount *ump);
117 int nilfs_synchronise_caches(struct nilfs_mount *ump);
118 
119 /* tags operations */
120 int nilfs_fidsize(struct fileid_desc *fid);
121 int nilfs_check_tag(void *blob);
122 int nilfs_check_tag_payload(void *blob, uint32_t max_length);
123 void nilfs_validate_tag_sum(void *blob);
124 void nilfs_validate_tag_and_crc_sums(void *blob);
125 int nilfs_tagsize(union dscrptr *dscr, uint32_t nilfs_sector_size);
126 
127 /* read/write descriptors */
128 int nilfs_read_phys_dscr(
129 		struct nilfs_mount *ump,
130 		uint32_t sector,
131 		struct malloc_type *mtype,		/* where to allocate */
132 		union dscrptr **dstp);			/* out */
133 
134 int nilfs_write_phys_dscr_sync(struct nilfs_mount *ump, struct nilfs_node *nilfs_node,
135 		int what, union dscrptr *dscr,
136 		uint32_t sector, uint32_t logsector);
137 int nilfs_write_phys_dscr_async(struct nilfs_mount *ump, struct nilfs_node *nilfs_node,
138 		      int what, union dscrptr *dscr,
139 		      uint32_t sector, uint32_t logsector,
140 		      void (*dscrwr_callback)(struct buf *));
141 
142 /* read/write node descriptors */
143 int nilfs_create_logvol_dscr(struct nilfs_mount *ump, struct nilfs_node *nilfs_node,
144 	struct long_ad *icb, union dscrptr **dscrptr);
145 void nilfs_free_logvol_dscr(struct nilfs_mount *ump, struct long_ad *icb_loc,
146 	void *dscr);
147 int nilfs_read_logvol_dscr(struct nilfs_mount *ump, struct long_ad *icb,
148 	union dscrptr **dscrptr);
149 int nilfs_write_logvol_dscr(struct nilfs_node *nilfs_node, union dscrptr *dscr,
150 	struct long_ad *icb, int waitfor);
151 
152 
153 /* volume descriptors readers and checkers */
154 int nilfs_read_anchors(struct nilfs_mount *ump);
155 int nilfs_read_vds_space(struct nilfs_mount *ump);
156 int nilfs_process_vds(struct nilfs_mount *ump);
157 int nilfs_read_vds_tables(struct nilfs_mount *ump);
158 int nilfs_read_rootdirs(struct nilfs_mount *ump);
159 
160 /* open/close and sync volumes */
161 int nilfs_open_logvol(struct nilfs_mount *ump);
162 int nilfs_close_logvol(struct nilfs_mount *ump, int mntflags);
163 int nilfs_writeout_vat(struct nilfs_mount *ump);
164 int nilfs_write_physical_partition_spacetables(struct nilfs_mount *ump, int waitfor);
165 int nilfs_write_metadata_partition_spacetable(struct nilfs_mount *ump, int waitfor);
166 void nilfs_do_sync(struct nilfs_mount *ump, kauth_cred_t cred, int waitfor);
167 
168 /* translation services */
169 int nilfs_translate_vtop(struct nilfs_mount *ump, struct long_ad *icb_loc,
170 		uint32_t *lb_numres, uint32_t *extres);
171 void nilfs_translate_vtop_list(struct nilfs_mount *ump, uint32_t sectors,
172 		uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping);
173 int nilfs_translate_file_extent(struct nilfs_node *node,
174 		uint32_t from, uint32_t num_lb, uint64_t *map);
175 void nilfs_get_adslot(struct nilfs_node *nilfs_node, int slot, struct long_ad *icb, int *eof);
176 int nilfs_append_adslot(struct nilfs_node *nilfs_node, int *slot, struct long_ad *icb);
177 
178 int nilfs_vat_read(struct nilfs_node *vat_node, uint8_t *blob, int size, uint32_t offset);
179 int nilfs_vat_write(struct nilfs_node *vat_node, uint8_t *blob, int size, uint32_t offset);
180 
181 /* disc allocation */
182 void nilfs_late_allocate_buf(struct nilfs_mount *ump, struct buf *buf, uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_num);
183 void nilfs_free_allocated_space(struct nilfs_mount *ump, uint32_t lb_num, uint16_t vpart_num, uint32_t num_lb);
184 int nilfs_pre_allocate_space(struct nilfs_mount *ump, int nilfs_c_type, uint32_t num_lb, uint16_t vpartnr, uint64_t *lmapping);
185 int nilfs_grow_node(struct nilfs_node *node, uint64_t new_size);
186 int nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size);
187 
188 /* node readers and writers */
189 uint64_t nilfs_advance_uniqueid(struct nilfs_mount *ump);
190 
191 #define NILFS_LOCK_NODE(nilfs_node, flag) nilfs_lock_node(nilfs_node, (flag), __FILE__, __LINE__)
192 #define NILFS_UNLOCK_NODE(nilfs_node, flag) nilfs_unlock_node(nilfs_node, (flag))
193 void nilfs_lock_node(struct nilfs_node *nilfs_node, int flag, char const *fname, const int lineno);
194 void nilfs_unlock_node(struct nilfs_node *nilfs_node, int flag);
195 
196 int nilfs_get_node(struct nilfs_mount *ump, struct long_ad *icbloc, struct nilfs_node **noderes);
197 int nilfs_writeout_node(struct nilfs_node *nilfs_node, int waitfor);
198 int nilfs_dispose_node(struct nilfs_node *node);
199 
200 /* node ops */
201 int nilfs_resize_node(struct nilfs_node *node, uint64_t new_size, int *extended);
202 int nilfs_extattr_search_intern(struct nilfs_node *node, uint32_t sattr, char const *sattrname, uint32_t *offsetp, uint32_t *lengthp);
203 
204 /* node data buffer read/write */
205 void nilfs_read_filebuf(struct nilfs_node *node, struct buf *buf);
206 void nilfs_write_filebuf(struct nilfs_node *node, struct buf *buf);
207 void nilfs_fixup_fid_block(uint8_t *blob, int lb_size, int rfix_pos, int max_rfix_pos, uint32_t lb_num);
208 void nilfs_fixup_internal_extattr(uint8_t *blob, uint32_t lb_num);
209 void nilfs_fixup_node_internals(struct nilfs_mount *ump, uint8_t *blob, int nilfs_c_type);
210 
211 /* device strategy */
212 void nilfs_discstrat_init(struct nilfs_mount *ump);
213 void nilfs_discstrat_finish(struct nilfs_mount *ump);
214 void nilfs_discstrat_queuebuf(struct nilfs_mount *ump, struct buf *nestbuf);
215 
216 /* structure writers */
217 int nilfs_write_terminator(struct nilfs_mount *ump, uint32_t sector);
218 
219 /* structure creators */
220 void nilfs_inittag(struct nilfs_mount *ump, struct desc_tag *tag, int tagid, uint32_t sector);
221 void nilfs_set_regid(struct regid *regid, char const *name);
222 void nilfs_add_domain_regid(struct nilfs_mount *ump, struct regid *regid);
223 void nilfs_add_nilfs_regid(struct nilfs_mount *ump, struct regid *regid);
224 void nilfs_add_impl_regid(struct nilfs_mount *ump, struct regid *regid);
225 void nilfs_add_app_regid(struct nilfs_mount *ump, struct regid *regid);
226 
227 /* directory operations and helpers */
228 void nilfs_osta_charset(struct charspec *charspec);
229 int nilfs_read_fid_stream(struct vnode *vp, uint64_t *offset, struct fileid_desc *fid, struct dirent *dirent);
230 int nilfs_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen, struct long_ad *icb_loc, int *found);
231 int nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp);
232 void nilfs_delete_node(struct nilfs_node *nilfs_node);
233 
234 int nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred);
235 int nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct componentname *cnp);
236 int nilfs_dir_attach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct vattr *vap, struct componentname *cnp);
237 
238 /* update and times */
239 void nilfs_add_to_dirtylist(struct nilfs_node *nilfs_node);
240 void nilfs_remove_from_dirtylist(struct nilfs_node *nilfs_node);
241 void nilfs_itimes(struct nilfs_node *nilfs_node, struct timespec *acc,
242 	struct timespec *mod, struct timespec *birth);
243 int  nilfs_update(struct vnode *node, struct timespec *acc,
244 	struct timespec *mod, struct timespec *birth, int updflags);
245 
246 /* helpers and converters */
247 long nilfs_calchash(struct long_ad *icbptr);    /* for `inode' numbering */
248 uint32_t nilfs_getaccessmode(struct nilfs_node *node);
249 void nilfs_setaccessmode(struct nilfs_node *nilfs_node, mode_t mode);
250 void nilfs_getownership(struct nilfs_node *nilfs_node, uid_t *uidp, gid_t *gidp);
251 void nilfs_setownership(struct nilfs_node *nilfs_node, uid_t uid, gid_t gid);
252 
253 void nilfs_to_unix_name(char *result, int result_len, char *id, int len, struct charspec *chsp);
254 void unix_to_nilfs_name(char *result, uint8_t *result_len, char const *name, int name_len, struct charspec *chsp);
255 
256 void nilfs_timestamp_to_timespec(struct nilfs_mount *ump, struct timestamp *timestamp, struct timespec *timespec);
257 void nilfs_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp);
258 #endif
259