1/* $NetBSD: nilfs_subr.h,v 1.12 2008/08/06 13:41:12 reinoud 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 */
36uint64_t nilfs_get_segnum_of_block(struct nilfs_device *nilfsdev, uint64_t blocknr);
37void nilfs_get_segment_range(struct nilfs_device *nilfsdev, uint64_t segnum,
38	uint64_t *seg_start, uint64_t *seg_end);
39void nilfs_calc_mdt_consts(struct nilfs_device *nilfsdev,
40	struct nilfs_mdt *mdt, int entry_size);
41uint32_t crc32_le(uint32_t crc, const uint8_t *buf, size_t len);
42
43/* log reading / volume helpers */
44int nilfs_get_segment_log(struct nilfs_device *nilfsdev, uint64_t *blocknr,
45	uint64_t *offset, struct buf **bpp, int len, void *blob);
46void nilfs_search_super_root(struct nilfs_device *nilfsdev);
47
48/* reading */
49int nilfs_bread(struct nilfs_node *node, uint64_t blocknr, struct kauth_cred *cred,
50	int flags, struct buf **bpp);
51
52/* btree operations */
53int nilfs_btree_nlookup(struct nilfs_node *node, uint64_t from, uint64_t blks, uint64_t *l2vmap);
54
55/* vtop operations */
56int nilfs_nvtop(struct nilfs_node *node, uint64_t blks, uint64_t *l2vmap, uint64_t *v2pmap);
57
58/* node action implementators */
59int nilfs_get_node(struct nilfs_mount *ump, uint64_t ino, struct nilfs_node **nodep);
60int nilfs_get_node_raw(struct nilfs_device *nilfsdev, struct nilfs_mount *ump, uint64_t ino, struct nilfs_inode *inode, struct nilfs_node **nodep);
61void nilfs_dispose_node(struct nilfs_node **node);
62
63int nilfs_grow_node(struct nilfs_node *node, uint64_t new_size);
64int nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size);
65void nilfs_itimes(struct nilfs_node *nilfs_node, struct timespec *acc,
66	struct timespec *mod, struct timespec *birth);
67int  nilfs_update(struct vnode *node, struct timespec *acc,
68	struct timespec *mod, struct timespec *birth, int updflags);
69
70/* return vpp? */
71int nilfs_lookup_name_in_dir(struct vnode *dvp, const char *name, int namelen, uint64_t *ino, int *found);
72int nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp);
73void nilfs_delete_node(struct nilfs_node *nilfs_node);
74
75int nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred);
76int nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct componentname *cnp);
77int 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 */
81int nilfs_inactive(void *v);
82int nilfs_reclaim(void *v);
83int nilfs_readdir(void *v);
84int nilfs_getattr(void *v);
85int nilfs_setattr(void *v);
86int nilfs_pathconf(void *v);
87int nilfs_open(void *v);
88int nilfs_close(void *v);
89int nilfs_access(void *v);
90int nilfs_read(void *v);
91int nilfs_write(void *v);
92int nilfs_trivial_bmap(void *v);
93int nilfs_vfsstrategy(void *v);
94int nilfs_lookup(void *v);
95int nilfs_create(void *v);
96int nilfs_mknod(void *v);
97int nilfs_link(void *);
98int nilfs_symlink(void *v);
99int nilfs_readlink(void *v);
100int nilfs_rename(void *v);
101int nilfs_remove(void *v);
102int nilfs_mkdir(void *v);
103int nilfs_rmdir(void *v);
104int nilfs_fsync(void *v);
105int nilfs_advlock(void *v);
106
107#endif	/* !_FS_NILFS_NILFS_SUBR_H_ */
108
109#if 0
110/* device information updating */
111int nilfs_update_trackinfo(struct nilfs_mount *ump, struct mmc_trackinfo *trackinfo);
112int nilfs_update_discinfo(struct nilfs_mount *ump);
113int nilfs_search_tracks(struct nilfs_mount *ump, struct nilfs_args *args,
114		  int *first_tracknr, int *last_tracknr);
115int nilfs_search_writing_tracks(struct nilfs_mount *ump);
116int nilfs_setup_writeparams(struct nilfs_mount *ump);
117int nilfs_synchronise_caches(struct nilfs_mount *ump);
118
119/* tags operations */
120int nilfs_fidsize(struct fileid_desc *fid);
121int nilfs_check_tag(void *blob);
122int nilfs_check_tag_payload(void *blob, uint32_t max_length);
123void nilfs_validate_tag_sum(void *blob);
124void nilfs_validate_tag_and_crc_sums(void *blob);
125int nilfs_tagsize(union dscrptr *dscr, uint32_t nilfs_sector_size);
126
127/* read/write descriptors */
128int 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
134int 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);
137int 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 */
143int nilfs_create_logvol_dscr(struct nilfs_mount *ump, struct nilfs_node *nilfs_node,
144	struct long_ad *icb, union dscrptr **dscrptr);
145void nilfs_free_logvol_dscr(struct nilfs_mount *ump, struct long_ad *icb_loc,
146	void *dscr);
147int nilfs_read_logvol_dscr(struct nilfs_mount *ump, struct long_ad *icb,
148	union dscrptr **dscrptr);
149int 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 */
154int nilfs_read_anchors(struct nilfs_mount *ump);
155int nilfs_read_vds_space(struct nilfs_mount *ump);
156int nilfs_process_vds(struct nilfs_mount *ump);
157int nilfs_read_vds_tables(struct nilfs_mount *ump);
158int nilfs_read_rootdirs(struct nilfs_mount *ump);
159
160/* open/close and sync volumes */
161int nilfs_open_logvol(struct nilfs_mount *ump);
162int nilfs_close_logvol(struct nilfs_mount *ump, int mntflags);
163int nilfs_writeout_vat(struct nilfs_mount *ump);
164int nilfs_write_physical_partition_spacetables(struct nilfs_mount *ump, int waitfor);
165int nilfs_write_metadata_partition_spacetable(struct nilfs_mount *ump, int waitfor);
166void nilfs_do_sync(struct nilfs_mount *ump, kauth_cred_t cred, int waitfor);
167
168/* translation services */
169int nilfs_translate_vtop(struct nilfs_mount *ump, struct long_ad *icb_loc,
170		uint32_t *lb_numres, uint32_t *extres);
171void nilfs_translate_vtop_list(struct nilfs_mount *ump, uint32_t sectors,
172		uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping);
173int nilfs_translate_file_extent(struct nilfs_node *node,
174		uint32_t from, uint32_t num_lb, uint64_t *map);
175void nilfs_get_adslot(struct nilfs_node *nilfs_node, int slot, struct long_ad *icb, int *eof);
176int nilfs_append_adslot(struct nilfs_node *nilfs_node, int *slot, struct long_ad *icb);
177
178int nilfs_vat_read(struct nilfs_node *vat_node, uint8_t *blob, int size, uint32_t offset);
179int nilfs_vat_write(struct nilfs_node *vat_node, uint8_t *blob, int size, uint32_t offset);
180
181/* disc allocation */
182void nilfs_late_allocate_buf(struct nilfs_mount *ump, struct buf *buf, uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_num);
183void nilfs_free_allocated_space(struct nilfs_mount *ump, uint32_t lb_num, uint16_t vpart_num, uint32_t num_lb);
184int nilfs_pre_allocate_space(struct nilfs_mount *ump, int nilfs_c_type, uint32_t num_lb, uint16_t vpartnr, uint64_t *lmapping);
185int nilfs_grow_node(struct nilfs_node *node, uint64_t new_size);
186int nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size);
187
188/* node readers and writers */
189uint64_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))
193void nilfs_lock_node(struct nilfs_node *nilfs_node, int flag, char const *fname, const int lineno);
194void nilfs_unlock_node(struct nilfs_node *nilfs_node, int flag);
195
196int nilfs_get_node(struct nilfs_mount *ump, struct long_ad *icbloc, struct nilfs_node **noderes);
197int nilfs_writeout_node(struct nilfs_node *nilfs_node, int waitfor);
198int nilfs_dispose_node(struct nilfs_node *node);
199
200/* node ops */
201int nilfs_resize_node(struct nilfs_node *node, uint64_t new_size, int *extended);
202int 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 */
205void nilfs_read_filebuf(struct nilfs_node *node, struct buf *buf);
206void nilfs_write_filebuf(struct nilfs_node *node, struct buf *buf);
207void nilfs_fixup_fid_block(uint8_t *blob, int lb_size, int rfix_pos, int max_rfix_pos, uint32_t lb_num);
208void nilfs_fixup_internal_extattr(uint8_t *blob, uint32_t lb_num);
209void nilfs_fixup_node_internals(struct nilfs_mount *ump, uint8_t *blob, int nilfs_c_type);
210
211/* device strategy */
212void nilfs_discstrat_init(struct nilfs_mount *ump);
213void nilfs_discstrat_finish(struct nilfs_mount *ump);
214void nilfs_discstrat_queuebuf(struct nilfs_mount *ump, struct buf *nestbuf);
215
216/* structure writers */
217int nilfs_write_terminator(struct nilfs_mount *ump, uint32_t sector);
218
219/* structure creators */
220void nilfs_inittag(struct nilfs_mount *ump, struct desc_tag *tag, int tagid, uint32_t sector);
221void nilfs_set_regid(struct regid *regid, char const *name);
222void nilfs_add_domain_regid(struct nilfs_mount *ump, struct regid *regid);
223void nilfs_add_nilfs_regid(struct nilfs_mount *ump, struct regid *regid);
224void nilfs_add_impl_regid(struct nilfs_mount *ump, struct regid *regid);
225void nilfs_add_app_regid(struct nilfs_mount *ump, struct regid *regid);
226
227/* directory operations and helpers */
228void nilfs_osta_charset(struct charspec *charspec);
229int nilfs_read_fid_stream(struct vnode *vp, uint64_t *offset, struct fileid_desc *fid, struct dirent *dirent);
230int nilfs_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen, struct long_ad *icb_loc, int *found);
231int nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp);
232void nilfs_delete_node(struct nilfs_node *nilfs_node);
233
234int nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred);
235int nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *nilfs_node, struct componentname *cnp);
236int 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 */
239void nilfs_add_to_dirtylist(struct nilfs_node *nilfs_node);
240void nilfs_remove_from_dirtylist(struct nilfs_node *nilfs_node);
241void nilfs_itimes(struct nilfs_node *nilfs_node, struct timespec *acc,
242	struct timespec *mod, struct timespec *birth);
243int  nilfs_update(struct vnode *node, struct timespec *acc,
244	struct timespec *mod, struct timespec *birth, int updflags);
245
246/* helpers and converters */
247long nilfs_calchash(struct long_ad *icbptr);    /* for `inode' numbering */
248uint32_t nilfs_getaccessmode(struct nilfs_node *node);
249void nilfs_setaccessmode(struct nilfs_node *nilfs_node, mode_t mode);
250void nilfs_getownership(struct nilfs_node *nilfs_node, uid_t *uidp, gid_t *gidp);
251void nilfs_setownership(struct nilfs_node *nilfs_node, uid_t uid, gid_t gid);
252
253void nilfs_to_unix_name(char *result, int result_len, char *id, int len, struct charspec *chsp);
254void unix_to_nilfs_name(char *result, uint8_t *result_len, char const *name, int name_len, struct charspec *chsp);
255
256void nilfs_timestamp_to_timespec(struct nilfs_mount *ump, struct timestamp *timestamp, struct timespec *timespec);
257void nilfs_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp);
258#endif
259