1
2
3
4
5
6
7
8
9
10
11
12
13
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#define POHMELFS_NULL_IDX 65535
29
30
31
32
33
34struct netfs_cmd {
35 __u16 cmd;
36 __u16 csize;
37 __u16 cpad;
38 __u16 ext;
39 __u32 size;
40 __u32 trans;
41 __u64 id;
42 __u64 start;
43 __u64 iv;
44 __u8 data[0];
45};
46
47static 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
61enum {
62 NETFS_READDIR = 1,
63 NETFS_READ_PAGE,
64 NETFS_WRITE_PAGE,
65 NETFS_CREATE,
66 NETFS_REMOVE,
67
68 NETFS_LOOKUP,
69 NETFS_LINK,
70 NETFS_TRANS,
71 NETFS_OPEN,
72 NETFS_INODE_INFO,
73
74 NETFS_PAGE_CACHE,
75 NETFS_READ_PAGES,
76 NETFS_RENAME,
77 NETFS_CAPABILITIES,
78 NETFS_LOCK,
79
80 NETFS_XATTR_SET,
81 NETFS_XATTR_GET,
82 NETFS_CMD_MAX
83};
84
85enum {
86 POHMELFS_FLAGS_ADD = 0,
87 POHMELFS_FLAGS_DEL,
88 POHMELFS_FLAGS_SHOW,
89 POHMELFS_FLAGS_CRYPTO,
90 POHMELFS_FLAGS_MODIFY,
91 POHMELFS_FLAGS_DUMP,
92 POHMELFS_FLAGS_FLUSH,
93};
94
95
96
97
98
99#define _K_SS_MAXSIZE 128
100
101struct saddr {
102 unsigned short sa_family;
103 char addr[_K_SS_MAXSIZE];
104};
105
106enum {
107 POHMELFS_CRYPTO_HASH = 0,
108 POHMELFS_CRYPTO_CIPHER,
109};
110
111struct pohmelfs_crypto {
112 unsigned int idx;
113 unsigned short strlen;
114
115 unsigned short type;
116 unsigned int keysize;
117 unsigned char data[0];
118};
119
120#define POHMELFS_IO_PERM_READ (1<<0)
121#define POHMELFS_IO_PERM_WRITE (1<<1)
122
123
124
125
126struct pohmelfs_ctl {
127 __u32 idx;
128 __u32 type;
129 __u32 proto;
130 __u16 addrlen;
131 __u16 perm;
132 __u16 prio;
133 struct saddr addr;
134};
135
136
137
138
139struct pohmelfs_cn_ack {
140 struct cn_msg msg;
141 int error;
142 int msg_num;
143 int unused[3];
144 struct pohmelfs_ctl ctl;
145};
146
147
148
149
150
151struct netfs_inode_info {
152 unsigned int mode;
153 unsigned int nlink;
154 unsigned int uid;
155 unsigned int gid;
156 unsigned int blocksize;
157 unsigned int padding;
158 __u64 ino;
159 __u64 blocks;
160 __u64 rdev;
161 __u64 size;
162 __u64 version;
163};
164
165static inline void netfs_convert_inode_info(struct netfs_inode_info *info)
166{
167 info->mode = __cpu_to_be32(info->mode);
168 info->nlink = __cpu_to_be32(info->nlink);
169 info->uid = __cpu_to_be32(info->uid);
170 info->gid = __cpu_to_be32(info->gid);
171 info->blocksize = __cpu_to_be32(info->blocksize);
172 info->blocks = __cpu_to_be64(info->blocks);
173 info->rdev = __cpu_to_be64(info->rdev);
174 info->size = __cpu_to_be64(info->size);
175 info->version = __cpu_to_be64(info->version);
176 info->ino = __cpu_to_be64(info->ino);
177}
178
179
180
181
182enum {
183 NETFS_COMMAND_PENDING = 0,
184 NETFS_INODE_REMOTE_SYNCED,
185 NETFS_INODE_REMOTE_DIR_SYNCED,
186 NETFS_INODE_OWNED,
187 NETFS_INODE_NEED_FLUSH,
188};
189
190
191
192
193
194
195enum pohmelfs_capabilities {
196 POHMELFS_CRYPTO_CAPABILITIES = 0,
197 POHMELFS_ROOT_CAPABILITIES,
198};
199
200
201#define POHMELFS_FLAGS_RO (1<<0)
202
203#define POHMELFS_FLAGS_XATTR (1<<1)
204
205struct netfs_root_capabilities {
206 __u64 nr_files;
207 __u64 used, avail;
208 __u64 flags;
209};
210
211static inline void netfs_convert_root_capabilities(struct netfs_root_capabilities *cap)
212{
213 cap->nr_files = __cpu_to_be64(cap->nr_files);
214 cap->used = __cpu_to_be64(cap->used);
215 cap->avail = __cpu_to_be64(cap->avail);
216 cap->flags = __cpu_to_be64(cap->flags);
217}
218
219struct netfs_crypto_capabilities {
220 unsigned short hash_strlen;
221 unsigned short cipher_strlen;
222 unsigned int cipher_keysize;
223};
224
225static inline void netfs_convert_crypto_capabilities(struct netfs_crypto_capabilities *cap)
226{
227 cap->hash_strlen = __cpu_to_be16(cap->hash_strlen);
228 cap->cipher_strlen = __cpu_to_be16(cap->cipher_strlen);
229 cap->cipher_keysize = __cpu_to_be32(cap->cipher_keysize);
230}
231
232enum pohmelfs_lock_type {
233 POHMELFS_LOCK_GRAB = (1<<15),
234
235 POHMELFS_READ_LOCK = 0,
236 POHMELFS_WRITE_LOCK,
237};
238
239struct netfs_lock {
240 __u64 start;
241 __u64 ino;
242 __u32 size;
243 __u32 type;
244};
245
246static inline void netfs_convert_lock(struct netfs_lock *lock)
247{
248 lock->start = __cpu_to_be64(lock->start);
249 lock->ino = __cpu_to_be64(lock->ino);
250 lock->size = __cpu_to_be32(lock->size);
251 lock->type = __cpu_to_be32(lock->type);
252}
253
254#ifdef __KERNEL__
255
256#include <linux/kernel.h>
257#include <linux/completion.h>
258#include <linux/rbtree.h>
259#include <linux/net.h>
260#include <linux/poll.h>
261
262
263
264
265struct pohmelfs_name {
266 struct rb_node hash_node;
267
268 struct list_head sync_create_entry;
269
270 u64 ino;
271
272 u32 hash;
273 u32 mode;
274 u32 len;
275
276 char *data;
277};
278
279
280
281
282struct pohmelfs_inode {
283 struct list_head inode_entry;
284
285
286
287 struct rb_root hash_root;
288 struct mutex offset_lock;
289
290 struct list_head sync_create_list;
291
292 unsigned int drop_count;
293
294 int lock_type;
295
296 int error;
297
298 long state;
299
300 u64 ino;
301 u64 total_len;
302
303 struct inode vfs_inode;
304};
305
306struct netfs_trans;
307typedef int (* netfs_trans_complete_t)(struct page **pages, unsigned int page_num,
308 void *private, int err);
309
310struct netfs_state;
311struct pohmelfs_sb;
312
313struct netfs_trans {
314
315
316
317 struct iovec iovec;
318
319
320
321
322 struct page **pages;
323
324
325
326
327
328 spinlock_t dst_lock;
329 struct list_head dst_list;
330
331
332
333
334
335
336 atomic_t refcnt;
337
338
339
340
341
342
343
344 unsigned int page_num;
345
346
347
348
349 unsigned int flags;
350
351
352
353
354
355 unsigned int total_size;
356
357
358
359
360
361
362
363 unsigned int attached_pages;
364
365
366
367
368 unsigned int attached_size;
369
370
371
372
373
374 unsigned int gen;
375
376
377
378
379 int result;
380
381
382
383
384 struct pohmelfs_sb *psb;
385
386
387
388
389
390 struct pohmelfs_crypto_engine *eng;
391
392
393 void *private;
394
395
396 netfs_trans_complete_t complete;
397};
398
399static inline int netfs_trans_cur_len(struct netfs_trans *t)
400{
401 return (signed)(t->total_size - t->iovec.iov_len);
402}
403
404static inline void *netfs_trans_current(struct netfs_trans *t)
405{
406 return t->iovec.iov_base + t->iovec.iov_len;
407}
408
409struct netfs_trans *netfs_trans_alloc(struct pohmelfs_sb *psb, unsigned int size,
410 unsigned int flags, unsigned int nr);
411void netfs_trans_free(struct netfs_trans *t);
412int netfs_trans_finish(struct netfs_trans *t, struct pohmelfs_sb *psb);
413int netfs_trans_finish_send(struct netfs_trans *t, struct pohmelfs_sb *psb);
414
415static inline void netfs_trans_reset(struct netfs_trans *t)
416{
417 t->complete = NULL;
418}
419
420struct netfs_trans_dst {
421 struct list_head trans_entry;
422 struct rb_node state_entry;
423
424 unsigned long send_time;
425
426
427
428
429
430
431
432 unsigned int retries;
433
434 struct netfs_trans *trans;
435 struct netfs_state *state;
436};
437
438struct netfs_trans_dst *netfs_trans_search(struct netfs_state *st, unsigned int gen);
439void netfs_trans_drop_dst(struct netfs_trans_dst *dst);
440void netfs_trans_drop_dst_nostate(struct netfs_trans_dst *dst);
441void netfs_trans_drop_trans(struct netfs_trans *t, struct netfs_state *st);
442void netfs_trans_drop_last(struct netfs_trans *t, struct netfs_state *st);
443int netfs_trans_resend(struct netfs_trans *t, struct pohmelfs_sb *psb);
444int netfs_trans_remove_nolock(struct netfs_trans_dst *dst, struct netfs_state *st);
445
446int netfs_trans_init(void);
447void netfs_trans_exit(void);
448
449struct pohmelfs_crypto_engine {
450 u64 iv;
451 unsigned long timeout;
452 unsigned int size;
453 void *data;
454
455
456
457 struct crypto_hash *hash;
458 struct crypto_ablkcipher *cipher;
459
460 struct pohmelfs_crypto_thread *thread;
461
462 struct page **pages;
463 unsigned int page_num;
464};
465
466struct pohmelfs_crypto_thread {
467 struct list_head thread_entry;
468
469 struct task_struct *thread;
470 struct pohmelfs_sb *psb;
471
472 struct pohmelfs_crypto_engine eng;
473
474 struct netfs_trans *trans;
475
476 wait_queue_head_t wait;
477 int error;
478
479 unsigned int size;
480 struct page *page;
481};
482
483void pohmelfs_crypto_thread_make_ready(struct pohmelfs_crypto_thread *th);
484
485
486
487
488struct netfs_state {
489 struct mutex __state_lock;
490 struct mutex __state_send_lock;
491 struct netfs_cmd cmd;
492 struct netfs_inode_info info;
493
494 void *data;
495 unsigned int size;
496
497 struct pohmelfs_sb *psb;
498
499 struct task_struct *thread;
500
501
502 wait_queue_t wait;
503 wait_queue_head_t *whead;
504 wait_queue_head_t thread_wait;
505
506 struct mutex trans_lock;
507 struct rb_root trans_root;
508
509 struct pohmelfs_ctl ctl;
510
511 struct socket *socket;
512 struct socket *read_socket;
513
514
515
516
517
518
519 struct pohmelfs_crypto_engine eng;
520
521 int need_reset;
522};
523
524int netfs_state_init(struct netfs_state *st);
525void netfs_state_exit(struct netfs_state *st);
526
527static inline void netfs_state_lock_send(struct netfs_state *st)
528{
529 mutex_lock(&st->__state_send_lock);
530}
531
532static inline int netfs_state_trylock_send(struct netfs_state *st)
533{
534 return mutex_trylock(&st->__state_send_lock);
535}
536
537static inline void netfs_state_unlock_send(struct netfs_state *st)
538{
539 BUG_ON(!mutex_is_locked(&st->__state_send_lock));
540
541 mutex_unlock(&st->__state_send_lock);
542}
543
544static inline void netfs_state_lock(struct netfs_state *st)
545{
546 mutex_lock(&st->__state_lock);
547}
548
549static inline void netfs_state_unlock(struct netfs_state *st)
550{
551 BUG_ON(!mutex_is_locked(&st->__state_lock));
552
553 mutex_unlock(&st->__state_lock);
554}
555
556static inline unsigned int netfs_state_poll(struct netfs_state *st)
557{
558 unsigned int revents = POLLHUP | POLLERR;
559
560 netfs_state_lock(st);
561 if (st->socket)
562 revents = st->socket->ops->poll(NULL, st->socket, NULL);
563 netfs_state_unlock(st);
564
565 return revents;
566}
567
568struct pohmelfs_config;
569
570struct pohmelfs_sb {
571 struct rb_root mcache_root;
572 struct mutex mcache_lock;
573 atomic_long_t mcache_gen;
574 unsigned long mcache_timeout;
575
576 unsigned int idx;
577
578 unsigned int trans_retries;
579
580 atomic_t trans_gen;
581
582 unsigned int crypto_attached_size;
583 unsigned int crypto_align_size;
584
585 unsigned int crypto_fail_unsupported;
586
587 unsigned int crypto_thread_num;
588 struct list_head crypto_active_list, crypto_ready_list;
589 struct mutex crypto_thread_lock;
590
591 unsigned int trans_max_pages;
592 unsigned long trans_data_size;
593 unsigned long trans_timeout;
594
595 unsigned long drop_scan_timeout;
596 unsigned long trans_scan_timeout;
597
598 unsigned long wait_on_page_timeout;
599
600 struct list_head flush_list;
601 struct list_head drop_list;
602 spinlock_t ino_lock;
603 u64 ino;
604
605
606
607
608 struct list_head state_list;
609 struct mutex state_lock;
610
611
612
613
614 struct pohmelfs_config *active_state;
615
616
617 wait_queue_head_t wait;
618
619
620
621
622 struct delayed_work dwork;
623 struct delayed_work drop_dwork;
624
625 struct super_block *sb;
626
627
628
629
630 char *hash_string;
631 char *cipher_string;
632
633 u8 *hash_key;
634 u8 *cipher_key;
635
636
637
638
639 unsigned int hash_strlen;
640 unsigned int cipher_strlen;
641 unsigned int hash_keysize;
642 unsigned int cipher_keysize;
643
644
645
646
647 int perform_crypto;
648
649
650
651
652 u64 total_size;
653 u64 avail_size;
654 atomic_long_t total_inodes;
655
656
657
658
659 u64 state_flags;
660
661
662
663
664 long flags;
665};
666
667static inline void netfs_trans_update(struct netfs_cmd *cmd,
668 struct netfs_trans *t, unsigned int size)
669{
670 unsigned int sz = ALIGN(size, t->psb->crypto_align_size);
671
672 t->iovec.iov_len += sizeof(struct netfs_cmd) + sz;
673 cmd->cpad = __cpu_to_be16(sz - size);
674}
675
676static inline struct pohmelfs_sb *POHMELFS_SB(struct super_block *sb)
677{
678 return sb->s_fs_info;
679}
680
681static inline struct pohmelfs_inode *POHMELFS_I(struct inode *inode)
682{
683 return container_of(inode, struct pohmelfs_inode, vfs_inode);
684}
685
686static inline u64 pohmelfs_new_ino(struct pohmelfs_sb *psb)
687{
688 u64 ino;
689
690 spin_lock(&psb->ino_lock);
691 ino = psb->ino++;
692 spin_unlock(&psb->ino_lock);
693
694 return ino;
695}
696
697static inline void pohmelfs_put_inode(struct pohmelfs_inode *pi)
698{
699 struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
700
701 spin_lock(&psb->ino_lock);
702 list_move_tail(&pi->inode_entry, &psb->drop_list);
703 pi->drop_count++;
704 spin_unlock(&psb->ino_lock);
705}
706
707struct pohmelfs_config {
708 struct list_head config_entry;
709
710 struct netfs_state state;
711};
712
713struct pohmelfs_config_group {
714
715
716
717 struct list_head group_entry;
718
719
720
721
722 unsigned int idx;
723
724
725
726 unsigned int num_entry;
727
728
729
730 char *hash_string;
731 char *cipher_string;
732
733
734
735
736 unsigned int hash_strlen;
737 unsigned int cipher_strlen;
738
739
740
741
742 unsigned int hash_keysize;
743 unsigned int cipher_keysize;
744 u8 *hash_key;
745 u8 *cipher_key;
746
747
748
749
750 struct list_head config_list;
751};
752
753int __init pohmelfs_config_init(void);
754void pohmelfs_config_exit(void);
755int pohmelfs_copy_config(struct pohmelfs_sb *psb);
756int pohmelfs_copy_crypto(struct pohmelfs_sb *psb);
757int pohmelfs_config_check(struct pohmelfs_config *config, int idx);
758int pohmelfs_state_init_one(struct pohmelfs_sb *psb, struct pohmelfs_config *conf);
759
760extern const struct file_operations pohmelfs_dir_fops;
761extern const struct inode_operations pohmelfs_dir_inode_ops;
762
763int pohmelfs_state_init(struct pohmelfs_sb *psb);
764void pohmelfs_state_exit(struct pohmelfs_sb *psb);
765void pohmelfs_state_flush_transactions(struct netfs_state *st);
766
767void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info);
768
769void pohmelfs_name_del(struct pohmelfs_inode *parent, struct pohmelfs_name *n);
770void pohmelfs_free_names(struct pohmelfs_inode *parent);
771struct pohmelfs_name *pohmelfs_search_hash(struct pohmelfs_inode *pi, u32 hash);
772
773void pohmelfs_inode_del_inode(struct pohmelfs_sb *psb, struct pohmelfs_inode *pi);
774
775struct pohmelfs_inode *pohmelfs_create_entry_local(struct pohmelfs_sb *psb,
776 struct pohmelfs_inode *parent, struct qstr *str, u64 start, int mode);
777
778int pohmelfs_write_create_inode(struct pohmelfs_inode *pi);
779
780int pohmelfs_write_inode_create(struct inode *inode, struct netfs_trans *trans);
781int pohmelfs_remove_child(struct pohmelfs_inode *parent, struct pohmelfs_name *n);
782
783struct pohmelfs_inode *pohmelfs_new_inode(struct pohmelfs_sb *psb,
784 struct pohmelfs_inode *parent, struct qstr *str,
785 struct netfs_inode_info *info, int link);
786
787int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr);
788int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr);
789
790int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags,
791 netfs_trans_complete_t complete, void *priv, u64 start);
792int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon,
793 unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start);
794
795void pohmelfs_check_states(struct pohmelfs_sb *psb);
796void pohmelfs_switch_active(struct pohmelfs_sb *psb);
797
798int pohmelfs_construct_path_string(struct pohmelfs_inode *pi, void *data, int len);
799int pohmelfs_path_length(struct pohmelfs_inode *pi);
800
801struct pohmelfs_crypto_completion {
802 struct completion complete;
803 int error;
804};
805
806int pohmelfs_trans_crypt(struct netfs_trans *t, struct pohmelfs_sb *psb);
807void pohmelfs_crypto_exit(struct pohmelfs_sb *psb);
808int pohmelfs_crypto_init(struct pohmelfs_sb *psb);
809
810int pohmelfs_crypto_engine_init(struct pohmelfs_crypto_engine *e, struct pohmelfs_sb *psb);
811void pohmelfs_crypto_engine_exit(struct pohmelfs_crypto_engine *e);
812
813int pohmelfs_crypto_process_input_data(struct pohmelfs_crypto_engine *e, u64 iv,
814 void *data, struct page *page, unsigned int size);
815int pohmelfs_crypto_process_input_page(struct pohmelfs_crypto_engine *e,
816 struct page *page, unsigned int size, u64 iv);
817
818static inline u64 pohmelfs_gen_iv(struct netfs_trans *t)
819{
820 u64 iv = t->gen;
821
822 iv <<= 32;
823 iv |= ((unsigned long)t) & 0xffffffff;
824
825 return iv;
826}
827
828int pohmelfs_data_lock(struct pohmelfs_inode *pi, u64 start, u32 size, int type);
829int pohmelfs_data_unlock(struct pohmelfs_inode *pi, u64 start, u32 size, int type);
830int pohmelfs_data_lock_response(struct netfs_state *st);
831
832static inline int pohmelfs_need_lock(struct pohmelfs_inode *pi, int type)
833{
834 if (test_bit(NETFS_INODE_OWNED, &pi->state)) {
835 if (type == pi->lock_type)
836 return 0;
837 if ((type == POHMELFS_READ_LOCK) && (pi->lock_type == POHMELFS_WRITE_LOCK))
838 return 0;
839 }
840
841 if (!test_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state))
842 return 0;
843
844 return 1;
845}
846
847int __init pohmelfs_mcache_init(void);
848void pohmelfs_mcache_exit(void);
849
850
851
852#ifdef CONFIG_POHMELFS_DEBUG
853#define dprintka(f, a...) printk(f, ##a)
854#define dprintk(f, a...) printk("%d: " f, task_pid_vnr(current), ##a)
855#else
856#define dprintka(f, a...) do {} while (0)
857#define dprintk(f, a...) do {} while (0)
858#endif
859
860static inline void netfs_trans_get(struct netfs_trans *t)
861{
862 atomic_inc(&t->refcnt);
863}
864
865static inline void netfs_trans_put(struct netfs_trans *t)
866{
867 if (atomic_dec_and_test(&t->refcnt)) {
868 dprintk("%s: t: %p, gen: %u, err: %d.\n",
869 __func__, t, t->gen, t->result);
870 if (t->complete)
871 t->complete(t->pages, t->page_num,
872 t->private, t->result);
873 netfs_trans_free(t);
874 }
875}
876
877struct pohmelfs_mcache {
878 struct rb_node mcache_entry;
879 struct completion complete;
880
881 atomic_t refcnt;
882
883 u64 gen;
884
885 void *data;
886 u64 start;
887 u32 size;
888 int err;
889
890 struct netfs_inode_info info;
891};
892
893struct pohmelfs_mcache *pohmelfs_mcache_alloc(struct pohmelfs_sb *psb, u64 start,
894 unsigned int size, void *data);
895void pohmelfs_mcache_free(struct pohmelfs_sb *psb, struct pohmelfs_mcache *m);
896struct pohmelfs_mcache *pohmelfs_mcache_search(struct pohmelfs_sb *psb, u64 gen);
897void pohmelfs_mcache_remove_locked(struct pohmelfs_sb *psb, struct pohmelfs_mcache *m);
898
899static inline void pohmelfs_mcache_get(struct pohmelfs_mcache *m)
900{
901 atomic_inc(&m->refcnt);
902}
903
904static inline void pohmelfs_mcache_put(struct pohmelfs_sb *psb,
905 struct pohmelfs_mcache *m)
906{
907 if (atomic_dec_and_test(&m->refcnt))
908 pohmelfs_mcache_free(psb, m);
909}
910
911
912
913#endif
914
915#endif
916