]> git.neil.brown.name Git - LaFS.git/blob - lafs.h
README update
[LaFS.git] / lafs.h
1
2 /*
3  * fs/lafs/lafs.h
4  * Copyright (C) 2005-2009
5  * Neil Brown <neilb@suse.de>
6  * Released under the GPL, version 2
7  */
8
9 #include        <linux/blkdev.h>
10 #include        <linux/fs.h>
11 #include        <linux/highmem.h>
12
13 #include        "layout.h"
14 #include        "state.h"
15
16 extern int lafs_trace;
17 #define dprintk(x...) do { if(lafs_trace)printk(x); }while(0)
18
19 #define LAFS_BUG(cond, b) do { if (cond) { printk(KERN_ERR "%s:%d: %s\n", __FILE__,__LINE__,strblk(b));BUG();}}while(0)
20
21 #ifdef DUMP
22 extern struct fs *dfs;
23 extern struct freelists {
24         struct list_head        lru;
25         unsigned long           freecnt;
26 } freelist;
27 extern void lafs_dump_tree(void);
28 #endif
29
30 #if DEBUG_REF
31 #define REFARG char *__refname
32 #define REF     __refname
33 #define MKREF(name) #name
34
35 #define add_ref(a,b,c,d) lafs_add_ref(a,b,c,d)
36 #define del_ref(a,b,c,d) lafs_del_ref(a,b,c,d)
37 #define has_ref(a,b) lafs_has_ref(a,b)
38
39 #define lafs_get_block(a,b,c,d,e) _lafs_get_block(a,b,c,d,e)
40 #define first_in_seg(a,b,c,d,e) _first_in_seg(a,b,c,d,e)
41 #define dir_lookup_blk(a,b,c,d,e,f,g) _dir_lookup_blk(a,b,c,d,e,f,g)
42 #define lafs_iblock_alloc(a,b,c,d) _lafs_iblock_alloc(a,b,c,d)
43 #define ihash_lookup(a,b,c,d,e) _ihash_lookup(a,b,c,d,e)
44 #define iblock_get(a,b,c,d,e) _iblock_get(a,b,c,d,e)
45 #define lafs_make_iblock(a,b,c,d) _lafs_make_iblock(a,b,c,d)
46 #define lafs_leaf_find(a,b,c,d,e,f) _lafs_leaf_find(a,b,c,d,e,f)
47 #define lafs_inode_dblock(a,b,c) _lafs_inode_dblock(a,b,c)
48 #define lafs_inode_get_dblock(a,b) _lafs_inode_get_dblock(a,b)
49 #else
50 #define REFARG void
51 #define REF    0
52 #define MKREF(name) 0
53
54 #define add_ref(a,b,c,d) do {} while (0)
55 #define del_ref(a,b,c,d) do {} while (0)
56 #define has_ref(a,b) (-1)
57
58 #define lafs_get_block(a,b,c,d,e) _lafs_get_block(a,b,c,d)
59 #define first_in_seg(a,b,c,d,e) _first_in_seg(a,b,c,d)
60 #define dir_lookup_blk(a,b,c,d,e,f,g) _dir_lookup_blk(a,b,c,d,e,f)
61 #define lafs_iblock_alloc(a,b,c,d) _lafs_iblock_alloc(a,b,c)
62 #define ihash_lookup(a,b,c,d,e) _ihash_lookup(a,b,c,d)
63 #define iblock_get(a,b,c,d,e) _iblock_get(a,b,c,d)
64 #define lafs_make_iblock(a,b,c,d) _lafs_make_iblock(a,b,c)
65 #define lafs_leaf_find(a,b,c,d,e,f) _lafs_leaf_find(a,b,c,d,e)
66 #define lafs_inode_dblock(a,b,c) _lafs_inode_dblock(a,b)
67 #define lafs_inode_get_dblock(a,b) _lafs_inode_get_dblock(a)
68 #endif
69 #define strblk(a) lafs_strblk(a)
70
71 static inline int
72 u32_after(u32 a, u32 b)
73 {
74         return ((long)a-(long)b)>0;
75 }
76
77 const extern struct inode_operations lafs_file_ino_operations;
78 const extern struct inode_operations lafs_dir_ino_operations;
79 const extern struct inode_operations lafs_subset_ino_operations;
80 const extern struct inode_operations lafs_link_ino_operations;
81 const extern struct inode_operations lafs_special_ino_operations;
82 const extern struct file_operations lafs_file_file_operations;
83 const extern struct file_operations lafs_dir_file_operations;
84 const extern struct file_operations lafs_subset_file_operations;
85 const extern struct address_space_operations lafs_file_aops;
86 const extern struct address_space_operations lafs_index_operations;
87
88 #define SYNC 0
89 #define ASYNC 1
90
91 #define NOADOPT 0
92 #define ADOPT 1
93
94 typedef u32 faddr_t;
95 typedef u64 paddr_t;
96 typedef u64 vaddr_t;
97 int lafs_sync_page_io(struct block_device *bdev, sector_t sector, int offset,
98                       int size, struct page *page, int rw);
99 int lafs_load_page(struct fs *fs, struct page *p, u64 vaddr, int blocks);
100 int lafs_load_pages(struct fs *fs, struct page *p, u64 vaddr, int blocks);
101 int lafs_load_page_async(struct fs *fs, struct page *p, u64 vaddr, int blocks,
102                          struct async_complete *ac);
103 int __must_check lafs_load_block(struct block *b, struct bio *bio);
104 int __must_check lafs_wait_block(struct block *b);
105 int __must_check lafs_wait_block_async(struct block *b);
106 int __must_check lafs_find_block(struct datablock *b, int adopt);
107 int __must_check lafs_find_block_async(struct datablock *b);
108 int __must_check lafs_read_block(struct datablock *b);
109 int __must_check lafs_read_block_async(struct datablock *b);
110 int __must_check lafs_find_next(struct inode *b, loff_t *bnum);
111 struct indexblock *lafs_leaf_find(struct inode *inode, u32 addr,
112                                   int adopt, u32 *next, int async, REFARG);
113 u32 lafs_leaf_next(struct indexblock *ib, u32 start);
114 int lafs_index_empty(struct indexblock *ib);
115 #ifdef DEBUG_IOLOCK
116 #define set_iolock_info(b) ( (b)->iolock_file = __FILE__, (b)->iolock_line = __LINE__)
117 #else
118 #define set_iolock_info(b) (0)
119 #endif
120 #define lafs_iolock_block(b) do { _lafs_iolock_block(b); set_iolock_info(b); } while(0)
121 #define lafs_iolock_block_async(b) ( _lafs_iolock_block_async(b) ? ( set_iolock_info(b), 1) : 0)
122 #define lafs_iolock_written(b) do { _lafs_iolock_written(b); set_iolock_info(b); } while(0)
123 #define lafs_iolock_written_async(b) ( _lafs_iolock_written_async(b) ? ( set_iolock_info(b), 1) : 0)
124
125 void _lafs_iolock_block(struct block *b);
126 void _lafs_iolock_written(struct block *b);
127 int _lafs_iolock_block_async(struct block *b);
128 int _lafs_iolock_written_async(struct block *b);
129
130 void lafs_iounlock_block(struct block *b);
131 void lafs_iocheck_block(struct datablock *db, int unlock);
132 void lafs_iocheck_writeback(struct datablock *db, int unlock);
133 void lafs_writeback_done(struct block *b);
134
135 void lafs_super_write(struct fs *fs, int dev, u64 addr, char *buf, int size);
136 int lafs_super_wait(struct fs *fs);
137
138 /* inode.c */
139 void lafs_add_atime_offset(struct timespec *atime, int offset);
140 int __must_check lafs_mount(struct fs *fs);
141 struct inode *lafs_iget(struct inode *filesys, ino_t inum, int async);
142 struct inode *lafs_iget_fs(struct fs *fs, int fsnum, int inum, int async);
143 int __must_check lafs_import_inode(struct inode *ino, struct datablock *b);
144 void lafs_inode_checkpin(struct inode *ino);
145 void lafs_evict_inode(struct inode *ino);
146 void lafs_dirty_inode(struct inode *ino);
147 int lafs_sync_inode(struct inode *ino, int wait);
148 struct inode *lafs_new_inode(struct fs *fs, struct inode *fsys,
149                              struct inode *dir, int type,
150                              int inum, int mode, struct datablock **inodbp);
151 int lafs_lock_inode(struct inode *ino);
152 void lafs_inode_fillblock(struct inode *ino);
153 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG);
154 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG);
155 int lafs_inode_handle_orphan(struct datablock *b);
156 int lafs_inode_inuse(struct fs *fs, struct inode *fsys, u32 inum);
157
158 static inline void lafs_iput_fs(struct inode *ino)
159 {
160         struct super_block *sb = ino->i_sb;
161         if (!test_bit(I_Deleting, &LAFSI(ino)->iflags))
162                 iput(ino);
163         deactivate_super(sb);
164 }
165
166 static inline void lafs_igrab_fs(struct inode *ino)
167 {
168         if (igrab(ino) == NULL &&       
169             !test_bit(I_Deleting, &LAFSI(ino)->iflags))
170                 BUG();
171         atomic_inc(&ino->i_sb->s_active);
172 }
173
174 struct datablock *lafs_get_block(struct inode *ino, unsigned long index,
175                                  struct page *p, int gfp, REFARG);
176 #if DEBUG_REF
177 void add_ref(struct block *b, char *ref, char *file, int line);
178 void del_ref(struct block *b, char *ref, char *file, int line);
179 int has_ref(struct block *b, char *ref);
180 #endif
181
182 int lafs_setattr(struct dentry *dentry, struct iattr *attr);
183 int lafs_getattr(struct vfsmount *mnt, struct dentry *denty,
184                  struct kstat *stat);
185 void lafs_fillattr(struct inode *ino, struct kstat *stat);
186
187 char *strblk(struct block *b);
188 int lafs_print_tree(struct block *b, int depth);
189 int lafs_dev_find(struct fs *fs, u64 virt);
190
191 static inline void
192 virttoseg(struct fs *fs, u64 virt, int *devp, u32 *segp, u32 *offsetp)
193 {
194         int d = lafs_dev_find(fs, virt);
195         struct fs_dev *dv = &fs->devs[d];
196         BUG_ON(d<0);
197         virt -= dv->start;
198         if (dv->segment_size >= dv->width * dv->stride) {
199                 *offsetp = do_div(virt, dv->segment_size);
200                 *segp = virt;
201         } else {
202                 u64 v2 = virt;
203                 int of = do_div(v2,  dv->stride);
204                 int strp;
205                 v2 = virt;
206                 do_div(v2, dv->width * dv->stride);
207                 strp = v2;
208
209                 *segp = (strp * dv->stride + of) /
210                         (dv->segment_size / dv->width);
211                 *offsetp = virt - dv->segment_stride * *segp;
212         }
213         *devp = d;
214 }
215
216 static inline u64 segtovirt(struct fs *fs, int dev, u32 segnum)
217 {
218         return fs->devs[dev].start +
219                 (u64)fs->devs[dev].segment_stride * segnum;
220 }
221
222 static inline int
223 in_seg(struct fs *fs, int d, u32 seg, u64 virt)
224 {
225         struct fs_dev *dv = &fs->devs[d];
226
227         if (virt == 0)
228                 return 0;
229         if (virt < dv->start ||
230             virt >= dv->start + dv->size)
231                 return 0;
232
233         virt -= dv->start;
234         if (dv->segment_size >= dv->width * dv->stride) {
235                 do_div(virt, dv->segment_size);
236                 return seg == virt;
237         } else {
238                 u64 v2 = virt;
239                 int of = do_div(v2,  dv->stride);
240                 int strp;
241                 v2 = virt;
242                 do_div(v2, dv->width * dv->stride);
243                 strp = v2;
244
245                 return seg == ((strp * dv->stride + of) /
246                                (dv->segment_size / dv->width));
247         }
248 }
249
250 static inline void
251 virttophys(struct fs *fs, u64 virt, int *devp, sector_t *sectp)
252 {
253         int d = lafs_dev_find(fs, virt);
254         struct fs_dev *dv = &fs->devs[d];
255
256         *devp = d;
257         if (d < 0) return;
258
259         virt -= dv->start;
260         virt <<= (fs->blocksize_bits - 9);
261         virt += (dv->segment_offset)>>9;
262         *sectp = virt;
263 }
264
265 static inline int dblock_offset(struct datablock *b)
266 {
267         return (b - (struct datablock*)b->page->private)
268                 << b->b.inode->i_blkbits;
269 }
270
271 #if 0
272 static inline int iblock_offset(struct indexblock *b)
273 {
274         return (b - (struct indexblock*)b->b.page->private)
275                 << b->b.inode->i_blkbits;
276 }
277
278 static inline int block_offset(struct block *b)
279 {
280         if (test_bit(B_Index, &b->flags))
281                 return iblock_offset(iblk(b));
282         else
283                 return dblock_offset(dblk(b));
284 }
285 #endif
286
287 static inline void *map_dblock(struct datablock *b)
288 {
289         void *a = kmap_atomic(b->page, KM_USER0);
290         a += dblock_offset(b);
291         return a;
292 }
293
294 static inline void unmap_dblock(struct datablock *b, void *buf)
295 {
296         kunmap_atomic(buf - dblock_offset(b), KM_USER0);
297 }
298
299 static inline void *map_dblock_2(struct datablock *b)
300 {
301         void *a = kmap_atomic(b->page, KM_USER1);
302         a += dblock_offset(b);
303         return a;
304 }
305
306 static inline void unmap_dblock_2(struct datablock *b, void *buf)
307 {
308         kunmap_atomic(buf - dblock_offset(b), KM_USER1);
309 }
310
311 static inline void *map_iblock(struct indexblock *b)
312 {
313         LAFS_BUG(!test_bit(B_IOLock, &b->b.flags), &b->b);
314         if (test_bit(B_InoIdx, &b->b.flags))
315                 return map_dblock(LAFSI(b->b.inode)->dblock);
316         return b->data;
317 }
318
319 static inline void unmap_iblock(struct indexblock *b, void *buf)
320 {
321         if (test_bit(B_InoIdx, &b->b.flags))
322                 unmap_dblock(LAFSI(b->b.inode)->dblock, buf);
323 }
324
325 static inline void *map_iblock_2(struct indexblock *b)
326 {
327         if (test_bit(B_InoIdx, &b->b.flags))
328                 return map_dblock_2(LAFSI(b->b.inode)->dblock);
329         return b->data;
330 }
331
332 static inline void unmap_iblock_2(struct indexblock *b, void *buf)
333 {
334         if (test_bit(B_InoIdx, &b->b.flags))
335                 unmap_dblock_2(LAFSI(b->b.inode)->dblock, buf);
336 }
337
338 static inline void decode_time(struct timespec *ts, u64 te)
339 {
340         /* low 35 bits are seconds (800 years)
341          * high 29 bits are 2nanoseconds
342          */
343         ts->tv_sec = te& (0x7FFFFFFFFULL);
344         ts->tv_nsec = (te>>34) & ~(long)1;
345 }
346
347 static inline u64 encode_time(struct timespec *ts)
348 {
349         u64 t, tn;
350         t = ts->tv_sec;
351         t &= (0x7FFFFFFFFULL);
352         tn = ts->tv_nsec & ~(long)1;
353         tn <<= 34;
354         return t | tn;
355 }
356
357 /* s_fs_info points to an allocated sb_key structure */
358 struct sb_key {
359         struct inode *root;
360         struct fs *fs;
361 };
362
363 static inline struct fs *fs_from_sb(struct super_block *sb)
364 {
365         struct sb_key *k = sb->s_fs_info;
366         return k->fs;
367 }
368
369 static inline struct fs *fs_from_inode(struct inode *ino)
370 {
371         return fs_from_sb(ino->i_sb);
372 }
373
374 static inline int set_phase(struct block *b, int ph)
375 {
376         if (b->inode->i_ino == 0 && b->fileaddr == 0)
377                 dprintk("SETPHASE %s to  %d\n", strblk(b), ph);
378         if (ph)
379                 set_bit(B_Phase1, &b->flags);
380         else
381                 clear_bit(B_Phase1, &b->flags);
382
383         /* FIXME do I need to lock access to ->parent */
384         if (!test_and_set_bit(B_Pinned, &b->flags) &&
385             b->parent) {
386                 atomic_inc(&b->parent->pincnt[ph]);
387                 return 1;
388         } else
389                 return 0;
390 }
391
392 /*
393  * db->my_inode is protected by rcu. We can 'get' it and
394  * remain rcu_protected, or 'iget' it and be protected by a
395  * refcount
396  */
397 static inline struct inode *rcu_my_inode(struct datablock *db)
398 {
399         struct inode *ino;
400         if (db == NULL)
401                 return NULL;
402         if (LAFSI(db->b.inode)->type != TypeInodeFile)
403                 return NULL;
404         rcu_read_lock();
405         ino = rcu_dereference(db->my_inode);
406         if (ino)
407                 return ino;
408         rcu_read_unlock();
409         return NULL;
410 }
411 static inline void rcu_iput(struct inode *ino)
412 {
413         if (ino)
414                 rcu_read_unlock();
415 }
416
417 static inline struct inode *iget_my_inode(struct datablock *db)
418 {
419         struct inode *ino = rcu_my_inode(db);
420         struct inode *rv = NULL;
421         if (ino)
422                 rv = igrab(ino);
423         rcu_iput(ino);
424         return rv;
425 }
426
427 /*
428  * blocks (data and index) are reference counted.
429  * 'getref' increments the reference count, and could remove the
430  *     block from any 'lru' list.  However to save effort, we simply
431  *     treat anything on an lru list which has a non-zero reference
432  *     count as invisible.
433  * 'putref' drops the count and calls lafs_refile to see if anything
434  *     has changed.
435  */
436 int lafs_is_leaf(struct block *b, int ph);
437 void lafs_refile(struct block *b, int dec);
438
439 extern struct indexblock *lafs_getiref_locked(struct indexblock *ib);
440
441 extern spinlock_t lafs_hash_lock;
442
443 static inline struct block *__getref(struct block *b)
444 {
445         if (b)
446                 atomic_inc(&b->refcnt);
447         return b;
448 }
449
450 static inline struct block *_getref(struct block *b)
451 {
452         LAFS_BUG(b && atomic_read(&b->refcnt) == 0, b);
453         return __getref(b);
454 }
455
456 static inline struct datablock *_getdref_locked(struct datablock *b)
457 {
458         LAFS_BUG(!spin_is_locked(&b->b.inode->i_data.private_lock), &b->b);
459         __getref(&b->b);
460         return b;
461 }
462
463 static inline struct block *_getref_locked(struct block *b)
464 {
465         LAFS_BUG(!spin_is_locked(&b->inode->i_data.private_lock), b);
466         if (test_bit(B_InoIdx, &b->flags))
467                 return &lafs_getiref_locked(iblk(b))->b;
468         __getref(b);
469         return b;
470 }
471
472 static inline struct indexblock *_getiref_locked_needsync(struct indexblock *b)
473 {
474         LAFS_BUG(!spin_is_locked(&lafs_hash_lock), &b->b);
475         if (test_bit(B_InoIdx, &b->b.flags))
476                 return lafs_getiref_locked(b);
477         __getref(&b->b);
478         return b;
479 }
480
481 static inline struct block *_getref_locked_needsync(struct block *b)
482 {
483         LAFS_BUG(!spin_is_locked(&fs_from_inode(b->inode)->lock), b);
484         if (test_bit(B_InoIdx, &b->flags))
485                 return &lafs_getiref_locked(iblk(b))->b;
486         __getref(b);
487         return b;
488 }
489
490 static inline void _putref(struct block *b)
491 {
492         if (!b)
493                 return;
494         BUG_ON(atomic_read(&b->refcnt)==0);
495         lafs_refile(b, 1);
496 }
497
498 #if DEBUG_REF == 0
499 #define _reflog(c,blk,ref,add) (c(blk))
500 #else
501 #define _reflog(c,blk,ref,add) ({ !blk ? 0 : \
502                         add? add_ref(blk,ref,__FILE__,__LINE__) : \
503                         del_ref(blk,ref, __FILE__,__LINE__); c(blk); })
504 #define _refxlog(c,blk,ref,add) ({ \
505                         add? add_ref(&(blk)->b,ref,__FILE__,__LINE__) : \
506                         del_ref(&(blk)->b,ref, __FILE__,__LINE__); c(blk); })
507 #endif
508 #define getref(blk, r) ( ({BUG_ON((blk) && ! atomic_read(&(blk)->refcnt));}), _reflog(_getref, blk,r,1))
509 #define getref_locked(blk, r) _reflog(_getref_locked, blk,r,1)
510 #define getdref_locked(blk, r) _refxlog(_getdref_locked, blk,r,1)
511 #define getref_locked_needsync(blk, r) _reflog(_getref_locked_needsync, blk,r,1)
512 #define getiref_locked_needsync(blk, r) _refxlog(_getiref_locked_needsync, blk,r,1)
513 #define putref(blk, r) _reflog(_putref, blk,r,0)
514 #define getdref(blk, r) ( ({BUG_ON((blk) && ! atomic_read(&(blk)->b.refcnt));}), _reflog(_getref, (&(blk)->b),r,1),blk)
515 #define getiref(blk, r) ( ({BUG_ON((blk) && ! atomic_read(&(blk)->b.refcnt));}), _reflog(_getref, (&(blk)->b),r,1),blk)
516 #define putdref(blk, r) ({ if (blk) _reflog(_putref, (&(blk)->b),r,0);})
517 #define putiref(blk, r) ({ if (blk) _reflog(_putref, (&(blk)->b),r,0);})
518
519 /* When we get a ref on a block other than under b->inode->i_data.private_lock, we
520  * need to call this to ensure that lafs_refile isn't still handling a refcnt->0 transition.
521  */
522 static inline void sync_ref(struct block *b)
523 {
524         spin_lock(&b->inode->i_data.private_lock);
525         spin_unlock(&b->inode->i_data.private_lock);
526 }
527
528 /*
529  * Notes on locking and block references.
530  * There are several places that can hold uncounted references to blocks.
531  * When we try to convert such a reference to a counted reference we need to be careful.
532  * We must increment the refcount under a lock that protects the particular reference, and
533  * then must call sync_ref above to ensure our new reference is safe.
534  * When the last counted reference on a block is dropped (in lafs_refile) we hold private_lock
535  * while cleaning up and sync_ref assures that cleanup is finished.
536  * Before we can free a block we must synchronise with these other locks and
537  * then ensure that the refcount is still zero.
538  * For index blocks that means taking lafs_hash_lock and ensuring the count is still zero.
539  * For most data blocks we need to remove from the lru list using fs->lock, and check the count is
540  * still zero.
541  * For inode data blocks we need to follow the inode and break the ->dblock lock under the inodes
542  * own lock - but only if the refcount is still zero.
543  *
544  * The list of non-counted references and the locks which protect them is:
545  *
546  *         index hash table                      lafs_hash_lock
547  *         index freelist                        lafs_hash_lock
548  *         phase_leafs / clean_leafs             fs->lock
549  *         inode->iblock                         lafs_hash_lock
550  *         inode->dblock                         inode->i_data.private_lock
551  *         inode->free_index                     lafs_hash_lock
552  *         page->private                         page lock
553  *
554  * Lock ordering among these is:
555  *   inode->i_data.private_lock
556  *      LAFSI(inode)->dblock->inode->i_data.private_lock
557  *         fs->lock
558  *            lafs_hash_lock
559  *
560  */
561
562 int __must_check lafs_setparent(struct datablock *blk);
563
564 /*
565  * extract little-endian values out of memory.
566  * Each function is given a char*, and moves it forwards
567  */
568
569 #define decode16(p) ({ unsigned int _a; _a= (unsigned char)*(p++); \
570                         _a + (((unsigned char)*p++)<<8); })
571 #define decode32(p) ({ u32 _b; _b = decode16(p); _b + ((u32)decode16(p)<<16); })
572 #define decode48(p) ({ u64 _c; _c = decode32(p); _c + ((u64)decode16(p)<<32); })
573
574 #define encode16(p,n) ({ *(p++) = (n)&255; *(p++) = ((n)>>8) & 255; })
575 #define encode32(p,n) ({ encode16(p,n); encode16(p, ((n)>>16)); })
576 #define encode48(p,n) ({ encode32(p,n); encode16(p, ((n)>>32)); })
577
578 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
579
580 static inline int space_needed(int len, int chainoffset, int psz)
581 {
582         int space;
583         space = len + (chainoffset > 255 ? 4 : chainoffset > 1 ? 1 : 0);
584         space += offsetof(struct dirpiece, name);
585         space = DIV_ROUND_UP(space, 1<<psz);
586         return space;
587 }
588
589 struct dir_ent {
590         char *name;
591         int nlen;
592         u32 target;
593         int type;
594 };
595
596 struct update_handle {
597         struct fs *fs;
598         int reserved;
599 };
600
601 void lafs_dir_print(char *buf, int psz);
602 int lafs_dir_blk_size(char *block, int psz);
603 void lafs_dir_clearparent(struct datablock *b);
604 struct block *lafs_dir_drop_parent(struct datablock *b);
605
606 int lafs_hash_name(u32 seed, int len, const char *name);
607 int lafs_dir_find(char *block, int psz, u32 seed, u32 hash,
608                   u8 *pp);
609 int lafs_dir_findfirst(char *block, int psz);
610 void lafs_dir_init_block(char *block, int psz, const char *name, int len,
611                          u32 target, int type, int chainoffset);
612 #define DT_TEST 128 /* for lafs_dir_add_ent, this flags to test for space,
613                      * but not do any actual add
614                      */
615 #define DT_INTERNAL 129 /* Flags that this is an internal dir block */
616 int lafs_dir_add_ent(char *block, int psz, const char *name, int len,
617                      u32 target, int type, u32 seed, u32 hash, int chainoffset);
618 int lafs_dir_del_ent(char *block, int psz, u32 seed, u32 hash);
619 int lafs_dir_next_ent(char *block, int psz, int *pp, char *name, u32 *target,
620                       int *type);
621 void lafs_dir_repack(char *block, int psz, char *new, u32 seed, int merge);
622 void dir_get_prefix(char *b1, char *b2, int psz, char *prefix);
623 void lafs_dir_split(char *orig, int psz, char *new1, char *new2, const char *name,
624                    u32 target, int type, u32 *newhash, u32 seed, u32 hash,
625                    int chainoffset);
626 /*void dir_settarget(char *block, int psz, int piece, u32 target);*/
627 struct dir_ent *lafs_dir_extract(char *block, int psz, struct dir_ent *de,
628                                  int pnum, u32 *hash);
629 void lafs_dir_set_target(char *block, int psz, struct dir_ent *de, int pnum);
630 int lafs_dir_empty(char *block);
631 void lafs_dir_make_index(char *orig, char *new, int psz, u32 target);
632
633 int lafs_dir_handle_orphan(struct datablock *db);
634 int lafs_dir_roll_mini(struct inode *dir, int handle, int dirop,
635                        u32 inum, char *name, int len);
636
637 int lafs_release_page(struct page *page, gfp_t gfp_flags);
638 void lafs_invalidate_page(struct page *page, unsigned long offset);
639
640 extern struct file_system_type lafs_fs_type;
641 extern struct file_system_type lafs_snap_fs_type;
642
643 int lafs_ihash_init(void);
644 void lafs_ihash_free(void);
645 void lafs_release_index(struct list_head *head);
646 struct indexblock *lafs_iblock_alloc(struct fs *fs, int gfp, int with_buffer,
647                                      REFARG);
648 void lafs_iblock_free(struct indexblock *ib);
649 void lafs_hash_iblock(struct indexblock *ib);
650 void lafs_unhash_iblock(struct indexblock *ib);
651
652 void lafs_summary_update(struct fs *fs, struct inode *ino,
653                          u64 oldphys, u64 newphys, int is_index, int phase,
654                          int moveref);
655 int lafs_summary_allocate(struct fs *fs, struct inode *ino, int diff);
656 void lafs_qcommit(struct fs *fs, struct inode *ino, int diff, int phase);
657 void lafs_incorporate(struct fs *fs, struct indexblock *ib);
658 void lafs_walk_leaf_index(struct indexblock *ib,
659                           int (*handle)(void*, u32, u64, int),
660                           void *data);
661 void lafs_clear_index(struct indexblock *ib);
662 void lafs_print_uninc(struct uninc *ui);
663
664 int lafs_allocated_block(struct fs *fs, struct block *blk, u64 phys);
665
666 int lafs_pin_dblock(struct datablock *b, int alloc_type);
667 int lafs_reserve_block(struct block *b, int alloc_type);
668 void lafs_dirty_dblock(struct datablock *b);
669 void lafs_erase_dblock(struct datablock *b);
670 int lafs_erase_dblock_async(struct datablock *b);
671 void lafs_dirty_iblock(struct indexblock *b, int want_realloc);
672 void block_drop_addr(struct fs *fs, struct inode *ino, u32 addr);
673 void lafs_flush(struct datablock *b);
674
675 bio_end_io_t *lafs_cluster_endio_choose(int which, int header);
676 int lafs_cluster_update_prepare(struct update_handle *uh, struct fs *fs,
677                                 int len);
678 int lafs_cluster_update_pin(struct update_handle *uh);
679 unsigned long long
680      lafs_cluster_update_commit(struct update_handle *uh,
681                                 struct datablock *b,
682                                 int offset, int len);
683 unsigned long long
684      lafs_cluster_update_commit_buf(struct update_handle *uh, struct fs *fs,
685                                     struct inode *ino, u32 addr,
686                                     int offset, int len, const char *str1,
687                                     int len2, const char *str2);
688 void lafs_cluster_update_abort(struct update_handle *uh);
689 #define MAX_CHEAD_BLOCKS 4
690
691 static inline void lafs_cluster_wait(struct fs *fs, unsigned long long seq)
692 {
693         wait_event(fs->wc[0].pending_wait,
694                    fs->wc[0].cluster_seq > seq);
695 }
696
697 void lafs_cluster_wait_all(struct fs *fs);
698 int lafs_cluster_empty(struct fs *fs, int cnum);
699
700 /* super.c */
701 int lafs_write_state(struct fs *fs);
702 void lafs_destroy_inode(struct inode *inode);
703
704 /* checkpoint.c */
705 void lafs_checkpoint_lock(struct fs *fs);
706 void lafs_checkpoint_unlock(struct fs *fs);
707 void lafs_checkpoint_unlock_wait(struct fs *fs);
708 void lafs_checkpoint_wait(struct fs *fs);
709 unsigned long long lafs_checkpoint_start(struct fs *fs);
710 unsigned long lafs_do_checkpoint(struct fs *fs);
711 struct block *lafs_get_flushable(struct fs *fs, int phase);
712
713 int lafs_make_orphan(struct fs *fs, struct datablock *db);
714 int lafs_make_orphan_nb(struct fs *fs, struct datablock *db);
715 void lafs_orphan_release(struct fs *fs, struct datablock *db);
716 long lafs_run_orphans(struct fs *fs);
717 void lafs_add_orphan(struct fs *fs, struct datablock *db);
718 void lafs_orphan_forget(struct fs *fs, struct datablock *db);
719 struct datablock *lafs_find_orphan(struct inode *ino);
720 int lafs_count_orphans(struct inode *ino);
721 void lafs_add_orphans(struct fs *fs, struct inode *ino, int count);
722
723 /* Segment.c */
724 int lafs_prealloc(struct block *b, int type);
725 int lafs_seg_ref_block(struct block *b, int ssnum);
726 void lafs_seg_deref(struct fs *fs, u64 addr, int ssnum);
727 void lafs_add_active(struct fs *fs, u64 addr);
728 void lafs_seg_forget(struct fs *fs, int dev, u32 seg);
729 void lafs_seg_flush_all(struct fs *fs);
730 void lafs_seg_apply_all(struct fs *fs);
731 void lafs_seg_put_all(struct fs *fs);
732 void lafs_empty_segment_table(struct fs *fs);
733 int __must_check lafs_seg_dup(struct fs *fs, int which);
734 void lafs_seg_move(struct fs *fs, u64 oldaddr, u64 newaddr,
735                    int ssnum, int phase, int moveref);
736 int lafs_segtrack_init(struct segtracker *st);
737 void lafs_segtrack_free(struct segtracker *st);
738 void lafs_update_youth(struct fs *fs, int dev, u32 seg);
739
740 extern int temp_credits;/* debugging */
741 void lafs_free_get(struct fs *fs, unsigned int *dev, u32 *seg,
742                    int nonlogged);
743 int lafs_get_cleanable(struct fs *fs, u16 *dev, u32 *seg);
744
745 void lafs_space_return(struct fs *fs, int credits);
746 int lafs_alloc_cleaner_segs(struct fs *fs, int max);
747 int lafs_space_alloc(struct fs *fs, int credits, int why);
748 unsigned long lafs_scan_seg(struct fs *fs);
749 int lafs_clean_count(struct fs *fs, int *any_clean);
750 void lafs_clean_free(struct fs *fs);
751
752 /* Cleaner */
753 unsigned long lafs_do_clean(struct fs *fs);
754 void lafs_unclean(struct datablock *db);
755
756 /* Thread management */
757 int lafs_start_thread(struct fs *fs);
758 void lafs_stop_thread(struct fs *fs);
759 void lafs_wake_thread(struct fs *fs);
760 void lafs_trigger_flush(struct block *b);
761
762 /* cluster.c */
763 int lafs_cluster_allocate(struct block *b, int cnum);
764 void lafs_cluster_flush(struct fs *fs, int cnum);
765 int lafs_calc_cluster_csum(struct cluster_head *head);
766 int lafs_cluster_init(struct fs *fs, int cnum, u64 addr, u64 prev, u64 seq);
767 void lafs_clusters_done(struct fs *fs);
768 void lafs_done_work(struct work_struct *ws);
769 void lafs_close_all_segments(struct fs *fs);
770 u32 lafs_seg_setsize(struct fs *fs, struct segpos *seg, u32 size);
771 void lafs_seg_setpos(struct fs *fs, struct segpos *seg, u64 addr);
772 u64 lafs_seg_next(struct fs *fs, struct segpos *seg);
773
774 /* index.c */
775 void lafs_pin_block_ph(struct block *b, int ph);
776 static inline void lafs_pin_block(struct block *b)
777 {
778         lafs_pin_block_ph(b, fs_from_inode(b->inode)->phase);
779 }
780
781 int lafs_add_block_address(struct fs *fs, struct block *blk);
782 void lafs_flip_dblock(struct datablock *db);
783 void lafs_phase_flip(struct fs *fs, struct indexblock *ib);
784 struct indexblock * __must_check
785 lafs_make_iblock(struct inode *ino, int adopt, int async, REFARG);
786 struct indexblock *
787 lafs_iblock_get(struct inode *ino, faddr_t addr, int depth, paddr_t phys, REFARG);
788
789 /* io.c */
790 void lafs_write_head(struct fs *fs, struct cluster_head *head, u64 virt,
791                      struct wc *wc);
792 void lafs_write_block(struct fs *fs, struct block *b, struct wc *wc);
793 void lafs_write_flush(struct fs *fs, struct wc *wc);
794
795 /* quota.c */
796 int lafs_quota_allocate(struct fs *fs, struct inode *ino, int diff);
797
798 #define __wait_event_lock(wq, condition, lock)                          \
799 do {                                                                    \
800         wait_queue_t __wait;                                            \
801         init_waitqueue_entry(&__wait, current);                         \
802                                                                         \
803         add_wait_queue(&wq, &__wait);                                   \
804         for (;;) {                                                      \
805                 set_current_state(TASK_UNINTERRUPTIBLE);                \
806                 if (condition)                                          \
807                         break;                                          \
808                 spin_unlock(&lock);                                     \
809                 schedule();                                             \
810                 spin_lock(&lock);                                       \
811         }                                                               \
812         current->state = TASK_RUNNING;                                  \
813         remove_wait_queue(&wq, &__wait);                                \
814 } while (0)
815
816 #define wait_event_lock(wq, condition, lock)                            \
817 do {                                                                    \
818         if (condition)                                                  \
819                 break;                                                  \
820         __wait_event_lock(wq, condition, lock);                         \
821 } while (0)
822