added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / include / linux / intermezzo_fs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *  Copyright (C) 2001 Tacitus Systems, Inc.
6  *  Copyright (C) 2000 Stelias Computing, Inc.
7  *  Copyright (C) 2000 Red Hat, Inc.
8  *  Copyright (C) 2000 TurboLinux, Inc.
9  *  Copyright (C) 2000 Los Alamos National Laboratory.
10  *
11  *   This file is part of InterMezzo, http://www.inter-mezzo.org.
12  *
13  *   InterMezzo is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   InterMezzo is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with InterMezzo; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #ifndef __INTERMEZZO_FS_H_
28 #define __INTERMEZZO_FS_H_ 1
29
30 #include <linux/intermezzo_lib.h>
31 #include <linux/intermezzo_idl.h>
32
33
34 #ifdef __KERNEL__
35 typedef __u8 uuid_t[16];
36 #else
37 # include <uuid/uuid.h>
38 #endif
39
40 struct lento_vfs_context {
41         __u64 kml_offset;
42         __u64 updated_time;
43         __u64 remote_ino;
44         __u64 remote_generation;
45         __u32 slot_offset;
46         __u32 recno;
47         __u32 flags;
48         uuid_t uuid;
49         struct presto_version remote_version;
50 };
51
52 static inline int izo_ioctl_is_invalid(struct izo_ioctl_data *data);
53
54 #ifdef __KERNEL__
55 # include <linux/smp.h>
56 # include <linux/fsfilter.h>
57 # include <linux/slab.h>
58 # include <linux/vmalloc.h>
59 # include <linux/smp_lock.h>
60
61 /* fixups for fs.h */
62 # ifndef fs_down
63 #  define fs_down(sem) down(sem)
64 # endif
65
66 # ifndef fs_up
67 #  define fs_up(sem) up(sem)
68 # endif
69
70 # define KML_IDLE                        0
71 # define KML_DECODE                      1
72 # define KML_OPTIMIZE                    2
73 # define KML_REINT                       3
74
75 # define KML_OPEN_REINT                  0x0100
76 # define KML_REINT_BEGIN                 0x0200
77 # define KML_BACKFETCH                   0x0400
78 # define KML_REINT_END                   0x0800
79 # define KML_CLOSE_REINT                 0x1000
80 # define KML_REINT_MAXBUF                (64 * 1024)
81
82 # define CACHE_CLIENT_RO       0x4
83 # define CACHE_LENTO_RO        0x8
84
85 /* global variables */
86 extern int presto_debug;
87 extern int presto_print_entry;
88 extern long presto_kmemory;
89 extern long presto_vmemory;
90
91 # define PRESTO_DEBUG
92 # ifdef PRESTO_DEBUG
93 /* debugging masks */
94 #  define D_SUPER       1
95 #  define D_INODE       2
96 #  define D_FILE        4
97 #  define D_CACHE       8  /* cache debugging */
98 #  define D_MALLOC     16  /* print malloc, de-alloc information */
99 #  define D_JOURNAL    32
100 #  define D_UPCALL     64  /* up and downcall debugging */
101 #  define D_PSDEV     128
102 #  define D_PIOCTL    256
103 #  define D_SPECIAL   512
104 #  define D_TIMING   1024
105 #  define D_DOWNCALL 2048
106 #  define D_KML      4096
107 #  define D_FSDATA   8192
108
109 #  define CDEBUG(mask, format, a...)                                    \
110         do {                                                            \
111                 if (presto_debug & mask) {                              \
112                         printk("(%s:%s,l. %d %d): " format, __FILE__,   \
113                                __FUNCTION__, __LINE__, current->pid     \
114                                , ## a);                                 \
115                 }                                                       \
116         } while (0)
117
118 #define CERROR(format, a...)                                            \
119 do {                                                                    \
120         printk("(%s:%s,l. %d %d): " format, __FILE__, __FUNCTION__,     \
121                __LINE__, current->pid , ## a);                          \
122 } while (0)
123
124 #  define ENTRY                                                         \
125         if (presto_print_entry)                                         \
126                 printk("Process %d entered %s\n", current->pid, __FUNCTION__)
127
128 #  define EXIT                                                          \
129         if (presto_print_entry)                                         \
130                 printk("Process %d leaving %s at %d\n", current->pid,   \
131                        __FUNCTION__, __LINE__)
132
133 #  define presto_kmem_inc(ptr, size) presto_kmemory += (size)
134 #  define presto_kmem_dec(ptr, size) presto_kmemory -= (size)
135 #  define presto_vmem_inc(ptr, size) presto_vmemory += (size)
136 #  define presto_vmem_dec(ptr, size) presto_vmemory -= (size)
137 # else /* !PRESTO_DEBUG */
138 #  define CDEBUG(mask, format, a...) do {} while (0)
139 #  define ENTRY do {} while (0)
140 #  define EXIT do {} while (0)
141 #  define presto_kmem_inc(ptr, size) do {} while (0)
142 #  define presto_kmem_dec(ptr, size) do {} while (0)
143 #  define presto_vmem_inc(ptr, size) do {} while (0)
144 #  define presto_vmem_dec(ptr, size) do {} while (0)
145 # endif /* PRESTO_DEBUG */
146
147
148 struct run_ctxt {
149         struct vfsmount *pwdmnt;
150         struct dentry   *pwd;
151         struct vfsmount *rootmnt;
152         struct dentry   *root;
153         uid_t            fsuid;
154         gid_t            fsgid;
155         mm_segment_t     fs;
156         int              ngroups;
157         gid_t            groups[NGROUPS];
158
159 };
160
161 static inline void push_ctxt(struct run_ctxt *save, struct run_ctxt *new)
162 {
163         int i;
164         save->fs = get_fs();
165         save->pwd = dget(current->fs->pwd);
166         save->pwdmnt = mntget(current->fs->pwdmnt);
167         save->fsgid = current->fsgid;
168         save->fsuid = current->fsuid;
169         save->root = current->fs->root;
170         save->rootmnt = current->fs->rootmnt;
171         save->ngroups = current->ngroups;
172         for (i = 0; i< current->ngroups; i++) 
173                 save->groups[i] = current->groups[i];
174
175         set_fs(new->fs);
176         lock_kernel();
177         set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
178         if (new->root)
179                 set_fs_root(current->fs, new->rootmnt, new->root);
180         unlock_kernel();
181         current->fsuid = new->fsuid;
182         current->fsgid = new->fsgid;
183         if (new->ngroups > 0) {
184                 current->ngroups = new->ngroups;
185                 for (i = 0; i< new->ngroups; i++) 
186                         current->groups[i] = new->groups[i];
187         }
188         
189 }
190
191 static inline void pop_ctxt(struct run_ctxt *saved)
192 {
193         int i;
194
195         set_fs(saved->fs);
196         lock_kernel();
197         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
198         if (saved->root)
199                 set_fs_root(current->fs, saved->rootmnt, saved->root);
200         unlock_kernel();
201         current->fsuid = saved->fsuid;
202         current->fsgid = saved->fsgid;
203         current->ngroups = saved->ngroups;
204         for (i = 0; i< saved->ngroups; i++) 
205                 current->groups[i] = saved->groups[i];
206
207         mntput(saved->pwdmnt);
208         dput(saved->pwd);
209 }
210
211 static inline struct presto_dentry_data *presto_d2d(struct dentry *dentry)
212 {
213         return (struct presto_dentry_data *)(dentry->d_fsdata);
214 }
215
216 struct presto_cache {
217         spinlock_t          cache_lock;
218         loff_t              cache_reserved;
219         struct  vfsmount   *cache_vfsmount;
220         struct super_block *cache_sb;
221         struct  dentry     *cache_root;
222         struct list_head    cache_chain; /* for the dev/cache hash */
223
224         int   cache_flags;
225
226         kdev_t cache_dev;            /* underlying block device */
227
228         char *cache_type;            /* filesystem type of cache */
229         struct filter_fs *cache_filter;
230
231         struct upc_channel *cache_psdev;  /* points to channel used */
232         struct list_head cache_channel_list; 
233         struct list_head cache_fset_list; /* filesets mounted in cache */
234 };
235
236 struct presto_log_fd {
237         rwlock_t         fd_lock;
238         loff_t           fd_offset;  /* offset where next record should go */
239         struct file    *fd_file;
240         int             fd_truncating;
241         unsigned int   fd_recno;   /* last recno written */
242         struct list_head  fd_reservations;
243 };
244
245 /* file sets */
246 # define CHUNK_BITS  16
247
248 struct presto_file_set {
249         struct list_head fset_list;
250         struct presto_log_fd fset_kml;
251         struct presto_log_fd fset_lml;
252         struct presto_log_fd fset_rcvd;
253         struct list_head *fset_clients;  /* cache of clients */
254         struct dentry *fset_dentry;
255         struct vfsmount *fset_mnt;
256         struct presto_cache *fset_cache;
257
258         unsigned int fset_lento_recno;  /* last recno mentioned to lento */
259         loff_t fset_lento_off;    /* last offset mentioned to lento */
260         loff_t fset_kml_logical_off; /* logical offset of kml file byte 0 */
261         char * fset_name;
262
263         int fset_flags;
264         int fset_chunkbits;
265         char *fset_reint_buf; /* temporary buffer holds kml during reint */
266
267         spinlock_t fset_permit_lock;
268         int fset_permit_count;
269         int fset_permit_upcall_count;
270         /* This queue is used both for processes waiting for the kernel to give
271          * up the permit as well as processes waiting for the kernel to be given
272          * the permit, depending on the state of FSET_HASPERMIT. */
273         wait_queue_head_t fset_permit_queue;
274
275         loff_t  fset_file_maxio;  /* writing more than this causes a close */
276         unsigned long int kml_truncate_size;
277 };
278
279 /* This is the default number of bytes written before a close is recorded*/
280 #define FSET_DEFAULT_MAX_FILEIO (1024<<10)
281
282 struct dentry *presto_tmpfs_ilookup(struct inode *dir, struct dentry *dentry, 
283                                     ino_t ino, unsigned int generation);
284 struct dentry *presto_iget_ilookup(struct inode *dir, struct dentry *dentry, 
285                                     ino_t ino, unsigned int generation);
286 struct dentry *presto_add_ilookup_dentry(struct dentry *parent,
287                                          struct dentry *real);
288
289 struct journal_ops {
290         int (*tr_all_data)(struct inode *);
291         loff_t (*tr_avail)(struct presto_cache *fset, struct super_block *);
292         void *(*tr_start)(struct presto_file_set *, struct inode *, int op);
293         void (*tr_commit)(struct presto_file_set *, void *handle);
294         void (*tr_journal_data)(struct inode *);
295         struct dentry *(*tr_ilookup)(struct inode *dir, struct dentry *dentry, ino_t ino, unsigned int generation);
296         struct dentry *(*tr_add_ilookup)(struct dentry *parent, struct dentry *real);
297 };
298
299 extern struct journal_ops presto_ext2_journal_ops;
300 extern struct journal_ops presto_ext3_journal_ops;
301 extern struct journal_ops presto_tmpfs_journal_ops;
302 extern struct journal_ops presto_xfs_journal_ops;
303 extern struct journal_ops presto_reiserfs_journal_ops;
304 extern struct journal_ops presto_obdfs_journal_ops;
305
306 # define LENTO_FL_KML            0x0001
307 # define LENTO_FL_EXPECT         0x0002
308 # define LENTO_FL_VFSCHECK       0x0004
309 # define LENTO_FL_JUSTLOG        0x0008
310 # define LENTO_FL_WRITE_KML      0x0010
311 # define LENTO_FL_CANCEL_LML     0x0020
312 # define LENTO_FL_WRITE_EXPECT   0x0040
313 # define LENTO_FL_IGNORE_TIME    0x0080
314 # define LENTO_FL_TOUCH_PARENT   0x0100
315 # define LENTO_FL_TOUCH_NEWOBJ   0x0200
316 # define LENTO_FL_SET_DDFILEID   0x0400
317
318 struct presto_cache *presto_get_cache(struct inode *inode);
319 int presto_sprint_mounts(char *buf, int buflen, int minor);
320 struct presto_file_set *presto_fset(struct dentry *de);
321 int presto_journal(struct dentry *dentry, char *buf, size_t size);
322 int presto_fwrite(struct file *file, const char *str, int len, loff_t *off);
323 int presto_ispresto(struct inode *);
324
325 /* super.c */
326 extern struct file_system_type presto_fs_type;
327 extern int init_intermezzo_fs(void);
328
329 /* fileset.c */
330 extern int izo_prepare_fileset(struct dentry *root, char *fsetname);
331 char * izo_make_path(struct presto_file_set *fset, char *name);
332 struct file *izo_fset_open(struct presto_file_set *fset, char *name, int flags, int mode);
333
334 /* psdev.c */
335 int izo_psdev_get_free_channel(void);
336 int presto_psdev_init(void);
337 int izo_psdev_setpid(int minor);
338 extern void presto_psdev_cleanup(void);
339 inline int presto_lento_up(int minor);
340 int izo_psdev_setchannel(struct file *file, int fd);
341
342 /* inode.c */
343 extern struct super_operations presto_super_ops;
344 void presto_set_ops(struct inode *inode, struct  filter_fs *filter);
345
346 /* dcache.c */
347 void presto_frob_dop(struct dentry *de);
348 char *presto_path(struct dentry *dentry, struct dentry *root,
349                   char *buffer, int buflen);
350 inline struct presto_dentry_data *izo_alloc_ddata(void);
351 int presto_set_dd(struct dentry *);
352 int presto_init_ddata_cache(void);
353 void presto_cleanup_ddata_cache(void);
354 extern struct dentry_operations presto_dentry_ops;
355
356 /* dir.c */
357 extern struct inode_operations presto_dir_iops;
358 extern struct inode_operations presto_file_iops;
359 extern struct inode_operations presto_sym_iops;
360 extern struct file_operations presto_dir_fops;
361 extern struct file_operations presto_file_fops;
362 extern struct file_operations presto_sym_fops;
363 int presto_setattr(struct dentry *de, struct iattr *iattr);
364 int presto_settime(struct presto_file_set *fset, struct dentry *newobj,
365                    struct dentry *parent, struct dentry *target,
366                    struct lento_vfs_context *ctx, int valid);
367 int presto_ioctl(struct inode *inode, struct file *file,
368                  unsigned int cmd, unsigned long arg);
369
370 extern int presto_ilookup_uid;
371 # define PRESTO_ILOOKUP_MAGIC "...ino:"
372 # define PRESTO_ILOOKUP_SEP ':'
373 int izo_dentry_is_ilookup(struct dentry *, ino_t *id, unsigned int *generation);
374 struct dentry *presto_lookup(struct inode * dir, struct dentry *dentry);
375
376 struct presto_dentry_data {
377         int dd_count; /* how mnay dentries are using this dentry */
378         struct presto_file_set *dd_fset;
379         struct dentry *dd_inodentry; 
380         loff_t dd_kml_offset;
381         int dd_flags;
382         __u64 remote_ino;
383         __u64 remote_generation;
384 };
385
386 struct presto_file_data {
387         int fd_do_lml;
388         loff_t fd_lml_offset;
389         size_t fd_bytes_written;
390         /* authorization related data of file at open time */
391         uid_t fd_uid;
392         gid_t fd_gid;
393         mode_t fd_mode;
394         /* identification data of calling process */
395         uid_t fd_fsuid;
396         gid_t fd_fsgid;
397         int fd_ngroups;
398         gid_t fd_groups[NGROUPS_MAX];
399         /* information how to complete the close operation */
400         struct lento_vfs_context fd_info;
401         struct presto_version fd_version;
402 };
403
404 /* presto.c and Lento::Downcall */
405
406 int presto_walk(const char *name, struct nameidata *nd);
407 int izo_clear_fsetroot(struct dentry *dentry);
408 int izo_clear_all_fsetroots(struct presto_cache *cache);
409 int presto_get_kmlsize(char *path, __u64 *size);
410 int presto_get_lastrecno(char *path, off_t *size);
411 int presto_set_fsetroot(struct dentry *dentry, char *fsetname,
412                        unsigned int flags);
413 int presto_set_fsetroot_from_ioc(struct dentry *dentry, char *fsetname,
414                                  unsigned int flags);
415 inline int presto_is_read_only(struct presto_file_set *);
416 int presto_truncate_lml(struct presto_file_set *fset);
417 int lento_write_lml(char *path,
418                      __u64 remote_ino,
419                      __u32 remote_generation,
420                      __u32 remote_version,
421                     struct presto_version *remote_file_version);
422 int lento_complete_closes(char *path);
423 inline int presto_f2m(struct presto_file_set *fset);
424 int presto_prep(struct dentry *, struct presto_cache **,
425                        struct presto_file_set **);
426 /* cache.c */
427 extern struct presto_cache *presto_cache_init(void);
428 extern inline void presto_cache_add(struct presto_cache *cache, kdev_t dev);
429 extern inline void presto_cache_init_hash(void);
430
431 struct presto_cache *presto_cache_find(kdev_t dev);
432
433 #define PRESTO_REQLOW  (3 * 4096)
434 #define PRESTO_REQHIGH (6 * 4096)
435 void presto_release_space(struct presto_cache *cache, loff_t req);
436 int presto_reserve_space(struct presto_cache *cache, loff_t req);
437
438 #define PRESTO_DATA             0x00000002 /* cached data is valid */
439 #define PRESTO_ATTR             0x00000004 /* attributes cached */
440 #define PRESTO_DONT_JOURNAL     0x00000008 /* things like .intermezzo/ */
441
442 struct presto_file_set *presto_path2fileset(const char *name);
443 int izo_revoke_permit(struct dentry *, uuid_t uuid);
444 int presto_chk(struct dentry *dentry, int flag);
445 void presto_set(struct dentry *dentry, int flag);
446 int presto_get_permit(struct inode *inode);
447 int presto_put_permit(struct inode *inode);
448 int presto_set_max_kml_size(const char *path, unsigned long max_size);
449 int izo_mark_dentry(struct dentry *dentry, int and, int or, int *res);
450 int izo_mark_cache(struct dentry *dentry, int and_bits, int or_bits, int *);
451 int izo_mark_fset(struct dentry *dentry, int and_bits, int or_bits, int *);
452 void presto_getversion(struct presto_version *pv, struct inode *inode);
453 int presto_i2m(struct inode *inode);
454 int presto_c2m(struct presto_cache *cache);
455
456
457 /* file.c */
458 int izo_purge_file(struct presto_file_set *fset, char *file);
459 int presto_adjust_lml(struct file *file, struct lento_vfs_context *info);
460
461 /* journal.c */
462 struct rec_info {
463         loff_t offset;
464         int size;
465         int recno;
466         int is_kml;
467 };
468
469 void presto_trans_commit(struct presto_file_set *fset, void *handle);
470 void *presto_trans_start(struct presto_file_set *fset, struct inode *inode,
471                          int op);
472 int presto_fread(struct file *file, char *str, int len, loff_t *off);
473 int presto_clear_lml_close(struct presto_file_set *fset,
474                            loff_t  lml_offset);
475 int presto_complete_lml(struct presto_file_set *fset);
476 int presto_read_kml_logical_offset(struct rec_info *recinfo,
477                                    struct presto_file_set *fset);
478 int presto_write_kml_logical_offset(struct presto_file_set *fset);
479 struct file *presto_copy_kml_tail(struct presto_file_set *fset,
480                                   unsigned long int start);
481 int presto_finish_kml_truncate(struct presto_file_set *fset,
482                                unsigned long int offset);
483 int izo_lookup_file(struct presto_file_set *fset, char *path,
484                     struct nameidata *nd);
485 int izo_do_truncate(struct presto_file_set *fset, struct dentry *dentry,
486                     loff_t length,  loff_t size_check);
487 int izo_log_close(struct presto_log_fd *logfd);
488 struct file *izo_log_open(struct presto_file_set *fset, char *name, int flags);
489 int izo_init_kml_file(struct presto_file_set *, struct presto_log_fd *);
490 int izo_init_lml_file(struct presto_file_set *, struct presto_log_fd *);
491 int izo_init_last_rcvd_file(struct presto_file_set *, struct presto_log_fd *);
492
493 /* vfs.c */
494
495 /* Extra data needed in the KML for rollback operations; this structure is
496  * passed around during the KML-writing process. */
497 struct izo_rollback_data {
498         __u32 rb_mode;
499         __u32 rb_rdev;
500         __u64 rb_uid;
501         __u64 rb_gid;
502 };
503
504 int presto_write_last_rcvd(struct rec_info *recinfo,
505                            struct presto_file_set *fset,
506                            struct lento_vfs_context *info);
507 void izo_get_rollback_data(struct inode *inode, struct izo_rollback_data *rb);
508 int presto_do_close(struct presto_file_set *fset, struct file *file);
509 int presto_do_setattr(struct presto_file_set *fset, struct dentry *dentry,
510                       struct iattr *iattr, struct lento_vfs_context *info);
511 int presto_do_create(struct presto_file_set *fset, struct dentry *dir,
512                      struct dentry *dentry, int mode,
513                      struct lento_vfs_context *info);
514 int presto_do_link(struct presto_file_set *fset, struct dentry *dir,
515                    struct dentry *old_dentry, struct dentry *new_dentry,
516                    struct lento_vfs_context *info);
517 int presto_do_unlink(struct presto_file_set *fset, struct dentry *dir,
518                      struct dentry *dentry, struct lento_vfs_context *info);
519 int presto_do_symlink(struct presto_file_set *fset, struct dentry *dir,
520                       struct dentry *dentry, const char *name,
521                       struct lento_vfs_context *info);
522 int presto_do_mkdir(struct presto_file_set *fset, struct dentry *dir,
523                     struct dentry *dentry, int mode,
524                     struct lento_vfs_context *info);
525 int presto_do_rmdir(struct presto_file_set *fset, struct dentry *dir,
526                     struct dentry *dentry, struct lento_vfs_context *info);
527 int presto_do_mknod(struct presto_file_set *fset, struct dentry *dir,
528                     struct dentry *dentry, int mode, dev_t dev,
529                     struct lento_vfs_context *info);
530 int do_rename(struct presto_file_set *fset, struct dentry *old_dir,
531               struct dentry *old_dentry, struct dentry *new_dir,
532               struct dentry *new_dentry, struct lento_vfs_context *info);
533 int presto_do_statfs (struct presto_file_set *fset,
534                       struct statfs * buf);
535
536 int lento_setattr(const char *name, struct iattr *iattr,
537                   struct lento_vfs_context *info);
538 int lento_create(const char *name, int mode, struct lento_vfs_context *info);
539 int lento_link(const char *oldname, const char *newname,
540                struct lento_vfs_context *info);
541 int lento_unlink(const char *name, struct lento_vfs_context *info);
542 int lento_symlink(const char *oldname,const char *newname,
543                   struct lento_vfs_context *info);
544 int lento_mkdir(const char *name, int mode, struct lento_vfs_context *info);
545 int lento_rmdir(const char *name, struct lento_vfs_context *info);
546 int lento_mknod(const char *name, int mode, dev_t dev,
547                 struct lento_vfs_context *info);
548 int lento_rename(const char *oldname, const char *newname,
549                  struct lento_vfs_context *info);
550 int lento_iopen(const char *name, ino_t ino, unsigned int generation,int flags);
551
552 /* journal.c */
553
554 #define JOURNAL_PAGE_SZ  PAGE_SIZE
555
556 __inline__ int presto_no_journal(struct presto_file_set *fset);
557 int journal_fetch(int minor);
558 int presto_log(struct presto_file_set *fset, struct rec_info *rec,
559                const char *buf, size_t size,
560                const char *string1, int len1, 
561                const char *string2, int len2,
562                const char *string3, int len3);
563 int presto_get_fileid(int minor, struct presto_file_set *fset,
564                       struct dentry *dentry);
565 int presto_journal_setattr(struct rec_info *rec, struct presto_file_set *fset,
566                            struct dentry *dentry, struct presto_version *old_ver,
567                            struct izo_rollback_data *, struct iattr *iattr);
568 int presto_journal_create(struct rec_info *rec, struct presto_file_set *fset,
569                           struct dentry *dentry,
570                           struct presto_version *tgt_dir_ver,
571                           struct presto_version *new_file_ver, int mode);
572 int presto_journal_link(struct rec_info *rec, struct presto_file_set *fset,
573                         struct dentry *src, struct dentry *tgt,
574                         struct presto_version *tgt_dir_ver,
575                         struct presto_version *new_link_ver);
576 int presto_journal_unlink(struct rec_info *rec, struct presto_file_set *fset,
577                           struct dentry *dir,
578                           struct presto_version *tgt_dir_ver,
579                           struct presto_version *old_file_ver,
580                           struct izo_rollback_data *, struct dentry *dentry,
581                           char *old_target, int old_targetlen);
582 int presto_journal_symlink(struct rec_info *rec, struct presto_file_set *fset,
583                            struct dentry *dentry, const char *target,
584                            struct presto_version *tgt_dir_ver,
585                            struct presto_version *new_link_ver);
586 int presto_journal_mkdir(struct rec_info *rec, struct presto_file_set *fset,
587                          struct dentry *dentry,
588                          struct presto_version *tgt_dir_ver,
589                          struct presto_version *new_dir_ver, int mode);
590 int presto_journal_rmdir(struct rec_info *rec, struct presto_file_set *fset,
591                          struct dentry *dentry,
592                          struct presto_version *tgt_dir_ver,
593                          struct presto_version *old_dir_ver,
594                          struct izo_rollback_data *, int len, const char *name);
595 int presto_journal_mknod(struct rec_info *rec, struct presto_file_set *fset,
596                          struct dentry *dentry,
597                          struct presto_version *tgt_dir_ver,
598                          struct presto_version *new_node_ver, int mode,
599                          int dmajor, int dminor);
600 int presto_journal_rename(struct rec_info *rec, struct presto_file_set *fset,
601                           struct dentry *src, struct dentry *tgt,
602                           struct presto_version *src_dir_ver,
603                           struct presto_version *tgt_dir_ver);
604 int presto_journal_open(struct rec_info *, struct presto_file_set *,
605                         struct dentry *, struct presto_version *old_ver);
606 int presto_journal_close(struct rec_info *rec, struct presto_file_set *,
607                          struct file *, struct dentry *,
608                          struct presto_version *old_file_ver,
609                          struct presto_version *new_file_ver);
610 int presto_write_lml_close(struct rec_info *rec,
611                            struct presto_file_set *fset, 
612                            struct file *file,
613                            __u64 remote_ino,
614                            __u64 remote_generation,
615                            struct presto_version *remote_version,
616                            struct presto_version *new_file_ver);
617 void presto_log_op(void *data, int len);
618 loff_t presto_kml_offset(struct presto_file_set *fset);
619
620 /* upcall.c */
621 #define SYNCHRONOUS 0
622 #define ASYNCHRONOUS 1
623 /* asynchronous calls */
624 int izo_upc_kml(int minor, __u64 offset, __u32 first_recno, __u64 length,
625                 __u32 last_recno, char *fsetname);
626 int izo_upc_kml_truncate(int minor, __u64 length, __u32 last_recno,
627                          char *fsetname);
628 int izo_upc_go_fetch_kml(int minor, char *fsetname, uuid_t uuid, __u64 kmlsize);
629 int izo_upc_backfetch(int minor, char *path, char *fileset, 
630                       struct lento_vfs_context *);
631
632 /* synchronous calls */
633 int izo_upc_get_fileid(int minor, __u32 reclen, char *rec, 
634                        __u32 pathlen, char *path, char *fsetname);
635 int izo_upc_permit(int minor, struct dentry *, __u32 pathlen, char *path,
636                    char *fset);
637 int izo_upc_open(int minor, __u32 pathlen, char *path, char *fsetname, 
638                  struct lento_vfs_context *info);
639 int izo_upc_connect(int minor, __u64 ip_address, __u64 port, __u8 uuid[16],
640                     int client_flag);
641 int izo_upc_revoke_permit(int minor, char *fsetname, uuid_t uuid);
642 int izo_upc_set_kmlsize(int minor, char *fsetname, uuid_t uuid, __u64 kmlsize);
643 int izo_upc_client_make_branch(int minor, char *fsetname);
644 int izo_upc_server_make_branch(int minor, char *fsetname);
645 int izo_upc_branch_undo(int minor, char *fsetname, char *branchname);
646 int izo_upc_branch_redo(int minor, char *fsetname, char *branchname);
647 int izo_upc_repstatus(int minor,  char * fsetname, struct izo_rcvd_rec *lr_server);
648
649 /* general mechanism */
650 int izo_upc_upcall(int minor, int *size, struct izo_upcall_hdr *, int async);
651
652 /* replicator.c */
653 int izo_repstatus(struct presto_file_set *fset, __u64 client_kmlsize, 
654                   struct izo_rcvd_rec *lr_client, struct izo_rcvd_rec *lr_server);
655 int izo_rep_cache_init(struct presto_file_set *);
656 loff_t izo_rcvd_get(struct izo_rcvd_rec *, struct presto_file_set *, char *uuid);
657 loff_t izo_rcvd_write(struct presto_file_set *, struct izo_rcvd_rec *);
658 loff_t izo_rcvd_upd_remote(struct presto_file_set *fset, char * uuid,  __u64 remote_recno,
659                            __u64 remote_offset);
660
661 /* sysctl.c */
662 int init_intermezzo_sysctl(void);
663 void cleanup_intermezzo_sysctl(void);
664
665 /* ext_attr.c */
666 /* We will be more tolerant than the default ea patch with attr name sizes and
667  * the size of value. If these come via VFS from the default ea patches, the
668  * corresponding character strings will be truncated anyway. During journalling- * we journal length for both name and value. See journal_set_ext_attr.
669  */
670 #define PRESTO_EXT_ATTR_NAME_MAX 128
671 #define PRESTO_EXT_ATTR_VALUE_MAX 8192
672
673 #define PRESTO_ALLOC(ptr, size)                                         \
674 do {                                                                    \
675         long s = (size);                                                \
676         (ptr) = kmalloc(s, GFP_KERNEL);                                 \
677         if ((ptr) == NULL)                                              \
678                 CERROR("IZO: out of memory at %s:%d (trying to "        \
679                        "allocate %ld)\n", __FILE__, __LINE__, s);       \
680         else {                                                          \
681                 presto_kmem_inc((ptr), s);                              \
682                 memset((ptr), 0, s);                                    \
683         }                                                               \
684         CDEBUG(D_MALLOC, "kmalloced: %ld at %p (tot %ld).\n",           \
685                s, (ptr), presto_kmemory);                               \
686 } while (0)
687
688 #define PRESTO_FREE(ptr, size)                                          \
689 do {                                                                    \
690         long s = (size);                                                \
691         if ((ptr) == NULL) {                                            \
692                 CERROR("IZO: free NULL pointer (%ld bytes) at "         \
693                        "%s:%d\n", s, __FILE__, __LINE__);               \
694                 break;                                                  \
695         }                                                               \
696         kfree(ptr);                                                     \
697         CDEBUG(D_MALLOC, "kfreed: %ld at %p (tot %ld).\n",              \
698                s, (ptr), presto_kmemory);                               \
699         presto_kmem_dec((ptr), s);                                      \
700 } while (0)
701
702 static inline int dentry_name_cmp(struct dentry *dentry, char *name)
703 {
704         return (strlen(name) == dentry->d_name.len &&
705                 memcmp(name, dentry->d_name.name, dentry->d_name.len) == 0);
706 }
707
708 static inline char *strdup(char *str)
709 {
710         char *tmp;
711         tmp = kmalloc(strlen(str) + 1, GFP_KERNEL);
712         if (tmp)
713                 memcpy(tmp, str, strlen(str) + 1);
714                
715         return tmp;
716 }
717
718 /* buffer MUST be at least the size of izo_ioctl_hdr */
719 static inline int izo_ioctl_getdata(char *buf, char *end, void *arg)
720 {
721         struct izo_ioctl_hdr *hdr;
722         struct izo_ioctl_data *data;
723         int err;
724         ENTRY;
725
726         hdr = (struct izo_ioctl_hdr *)buf;
727         data = (struct izo_ioctl_data *)buf;
728
729         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
730         if ( err ) {
731                 EXIT;
732                 return err;
733         }
734
735         if (hdr->ioc_version != IZO_IOCTL_VERSION) {
736                 CERROR("IZO: version mismatch kernel vs application\n");
737                 return -EINVAL;
738         }
739
740         if (hdr->ioc_len + buf >= end) {
741                 CERROR("IZO: user buffer exceeds kernel buffer\n");
742                 return -EINVAL;
743         }
744
745         if (hdr->ioc_len < sizeof(struct izo_ioctl_data)) {
746                 CERROR("IZO: user buffer too small for ioctl\n");
747                 return -EINVAL;
748         }
749
750         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
751         if ( err ) {
752                 EXIT;
753                 return err;
754         }
755
756         if (izo_ioctl_is_invalid(data)) {
757                 CERROR("IZO: ioctl not correctly formatted\n");
758                 return -EINVAL;
759         }
760
761         if (data->ioc_inllen1) {
762                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
763         }
764
765         if (data->ioc_inllen2) {
766                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
767                         size_round(data->ioc_inllen1);
768         }
769
770         EXIT;
771         return 0;
772 }
773
774 # define MYPATHLEN(buffer, path) ((buffer) + PAGE_SIZE - (path))
775
776 # define free kfree
777 # define malloc(a) kmalloc(a, GFP_KERNEL)
778 # define printf printk
779 int kml_reint_rec(struct file *dir, struct izo_ioctl_data *data);
780 int izo_get_fileid(struct file *dir, struct izo_ioctl_data *data);
781 int izo_set_fileid(struct file *dir, struct izo_ioctl_data *data);
782
783 #else /* __KERNEL__ */
784 # include <stdlib.h>
785 # include <stdio.h>
786 # include <sys/types.h>
787 # include <sys/ioctl.h>
788 # include <string.h>
789
790 # define printk printf
791 # ifndef CERROR
792 #   define CERROR printf
793 # endif
794 # define kmalloc(a,b) malloc(a)
795
796 void init_fsreintdata (void);
797 int kml_fsreint(struct kml_rec *rec, char *basedir);
798 int kml_iocreint(__u32 size, char *ptr, __u32 offset, int dird,
799                  uuid_t uuid, __u32 generate_kml);
800
801 static inline int izo_ioctl_packlen(struct izo_ioctl_data *data);
802
803 static inline void izo_ioctl_init(struct izo_ioctl_data *data)
804 {
805         memset(data, 0, sizeof(*data));
806         data->ioc_len = sizeof(*data);
807         data->ioc_version = IZO_IOCTL_VERSION;
808 }
809
810 static inline int
811 izo_ioctl_pack(struct izo_ioctl_data *data, char **pbuf, int max)
812 {
813         char *ptr;
814         struct izo_ioctl_data *overlay;
815         data->ioc_len = izo_ioctl_packlen(data);
816         data->ioc_version = IZO_IOCTL_VERSION;
817
818         if (*pbuf && izo_ioctl_packlen(data) > max)
819                 return 1;
820         if (*pbuf == NULL)
821                 *pbuf = malloc(data->ioc_len);
822         if (*pbuf == NULL)
823                 return 1;
824         overlay = (struct izo_ioctl_data *)*pbuf;
825         memcpy(*pbuf, data, sizeof(*data));
826
827         ptr = overlay->ioc_bulk;
828         if (data->ioc_inlbuf1)
829                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
830         if (data->ioc_inlbuf2)
831                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
832         if (izo_ioctl_is_invalid(overlay))
833                 return 1;
834
835         return 0;
836 }
837
838 #endif /* __KERNEL__*/
839
840 #define IZO_ERROR_NAME 1
841 #define IZO_ERROR_UPDATE 2
842 #define IZO_ERROR_DELETE 3
843 #define IZO_ERROR_RENAME 4
844
845 static inline char *izo_error(int err)
846 {
847 #ifndef __KERNEL__
848         if (err <= 0)
849                 return strerror(-err);
850 #endif
851         switch (err) {
852         case IZO_ERROR_NAME:
853                 return "InterMezzo name/name conflict";
854         case IZO_ERROR_UPDATE:
855                 return "InterMezzo update/update conflict";
856         case IZO_ERROR_DELETE:
857                 return "InterMezzo update/delete conflict";
858         case IZO_ERROR_RENAME:
859                 return "InterMezzo rename/rename conflict";
860         }
861         return "Unknown InterMezzo error";
862 }
863
864 static inline int izo_ioctl_packlen(struct izo_ioctl_data *data)
865 {
866         int len = sizeof(struct izo_ioctl_data);
867         len += size_round(data->ioc_inllen1);
868         len += size_round(data->ioc_inllen2);
869         return len;
870 }
871
872 static inline int izo_ioctl_is_invalid(struct izo_ioctl_data *data)
873 {
874         if (data->ioc_len > (1<<30)) {
875                 CERROR("IZO ioctl: ioc_len larger than 1<<30\n");
876                 return 1;
877         }
878         if (data->ioc_inllen1 > (1<<30)) {
879                 CERROR("IZO ioctl: ioc_inllen1 larger than 1<<30\n");
880                 return 1;
881         }
882         if (data->ioc_inllen2 > (1<<30)) {
883                 CERROR("IZO ioctl: ioc_inllen2 larger than 1<<30\n");
884                 return 1;
885         }
886         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
887                 CERROR("IZO ioctl: inlbuf1 pointer but 0 length\n");
888                 return 1;
889         }
890         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
891                 CERROR("IZO ioctl: inlbuf2 pointer but 0 length\n");
892                 return 1;
893         }
894         if (data->ioc_pbuf1 && !data->ioc_plen1) {
895                 CERROR("IZO ioctl: pbuf1 pointer but 0 length\n");
896                 return 1;
897         }
898         if (data->ioc_pbuf2 && !data->ioc_plen2) {
899                 CERROR("IZO ioctl: pbuf2 pointer but 0 length\n");
900                 return 1;
901         }
902         if (izo_ioctl_packlen(data) != data->ioc_len ) {
903                 CERROR("IZO ioctl: packlen exceeds ioc_len\n");
904                 return 1;
905         }
906         if (data->ioc_inllen1 &&
907             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
908                 CERROR("IZO ioctl: inlbuf1 not 0 terminated\n");
909                 return 1;
910         }
911         if (data->ioc_inllen2 &&
912             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2
913                            - 1] != '\0') {
914                 CERROR("IZO ioctl: inlbuf2 not 0 terminated\n");
915                 return 1;
916         }
917         return 0;
918 }
919
920 /* kml_unpack.c */
921 char *kml_print_rec(struct kml_rec *rec, int brief);
922 int kml_unpack(struct kml_rec *rec, char **buf, char *end);
923
924 #endif