Add quirk for SONY SMO drive. This (pre SCSI-2) drive returns a mystic
[dragonfly.git] / sys / vfs / hammer / hammer.h
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer.h,v 1.5 2007/11/07 00:43:24 dillon Exp $
35  */
36 /*
37  * This header file contains structures used internally by the HAMMERFS
38  * implementation.  See hammer_disk.h for on-disk structures.
39  */
40
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/tree.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/globaldata.h>
50 #include <sys/lockf.h>
51 #include <sys/buf.h>
52 #include <sys/globaldata.h>
53
54 #include <sys/buf2.h>
55
56 #include "hammer_alist.h"
57 #include "hammer_disk.h"
58 #include "hammer_mount.h"
59
60 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
61
62 MALLOC_DECLARE(M_HAMMER);
63
64 struct hammer_mount;
65
66 /*
67  * Key structure used for custom RB tree inode lookups.  This prototypes
68  * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
69  */
70 typedef struct hammer_inode_info {
71         u_int64_t       obj_id;         /* (key) object identifier */
72         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
73 } *hammer_inode_info_t;
74
75 /*
76  * HAMMER Transaction tracking
77  */
78 struct hammer_transaction {
79         struct hammer_mount *hmp;
80         hammer_tid_t    tid;
81 };
82
83 /*
84  * HAMMER locks
85  */
86 struct hammer_lock {
87         int     refs;
88         int     wanted;
89         struct thread *locktd;
90 };
91
92 static __inline int
93 hammer_islocked(struct hammer_lock *lock)
94 {
95         return(lock->refs > 0);
96 }
97
98 static __inline int
99 hammer_islastref(struct hammer_lock *lock)
100 {
101         return(lock->refs == 1);
102 }
103
104 /*
105  * Structures used to internally represent an inode
106  */
107 struct hammer_ino_rb_tree;
108 struct hammer_inode;
109 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
110 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
111              hammer_ino_rb_compare, hammer_inode_info_t);
112
113 struct hammer_inode {
114         RB_ENTRY(hammer_inode) rb_node;
115         u_int64_t       obj_id;         /* (key) object identifier */
116         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
117         hammer_tid_t    obj_lasttid;    /* last modified tid (for fsync) */
118         struct hammer_mount *hmp;
119         int             flags;
120         struct vnode    *vp;
121         struct lockf    advlock;
122         struct hammer_inode_record ino_rec;
123         struct hammer_inode_data ino_data;
124         struct hammer_lock lock;
125 };
126
127 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
128
129 #define HAMMER_INODE_DDIRTY     0x0001
130 #define HAMMER_INODE_RDIRTY     0x0002
131 #define HAMMER_INODE_ITIMES     0x0004  /* mtime or atime modified */
132
133 /*
134  * Structures used to internally represent a volume and a cluster
135  */
136
137 struct hammer_volume;
138 struct hammer_cluster;
139 struct hammer_supercl;
140 struct hammer_buffer;
141 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
142 RB_HEAD(hammer_clu_rb_tree, hammer_cluster);
143 RB_HEAD(hammer_scl_rb_tree, hammer_supercl);
144 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
145
146 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
147               hammer_vol_rb_compare, int32_t);
148 RB_PROTOTYPE2(hammer_clu_rb_tree, hammer_cluster, rb_node,
149               hammer_clu_rb_compare, int32_t);
150 RB_PROTOTYPE2(hammer_scl_rb_tree, hammer_supercl, rb_node,
151               hammer_scl_rb_compare, int32_t);
152 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
153               hammer_buf_rb_compare, int32_t);
154
155 /*
156  * IO management - embedded at the head of various in-memory structures
157  */
158 enum hammer_io_type { HAMMER_STRUCTURE_VOLUME,
159                       HAMMER_STRUCTURE_SUPERCL,
160                       HAMMER_STRUCTURE_CLUSTER,
161                       HAMMER_STRUCTURE_BUFFER };
162
163 union hammer_io_structure;
164
165 struct worklist {
166         LIST_ENTRY(worklist) node;
167 };
168
169 struct hammer_io {
170         struct worklist worklist;
171         struct hammer_lock lock;
172         enum hammer_io_type type;
173         struct buf      *bp;
174         int64_t         offset;
175         u_int           modified : 1;   /* bp's data was modified */
176         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
177 };
178
179 /*
180  * In-memory volume
181  */
182 struct hammer_volume {
183         struct hammer_io io;
184         RB_ENTRY(hammer_volume) rb_node;
185         struct hammer_clu_rb_tree rb_clus_root;
186         struct hammer_scl_rb_tree rb_scls_root;
187         struct hammer_volume_ondisk *ondisk;
188         struct hammer_alist_live alist;
189         int32_t vol_no;
190         int32_t vol_clsize;
191         int64_t cluster_base;   /* base offset of cluster 0 */
192         char    *vol_name;
193         struct vnode *devvp;
194         struct hammer_mount *hmp;
195         int     vol_flags;
196 };
197
198 /*
199  * In-memory super-cluster
200  */
201 struct hammer_supercl {
202         struct hammer_io io;
203         RB_ENTRY(hammer_supercl) rb_node;
204         struct hammer_supercl_ondisk *ondisk;
205         struct hammer_volume *volume;
206         struct hammer_alist_live alist;
207         int32_t scl_no;
208 };
209
210 /*
211  * In-memory cluster
212  */
213 struct hammer_cluster {
214         struct hammer_io io;
215         RB_ENTRY(hammer_cluster) rb_node;
216         struct hammer_buf_rb_tree rb_bufs_root;
217         struct hammer_cluster_ondisk *ondisk;
218         struct hammer_volume *volume;
219         struct hammer_alist_live alist_master;
220         struct hammer_alist_live alist_btree;
221         struct hammer_alist_live alist_record;
222         struct hammer_alist_live alist_mdata;
223         int32_t clu_no;
224 };
225
226 /*
227  * In-memory buffer (other then volume, super-cluster, or cluster)
228  */
229 struct hammer_buffer {
230         struct hammer_io io;
231         RB_ENTRY(hammer_buffer) rb_node;
232         hammer_fsbuf_ondisk_t ondisk;
233         struct hammer_cluster *cluster;
234         struct hammer_volume *volume;
235         struct hammer_alist_live alist;
236         int32_t buf_no;
237 };
238
239 union hammer_io_structure {
240         struct hammer_io        io;
241         struct hammer_volume    volume;
242         struct hammer_supercl   supercl;
243         struct hammer_cluster   cluster;
244         struct hammer_buffer    buffer;
245 };
246
247 #define HAMFS_CLUSTER_DIRTY     0x0001
248
249 /*
250  * Internal hammer mount data structure
251  */
252 struct hammer_mount {
253         struct mount *mp;
254         /*struct vnode *rootvp;*/
255         struct hammer_ino_rb_tree rb_inos_root;
256         struct hammer_vol_rb_tree rb_vols_root;
257         struct hammer_volume *rootvol;
258         struct hammer_cluster *rootcl;
259         /* struct hammer_volume *cache_volume */
260         /* struct hammer_cluster *cache_cluster */
261         /* struct hammer_buffer *cache_buffer */
262         char *zbuf;     /* HAMMER_BUFSIZE bytes worth of all-zeros */
263         uuid_t  fsid;
264         udev_t  fsid_udev;
265         u_int32_t namekey_iterator;
266         hammer_tid_t last_tid;
267 };
268
269
270 #endif
271
272 #if defined(_KERNEL)
273
274 extern struct vop_ops hammer_vnode_vops;
275 extern struct hammer_alist_config Buf_alist_config;
276 extern struct hammer_alist_config Vol_normal_alist_config;
277 extern struct hammer_alist_config Vol_super_alist_config;
278 extern struct hammer_alist_config Supercl_alist_config;
279 extern struct hammer_alist_config Clu_master_alist_config;
280 extern struct hammer_alist_config Clu_slave_alist_config;
281 extern struct bio_ops hammer_bioops;
282
283 int     hammer_vop_inactive(struct vop_inactive_args *);
284 int     hammer_vop_reclaim(struct vop_reclaim_args *);
285 int     hammer_vfs_vget(struct mount *mp, ino_t ino, struct  vnode **vpp);
286
287 int     hammer_get_vnode(struct hammer_inode *ip, int lktype,
288                         struct vnode **vpp);
289 struct hammer_inode *hammer_get_inode(struct hammer_mount *hmp,
290                         u_int64_t obj_id, int *errorp);
291 void    hammer_lock_inode(struct hammer_inode *ip);
292 void    hammer_put_inode(struct hammer_inode *ip);
293 void    hammer_put_inode_ref(struct hammer_inode *ip);
294
295 int     hammer_unload_inode(struct hammer_inode *ip, void *data __unused);
296 int     hammer_unload_volume(struct hammer_volume *volume, void *data __unused);
297 int     hammer_load_volume(struct hammer_mount *hmp, const char *volname);
298
299 void    hammer_lock(struct hammer_lock *lock);
300 void    hammer_unlock(struct hammer_lock *lock);
301 void    hammer_ref(struct hammer_lock *lock);
302 void    hammer_unref(struct hammer_lock *lock);
303 void    hammer_ref_to_lock(struct hammer_lock *lock);
304 void    hammer_lock_to_ref(struct hammer_lock *lock);
305 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
306 void    hammer_to_timespec(u_int64_t hammerts, struct timespec *ts);
307 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
308 u_int8_t hammer_get_obj_type(enum vtype vtype);
309 int64_t hammer_directory_namekey(void *name, int len);
310
311 int     hammer_btree_lookup(hammer_btree_info_t info,
312                             hammer_base_elm_t key, int flags);
313 int     hammer_btree_extract(hammer_btree_info_t info, int flags);
314 int     hammer_btree_iterate(hammer_btree_cursor_t cursor,
315                             hammer_base_elm_t key);
316 int     hammer_btree_insert(hammer_btree_info_t info,
317                             hammer_btree_leaf_elm_t elm);
318 int     hammer_btree_delete(hammer_btree_info_t info, hammer_base_elm_t key);
319 int     hammer_btree_cursor_init(hammer_btree_cursor_t cursor,
320                             struct hammer_cluster *cluster);
321 void    hammer_btree_cursor_done(hammer_btree_cursor_t cusor);
322 int     hammer_btree_info_init(hammer_btree_info_t info,
323                             struct hammer_cluster *cluster);
324 void    hammer_btree_info_done(hammer_btree_info_t info);
325
326 void    *hammer_bread(struct hammer_cluster *cluster, int32_t cloff,
327                       u_int64_t buf_type,
328                       int *errorp, struct hammer_buffer **bufferp);
329
330 struct hammer_volume *hammer_get_volume(struct hammer_mount *hmp,
331                         int32_t vol_no, int *errorp);
332 struct hammer_supercl *hammer_get_supercl(struct hammer_volume *volume,
333                         int32_t scl_no, int *errorp, int isnew);
334 struct hammer_cluster *hammer_get_cluster(struct hammer_volume *volume,
335                         int32_t clu_no, int *errorp, int isnew);
336 struct hammer_buffer *hammer_get_buffer(struct hammer_cluster *cluster,
337                         int32_t buf_no, int64_t buf_type, int *errorp);
338 struct hammer_cluster *hammer_get_rootcl(struct hammer_mount *hmp);
339 void hammer_dup_buffer(struct hammer_buffer **bufferp,
340                         struct hammer_buffer *buffer);
341 void hammer_dup_cluster(struct hammer_cluster **clusterp,
342                         struct hammer_cluster *cluster);
343 void *hammer_alloc_btree(struct hammer_cluster *cluster,        
344                         int *errorp, struct hammer_buffer **bufferp);
345 void *hammer_alloc_data(struct hammer_cluster *cluster, int32_t bytes,
346                         int *errorp, struct hammer_buffer **bufferp);
347 void *hammer_alloc_record(struct hammer_cluster *cluster,
348                         int *errorp, struct hammer_buffer **bufferp);
349 void hammer_free_btree_ptr(struct hammer_buffer *buffer,
350                         hammer_btree_node_t node);
351 void hammer_free_data_ptr(struct hammer_buffer *buffer, 
352                         void *data, int bytes);
353 void hammer_free_record_ptr(struct hammer_buffer *buffer,
354                         union hammer_record_ondisk *rec);
355 void hammer_free_btree(struct hammer_cluster *cluster, int32_t bclu_offset);
356 void hammer_free_data(struct hammer_cluster *cluster, int32_t bclu_offset,
357                         int32_t bytes);
358 void hammer_free_record(struct hammer_cluster *cluster, int32_t bclu_offset);
359
360 void hammer_put_volume(struct hammer_volume *volume, int flush);
361 void hammer_put_supercl(struct hammer_supercl *supercl, int flush);
362 void hammer_put_cluster(struct hammer_cluster *cluster, int flush);
363 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
364
365 void hammer_init_alist_config(void);
366
367 void hammer_start_transaction(struct hammer_mount *hmp,
368                               struct hammer_transaction *trans);
369 void hammer_commit_transaction(struct hammer_transaction *trans);
370 void hammer_abort_transaction(struct hammer_transaction *trans);
371
372 void hammer_modify_inode(struct hammer_transaction *trans,
373                         struct hammer_inode *ip, int flags);
374 int  hammer_alloc_inode(struct hammer_transaction *trans, struct vattr *vap,
375                         struct ucred *cred, struct hammer_inode **ipp);
376 int  hammer_add_directory(struct hammer_transaction *trans,
377                         struct hammer_inode *dip, struct namecache *ncp,
378                         struct hammer_inode *nip);
379
380 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
381 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
382 void hammer_io_release(struct hammer_io *io, int flush);
383
384 #endif
385
386 /*
387  * Inline support functions (not kernel specific)
388  */
389 static __inline void
390 hammer_modify_volume(struct hammer_volume *volume)
391 {
392         volume->io.modified = 1;
393 }
394
395 static __inline void
396 hammer_modify_supercl(struct hammer_supercl *supercl)
397 {
398         supercl->io.modified = 1;
399 }
400
401 static __inline void
402 hammer_modify_cluster(struct hammer_cluster *cluster)
403 {
404         cluster->io.modified = 1;
405 }
406
407 static __inline void
408 hammer_modify_buffer(struct hammer_buffer *buffer)
409 {
410         buffer->io.modified = 1;
411 }
412
413 /*
414  * Return the cluster-relative byte offset of an element within a buffer
415  */
416 static __inline int
417 hammer_bclu_offset(struct hammer_buffer *buffer, void *ptr)
418 {
419         int bclu_offset;
420
421         bclu_offset = buffer->buf_no * HAMMER_BUFSIZE + 
422                       ((char *)ptr - (char *)buffer->ondisk);
423         return(bclu_offset);
424 }
425