Merge branch 'for-2.6.38' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[cascardo/linux.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 static void pohmelfs_i_callback(struct rcu_head *head)
830 {
831         struct inode *inode = container_of(head, struct inode, i_rcu);
832         INIT_LIST_HEAD(&inode->i_dentry);
833         kmem_cache_free(pohmelfs_inode_cache, POHMELFS_I(inode));
834 }
835
836 /*
837  * ->detroy_inode() callback. Deletes inode from the caches
838  *  and frees private data.
839  */
840 static void pohmelfs_destroy_inode(struct inode *inode)
841 {
842         struct super_block *sb = inode->i_sb;
843         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
844         struct pohmelfs_inode *pi = POHMELFS_I(inode);
845
846         /* pohmelfs_data_unlock(pi, 0, inode->i_size, POHMELFS_READ_LOCK); */
847
848         pohmelfs_inode_del_inode(psb, pi);
849
850         dprintk("%s: pi: %p, inode: %p, ino: %llu.\n",
851                 __func__, pi, &pi->vfs_inode, pi->ino);
852         atomic_long_dec(&psb->total_inodes);
853         call_rcu(&inode->i_rcu, pohmelfs_i_callback);
854 }
855
856 /*
857  * ->alloc_inode() callback. Allocates inode and initializes private data.
858  */
859 static struct inode *pohmelfs_alloc_inode(struct super_block *sb)
860 {
861         struct pohmelfs_inode *pi;
862
863         pi = kmem_cache_alloc(pohmelfs_inode_cache, GFP_NOIO);
864         if (!pi)
865                 return NULL;
866
867         pi->hash_root = RB_ROOT;
868         mutex_init(&pi->offset_lock);
869
870         INIT_LIST_HEAD(&pi->sync_create_list);
871
872         INIT_LIST_HEAD(&pi->inode_entry);
873
874         pi->lock_type = 0;
875         pi->state = 0;
876         pi->total_len = 0;
877         pi->drop_count = 0;
878
879         dprintk("%s: pi: %p, inode: %p.\n", __func__, pi, &pi->vfs_inode);
880
881         atomic_long_inc(&POHMELFS_SB(sb)->total_inodes);
882
883         return &pi->vfs_inode;
884 }
885
886 /*
887  * We want fsync() to work on POHMELFS.
888  */
889 static int pohmelfs_fsync(struct file *file, int datasync)
890 {
891         struct inode *inode = file->f_mapping->host;
892
893         return sync_inode_metadata(inode, 1);
894 }
895
896 ssize_t pohmelfs_write(struct file *file, const char __user *buf,
897                 size_t len, loff_t *ppos)
898 {
899         struct address_space *mapping = file->f_mapping;
900         struct inode *inode = mapping->host;
901         struct pohmelfs_inode *pi = POHMELFS_I(inode);
902         struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
903         struct kiocb kiocb;
904         ssize_t ret;
905         loff_t pos = *ppos;
906
907         init_sync_kiocb(&kiocb, file);
908         kiocb.ki_pos = pos;
909         kiocb.ki_left = len;
910
911         dprintk("%s: len: %zu, pos: %llu.\n", __func__, len, pos);
912
913         mutex_lock(&inode->i_mutex);
914         ret = pohmelfs_data_lock(pi, pos, len, POHMELFS_WRITE_LOCK);
915         if (ret)
916                 goto err_out_unlock;
917
918         ret = __generic_file_aio_write(&kiocb, &iov, 1, &kiocb.ki_pos);
919         *ppos = kiocb.ki_pos;
920
921         mutex_unlock(&inode->i_mutex);
922         WARN_ON(ret < 0);
923
924         if (ret > 0) {
925                 ssize_t err;
926
927                 err = generic_write_sync(file, pos, ret);
928                 if (err < 0)
929                         ret = err;
930                 WARN_ON(ret < 0);
931         }
932
933         return ret;
934
935 err_out_unlock:
936         mutex_unlock(&inode->i_mutex);
937         return ret;
938 }
939
940 static const struct file_operations pohmelfs_file_ops = {
941         .open           = generic_file_open,
942         .fsync          = pohmelfs_fsync,
943
944         .llseek         = generic_file_llseek,
945
946         .read           = do_sync_read,
947         .aio_read       = generic_file_aio_read,
948
949         .mmap           = generic_file_mmap,
950
951         .splice_read    = generic_file_splice_read,
952         .splice_write   = generic_file_splice_write,
953
954         .write          = pohmelfs_write,
955         .aio_write      = generic_file_aio_write,
956 };
957
958 const struct inode_operations pohmelfs_symlink_inode_operations = {
959         .readlink       = generic_readlink,
960         .follow_link    = page_follow_link_light,
961         .put_link       = page_put_link,
962 };
963
964 int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr)
965 {
966         int err;
967
968         err = inode_change_ok(inode, attr);
969         if (err) {
970                 dprintk("%s: ino: %llu, inode changes are not allowed.\n", __func__, POHMELFS_I(inode)->ino);
971                 goto err_out_exit;
972         }
973
974         if ((attr->ia_valid & ATTR_SIZE) &&
975             attr->ia_size != i_size_read(inode)) {
976                 err = vmtruncate(inode, attr->ia_size);
977                 if (err) {
978                         dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino);
979                         goto err_out_exit;
980                 }
981         }
982
983         setattr_copy(inode, attr);
984         mark_inode_dirty(inode);
985
986         dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n",
987                         __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode,
988                         inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size);
989
990         return 0;
991
992 err_out_exit:
993         return err;
994 }
995
996 int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr)
997 {
998         struct inode *inode = dentry->d_inode;
999         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1000         int err;
1001
1002         err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
1003         if (err)
1004                 goto err_out_exit;
1005
1006         err = security_inode_setattr(dentry, attr);
1007         if (err)
1008                 goto err_out_exit;
1009
1010         err = pohmelfs_setattr_raw(inode, attr);
1011         if (err)
1012                 goto err_out_exit;
1013
1014         return 0;
1015
1016 err_out_exit:
1017         return err;
1018 }
1019
1020 static int pohmelfs_send_xattr_req(struct pohmelfs_inode *pi, u64 id, u64 start,
1021                 const char *name, const void *value, size_t attrsize, int command)
1022 {
1023         struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
1024         int err, path_len, namelen = strlen(name) + 1; /* 0-byte */
1025         struct netfs_trans *t;
1026         struct netfs_cmd *cmd;
1027         void *data;
1028
1029         dprintk("%s: id: %llu, start: %llu, name: '%s', attrsize: %zu, cmd: %d.\n",
1030                         __func__, id, start, name, attrsize, command);
1031
1032         path_len = pohmelfs_path_length(pi);
1033         if (path_len < 0) {
1034                 err = path_len;
1035                 goto err_out_exit;
1036         }
1037
1038         t = netfs_trans_alloc(psb, namelen + path_len + attrsize, 0, 0);
1039         if (!t) {
1040                 err = -ENOMEM;
1041                 goto err_out_exit;
1042         }
1043
1044         cmd = netfs_trans_current(t);
1045         data = cmd + 1;
1046
1047         path_len = pohmelfs_construct_path_string(pi, data, path_len);
1048         if (path_len < 0) {
1049                 err = path_len;
1050                 goto err_out_put;
1051         }
1052         data += path_len;
1053
1054         /*
1055          * 'name' is a NUL-terminated string already and
1056          * 'namelen' includes 0-byte.
1057          */
1058         memcpy(data, name, namelen);
1059         data += namelen;
1060
1061         memcpy(data, value, attrsize);
1062
1063         cmd->cmd = command;
1064         cmd->id = id;
1065         cmd->start = start;
1066         cmd->size = attrsize + namelen + path_len;
1067         cmd->ext = path_len;
1068         cmd->csize = 0;
1069         cmd->cpad = 0;
1070
1071         netfs_convert_cmd(cmd);
1072         netfs_trans_update(cmd, t, namelen + path_len + attrsize);
1073
1074         return netfs_trans_finish(t, psb);
1075
1076 err_out_put:
1077         t->result = err;
1078         netfs_trans_put(t);
1079 err_out_exit:
1080         return err;
1081 }
1082
1083 static int pohmelfs_setxattr(struct dentry *dentry, const char *name,
1084                 const void *value, size_t attrsize, int flags)
1085 {
1086         struct inode *inode = dentry->d_inode;
1087         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1088         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1089
1090         if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
1091                 return -EOPNOTSUPP;
1092
1093         return pohmelfs_send_xattr_req(pi, flags, attrsize, name,
1094                         value, attrsize, NETFS_XATTR_SET);
1095 }
1096
1097 static ssize_t pohmelfs_getxattr(struct dentry *dentry, const char *name,
1098                 void *value, size_t attrsize)
1099 {
1100         struct inode *inode = dentry->d_inode;
1101         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1102         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1103         struct pohmelfs_mcache *m;
1104         int err;
1105         long timeout = psb->mcache_timeout;
1106
1107         if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
1108                 return -EOPNOTSUPP;
1109
1110         m = pohmelfs_mcache_alloc(psb, 0, attrsize, value);
1111         if (IS_ERR(m))
1112                 return PTR_ERR(m);
1113
1114         dprintk("%s: ino: %llu, name: '%s', size: %zu.\n",
1115                         __func__, pi->ino, name, attrsize);
1116
1117         err = pohmelfs_send_xattr_req(pi, m->gen, attrsize, name, value, 0, NETFS_XATTR_GET);
1118         if (err)
1119                 goto err_out_put;
1120
1121         do {
1122                 err = wait_for_completion_timeout(&m->complete, timeout);
1123                 if (err) {
1124                         err = m->err;
1125                         break;
1126                 }
1127
1128                 /*
1129                  * This loop is a bit ugly, since it waits until reference counter
1130                  * hits 1 and then put object here. Main goal is to prevent race with
1131                  * network thread, when it can start processing given request, i.e.
1132                  * increase its reference counter but yet not complete it, while
1133                  * we will exit from ->getxattr() with timeout, and although request
1134                  * will not be freed (its reference counter was increased by network
1135                  * thread), data pointer provided by user may be released, so we will
1136                  * overwrite already freed area in network thread.
1137                  *
1138                  * Now after timeout we remove request from the cache, so it can not be
1139                  * found by network thread, and wait for its reference counter to hit 1,
1140                  * i.e. if network thread already started to process this request, we wait
1141                  * it to finish, and then free object locally. If reference counter is
1142                  * already 1, i.e. request is not used by anyone else, we can free it without
1143                  * problem.
1144                  */
1145                 err = -ETIMEDOUT;
1146                 timeout = HZ;
1147
1148                 pohmelfs_mcache_remove_locked(psb, m);
1149         } while (atomic_read(&m->refcnt) != 1);
1150
1151         pohmelfs_mcache_put(psb, m);
1152
1153         dprintk("%s: ino: %llu, err: %d.\n", __func__, pi->ino, err);
1154
1155         return err;
1156
1157 err_out_put:
1158         pohmelfs_mcache_put(psb, m);
1159         return err;
1160 }
1161
1162 static int pohmelfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1163 {
1164         struct inode *inode = dentry->d_inode;
1165 #if 0
1166         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1167         int err;
1168
1169         err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_READ_LOCK);
1170         if (err)
1171                 return err;
1172         dprintk("%s: ino: %llu, mode: %o, uid: %u, gid: %u, size: %llu.\n",
1173                         __func__, pi->ino, inode->i_mode, inode->i_uid,
1174                         inode->i_gid, inode->i_size);
1175 #endif
1176
1177         generic_fillattr(inode, stat);
1178         return 0;
1179 }
1180
1181 const struct inode_operations pohmelfs_file_inode_operations = {
1182         .setattr        = pohmelfs_setattr,
1183         .getattr        = pohmelfs_getattr,
1184         .setxattr       = pohmelfs_setxattr,
1185         .getxattr       = pohmelfs_getxattr,
1186 };
1187
1188 /*
1189  * Fill inode data: mode, size, operation callbacks and so on...
1190  */
1191 void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info)
1192 {
1193         inode->i_mode = info->mode;
1194         inode->i_nlink = info->nlink;
1195         inode->i_uid = info->uid;
1196         inode->i_gid = info->gid;
1197         inode->i_blocks = info->blocks;
1198         inode->i_rdev = info->rdev;
1199         inode->i_size = info->size;
1200         inode->i_version = info->version;
1201         inode->i_blkbits = ffs(info->blocksize);
1202
1203         dprintk("%s: inode: %p, num: %lu/%llu inode is regular: %d, dir: %d, link: %d, mode: %o, size: %llu.\n",
1204                         __func__, inode, inode->i_ino, info->ino,
1205                         S_ISREG(inode->i_mode), S_ISDIR(inode->i_mode),
1206                         S_ISLNK(inode->i_mode), inode->i_mode, inode->i_size);
1207
1208         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
1209
1210         /*
1211          * i_mapping is a pointer to i_data during inode initialization.
1212          */
1213         inode->i_data.a_ops = &pohmelfs_aops;
1214
1215         if (S_ISREG(inode->i_mode)) {
1216                 inode->i_fop = &pohmelfs_file_ops;
1217                 inode->i_op = &pohmelfs_file_inode_operations;
1218         } else if (S_ISDIR(inode->i_mode)) {
1219                 inode->i_fop = &pohmelfs_dir_fops;
1220                 inode->i_op = &pohmelfs_dir_inode_ops;
1221         } else if (S_ISLNK(inode->i_mode)) {
1222                 inode->i_op = &pohmelfs_symlink_inode_operations;
1223                 inode->i_fop = &pohmelfs_file_ops;
1224         } else {
1225                 inode->i_fop = &generic_ro_fops;
1226         }
1227 }
1228
1229 static int pohmelfs_drop_inode(struct inode *inode)
1230 {
1231         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1232         struct pohmelfs_inode *pi = POHMELFS_I(inode);
1233
1234         spin_lock(&psb->ino_lock);
1235         list_del_init(&pi->inode_entry);
1236         spin_unlock(&psb->ino_lock);
1237
1238         return generic_drop_inode(inode);
1239 }
1240
1241 static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb,
1242                 struct list_head *head, unsigned int *count)
1243 {
1244         struct pohmelfs_inode *pi = NULL;
1245
1246         spin_lock(&psb->ino_lock);
1247         if (!list_empty(head)) {
1248                 pi = list_entry(head->next, struct pohmelfs_inode,
1249                                         inode_entry);
1250                 list_del_init(&pi->inode_entry);
1251                 *count = pi->drop_count;
1252                 pi->drop_count = 0;
1253         }
1254         spin_unlock(&psb->ino_lock);
1255
1256         return pi;
1257 }
1258
1259 static void pohmelfs_flush_transactions(struct pohmelfs_sb *psb)
1260 {
1261         struct pohmelfs_config *c;
1262
1263         mutex_lock(&psb->state_lock);
1264         list_for_each_entry(c, &psb->state_list, config_entry) {
1265                 pohmelfs_state_flush_transactions(&c->state);
1266         }
1267         mutex_unlock(&psb->state_lock);
1268 }
1269
1270 /*
1271  * ->put_super() callback. Invoked before superblock is destroyed,
1272  *  so it has to clean all private data.
1273  */
1274 static void pohmelfs_put_super(struct super_block *sb)
1275 {
1276         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1277         struct pohmelfs_inode *pi;
1278         unsigned int count = 0;
1279         unsigned int in_drop_list = 0;
1280         struct inode *inode, *tmp;
1281
1282         dprintk("%s.\n", __func__);
1283
1284         /*
1285          * Kill pending transactions, which could affect inodes in-flight.
1286          */
1287         pohmelfs_flush_transactions(psb);
1288
1289         while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count))) {
1290                 inode = &pi->vfs_inode;
1291
1292                 dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1293                                 __func__, pi->ino, pi, inode, count);
1294
1295                 if (atomic_read(&inode->i_count) != count) {
1296                         printk("%s: ino: %llu, pi: %p, inode: %p, count: %u, i_count: %d.\n",
1297                                         __func__, pi->ino, pi, inode, count,
1298                                         atomic_read(&inode->i_count));
1299                         count = atomic_read(&inode->i_count);
1300                         in_drop_list++;
1301                 }
1302
1303                 while (count--)
1304                         iput(&pi->vfs_inode);
1305         }
1306
1307         list_for_each_entry_safe(inode, tmp, &sb->s_inodes, i_sb_list) {
1308                 pi = POHMELFS_I(inode);
1309
1310                 dprintk("%s: ino: %llu, pi: %p, inode: %p, i_count: %u.\n",
1311                                 __func__, pi->ino, pi, inode, atomic_read(&inode->i_count));
1312
1313                 /*
1314                  * These are special inodes, they were created during
1315                  * directory reading or lookup, and were not bound to dentry,
1316                  * so they live here with reference counter being 1 and prevent
1317                  * umount from succeed since it believes that they are busy.
1318                  */
1319                 count = atomic_read(&inode->i_count);
1320                 if (count) {
1321                         list_del_init(&inode->i_sb_list);
1322                         while (count--)
1323                                 iput(&pi->vfs_inode);
1324                 }
1325         }
1326
1327         psb->trans_scan_timeout = psb->drop_scan_timeout = 0;
1328         cancel_delayed_work_sync(&psb->dwork);
1329         cancel_delayed_work_sync(&psb->drop_dwork);
1330         flush_scheduled_work();
1331
1332         dprintk("%s: stopped workqueues.\n", __func__);
1333
1334         pohmelfs_crypto_exit(psb);
1335         pohmelfs_state_exit(psb);
1336
1337         bdi_destroy(&psb->bdi);
1338
1339         kfree(psb);
1340         sb->s_fs_info = NULL;
1341 }
1342
1343 static int pohmelfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1344 {
1345         struct super_block *sb = dentry->d_sb;
1346         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1347
1348         /*
1349          * There are no filesystem size limits yet.
1350          */
1351         memset(buf, 0, sizeof(struct kstatfs));
1352
1353         buf->f_type = POHMELFS_MAGIC_NUM; /* 'POH.' */
1354         buf->f_bsize = sb->s_blocksize;
1355         buf->f_files = psb->ino;
1356         buf->f_namelen = 255;
1357         buf->f_files = atomic_long_read(&psb->total_inodes);
1358         buf->f_bfree = buf->f_bavail = psb->avail_size >> PAGE_SHIFT;
1359         buf->f_blocks = psb->total_size >> PAGE_SHIFT;
1360
1361         dprintk("%s: total: %llu, avail: %llu, inodes: %llu, bsize: %lu.\n",
1362                 __func__, psb->total_size, psb->avail_size, buf->f_files, sb->s_blocksize);
1363
1364         return 0;
1365 }
1366
1367 static int pohmelfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1368 {
1369         struct pohmelfs_sb *psb = POHMELFS_SB(vfs->mnt_sb);
1370
1371         seq_printf(seq, ",idx=%u", psb->idx);
1372         seq_printf(seq, ",trans_scan_timeout=%u", jiffies_to_msecs(psb->trans_scan_timeout));
1373         seq_printf(seq, ",drop_scan_timeout=%u", jiffies_to_msecs(psb->drop_scan_timeout));
1374         seq_printf(seq, ",wait_on_page_timeout=%u", jiffies_to_msecs(psb->wait_on_page_timeout));
1375         seq_printf(seq, ",trans_retries=%u", psb->trans_retries);
1376         seq_printf(seq, ",crypto_thread_num=%u", psb->crypto_thread_num);
1377         seq_printf(seq, ",trans_max_pages=%u", psb->trans_max_pages);
1378         seq_printf(seq, ",mcache_timeout=%u", jiffies_to_msecs(psb->mcache_timeout));
1379         if (psb->crypto_fail_unsupported)
1380                 seq_printf(seq, ",crypto_fail_unsupported");
1381
1382         return 0;
1383 }
1384
1385 enum {
1386         pohmelfs_opt_idx,
1387         pohmelfs_opt_crypto_thread_num,
1388         pohmelfs_opt_trans_max_pages,
1389         pohmelfs_opt_crypto_fail_unsupported,
1390
1391         /* Remountable options */
1392         pohmelfs_opt_trans_scan_timeout,
1393         pohmelfs_opt_drop_scan_timeout,
1394         pohmelfs_opt_wait_on_page_timeout,
1395         pohmelfs_opt_trans_retries,
1396         pohmelfs_opt_mcache_timeout,
1397 };
1398
1399 static struct match_token pohmelfs_tokens[] = {
1400         {pohmelfs_opt_idx, "idx=%u"},
1401         {pohmelfs_opt_crypto_thread_num, "crypto_thread_num=%u"},
1402         {pohmelfs_opt_trans_max_pages, "trans_max_pages=%u"},
1403         {pohmelfs_opt_crypto_fail_unsupported, "crypto_fail_unsupported"},
1404         {pohmelfs_opt_trans_scan_timeout, "trans_scan_timeout=%u"},
1405         {pohmelfs_opt_drop_scan_timeout, "drop_scan_timeout=%u"},
1406         {pohmelfs_opt_wait_on_page_timeout, "wait_on_page_timeout=%u"},
1407         {pohmelfs_opt_trans_retries, "trans_retries=%u"},
1408         {pohmelfs_opt_mcache_timeout, "mcache_timeout=%u"},
1409 };
1410
1411 static int pohmelfs_parse_options(char *options, struct pohmelfs_sb *psb, int remount)
1412 {
1413         char *p;
1414         substring_t args[MAX_OPT_ARGS];
1415         int option, err;
1416
1417         if (!options)
1418                 return 0;
1419
1420         while ((p = strsep(&options, ",")) != NULL) {
1421                 int token;
1422                 if (!*p)
1423                         continue;
1424
1425                 token = match_token(p, pohmelfs_tokens, args);
1426
1427                 err = match_int(&args[0], &option);
1428                 if (err)
1429                         return err;
1430
1431                 if (remount && token <= pohmelfs_opt_crypto_fail_unsupported)
1432                         continue;
1433
1434                 switch (token) {
1435                 case pohmelfs_opt_idx:
1436                         psb->idx = option;
1437                         break;
1438                 case pohmelfs_opt_trans_scan_timeout:
1439                         psb->trans_scan_timeout = msecs_to_jiffies(option);
1440                         break;
1441                 case pohmelfs_opt_drop_scan_timeout:
1442                         psb->drop_scan_timeout = msecs_to_jiffies(option);
1443                         break;
1444                 case pohmelfs_opt_wait_on_page_timeout:
1445                         psb->wait_on_page_timeout = msecs_to_jiffies(option);
1446                         break;
1447                 case pohmelfs_opt_mcache_timeout:
1448                         psb->mcache_timeout = msecs_to_jiffies(option);
1449                         break;
1450                 case pohmelfs_opt_trans_retries:
1451                         psb->trans_retries = option;
1452                         break;
1453                 case pohmelfs_opt_crypto_thread_num:
1454                         psb->crypto_thread_num = option;
1455                         break;
1456                 case pohmelfs_opt_trans_max_pages:
1457                         psb->trans_max_pages = option;
1458                         break;
1459                 case pohmelfs_opt_crypto_fail_unsupported:
1460                         psb->crypto_fail_unsupported = 1;
1461                         break;
1462                 default:
1463                         return -EINVAL;
1464                 }
1465         }
1466
1467         return 0;
1468 }
1469
1470 static int pohmelfs_remount(struct super_block *sb, int *flags, char *data)
1471 {
1472         int err;
1473         struct pohmelfs_sb *psb = POHMELFS_SB(sb);
1474         unsigned long old_sb_flags = sb->s_flags;
1475
1476         err = pohmelfs_parse_options(data, psb, 1);
1477         if (err)
1478                 goto err_out_restore;
1479
1480         if (!(*flags & MS_RDONLY))
1481                 sb->s_flags &= ~MS_RDONLY;
1482         return 0;
1483
1484 err_out_restore:
1485         sb->s_flags = old_sb_flags;
1486         return err;
1487 }
1488
1489 static void pohmelfs_flush_inode(struct pohmelfs_inode *pi, unsigned int count)
1490 {
1491         struct inode *inode = &pi->vfs_inode;
1492
1493         dprintk("%s: %p: ino: %llu, owned: %d.\n",
1494                 __func__, inode, pi->ino, test_bit(NETFS_INODE_OWNED, &pi->state));
1495
1496         mutex_lock(&inode->i_mutex);
1497         if (test_and_clear_bit(NETFS_INODE_OWNED, &pi->state)) {
1498                 filemap_fdatawrite(inode->i_mapping);
1499                 inode->i_sb->s_op->write_inode(inode, 0);
1500         }
1501
1502 #ifdef POHMELFS_TRUNCATE_ON_INODE_FLUSH
1503         truncate_inode_pages(inode->i_mapping, 0);
1504 #endif
1505
1506         pohmelfs_data_unlock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
1507         mutex_unlock(&inode->i_mutex);
1508 }
1509
1510 static void pohmelfs_put_inode_count(struct pohmelfs_inode *pi, unsigned int count)
1511 {
1512         dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
1513                         __func__, pi->ino, pi, &pi->vfs_inode, count);
1514
1515         if (test_and_clear_bit(NETFS_INODE_NEED_FLUSH, &pi->state))
1516                 pohmelfs_flush_inode(pi, count);
1517
1518         while (count--)
1519                 iput(&pi->vfs_inode);
1520 }
1521
1522 static void pohmelfs_drop_scan(struct work_struct *work)
1523 {
1524         struct pohmelfs_sb *psb =
1525                 container_of(work, struct pohmelfs_sb, drop_dwork.work);
1526         struct pohmelfs_inode *pi;
1527         unsigned int count = 0;
1528
1529         while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count)))
1530                 pohmelfs_put_inode_count(pi, count);
1531
1532         pohmelfs_check_states(psb);
1533
1534         if (psb->drop_scan_timeout)
1535                 schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
1536 }
1537
1538 /*
1539  * Run through all transactions starting from the oldest,
1540  * drop transaction from current state and try to send it
1541  * to all remote nodes, which are currently installed.
1542  */
1543 static void pohmelfs_trans_scan_state(struct netfs_state *st)
1544 {
1545         struct rb_node *rb_node;
1546         struct netfs_trans_dst *dst;
1547         struct pohmelfs_sb *psb = st->psb;
1548         unsigned int timeout = psb->trans_scan_timeout;
1549         struct netfs_trans *t;
1550         int err;
1551
1552         mutex_lock(&st->trans_lock);
1553         for (rb_node = rb_first(&st->trans_root); rb_node; ) {
1554                 dst = rb_entry(rb_node, struct netfs_trans_dst, state_entry);
1555                 t = dst->trans;
1556
1557                 if (timeout && time_after(dst->send_time + timeout, jiffies)
1558                                 && dst->retries == 0)
1559                         break;
1560
1561                 dprintk("%s: t: %p, gen: %u, st: %p, retries: %u, max: %u.\n",
1562                         __func__, t, t->gen, st, dst->retries, psb->trans_retries);
1563                 netfs_trans_get(t);
1564
1565                 rb_node = rb_next(rb_node);
1566
1567                 err = -ETIMEDOUT;
1568                 if (timeout && (++dst->retries < psb->trans_retries))
1569                         err = netfs_trans_resend(t, psb);
1570
1571                 if (err || (t->flags & NETFS_TRANS_SINGLE_DST)) {
1572                         if (netfs_trans_remove_nolock(dst, st))
1573                                 netfs_trans_drop_dst_nostate(dst);
1574                 }
1575
1576                 t->result = err;
1577                 netfs_trans_put(t);
1578         }
1579         mutex_unlock(&st->trans_lock);
1580 }
1581
1582 /*
1583  * Walk through all installed network states and resend all
1584  * transactions, which are old enough.
1585  */
1586 static void pohmelfs_trans_scan(struct work_struct *work)
1587 {
1588         struct pohmelfs_sb *psb =
1589                 container_of(work, struct pohmelfs_sb, dwork.work);
1590         struct netfs_state *st;
1591         struct pohmelfs_config *c;
1592
1593         mutex_lock(&psb->state_lock);
1594         list_for_each_entry(c, &psb->state_list, config_entry) {
1595                 st = &c->state;
1596
1597                 pohmelfs_trans_scan_state(st);
1598         }
1599         mutex_unlock(&psb->state_lock);
1600
1601         /*
1602          * If no timeout specified then system is in the middle of umount process,
1603          * so no need to reschedule scanning process again.
1604          */
1605         if (psb->trans_scan_timeout)
1606                 schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
1607 }
1608
1609 int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon,
1610                 unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start)
1611 {
1612         struct inode *inode = &pi->vfs_inode;
1613         struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
1614         int err = 0, sz;
1615         struct netfs_trans *t;
1616         int path_len, addon_len = 0;
1617         void *data;
1618         struct netfs_inode_info *info;
1619         struct netfs_cmd *cmd;
1620
1621         dprintk("%s: ino: %llu, cmd: %u, addon: %p.\n", __func__, pi->ino, cmd_op, addon);
1622
1623         path_len = pohmelfs_path_length(pi);
1624         if (path_len < 0) {
1625                 err = path_len;
1626                 goto err_out_exit;
1627         }
1628
1629         if (addon)
1630                 addon_len = strlen(addon) + 1; /* 0-byte */
1631         sz = addon_len;
1632
1633         if (cmd_op == NETFS_INODE_INFO)
1634                 sz += sizeof(struct netfs_inode_info);
1635
1636         t = netfs_trans_alloc(psb, sz + path_len, flags, 0);
1637         if (!t) {
1638                 err = -ENOMEM;
1639                 goto err_out_exit;
1640         }
1641         t->complete = complete;
1642         t->private = priv;
1643
1644         cmd = netfs_trans_current(t);
1645         data = (void *)(cmd + 1);
1646
1647         if (cmd_op == NETFS_INODE_INFO) {
1648                 info = (struct netfs_inode_info *)(cmd + 1);
1649                 data = (void *)(info + 1);
1650
1651                 /*
1652                  * We are under i_mutex, can read and change whatever we want...
1653                  */
1654                 info->mode = inode->i_mode;
1655                 info->nlink = inode->i_nlink;
1656                 info->uid = inode->i_uid;
1657                 info->gid = inode->i_gid;
1658                 info->blocks = inode->i_blocks;
1659                 info->rdev = inode->i_rdev;
1660                 info->size = inode->i_size;
1661                 info->version = inode->i_version;
1662
1663                 netfs_convert_inode_info(info);
1664         }
1665
1666         path_len = pohmelfs_construct_path_string(pi, data, path_len);
1667         if (path_len < 0)
1668                 goto err_out_free;
1669
1670         dprintk("%s: path_len: %d.\n", __func__, path_len);
1671
1672         if (addon) {
1673                 path_len--; /* Do not place null-byte before the addon */
1674                 path_len += sprintf(data + path_len, "/%s", addon) + 1; /* 0 - byte */
1675         }
1676
1677         sz += path_len;
1678
1679         cmd->cmd = cmd_op;
1680         cmd->ext = path_len;
1681         cmd->size = sz;
1682         cmd->id = id;
1683         cmd->start = start;
1684
1685         netfs_convert_cmd(cmd);
1686         netfs_trans_update(cmd, t, sz);
1687
1688         /*
1689          * Note, that it is possible to leak error here: transaction callback will not
1690          * be invoked for allocation path failure.
1691          */
1692         return netfs_trans_finish(t, psb);
1693
1694 err_out_free:
1695         netfs_trans_free(t);
1696 err_out_exit:
1697         if (complete)
1698                 complete(NULL, 0, priv, err);
1699         return err;
1700 }
1701
1702 int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags,
1703                 netfs_trans_complete_t complete, void *priv, u64 start)
1704 {
1705         return pohmelfs_meta_command_data(pi, pi->ino, cmd_op, NULL, flags, complete, priv, start);
1706 }
1707
1708 /*
1709  * Send request and wait for POHMELFS root capabilities response,
1710  * which will update server's informaion about size of the export,
1711  * permissions, number of objects, available size and so on.
1712  */
1713 static int pohmelfs_root_handshake(struct pohmelfs_sb *psb)
1714 {
1715         struct netfs_trans *t;
1716         struct netfs_cmd *cmd;
1717         int err = -ENOMEM;
1718
1719         t = netfs_trans_alloc(psb, 0, 0, 0);
1720         if (!t)
1721                 goto err_out_exit;
1722
1723         cmd = netfs_trans_current(t);
1724
1725         cmd->cmd = NETFS_CAPABILITIES;
1726         cmd->id = POHMELFS_ROOT_CAPABILITIES;
1727         cmd->size = 0;
1728         cmd->start = 0;
1729         cmd->ext = 0;
1730         cmd->csize = 0;
1731
1732         netfs_convert_cmd(cmd);
1733         netfs_trans_update(cmd, t, 0);
1734
1735         err = netfs_trans_finish(t, psb);
1736         if (err)
1737                 goto err_out_exit;
1738
1739         psb->flags = ~0;
1740         err = wait_event_interruptible_timeout(psb->wait,
1741                         (psb->flags != ~0),
1742                         psb->wait_on_page_timeout);
1743         if (!err)
1744                 err = -ETIMEDOUT;
1745         else if (err > 0)
1746                 err = -psb->flags;
1747
1748         if (err)
1749                 goto err_out_exit;
1750
1751         return 0;
1752
1753 err_out_exit:
1754         return err;
1755 }
1756
1757 static int pohmelfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
1758 {
1759         struct netfs_state *st;
1760         struct pohmelfs_ctl *ctl;
1761         struct pohmelfs_sb *psb = POHMELFS_SB(mnt->mnt_sb);
1762         struct pohmelfs_config *c;
1763
1764         mutex_lock(&psb->state_lock);
1765
1766         seq_printf(m, "\nidx addr(:port) socket_type protocol active priority permissions\n");
1767
1768         list_for_each_entry(c, &psb->state_list, config_entry) {
1769                 st = &c->state;
1770                 ctl = &st->ctl;
1771
1772                 seq_printf(m, "%u ", ctl->idx);
1773                 if (ctl->addr.sa_family == AF_INET) {
1774                         struct sockaddr_in *sin = (struct sockaddr_in *)&st->ctl.addr;
1775                         seq_printf(m, "%pI4:%u", &sin->sin_addr.s_addr, ntohs(sin->sin_port));
1776                 } else if (ctl->addr.sa_family == AF_INET6) {
1777                         struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&st->ctl.addr;
1778                         seq_printf(m, "%pi6:%u", &sin->sin6_addr, ntohs(sin->sin6_port));
1779                 } else {
1780                         unsigned int i;
1781                         for (i = 0; i < ctl->addrlen; ++i)
1782                                 seq_printf(m, "%02x.", ctl->addr.addr[i]);
1783                 }
1784
1785                 seq_printf(m, " %u %u %d %u %x\n",
1786                                 ctl->type, ctl->proto,
1787                                 st->socket != NULL,
1788                                 ctl->prio, ctl->perm);
1789         }
1790         mutex_unlock(&psb->state_lock);
1791
1792         return 0;
1793 }
1794
1795 static const struct super_operations pohmelfs_sb_ops = {
1796         .alloc_inode    = pohmelfs_alloc_inode,
1797         .destroy_inode  = pohmelfs_destroy_inode,
1798         .drop_inode     = pohmelfs_drop_inode,
1799         .write_inode    = pohmelfs_write_inode,
1800         .put_super      = pohmelfs_put_super,
1801         .remount_fs     = pohmelfs_remount,
1802         .statfs         = pohmelfs_statfs,
1803         .show_options   = pohmelfs_show_options,
1804         .show_stats     = pohmelfs_show_stats,
1805 };
1806
1807 /*
1808  * Allocate private superblock and create root dir.
1809  */
1810 static int pohmelfs_fill_super(struct super_block *sb, void *data, int silent)
1811 {
1812         struct pohmelfs_sb *psb;
1813         int err = -ENOMEM;
1814         struct inode *root;
1815         struct pohmelfs_inode *npi;
1816         struct qstr str;
1817
1818         psb = kzalloc(sizeof(struct pohmelfs_sb), GFP_KERNEL);
1819         if (!psb)
1820                 goto err_out_exit;
1821
1822         err = bdi_init(&psb->bdi);
1823         if (err)
1824                 goto err_out_free_sb;
1825
1826         err = bdi_register(&psb->bdi, NULL, "pfs-%d", atomic_inc_return(&psb_bdi_num));
1827         if (err) {
1828                 bdi_destroy(&psb->bdi);
1829                 goto err_out_free_sb;
1830         }
1831
1832         sb->s_fs_info = psb;
1833         sb->s_op = &pohmelfs_sb_ops;
1834         sb->s_magic = POHMELFS_MAGIC_NUM;
1835         sb->s_maxbytes = MAX_LFS_FILESIZE;
1836         sb->s_blocksize = PAGE_SIZE;
1837         sb->s_bdi = &psb->bdi;
1838
1839         psb->sb = sb;
1840
1841         psb->ino = 2;
1842         psb->idx = 0;
1843         psb->active_state = NULL;
1844         psb->trans_retries = 5;
1845         psb->trans_data_size = PAGE_SIZE;
1846         psb->drop_scan_timeout = msecs_to_jiffies(1000);
1847         psb->trans_scan_timeout = msecs_to_jiffies(5000);
1848         psb->wait_on_page_timeout = msecs_to_jiffies(5000);
1849         init_waitqueue_head(&psb->wait);
1850
1851         spin_lock_init(&psb->ino_lock);
1852
1853         INIT_LIST_HEAD(&psb->drop_list);
1854
1855         mutex_init(&psb->mcache_lock);
1856         psb->mcache_root = RB_ROOT;
1857         psb->mcache_timeout = msecs_to_jiffies(5000);
1858         atomic_long_set(&psb->mcache_gen, 0);
1859
1860         psb->trans_max_pages = 100;
1861
1862         psb->crypto_align_size = 16;
1863         psb->crypto_attached_size = 0;
1864         psb->hash_strlen = 0;
1865         psb->cipher_strlen = 0;
1866         psb->perform_crypto = 0;
1867         psb->crypto_thread_num = 2;
1868         psb->crypto_fail_unsupported = 0;
1869         mutex_init(&psb->crypto_thread_lock);
1870         INIT_LIST_HEAD(&psb->crypto_ready_list);
1871         INIT_LIST_HEAD(&psb->crypto_active_list);
1872
1873         atomic_set(&psb->trans_gen, 1);
1874         atomic_long_set(&psb->total_inodes, 0);
1875
1876         mutex_init(&psb->state_lock);
1877         INIT_LIST_HEAD(&psb->state_list);
1878
1879         err = pohmelfs_parse_options((char *) data, psb, 0);
1880         if (err)
1881                 goto err_out_free_bdi;
1882
1883         err = pohmelfs_copy_crypto(psb);
1884         if (err)
1885                 goto err_out_free_bdi;
1886
1887         err = pohmelfs_state_init(psb);
1888         if (err)
1889                 goto err_out_free_strings;
1890
1891         err = pohmelfs_crypto_init(psb);
1892         if (err)
1893                 goto err_out_state_exit;
1894
1895         err = pohmelfs_root_handshake(psb);
1896         if (err)
1897                 goto err_out_crypto_exit;
1898
1899         str.name = "/";
1900         str.hash = jhash("/", 1, 0);
1901         str.len = 1;
1902
1903         npi = pohmelfs_create_entry_local(psb, NULL, &str, 0, 0755|S_IFDIR);
1904         if (IS_ERR(npi)) {
1905                 err = PTR_ERR(npi);
1906                 goto err_out_crypto_exit;
1907         }
1908         set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state);
1909         clear_bit(NETFS_INODE_OWNED, &npi->state);
1910
1911         root = &npi->vfs_inode;
1912
1913         sb->s_root = d_alloc_root(root);
1914         if (!sb->s_root)
1915                 goto err_out_put_root;
1916
1917         INIT_DELAYED_WORK(&psb->drop_dwork, pohmelfs_drop_scan);
1918         schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
1919
1920         INIT_DELAYED_WORK(&psb->dwork, pohmelfs_trans_scan);
1921         schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
1922
1923         return 0;
1924
1925 err_out_put_root:
1926         iput(root);
1927 err_out_crypto_exit:
1928         pohmelfs_crypto_exit(psb);
1929 err_out_state_exit:
1930         pohmelfs_state_exit(psb);
1931 err_out_free_strings:
1932         kfree(psb->cipher_string);
1933         kfree(psb->hash_string);
1934 err_out_free_bdi:
1935         bdi_destroy(&psb->bdi);
1936 err_out_free_sb:
1937         kfree(psb);
1938 err_out_exit:
1939
1940         dprintk("%s: err: %d.\n", __func__, err);
1941         return err;
1942 }
1943
1944 /*
1945  * Some VFS magic here...
1946  */
1947 static struct dentry *pohmelfs_mount(struct file_system_type *fs_type,
1948         int flags, const char *dev_name, void *data)
1949 {
1950         return mount_nodev(fs_type, flags, data, pohmelfs_fill_super);
1951 }
1952
1953 /*
1954  * We need this to sync all inodes earlier, since when writeback
1955  * is invoked from the umount/mntput path dcache is already shrunk,
1956  * see generic_shutdown_super(), and no inodes can access the path.
1957  */
1958 static void pohmelfs_kill_super(struct super_block *sb)
1959 {
1960         sync_inodes_sb(sb);
1961         kill_anon_super(sb);
1962 }
1963
1964 static struct file_system_type pohmel_fs_type = {
1965         .owner          = THIS_MODULE,
1966         .name           = "pohmel",
1967         .mount          = pohmelfs_mount,
1968         .kill_sb        = pohmelfs_kill_super,
1969 };
1970
1971 /*
1972  * Cache and module initializations and freeing routings.
1973  */
1974 static void pohmelfs_init_once(void *data)
1975 {
1976         struct pohmelfs_inode *pi = data;
1977
1978         inode_init_once(&pi->vfs_inode);
1979 }
1980
1981 static int __init pohmelfs_init_inodecache(void)
1982 {
1983         pohmelfs_inode_cache = kmem_cache_create("pohmelfs_inode_cache",
1984                                 sizeof(struct pohmelfs_inode),
1985                                 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1986                                 pohmelfs_init_once);
1987         if (!pohmelfs_inode_cache)
1988                 return -ENOMEM;
1989
1990         return 0;
1991 }
1992
1993 static void pohmelfs_destroy_inodecache(void)
1994 {
1995         kmem_cache_destroy(pohmelfs_inode_cache);
1996 }
1997
1998 static int __init init_pohmel_fs(void)
1999 {
2000         int err;
2001
2002         err = pohmelfs_config_init();
2003         if (err)
2004                 goto err_out_exit;
2005
2006         err = pohmelfs_init_inodecache();
2007         if (err)
2008                 goto err_out_config_exit;
2009
2010         err = pohmelfs_mcache_init();
2011         if (err)
2012                 goto err_out_destroy;
2013
2014         err = netfs_trans_init();
2015         if (err)
2016                 goto err_out_mcache_exit;
2017
2018         err = register_filesystem(&pohmel_fs_type);
2019         if (err)
2020                 goto err_out_trans;
2021
2022         return 0;
2023
2024 err_out_trans:
2025         netfs_trans_exit();
2026 err_out_mcache_exit:
2027         pohmelfs_mcache_exit();
2028 err_out_destroy:
2029         pohmelfs_destroy_inodecache();
2030 err_out_config_exit:
2031         pohmelfs_config_exit();
2032 err_out_exit:
2033         return err;
2034 }
2035
2036 static void __exit exit_pohmel_fs(void)
2037 {
2038         unregister_filesystem(&pohmel_fs_type);
2039         pohmelfs_destroy_inodecache();
2040         pohmelfs_mcache_exit();
2041         pohmelfs_config_exit();
2042         netfs_trans_exit();
2043 }
2044
2045 module_init(init_pohmel_fs);
2046 module_exit(exit_pohmel_fs);
2047
2048 MODULE_LICENSE("GPL");
2049 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
2050 MODULE_DESCRIPTION("Pohmel filesystem");