2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
16 #include <linux/module.h>
17 #include <linux/backing-dev.h>
18 #include <linux/crypto.h>
20 #include <linux/jhash.h>
21 #include <linux/hash.h>
22 #include <linux/ktime.h>
24 #include <linux/mount.h>
25 #include <linux/pagemap.h>
26 #include <linux/pagevec.h>
27 #include <linux/parser.h>
28 #include <linux/swap.h>
29 #include <linux/slab.h>
30 #include <linux/statfs.h>
31 #include <linux/writeback.h>
32 #include <linux/quotaops.h>
36 #define POHMELFS_MAGIC_NUM 0x504f482e
38 static struct kmem_cache
*pohmelfs_inode_cache
;
41 * Removes inode from all trees, drops local name cache and removes all queued
42 * requests for object removal.
44 void pohmelfs_inode_del_inode(struct pohmelfs_sb
*psb
, struct pohmelfs_inode
*pi
)
46 mutex_lock(&pi
->offset_lock
);
47 pohmelfs_free_names(pi
);
48 mutex_unlock(&pi
->offset_lock
);
50 dprintk("%s: deleted stuff in ino: %llu.\n", __func__
, pi
->ino
);
54 * Sync inode to server.
55 * Returns zero in success and negative error value otherwise.
56 * It will gather path to root directory into structures containing
57 * creation mode, permissions and names, so that the whole path
58 * to given inode could be created using only single network command.
60 int pohmelfs_write_inode_create(struct inode
*inode
, struct netfs_trans
*trans
)
62 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
63 int err
= -ENOMEM
, size
;
64 struct netfs_cmd
*cmd
;
66 int cur_len
= netfs_trans_cur_len(trans
);
68 if (unlikely(cur_len
< 0))
71 cmd
= netfs_trans_current(trans
);
72 cur_len
-= sizeof(struct netfs_cmd
);
74 data
= (void *)(cmd
+ 1);
76 err
= pohmelfs_construct_path_string(pi
, data
, cur_len
);
82 cmd
->start
= i_size_read(inode
);
83 cmd
->cmd
= NETFS_CREATE
;
86 cmd
->ext
= inode
->i_mode
;
88 netfs_convert_cmd(cmd
);
90 netfs_trans_update(cmd
, trans
, size
);
95 printk("%s: completed ino: %llu, err: %d.\n", __func__
, pi
->ino
, err
);
99 static int pohmelfs_write_trans_complete(struct page
**pages
, unsigned int page_num
,
100 void *private, int err
)
104 dprintk("%s: pages: %lu-%lu, page_num: %u, err: %d.\n",
105 __func__
, pages
[0]->index
, pages
[page_num
-1]->index
,
108 for (i
= 0; i
< page_num
; i
++) {
109 struct page
*page
= pages
[i
];
114 end_page_writeback(page
);
118 set_page_dirty(page
);
122 page_cache_release(page
);
124 /* dprintk("%s: %3u/%u: page: %p.\n", __func__, i, page_num, page); */
129 static int pohmelfs_inode_has_dirty_pages(struct address_space
*mapping
, pgoff_t index
)
135 ret
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
136 (void **)&page
, index
, 1, PAGECACHE_TAG_DIRTY
);
141 static int pohmelfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
143 struct inode
*inode
= mapping
->host
;
144 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
145 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
146 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
151 pgoff_t end
; /* Inclusive */
155 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
156 wbc
->encountered_congestion
= 1;
160 if (wbc
->range_cyclic
) {
161 index
= mapping
->writeback_index
; /* Start from prev offset */
164 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
165 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
166 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
171 while (!done
&& (index
<= end
)) {
172 unsigned int i
= min(end
- index
, (pgoff_t
)psb
->trans_max_pages
);
174 struct netfs_trans
*trans
;
176 err
= pohmelfs_inode_has_dirty_pages(mapping
, index
);
180 err
= pohmelfs_path_length(pi
);
191 trans
= netfs_trans_alloc(psb
, path_len
, 0, i
);
196 trans
->complete
= &pohmelfs_write_trans_complete
;
198 trans
->page_num
= nr_pages
= find_get_pages_tag(mapping
, &index
,
199 PAGECACHE_TAG_DIRTY
, trans
->page_num
,
202 dprintk("%s: t: %p, nr_pages: %u, end: %lu, index: %lu, max: %u.\n",
203 __func__
, trans
, nr_pages
, end
, index
, trans
->page_num
);
208 err
= pohmelfs_write_inode_create(inode
, trans
);
215 for (i
= 0; i
< trans
->page_num
; i
++) {
216 struct page
*page
= trans
->pages
[i
];
220 if (unlikely(page
->mapping
!= mapping
))
223 if (!wbc
->range_cyclic
&& page
->index
> end
) {
228 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
229 wait_on_page_writeback(page
);
231 if (PageWriteback(page
) ||
232 !clear_page_dirty_for_io(page
)) {
233 dprintk("%s: not clear for io page: %p, writeback: %d.\n",
234 __func__
, page
, PageWriteback(page
));
238 set_page_writeback(page
);
240 trans
->attached_size
+= page_private(page
);
241 trans
->attached_pages
++;
243 dprintk("%s: %u/%u added trans: %p, gen: %u, page: %p, [High: %d], size: %lu, idx: %lu.\n",
244 __func__
, i
, trans
->page_num
, trans
, trans
->gen
, page
,
245 !!PageHighMem(page
), page_private(page
), page
->index
);
249 if (wbc
->nr_to_write
<= 0)
251 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
252 wbc
->encountered_congestion
= 1;
259 trans
->pages
[i
] = NULL
;
262 err
= netfs_trans_finish(trans
, psb
);
270 netfs_trans_reset(trans
);
271 netfs_trans_put(trans
);
275 if (!scanned
&& !done
) {
277 * We hit the last page and there is more work to be done: wrap
278 * back to the start of the file
285 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
286 mapping
->writeback_index
= index
;
292 * Inode writeback creation completion callback.
293 * Only invoked for just created inodes, which do not have pages attached,
294 * like dirs and empty files.
296 static int pohmelfs_write_inode_complete(struct page
**pages
, unsigned int page_num
,
297 void *private, int err
)
299 struct inode
*inode
= private;
300 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
304 mark_inode_dirty(inode
);
305 clear_bit(NETFS_INODE_REMOTE_SYNCED
, &pi
->state
);
307 set_bit(NETFS_INODE_REMOTE_SYNCED
, &pi
->state
);
310 pohmelfs_put_inode(pi
);
316 int pohmelfs_write_create_inode(struct pohmelfs_inode
*pi
)
318 struct netfs_trans
*t
;
319 struct inode
*inode
= &pi
->vfs_inode
;
320 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
323 if (test_bit(NETFS_INODE_REMOTE_SYNCED
, &pi
->state
))
326 dprintk("%s: started ino: %llu.\n", __func__
, pi
->ino
);
328 err
= pohmelfs_path_length(pi
);
332 t
= netfs_trans_alloc(psb
, err
+ 1, 0, 0);
337 t
->complete
= pohmelfs_write_inode_complete
;
338 t
->private = igrab(inode
);
344 err
= pohmelfs_write_inode_create(inode
, t
);
348 netfs_trans_finish(t
, POHMELFS_SB(inode
->i_sb
));
360 * Sync all not-yet-created children in given directory to the server.
362 static int pohmelfs_write_inode_create_children(struct inode
*inode
)
364 struct pohmelfs_inode
*parent
= POHMELFS_I(inode
);
365 struct super_block
*sb
= inode
->i_sb
;
366 struct pohmelfs_name
*n
;
368 while (!list_empty(&parent
->sync_create_list
)) {
370 mutex_lock(&parent
->offset_lock
);
371 if (!list_empty(&parent
->sync_create_list
)) {
372 n
= list_first_entry(&parent
->sync_create_list
,
373 struct pohmelfs_name
, sync_create_entry
);
374 list_del_init(&n
->sync_create_entry
);
376 mutex_unlock(&parent
->offset_lock
);
381 inode
= ilookup(sb
, n
->ino
);
383 dprintk("%s: parent: %llu, ino: %llu, inode: %p.\n",
384 __func__
, parent
->ino
, n
->ino
, inode
);
386 if (inode
&& (inode
->i_state
& I_DIRTY
)) {
387 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
388 pohmelfs_write_create_inode(pi
);
389 //pohmelfs_meta_command(pi, NETFS_INODE_INFO, 0, NULL, NULL, 0);
398 * Removes given child from given inode on server.
400 int pohmelfs_remove_child(struct pohmelfs_inode
*pi
, struct pohmelfs_name
*n
)
402 return pohmelfs_meta_command_data(pi
, pi
->ino
, NETFS_REMOVE
, NULL
, 0, NULL
, NULL
, 0);
406 * Writeback for given inode.
408 static int pohmelfs_write_inode(struct inode
*inode
, int sync
)
410 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
412 pohmelfs_write_create_inode(pi
);
413 pohmelfs_write_inode_create_children(inode
);
419 * It is not exported, sorry...
421 static inline wait_queue_head_t
*page_waitqueue(struct page
*page
)
423 const struct zone
*zone
= page_zone(page
);
425 return &zone
->wait_table
[hash_ptr(page
, zone
->wait_table_bits
)];
428 static int pohmelfs_wait_on_page_locked(struct page
*page
)
430 struct pohmelfs_sb
*psb
= POHMELFS_SB(page
->mapping
->host
->i_sb
);
431 long ret
= psb
->wait_on_page_timeout
;
432 DEFINE_WAIT_BIT(wait
, &page
->flags
, PG_locked
);
435 if (!PageLocked(page
))
439 prepare_to_wait(page_waitqueue(page
),
440 &wait
.wait
, TASK_INTERRUPTIBLE
);
442 dprintk("%s: page: %p, locked: %d, uptodate: %d, error: %d, flags: %lx.\n",
443 __func__
, page
, PageLocked(page
), PageUptodate(page
),
444 PageError(page
), page
->flags
);
446 if (!PageLocked(page
))
449 if (!signal_pending(current
)) {
450 ret
= schedule_timeout(ret
);
458 finish_wait(page_waitqueue(page
), &wait
.wait
);
465 SetPageUptodate(page
);
468 printk("%s: page: %p, uptodate: %d, locked: %d, err: %d.\n",
469 __func__
, page
, PageUptodate(page
), PageLocked(page
), err
);
474 static int pohmelfs_read_page_complete(struct page
**pages
, unsigned int page_num
,
475 void *private, int err
)
477 struct page
*page
= private;
479 if (PageChecked(page
))
483 dprintk("%s: page: %p, err: %d.\n", __func__
, page
, err
);
493 * Read a page from remote server.
494 * Function will wait until page is unlocked.
496 static int pohmelfs_readpage(struct file
*file
, struct page
*page
)
498 struct inode
*inode
= page
->mapping
->host
;
499 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
500 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
501 struct netfs_trans
*t
;
502 struct netfs_cmd
*cmd
;
507 err
= pohmelfs_data_lock(pi
, page
->index
<< PAGE_CACHE_SHIFT
,
508 PAGE_SIZE
, POHMELFS_READ_LOCK
);
512 isize
= i_size_read(inode
);
513 if (isize
<= page
->index
<< PAGE_CACHE_SHIFT
) {
514 SetPageUptodate(page
);
519 path_len
= pohmelfs_path_length(pi
);
525 t
= netfs_trans_alloc(psb
, path_len
, NETFS_TRANS_SINGLE_DST
, 0);
531 t
->complete
= pohmelfs_read_page_complete
;
534 cmd
= netfs_trans_current(t
);
535 data
= (void *)(cmd
+ 1);
537 err
= pohmelfs_construct_path_string(pi
, data
, path_len
);
544 cmd
->start
= page
->index
;
545 cmd
->start
<<= PAGE_CACHE_SHIFT
;
546 cmd
->size
= PAGE_CACHE_SIZE
+ path_len
;
547 cmd
->cmd
= NETFS_READ_PAGE
;
550 dprintk("%s: path: '%s', page: %p, ino: %llu, start: %llu, size: %lu.\n",
551 __func__
, (char *)data
, page
, pi
->ino
, cmd
->start
, PAGE_CACHE_SIZE
);
553 netfs_convert_cmd(cmd
);
554 netfs_trans_update(cmd
, t
, path_len
);
556 err
= netfs_trans_finish(t
, psb
);
560 return pohmelfs_wait_on_page_locked(page
);
567 if (PageLocked(page
))
570 printk("%s: page: %p, start: %lu, size: %lu, err: %d.\n",
571 __func__
, page
, page
->index
<< PAGE_CACHE_SHIFT
, PAGE_CACHE_SIZE
, err
);
577 * Write begin/end magic.
578 * Allocates a page and writes inode if it was not synced to server before.
580 static int pohmelfs_write_begin(struct file
*file
, struct address_space
*mapping
,
581 loff_t pos
, unsigned len
, unsigned flags
,
582 struct page
**pagep
, void **fsdata
)
584 struct inode
*inode
= mapping
->host
;
592 index
= pos
>> PAGE_CACHE_SHIFT
;
593 start
= pos
& (PAGE_CACHE_SIZE
- 1);
596 page
= grab_cache_page(mapping
, index
);
598 dprintk("%s: page: %p pos: %llu, len: %u, index: %lu, start: %u, end: %u, uptodate: %d.\n",
599 __func__
, page
, pos
, len
, index
, start
, end
, PageUptodate(page
));
606 while (!PageUptodate(page
)) {
607 if (start
&& test_bit(NETFS_INODE_REMOTE_SYNCED
, &POHMELFS_I(inode
)->state
)) {
608 err
= pohmelfs_readpage(file
, page
);
616 if (len
!= PAGE_CACHE_SIZE
) {
617 void *kaddr
= kmap_atomic(page
, KM_USER0
);
619 memset(kaddr
+ start
, 0, PAGE_CACHE_SIZE
- start
);
620 flush_dcache_page(page
);
621 kunmap_atomic(kaddr
, KM_USER0
);
623 SetPageUptodate(page
);
626 set_page_private(page
, end
);
633 page_cache_release(page
);
639 static int pohmelfs_write_end(struct file
*file
, struct address_space
*mapping
,
640 loff_t pos
, unsigned len
, unsigned copied
,
641 struct page
*page
, void *fsdata
)
643 struct inode
*inode
= mapping
->host
;
646 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
647 void *kaddr
= kmap_atomic(page
, KM_USER0
);
649 memset(kaddr
+ from
+ copied
, 0, len
- copied
);
650 flush_dcache_page(page
);
651 kunmap_atomic(kaddr
, KM_USER0
);
654 SetPageUptodate(page
);
655 set_page_dirty(page
);
657 dprintk("%s: page: %p [U: %d, D: %d, L: %d], pos: %llu, len: %u, copied: %u.\n",
659 PageUptodate(page
), PageDirty(page
), PageLocked(page
),
662 flush_dcache_page(page
);
665 page_cache_release(page
);
667 if (pos
+ copied
> inode
->i_size
) {
668 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
670 psb
->avail_size
-= pos
+ copied
- inode
->i_size
;
672 i_size_write(inode
, pos
+ copied
);
678 static int pohmelfs_readpages_trans_complete(struct page
**__pages
, unsigned int page_num
,
679 void *private, int err
)
681 struct pohmelfs_inode
*pi
= private;
683 struct page
**pages
, *page
= (struct page
*)__pages
;
684 loff_t index
= page
->index
;
686 pages
= kzalloc(sizeof(void *) * page_num
, GFP_NOIO
);
690 num
= find_get_pages_contig(pi
->vfs_inode
.i_mapping
, index
, page_num
, pages
);
696 for (i
=0; i
<num
; ++i
) {
700 printk("%s: %u/%u: page: %p, index: %lu, uptodate: %d, locked: %d, err: %d.\n",
701 __func__
, i
, num
, page
, page
->index
,
702 PageUptodate(page
), PageLocked(page
), err
);
704 if (!PageChecked(page
)) {
709 page_cache_release(page
);
710 page_cache_release(page
);
718 static int pohmelfs_send_readpages(struct pohmelfs_inode
*pi
, struct page
*first
, unsigned int num
)
720 struct netfs_trans
*t
;
721 struct netfs_cmd
*cmd
;
722 struct pohmelfs_sb
*psb
= POHMELFS_SB(pi
->vfs_inode
.i_sb
);
726 err
= pohmelfs_data_lock(pi
, first
->index
<< PAGE_CACHE_SHIFT
,
727 num
* PAGE_SIZE
, POHMELFS_READ_LOCK
);
731 path_len
= pohmelfs_path_length(pi
);
737 t
= netfs_trans_alloc(psb
, path_len
, NETFS_TRANS_SINGLE_DST
, 0);
743 cmd
= netfs_trans_current(t
);
744 data
= (void *)(cmd
+ 1);
746 t
->complete
= pohmelfs_readpages_trans_complete
;
749 t
->pages
= (struct page
**)first
;
751 err
= pohmelfs_construct_path_string(pi
, data
, path_len
);
757 cmd
->cmd
= NETFS_READ_PAGES
;
758 cmd
->start
= first
->index
;
759 cmd
->start
<<= PAGE_CACHE_SHIFT
;
760 cmd
->size
= (num
<< 8 | PAGE_CACHE_SHIFT
);
764 dprintk("%s: t: %p, gen: %u, path: '%s', path_len: %u, "
765 "start: %lu, num: %u.\n",
766 __func__
, t
, t
->gen
, (char *)data
, path_len
,
769 netfs_convert_cmd(cmd
);
770 netfs_trans_update(cmd
, t
, path_len
);
772 return netfs_trans_finish(t
, psb
);
777 pohmelfs_readpages_trans_complete((struct page
**)first
, num
, pi
, err
);
781 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
783 static int pohmelfs_readpages(struct file
*file
, struct address_space
*mapping
,
784 struct list_head
*pages
, unsigned nr_pages
)
786 unsigned int page_idx
, num
= 0;
787 struct page
*page
= NULL
, *first
= NULL
;
789 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
790 page
= list_to_page(pages
);
792 prefetchw(&page
->flags
);
793 list_del(&page
->lru
);
795 if (!add_to_page_cache_lru(page
, mapping
,
796 page
->index
, GFP_KERNEL
)) {
804 dprintk("%s: added to lru page: %p, page_index: %lu, first_index: %lu.\n",
805 __func__
, page
, page
->index
, first
->index
);
807 if (unlikely(first
->index
+ num
!= page
->index
) || (num
> 500)) {
808 pohmelfs_send_readpages(POHMELFS_I(mapping
->host
),
817 pohmelfs_send_readpages(POHMELFS_I(mapping
->host
), first
, num
);
820 * This will be sync read, so when last page is processed,
821 * all previous are alerady unlocked and ready to be used.
827 * Small addres space operations for POHMELFS.
829 const struct address_space_operations pohmelfs_aops
= {
830 .readpage
= pohmelfs_readpage
,
831 .readpages
= pohmelfs_readpages
,
832 .writepages
= pohmelfs_writepages
,
833 .write_begin
= pohmelfs_write_begin
,
834 .write_end
= pohmelfs_write_end
,
835 .set_page_dirty
= __set_page_dirty_nobuffers
,
839 * ->detroy_inode() callback. Deletes inode from the caches
840 * and frees private data.
842 static void pohmelfs_destroy_inode(struct inode
*inode
)
844 struct super_block
*sb
= inode
->i_sb
;
845 struct pohmelfs_sb
*psb
= POHMELFS_SB(sb
);
846 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
848 //pohmelfs_data_unlock(pi, 0, inode->i_size, POHMELFS_READ_LOCK);
850 pohmelfs_inode_del_inode(psb
, pi
);
852 dprintk("%s: pi: %p, inode: %p, ino: %llu.\n",
853 __func__
, pi
, &pi
->vfs_inode
, pi
->ino
);
854 kmem_cache_free(pohmelfs_inode_cache
, pi
);
855 atomic_long_dec(&psb
->total_inodes
);
859 * ->alloc_inode() callback. Allocates inode and initilizes private data.
861 static struct inode
*pohmelfs_alloc_inode(struct super_block
*sb
)
863 struct pohmelfs_inode
*pi
;
865 pi
= kmem_cache_alloc(pohmelfs_inode_cache
, GFP_NOIO
);
869 pi
->hash_root
= RB_ROOT
;
870 mutex_init(&pi
->offset_lock
);
872 INIT_LIST_HEAD(&pi
->sync_create_list
);
874 INIT_LIST_HEAD(&pi
->inode_entry
);
881 dprintk("%s: pi: %p, inode: %p.\n", __func__
, pi
, &pi
->vfs_inode
);
883 atomic_long_inc(&POHMELFS_SB(sb
)->total_inodes
);
885 return &pi
->vfs_inode
;
889 * We want fsync() to work on POHMELFS.
891 static int pohmelfs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
893 struct inode
*inode
= file
->f_mapping
->host
;
894 struct writeback_control wbc
= {
895 .sync_mode
= WB_SYNC_ALL
,
896 .nr_to_write
= 0, /* sys_fsync did this */
899 return sync_inode(inode
, &wbc
);
902 ssize_t
pohmelfs_write(struct file
*file
, const char __user
*buf
,
903 size_t len
, loff_t
*ppos
)
905 struct address_space
*mapping
= file
->f_mapping
;
906 struct inode
*inode
= mapping
->host
;
907 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
908 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= len
};
913 init_sync_kiocb(&kiocb
, file
);
917 dprintk("%s: len: %zu, pos: %llu.\n", __func__
, len
, pos
);
919 mutex_lock(&inode
->i_mutex
);
920 ret
= pohmelfs_data_lock(pi
, pos
, len
, POHMELFS_WRITE_LOCK
);
924 ret
= generic_file_aio_write_nolock(&kiocb
, &iov
, 1, pos
);
925 *ppos
= kiocb
.ki_pos
;
927 mutex_unlock(&inode
->i_mutex
);
930 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
933 err
= sync_page_range(inode
, mapping
, pos
, ret
);
942 mutex_unlock(&inode
->i_mutex
);
946 const static struct file_operations pohmelfs_file_ops
= {
947 .open
= generic_file_open
,
948 .fsync
= pohmelfs_fsync
,
950 .llseek
= generic_file_llseek
,
952 .read
= do_sync_read
,
953 .aio_read
= generic_file_aio_read
,
955 .mmap
= generic_file_mmap
,
957 .splice_read
= generic_file_splice_read
,
958 .splice_write
= generic_file_splice_write
,
960 .write
= pohmelfs_write
,
961 .aio_write
= generic_file_aio_write
,
964 const struct inode_operations pohmelfs_symlink_inode_operations
= {
965 .readlink
= generic_readlink
,
966 .follow_link
= page_follow_link_light
,
967 .put_link
= page_put_link
,
970 int pohmelfs_setattr_raw(struct inode
*inode
, struct iattr
*attr
)
974 err
= inode_change_ok(inode
, attr
);
976 dprintk("%s: ino: %llu, inode changes are not allowed.\n", __func__
, POHMELFS_I(inode
)->ino
);
980 if ((attr
->ia_valid
& ATTR_UID
&& attr
->ia_uid
!= inode
->i_uid
) ||
981 (attr
->ia_valid
& ATTR_GID
&& attr
->ia_gid
!= inode
->i_gid
)) {
982 err
= vfs_dq_transfer(inode
, attr
) ? -EDQUOT
: 0;
987 err
= inode_setattr(inode
, attr
);
989 dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__
, POHMELFS_I(inode
)->ino
);
993 dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n",
994 __func__
, POHMELFS_I(inode
)->ino
, inode
->i_mode
, attr
->ia_mode
,
995 inode
->i_uid
, attr
->ia_uid
, inode
->i_gid
, attr
->ia_gid
, inode
->i_size
, attr
->ia_size
);
1003 int pohmelfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1005 struct inode
*inode
= dentry
->d_inode
;
1006 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
1009 err
= pohmelfs_data_lock(pi
, 0, ~0, POHMELFS_WRITE_LOCK
);
1013 err
= security_inode_setattr(dentry
, attr
);
1017 err
= pohmelfs_setattr_raw(inode
, attr
);
1027 static int pohmelfs_send_xattr_req(struct pohmelfs_inode
*pi
, u64 id
, u64 start
,
1028 const char *name
, const void *value
, size_t attrsize
, int command
)
1030 struct pohmelfs_sb
*psb
= POHMELFS_SB(pi
->vfs_inode
.i_sb
);
1031 int err
, path_len
, namelen
= strlen(name
) + 1; /* 0-byte */
1032 struct netfs_trans
*t
;
1033 struct netfs_cmd
*cmd
;
1036 dprintk("%s: id: %llu, start: %llu, name: '%s', attrsize: %zu, cmd: %d.\n",
1037 __func__
, id
, start
, name
, attrsize
, command
);
1039 path_len
= pohmelfs_path_length(pi
);
1045 t
= netfs_trans_alloc(psb
, namelen
+ path_len
+ attrsize
, 0, 0);
1051 cmd
= netfs_trans_current(t
);
1054 path_len
= pohmelfs_construct_path_string(pi
, data
, path_len
);
1062 * 'name' is a NUL-terminated string already and
1063 * 'namelen' includes 0-byte.
1065 memcpy(data
, name
, namelen
);
1068 memcpy(data
, value
, attrsize
);
1073 cmd
->size
= attrsize
+ namelen
+ path_len
;
1074 cmd
->ext
= path_len
;
1078 netfs_convert_cmd(cmd
);
1079 netfs_trans_update(cmd
, t
, namelen
+ path_len
+ attrsize
);
1081 return netfs_trans_finish(t
, psb
);
1090 static int pohmelfs_setxattr(struct dentry
*dentry
, const char *name
,
1091 const void *value
, size_t attrsize
, int flags
)
1093 struct inode
*inode
= dentry
->d_inode
;
1094 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
1095 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
1097 if (!(psb
->state_flags
& POHMELFS_FLAGS_XATTR
))
1100 return pohmelfs_send_xattr_req(pi
, flags
, attrsize
, name
,
1101 value
, attrsize
, NETFS_XATTR_SET
);
1104 static ssize_t
pohmelfs_getxattr(struct dentry
*dentry
, const char *name
,
1105 void *value
, size_t attrsize
)
1107 struct inode
*inode
= dentry
->d_inode
;
1108 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
1109 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
1110 struct pohmelfs_mcache
*m
;
1112 long timeout
= psb
->mcache_timeout
;
1114 if (!(psb
->state_flags
& POHMELFS_FLAGS_XATTR
))
1117 m
= pohmelfs_mcache_alloc(psb
, 0, attrsize
, value
);
1121 dprintk("%s: ino: %llu, name: '%s', size: %zu.\n",
1122 __func__
, pi
->ino
, name
, attrsize
);
1124 err
= pohmelfs_send_xattr_req(pi
, m
->gen
, attrsize
, name
, value
, 0, NETFS_XATTR_GET
);
1129 err
= wait_for_completion_timeout(&m
->complete
, timeout
);
1136 * This loop is a bit ugly, since it waits until reference counter
1137 * hits 1 and then put object here. Main goal is to prevent race with
1138 * network thread, when it can start processing given request, i.e.
1139 * increase its reference counter but yet not complete it, while
1140 * we will exit from ->getxattr() with timeout, and although request
1141 * will not be freed (its reference counter was increased by network
1142 * thread), data pointer provided by user may be released, so we will
1143 * overwrite already freed area in network thread.
1145 * Now after timeout we remove request from the cache, so it can not be
1146 * found by network thread, and wait for its reference counter to hit 1,
1147 * i.e. if network thread already started to process this request, we wait
1148 * it to finish, and then free object locally. If reference counter is
1149 * already 1, i.e. request is not used by anyone else, we can free it without
1155 pohmelfs_mcache_remove_locked(psb
, m
);
1156 } while (atomic_read(&m
->refcnt
) != 1);
1158 pohmelfs_mcache_put(psb
, m
);
1160 dprintk("%s: ino: %llu, err: %d.\n", __func__
, pi
->ino
, err
);
1165 pohmelfs_mcache_put(psb
, m
);
1169 static int pohmelfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1171 struct inode
*inode
= dentry
->d_inode
;
1173 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
1176 err
= pohmelfs_data_lock(pi
, 0, ~0, POHMELFS_READ_LOCK
);
1179 dprintk("%s: ino: %llu, mode: %o, uid: %u, gid: %u, size: %llu.\n",
1180 __func__
, pi
->ino
, inode
->i_mode
, inode
->i_uid
,
1181 inode
->i_gid
, inode
->i_size
);
1184 generic_fillattr(inode
, stat
);
1188 const struct inode_operations pohmelfs_file_inode_operations
= {
1189 .setattr
= pohmelfs_setattr
,
1190 .getattr
= pohmelfs_getattr
,
1191 .setxattr
= pohmelfs_setxattr
,
1192 .getxattr
= pohmelfs_getxattr
,
1196 * Fill inode data: mode, size, operation callbacks and so on...
1198 void pohmelfs_fill_inode(struct inode
*inode
, struct netfs_inode_info
*info
)
1200 inode
->i_mode
= info
->mode
;
1201 inode
->i_nlink
= info
->nlink
;
1202 inode
->i_uid
= info
->uid
;
1203 inode
->i_gid
= info
->gid
;
1204 inode
->i_blocks
= info
->blocks
;
1205 inode
->i_rdev
= info
->rdev
;
1206 inode
->i_size
= info
->size
;
1207 inode
->i_version
= info
->version
;
1208 inode
->i_blkbits
= ffs(info
->blocksize
);
1210 dprintk("%s: inode: %p, num: %lu/%llu inode is regular: %d, dir: %d, link: %d, mode: %o, size: %llu.\n",
1211 __func__
, inode
, inode
->i_ino
, info
->ino
,
1212 S_ISREG(inode
->i_mode
), S_ISDIR(inode
->i_mode
),
1213 S_ISLNK(inode
->i_mode
), inode
->i_mode
, inode
->i_size
);
1215 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
1218 * i_mapping is a pointer to i_data during inode initialization.
1220 inode
->i_data
.a_ops
= &pohmelfs_aops
;
1222 if (S_ISREG(inode
->i_mode
)) {
1223 inode
->i_fop
= &pohmelfs_file_ops
;
1224 inode
->i_op
= &pohmelfs_file_inode_operations
;
1225 } else if (S_ISDIR(inode
->i_mode
)) {
1226 inode
->i_fop
= &pohmelfs_dir_fops
;
1227 inode
->i_op
= &pohmelfs_dir_inode_ops
;
1228 } else if (S_ISLNK(inode
->i_mode
)) {
1229 inode
->i_op
= &pohmelfs_symlink_inode_operations
;
1230 inode
->i_fop
= &pohmelfs_file_ops
;
1232 inode
->i_fop
= &generic_ro_fops
;
1236 static void pohmelfs_drop_inode(struct inode
*inode
)
1238 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
1239 struct pohmelfs_inode
*pi
= POHMELFS_I(inode
);
1241 spin_lock(&psb
->ino_lock
);
1242 list_del_init(&pi
->inode_entry
);
1243 spin_unlock(&psb
->ino_lock
);
1245 generic_drop_inode(inode
);
1248 static struct pohmelfs_inode
*pohmelfs_get_inode_from_list(struct pohmelfs_sb
*psb
,
1249 struct list_head
*head
, unsigned int *count
)
1251 struct pohmelfs_inode
*pi
= NULL
;
1253 spin_lock(&psb
->ino_lock
);
1254 if (!list_empty(head
)) {
1255 pi
= list_entry(head
->next
, struct pohmelfs_inode
,
1257 list_del_init(&pi
->inode_entry
);
1258 *count
= pi
->drop_count
;
1261 spin_unlock(&psb
->ino_lock
);
1266 static void pohmelfs_flush_transactions(struct pohmelfs_sb
*psb
)
1268 struct pohmelfs_config
*c
;
1270 mutex_lock(&psb
->state_lock
);
1271 list_for_each_entry(c
, &psb
->state_list
, config_entry
) {
1272 pohmelfs_state_flush_transactions(&c
->state
);
1274 mutex_unlock(&psb
->state_lock
);
1278 * ->put_super() callback. Invoked before superblock is destroyed,
1279 * so it has to clean all private data.
1281 static void pohmelfs_put_super(struct super_block
*sb
)
1283 struct pohmelfs_sb
*psb
= POHMELFS_SB(sb
);
1284 struct pohmelfs_inode
*pi
;
1286 unsigned int in_drop_list
= 0;
1287 struct inode
*inode
, *tmp
;
1289 dprintk("%s.\n", __func__
);
1292 * Kill pending transactions, which could affect inodes in-flight.
1294 pohmelfs_flush_transactions(psb
);
1296 while ((pi
= pohmelfs_get_inode_from_list(psb
, &psb
->drop_list
, &count
))) {
1297 inode
= &pi
->vfs_inode
;
1299 dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1300 __func__
, pi
->ino
, pi
, inode
, count
);
1302 if (atomic_read(&inode
->i_count
) != count
) {
1303 printk("%s: ino: %llu, pi: %p, inode: %p, count: %u, i_count: %d.\n",
1304 __func__
, pi
->ino
, pi
, inode
, count
,
1305 atomic_read(&inode
->i_count
));
1306 count
= atomic_read(&inode
->i_count
);
1311 iput(&pi
->vfs_inode
);
1314 list_for_each_entry_safe(inode
, tmp
, &sb
->s_inodes
, i_sb_list
) {
1315 pi
= POHMELFS_I(inode
);
1317 dprintk("%s: ino: %llu, pi: %p, inode: %p, i_count: %u.\n",
1318 __func__
, pi
->ino
, pi
, inode
, atomic_read(&inode
->i_count
));
1321 * These are special inodes, they were created during
1322 * directory reading or lookup, and were not bound to dentry,
1323 * so they live here with reference counter being 1 and prevent
1324 * umount from succeed since it believes that they are busy.
1326 count
= atomic_read(&inode
->i_count
);
1328 list_del_init(&inode
->i_sb_list
);
1330 iput(&pi
->vfs_inode
);
1334 psb
->trans_scan_timeout
= psb
->drop_scan_timeout
= 0;
1335 cancel_rearming_delayed_work(&psb
->dwork
);
1336 cancel_rearming_delayed_work(&psb
->drop_dwork
);
1337 flush_scheduled_work();
1339 dprintk("%s: stopped workqueues.\n", __func__
);
1341 pohmelfs_crypto_exit(psb
);
1342 pohmelfs_state_exit(psb
);
1345 sb
->s_fs_info
= NULL
;
1347 pohmelfs_ftrans_exit();
1350 static int pohmelfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1352 struct super_block
*sb
= dentry
->d_sb
;
1353 struct pohmelfs_sb
*psb
= POHMELFS_SB(sb
);
1356 * There are no filesystem size limits yet.
1358 memset(buf
, 0, sizeof(struct kstatfs
));
1360 buf
->f_type
= POHMELFS_MAGIC_NUM
; /* 'POH.' */
1361 buf
->f_bsize
= sb
->s_blocksize
;
1362 buf
->f_files
= psb
->ino
;
1363 buf
->f_namelen
= 255;
1364 buf
->f_files
= atomic_long_read(&psb
->total_inodes
);
1365 buf
->f_bfree
= buf
->f_bavail
= psb
->avail_size
>> PAGE_SHIFT
;
1366 buf
->f_blocks
= psb
->total_size
>> PAGE_SHIFT
;
1368 dprintk("%s: total: %llu, avail: %llu, inodes: %llu, bsize: %lu.\n",
1369 __func__
, psb
->total_size
, psb
->avail_size
, buf
->f_files
, sb
->s_blocksize
);
1374 static int pohmelfs_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
1376 struct pohmelfs_sb
*psb
= POHMELFS_SB(vfs
->mnt_sb
);
1378 seq_printf(seq
, ",idx=%u", psb
->idx
);
1379 seq_printf(seq
, ",trans_scan_timeout=%u", jiffies_to_msecs(psb
->trans_scan_timeout
));
1380 seq_printf(seq
, ",drop_scan_timeout=%u", jiffies_to_msecs(psb
->drop_scan_timeout
));
1381 seq_printf(seq
, ",wait_on_page_timeout=%u", jiffies_to_msecs(psb
->wait_on_page_timeout
));
1382 seq_printf(seq
, ",trans_retries=%u", psb
->trans_retries
);
1383 seq_printf(seq
, ",crypto_thread_num=%u", psb
->crypto_thread_num
);
1384 seq_printf(seq
, ",trans_max_pages=%u", psb
->trans_max_pages
);
1385 seq_printf(seq
, ",mcache_timeout=%u", jiffies_to_msecs(psb
->mcache_timeout
));
1386 if (psb
->crypto_fail_unsupported
)
1387 seq_printf(seq
, ",crypto_fail_unsupported");
1394 pohmelfs_opt_crypto_thread_num
,
1395 pohmelfs_opt_trans_max_pages
,
1396 pohmelfs_opt_crypto_fail_unsupported
,
1398 /* Remountable options */
1399 pohmelfs_opt_trans_scan_timeout
,
1400 pohmelfs_opt_drop_scan_timeout
,
1401 pohmelfs_opt_wait_on_page_timeout
,
1402 pohmelfs_opt_trans_retries
,
1403 pohmelfs_opt_mcache_timeout
,
1406 static struct match_token pohmelfs_tokens
[] = {
1407 {pohmelfs_opt_idx
, "idx=%u"},
1408 {pohmelfs_opt_crypto_thread_num
, "crypto_thread_num=%u"},
1409 {pohmelfs_opt_trans_max_pages
, "trans_max_pages=%u"},
1410 {pohmelfs_opt_crypto_fail_unsupported
, "crypto_fail_unsupported"},
1411 {pohmelfs_opt_trans_scan_timeout
, "trans_scan_timeout=%u"},
1412 {pohmelfs_opt_drop_scan_timeout
, "drop_scan_timeout=%u"},
1413 {pohmelfs_opt_wait_on_page_timeout
, "wait_on_page_timeout=%u"},
1414 {pohmelfs_opt_trans_retries
, "trans_retries=%u"},
1415 {pohmelfs_opt_mcache_timeout
, "mcache_timeout=%u"},
1418 static int pohmelfs_parse_options(char *options
, struct pohmelfs_sb
*psb
, int remount
)
1421 substring_t args
[MAX_OPT_ARGS
];
1427 while ((p
= strsep(&options
, ",")) != NULL
) {
1432 token
= match_token(p
, pohmelfs_tokens
, args
);
1434 err
= match_int(&args
[0], &option
);
1438 if (remount
&& token
<= pohmelfs_opt_crypto_fail_unsupported
)
1442 case pohmelfs_opt_idx
:
1445 case pohmelfs_opt_trans_scan_timeout
:
1446 psb
->trans_scan_timeout
= msecs_to_jiffies(option
);
1448 case pohmelfs_opt_drop_scan_timeout
:
1449 psb
->drop_scan_timeout
= msecs_to_jiffies(option
);
1451 case pohmelfs_opt_wait_on_page_timeout
:
1452 psb
->wait_on_page_timeout
= msecs_to_jiffies(option
);
1454 case pohmelfs_opt_mcache_timeout
:
1455 psb
->mcache_timeout
= msecs_to_jiffies(option
);
1457 case pohmelfs_opt_trans_retries
:
1458 psb
->trans_retries
= option
;
1460 case pohmelfs_opt_crypto_thread_num
:
1461 psb
->crypto_thread_num
= option
;
1463 case pohmelfs_opt_trans_max_pages
:
1464 psb
->trans_max_pages
= option
;
1466 case pohmelfs_opt_crypto_fail_unsupported
:
1467 psb
->crypto_fail_unsupported
= 1;
1477 static int pohmelfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1480 struct pohmelfs_sb
*psb
= POHMELFS_SB(sb
);
1481 unsigned long old_sb_flags
= sb
->s_flags
;
1483 err
= pohmelfs_parse_options(data
, psb
, 1);
1485 goto err_out_restore
;
1487 if (!(*flags
& MS_RDONLY
))
1488 sb
->s_flags
&= ~MS_RDONLY
;
1492 sb
->s_flags
= old_sb_flags
;
1496 static void pohmelfs_flush_inode(struct pohmelfs_inode
*pi
, unsigned int count
)
1498 struct inode
*inode
= &pi
->vfs_inode
;
1500 dprintk("%s: %p: ino: %llu, owned: %d.\n",
1501 __func__
, inode
, pi
->ino
, test_bit(NETFS_INODE_OWNED
, &pi
->state
));
1503 mutex_lock(&inode
->i_mutex
);
1504 if (test_and_clear_bit(NETFS_INODE_OWNED
, &pi
->state
)) {
1505 filemap_fdatawrite(inode
->i_mapping
);
1506 inode
->i_sb
->s_op
->write_inode(inode
, 0);
1509 truncate_inode_pages(inode
->i_mapping
, 0);
1511 pohmelfs_data_unlock(pi
, 0, ~0, POHMELFS_WRITE_LOCK
);
1512 mutex_unlock(&inode
->i_mutex
);
1515 static void pohmelfs_put_inode_count(struct pohmelfs_inode
*pi
, unsigned int count
)
1517 dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1518 __func__
, pi
->ino
, pi
, &pi
->vfs_inode
, count
);
1520 if (test_and_clear_bit(NETFS_INODE_NEED_FLUSH
, &pi
->state
))
1521 pohmelfs_flush_inode(pi
, count
);
1524 iput(&pi
->vfs_inode
);
1527 static void pohmelfs_drop_scan(struct work_struct
*work
)
1529 struct pohmelfs_sb
*psb
=
1530 container_of(work
, struct pohmelfs_sb
, drop_dwork
.work
);
1531 struct pohmelfs_inode
*pi
;
1532 unsigned int count
= 0;
1534 while ((pi
= pohmelfs_get_inode_from_list(psb
, &psb
->drop_list
, &count
))) {
1535 pohmelfs_put_inode_count(pi
, count
);
1537 pohmelfs_check_states(psb
);
1539 if (psb
->drop_scan_timeout
)
1540 schedule_delayed_work(&psb
->drop_dwork
, psb
->drop_scan_timeout
);
1544 * Run through all transactions starting from the oldest,
1545 * drop transaction from current state and try to send it
1546 * to all remote nodes, which are currently installed.
1548 static void pohmelfs_trans_scan_state(struct netfs_state
*st
)
1550 struct rb_node
*rb_node
;
1551 struct netfs_trans_dst
*dst
;
1552 struct pohmelfs_sb
*psb
= st
->psb
;
1553 unsigned int timeout
= psb
->trans_scan_timeout
;
1554 struct netfs_trans
*t
;
1557 mutex_lock(&st
->trans_lock
);
1558 for (rb_node
= rb_first(&st
->trans_root
); rb_node
; ) {
1559 dst
= rb_entry(rb_node
, struct netfs_trans_dst
, state_entry
);
1562 if (timeout
&& time_after(dst
->send_time
+ timeout
, jiffies
)
1563 && dst
->retries
== 0)
1566 dprintk("%s: t: %p, gen: %u, st: %p, retries: %u, max: %u.\n",
1567 __func__
, t
, t
->gen
, st
, dst
->retries
, psb
->trans_retries
);
1570 rb_node
= rb_next(rb_node
);
1573 if (timeout
&& (++dst
->retries
< psb
->trans_retries
)) {
1574 err
= netfs_trans_resend(t
, psb
);
1577 if (err
|| (t
->flags
& NETFS_TRANS_SINGLE_DST
)) {
1578 if (netfs_trans_remove_nolock(dst
, st
))
1579 netfs_trans_drop_dst_nostate(dst
);
1585 mutex_unlock(&st
->trans_lock
);
1589 * Walk through all installed network states and resend all
1590 * transactions, which are old enough.
1592 static void pohmelfs_trans_scan(struct work_struct
*work
)
1594 struct pohmelfs_sb
*psb
=
1595 container_of(work
, struct pohmelfs_sb
, dwork
.work
);
1596 struct netfs_state
*st
;
1597 struct pohmelfs_config
*c
;
1599 mutex_lock(&psb
->state_lock
);
1600 list_for_each_entry(c
, &psb
->state_list
, config_entry
) {
1603 pohmelfs_trans_scan_state(st
);
1605 mutex_unlock(&psb
->state_lock
);
1608 * If no timeout specified then system is in the middle of umount process,
1609 * so no need to reschedule scanning process again.
1611 if (psb
->trans_scan_timeout
)
1612 schedule_delayed_work(&psb
->dwork
, psb
->trans_scan_timeout
);
1615 int pohmelfs_meta_command_data(struct pohmelfs_inode
*pi
, u64 id
, unsigned int cmd_op
, char *addon
,
1616 unsigned int flags
, netfs_trans_complete_t complete
, void *priv
, u64 start
)
1618 struct inode
*inode
= &pi
->vfs_inode
;
1619 struct pohmelfs_sb
*psb
= POHMELFS_SB(inode
->i_sb
);
1621 struct netfs_trans
*t
;
1622 int path_len
, addon_len
= 0;
1624 struct netfs_inode_info
*info
;
1625 struct netfs_cmd
*cmd
;
1627 dprintk("%s: ino: %llu, cmd: %u, addon: %p.\n", __func__
, pi
->ino
, cmd_op
, addon
);
1629 path_len
= pohmelfs_path_length(pi
);
1636 addon_len
= strlen(addon
) + 1; /* 0-byte */
1639 if (cmd_op
== NETFS_INODE_INFO
)
1640 sz
+= sizeof(struct netfs_inode_info
);
1642 t
= netfs_trans_alloc(psb
, sz
+ path_len
, flags
, 0);
1647 t
->complete
= complete
;
1650 cmd
= netfs_trans_current(t
);
1651 data
= (void *)(cmd
+ 1);
1653 if (cmd_op
== NETFS_INODE_INFO
) {
1654 info
= (struct netfs_inode_info
*)(cmd
+ 1);
1655 data
= (void *)(info
+ 1);
1658 * We are under i_mutex, can read and change whatever we want...
1660 info
->mode
= inode
->i_mode
;
1661 info
->nlink
= inode
->i_nlink
;
1662 info
->uid
= inode
->i_uid
;
1663 info
->gid
= inode
->i_gid
;
1664 info
->blocks
= inode
->i_blocks
;
1665 info
->rdev
= inode
->i_rdev
;
1666 info
->size
= inode
->i_size
;
1667 info
->version
= inode
->i_version
;
1669 netfs_convert_inode_info(info
);
1672 path_len
= pohmelfs_construct_path_string(pi
, data
, path_len
);
1676 dprintk("%s: path_len: %d.\n", __func__
, path_len
);
1679 path_len
--; /* Do not place null-byte before the addon */
1680 path_len
+= sprintf(data
+ path_len
, "/%s", addon
) + 1; /* 0 - byte */
1686 cmd
->ext
= path_len
;
1691 netfs_convert_cmd(cmd
);
1692 netfs_trans_update(cmd
, t
, sz
);
1695 * Note, that it is possible to leak error here: transaction callback will not
1696 * be invoked for allocation path failure.
1698 return netfs_trans_finish(t
, psb
);
1701 netfs_trans_free(t
);
1704 complete(NULL
, 0, priv
, err
);
1708 int pohmelfs_meta_command(struct pohmelfs_inode
*pi
, unsigned int cmd_op
, unsigned int flags
,
1709 netfs_trans_complete_t complete
, void *priv
, u64 start
)
1711 return pohmelfs_meta_command_data(pi
, pi
->ino
, cmd_op
, NULL
, flags
, complete
, priv
, start
);
1715 * Send request and wait for POHMELFS root capabilities response,
1716 * which will update server's informaion about size of the export,
1717 * permissions, number of objects, available size and so on.
1719 static int pohmelfs_root_handshake(struct pohmelfs_sb
*psb
)
1721 struct netfs_trans
*t
;
1722 struct netfs_cmd
*cmd
;
1725 t
= netfs_trans_alloc(psb
, 0, 0, 0);
1729 cmd
= netfs_trans_current(t
);
1731 cmd
->cmd
= NETFS_CAPABILITIES
;
1732 cmd
->id
= POHMELFS_ROOT_CAPABILITIES
;
1738 netfs_convert_cmd(cmd
);
1739 netfs_trans_update(cmd
, t
, 0);
1741 err
= netfs_trans_finish(t
, psb
);
1746 err
= wait_event_interruptible_timeout(psb
->wait
,
1748 psb
->wait_on_page_timeout
);
1764 static const struct super_operations pohmelfs_sb_ops
= {
1765 .alloc_inode
= pohmelfs_alloc_inode
,
1766 .destroy_inode
= pohmelfs_destroy_inode
,
1767 .drop_inode
= pohmelfs_drop_inode
,
1768 .write_inode
= pohmelfs_write_inode
,
1769 .put_super
= pohmelfs_put_super
,
1770 .remount_fs
= pohmelfs_remount
,
1771 .statfs
= pohmelfs_statfs
,
1772 .show_options
= pohmelfs_show_options
,
1776 * Allocate private superblock and create root dir.
1778 static int pohmelfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1780 struct pohmelfs_sb
*psb
;
1783 struct pohmelfs_inode
*npi
;
1786 pohmelfs_ftrans_init();
1788 psb
= kzalloc(sizeof(struct pohmelfs_sb
), GFP_KERNEL
);
1792 sb
->s_fs_info
= psb
;
1793 sb
->s_op
= &pohmelfs_sb_ops
;
1794 sb
->s_magic
= POHMELFS_MAGIC_NUM
;
1795 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1796 sb
->s_blocksize
= PAGE_SIZE
;
1802 psb
->active_state
= NULL
;
1803 psb
->trans_retries
= 5;
1804 psb
->trans_data_size
= PAGE_SIZE
;
1805 psb
->drop_scan_timeout
= msecs_to_jiffies(1000);
1806 psb
->trans_scan_timeout
= msecs_to_jiffies(5000);
1807 psb
->wait_on_page_timeout
= msecs_to_jiffies(5000);
1808 init_waitqueue_head(&psb
->wait
);
1810 spin_lock_init(&psb
->ino_lock
);
1812 INIT_LIST_HEAD(&psb
->drop_list
);
1814 mutex_init(&psb
->mcache_lock
);
1815 psb
->mcache_root
= RB_ROOT
;
1816 psb
->mcache_timeout
= msecs_to_jiffies(5000);
1817 atomic_long_set(&psb
->mcache_gen
, 0);
1819 psb
->trans_max_pages
= 100;
1821 psb
->crypto_align_size
= 16;
1822 psb
->crypto_attached_size
= 0;
1823 psb
->hash_strlen
= 0;
1824 psb
->cipher_strlen
= 0;
1825 psb
->perform_crypto
= 0;
1826 psb
->crypto_thread_num
= 2;
1827 psb
->crypto_fail_unsupported
= 0;
1828 mutex_init(&psb
->crypto_thread_lock
);
1829 INIT_LIST_HEAD(&psb
->crypto_ready_list
);
1830 INIT_LIST_HEAD(&psb
->crypto_active_list
);
1832 atomic_set(&psb
->trans_gen
, 1);
1833 atomic_set(&psb
->total_inodes
, 0);
1835 mutex_init(&psb
->state_lock
);
1836 INIT_LIST_HEAD(&psb
->state_list
);
1838 err
= pohmelfs_parse_options((char *) data
, psb
, 0);
1840 goto err_out_free_sb
;
1842 err
= pohmelfs_copy_crypto(psb
);
1844 goto err_out_free_sb
;
1846 err
= pohmelfs_state_init(psb
);
1848 goto err_out_free_strings
;
1850 err
= pohmelfs_crypto_init(psb
);
1852 goto err_out_state_exit
;
1854 err
= pohmelfs_root_handshake(psb
);
1856 goto err_out_crypto_exit
;
1859 str
.hash
= jhash("/", 1, 0);
1862 npi
= pohmelfs_create_entry_local(psb
, NULL
, &str
, 0, 0755|S_IFDIR
);
1865 goto err_out_crypto_exit
;
1867 set_bit(NETFS_INODE_REMOTE_SYNCED
, &npi
->state
);
1868 clear_bit(NETFS_INODE_OWNED
, &npi
->state
);
1870 root
= &npi
->vfs_inode
;
1872 sb
->s_root
= d_alloc_root(root
);
1874 goto err_out_put_root
;
1876 INIT_DELAYED_WORK(&psb
->drop_dwork
, pohmelfs_drop_scan
);
1877 schedule_delayed_work(&psb
->drop_dwork
, psb
->drop_scan_timeout
);
1879 INIT_DELAYED_WORK(&psb
->dwork
, pohmelfs_trans_scan
);
1880 schedule_delayed_work(&psb
->dwork
, psb
->trans_scan_timeout
);
1886 err_out_crypto_exit
:
1887 pohmelfs_crypto_exit(psb
);
1889 pohmelfs_state_exit(psb
);
1890 err_out_free_strings
:
1891 kfree(psb
->cipher_string
);
1892 kfree(psb
->hash_string
);
1897 dprintk("%s: err: %d.\n", __func__
, err
);
1902 * Some VFS magic here...
1904 static int pohmelfs_get_sb(struct file_system_type
*fs_type
,
1905 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1907 return get_sb_nodev(fs_type
, flags
, data
, pohmelfs_fill_super
,
1911 static struct file_system_type pohmel_fs_type
= {
1912 .owner
= THIS_MODULE
,
1914 .get_sb
= pohmelfs_get_sb
,
1915 .kill_sb
= kill_anon_super
,
1919 * Cache and module initializations and freeing routings.
1921 static void pohmelfs_init_once(void *data
)
1923 struct pohmelfs_inode
*pi
= data
;
1925 inode_init_once(&pi
->vfs_inode
);
1928 static int __init
pohmelfs_init_inodecache(void)
1930 pohmelfs_inode_cache
= kmem_cache_create("pohmelfs_inode_cache",
1931 sizeof(struct pohmelfs_inode
),
1932 0, (SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
),
1933 pohmelfs_init_once
);
1934 if (!pohmelfs_inode_cache
)
1940 static void pohmelfs_destroy_inodecache(void)
1942 kmem_cache_destroy(pohmelfs_inode_cache
);
1945 static int __init
init_pohmel_fs(void)
1949 err
= pohmelfs_config_init();
1953 err
= pohmelfs_init_inodecache();
1955 goto err_out_config_exit
;
1957 err
= pohmelfs_mcache_init();
1959 goto err_out_destroy
;
1961 err
= netfs_trans_init();
1963 goto err_out_mcache_exit
;
1965 err
= register_filesystem(&pohmel_fs_type
);
1973 err_out_mcache_exit
:
1974 pohmelfs_mcache_exit();
1976 pohmelfs_destroy_inodecache();
1977 err_out_config_exit
:
1978 pohmelfs_config_exit();
1983 static void __exit
exit_pohmel_fs(void)
1985 unregister_filesystem(&pohmel_fs_type
);
1986 pohmelfs_destroy_inodecache();
1987 pohmelfs_mcache_exit();
1988 pohmelfs_config_exit();
1992 module_init(init_pohmel_fs
);
1993 module_exit(exit_pohmel_fs
);
1995 MODULE_LICENSE("GPL");
1996 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1997 MODULE_DESCRIPTION("Pohmel filesystem");