xref: /netbsd/sys/fs/udf/udf.h (revision 87d35fd6)
1 /* $NetBSD: udf.h,v 1.55 2023/06/27 09:58:50 reinoud Exp $ */
2 
3 /*
4  * Copyright (c) 2006, 2008 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_UDF_UDF_H_
30 #define _FS_UDF_UDF_H_
31 
32 #include <sys/queue.h>
33 #include <sys/rbtree.h>
34 #include <sys/uio.h>
35 #include <sys/mutex.h>
36 
37 #include "udf_osta.h"
38 #include "ecma167-udf.h"
39 #include <sys/cdio.h>
40 #include <sys/bufq.h>
41 #include <sys/disk.h>
42 #include <sys/kthread.h>
43 #include <miscfs/genfs/genfs_node.h>
44 
45 /* debug section */
46 extern int udf_verbose;
47 
48 /* undefine UDF_COMPLETE_DELETE to need `purge'; but purge is not implemented */
49 #define UDF_COMPLETE_DELETE
50 
51 /* debug categories */
52 #define UDF_DEBUG_VOLUMES	0x0000001
53 #define UDF_DEBUG_LOCKING	0x0000002
54 #define UDF_DEBUG_NODE		0x0000004
55 #define UDF_DEBUG_LOOKUP	0x0000008
56 #define UDF_DEBUG_READDIR	0x0000010
57 #define UDF_DEBUG_FIDS		0x0000020
58 #define UDF_DEBUG_DESCRIPTOR	0x0000040
59 #define UDF_DEBUG_TRANSLATE	0x0000080
60 #define UDF_DEBUG_STRATEGY	0x0000100
61 #define UDF_DEBUG_READ		0x0000200
62 #define UDF_DEBUG_WRITE		0x0000400
63 #define UDF_DEBUG_CALL		0x0000800
64 #define UDF_DEBUG_ATTR		0x0001000
65 #define UDF_DEBUG_EXTATTR	0x0002000
66 #define UDF_DEBUG_ALLOC		0x0004000
67 #define UDF_DEBUG_ADWLK		0x0008000
68 #define UDF_DEBUG_DIRHASH	0x0010000
69 #define UDF_DEBUG_NOTIMPL	0x0020000
70 #define UDF_DEBUG_SHEDULE	0x0040000
71 #define UDF_DEBUG_ECCLINE	0x0080000
72 #define UDF_DEBUG_SYNC		0x0100000
73 #define UDF_DEBUG_PARANOIA	0x0200000
74 #define UDF_DEBUG_PARANOIDADWLK	0x0400000
75 #define UDF_DEBUG_NODEDUMP	0x0800000
76 #define UDF_DEBUG_RESERVE	0x1000000
77 
78 /* initial value of udf_verbose */
79 #define UDF_DEBUGGING		(0x0000000)
80 //#define UDF_DEBUGGING		(0x02fffff)
81 
82 
83 #ifdef UDF_DEBUG
84 #define DPRINTF(name, arg) { \
85 		if (udf_verbose & UDF_DEBUG_##name) {\
86 			printf arg;\
87 		};\
88 	}
89 #define DPRINTFIF(name, cond, arg) { \
90 		if (udf_verbose & UDF_DEBUG_##name) { \
91 			if (cond) printf arg;\
92 		};\
93 	}
94 #else
95 #define DPRINTF(name, arg) {}
96 #define DPRINTFIF(name, cond, arg) {}
97 #endif
98 
99 VFS_PROTOS(udf);
100 
101 /* constants to identify what kind of identifier we are dealing with */
102 #define UDF_REGID_DOMAIN		 1
103 #define UDF_REGID_UDF			 2
104 #define UDF_REGID_IMPLEMENTATION	 3
105 #define UDF_REGID_APPLICATION		 4
106 #define UDF_REGID_NAME			99
107 
108 
109 /* DON'T change these: they identify 13thmonkey's UDF implementation */
110 #define APP_NAME		"*NetBSD UDF"
111 #define APP_VERSION_MAIN	0
112 #define APP_VERSION_SUB		5
113 #define IMPL_NAME		"*NetBSD kernel UDF"
114 
115 
116 /* Configuration values */
117 #define UDF_INODE_HASHBITS 	10
118 #define UDF_INODE_HASHSIZE	(1<<UDF_INODE_HASHBITS)
119 #define UDF_INODE_HASHMASK	(UDF_INODE_HASHSIZE - 1)
120 #define UDF_ECCBUF_HASHBITS	10
121 #define UDF_ECCBUF_HASHSIZE	(1<<UDF_ECCBUF_HASHBITS)
122 #define UDF_ECCBUF_HASHMASK	(UDF_ECCBUF_HASHSIZE -1)
123 
124 #define UDF_ECCLINE_MAXFREE	5			/* picked, needs calculation */
125 #define UDF_ECCLINE_MAXBUSY	100			/* picked, needs calculation */
126 
127 #define UDF_MAX_MAPPINGS	(MAXPHYS/DEV_BSIZE)	/* 128 */
128 #define UDF_VAT_CHUNKSIZE	(64*1024)		/* picked */
129 #define UDF_SYMLINKBUFLEN	(64*1024)		/* picked */
130 
131 #define UDF_DISC_SLACK		(128)			/* picked, at least 64 kb or 128 */
132 #define UDF_ISO_VRS_SIZE	(32*2048)		/* 32 ISO `sectors' */
133 
134 #define UDF_MAX_INDIRS_FOLLOW	1024			/* picked */
135 
136 
137 /* structure space */
138 #define UDF_ANCHORS		4	/* 256, 512, N-256, N */
139 #define UDF_PARTITIONS		4	/* overkill */
140 #define UDF_PMAPS		5	/* overkill */
141 #define UDF_LVDINT_SEGMENTS	100	/* big overkill */
142 #define UDF_LVINT_LOSSAGE	4	/* lose 2 openings */
143 #define UDF_MAX_ALLOC_EXTENTS	50	/* overkill */
144 
145 
146 /* constants */
147 #define UDF_MAXNAMLEN		255			/* as per SPEC */
148 #define UDF_TRANS_ZERO		((uint64_t) -1)
149 #define UDF_TRANS_UNMAPPED	((uint64_t) -2)
150 #define UDF_TRANS_INTERN	((uint64_t) -3)
151 #define UDF_MAX_SECTOR		((uint64_t) -10)	/* high water mark */
152 
153 
154 /* RW content hint for allocation and other purposes */
155 #define UDF_C_ABSOLUTE		 0	/* blob to write at absolute */
156 #define UDF_C_PROCESSED		 0	/* not relevant */
157 #define UDF_C_USERDATA		 1	/* all but userdata is metadata */
158 #define UDF_C_DSCR		 2	/* update sectornr and CRC */
159 #define UDF_C_FLOAT_DSCR	 3	/* update sectornr and CRC; sequential */
160 #define UDF_C_NODE		 4	/* file/dir node, update sectornr and CRC */
161 #define UDF_C_FIDS		 5	/* update all contained fids */
162 #define UDF_C_METADATA_SBM	 6	/* space bitmap, update sectornr and CRC */
163 #define UDF_C_EXTATTRS		 7	/* dunno what to do yet */
164 
165 /* use unused b_freelistindex for our UDF_C_TYPE */
166 #define b_udf_c_type	b_freelistindex
167 
168 
169 /* virtual to physical mapping types */
170 #define UDF_VTOP_RAWPART UDF_PMAPS	/* [0..UDF_PMAPS> are normal     */
171 
172 #define UDF_VTOP_TYPE_RAW            0
173 #define UDF_VTOP_TYPE_UNKNOWN        0
174 #define UDF_VTOP_TYPE_PHYS           1
175 #define UDF_VTOP_TYPE_VIRT           2
176 #define UDF_VTOP_TYPE_SPARABLE       3
177 #define UDF_VTOP_TYPE_META           4
178 
179 
180 /* allocation strategies */
181 #define UDF_ALLOC_INVALID            0
182 #define UDF_ALLOC_SEQUENTIAL         1  /* linear on NWA                 */
183 #define UDF_ALLOC_VAT                2  /* VAT handling                  */
184 #define UDF_ALLOC_SPACEMAP           3  /* spacemaps                     */
185 #define UDF_ALLOC_METABITMAP         4  /* metadata bitmap               */
186 #define UDF_ALLOC_METASEQUENTIAL     5  /* in chunks seq., nodes not seq */
187 #define UDF_ALLOC_RELAXEDSEQUENTIAL  6  /* only nodes not seq.           */
188 
189 
190 /* logical volume open/close actions */
191 #define UDF_OPEN_SESSION	  0x01  /* if needed writeout VRS + VDS	     */
192 #define UDF_CLOSE_SESSION	  0x02	/* close session after writing VAT   */
193 #define UDF_FINALISE_DISC	  0x04	/* close session after writing VAT   */
194 #define UDF_WRITE_VAT		  0x08	/* sequential VAT filesystem         */
195 #define UDF_WRITE_LVINT		  0x10	/* write out open lvint              */
196 #define UDF_WRITE_PART_BITMAPS	  0x20	/* write out partition space bitmaps */
197 #define UDF_APPENDONLY_LVINT	  0x40	/* no shifting, only appending       */
198 #define UDF_WRITE_METAPART_NODES  0x80	/* write out metadata partition nodes*/
199 #define UDFLOGVOL_BITS "\20\1OPEN_SESSION\2CLOSE_SESSION\3FINALISE_DISC" \
200 			"\4WRITE_VAT\5WRITE_LVINT\6WRITE_PART_BITMAPS" \
201 			"\7APPENDONLY_LVINT\10WRITE_METAPART_NODES"
202 
203 /* logical volume error handling actions */
204 #define UDF_UPDATE_TRACKINFO	  0x01	/* update trackinfo and re-schedule  */
205 #define UDF_REMAP_BLOCK		  0x02	/* remap the failing block length    */
206 #define UDFONERROR_BITS "\20\1UPDATE_TRACKINFO\2REMAP_BLOCK"
207 
208 
209 /* readdir cookies */
210 #define UDF_DIRCOOKIE_DOT 1
211 
212 
213 /* malloc pools */
214 MALLOC_DECLARE(M_UDFMNT);
215 MALLOC_DECLARE(M_UDFVOLD);
216 MALLOC_DECLARE(M_UDFTEMP);
217 
218 extern struct pool udf_node_pool;
219 struct udf_node;
220 struct udf_strategy;
221 
222 
223 struct udf_lvintq {
224 	uint32_t		start;
225 	uint32_t		end;
226 	uint32_t		pos;
227 	uint32_t		wpos;
228 };
229 
230 
231 struct udf_bitmap {
232 	uint8_t			*blob;			/* allocated         */
233 	uint8_t			*bits;			/* bits themselves   */
234 	uint8_t			*pages;			/* dirty pages       */
235 	uint32_t		 max_offset;		/* in bits           */
236 	uint32_t		 data_pos;		/* position in data  */
237 	uint32_t		 metadata_pos;		/* .. in metadata    */
238 };
239 
240 
241 struct udf_strat_args {
242 	struct udf_mount *ump;
243 	struct udf_node  *udf_node;
244 	struct long_ad   *icb;
245 	union dscrptr    *dscr;
246 	struct buf       *nestbuf;
247 	kauth_cred_t	  cred;
248 	int waitfor;
249 };
250 
251 struct udf_strategy {
252 	int  (*create_logvol_dscr)  (struct udf_strat_args *args);
253 	void (*free_logvol_dscr)    (struct udf_strat_args *args);
254 	int  (*read_logvol_dscr)    (struct udf_strat_args *args);
255 	int  (*write_logvol_dscr)   (struct udf_strat_args *args);
256 	void (*queuebuf)	    (struct udf_strat_args *args);
257 	void (*sync_caches)	    (struct udf_strat_args *args);
258 	void (*discstrat_init)      (struct udf_strat_args *args);
259 	void (*discstrat_finish)    (struct udf_strat_args *args);
260 };
261 
262 extern struct udf_strategy udf_strat_bootstrap;
263 extern struct udf_strategy udf_strat_sequential;
264 extern struct udf_strategy udf_strat_direct;
265 extern struct udf_strategy udf_strat_rmw;
266 
267 
268 /* pre cleanup */
269 struct udf_mount {
270 	struct mount		*vfs_mountp;
271 	struct vnode		*devvp;
272 	struct mmc_discinfo	 discinfo;
273 	struct udf_args		 mount_args;
274 
275 	/* format descriptors */
276 	kmutex_t		 logvol_mutex;
277 	struct anchor_vdp	*anchors[UDF_ANCHORS];	/* anchors to VDS    */
278 	struct pri_vol_desc	*primary_vol;		/* identification    */
279 	struct logvol_desc	*logical_vol;		/* main mapping v->p */
280 	struct unalloc_sp_desc	*unallocated;		/* free UDF space    */
281 	struct impvol_desc	*implementation;	/* likely redundant  */
282 	struct logvol_int_desc	*logvol_integrity;	/* current integrity */
283 	struct part_desc	*partitions[UDF_PARTITIONS]; /* partitions   */
284 	/* logvol_info is derived; points *into* other structures */
285 	struct udf_logvol_info	*logvol_info;		/* integrity descr.  */
286 
287 	/* fileset and root directories */
288 	struct fileset_desc	*fileset_desc;		/* normally one      */
289 
290 	/* tracing logvol integrity history */
291 	struct udf_lvintq	 lvint_trace[UDF_LVDINT_SEGMENTS];
292 	int			 lvopen;		/* logvol actions    */
293 	int			 lvclose;		/* logvol actions    */
294 
295 	/* logical to physical translations */
296 	int 			 vtop[UDF_PMAPS+1];	/* vpartnr trans     */
297 	int			 vtop_tp[UDF_PMAPS+1];	/* type of trans     */
298 
299 	/* disc allocation / writing method */
300 	kmutex_t		 allocate_mutex;
301 	int			 lvreadwrite;		/* error handling    */
302 	int			 vtop_alloc[UDF_PMAPS+1]; /* alloc scheme    */
303 	int			 data_part;
304 	int			 node_part;
305 	int			 fids_part;
306 
307 	/* sequential track info */
308 	struct mmc_trackinfo	 data_track;
309 	struct mmc_trackinfo	 metadata_track;
310 
311 	/* VAT */
312 	uint32_t		 first_possible_vat_location;
313 	uint32_t		 last_possible_vat_location;
314 	uint32_t		 vat_entries;
315 	uint32_t		 vat_offset;		/* offset in table   */
316 	uint32_t		 vat_last_free_lb;	/* last free lb_num  */
317 	uint32_t		 vat_table_len;
318 	uint32_t		 vat_table_alloc_len;
319 	uint8_t			*vat_table;
320 	uint8_t			*vat_pages;		/* TODO */
321 	struct udf_node		*vat_node;		/* system node       */
322 
323 	/* space bitmaps for physical partitions */
324 	struct space_bitmap_desc*part_unalloc_dscr[UDF_PARTITIONS];
325 	struct space_bitmap_desc*part_freed_dscr  [UDF_PARTITIONS];
326 	struct udf_bitmap	 part_unalloc_bits[UDF_PARTITIONS];
327 	struct udf_bitmap	 part_freed_bits  [UDF_PARTITIONS];
328 
329 	/* sparable */
330 	uint32_t		 sparable_packet_size;
331 	uint32_t		 packet_size;
332 	struct udf_sparing_table*sparing_table;
333 
334 	/* meta */
335 	struct udf_node 	*metadata_node;		/* system node       */
336 	struct udf_node 	*metadatamirror_node;	/* system node       */
337 	struct udf_node 	*metadatabitmap_node;	/* system node       */
338 	struct space_bitmap_desc*metadata_unalloc_dscr;
339 	struct udf_bitmap	 metadata_unalloc_bits;
340 	uint32_t		 metadata_alloc_unit_size;
341 	uint16_t		 metadata_alignment_unit_size;
342 	uint8_t			 metadata_flags;
343 
344 	/* rb tree for lookup icb to udf_node and sorted list for sync */
345 	struct rb_tree	udf_node_tree;
346 
347 	/* syncing */
348 	int		syncing;			/* are we syncing?   */
349 	kmutex_t	sync_lock;			/* serialize syncing */
350 
351 	/* late allocation */
352 	int32_t			 uncommitted_lbs[UDF_PARTITIONS];
353 	struct long_ad		*la_node_ad_cpy;		/* issue buf */
354 	uint64_t		*la_lmapping, *la_pmapping;	/* issue buf */
355 
356 	/* lists */
357 	STAILQ_HEAD(udfmntpts, udf_mount) all_udf_mntpnts;
358 
359 	/* device strategy */
360 	struct udf_strategy	*strategy;
361 	void			*strategy_private;
362 };
363 
364 /*
365  * UDF node describing a file/directory.
366  *
367  * BUGALERT claim node_mutex before reading/writing to prevent inconsistencies !
368  */
369 struct udf_node {
370 	struct genfs_node	i_gnode;		/* has to be first   */
371 	struct vnode		*vnode;			/* vnode associated  */
372 	struct udf_mount	*ump;
373 
374 	kmutex_t		 node_mutex;
375 	kcondvar_t		 node_lock;		/* sleeping lock */
376 	char const		*lock_fname;
377 	int			 lock_lineno;
378 
379 	/* rb_node for fast lookup and fast sequential visiting */
380 	struct rb_node		 rbnode;
381 
382 	/* one of `fe' or `efe' can be set, not both (UDF file entry dscr.)  */
383 	struct file_entry	*fe;
384 	struct extfile_entry	*efe;
385 	struct alloc_ext_entry	*ext[UDF_MAX_ALLOC_EXTENTS];
386 	int			 num_extensions;
387 
388 	/* location found, recording location & hints */
389 	struct long_ad		 loc;			/* FID/hash loc.     */
390 	struct long_ad		 write_loc;		/* strat 4096 loc    */
391 	int			 needs_indirect;	/* has missing indr. */
392 	struct long_ad		 ext_loc[UDF_MAX_ALLOC_EXTENTS];
393 
394 	struct dirhash		*dir_hash;
395 
396 	/* misc */
397 	uint32_t		 i_flags;		/* associated flags  */
398 	struct lockf		*lockf;			/* lock list         */
399 	uint32_t		 outstanding_bufs;	/* file data         */
400 	uint32_t		 outstanding_nodedscr;	/* node dscr         */
401 	int32_t			 uncommitted_lbs;	/* in UBC            */
402 
403 	/* references to associated nodes */
404 	struct udf_node		*extattr;
405 	struct udf_node		*streamdir;
406 	struct udf_node		*my_parent;		/* if extended attr. */
407 };
408 
409 
410 /* misc. flags stored in i_flags (XXX needs cleaning up) */
411 #define	IN_ACCESS		0x0001	/* Inode access time update request  */
412 #define	IN_CHANGE		0x0002	/* Inode change time update request  */
413 #define	IN_UPDATE		0x0004	/* Inode was written to; update mtime*/
414 #define	IN_MODIFY		0x0008	/* Modification time update request  */
415 #define	IN_MODIFIED		0x0010	/* node has been modified */
416 #define	IN_ACCESSED		0x0020	/* node has been accessed */
417 #define	IN_RENAME		0x0040	/* node is being renamed. XXX ?? */
418 #define	IN_DELETED		0x0080	/* node is unlinked, no FID reference*/
419 #define	IN_LOCKED		0x0100	/* node is locked by condvar */
420 #define	IN_SYNCED		0x0200	/* node is being used by sync */
421 #define	IN_CALLBACK_ULK		0x0400	/* node will be unlocked by callback */
422 #define	IN_NODE_REBUILD		0x0800	/* node is rebuild */
423 #define IN_NO_DELETE		0x1000	/* node is not to be deleted */
424 
425 
426 #define IN_FLAGBITS \
427 	"\10\1IN_ACCESS\2IN_CHANGE\3IN_UPDATE\4IN_MODIFY\5IN_MODIFIED" \
428 	"\6IN_ACCESSED\7IN_RENAME\10IN_DELETED\11IN_LOCKED\12IN_SYNCED" \
429 	"\13IN_CALLBACK_ULK\14IN_NODE_REBUILD\15IN_NO_DELETE"
430 
431 #endif /* !_FS_UDF_UDF_H_ */
432