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