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