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