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