mxcmmc: decrease minimum frequency to make MMC cards work
[safe/jmp/linux-2.6] / drivers / staging / pohmelfs / netfs.h
1 /*
2  * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #ifndef __NETFS_H
17 #define __NETFS_H
18
19 #include <linux/types.h>
20 #include <linux/connector.h>
21
22 #define POHMELFS_CN_IDX                 5
23 #define POHMELFS_CN_VAL                 0
24
25 #define POHMELFS_CTLINFO_ACK            1
26 #define POHMELFS_NOINFO_ACK             2
27
28
29 /*
30  * Network command structure.
31  * Will be extended.
32  */
33 struct netfs_cmd
34 {
35         __u16                   cmd;    /* Command number */
36         __u16                   csize;  /* Attached crypto information size */
37         __u16                   cpad;   /* Attached padding size */
38         __u16                   ext;    /* External flags */
39         __u32                   size;   /* Size of the attached data */
40         __u32                   trans;  /* Transaction id */
41         __u64                   id;     /* Object ID to operate on. Used for feedback.*/
42         __u64                   start;  /* Start of the object. */
43         __u64                   iv;     /* IV sequence */
44         __u8                    data[0];
45 };
46
47 static inline void netfs_convert_cmd(struct netfs_cmd *cmd)
48 {
49         cmd->id = __be64_to_cpu(cmd->id);
50         cmd->start = __be64_to_cpu(cmd->start);
51         cmd->iv = __be64_to_cpu(cmd->iv);
52         cmd->cmd = __be16_to_cpu(cmd->cmd);
53         cmd->ext = __be16_to_cpu(cmd->ext);
54         cmd->csize = __be16_to_cpu(cmd->csize);
55         cmd->cpad = __be16_to_cpu(cmd->cpad);
56         cmd->size = __be32_to_cpu(cmd->size);
57 }
58
59 #define NETFS_TRANS_SINGLE_DST          (1<<0)
60
61 enum {
62         NETFS_READDIR   = 1,    /* Read directory for given inode number */
63         NETFS_READ_PAGE,        /* Read data page from the server */
64         NETFS_WRITE_PAGE,       /* Write data page to the server */
65         NETFS_CREATE,           /* Create directory entry */
66         NETFS_REMOVE,           /* Remove directory entry */
67
68         NETFS_LOOKUP,           /* Lookup single object */
69         NETFS_LINK,             /* Create a link */
70         NETFS_TRANS,            /* Transaction */
71         NETFS_OPEN,             /* Open intent */
72         NETFS_INODE_INFO,       /* Metadata cache coherency synchronization message */
73
74         NETFS_PAGE_CACHE,       /* Page cache invalidation message */
75         NETFS_READ_PAGES,       /* Read multiple contiguous pages in one go */
76         NETFS_RENAME,           /* Rename object */
77         NETFS_CAPABILITIES,     /* Capabilities of the client, for example supported crypto */
78         NETFS_LOCK,             /* Distributed lock message */
79
80         NETFS_XATTR_SET,        /* Set extended attribute */
81         NETFS_XATTR_GET,        /* Get extended attribute */
82         NETFS_CMD_MAX
83 };
84
85 enum {
86         POHMELFS_FLAGS_ADD = 0, /* Network state control message for ADD */
87         POHMELFS_FLAGS_DEL,     /* Network state control message for DEL */
88         POHMELFS_FLAGS_SHOW,    /* Network state control message for SHOW */
89         POHMELFS_FLAGS_CRYPTO,  /* Crypto data control message */
90         POHMELFS_FLAGS_MODIFY,  /* Network state modification message */
91 };
92
93 /*
94  * Always wanted to copy it from socket headers into public one,
95  * since they are __KERNEL__ protected there.
96  */
97 #define _K_SS_MAXSIZE   128
98
99 struct saddr
100 {
101         unsigned short          sa_family;
102         char                    addr[_K_SS_MAXSIZE];
103 };
104
105 enum {
106         POHMELFS_CRYPTO_HASH = 0,
107         POHMELFS_CRYPTO_CIPHER,
108 };
109
110 struct pohmelfs_crypto
111 {
112         unsigned int            idx;            /* Config index */
113         unsigned short          strlen;         /* Size of the attached crypto string including 0-byte
114                                                  * "cbc(aes)" for example */
115         unsigned short          type;           /* HMAC, cipher, both */
116         unsigned int            keysize;        /* Key size */
117         unsigned char           data[0];        /* Algorithm string, key and IV */
118 };
119
120 #define POHMELFS_IO_PERM_READ           (1<<0)
121 #define POHMELFS_IO_PERM_WRITE          (1<<1)
122
123 /*
124  * Configuration command used to create table of different remote servers.
125  */
126 struct pohmelfs_ctl
127 {
128         __u32                   idx;            /* Config index */
129         __u32                   type;           /* Socket type */
130         __u32                   proto;          /* Socket protocol */
131         __u16                   addrlen;        /* Size of the address */
132         __u16                   perm;           /* IO permission */
133         __u16                   prio;           /* IO priority */
134         struct saddr            addr;           /* Remote server address */
135 };
136
137 /*
138  * Ack for userspace about requested command.
139  */
140 struct pohmelfs_cn_ack
141 {
142         struct cn_msg           msg;
143         int                     error;
144         int                     msg_num;
145         int                     unused[3];
146         struct pohmelfs_ctl     ctl;
147 };
148
149 /*
150  * Inode info structure used to sync with server.
151  * Check what stat() returns.
152  */
153 struct netfs_inode_info
154 {
155         unsigned int            mode;
156         unsigned int            nlink;
157         unsigned int            uid;
158         unsigned int            gid;
159         unsigned int            blocksize;
160         unsigned int            padding;
161         __u64                   ino;
162         __u64                   blocks;
163         __u64                   rdev;
164         __u64                   size;
165         __u64                   version;
166 };
167
168 static inline void netfs_convert_inode_info(struct netfs_inode_info *info)
169 {
170         info->mode = __cpu_to_be32(info->mode);
171         info->nlink = __cpu_to_be32(info->nlink);
172         info->uid = __cpu_to_be32(info->uid);
173         info->gid = __cpu_to_be32(info->gid);
174         info->blocksize = __cpu_to_be32(info->blocksize);
175         info->blocks = __cpu_to_be64(info->blocks);
176         info->rdev = __cpu_to_be64(info->rdev);
177         info->size = __cpu_to_be64(info->size);
178         info->version = __cpu_to_be64(info->version);
179         info->ino = __cpu_to_be64(info->ino);
180 }
181
182 /*
183  * Cache state machine.
184  */
185 enum {
186         NETFS_COMMAND_PENDING = 0,      /* Command is being executed */
187         NETFS_INODE_REMOTE_SYNCED,      /* Inode was synced to server */
188         NETFS_INODE_REMOTE_DIR_SYNCED,  /* Inode (directory) was synced from the server */
189         NETFS_INODE_OWNED,              /* Inode is owned by given host */
190         NETFS_INODE_NEED_FLUSH,         /* Inode has to be flushed to the server */
191 };
192
193 /*
194  * POHMELFS capabilities: information about supported
195  * crypto operations (hash/cipher, modes, key sizes and so on),
196  * root informaion (used/available size, number of objects, permissions)
197  */
198 enum pohmelfs_capabilities {
199         POHMELFS_CRYPTO_CAPABILITIES = 0,
200         POHMELFS_ROOT_CAPABILITIES,
201 };
202
203 /* Read-only mount */
204 #define POHMELFS_FLAGS_RO               (1<<0)
205 /* Extended attributes support on/off */
206 #define POHMELFS_FLAGS_XATTR            (1<<1)
207
208 struct netfs_root_capabilities
209 {
210         __u64                   nr_files;
211         __u64                   used, avail;
212         __u64                   flags;
213 };
214
215 static inline void netfs_convert_root_capabilities(struct netfs_root_capabilities *cap)
216 {
217         cap->nr_files = __cpu_to_be64(cap->nr_files);
218         cap->used = __cpu_to_be64(cap->used);
219         cap->avail = __cpu_to_be64(cap->avail);
220         cap->flags = __cpu_to_be64(cap->flags);
221 }
222
223 struct netfs_crypto_capabilities
224 {
225         unsigned short          hash_strlen;    /* Hash string length, like "hmac(sha1) including 0 byte "*/
226         unsigned short          cipher_strlen;  /* Cipher string length with the same format */
227         unsigned int            cipher_keysize; /* Cipher key size */
228 };
229
230 static inline void netfs_convert_crypto_capabilities(struct netfs_crypto_capabilities *cap)
231 {
232         cap->hash_strlen = __cpu_to_be16(cap->hash_strlen);
233         cap->cipher_strlen = __cpu_to_be16(cap->cipher_strlen);
234         cap->cipher_keysize = __cpu_to_be32(cap->cipher_keysize);
235 }
236
237 enum pohmelfs_lock_type {
238         POHMELFS_LOCK_GRAB      = (1<<15),
239
240         POHMELFS_READ_LOCK      = 0,
241         POHMELFS_WRITE_LOCK,
242 };
243
244 struct netfs_lock
245 {
246         __u64                   start;
247         __u64                   ino;
248         __u32                   size;
249         __u32                   type;
250 };
251
252 static inline void netfs_convert_lock(struct netfs_lock *lock)
253 {
254         lock->start = __cpu_to_be64(lock->start);
255         lock->ino = __cpu_to_be64(lock->ino);
256         lock->size = __cpu_to_be32(lock->size);
257         lock->type = __cpu_to_be32(lock->type);
258 }
259
260 #ifdef __KERNEL__
261
262 #include <linux/kernel.h>
263 #include <linux/completion.h>
264 #include <linux/rbtree.h>
265 #include <linux/net.h>
266 #include <linux/poll.h>
267
268 /*
269  * Private POHMELFS cache of objects in directory.
270  */
271 struct pohmelfs_name
272 {
273         struct rb_node          hash_node;
274
275         struct list_head        sync_create_entry;
276
277         u64                     ino;
278
279         u32                     hash;
280         u32                     mode;
281         u32                     len;
282
283         char                    *data;
284 };
285
286 /*
287  * POHMELFS inode. Main object.
288  */
289 struct pohmelfs_inode
290 {
291         struct list_head        inode_entry;            /* Entry in superblock list.
292                                                          * Objects which are not bound to dentry require to be dropped
293                                                          * in ->put_super()
294                                                          */
295         struct rb_root          hash_root;              /* The same, but indexed by name hash and len */
296         struct mutex            offset_lock;            /* Protect both above trees */
297
298         struct list_head        sync_create_list;       /* List of created but not yet synced to the server children */
299
300         unsigned int            drop_count;
301
302         int                     lock_type;              /* How this inode is locked: read or write */
303
304         int                     error;                  /* Transaction error for given inode */
305
306         long                    state;                  /* State machine above */
307
308         u64                     ino;                    /* Inode number */
309         u64                     total_len;              /* Total length of all children names, used to create offsets */
310
311         struct inode            vfs_inode;
312 };
313
314 struct netfs_trans;
315 typedef int (* netfs_trans_complete_t)(struct page **pages, unsigned int page_num,
316                 void *private, int err);
317
318 struct netfs_state;
319 struct pohmelfs_sb;
320
321 struct netfs_trans
322 {
323         /*
324          * Transaction header and attached contiguous data live here.
325          */
326         struct iovec                    iovec;
327
328         /*
329          * Pages attached to transaction.
330          */
331         struct page                     **pages;
332
333         /*
334          * List and protecting lock for transaction destination
335          * network states.
336          */
337         spinlock_t                      dst_lock;
338         struct list_head                dst_list;
339
340         /*
341          * Number of users for given transaction.
342          * For example each network state attached to transaction
343          * via dst_list increases it.
344          */
345         atomic_t                        refcnt;
346
347         /*
348          * Number of pages attached to given transaction.
349          * Some slots in above page array can be NULL, since
350          * for example page can be under writeback already,
351          * so we skip it in this transaction.
352          */
353         unsigned int                    page_num;
354
355         /*
356          * Transaction flags: single dst or broadcast and so on.
357          */
358         unsigned int                    flags;
359
360         /*
361          * Size of the data, which can be placed into
362          * iovec.iov_base area.
363          */
364         unsigned int                    total_size;
365
366         /*
367          * Number of pages to be sent to remote server.
368          * Usually equal to above page_num, but in case of partial
369          * writeback it can accumulate only pages already completed
370          * previous writeback.
371          */
372         unsigned int                    attached_pages;
373
374         /*
375          * Attached number of bytes in all above pages.
376          */
377         unsigned int                    attached_size;
378
379         /*
380          * Unique transacton generation number.
381          * Used as identity in the network state tree of transactions.
382          */
383         unsigned int                    gen;
384
385         /*
386          * Transaction completion status.
387          */
388         int                             result;
389
390         /*
391          * Superblock this transaction belongs to
392          */
393         struct pohmelfs_sb              *psb;
394
395         /*
396          * Crypto engine, which processed this transaction.
397          * Can be not NULL only if crypto engine holds encrypted pages.
398          */
399         struct pohmelfs_crypto_engine   *eng;
400
401         /* Private data */
402         void                            *private;
403
404         /* Completion callback, invoked just before transaction is destroyed */
405         netfs_trans_complete_t          complete;
406 };
407
408 static inline int netfs_trans_cur_len(struct netfs_trans *t)
409 {
410         return (signed)(t->total_size - t->iovec.iov_len);
411 }
412
413 static inline void *netfs_trans_current(struct netfs_trans *t)
414 {
415         return t->iovec.iov_base + t->iovec.iov_len;
416 }
417
418 struct netfs_trans *netfs_trans_alloc(struct pohmelfs_sb *psb, unsigned int size,
419                 unsigned int flags, unsigned int nr);
420 void netfs_trans_free(struct netfs_trans *t);
421 int netfs_trans_finish(struct netfs_trans *t, struct pohmelfs_sb *psb);
422 int netfs_trans_finish_send(struct netfs_trans *t, struct pohmelfs_sb *psb);
423
424 static inline void netfs_trans_reset(struct netfs_trans *t)
425 {
426         t->complete = NULL;
427 }
428
429 struct netfs_trans_dst
430 {
431         struct list_head                trans_entry;
432         struct rb_node                  state_entry;
433
434         unsigned long                   send_time;
435
436         /*
437          * Times this transaction was resent to its old or new,
438          * depending on flags, destinations. When it reaches maximum
439          * allowed number, specified in superblock->trans_retries,
440          * transaction will be freed with ETIMEDOUT error.
441          */
442         unsigned int                    retries;
443
444         struct netfs_trans              *trans;
445         struct netfs_state              *state;
446 };
447
448 struct netfs_trans_dst *netfs_trans_search(struct netfs_state *st, unsigned int gen);
449 void netfs_trans_drop_dst(struct netfs_trans_dst *dst);
450 void netfs_trans_drop_dst_nostate(struct netfs_trans_dst *dst);
451 void netfs_trans_drop_trans(struct netfs_trans *t, struct netfs_state *st);
452 void netfs_trans_drop_last(struct netfs_trans *t, struct netfs_state *st);
453 int netfs_trans_resend(struct netfs_trans *t, struct pohmelfs_sb *psb);
454 int netfs_trans_remove_nolock(struct netfs_trans_dst *dst, struct netfs_state *st);
455
456 int netfs_trans_init(void);
457 void netfs_trans_exit(void);
458
459 struct pohmelfs_crypto_engine
460 {
461         u64                             iv;             /* Crypto IV for current operation */
462         unsigned long                   timeout;        /* Crypto waiting timeout */
463         unsigned int                    size;           /* Size of crypto scratchpad */
464         void                            *data;          /* Temporal crypto scratchpad */
465         /*
466          * Crypto operations performed on objects.
467          */
468         struct crypto_hash              *hash;
469         struct crypto_ablkcipher        *cipher;
470
471         struct pohmelfs_crypto_thread   *thread;        /* Crypto thread which hosts this engine */
472
473         struct page                     **pages;
474         unsigned int                    page_num;
475 };
476
477 struct pohmelfs_crypto_thread
478 {
479         struct list_head                thread_entry;
480
481         struct task_struct              *thread;
482         struct pohmelfs_sb              *psb;
483
484         struct pohmelfs_crypto_engine   eng;
485
486         struct netfs_trans              *trans;
487
488         wait_queue_head_t               wait;
489         int                             error;
490
491         unsigned int                    size;
492         struct page                     *page;
493 };
494
495 void pohmelfs_crypto_thread_make_ready(struct pohmelfs_crypto_thread *th);
496
497 /*
498  * Network state, attached to one server.
499  */
500 struct netfs_state
501 {
502         struct mutex            __state_lock;           /* Can not allow to use the same socket simultaneously */
503         struct mutex            __state_send_lock;
504         struct netfs_cmd        cmd;                    /* Cached command */
505         struct netfs_inode_info info;                   /* Cached inode info */
506
507         void                    *data;                  /* Cached some data */
508         unsigned int            size;                   /* Size of that data */
509
510         struct pohmelfs_sb      *psb;                   /* Superblock */
511
512         struct task_struct      *thread;                /* Async receiving thread */
513
514         /* Waiting/polling machinery */
515         wait_queue_t            wait;
516         wait_queue_head_t       *whead;
517         wait_queue_head_t       thread_wait;
518
519         struct mutex            trans_lock;
520         struct rb_root          trans_root;
521
522         struct pohmelfs_ctl     ctl;                    /* Remote peer */
523
524         struct socket           *socket;                /* Socket object */
525         struct socket           *read_socket;           /* Cached pointer to socket object.
526                                                          * Used to determine if between lock drops socket was changed.
527                                                          * Never used to read data or any kind of access.
528                                                          */
529         /*
530          * Crypto engines to process incoming data.
531          */
532         struct pohmelfs_crypto_engine   eng;
533
534         int                     need_reset;
535 };
536
537 int netfs_state_init(struct netfs_state *st);
538 void netfs_state_exit(struct netfs_state *st);
539
540 static inline void netfs_state_lock_send(struct netfs_state *st)
541 {
542         mutex_lock(&st->__state_send_lock);
543 }
544
545 static inline int netfs_state_trylock_send(struct netfs_state *st)
546 {
547         return mutex_trylock(&st->__state_send_lock);
548 }
549
550 static inline void netfs_state_unlock_send(struct netfs_state *st)
551 {
552         BUG_ON(!mutex_is_locked(&st->__state_send_lock));
553
554         mutex_unlock(&st->__state_send_lock);
555 }
556
557 static inline void netfs_state_lock(struct netfs_state *st)
558 {
559         mutex_lock(&st->__state_lock);
560 }
561
562 static inline void netfs_state_unlock(struct netfs_state *st)
563 {
564         BUG_ON(!mutex_is_locked(&st->__state_lock));
565
566         mutex_unlock(&st->__state_lock);
567 }
568
569 static inline unsigned int netfs_state_poll(struct netfs_state *st)
570 {
571         unsigned int revents = POLLHUP | POLLERR;
572
573         netfs_state_lock(st);
574         if (st->socket)
575                 revents = st->socket->ops->poll(NULL, st->socket, NULL);
576         netfs_state_unlock(st);
577
578         return revents;
579 }
580
581 struct pohmelfs_config;
582
583 struct pohmelfs_sb
584 {
585         struct rb_root          mcache_root;
586         struct mutex            mcache_lock;
587         atomic_long_t           mcache_gen;
588         unsigned long           mcache_timeout;
589
590         unsigned int            idx;
591
592         unsigned int            trans_retries;
593
594         atomic_t                trans_gen;
595
596         unsigned int            crypto_attached_size;
597         unsigned int            crypto_align_size;
598
599         unsigned int            crypto_fail_unsupported;
600
601         unsigned int            crypto_thread_num;
602         struct list_head        crypto_active_list, crypto_ready_list;
603         struct mutex            crypto_thread_lock;
604
605         unsigned int            trans_max_pages;
606         unsigned long           trans_data_size;
607         unsigned long           trans_timeout;
608
609         unsigned long           drop_scan_timeout;
610         unsigned long           trans_scan_timeout;
611
612         unsigned long           wait_on_page_timeout;
613
614         struct list_head        flush_list;
615         struct list_head        drop_list;
616         spinlock_t              ino_lock;
617         u64                     ino;
618
619         /*
620          * Remote nodes POHMELFS connected to.
621          */
622         struct list_head        state_list;
623         struct mutex            state_lock;
624
625         /*
626          * Currently active state to request data from.
627          */
628         struct pohmelfs_config  *active_state;
629
630
631         wait_queue_head_t       wait;
632
633         /*
634          * Timed checks: stale transactions, inodes to be freed and so on.
635          */
636         struct delayed_work     dwork;
637         struct delayed_work     drop_dwork;
638
639         struct super_block      *sb;
640
641         /*
642          * Algorithm strings.
643          */
644         char                    *hash_string;
645         char                    *cipher_string;
646
647         u8                      *hash_key;
648         u8                      *cipher_key;
649
650         /*
651          * Algorithm string lengths.
652          */
653         unsigned int            hash_strlen;
654         unsigned int            cipher_strlen;
655         unsigned int            hash_keysize;
656         unsigned int            cipher_keysize;
657
658         /*
659          * Controls whether to perfrom crypto processing or not.
660          */
661         int                     perform_crypto;
662
663         /*
664          * POHMELFS statistics.
665          */
666         u64                     total_size;
667         u64                     avail_size;
668         atomic_long_t           total_inodes;
669
670         /*
671          * Xattr support, read-only and so on.
672          */
673         u64                     state_flags;
674
675         /*
676          * Temporary storage to detect changes in the wait queue.
677          */
678         long                    flags;
679 };
680
681 static inline void netfs_trans_update(struct netfs_cmd *cmd,
682                 struct netfs_trans *t, unsigned int size)
683 {
684         unsigned int sz = ALIGN(size, t->psb->crypto_align_size);
685
686         t->iovec.iov_len += sizeof(struct netfs_cmd) + sz;
687         cmd->cpad = __cpu_to_be16(sz - size);
688 }
689
690 static inline struct pohmelfs_sb *POHMELFS_SB(struct super_block *sb)
691 {
692         return sb->s_fs_info;
693 }
694
695 static inline struct pohmelfs_inode *POHMELFS_I(struct inode *inode)
696 {
697         return container_of(inode, struct pohmelfs_inode, vfs_inode);
698 }
699
700 static inline u64 pohmelfs_new_ino(struct pohmelfs_sb *psb)
701 {
702         u64 ino;
703
704         spin_lock(&psb->ino_lock);
705         ino = psb->ino++;
706         spin_unlock(&psb->ino_lock);
707
708         return ino;
709 }
710
711 static inline void pohmelfs_put_inode(struct pohmelfs_inode *pi)
712 {
713         struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
714
715         spin_lock(&psb->ino_lock);
716         list_move_tail(&pi->inode_entry, &psb->drop_list);
717         pi->drop_count++;
718         spin_unlock(&psb->ino_lock);
719 }
720
721 struct pohmelfs_config
722 {
723         struct list_head        config_entry;
724
725         struct netfs_state      state;
726 };
727
728 struct pohmelfs_config_group
729 {
730         /*
731          * Entry in the global config group list.
732          */
733         struct list_head        group_entry;
734
735         /*
736          * Index of the current group.
737          */
738         unsigned int            idx;
739         /*
740          * Number of config_list entries in this group entry.
741          */
742         unsigned int            num_entry;
743         /*
744          * Algorithm strings.
745          */
746         char                    *hash_string;
747         char                    *cipher_string;
748
749         /*
750          * Algorithm string lengths.
751          */
752         unsigned int            hash_strlen;
753         unsigned int            cipher_strlen;
754
755         /*
756          * Key and its size.
757          */
758         unsigned int            hash_keysize;
759         unsigned int            cipher_keysize;
760         u8                      *hash_key;
761         u8                      *cipher_key;
762
763         /*
764          * List of config entries (network state info) for given idx.
765          */
766         struct list_head        config_list;
767 };
768
769 int __init pohmelfs_config_init(void);
770 void pohmelfs_config_exit(void);
771 int pohmelfs_copy_config(struct pohmelfs_sb *psb);
772 int pohmelfs_copy_crypto(struct pohmelfs_sb *psb);
773 int pohmelfs_config_check(struct pohmelfs_config *config, int idx);
774 int pohmelfs_state_init_one(struct pohmelfs_sb *psb, struct pohmelfs_config *conf);
775
776 extern const struct file_operations pohmelfs_dir_fops;
777 extern const struct inode_operations pohmelfs_dir_inode_ops;
778
779 int pohmelfs_state_init(struct pohmelfs_sb *psb);
780 void pohmelfs_state_exit(struct pohmelfs_sb *psb);
781 void pohmelfs_state_flush_transactions(struct netfs_state *st);
782
783 void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info);
784
785 void pohmelfs_name_del(struct pohmelfs_inode *parent, struct pohmelfs_name *n);
786 void pohmelfs_free_names(struct pohmelfs_inode *parent);
787 struct pohmelfs_name *pohmelfs_search_hash(struct pohmelfs_inode *pi, u32 hash);
788
789 void pohmelfs_inode_del_inode(struct pohmelfs_sb *psb, struct pohmelfs_inode *pi);
790
791 struct pohmelfs_inode *pohmelfs_create_entry_local(struct pohmelfs_sb *psb,
792         struct pohmelfs_inode *parent, struct qstr *str, u64 start, int mode);
793
794 int pohmelfs_write_create_inode(struct pohmelfs_inode *pi);
795
796 int pohmelfs_write_inode_create(struct inode *inode, struct netfs_trans *trans);
797 int pohmelfs_remove_child(struct pohmelfs_inode *parent, struct pohmelfs_name *n);
798
799 struct pohmelfs_inode *pohmelfs_new_inode(struct pohmelfs_sb *psb,
800                 struct pohmelfs_inode *parent, struct qstr *str,
801                 struct netfs_inode_info *info, int link);
802
803 int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr);
804 int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr);
805
806 int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags,
807                 netfs_trans_complete_t complete, void *priv, u64 start);
808 int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon,
809                 unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start);
810
811 void pohmelfs_check_states(struct pohmelfs_sb *psb);
812 void pohmelfs_switch_active(struct pohmelfs_sb *psb);
813
814 int pohmelfs_construct_path_string(struct pohmelfs_inode *pi, void *data, int len);
815 int pohmelfs_path_length(struct pohmelfs_inode *pi);
816
817 struct pohmelfs_crypto_completion
818 {
819         struct completion       complete;
820         int                     error;
821 };
822
823 int pohmelfs_trans_crypt(struct netfs_trans *t, struct pohmelfs_sb *psb);
824 void pohmelfs_crypto_exit(struct pohmelfs_sb *psb);
825 int pohmelfs_crypto_init(struct pohmelfs_sb *psb);
826
827 int pohmelfs_crypto_engine_init(struct pohmelfs_crypto_engine *e, struct pohmelfs_sb *psb);
828 void pohmelfs_crypto_engine_exit(struct pohmelfs_crypto_engine *e);
829
830 int pohmelfs_crypto_process_input_data(struct pohmelfs_crypto_engine *e, u64 iv,
831                 void *data, struct page *page, unsigned int size);
832 int pohmelfs_crypto_process_input_page(struct pohmelfs_crypto_engine *e,
833                 struct page *page, unsigned int size, u64 iv);
834
835 static inline u64 pohmelfs_gen_iv(struct netfs_trans *t)
836 {
837         u64 iv = t->gen;
838
839         iv <<= 32;
840         iv |= ((unsigned long)t) & 0xffffffff;
841
842         return iv;
843 }
844
845 int pohmelfs_data_lock(struct pohmelfs_inode *pi, u64 start, u32 size, int type);
846 int pohmelfs_data_unlock(struct pohmelfs_inode *pi, u64 start, u32 size, int type);
847 int pohmelfs_data_lock_response(struct netfs_state *st);
848
849 static inline int pohmelfs_need_lock(struct pohmelfs_inode *pi, int type)
850 {
851         if (test_bit(NETFS_INODE_OWNED, &pi->state)) {
852                 if (type == pi->lock_type)
853                         return 0;
854                 if ((type == POHMELFS_READ_LOCK) && (pi->lock_type == POHMELFS_WRITE_LOCK))
855                         return 0;
856         }
857
858         if (!test_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state))
859                 return 0;
860
861         return 1;
862 }
863
864 int __init pohmelfs_mcache_init(void);
865 void pohmelfs_mcache_exit(void);
866
867 //#define CONFIG_POHMELFS_DEBUG
868
869 #ifdef CONFIG_POHMELFS_DEBUG
870 #define dprintka(f, a...) printk(f, ##a)
871 #define dprintk(f, a...) printk("%d: " f, task_pid_vnr(current), ##a)
872 #else
873 #define dprintka(f, a...) do {} while (0)
874 #define dprintk(f, a...) do {} while (0)
875 #endif
876
877 static inline void netfs_trans_get(struct netfs_trans *t)
878 {
879         atomic_inc(&t->refcnt);
880 }
881
882 static inline void netfs_trans_put(struct netfs_trans *t)
883 {
884         if (atomic_dec_and_test(&t->refcnt)) {
885                 dprintk("%s: t: %p, gen: %u, err: %d.\n",
886                         __func__, t, t->gen, t->result);
887                 if (t->complete)
888                         t->complete(t->pages, t->page_num,
889                                 t->private, t->result);
890                 netfs_trans_free(t);
891         }
892 }
893
894 struct pohmelfs_mcache
895 {
896         struct rb_node                  mcache_entry;
897         struct completion               complete;
898
899         atomic_t                        refcnt;
900
901         u64                             gen;
902
903         void                            *data;
904         u64                             start;
905         u32                             size;
906         int                             err;
907
908         struct netfs_inode_info         info;
909 };
910
911 struct pohmelfs_mcache *pohmelfs_mcache_alloc(struct pohmelfs_sb *psb, u64 start,
912                 unsigned int size, void *data);
913 void pohmelfs_mcache_free(struct pohmelfs_sb *psb, struct pohmelfs_mcache *m);
914 struct pohmelfs_mcache *pohmelfs_mcache_search(struct pohmelfs_sb *psb, u64 gen);
915 void pohmelfs_mcache_remove_locked(struct pohmelfs_sb *psb, struct pohmelfs_mcache *m);
916
917 static inline void pohmelfs_mcache_get(struct pohmelfs_mcache *m)
918 {
919         atomic_inc(&m->refcnt);
920 }
921
922 static inline void pohmelfs_mcache_put(struct pohmelfs_sb *psb,
923                 struct pohmelfs_mcache *m)
924 {
925         if (atomic_dec_and_test(&m->refcnt))
926                 pohmelfs_mcache_free(psb, m);
927 }
928
929 #endif /* __KERNEL__*/
930
931 #endif /* __NETFS_H */