Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / staging / pohmelfs / inode.c
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 #include <linux/module.h>
17 #include <linux/backing-dev.h>
18 #include <linux/crypto.h>
19 #include <linux/fs.h>
20 #include <linux/jhash.h>
21 #include <linux/hash.h>
22 #include <linux/ktime.h>
23 #include <linux/mm.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/prefetch.h>
33
34 #include "netfs.h"
35
36 #define POHMELFS_MAGIC_NUM      0x504f482e
37
38 static struct kmem_cache *pohmelfs_inode_cache;
39 static atomic_t psb_bdi_num = ATOMIC_INIT(0);
40
41 /*
42  * Removes inode from all trees, drops local name cache and removes all queued
43  * requests for object removal.
44  */
45 void pohmelfs_inode_del_inode(struct pohmelfs_sb *psb, struct pohmelfs_inode *pi)
46 {
47         mutex_lock(&pi->offset_lock);
48         pohmelfs_free_names(pi);
49         mutex_unlock(&pi->offset_lock);
50
51         dprintk("%s: deleted stuff in ino: %llu.\n", __func__, pi->ino);
52 }
53
54 /*
55  * Sync inode to server.
56  * Returns zero in success and negative error value otherwise.
57  * It will gather path to root directory into structures containing
58  * creation mode, permissions and names, so that the whole path
59  * to given inode could be created using only single network command.
60  */
61 int pohmelfs_write_inode_create(struct inode *inode, struct netfs_trans *trans)
62 {
63         struct pohmelfs_inode *pi = POHMELFS_I(inode);
64         int err = -ENOMEM, size;
65         struct netfs_cmd *cmd;
66         void *data;
67         int cur_len = netfs_trans_cur_len(trans);
68
69         if (unlikely(cur_len < 0))
70                 return -ETOOSMALL;
71
72         cmd = netfs_trans_current(trans);
73         cur_len -= sizeof(struct netfs_cmd);
74
75         data = (void *)(cmd + 1);
76
77         err = pohmelfs_construct_path_string(pi, data, cur_len);
78         if (err < 0)
79                 goto err_out_exit;
80
81         size = err;
82
83         cmd->start = i_size_read(inode);
84         cmd->cmd = NETFS_CREATE;
85         cmd->size = size;
86         cmd->id = pi->ino;
87         cmd->ext = inode->i_mode;
88
89         netfs_convert_cmd(cmd);
90
91         netfs_trans_update(cmd, trans, size);
92
93         return 0;
94
95 err_out_exit:
96         printk("%s: completed ino: %llu, err: %d.\n", __func__, pi->ino, err);
97         return err;
98 }
99
100 static int pohmelfs_write_trans_complete(struct page **pages, unsigned int page_num,
101                 void *private, int err)
102 {
103         unsigned i;
104
105         dprintk("%s: pages: %lu-%lu, page_num: %u, err: %d.\n",
106                         __func__, pages[0]->index, pages[page_num-1]->index,
107                         page_num, err);
108
109         for (i = 0; i < page_num; i++) {
110                 struct page *page = pages[i];
111
112                 if (!page)
113                         continue;
114
115                 end_page_writeback(page);
116
117                 if (err < 0) {
118                         SetPageError(page);
119                         set_page_dirty(page);
120                 }
121
122                 unlock_page(page);
123                 page_cache_release(page);
124
125                 /* dprintk("%s: %3u/%u: page: %p.\n", __func__, i, page_num, page); */
126         }
127         return err;
128 }
129
130 static int pohmelfs_inode_has_dirty_pages(struct address_space *mapping, pgoff_t index)
131 {
132         int ret;
133         struct page *page;
134
135         rcu_read_lock();
136         ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
137                                 (void **)&page, index, 1, PAGECACHE_TAG_DIRTY);
138         rcu_read_unlock();
139         return ret;
140 }
141
142 static int pohmelfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
143 {
144         struct inode *inode = mapping->host;
145         struct pohmelfs_inode *pi = POHMELFS_I(inode);
146         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
147         int err = 0;
148         int done = 0;
149         int nr_pages;
150         pgoff_t index;
151         pgoff_t end;            /* Inclusive */
152         int scanned = 0;
153         int range_whole = 0;
154
155         if (wbc->range_cyclic) {
156                 index = mapping->writeback_index; /* Start from prev offset */
157                 end = -1;
158         } else {
159                 index = wbc->range_start >> PAGE_CACHE_SHIFT;
160                 end = wbc->range_end >> PAGE_CACHE_SHIFT;
161                 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
162                         range_whole = 1;
163                 scanned = 1;
164         }
165 retry:
166         while (!done && (index <= end)) {
167                 unsigned int i = min(end - index, (pgoff_t)psb->trans_max_pages);
168                 int path_len;
169                 struct netfs_trans *trans;
170
171                 err = pohmelfs_inode_has_dirty_pages(mapping, index);
172                 if (!err)
173                         break;
174
175                 err = pohmelfs_path_length(pi);
176                 if (err < 0)
177                         break;
178
179                 path_len = err;
180
181                 if (path_len <= 2) {
182                         err = -ENOENT;
183                         break;
184                 }
185
186                 trans = netfs_trans_alloc(psb, path_len, 0, i);
187                 if (!trans) {
188                         err = -ENOMEM;
189                         break;
190                 }
191                 trans->complete = &pohmelfs_write_trans_complete;
192
193                 trans->page_num = nr_pages = find_get_pages_tag(mapping, &index,
194                                 PAGECACHE_TAG_DIRTY, trans->page_num,
195                                 trans->pages);
196
197                 dprintk("%s: t: %p, nr_pages: %u, end: %lu, index: %lu, max: %u.\n",
198                                 __func__, trans, nr_pages, end, index, trans->page_num);
199
200                 if (!nr_pages)
201                         goto err_out_reset;
202
203                 err = pohmelfs_write_inode_create(inode, trans);
204                 if (err)
205                         goto err_out_reset;
206
207                 err = 0;
208                 scanned = 1;
209
210                 for (i = 0; i < trans->page_num; i++) {
211                         struct page *page = trans->pages[i];
212
213                         lock_page(page);
214
215                         if (unlikely(page->mapping != mapping))
216                                 goto out_continue;
217
218                         if (!wbc->range_cyclic && page->index > end) {
219                                 done = 1;
220                                 goto out_continue;
221                         }
222
223                         if (wbc->sync_mode != WB_SYNC_NONE)
224                                 wait_on_page_writeback(page);
225
226                         if (PageWriteback(page) ||
227                             !clear_page_dirty_for_io(page)) {
228                                 dprintk("%s: not clear for io page: %p, writeback: %d.\n",
229                                                 __func__, page, PageWriteback(page));
230                                 goto out_continue;
231                         }
232
233                         set_page_writeback(page);
234
235                         trans->attached_size += page_private(page);
236                         trans->attached_pages++;
237 #if 0
238                         dprintk("%s: %u/%u added trans: %p, gen: %u, page: %p, [High: %d], size: %lu, idx: %lu.\n",
239                                         __func__, i, trans->page_num, trans, trans->gen, page,
240                                         !!PageHighMem(page), page_private(page), page->index);
241 #endif
242                         wbc->nr_to_write--;
243
244                         if (wbc->nr_to_write <= 0)
245                                 done = 1;
246
247                         continue;
248 out_continue:
249                         unlock_page(page);
250                         trans->pages[i] = NULL;
251                 }
252
253                 err = netfs_trans_finish(trans, psb);
254                 if (err)
255                         break;
256
257                 continue;
258
259 err_out_reset:
260                 trans->result = err;
261                 netfs_trans_reset(trans);
262                 netfs_trans_put(trans);
263                 break;
264         }
265
266         if (!scanned && !done) {
267                 /*
268                  * We hit the last page and there is more work to be done: wrap
269                  * back to the start of the file
270                  */
271                 scanned = 1;
272                 index = 0;
273                 goto retry;
274         }
275
276         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
277                 mapping->writeback_index = index;
278
279         return err;
280 }
281
282 /*
283  * Inode writeback creation completion callback.
284  * Only invoked for just created inodes, which do not have pages attached,
285  * like dirs and empty files.
286  */
287 static int pohmelfs_write_inode_complete(struct page **pages, unsigned int page_num,
288                 void *private, int err)
289 {
290         struct inode *inode = private;
291         struct pohmelfs_inode *pi = POHMELFS_I(inode);
292
293         if (inode) {
294                 if (err) {
295                         mark_inode_dirty(inode);
296                         clear_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state);
297                 } else {
298                         set_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state);
299                 }
300
301                 pohmelfs_put_inode(pi);
302         }
303
304         return err;
305 }
306
307 int pohmelfs_write_create_inode(struct pohmelfs_inode *pi)
308 {
309         struct netfs_trans *t;
310         struct inode *inode = &pi->vfs_inode;
311         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
312         int err;
313
314         if (test_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state))
315                 return 0;
316
317         dprintk("%s: started ino: %llu.\n", __func__, pi->ino);
318
319         err = pohmelfs_path_length(pi);
320         if (err < 0)
321                 goto err_out_exit;
322
323         t = netfs_trans_alloc(psb, err + 1, 0, 0);
324         if (!t) {
325                 err = -ENOMEM;
326                 goto err_out_exit;
327         }
328         t->complete = pohmelfs_write_inode_complete;
329         t->private = igrab(inode);
330         if (!t->private) {
331                 err = -ENOENT;
332                 goto err_out_put;
333         }
334
335         err = pohmelfs_write_inode_create(inode, t);
336         if (err)
337                 goto err_out_put;
338
339         netfs_trans_finish(t, POHMELFS_SB(inode->i_sb));
340
341         return 0;
342
343 err_out_put:
344         t->result = err;
345         netfs_trans_put(t);
346 err_out_exit:
347         return err;
348 }
349
350 /*
351  * Sync all not-yet-created children in given directory to the server.
352  */
353 static int pohmelfs_write_inode_create_children(struct inode *inode)
354 {
355         struct pohmelfs_inode *parent = POHMELFS_I(inode);
356         struct super_block *sb = inode->i_sb;
357         struct pohmelfs_name *n;
358
359         while (!list_empty(&parent->sync_create_list)) {
360                 n = NULL;
361                 mutex_lock(&parent->offset_lock);
362                 if (!list_empty(&parent->sync_create_list)) {
363                         n = list_first_entry(&parent->sync_create_list,
364                                 struct pohmelfs_name, sync_create_entry);
365                         list_del_init(&n->sync_create_entry);
366                 }
367                 mutex_unlock(&parent->offset_lock);
368
369                 if (!n)
370                         break;
371
372                 inode = ilookup(sb, n->ino);
373
374                 dprintk("%s: parent: %llu, ino: %llu, inode: %p.\n",
375                                 __func__, parent->ino, n->ino, inode);
376
377                 if (inode && (inode->i_state & I_DIRTY)) {
378                         struct pohmelfs_inode *pi = POHMELFS_I(inode);
379                         pohmelfs_write_create_inode(pi);
380                         /* pohmelfs_meta_command(pi, NETFS_INODE_INFO, 0, NULL, NULL, 0); */
381                         iput(inode);
382                 }
383         }
384
385         return 0;
386 }
387
388 /*
389  * Removes given child from given inode on server.
390  */
391 int pohmelfs_remove_child(struct pohmelfs_inode *pi, struct pohmelfs_name *n)
392 {
393         return pohmelfs_meta_command_data(pi, pi->ino, NETFS_REMOVE, NULL, 0, NULL, NULL, 0);
394 }
395
396 /*
397  * Writeback for given inode.
398  */
399 static int pohmelfs_write_inode(struct inode *inode,
400                                 struct writeback_control *wbc)
401 {
402         struct pohmelfs_inode *pi = POHMELFS_I(inode);
403
404         pohmelfs_write_create_inode(pi);
405         pohmelfs_write_inode_create_children(inode);
406
407         return 0;
408 }
409
410 /*
411  * It is not exported, sorry...
412  */
413 static inline wait_queue_head_t *page_waitqueue(struct page *page)
414 {
415         const struct zone *zone = page_zone(page);
416
417         return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
418 }
419
420 static int pohmelfs_wait_on_page_locked(struct page *page)
421 {
422         struct pohmelfs_sb *psb = POHMELFS_SB(page->mapping->host->i_sb);
423         long ret = psb->wait_on_page_timeout;
424         DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
425         int err = 0;
426
427         if (!PageLocked(page))
428                 return 0;
429
430         for (;;) {
431                 prepare_to_wait(page_waitqueue(page),
432                                 &wait.wait, TASK_INTERRUPTIBLE);
433
434                 dprintk("%s: page: %p, locked: %d, uptodate: %d, error: %d, flags: %lx.\n",
435                                 __func__, page, PageLocked(page), PageUptodate(page),
436                                 PageError(page), page->flags);
437
438                 if (!PageLocked(page))
439                         break;
440
441                 if (!signal_pending(current)) {
442                         ret = schedule_timeout(ret);
443                         if (!ret)
444                                 break;
445                         continue;
446                 }
447                 ret = -ERESTARTSYS;
448                 break;
449         }
450         finish_wait(page_waitqueue(page), &wait.wait);
451
452         if (!ret)
453                 err = -ETIMEDOUT;
454
455
456         if (!err)
457                 SetPageUptodate(page);
458
459         if (err)
460                 printk("%s: page: %p, uptodate: %d, locked: %d, err: %d.\n",
461                         __func__, page, PageUptodate(page), PageLocked(page), err);
462
463         return err;
464 }
465
466 static int pohmelfs_read_page_complete(struct page **pages, unsigned int page_num,
467                 void *private, int err)
468 {
469         struct page *page = private;
470
471         if (PageChecked(page))
472                 return err;
473
474         if (err < 0) {
475                 dprintk("%s: page: %p, err: %d.\n", __func__, page, err);
476                 SetPageError(page);
477         }
478
479         unlock_page(page);
480
481         return err;
482 }
483
484 /*
485  * Read a page from remote server.
486  * Function will wait until page is unlocked.
487  */
488 static int pohmelfs_readpage(struct file *file, struct page *page)
489 {
490         struct inode *inode = page->mapping->host;
491         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
492         struct pohmelfs_inode *pi = POHMELFS_I(inode);
493         struct netfs_trans *t;
494         struct netfs_cmd *cmd;
495         int err, path_len;
496         void *data;
497         u64 isize;
498
499         err = pohmelfs_data_lock(pi, page->index << PAGE_CACHE_SHIFT,
500                         PAGE_SIZE, POHMELFS_READ_LOCK);
501         if (err)
502                 goto err_out_exit;
503
504         isize = i_size_read(inode);
505         if (isize <= page->index << PAGE_CACHE_SHIFT) {
506                 SetPageUptodate(page);
507                 unlock_page(page);
508                 return 0;
509         }
510
511         path_len = pohmelfs_path_length(pi);
512         if (path_len < 0) {
513                 err = path_len;
514                 goto err_out_exit;
515         }
516
517         t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0);
518         if (!t) {
519                 err = -ENOMEM;
520                 goto err_out_exit;
521         }
522
523         t->complete = pohmelfs_read_page_complete;
524         t->private = page;
525
526         cmd = netfs_trans_current(t);
527         data = (void *)(cmd + 1);
528
529         err = pohmelfs_construct_path_string(pi, data, path_len);
530         if (err < 0)
531                 goto err_out_free;
532
533         path_len = err;
534
535         cmd->id = pi->ino;
536         cmd->start = page->index;
537         cmd->start <<= PAGE_CACHE_SHIFT;
538         cmd->size = PAGE_CACHE_SIZE + path_len;
539         cmd->cmd = NETFS_READ_PAGE;
540         cmd->ext = path_len;
541
542         dprintk("%s: path: '%s', page: %p, ino: %llu, start: %llu, size: %lu.\n",
543                         __func__, (char *)data, page, pi->ino, cmd->start, PAGE_CACHE_SIZE);
544
545         netfs_convert_cmd(cmd);
546         netfs_trans_update(cmd, t, path_len);
547
548         err = netfs_trans_finish(t, psb);
549         if (err)
550                 goto err_out_return;
551
552         return pohmelfs_wait_on_page_locked(page);
553
554 err_out_free:
555         t->result = err;
556         netfs_trans_put(t);
557 err_out_exit:
558         SetPageError(page);
559         if (PageLocked(page))
560                 unlock_page(page);
561 err_out_return:
562         printk("%s: page: %p, start: %lu, size: %lu, err: %d.\n",
563                 __func__, page, page->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE, err);
564
565         return err;
566 }
567
568 /*
569  * Write begin/end magic.
570  * Allocates a page and writes inode if it was not synced to server before.
571  */
572 static int pohmelfs_write_begin(struct file *file, struct address_space *mapping,
573                 loff_t pos, unsigned len, unsigned flags,
574                 struct page **pagep, void **fsdata)
575 {
576         struct inode *inode = mapping->host;
577         struct page *page;
578         pgoff_t index;
579         unsigned start, end;
580         int err;
581
582         *pagep = NULL;
583
584         index = pos >> PAGE_CACHE_SHIFT;
585         start = pos & (PAGE_CACHE_SIZE - 1);
586         end = start + len;
587
588         page = grab_cache_page(mapping, index);
589 #if 0
590         dprintk("%s: page: %p pos: %llu, len: %u, index: %lu, start: %u, end: %u, uptodate: %d.\n",
591                         __func__, page, pos, len, index, start, end, PageUptodate(page));
592 #endif
593         if (!page) {
594                 err = -ENOMEM;
595                 goto err_out_exit;
596         }
597
598         while (!PageUptodate(page)) {
599                 if (start && test_bit(NETFS_INODE_REMOTE_SYNCED, &POHMELFS_I(inode)->state)) {
600                         err = pohmelfs_readpage(file, page);
601                         if (err)
602                                 goto err_out_exit;
603
604                         lock_page(page);
605                         continue;
606                 }
607
608                 if (len != PAGE_CACHE_SIZE) {
609                         void *kaddr = kmap_atomic(page, KM_USER0);
610
611                         memset(kaddr + start, 0, PAGE_CACHE_SIZE - start);
612                         flush_dcache_page(page);
613                         kunmap_atomic(kaddr, KM_USER0);
614                 }
615                 SetPageUptodate(page);
616         }
617
618         set_page_private(page, end);
619
620         *pagep = page;
621
622         return 0;
623
624 err_out_exit:
625         page_cache_release(page);
626         *pagep = NULL;
627
628         return err;
629 }
630
631 static int pohmelfs_write_end(struct file *file, struct address_space *mapping,
632                         loff_t pos, unsigned len, unsigned copied,
633                         struct page *page, void *fsdata)
634 {
635         struct inode *inode = mapping->host;
636
637         if (copied != len) {
638                 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
639                 void *kaddr = kmap_atomic(page, KM_USER0);
640
641                 memset(kaddr + from + copied, 0, len - copied);
642                 flush_dcache_page(page);
643                 kunmap_atomic(kaddr, KM_USER0);
644         }
645
646         SetPageUptodate(page);
647         set_page_dirty(page);
648 #if 0
649         dprintk("%s: page: %p [U: %d, D: %d, L: %d], pos: %llu, len: %u, copied: %u.\n",
650                         __func__, page,
651                         PageUptodate(page), PageDirty(page), PageLocked(page),
652                         pos, len, copied);
653 #endif
654         flush_dcache_page(page);
655
656         unlock_page(page);
657         page_cache_release(page);
658
659         if (pos + copied > inode->i_size) {
660                 struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
661
662                 psb->avail_size -= pos + copied - inode->i_size;
663
664                 i_size_write(inode, pos + copied);
665         }
666
667         return copied;
668 }
669
670 static int pohmelfs_readpages_trans_complete(struct page **__pages, unsigned int page_num,
671                 void *private, int err)
672 {
673         struct pohmelfs_inode *pi = private;
674         unsigned int i, num;
675         struct page **pages, *page = (struct page *)__pages;
676         loff_t index = page->index;
677
678         pages = kzalloc(sizeof(void *) * page_num, GFP_NOIO);
679         if (!pages)
680                 return -ENOMEM;
681
682         num = find_get_pages_contig(pi->vfs_inode.i_mapping, index, page_num, pages);
683         if (num <= 0) {
684                 err = num;
685                 goto err_out_free;
686         }
687
688         for (i = 0; i < num; ++i) {
689                 page = pages[i];
690
691                 if (err)
692                         printk("%s: %u/%u: page: %p, index: %lu, uptodate: %d, locked: %d, err: %d.\n",
693                                 __func__, i, num, page, page->index,
694                                 PageUptodate(page), PageLocked(page), err);
695
696                 if (!PageChecked(page)) {
697                         if (err < 0)
698                                 SetPageError(page);
699                         unlock_page(page);
700                 }
701                 page_cache_release(page);
702                 page_cache_release(page);
703         }
704
705 err_out_free:
706         kfree(pages);
707         return err;
708 }
709
710 static int pohmelfs_send_readpages(struct pohmelfs_inode *pi, struct page *first, unsigned int num)
711 {
712         struct netfs_trans *t;
713         struct netfs_cmd *cmd;
714         struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
715         int err, path_len;
716         void *data;
717
718         err = pohmelfs_data_lock(pi, first->index << PAGE_CACHE_SHIFT,
719                         num * PAGE_SIZE, POHMELFS_READ_LOCK);
720         if (err)
721                 goto err_out_exit;
722
723         path_len = pohmelfs_path_length(pi);
724         if (path_len < 0) {
725                 err = path_len;
726                 goto err_out_exit;
727         }
728
729         t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0);
730         if (!t) {
731                 err = -ENOMEM;
732                 goto err_out_exit;
733         }
734
735         cmd = netfs_trans_current(t);
736         data = (void *)(cmd + 1);
737
738         t->complete = pohmelfs_readpages_trans_complete;
739         t->private = pi;
740         t->page_num = num;
741         t->pages = (struct page **)first;
742
743         err = pohmelfs_construct_path_string(pi, data, path_len);
744         if (err < 0)
745                 goto err_out_put;
746
747         path_len = err;
748
749         cmd->cmd = NETFS_READ_PAGES;
750         cmd->start = first->index;
751         cmd->start <<= PAGE_CACHE_SHIFT;
752         cmd->size = (num << 8 | PAGE_CACHE_SHIFT);
753         cmd->id = pi->ino;
754         cmd->ext = path_len;
755
756         dprintk("%s: t: %p, gen: %u, path: '%s', path_len: %u, "
757                         "start: %lu, num: %u.\n",
758                         __func__, t, t->gen, (char *)data, path_len,
759                         first->index, num);
760
761         netfs_convert_cmd(cmd);
762         netfs_trans_update(cmd, t, path_len);
763
764         return netfs_trans_finish(t, psb);
765
766 err_out_put:
767         netfs_trans_free(t);
768 err_out_exit:
769         pohmelfs_readpages_trans_complete((struct page **)first, num, pi, err);
770         return err;
771 }
772
773 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
774
775 static int pohmelfs_readpages(struct file *file, struct address_space *mapping,
776                         struct list_head *pages, unsigned nr_pages)
777 {
778         unsigned int page_idx, num = 0;
779         struct page *page = NULL, *first = NULL;
780
781         for (page_idx = 0; page_idx < nr_pages; page_idx++) {
782                 page = list_to_page(pages);
783
784                 prefetchw(&page->flags);
785                 list_del(&page->lru);
786
787                 if (!add_to_page_cache_lru(page, mapping,
788                                         page->index, GFP_KERNEL)) {
789
790                         if (!num) {
791                                 num = 1;
792                                 first = page;
793                                 continue;
794                         }
795
796                         dprintk("%s: added to lru page: %p, page_index: %lu, first_index: %lu.\n",
797                                         __func__, page, page->index, first->index);
798
799                         if (unlikely(first->index + num != page->index) || (num > 500)) {
800                                 pohmelfs_send_readpages(POHMELFS_I(mapping->host),
801                                                 first, num);
802                                 first = page;
803                                 num = 0;
804                         }
805
806                         num++;
807                 }
808         }
809         pohmelfs_send_readpages(POHMELFS_I(mapping->host), first, num);
810
811         /*
812          * This will be sync read, so when last page is processed,
813          * all previous are alerady unlocked and ready to be used.
814          */
815         return 0;
816 }
817
818 /*
819  * Small address space operations for POHMELFS.
820  */
821 const struct address_space_operations pohmelfs_aops = {
822         .readpage               = pohmelfs_readpage,
823         .readpages              = pohmelfs_readpages,
824         .writepages             = pohmelfs_writepages,
825         .write_begin            = pohmelfs_write_begin,
826         .write_end              = pohmelfs_write_end,
827         .set_page_dirty         = __set_page_dirty_nobuffers,
828 };
829
830 static void pohmelfs_i_callback(struct rcu_head *head)
831 {
832         struct inode *inode = container_of(head, struct inode, i_rcu);
833         INIT_LIST_HEAD(&inode->i_dentry);
834         kmem_cache_free(pohmelfs_inode_cache, POHMELFS_I(inode));
835 }
836
837 /*
838  * ->destroy_inode() callback. Deletes inode from the caches
839  *  and frees private data.
840  */
841 static void pohmelfs_destroy_inode(struct inode *inode)
842 {
843         struct super_block *sb = inode->i_sb;
844         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
845         struct pohmelfs_inode *pi = POHMELFS_I(inode);
846
847         /* pohmelfs_data_unlock(pi, 0, inode->i_size, POHMELFS_READ_LOCK); */
848
849         pohmelfs_inode_del_inode(psb, pi);
850
851         dprintk("%s: pi: %p, inode: %p, ino: %llu.\n",
852                 __func__, pi, &pi->vfs_inode, pi->ino);
853         atomic_long_dec(&psb->total_inodes);
854         call_rcu(&inode->i_rcu, pohmelfs_i_callback);
855 }
856
857 /*
858  * ->alloc_inode() callback. Allocates inode and initializes private data.
859  */
860 static struct inode *pohmelfs_alloc_inode(struct super_block *sb)
861 {
862         struct pohmelfs_inode *pi;
863
864         pi = kmem_cache_alloc(pohmelfs_inode_cache, GFP_NOIO);
865         if (!pi)
866                 return NULL;
867
868         pi->hash_root = RB_ROOT;
869         mutex_init(&pi->offset_lock);
870
871         INIT_LIST_HEAD(&pi->sync_create_list);
872
873         INIT_LIST_HEAD(&pi->inode_entry);
874
875         pi->lock_type = 0;
876         pi->state = 0;
877         pi->total_len = 0;
878         pi->drop_count = 0;
879
880         dprintk("%s: pi: %p, inode: %p.\n", __func__, pi, &pi->vfs_inode);
881
882         atomic_long_inc(&POHMELFS_SB(sb)->total_inodes);
883
884         return &pi->vfs_inode;
885 }
886
887 /*
888  * We want fsync() to work on POHMELFS.
889  */
890 static int pohmelfs_fsync(struct file *file, int datasync)
891 {
892         struct inode *inode = file->f_mapping->host;
893
894         return sync_inode_metadata(inode, 1);
895 }
896
897 ssize_t pohmelfs_write(struct file *file, const char __user *buf,
898                 size_t len, loff_t *ppos)
899 {
900         struct address_space *mapping = file->f_mapping;
901         struct inode *inode = mapping->host;
902         struct pohmelfs_inode *pi = POHMELFS_I(inode);
903         struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
904         struct kiocb kiocb;
905         ssize_t ret;
906         loff_t pos = *ppos;
907
908         init_sync_kiocb(&kiocb, file);
909         kiocb.ki_pos = pos;
910         kiocb.ki_left = len;
911
912         dprintk("%s: len: %zu, pos: %llu.\n", __func__, len, pos);
913
914         mutex_lock(&inode->i_mutex);
915         ret = pohmelfs_data_lock(pi, pos, len, POHMELFS_WRITE_LOCK);
916         if (ret)
917                 goto err_out_unlock;
918
919         ret = __generic_file_aio_write(&kiocb, &iov, 1, &kiocb.ki_pos);
920         *ppos = kiocb.ki_pos;
921
922         mutex_unlock(&inode->i_mutex);
923         WARN_ON(ret < 0);
924
925         if (ret > 0) {
926                 ssize_t err;
927
928                 err = generic_write_sync(file, pos, ret);
929                 if (err < 0)
930                         ret = err;
931                 WARN_ON(ret < 0);
932         }
933
934         return ret;
935
936 err_out_unlock:
937         mutex_unlock(&inode->i_mutex);
938         return ret;
939 }
940
941 static const struct file_operations pohmelfs_file_ops = {
942         .open           = generic_file_open,
943         .fsync          = pohmelfs_fsync,
944
945         .llseek         = generic_file_llseek,
946
947         .read           = do_sync_read,
948         .aio_read       = generic_file_aio_read,
949
950         .mmap           = generic_file_mmap,
951
952         .splice_read    = generic_file_splice_read,
953         .splice_write   = generic_file_splice_write,
954
955         .write          = pohmelfs_write,
956         .aio_write      = generic_file_aio_write,
957 };
958
959 const struct inode_operations pohmelfs_symlink_inode_operations = {
960         .readlink       = generic_readlink,
961         .follow_link    = page_follow_link_light,
962         .put_link       = page_put_link,
963 };
964
965 int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr)
966 {
967         int err;
968
969         err = inode_change_ok(inode, attr);
970         if (err) {
971                 dprintk("%s: ino: %llu, inode changes are not allowed.\n", __func__, POHMELFS_I(inode)->ino);
972                 goto err_out_exit;
973         }
974
975         if ((attr->ia_valid & ATTR_SIZE) &&
976             attr->ia_size != i_size_read(inode)) {
977                 err = vmtruncate(inode, attr->ia_size);
978                 if (err) {
979                         dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino);
980                         goto err_out_exit;
981                 }
982         }
983
984         setattr_copy(inode, attr);
985         mark_inode_dirty(inode);
986
987         dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n",
988                         __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode,
989                         inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size);
990
991         return 0;
992
993 err_out_exit:
994         return err;
995 }
996
997 int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr)
998 {
999         struct inode *inode = dentry->d_inode;
1000         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1001         int err;
1002
1003         err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
1004         if (err)
1005                 goto err_out_exit;
1006
1007         err = security_inode_setattr(dentry, attr);
1008         if (err)
1009                 goto err_out_exit;
1010
1011         err = pohmelfs_setattr_raw(inode, attr);
1012         if (err)
1013                 goto err_out_exit;
1014
1015         return 0;
1016
1017 err_out_exit:
1018         return err;
1019 }
1020
1021 static int pohmelfs_send_xattr_req(struct pohmelfs_inode *pi, u64 id, u64 start,
1022                 const char *name, const void *value, size_t attrsize, int command)
1023 {
1024         struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
1025         int err, path_len, namelen = strlen(name) + 1; /* 0-byte */
1026         struct netfs_trans *t;
1027         struct netfs_cmd *cmd;
1028         void *data;
1029
1030         dprintk("%s: id: %llu, start: %llu, name: '%s', attrsize: %zu, cmd: %d.\n",
1031                         __func__, id, start, name, attrsize, command);
1032
1033         path_len = pohmelfs_path_length(pi);
1034         if (path_len < 0) {
1035                 err = path_len;
1036                 goto err_out_exit;
1037         }
1038
1039         t = netfs_trans_alloc(psb, namelen + path_len + attrsize, 0, 0);
1040         if (!t) {
1041                 err = -ENOMEM;
1042                 goto err_out_exit;
1043         }
1044
1045         cmd = netfs_trans_current(t);
1046         data = cmd + 1;
1047
1048         path_len = pohmelfs_construct_path_string(pi, data, path_len);
1049         if (path_len < 0) {
1050                 err = path_len;
1051                 goto err_out_put;
1052         }
1053         data += path_len;
1054
1055         /*
1056          * 'name' is a NUL-terminated string already and
1057          * 'namelen' includes 0-byte.
1058          */
1059         memcpy(data, name, namelen);
1060         data += namelen;
1061
1062         memcpy(data, value, attrsize);
1063
1064         cmd->cmd = command;
1065         cmd->id = id;
1066         cmd->start = start;
1067         cmd->size = attrsize + namelen + path_len;
1068         cmd->ext = path_len;
1069         cmd->csize = 0;
1070         cmd->cpad = 0;
1071
1072         netfs_convert_cmd(cmd);
1073         netfs_trans_update(cmd, t, namelen + path_len + attrsize);
1074
1075         return netfs_trans_finish(t, psb);
1076
1077 err_out_put:
1078         t->result = err;
1079         netfs_trans_put(t);
1080 err_out_exit:
1081         return err;
1082 }
1083
1084 static int pohmelfs_setxattr(struct dentry *dentry, const char *name,
1085                 const void *value, size_t attrsize, int flags)
1086 {
1087         struct inode *inode = dentry->d_inode;
1088         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1089         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1090
1091         if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
1092                 return -EOPNOTSUPP;
1093
1094         return pohmelfs_send_xattr_req(pi, flags, attrsize, name,
1095                         value, attrsize, NETFS_XATTR_SET);
1096 }
1097
1098 static ssize_t pohmelfs_getxattr(struct dentry *dentry, const char *name,
1099                 void *value, size_t attrsize)
1100 {
1101         struct inode *inode = dentry->d_inode;
1102         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1103         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1104         struct pohmelfs_mcache *m;
1105         int err;
1106         long timeout = psb->mcache_timeout;
1107
1108         if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
1109                 return -EOPNOTSUPP;
1110
1111         m = pohmelfs_mcache_alloc(psb, 0, attrsize, value);
1112         if (IS_ERR(m))
1113                 return PTR_ERR(m);
1114
1115         dprintk("%s: ino: %llu, name: '%s', size: %zu.\n",
1116                         __func__, pi->ino, name, attrsize);
1117
1118         err = pohmelfs_send_xattr_req(pi, m->gen, attrsize, name, value, 0, NETFS_XATTR_GET);
1119         if (err)
1120                 goto err_out_put;
1121
1122         do {
1123                 err = wait_for_completion_timeout(&m->complete, timeout);
1124                 if (err) {
1125                         err = m->err;
1126                         break;
1127                 }
1128
1129                 /*
1130                  * This loop is a bit ugly, since it waits until reference counter
1131                  * hits 1 and then puts the object here. Main goal is to prevent race with
1132                  * the network thread, when it can start processing the given request, i.e.
1133                  * increase its reference counter but yet not complete it, while
1134                  * we will exit from ->getxattr() with timeout, and although request
1135                  * will not be freed (its reference counter was increased by network
1136                  * thread), data pointer provided by user may be released, so we will
1137                  * overwrite an already freed area in the network thread.
1138                  *
1139                  * Now after timeout we remove request from the cache, so it can not be
1140                  * found by network thread, and wait for its reference counter to hit 1,
1141                  * i.e. if network thread already started to process this request, we wait
1142                  * for it to finish, and then free object locally. If reference counter is
1143                  * already 1, i.e. request is not used by anyone else, we can free it without
1144                  * problem.
1145                  */
1146                 err = -ETIMEDOUT;
1147                 timeout = HZ;
1148
1149                 pohmelfs_mcache_remove_locked(psb, m);
1150         } while (atomic_read(&m->refcnt) != 1);
1151
1152         pohmelfs_mcache_put(psb, m);
1153
1154         dprintk("%s: ino: %llu, err: %d.\n", __func__, pi->ino, err);
1155
1156         return err;
1157
1158 err_out_put:
1159         pohmelfs_mcache_put(psb, m);
1160         return err;
1161 }
1162
1163 static int pohmelfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1164 {
1165         struct inode *inode = dentry->d_inode;
1166 #if 0
1167         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1168         int err;
1169
1170         err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_READ_LOCK);
1171         if (err)
1172                 return err;
1173         dprintk("%s: ino: %llu, mode: %o, uid: %u, gid: %u, size: %llu.\n",
1174                         __func__, pi->ino, inode->i_mode, inode->i_uid,
1175                         inode->i_gid, inode->i_size);
1176 #endif
1177
1178         generic_fillattr(inode, stat);
1179         return 0;
1180 }
1181
1182 const struct inode_operations pohmelfs_file_inode_operations = {
1183         .setattr        = pohmelfs_setattr,
1184         .getattr        = pohmelfs_getattr,
1185         .setxattr       = pohmelfs_setxattr,
1186         .getxattr       = pohmelfs_getxattr,
1187 };
1188
1189 /*
1190  * Fill inode data: mode, size, operation callbacks and so on...
1191  */
1192 void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info)
1193 {
1194         inode->i_mode = info->mode;
1195         inode->i_nlink = info->nlink;
1196         inode->i_uid = info->uid;
1197         inode->i_gid = info->gid;
1198         inode->i_blocks = info->blocks;
1199         inode->i_rdev = info->rdev;
1200         inode->i_size = info->size;
1201         inode->i_version = info->version;
1202         inode->i_blkbits = ffs(info->blocksize);
1203
1204         dprintk("%s: inode: %p, num: %lu/%llu inode is regular: %d, dir: %d, link: %d, mode: %o, size: %llu.\n",
1205                         __func__, inode, inode->i_ino, info->ino,
1206                         S_ISREG(inode->i_mode), S_ISDIR(inode->i_mode),
1207                         S_ISLNK(inode->i_mode), inode->i_mode, inode->i_size);
1208
1209         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
1210
1211         /*
1212          * i_mapping is a pointer to i_data during inode initialization.
1213          */
1214         inode->i_data.a_ops = &pohmelfs_aops;
1215
1216         if (S_ISREG(inode->i_mode)) {
1217                 inode->i_fop = &pohmelfs_file_ops;
1218                 inode->i_op = &pohmelfs_file_inode_operations;
1219         } else if (S_ISDIR(inode->i_mode)) {
1220                 inode->i_fop = &pohmelfs_dir_fops;
1221                 inode->i_op = &pohmelfs_dir_inode_ops;
1222         } else if (S_ISLNK(inode->i_mode)) {
1223                 inode->i_op = &pohmelfs_symlink_inode_operations;
1224                 inode->i_fop = &pohmelfs_file_ops;
1225         } else {
1226                 inode->i_fop = &generic_ro_fops;
1227         }
1228 }
1229
1230 static int pohmelfs_drop_inode(struct inode *inode)
1231 {
1232         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1233         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1234
1235         spin_lock(&psb->ino_lock);
1236         list_del_init(&pi->inode_entry);
1237         spin_unlock(&psb->ino_lock);
1238
1239         return generic_drop_inode(inode);
1240 }
1241
1242 static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb,
1243                 struct list_head *head, unsigned int *count)
1244 {
1245         struct pohmelfs_inode *pi = NULL;
1246
1247         spin_lock(&psb->ino_lock);
1248         if (!list_empty(head)) {
1249                 pi = list_entry(head->next, struct pohmelfs_inode,
1250                                         inode_entry);
1251                 list_del_init(&pi->inode_entry);
1252                 *count = pi->drop_count;
1253                 pi->drop_count = 0;
1254         }
1255         spin_unlock(&psb->ino_lock);
1256
1257         return pi;
1258 }
1259
1260 static void pohmelfs_flush_transactions(struct pohmelfs_sb *psb)
1261 {
1262         struct pohmelfs_config *c;
1263
1264         mutex_lock(&psb->state_lock);
1265         list_for_each_entry(c, &psb->state_list, config_entry) {
1266                 pohmelfs_state_flush_transactions(&c->state);
1267         }
1268         mutex_unlock(&psb->state_lock);
1269 }
1270
1271 /*
1272  * ->put_super() callback. Invoked before superblock is destroyed,
1273  *  so it has to clean all private data.
1274  */
1275 static void pohmelfs_put_super(struct super_block *sb)
1276 {
1277         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1278         struct pohmelfs_inode *pi;
1279         unsigned int count = 0;
1280         unsigned int in_drop_list = 0;
1281         struct inode *inode, *tmp;
1282
1283         dprintk("%s.\n", __func__);
1284
1285         /*
1286          * Kill pending transactions, which could affect inodes in-flight.
1287          */
1288         pohmelfs_flush_transactions(psb);
1289
1290         while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count))) {
1291                 inode = &pi->vfs_inode;
1292
1293                 dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1294                                 __func__, pi->ino, pi, inode, count);
1295
1296                 if (atomic_read(&inode->i_count) != count) {
1297                         printk("%s: ino: %llu, pi: %p, inode: %p, count: %u, i_count: %d.\n",
1298                                         __func__, pi->ino, pi, inode, count,
1299                                         atomic_read(&inode->i_count));
1300                         count = atomic_read(&inode->i_count);
1301                         in_drop_list++;
1302                 }
1303
1304                 while (count--)
1305                         iput(&pi->vfs_inode);
1306         }
1307
1308         list_for_each_entry_safe(inode, tmp, &sb->s_inodes, i_sb_list) {
1309                 pi = POHMELFS_I(inode);
1310
1311                 dprintk("%s: ino: %llu, pi: %p, inode: %p, i_count: %u.\n",
1312                                 __func__, pi->ino, pi, inode, atomic_read(&inode->i_count));
1313
1314                 /*
1315                  * These are special inodes, they were created during
1316                  * directory reading or lookup, and were not bound to dentry,
1317                  * so they live here with reference counter being 1 and prevent
1318                  * umount from succeed since it believes that they are busy.
1319                  */
1320                 count = atomic_read(&inode->i_count);
1321                 if (count) {
1322                         list_del_init(&inode->i_sb_list);
1323                         while (count--)
1324                                 iput(&pi->vfs_inode);
1325                 }
1326         }
1327
1328         psb->trans_scan_timeout = psb->drop_scan_timeout = 0;
1329         cancel_delayed_work_sync(&psb->dwork);
1330         cancel_delayed_work_sync(&psb->drop_dwork);
1331         flush_scheduled_work();
1332
1333         dprintk("%s: stopped workqueues.\n", __func__);
1334
1335         pohmelfs_crypto_exit(psb);
1336         pohmelfs_state_exit(psb);
1337
1338         bdi_destroy(&psb->bdi);
1339
1340         kfree(psb);
1341         sb->s_fs_info = NULL;
1342 }
1343
1344 static int pohmelfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1345 {
1346         struct super_block *sb = dentry->d_sb;
1347         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1348
1349         /*
1350          * There are no filesystem size limits yet.
1351          */
1352         memset(buf, 0, sizeof(struct kstatfs));
1353
1354         buf->f_type = POHMELFS_MAGIC_NUM; /* 'POH.' */
1355         buf->f_bsize = sb->s_blocksize;
1356         buf->f_files = psb->ino;
1357         buf->f_namelen = 255;
1358         buf->f_files = atomic_long_read(&psb->total_inodes);
1359         buf->f_bfree = buf->f_bavail = psb->avail_size >> PAGE_SHIFT;
1360         buf->f_blocks = psb->total_size >> PAGE_SHIFT;
1361
1362         dprintk("%s: total: %llu, avail: %llu, inodes: %llu, bsize: %lu.\n",
1363                 __func__, psb->total_size, psb->avail_size, buf->f_files, sb->s_blocksize);
1364
1365         return 0;
1366 }
1367
1368 static int pohmelfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1369 {
1370         struct pohmelfs_sb *psb = POHMELFS_SB(vfs->mnt_sb);
1371
1372         seq_printf(seq, ",idx=%u", psb->idx);
1373         seq_printf(seq, ",trans_scan_timeout=%u", jiffies_to_msecs(psb->trans_scan_timeout));
1374         seq_printf(seq, ",drop_scan_timeout=%u", jiffies_to_msecs(psb->drop_scan_timeout));
1375         seq_printf(seq, ",wait_on_page_timeout=%u", jiffies_to_msecs(psb->wait_on_page_timeout));
1376         seq_printf(seq, ",trans_retries=%u", psb->trans_retries);
1377         seq_printf(seq, ",crypto_thread_num=%u", psb->crypto_thread_num);
1378         seq_printf(seq, ",trans_max_pages=%u", psb->trans_max_pages);
1379         seq_printf(seq, ",mcache_timeout=%u", jiffies_to_msecs(psb->mcache_timeout));
1380         if (psb->crypto_fail_unsupported)
1381                 seq_printf(seq, ",crypto_fail_unsupported");
1382
1383         return 0;
1384 }
1385
1386 enum {
1387         pohmelfs_opt_idx,
1388         pohmelfs_opt_crypto_thread_num,
1389         pohmelfs_opt_trans_max_pages,
1390         pohmelfs_opt_crypto_fail_unsupported,
1391
1392         /* Remountable options */
1393         pohmelfs_opt_trans_scan_timeout,
1394         pohmelfs_opt_drop_scan_timeout,
1395         pohmelfs_opt_wait_on_page_timeout,
1396         pohmelfs_opt_trans_retries,
1397         pohmelfs_opt_mcache_timeout,
1398 };
1399
1400 static struct match_token pohmelfs_tokens[] = {
1401         {pohmelfs_opt_idx, "idx=%u"},
1402         {pohmelfs_opt_crypto_thread_num, "crypto_thread_num=%u"},
1403         {pohmelfs_opt_trans_max_pages, "trans_max_pages=%u"},
1404         {pohmelfs_opt_crypto_fail_unsupported, "crypto_fail_unsupported"},
1405         {pohmelfs_opt_trans_scan_timeout, "trans_scan_timeout=%u"},
1406         {pohmelfs_opt_drop_scan_timeout, "drop_scan_timeout=%u"},
1407         {pohmelfs_opt_wait_on_page_timeout, "wait_on_page_timeout=%u"},
1408         {pohmelfs_opt_trans_retries, "trans_retries=%u"},
1409         {pohmelfs_opt_mcache_timeout, "mcache_timeout=%u"},
1410 };
1411
1412 static int pohmelfs_parse_options(char *options, struct pohmelfs_sb *psb, int remount)
1413 {
1414         char *p;
1415         substring_t args[MAX_OPT_ARGS];
1416         int option, err;
1417
1418         if (!options)
1419                 return 0;
1420
1421         while ((p = strsep(&options, ",")) != NULL) {
1422                 int token;
1423                 if (!*p)
1424                         continue;
1425
1426                 token = match_token(p, pohmelfs_tokens, args);
1427
1428                 err = match_int(&args[0], &option);
1429                 if (err)
1430                         return err;
1431
1432                 if (remount && token <= pohmelfs_opt_crypto_fail_unsupported)
1433                         continue;
1434
1435                 switch (token) {
1436                 case pohmelfs_opt_idx:
1437                         psb->idx = option;
1438                         break;
1439                 case pohmelfs_opt_trans_scan_timeout:
1440                         psb->trans_scan_timeout = msecs_to_jiffies(option);
1441                         break;
1442                 case pohmelfs_opt_drop_scan_timeout:
1443                         psb->drop_scan_timeout = msecs_to_jiffies(option);
1444                         break;
1445                 case pohmelfs_opt_wait_on_page_timeout:
1446                         psb->wait_on_page_timeout = msecs_to_jiffies(option);
1447                         break;
1448                 case pohmelfs_opt_mcache_timeout:
1449                         psb->mcache_timeout = msecs_to_jiffies(option);
1450                         break;
1451                 case pohmelfs_opt_trans_retries:
1452                         psb->trans_retries = option;
1453                         break;
1454                 case pohmelfs_opt_crypto_thread_num:
1455                         psb->crypto_thread_num = option;
1456                         break;
1457                 case pohmelfs_opt_trans_max_pages:
1458                         psb->trans_max_pages = option;
1459                         break;
1460                 case pohmelfs_opt_crypto_fail_unsupported:
1461                         psb->crypto_fail_unsupported = 1;
1462                         break;
1463                 default:
1464                         return -EINVAL;
1465                 }
1466         }
1467
1468         return 0;
1469 }
1470
1471 static int pohmelfs_remount(struct super_block *sb, int *flags, char *data)
1472 {
1473         int err;
1474         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1475         unsigned long old_sb_flags = sb->s_flags;
1476
1477         err = pohmelfs_parse_options(data, psb, 1);
1478         if (err)
1479                 goto err_out_restore;
1480
1481         if (!(*flags & MS_RDONLY))
1482                 sb->s_flags &= ~MS_RDONLY;
1483         return 0;
1484
1485 err_out_restore:
1486         sb->s_flags = old_sb_flags;
1487         return err;
1488 }
1489
1490 static void pohmelfs_flush_inode(struct pohmelfs_inode *pi, unsigned int count)
1491 {
1492         struct inode *inode = &pi->vfs_inode;
1493
1494         dprintk("%s: %p: ino: %llu, owned: %d.\n",
1495                 __func__, inode, pi->ino, test_bit(NETFS_INODE_OWNED, &pi->state));
1496
1497         mutex_lock(&inode->i_mutex);
1498         if (test_and_clear_bit(NETFS_INODE_OWNED, &pi->state)) {
1499                 filemap_fdatawrite(inode->i_mapping);
1500                 inode->i_sb->s_op->write_inode(inode, 0);
1501         }
1502
1503 #ifdef POHMELFS_TRUNCATE_ON_INODE_FLUSH
1504         truncate_inode_pages(inode->i_mapping, 0);
1505 #endif
1506
1507         pohmelfs_data_unlock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
1508         mutex_unlock(&inode->i_mutex);
1509 }
1510
1511 static void pohmelfs_put_inode_count(struct pohmelfs_inode *pi, unsigned int count)
1512 {
1513         dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1514                         __func__, pi->ino, pi, &pi->vfs_inode, count);
1515
1516         if (test_and_clear_bit(NETFS_INODE_NEED_FLUSH, &pi->state))
1517                 pohmelfs_flush_inode(pi, count);
1518
1519         while (count--)
1520                 iput(&pi->vfs_inode);
1521 }
1522
1523 static void pohmelfs_drop_scan(struct work_struct *work)
1524 {
1525         struct pohmelfs_sb *psb =
1526                 container_of(work, struct pohmelfs_sb, drop_dwork.work);
1527         struct pohmelfs_inode *pi;
1528         unsigned int count = 0;
1529
1530         while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count)))
1531                 pohmelfs_put_inode_count(pi, count);
1532
1533         pohmelfs_check_states(psb);
1534
1535         if (psb->drop_scan_timeout)
1536                 schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
1537 }
1538
1539 /*
1540  * Run through all transactions starting from the oldest,
1541  * drop transaction from current state and try to send it
1542  * to all remote nodes, which are currently installed.
1543  */
1544 static void pohmelfs_trans_scan_state(struct netfs_state *st)
1545 {
1546         struct rb_node *rb_node;
1547         struct netfs_trans_dst *dst;
1548         struct pohmelfs_sb *psb = st->psb;
1549         unsigned int timeout = psb->trans_scan_timeout;
1550         struct netfs_trans *t;
1551         int err;
1552
1553         mutex_lock(&st->trans_lock);
1554         for (rb_node = rb_first(&st->trans_root); rb_node; ) {
1555                 dst = rb_entry(rb_node, struct netfs_trans_dst, state_entry);
1556                 t = dst->trans;
1557
1558                 if (timeout && time_after(dst->send_time + timeout, jiffies)
1559                                 && dst->retries == 0)
1560                         break;
1561
1562                 dprintk("%s: t: %p, gen: %u, st: %p, retries: %u, max: %u.\n",
1563                         __func__, t, t->gen, st, dst->retries, psb->trans_retries);
1564                 netfs_trans_get(t);
1565
1566                 rb_node = rb_next(rb_node);
1567
1568                 err = -ETIMEDOUT;
1569                 if (timeout && (++dst->retries < psb->trans_retries))
1570                         err = netfs_trans_resend(t, psb);
1571
1572                 if (err || (t->flags & NETFS_TRANS_SINGLE_DST)) {
1573                         if (netfs_trans_remove_nolock(dst, st))
1574                                 netfs_trans_drop_dst_nostate(dst);
1575                 }
1576
1577                 t->result = err;
1578                 netfs_trans_put(t);
1579         }
1580         mutex_unlock(&st->trans_lock);
1581 }
1582
1583 /*
1584  * Walk through all installed network states and resend all
1585  * transactions, which are old enough.
1586  */
1587 static void pohmelfs_trans_scan(struct work_struct *work)
1588 {
1589         struct pohmelfs_sb *psb =
1590                 container_of(work, struct pohmelfs_sb, dwork.work);
1591         struct netfs_state *st;
1592         struct pohmelfs_config *c;
1593
1594         mutex_lock(&psb->state_lock);
1595         list_for_each_entry(c, &psb->state_list, config_entry) {
1596                 st = &c->state;
1597
1598                 pohmelfs_trans_scan_state(st);
1599         }
1600         mutex_unlock(&psb->state_lock);
1601
1602         /*
1603          * If no timeout specified then system is in the middle of umount process,
1604          * so no need to reschedule scanning process again.
1605          */
1606         if (psb->trans_scan_timeout)
1607                 schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
1608 }
1609
1610 int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon,
1611                 unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start)
1612 {
1613         struct inode *inode = &pi->vfs_inode;
1614         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1615         int err = 0, sz;
1616         struct netfs_trans *t;
1617         int path_len, addon_len = 0;
1618         void *data;
1619         struct netfs_inode_info *info;
1620         struct netfs_cmd *cmd;
1621
1622         dprintk("%s: ino: %llu, cmd: %u, addon: %p.\n", __func__, pi->ino, cmd_op, addon);
1623
1624         path_len = pohmelfs_path_length(pi);
1625         if (path_len < 0) {
1626                 err = path_len;
1627                 goto err_out_exit;
1628         }
1629
1630         if (addon)
1631                 addon_len = strlen(addon) + 1; /* 0-byte */
1632         sz = addon_len;
1633
1634         if (cmd_op == NETFS_INODE_INFO)
1635                 sz += sizeof(struct netfs_inode_info);
1636
1637         t = netfs_trans_alloc(psb, sz + path_len, flags, 0);
1638         if (!t) {
1639                 err = -ENOMEM;
1640                 goto err_out_exit;
1641         }
1642         t->complete = complete;
1643         t->private = priv;
1644
1645         cmd = netfs_trans_current(t);
1646         data = (void *)(cmd + 1);
1647
1648         if (cmd_op == NETFS_INODE_INFO) {
1649                 info = (struct netfs_inode_info *)(cmd + 1);
1650                 data = (void *)(info + 1);
1651
1652                 /*
1653                  * We are under i_mutex, can read and change whatever we want...
1654                  */
1655                 info->mode = inode->i_mode;
1656                 info->nlink = inode->i_nlink;
1657                 info->uid = inode->i_uid;
1658                 info->gid = inode->i_gid;
1659                 info->blocks = inode->i_blocks;
1660                 info->rdev = inode->i_rdev;
1661                 info->size = inode->i_size;
1662                 info->version = inode->i_version;
1663
1664                 netfs_convert_inode_info(info);
1665         }
1666
1667         path_len = pohmelfs_construct_path_string(pi, data, path_len);
1668         if (path_len < 0)
1669                 goto err_out_free;
1670
1671         dprintk("%s: path_len: %d.\n", __func__, path_len);
1672
1673         if (addon) {
1674                 path_len--; /* Do not place null-byte before the addon */
1675                 path_len += sprintf(data + path_len, "/%s", addon) + 1; /* 0 - byte */
1676         }
1677
1678         sz += path_len;
1679
1680         cmd->cmd = cmd_op;
1681         cmd->ext = path_len;
1682         cmd->size = sz;
1683         cmd->id = id;
1684         cmd->start = start;
1685
1686         netfs_convert_cmd(cmd);
1687         netfs_trans_update(cmd, t, sz);
1688
1689         /*
1690          * Note, that it is possible to leak error here: transaction callback will not
1691          * be invoked for allocation path failure.
1692          */
1693         return netfs_trans_finish(t, psb);
1694
1695 err_out_free:
1696         netfs_trans_free(t);
1697 err_out_exit:
1698         if (complete)
1699                 complete(NULL, 0, priv, err);
1700         return err;
1701 }
1702
1703 int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags,
1704                 netfs_trans_complete_t complete, void *priv, u64 start)
1705 {
1706         return pohmelfs_meta_command_data(pi, pi->ino, cmd_op, NULL, flags, complete, priv, start);
1707 }
1708
1709 /*
1710  * Send request and wait for POHMELFS root capabilities response,
1711  * which will update server's informaion about size of the export,
1712  * permissions, number of objects, available size and so on.
1713  */
1714 static int pohmelfs_root_handshake(struct pohmelfs_sb *psb)
1715 {
1716         struct netfs_trans *t;
1717         struct netfs_cmd *cmd;
1718         int err = -ENOMEM;
1719
1720         t = netfs_trans_alloc(psb, 0, 0, 0);
1721         if (!t)
1722                 goto err_out_exit;
1723
1724         cmd = netfs_trans_current(t);
1725
1726         cmd->cmd = NETFS_CAPABILITIES;
1727         cmd->id = POHMELFS_ROOT_CAPABILITIES;
1728         cmd->size = 0;
1729         cmd->start = 0;
1730         cmd->ext = 0;
1731         cmd->csize = 0;
1732
1733         netfs_convert_cmd(cmd);
1734         netfs_trans_update(cmd, t, 0);
1735
1736         err = netfs_trans_finish(t, psb);
1737         if (err)
1738                 goto err_out_exit;
1739
1740         psb->flags = ~0;
1741         err = wait_event_interruptible_timeout(psb->wait,
1742                         (psb->flags != ~0),
1743                         psb->wait_on_page_timeout);
1744         if (!err)
1745                 err = -ETIMEDOUT;
1746         else if (err > 0)
1747                 err = -psb->flags;
1748
1749         if (err)
1750                 goto err_out_exit;
1751
1752         return 0;
1753
1754 err_out_exit:
1755         return err;
1756 }
1757
1758 static int pohmelfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
1759 {
1760         struct netfs_state *st;
1761         struct pohmelfs_ctl *ctl;
1762         struct pohmelfs_sb *psb = POHMELFS_SB(mnt->mnt_sb);
1763         struct pohmelfs_config *c;
1764
1765         mutex_lock(&psb->state_lock);
1766
1767         seq_printf(m, "\nidx addr(:port) socket_type protocol active priority permissions\n");
1768
1769         list_for_each_entry(c, &psb->state_list, config_entry) {
1770                 st = &c->state;
1771                 ctl = &st->ctl;
1772
1773                 seq_printf(m, "%u ", ctl->idx);
1774                 if (ctl->addr.sa_family == AF_INET) {
1775                         struct sockaddr_in *sin = (struct sockaddr_in *)&st->ctl.addr;
1776                         seq_printf(m, "%pI4:%u", &sin->sin_addr.s_addr, ntohs(sin->sin_port));
1777                 } else if (ctl->addr.sa_family == AF_INET6) {
1778                         struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&st->ctl.addr;
1779                         seq_printf(m, "%pi6:%u", &sin->sin6_addr, ntohs(sin->sin6_port));
1780                 } else {
1781                         unsigned int i;
1782                         for (i = 0; i < ctl->addrlen; ++i)
1783                                 seq_printf(m, "%02x.", ctl->addr.addr[i]);
1784                 }
1785
1786                 seq_printf(m, " %u %u %d %u %x\n",
1787                                 ctl->type, ctl->proto,
1788                                 st->socket != NULL,
1789                                 ctl->prio, ctl->perm);
1790         }
1791         mutex_unlock(&psb->state_lock);
1792
1793         return 0;
1794 }
1795
1796 static const struct super_operations pohmelfs_sb_ops = {
1797         .alloc_inode    = pohmelfs_alloc_inode,
1798         .destroy_inode  = pohmelfs_destroy_inode,
1799         .drop_inode     = pohmelfs_drop_inode,
1800         .write_inode    = pohmelfs_write_inode,
1801         .put_super      = pohmelfs_put_super,
1802         .remount_fs     = pohmelfs_remount,
1803         .statfs         = pohmelfs_statfs,
1804         .show_options   = pohmelfs_show_options,
1805         .show_stats     = pohmelfs_show_stats,
1806 };
1807
1808 /*
1809  * Allocate private superblock and create root dir.
1810  */
1811 static int pohmelfs_fill_super(struct super_block *sb, void *data, int silent)
1812 {
1813         struct pohmelfs_sb *psb;
1814         int err = -ENOMEM;
1815         struct inode *root;
1816         struct pohmelfs_inode *npi;
1817         struct qstr str;
1818
1819         psb = kzalloc(sizeof(struct pohmelfs_sb), GFP_KERNEL);
1820         if (!psb)
1821                 goto err_out_exit;
1822
1823         err = bdi_init(&psb->bdi);
1824         if (err)
1825                 goto err_out_free_sb;
1826
1827         err = bdi_register(&psb->bdi, NULL, "pfs-%d", atomic_inc_return(&psb_bdi_num));
1828         if (err) {
1829                 bdi_destroy(&psb->bdi);
1830                 goto err_out_free_sb;
1831         }
1832
1833         sb->s_fs_info = psb;
1834         sb->s_op = &pohmelfs_sb_ops;
1835         sb->s_magic = POHMELFS_MAGIC_NUM;
1836         sb->s_maxbytes = MAX_LFS_FILESIZE;
1837         sb->s_blocksize = PAGE_SIZE;
1838         sb->s_bdi = &psb->bdi;
1839
1840         psb->sb = sb;
1841
1842         psb->ino = 2;
1843         psb->idx = 0;
1844         psb->active_state = NULL;
1845         psb->trans_retries = 5;
1846         psb->trans_data_size = PAGE_SIZE;
1847         psb->drop_scan_timeout = msecs_to_jiffies(1000);
1848         psb->trans_scan_timeout = msecs_to_jiffies(5000);
1849         psb->wait_on_page_timeout = msecs_to_jiffies(5000);
1850         init_waitqueue_head(&psb->wait);
1851
1852         spin_lock_init(&psb->ino_lock);
1853
1854         INIT_LIST_HEAD(&psb->drop_list);
1855
1856         mutex_init(&psb->mcache_lock);
1857         psb->mcache_root = RB_ROOT;
1858         psb->mcache_timeout = msecs_to_jiffies(5000);
1859         atomic_long_set(&psb->mcache_gen, 0);
1860
1861         psb->trans_max_pages = 100;
1862
1863         psb->crypto_align_size = 16;
1864         psb->crypto_attached_size = 0;
1865         psb->hash_strlen = 0;
1866         psb->cipher_strlen = 0;
1867         psb->perform_crypto = 0;
1868         psb->crypto_thread_num = 2;
1869         psb->crypto_fail_unsupported = 0;
1870         mutex_init(&psb->crypto_thread_lock);
1871         INIT_LIST_HEAD(&psb->crypto_ready_list);
1872         INIT_LIST_HEAD(&psb->crypto_active_list);
1873
1874         atomic_set(&psb->trans_gen, 1);
1875         atomic_long_set(&psb->total_inodes, 0);
1876
1877         mutex_init(&psb->state_lock);
1878         INIT_LIST_HEAD(&psb->state_list);
1879
1880         err = pohmelfs_parse_options((char *) data, psb, 0);
1881         if (err)
1882                 goto err_out_free_bdi;
1883
1884         err = pohmelfs_copy_crypto(psb);
1885         if (err)
1886                 goto err_out_free_bdi;
1887
1888         err = pohmelfs_state_init(psb);
1889         if (err)
1890                 goto err_out_free_strings;
1891
1892         err = pohmelfs_crypto_init(psb);
1893         if (err)
1894                 goto err_out_state_exit;
1895
1896         err = pohmelfs_root_handshake(psb);
1897         if (err)
1898                 goto err_out_crypto_exit;
1899
1900         str.name = "/";
1901         str.hash = jhash("/", 1, 0);
1902         str.len = 1;
1903
1904         npi = pohmelfs_create_entry_local(psb, NULL, &str, 0, 0755|S_IFDIR);
1905         if (IS_ERR(npi)) {
1906                 err = PTR_ERR(npi);
1907                 goto err_out_crypto_exit;
1908         }
1909         set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state);
1910         clear_bit(NETFS_INODE_OWNED, &npi->state);
1911
1912         root = &npi->vfs_inode;
1913
1914         sb->s_root = d_alloc_root(root);
1915         if (!sb->s_root)
1916                 goto err_out_put_root;
1917
1918         INIT_DELAYED_WORK(&psb->drop_dwork, pohmelfs_drop_scan);
1919         schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
1920
1921         INIT_DELAYED_WORK(&psb->dwork, pohmelfs_trans_scan);
1922         schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
1923
1924         return 0;
1925
1926 err_out_put_root:
1927         iput(root);
1928 err_out_crypto_exit:
1929         pohmelfs_crypto_exit(psb);
1930 err_out_state_exit:
1931         pohmelfs_state_exit(psb);
1932 err_out_free_strings:
1933         kfree(psb->cipher_string);
1934         kfree(psb->hash_string);
1935 err_out_free_bdi:
1936         bdi_destroy(&psb->bdi);
1937 err_out_free_sb:
1938         kfree(psb);
1939 err_out_exit:
1940
1941         dprintk("%s: err: %d.\n", __func__, err);
1942         return err;
1943 }
1944
1945 /*
1946  * Some VFS magic here...
1947  */
1948 static struct dentry *pohmelfs_mount(struct file_system_type *fs_type,
1949         int flags, const char *dev_name, void *data)
1950 {
1951         return mount_nodev(fs_type, flags, data, pohmelfs_fill_super);
1952 }
1953
1954 /*
1955  * We need this to sync all inodes earlier, since when writeback
1956  * is invoked from the umount/mntput path dcache is already shrunk,
1957  * see generic_shutdown_super(), and no inodes can access the path.
1958  */
1959 static void pohmelfs_kill_super(struct super_block *sb)
1960 {
1961         sync_inodes_sb(sb);
1962         kill_anon_super(sb);
1963 }
1964
1965 static struct file_system_type pohmel_fs_type = {
1966         .owner          = THIS_MODULE,
1967         .name           = "pohmel",
1968         .mount          = pohmelfs_mount,
1969         .kill_sb        = pohmelfs_kill_super,
1970 };
1971
1972 /*
1973  * Cache and module initializations and freeing routings.
1974  */
1975 static void pohmelfs_init_once(void *data)
1976 {
1977         struct pohmelfs_inode *pi = data;
1978
1979         inode_init_once(&pi->vfs_inode);
1980 }
1981
1982 static int __init pohmelfs_init_inodecache(void)
1983 {
1984         pohmelfs_inode_cache = kmem_cache_create("pohmelfs_inode_cache",
1985                                 sizeof(struct pohmelfs_inode),
1986                                 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1987                                 pohmelfs_init_once);
1988         if (!pohmelfs_inode_cache)
1989                 return -ENOMEM;
1990
1991         return 0;
1992 }
1993
1994 static void pohmelfs_destroy_inodecache(void)
1995 {
1996         kmem_cache_destroy(pohmelfs_inode_cache);
1997 }
1998
1999 static int __init init_pohmel_fs(void)
2000 {
2001         int err;
2002
2003         err = pohmelfs_config_init();
2004         if (err)
2005                 goto err_out_exit;
2006
2007         err = pohmelfs_init_inodecache();
2008         if (err)
2009                 goto err_out_config_exit;
2010
2011         err = pohmelfs_mcache_init();
2012         if (err)
2013                 goto err_out_destroy;
2014
2015         err = netfs_trans_init();
2016         if (err)
2017                 goto err_out_mcache_exit;
2018
2019         err = register_filesystem(&pohmel_fs_type);
2020         if (err)
2021                 goto err_out_trans;
2022
2023         return 0;
2024
2025 err_out_trans:
2026         netfs_trans_exit();
2027 err_out_mcache_exit:
2028         pohmelfs_mcache_exit();
2029 err_out_destroy:
2030         pohmelfs_destroy_inodecache();
2031 err_out_config_exit:
2032         pohmelfs_config_exit();
2033 err_out_exit:
2034         return err;
2035 }
2036
2037 static void __exit exit_pohmel_fs(void)
2038 {
2039         unregister_filesystem(&pohmel_fs_type);
2040         pohmelfs_destroy_inodecache();
2041         pohmelfs_mcache_exit();
2042         pohmelfs_config_exit();
2043         netfs_trans_exit();
2044 }
2045
2046 module_init(init_pohmel_fs);
2047 module_exit(exit_pohmel_fs);
2048
2049 MODULE_LICENSE("GPL");
2050 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
2051 MODULE_DESCRIPTION("Pohmel filesystem");