2 * Copyright (C) 2011+ Evgeniy Polyakov <zbr@ioremap.net>
8 #include <linux/backing-dev.h>
9 #include <linux/crypto.h>
11 #include <linux/kref.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/net.h>
15 #include <linux/pagemap.h>
16 #include <linux/pagevec.h>
17 #include <linux/printk.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/wait.h>
21 #include <linux/workqueue.h>
23 #include <crypto/sha.h>
25 #define dnet_bswap16(x) cpu_to_le16(x)
26 #define dnet_bswap32(x) cpu_to_le32(x)
27 #define dnet_bswap64(x) cpu_to_le64(x)
29 /* theese are needed for packet.h below to compile */
30 #define DNET_ID_SIZE SHA512_DIGEST_SIZE
31 #define DNET_CSUM_SIZE SHA512_DIGEST_SIZE
33 #define POHMELFS_INODE_COLUMN 3
36 * is not used in kernel, but we want to share the same header
37 * with userspace, so I put it here for compiler to shut up
39 int gettimeofday(struct timeval
*, struct timezone
*);
43 static inline struct timespec
pohmelfs_date(struct dnet_time
*tm
)
48 ts
.tv_nsec
= tm
->tnsec
;
55 struct dnet_attr attr
;
57 struct dnet_io_attr io
;
63 * Returns 1 when id1 > id2
67 static inline int dnet_id_cmp_str(const unsigned char *id1
, const unsigned char *id2
)
71 for (i
*=sizeof(unsigned long); i
<DNET_ID_SIZE
; ++i
) {
81 struct pohmelfs_state
;
83 struct pohmelfs_trans
;
85 struct pohmelfs_trans_cb
{
86 int (* init
)(struct pohmelfs_trans
*t
);
87 int (* complete
)(struct pohmelfs_trans
*t
, struct pohmelfs_state
*recv
);
88 int (* recv_reply
)(struct pohmelfs_trans
*t
, struct pohmelfs_state
*recv
);
89 void (* destroy
)(struct pohmelfs_trans
*t
);
92 struct pohmelfs_trans
{
93 struct list_head trans_entry
;
101 struct pohmelfs_state
*st
;
103 struct pohmelfs_cmd cmd
;
105 u64 header_size
, data_size
;
109 unsigned long long recv_offset
;
112 struct pohmelfs_write_ctl
*wctl
;
115 struct pohmelfs_trans_cb cb
;
118 struct pohmelfs_trans
*pohmelfs_trans_alloc(struct inode
*inode
);
119 struct pohmelfs_trans
*pohmelfs_trans_alloc_io_buf(struct inode
*inode
, int group
, int command
,
120 void *data
, u64 offset
, u64 size
, int aflags
, int ioflags
, int type
);
121 void pohmelfs_trans_put(struct pohmelfs_trans
*t
);
123 int pohmelfs_trans_insert(struct pohmelfs_trans
*t
);
124 struct pohmelfs_trans
*pohmelfs_trans_lookup(struct pohmelfs_state
*st
, struct dnet_cmd
*cmd
);
126 struct pohmelfs_state
{
127 struct pohmelfs_sb
*psb
;
128 struct list_head state_entry
;
130 struct sockaddr_storage sa
;
136 struct mutex trans_lock
;
137 struct list_head trans_list
;
138 struct list_head sent_trans_list
;
144 /* Waiting/polling machinery */
146 wait_queue_head_t
*whead
;
148 struct work_struct send_work
;
149 struct work_struct recv_work
;
151 /* is set when dnet_cmd is being read, otherwise attached data */
153 /* currently read command reply */
156 uint64_t bsize
; /* Block size */
157 uint64_t frsize
; /* Fragment size */
158 uint64_t blocks
; /* Filesystem size in frsize units */
159 uint64_t bfree
; /* # free blocks */
160 uint64_t bavail
; /* # free blocks for non-root */
163 struct pohmelfs_state
*pohmelfs_state_create(struct pohmelfs_sb
*psb
, struct sockaddr_storage
*sa
, int addrlen
,
164 int ask_route
, int group_id
);
165 struct pohmelfs_state
*pohmelfs_state_lookup(struct pohmelfs_sb
*psb
, struct dnet_raw_id
*id
, int group
);
166 int pohmelfs_grab_states(struct pohmelfs_sb
*psb
, struct pohmelfs_state
***stp
);
168 static inline void pohmelfs_state_get(struct pohmelfs_state
*st
)
170 kref_get(&st
->refcnt
);
173 void pohmelfs_state_put(struct pohmelfs_state
*st
);
174 void pohmelfs_state_kill(struct pohmelfs_state
*st
);
176 struct pohmelfs_state
*pohmelfs_addr_exist(struct pohmelfs_sb
*psb
, struct sockaddr_storage
*sa
, int addrlen
);
178 void pohmelfs_state_schedule(struct pohmelfs_state
*st
);
180 __attribute__ ((format (printf
, 2, 3))) void pohmelfs_print_addr(struct sockaddr_storage
*addr
, const char *fmt
, ...);
182 #define POHMELFS_INODE_INFO_REMOVED (1<<0)
184 struct pohmelfs_inode_info
{
185 struct dnet_raw_id id
;
191 unsigned int blocksize
;
192 unsigned int namelen
;
201 struct dnet_time ctime
;
202 struct dnet_time mtime
;
203 struct dnet_time atime
;
204 } __attribute__ ((packed
));
206 void pohmelfs_fill_inode_info(struct inode
*inode
, struct pohmelfs_inode_info
*info
);
207 void pohmelfs_fill_inode(struct inode
*inode
, struct pohmelfs_inode_info
*info
);
208 void pohmelfs_convert_inode_info(struct pohmelfs_inode_info
*info
);
210 struct pohmelfs_inode
{
211 struct inode vfs_inode
;
212 struct dnet_raw_id id
;
213 struct dnet_raw_id parent_id
;
226 int pohmelfs_send_dentry(struct pohmelfs_inode
*pi
, struct dnet_raw_id
*id
, const char *sname
, int len
, int sync
);
227 struct pohmelfs_inode
*pohmelfs_sb_inode_lookup(struct pohmelfs_sb
*psb
, struct dnet_raw_id
*id
);
229 struct pohmelfs_reconnect
{
230 struct list_head reconnect_entry
;
231 struct sockaddr_storage sa
;
236 int pohmelfs_state_add_reconnect(struct pohmelfs_state
*st
);
238 struct pohmelfs_path
{
243 int pohmelfs_http_compat_id(struct pohmelfs_inode
*pi
);
246 struct super_block
*sb
;
247 struct backing_dev_info bdi
;
249 struct pohmelfs_inode
*root
;
251 spinlock_t inode_lock
;
252 struct rb_root inode_root
;
255 struct pohmelfs_path
*path
;
259 struct rb_root route_root
;
260 struct list_head state_list
;
261 spinlock_t state_lock
;
263 long read_wait_timeout
;
264 long write_wait_timeout
;
267 struct delayed_work sync_work
;
279 struct crypto_hash
*hash
;
281 struct workqueue_struct
*wq
;
287 * number of copies to be successfully written to mark write as successful
288 * if not set, half of groups plus one must be successfully written, i.e. plain write quorum
290 int successful_write_count
;
292 struct mutex reconnect_lock
;
293 struct list_head reconnect_list
;
294 struct list_head kill_state_list
;
295 struct delayed_work reconnect_work
;
296 long reconnect_timeout
;
298 int keepalive_cnt
, keepalive_interval
, keepalive_idle
;
300 int readdir_allocation
;
305 static inline struct pohmelfs_sb
*pohmelfs_sb(struct super_block
*sb
)
307 return (struct pohmelfs_sb
*)sb
->s_fs_info
;
310 static inline struct pohmelfs_inode
*pohmelfs_inode(struct inode
*inode
)
312 return container_of(inode
, struct pohmelfs_inode
, vfs_inode
);
315 struct pohmelfs_wait
{
316 wait_queue_head_t wq
;
317 struct pohmelfs_inode
*pi
;
324 int pohmelfs_wait_init(struct pohmelfs_wait
*wait
, struct pohmelfs_inode
*pi
);
325 struct pohmelfs_wait
*pohmelfs_wait_alloc(struct pohmelfs_inode
*pi
);
326 void pohmelfs_wait_put(struct pohmelfs_wait
*wait
);
327 static inline void pohmelfs_wait_get(struct pohmelfs_wait
*wait
)
329 kref_get(&wait
->refcnt
);
332 struct pohmelfs_inode_info_binary_package
{
333 struct pohmelfs_inode_info info
;
335 struct pohmelfs_wait wait
;
338 struct pohmelfs_write_ctl
{
340 struct pohmelfs_inode_info
*info
;
343 atomic_t good_writes
;
346 struct pohmelfs_dentry_disk
{
347 struct dnet_raw_id id
;
356 struct pohmelfs_dentry
{
357 struct dnet_raw_id parent_id
;
358 struct pohmelfs_dentry_disk disk
;
361 extern struct kmem_cache
*pohmelfs_inode_cache
;
362 extern struct kmem_cache
*pohmelfs_trans_cache
;
363 extern struct kmem_cache
*pohmelfs_inode_info_cache
;
364 extern struct kmem_cache
*pohmelfs_route_cache
;
365 extern struct kmem_cache
*pohmelfs_wait_cache
;
366 extern struct kmem_cache
*pohmelfs_io_cache
;
367 extern struct kmem_cache
*pohmelfs_inode_info_binary_package_cache
;
368 extern struct kmem_cache
*pohmelfs_write_cache
;
369 extern struct kmem_cache
*pohmelfs_dentry_cache
;
371 struct inode
*pohmelfs_alloc_inode(struct super_block
*sb
);
372 void pohmelfs_destroy_inode(struct inode
*);
374 struct pohmelfs_inode
*pohmelfs_existing_inode(struct pohmelfs_sb
*psb
, struct pohmelfs_inode_info
*info
);
375 struct pohmelfs_inode
*pohmelfs_new_inode(struct pohmelfs_sb
*psb
, int mode
);
376 int pohmelfs_hash(struct pohmelfs_sb
*psb
, const void *data
, const size_t size
, struct dnet_raw_id
*id
);
378 char *pohmelfs_dump_id(const unsigned char *id
);
379 char *pohmelfs_dump_id_len_raw(const unsigned char *id
, unsigned int len
, char *dst
);
381 int pohmelfs_write_command(struct pohmelfs_inode
*pi
, struct pohmelfs_write_ctl
*ctl
, loff_t offset
, size_t len
);
382 void pohmelfs_write_ctl_release(struct kref
*kref
);
383 int pohmelfs_metadata_inode(struct pohmelfs_inode
*pi
, int sync
);
385 extern const struct file_operations pohmelfs_dir_fops
;
386 extern const struct inode_operations pohmelfs_dir_inode_operations
;
388 extern const struct file_operations pohmelfs_file_ops
;
389 extern const struct inode_operations pohmelfs_file_inode_operations
;
391 extern const struct inode_operations pohmelfs_symlink_inode_operations
;
392 extern const struct inode_operations pohmelfs_special_inode_operations
;
394 extern void *pohmelfs_scratch_buf
;
395 extern int pohmelfs_scratch_buf_size
;
398 * if this flag is set, pohmelfs_inode_info->data is owned by the caller,
399 * so sending path may use it on its own and free (using kfree) when it's done
401 * This logic does not work for shared buffers or
402 * when multiple transactions will be sent for single pohmelfs_inode_info
404 #define POHMELFS_IO_OWN (1<<0)
407 struct pohmelfs_inode
*pi
;
409 struct dnet_raw_id
*id
;
426 struct pohmelfs_write_ctl
*wctl
;
429 struct pohmelfs_trans_cb cb
;
432 int pohmelfs_send_io_group(struct pohmelfs_io
*pio
, int group_id
);
433 int pohmelfs_send_io(struct pohmelfs_io
*pio
);
434 int pohmelfs_send_buf_single(struct pohmelfs_io
*pio
, struct pohmelfs_state
*st
);
435 int pohmelfs_send_buf(struct pohmelfs_io
*pio
);
437 int pohmelfs_data_recv(struct pohmelfs_state
*st
, void *buf
, u64 size
, unsigned int flags
);
438 int pohmelfs_recv(struct pohmelfs_trans
*t
, struct pohmelfs_state
*recv
, void *data
, int size
);
440 struct pohmelfs_route
{
443 struct dnet_raw_id id
;
444 struct pohmelfs_state
*st
;
447 int pohmelfs_route_request(struct pohmelfs_state
*st
);
448 void pohmelfs_route_remove_all(struct pohmelfs_state
*st
);
450 struct pohmelfs_script_req
{
465 struct dnet_raw_id
*id
;
467 int (* complete
)(struct pohmelfs_trans
*t
, struct pohmelfs_state
*recv
);
472 int pohmelfs_send_script_request(struct pohmelfs_inode
*parent
, struct pohmelfs_script_req
*req
);
474 int pohmelfs_stat(struct pohmelfs_sb
*psb
, int sync
);
476 static inline int pohmelfs_need_resync(struct pohmelfs_inode
*pi
)
478 struct pohmelfs_sb
*psb
= pohmelfs_sb(pi
->vfs_inode
.i_sb
);
479 return get_seconds() > pi
->update
+ psb
->sync_timeout
;
482 #endif /* __POHMELFS_H */