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