]>
Commit | Line | Data |
---|---|---|
e8062719 BH |
1 | /* |
2 | * Copyright (C) 2005, 2006 | |
27d2e149 | 3 | * Avishay Traeger (avishay@gmail.com) |
e8062719 BH |
4 | * Copyright (C) 2008, 2009 |
5 | * Boaz Harrosh <bharrosh@panasas.com> | |
6 | * | |
7 | * Copyrights for code taken from ext2: | |
8 | * Copyright (C) 1992, 1993, 1994, 1995 | |
9 | * Remy Card (card@masi.ibp.fr) | |
10 | * Laboratoire MASI - Institut Blaise Pascal | |
11 | * Universite Pierre et Marie Curie (Paris VI) | |
12 | * from | |
13 | * linux/fs/minix/inode.c | |
14 | * Copyright (C) 1991, 1992 Linus Torvalds | |
15 | * | |
16 | * This file is part of exofs. | |
17 | * | |
18 | * exofs is free software; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation. Since it is based on ext2, and the only | |
21 | * valid version of GPL for the Linux kernel is version 2, the only valid | |
22 | * version of GPL for exofs is version 2. | |
23 | * | |
24 | * exofs is distributed in the hope that it will be useful, | |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License | |
30 | * along with exofs; if not, write to the Free Software | |
31 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
32 | */ | |
33 | ||
5a0e3ad6 | 34 | #include <linux/slab.h> |
e8062719 BH |
35 | |
36 | #include "exofs.h" | |
37 | ||
fe33cc1e BH |
38 | #define EXOFS_DBGMSG2(M...) do {} while (0) |
39 | ||
5a51c0c7 | 40 | enum {MAX_PAGES_KMALLOC = PAGE_SIZE / sizeof(struct page *), }; |
06886a5a | 41 | |
8ff660ab | 42 | unsigned exofs_max_io_pages(struct ore_layout *layout, |
66cd6cad | 43 | unsigned expected_pages) |
44 | { | |
45 | unsigned pages = min_t(unsigned, expected_pages, MAX_PAGES_KMALLOC); | |
46 | ||
47 | /* TODO: easily support bio chaining */ | |
5a51c0c7 | 48 | pages = min_t(unsigned, pages, layout->max_io_length / PAGE_SIZE); |
66cd6cad | 49 | return pages; |
50 | } | |
51 | ||
beaec07b BH |
52 | struct page_collect { |
53 | struct exofs_sb_info *sbi; | |
beaec07b BH |
54 | struct inode *inode; |
55 | unsigned expected_pages; | |
8ff660ab | 56 | struct ore_io_state *ios; |
beaec07b | 57 | |
86093aaf BH |
58 | struct page **pages; |
59 | unsigned alloc_pages; | |
beaec07b BH |
60 | unsigned nr_pages; |
61 | unsigned long length; | |
62 | loff_t pg_first; /* keep 64bit also in 32-arches */ | |
f17b1f9f BH |
63 | bool read_4_write; /* This means two things: that the read is sync |
64 | * And the pages should not be unlocked. | |
65 | */ | |
dd296619 | 66 | struct page *that_locked_page; |
beaec07b BH |
67 | }; |
68 | ||
69 | static void _pcol_init(struct page_collect *pcol, unsigned expected_pages, | |
06886a5a | 70 | struct inode *inode) |
beaec07b BH |
71 | { |
72 | struct exofs_sb_info *sbi = inode->i_sb->s_fs_info; | |
beaec07b BH |
73 | |
74 | pcol->sbi = sbi; | |
beaec07b BH |
75 | pcol->inode = inode; |
76 | pcol->expected_pages = expected_pages; | |
77 | ||
06886a5a | 78 | pcol->ios = NULL; |
86093aaf BH |
79 | pcol->pages = NULL; |
80 | pcol->alloc_pages = 0; | |
beaec07b BH |
81 | pcol->nr_pages = 0; |
82 | pcol->length = 0; | |
83 | pcol->pg_first = -1; | |
f17b1f9f | 84 | pcol->read_4_write = false; |
dd296619 | 85 | pcol->that_locked_page = NULL; |
beaec07b BH |
86 | } |
87 | ||
88 | static void _pcol_reset(struct page_collect *pcol) | |
89 | { | |
90 | pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages); | |
91 | ||
86093aaf BH |
92 | pcol->pages = NULL; |
93 | pcol->alloc_pages = 0; | |
beaec07b BH |
94 | pcol->nr_pages = 0; |
95 | pcol->length = 0; | |
96 | pcol->pg_first = -1; | |
06886a5a | 97 | pcol->ios = NULL; |
dd296619 | 98 | pcol->that_locked_page = NULL; |
beaec07b BH |
99 | |
100 | /* this is probably the end of the loop but in writes | |
101 | * it might not end here. don't be left with nothing | |
102 | */ | |
103 | if (!pcol->expected_pages) | |
86093aaf | 104 | pcol->expected_pages = MAX_PAGES_KMALLOC; |
beaec07b BH |
105 | } |
106 | ||
107 | static int pcol_try_alloc(struct page_collect *pcol) | |
108 | { | |
66cd6cad | 109 | unsigned pages; |
06886a5a | 110 | |
86093aaf | 111 | /* TODO: easily support bio chaining */ |
66cd6cad | 112 | pages = exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages); |
86093aaf | 113 | |
beaec07b | 114 | for (; pages; pages >>= 1) { |
86093aaf BH |
115 | pcol->pages = kmalloc(pages * sizeof(struct page *), |
116 | GFP_KERNEL); | |
117 | if (likely(pcol->pages)) { | |
118 | pcol->alloc_pages = pages; | |
beaec07b | 119 | return 0; |
86093aaf | 120 | } |
beaec07b BH |
121 | } |
122 | ||
86093aaf | 123 | EXOFS_ERR("Failed to kmalloc expected_pages=%u\n", |
beaec07b BH |
124 | pcol->expected_pages); |
125 | return -ENOMEM; | |
126 | } | |
127 | ||
128 | static void pcol_free(struct page_collect *pcol) | |
129 | { | |
86093aaf BH |
130 | kfree(pcol->pages); |
131 | pcol->pages = NULL; | |
06886a5a BH |
132 | |
133 | if (pcol->ios) { | |
8ff660ab | 134 | ore_put_io_state(pcol->ios); |
06886a5a BH |
135 | pcol->ios = NULL; |
136 | } | |
beaec07b BH |
137 | } |
138 | ||
139 | static int pcol_add_page(struct page_collect *pcol, struct page *page, | |
140 | unsigned len) | |
141 | { | |
86093aaf | 142 | if (unlikely(pcol->nr_pages >= pcol->alloc_pages)) |
beaec07b BH |
143 | return -ENOMEM; |
144 | ||
86093aaf | 145 | pcol->pages[pcol->nr_pages++] = page; |
beaec07b BH |
146 | pcol->length += len; |
147 | return 0; | |
148 | } | |
149 | ||
154a9300 | 150 | enum {PAGE_WAS_NOT_IN_IO = 17}; |
beaec07b BH |
151 | static int update_read_page(struct page *page, int ret) |
152 | { | |
154a9300 BH |
153 | switch (ret) { |
154 | case 0: | |
beaec07b BH |
155 | /* Everything is OK */ |
156 | SetPageUptodate(page); | |
157 | if (PageError(page)) | |
158 | ClearPageError(page); | |
154a9300 BH |
159 | break; |
160 | case -EFAULT: | |
beaec07b BH |
161 | /* In this case we were trying to read something that wasn't on |
162 | * disk yet - return a page full of zeroes. This should be OK, | |
163 | * because the object should be empty (if there was a write | |
164 | * before this read, the read would be waiting with the page | |
165 | * locked */ | |
166 | clear_highpage(page); | |
167 | ||
168 | SetPageUptodate(page); | |
169 | if (PageError(page)) | |
170 | ClearPageError(page); | |
beaec07b | 171 | EXOFS_DBGMSG("recovered read error\n"); |
154a9300 BH |
172 | /* fall through */ |
173 | case PAGE_WAS_NOT_IN_IO: | |
174 | ret = 0; /* recovered error */ | |
175 | break; | |
176 | default: | |
beaec07b | 177 | SetPageError(page); |
154a9300 | 178 | } |
beaec07b BH |
179 | return ret; |
180 | } | |
181 | ||
182 | static void update_write_page(struct page *page, int ret) | |
183 | { | |
154a9300 BH |
184 | if (unlikely(ret == PAGE_WAS_NOT_IN_IO)) |
185 | return; /* don't pass start don't collect $200 */ | |
186 | ||
beaec07b BH |
187 | if (ret) { |
188 | mapping_set_error(page->mapping, ret); | |
189 | SetPageError(page); | |
190 | } | |
191 | end_page_writeback(page); | |
192 | } | |
193 | ||
194 | /* Called at the end of reads, to optionally unlock pages and update their | |
195 | * status. | |
196 | */ | |
7aebf410 | 197 | static int __readpages_done(struct page_collect *pcol) |
beaec07b | 198 | { |
beaec07b | 199 | int i; |
beaec07b BH |
200 | u64 good_bytes; |
201 | u64 length = 0; | |
4b46c9f5 | 202 | int ret = ore_check_io(pcol->ios, NULL); |
beaec07b | 203 | |
154a9300 | 204 | if (likely(!ret)) { |
beaec07b | 205 | good_bytes = pcol->length; |
154a9300 BH |
206 | ret = PAGE_WAS_NOT_IN_IO; |
207 | } else { | |
4b46c9f5 | 208 | good_bytes = 0; |
154a9300 | 209 | } |
beaec07b | 210 | |
34ce4e7c | 211 | EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx" |
beaec07b BH |
212 | " length=0x%lx nr_pages=%u\n", |
213 | pcol->inode->i_ino, _LLU(good_bytes), pcol->length, | |
214 | pcol->nr_pages); | |
215 | ||
86093aaf BH |
216 | for (i = 0; i < pcol->nr_pages; i++) { |
217 | struct page *page = pcol->pages[i]; | |
beaec07b BH |
218 | struct inode *inode = page->mapping->host; |
219 | int page_stat; | |
220 | ||
221 | if (inode != pcol->inode) | |
222 | continue; /* osd might add more pages at end */ | |
223 | ||
224 | if (likely(length < good_bytes)) | |
225 | page_stat = 0; | |
226 | else | |
227 | page_stat = ret; | |
228 | ||
fe33cc1e | 229 | EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n", |
beaec07b BH |
230 | inode->i_ino, page->index, |
231 | page_stat ? "bad_bytes" : "good_bytes"); | |
232 | ||
233 | ret = update_read_page(page, page_stat); | |
7aebf410 | 234 | if (!pcol->read_4_write) |
beaec07b | 235 | unlock_page(page); |
86093aaf | 236 | length += PAGE_SIZE; |
beaec07b BH |
237 | } |
238 | ||
239 | pcol_free(pcol); | |
34ce4e7c | 240 | EXOFS_DBGMSG2("readpages_done END\n"); |
beaec07b BH |
241 | return ret; |
242 | } | |
243 | ||
244 | /* callback of async reads */ | |
8ff660ab | 245 | static void readpages_done(struct ore_io_state *ios, void *p) |
beaec07b BH |
246 | { |
247 | struct page_collect *pcol = p; | |
248 | ||
7aebf410 | 249 | __readpages_done(pcol); |
beaec07b | 250 | atomic_dec(&pcol->sbi->s_curr_pending); |
06886a5a | 251 | kfree(pcol); |
beaec07b BH |
252 | } |
253 | ||
254 | static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw) | |
255 | { | |
beaec07b BH |
256 | int i; |
257 | ||
86093aaf BH |
258 | for (i = 0; i < pcol->nr_pages; i++) { |
259 | struct page *page = pcol->pages[i]; | |
beaec07b BH |
260 | |
261 | if (rw == READ) | |
262 | update_read_page(page, ret); | |
263 | else | |
264 | update_write_page(page, ret); | |
265 | ||
266 | unlock_page(page); | |
267 | } | |
beaec07b BH |
268 | } |
269 | ||
b916c5cd BH |
270 | static int _maybe_not_all_in_one_io(struct ore_io_state *ios, |
271 | struct page_collect *pcol_src, struct page_collect *pcol) | |
272 | { | |
273 | /* length was wrong or offset was not page aligned */ | |
274 | BUG_ON(pcol_src->nr_pages < ios->nr_pages); | |
275 | ||
276 | if (pcol_src->nr_pages > ios->nr_pages) { | |
277 | struct page **src_page; | |
278 | unsigned pages_less = pcol_src->nr_pages - ios->nr_pages; | |
279 | unsigned long len_less = pcol_src->length - ios->length; | |
280 | unsigned i; | |
281 | int ret; | |
282 | ||
283 | /* This IO was trimmed */ | |
284 | pcol_src->nr_pages = ios->nr_pages; | |
285 | pcol_src->length = ios->length; | |
286 | ||
287 | /* Left over pages are passed to the next io */ | |
288 | pcol->expected_pages += pages_less; | |
289 | pcol->nr_pages = pages_less; | |
290 | pcol->length = len_less; | |
291 | src_page = pcol_src->pages + pcol_src->nr_pages; | |
292 | pcol->pg_first = (*src_page)->index; | |
293 | ||
294 | ret = pcol_try_alloc(pcol); | |
295 | if (unlikely(ret)) | |
296 | return ret; | |
297 | ||
298 | for (i = 0; i < pages_less; ++i) | |
299 | pcol->pages[i] = *src_page++; | |
300 | ||
301 | EXOFS_DBGMSG("Length was adjusted nr_pages=0x%x " | |
302 | "pages_less=0x%x expected_pages=0x%x " | |
303 | "next_offset=0x%llx next_len=0x%lx\n", | |
304 | pcol_src->nr_pages, pages_less, pcol->expected_pages, | |
305 | pcol->pg_first * PAGE_SIZE, pcol->length); | |
306 | } | |
307 | return 0; | |
308 | } | |
309 | ||
7aebf410 | 310 | static int read_exec(struct page_collect *pcol) |
beaec07b BH |
311 | { |
312 | struct exofs_i_info *oi = exofs_i(pcol->inode); | |
8ff660ab | 313 | struct ore_io_state *ios; |
beaec07b | 314 | struct page_collect *pcol_copy = NULL; |
beaec07b BH |
315 | int ret; |
316 | ||
86093aaf | 317 | if (!pcol->pages) |
beaec07b BH |
318 | return 0; |
319 | ||
e1042ba0 | 320 | if (!pcol->ios) { |
5bf696da | 321 | int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true, |
e1042ba0 BH |
322 | pcol->pg_first << PAGE_CACHE_SHIFT, |
323 | pcol->length, &pcol->ios); | |
324 | ||
325 | if (ret) | |
326 | return ret; | |
327 | } | |
328 | ||
329 | ios = pcol->ios; | |
86093aaf | 330 | ios->pages = pcol->pages; |
beaec07b | 331 | |
7aebf410 | 332 | if (pcol->read_4_write) { |
8ff660ab | 333 | ore_read(pcol->ios); |
7aebf410 | 334 | return __readpages_done(pcol); |
beaec07b BH |
335 | } |
336 | ||
337 | pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL); | |
338 | if (!pcol_copy) { | |
339 | ret = -ENOMEM; | |
340 | goto err; | |
341 | } | |
342 | ||
343 | *pcol_copy = *pcol; | |
06886a5a BH |
344 | ios->done = readpages_done; |
345 | ios->private = pcol_copy; | |
b916c5cd BH |
346 | |
347 | /* pages ownership was passed to pcol_copy */ | |
348 | _pcol_reset(pcol); | |
349 | ||
350 | ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol); | |
351 | if (unlikely(ret)) | |
352 | goto err; | |
353 | ||
354 | EXOFS_DBGMSG2("read_exec(0x%lx) offset=0x%llx length=0x%llx\n", | |
355 | pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length)); | |
356 | ||
8ff660ab | 357 | ret = ore_read(ios); |
beaec07b BH |
358 | if (unlikely(ret)) |
359 | goto err; | |
360 | ||
361 | atomic_inc(&pcol->sbi->s_curr_pending); | |
362 | ||
beaec07b BH |
363 | return 0; |
364 | ||
365 | err: | |
7aebf410 | 366 | if (!pcol->read_4_write) |
beaec07b | 367 | _unlock_pcol_pages(pcol, ret, READ); |
06886a5a BH |
368 | |
369 | pcol_free(pcol); | |
b76a3f93 | 370 | |
beaec07b | 371 | kfree(pcol_copy); |
beaec07b BH |
372 | return ret; |
373 | } | |
374 | ||
375 | /* readpage_strip is called either directly from readpage() or by the VFS from | |
376 | * within read_cache_pages(), to add one more page to be read. It will try to | |
377 | * collect as many contiguous pages as posible. If a discontinuity is | |
378 | * encountered, or it runs out of resources, it will submit the previous segment | |
379 | * and will start a new collection. Eventually caller must submit the last | |
380 | * segment if present. | |
381 | */ | |
382 | static int readpage_strip(void *data, struct page *page) | |
383 | { | |
384 | struct page_collect *pcol = data; | |
385 | struct inode *inode = pcol->inode; | |
386 | struct exofs_i_info *oi = exofs_i(inode); | |
387 | loff_t i_size = i_size_read(inode); | |
388 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
389 | size_t len; | |
390 | int ret; | |
391 | ||
0e8d96dd KC |
392 | BUG_ON(!PageLocked(page)); |
393 | ||
beaec07b BH |
394 | /* FIXME: Just for debugging, will be removed */ |
395 | if (PageUptodate(page)) | |
396 | EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino, | |
397 | page->index); | |
398 | ||
dd296619 BH |
399 | pcol->that_locked_page = page; |
400 | ||
beaec07b BH |
401 | if (page->index < end_index) |
402 | len = PAGE_CACHE_SIZE; | |
403 | else if (page->index == end_index) | |
404 | len = i_size & ~PAGE_CACHE_MASK; | |
405 | else | |
406 | len = 0; | |
407 | ||
408 | if (!len || !obj_created(oi)) { | |
409 | /* this will be out of bounds, or doesn't exist yet. | |
410 | * Current page is cleared and the request is split | |
411 | */ | |
412 | clear_highpage(page); | |
413 | ||
414 | SetPageUptodate(page); | |
415 | if (PageError(page)) | |
416 | ClearPageError(page); | |
417 | ||
f17b1f9f BH |
418 | if (!pcol->read_4_write) |
419 | unlock_page(page); | |
a8f1418f BH |
420 | EXOFS_DBGMSG("readpage_strip(0x%lx) empty page len=%zx " |
421 | "read_4_write=%d index=0x%lx end_index=0x%lx " | |
422 | "splitting\n", inode->i_ino, len, | |
423 | pcol->read_4_write, page->index, end_index); | |
beaec07b | 424 | |
7aebf410 | 425 | return read_exec(pcol); |
beaec07b BH |
426 | } |
427 | ||
428 | try_again: | |
429 | ||
430 | if (unlikely(pcol->pg_first == -1)) { | |
431 | pcol->pg_first = page->index; | |
432 | } else if (unlikely((pcol->pg_first + pcol->nr_pages) != | |
433 | page->index)) { | |
434 | /* Discontinuity detected, split the request */ | |
7aebf410 | 435 | ret = read_exec(pcol); |
beaec07b BH |
436 | if (unlikely(ret)) |
437 | goto fail; | |
438 | goto try_again; | |
439 | } | |
440 | ||
86093aaf | 441 | if (!pcol->pages) { |
beaec07b BH |
442 | ret = pcol_try_alloc(pcol); |
443 | if (unlikely(ret)) | |
444 | goto fail; | |
445 | } | |
446 | ||
447 | if (len != PAGE_CACHE_SIZE) | |
448 | zero_user(page, len, PAGE_CACHE_SIZE - len); | |
449 | ||
fe33cc1e | 450 | EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n", |
beaec07b BH |
451 | inode->i_ino, page->index, len); |
452 | ||
453 | ret = pcol_add_page(pcol, page, len); | |
454 | if (ret) { | |
fe33cc1e | 455 | EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p " |
beaec07b BH |
456 | "this_len=0x%zx nr_pages=%u length=0x%lx\n", |
457 | page, len, pcol->nr_pages, pcol->length); | |
458 | ||
459 | /* split the request, and start again with current page */ | |
7aebf410 | 460 | ret = read_exec(pcol); |
beaec07b BH |
461 | if (unlikely(ret)) |
462 | goto fail; | |
463 | ||
464 | goto try_again; | |
465 | } | |
466 | ||
467 | return 0; | |
468 | ||
469 | fail: | |
470 | /* SetPageError(page); ??? */ | |
471 | unlock_page(page); | |
472 | return ret; | |
473 | } | |
474 | ||
475 | static int exofs_readpages(struct file *file, struct address_space *mapping, | |
476 | struct list_head *pages, unsigned nr_pages) | |
477 | { | |
478 | struct page_collect pcol; | |
479 | int ret; | |
480 | ||
481 | _pcol_init(&pcol, nr_pages, mapping->host); | |
482 | ||
483 | ret = read_cache_pages(mapping, pages, readpage_strip, &pcol); | |
484 | if (ret) { | |
485 | EXOFS_ERR("read_cache_pages => %d\n", ret); | |
486 | return ret; | |
487 | } | |
488 | ||
b916c5cd BH |
489 | ret = read_exec(&pcol); |
490 | if (unlikely(ret)) | |
491 | return ret; | |
492 | ||
7aebf410 | 493 | return read_exec(&pcol); |
beaec07b BH |
494 | } |
495 | ||
7aebf410 | 496 | static int _readpage(struct page *page, bool read_4_write) |
beaec07b BH |
497 | { |
498 | struct page_collect pcol; | |
499 | int ret; | |
500 | ||
501 | _pcol_init(&pcol, 1, page->mapping->host); | |
502 | ||
7aebf410 | 503 | pcol.read_4_write = read_4_write; |
beaec07b BH |
504 | ret = readpage_strip(&pcol, page); |
505 | if (ret) { | |
506 | EXOFS_ERR("_readpage => %d\n", ret); | |
507 | return ret; | |
508 | } | |
509 | ||
7aebf410 | 510 | return read_exec(&pcol); |
beaec07b BH |
511 | } |
512 | ||
513 | /* | |
514 | * We don't need the file | |
515 | */ | |
516 | static int exofs_readpage(struct file *file, struct page *page) | |
517 | { | |
518 | return _readpage(page, false); | |
519 | } | |
520 | ||
06886a5a | 521 | /* Callback for osd_write. All writes are asynchronous */ |
8ff660ab | 522 | static void writepages_done(struct ore_io_state *ios, void *p) |
beaec07b BH |
523 | { |
524 | struct page_collect *pcol = p; | |
beaec07b | 525 | int i; |
beaec07b BH |
526 | u64 good_bytes; |
527 | u64 length = 0; | |
4b46c9f5 | 528 | int ret = ore_check_io(ios, NULL); |
beaec07b | 529 | |
beaec07b BH |
530 | atomic_dec(&pcol->sbi->s_curr_pending); |
531 | ||
154a9300 | 532 | if (likely(!ret)) { |
beaec07b | 533 | good_bytes = pcol->length; |
154a9300 BH |
534 | ret = PAGE_WAS_NOT_IN_IO; |
535 | } else { | |
4b46c9f5 | 536 | good_bytes = 0; |
154a9300 | 537 | } |
beaec07b | 538 | |
34ce4e7c | 539 | EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx" |
beaec07b BH |
540 | " length=0x%lx nr_pages=%u\n", |
541 | pcol->inode->i_ino, _LLU(good_bytes), pcol->length, | |
542 | pcol->nr_pages); | |
543 | ||
86093aaf BH |
544 | for (i = 0; i < pcol->nr_pages; i++) { |
545 | struct page *page = pcol->pages[i]; | |
beaec07b BH |
546 | struct inode *inode = page->mapping->host; |
547 | int page_stat; | |
548 | ||
549 | if (inode != pcol->inode) | |
550 | continue; /* osd might add more pages to a bio */ | |
551 | ||
552 | if (likely(length < good_bytes)) | |
553 | page_stat = 0; | |
554 | else | |
555 | page_stat = ret; | |
556 | ||
557 | update_write_page(page, page_stat); | |
558 | unlock_page(page); | |
fe33cc1e | 559 | EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n", |
beaec07b BH |
560 | inode->i_ino, page->index, page_stat); |
561 | ||
86093aaf | 562 | length += PAGE_SIZE; |
beaec07b BH |
563 | } |
564 | ||
565 | pcol_free(pcol); | |
566 | kfree(pcol); | |
34ce4e7c | 567 | EXOFS_DBGMSG2("writepages_done END\n"); |
beaec07b BH |
568 | } |
569 | ||
dd296619 BH |
570 | static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate) |
571 | { | |
572 | struct page_collect *pcol = priv; | |
573 | pgoff_t index = offset / PAGE_SIZE; | |
574 | ||
575 | if (!pcol->that_locked_page || | |
576 | (pcol->that_locked_page->index != index)) { | |
577 | struct page *page = find_get_page(pcol->inode->i_mapping, index); | |
578 | ||
579 | if (!page) { | |
580 | page = find_or_create_page(pcol->inode->i_mapping, | |
581 | index, GFP_NOFS); | |
582 | if (unlikely(!page)) { | |
583 | EXOFS_DBGMSG("grab_cache_page Failed " | |
584 | "index=0x%llx\n", _LLU(index)); | |
585 | return NULL; | |
586 | } | |
587 | unlock_page(page); | |
588 | } | |
589 | if (PageDirty(page) || PageWriteback(page)) | |
590 | *uptodate = true; | |
591 | else | |
592 | *uptodate = PageUptodate(page); | |
593 | EXOFS_DBGMSG("index=0x%lx uptodate=%d\n", index, *uptodate); | |
594 | return page; | |
595 | } else { | |
596 | EXOFS_DBGMSG("YES that_locked_page index=0x%lx\n", | |
597 | pcol->that_locked_page->index); | |
598 | *uptodate = true; | |
599 | return pcol->that_locked_page; | |
600 | } | |
601 | } | |
602 | ||
603 | static void __r4w_put_page(void *priv, struct page *page) | |
604 | { | |
605 | struct page_collect *pcol = priv; | |
606 | ||
607 | if (pcol->that_locked_page != page) { | |
608 | EXOFS_DBGMSG("index=0x%lx\n", page->index); | |
609 | page_cache_release(page); | |
610 | return; | |
611 | } | |
612 | EXOFS_DBGMSG("that_locked_page index=0x%lx\n", page->index); | |
613 | } | |
614 | ||
615 | static const struct _ore_r4w_op _r4w_op = { | |
616 | .get_page = &__r4w_get_page, | |
617 | .put_page = &__r4w_put_page, | |
618 | }; | |
619 | ||
beaec07b BH |
620 | static int write_exec(struct page_collect *pcol) |
621 | { | |
622 | struct exofs_i_info *oi = exofs_i(pcol->inode); | |
8ff660ab | 623 | struct ore_io_state *ios; |
beaec07b | 624 | struct page_collect *pcol_copy = NULL; |
beaec07b BH |
625 | int ret; |
626 | ||
86093aaf | 627 | if (!pcol->pages) |
beaec07b BH |
628 | return 0; |
629 | ||
e1042ba0 | 630 | BUG_ON(pcol->ios); |
5bf696da | 631 | ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false, |
e1042ba0 BH |
632 | pcol->pg_first << PAGE_CACHE_SHIFT, |
633 | pcol->length, &pcol->ios); | |
e1042ba0 BH |
634 | if (unlikely(ret)) |
635 | goto err; | |
636 | ||
beaec07b BH |
637 | pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL); |
638 | if (!pcol_copy) { | |
571f7f46 | 639 | EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n"); |
beaec07b BH |
640 | ret = -ENOMEM; |
641 | goto err; | |
642 | } | |
643 | ||
644 | *pcol_copy = *pcol; | |
645 | ||
e1042ba0 | 646 | ios = pcol->ios; |
86093aaf | 647 | ios->pages = pcol_copy->pages; |
06886a5a | 648 | ios->done = writepages_done; |
dd296619 | 649 | ios->r4w = &_r4w_op; |
06886a5a BH |
650 | ios->private = pcol_copy; |
651 | ||
b916c5cd BH |
652 | /* pages ownership was passed to pcol_copy */ |
653 | _pcol_reset(pcol); | |
654 | ||
655 | ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol); | |
656 | if (unlikely(ret)) | |
657 | goto err; | |
658 | ||
659 | EXOFS_DBGMSG2("write_exec(0x%lx) offset=0x%llx length=0x%llx\n", | |
660 | pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length)); | |
661 | ||
8ff660ab | 662 | ret = ore_write(ios); |
beaec07b | 663 | if (unlikely(ret)) { |
8ff660ab | 664 | EXOFS_ERR("write_exec: ore_write() Failed\n"); |
beaec07b BH |
665 | goto err; |
666 | } | |
667 | ||
668 | atomic_inc(&pcol->sbi->s_curr_pending); | |
beaec07b BH |
669 | return 0; |
670 | ||
671 | err: | |
672 | _unlock_pcol_pages(pcol, ret, WRITE); | |
06886a5a | 673 | pcol_free(pcol); |
beaec07b | 674 | kfree(pcol_copy); |
06886a5a | 675 | |
beaec07b BH |
676 | return ret; |
677 | } | |
678 | ||
679 | /* writepage_strip is called either directly from writepage() or by the VFS from | |
680 | * within write_cache_pages(), to add one more page to be written to storage. | |
681 | * It will try to collect as many contiguous pages as possible. If a | |
682 | * discontinuity is encountered or it runs out of resources it will submit the | |
683 | * previous segment and will start a new collection. | |
684 | * Eventually caller must submit the last segment if present. | |
685 | */ | |
686 | static int writepage_strip(struct page *page, | |
687 | struct writeback_control *wbc_unused, void *data) | |
688 | { | |
689 | struct page_collect *pcol = data; | |
690 | struct inode *inode = pcol->inode; | |
691 | struct exofs_i_info *oi = exofs_i(inode); | |
692 | loff_t i_size = i_size_read(inode); | |
693 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
694 | size_t len; | |
695 | int ret; | |
696 | ||
697 | BUG_ON(!PageLocked(page)); | |
698 | ||
699 | ret = wait_obj_created(oi); | |
700 | if (unlikely(ret)) | |
701 | goto fail; | |
702 | ||
703 | if (page->index < end_index) | |
704 | /* in this case, the page is within the limits of the file */ | |
705 | len = PAGE_CACHE_SIZE; | |
706 | else { | |
707 | len = i_size & ~PAGE_CACHE_MASK; | |
708 | ||
709 | if (page->index > end_index || !len) { | |
710 | /* in this case, the page is outside the limits | |
711 | * (truncate in progress) | |
712 | */ | |
713 | ret = write_exec(pcol); | |
714 | if (unlikely(ret)) | |
715 | goto fail; | |
716 | if (PageError(page)) | |
717 | ClearPageError(page); | |
718 | unlock_page(page); | |
06886a5a BH |
719 | EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) " |
720 | "outside the limits\n", | |
721 | inode->i_ino, page->index); | |
beaec07b BH |
722 | return 0; |
723 | } | |
724 | } | |
725 | ||
726 | try_again: | |
727 | ||
728 | if (unlikely(pcol->pg_first == -1)) { | |
729 | pcol->pg_first = page->index; | |
730 | } else if (unlikely((pcol->pg_first + pcol->nr_pages) != | |
731 | page->index)) { | |
732 | /* Discontinuity detected, split the request */ | |
733 | ret = write_exec(pcol); | |
734 | if (unlikely(ret)) | |
735 | goto fail; | |
06886a5a BH |
736 | |
737 | EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n", | |
738 | inode->i_ino, page->index); | |
beaec07b BH |
739 | goto try_again; |
740 | } | |
741 | ||
86093aaf | 742 | if (!pcol->pages) { |
beaec07b BH |
743 | ret = pcol_try_alloc(pcol); |
744 | if (unlikely(ret)) | |
745 | goto fail; | |
746 | } | |
747 | ||
fe33cc1e | 748 | EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n", |
beaec07b BH |
749 | inode->i_ino, page->index, len); |
750 | ||
751 | ret = pcol_add_page(pcol, page, len); | |
752 | if (unlikely(ret)) { | |
34ce4e7c | 753 | EXOFS_DBGMSG2("Failed pcol_add_page " |
beaec07b BH |
754 | "nr_pages=%u total_length=0x%lx\n", |
755 | pcol->nr_pages, pcol->length); | |
756 | ||
757 | /* split the request, next loop will start again */ | |
758 | ret = write_exec(pcol); | |
759 | if (unlikely(ret)) { | |
571f7f46 | 760 | EXOFS_DBGMSG("write_exec failed => %d", ret); |
beaec07b BH |
761 | goto fail; |
762 | } | |
763 | ||
764 | goto try_again; | |
765 | } | |
766 | ||
767 | BUG_ON(PageWriteback(page)); | |
768 | set_page_writeback(page); | |
769 | ||
770 | return 0; | |
771 | ||
772 | fail: | |
06886a5a BH |
773 | EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n", |
774 | inode->i_ino, page->index, ret); | |
beaec07b BH |
775 | set_bit(AS_EIO, &page->mapping->flags); |
776 | unlock_page(page); | |
777 | return ret; | |
778 | } | |
779 | ||
780 | static int exofs_writepages(struct address_space *mapping, | |
781 | struct writeback_control *wbc) | |
782 | { | |
783 | struct page_collect pcol; | |
784 | long start, end, expected_pages; | |
785 | int ret; | |
786 | ||
787 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | |
788 | end = (wbc->range_end == LLONG_MAX) ? | |
789 | start + mapping->nrpages : | |
790 | wbc->range_end >> PAGE_CACHE_SHIFT; | |
791 | ||
792 | if (start || end) | |
06886a5a | 793 | expected_pages = end - start + 1; |
beaec07b BH |
794 | else |
795 | expected_pages = mapping->nrpages; | |
796 | ||
06886a5a BH |
797 | if (expected_pages < 32L) |
798 | expected_pages = 32L; | |
799 | ||
34ce4e7c | 800 | EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx " |
06886a5a | 801 | "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n", |
beaec07b | 802 | mapping->host->i_ino, wbc->range_start, wbc->range_end, |
06886a5a | 803 | mapping->nrpages, start, end, expected_pages); |
beaec07b BH |
804 | |
805 | _pcol_init(&pcol, expected_pages, mapping->host); | |
806 | ||
807 | ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol); | |
b916c5cd | 808 | if (unlikely(ret)) { |
beaec07b BH |
809 | EXOFS_ERR("write_cache_pages => %d\n", ret); |
810 | return ret; | |
811 | } | |
812 | ||
b916c5cd BH |
813 | ret = write_exec(&pcol); |
814 | if (unlikely(ret)) | |
815 | return ret; | |
816 | ||
817 | if (wbc->sync_mode == WB_SYNC_ALL) { | |
818 | return write_exec(&pcol); /* pump the last reminder */ | |
819 | } else if (pcol.nr_pages) { | |
820 | /* not SYNC let the reminder join the next writeout */ | |
821 | unsigned i; | |
822 | ||
823 | for (i = 0; i < pcol.nr_pages; i++) { | |
824 | struct page *page = pcol.pages[i]; | |
825 | ||
826 | end_page_writeback(page); | |
827 | set_page_dirty(page); | |
828 | unlock_page(page); | |
829 | } | |
830 | } | |
831 | return 0; | |
beaec07b BH |
832 | } |
833 | ||
dd296619 | 834 | /* |
beaec07b BH |
835 | static int exofs_writepage(struct page *page, struct writeback_control *wbc) |
836 | { | |
837 | struct page_collect pcol; | |
838 | int ret; | |
839 | ||
840 | _pcol_init(&pcol, 1, page->mapping->host); | |
841 | ||
842 | ret = writepage_strip(page, NULL, &pcol); | |
843 | if (ret) { | |
844 | EXOFS_ERR("exofs_writepage => %d\n", ret); | |
845 | return ret; | |
846 | } | |
847 | ||
848 | return write_exec(&pcol); | |
849 | } | |
dd296619 | 850 | */ |
2f246fd0 BH |
851 | /* i_mutex held using inode->i_size directly */ |
852 | static void _write_failed(struct inode *inode, loff_t to) | |
853 | { | |
854 | if (to > inode->i_size) | |
855 | truncate_pagecache(inode, to, inode->i_size); | |
856 | } | |
857 | ||
beaec07b BH |
858 | int exofs_write_begin(struct file *file, struct address_space *mapping, |
859 | loff_t pos, unsigned len, unsigned flags, | |
860 | struct page **pagep, void **fsdata) | |
861 | { | |
862 | int ret = 0; | |
863 | struct page *page; | |
864 | ||
865 | page = *pagep; | |
866 | if (page == NULL) { | |
867 | ret = simple_write_begin(file, mapping, pos, len, flags, pagep, | |
868 | fsdata); | |
869 | if (ret) { | |
571f7f46 | 870 | EXOFS_DBGMSG("simple_write_begin failed\n"); |
2f246fd0 | 871 | goto out; |
beaec07b BH |
872 | } |
873 | ||
874 | page = *pagep; | |
875 | } | |
876 | ||
877 | /* read modify write */ | |
878 | if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) { | |
a8f1418f BH |
879 | loff_t i_size = i_size_read(mapping->host); |
880 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
881 | size_t rlen; | |
882 | ||
883 | if (page->index < end_index) | |
884 | rlen = PAGE_CACHE_SIZE; | |
885 | else if (page->index == end_index) | |
886 | rlen = i_size & ~PAGE_CACHE_MASK; | |
887 | else | |
888 | rlen = 0; | |
889 | ||
890 | if (!rlen) { | |
891 | clear_highpage(page); | |
892 | SetPageUptodate(page); | |
893 | goto out; | |
894 | } | |
895 | ||
beaec07b BH |
896 | ret = _readpage(page, true); |
897 | if (ret) { | |
898 | /*SetPageError was done by _readpage. Is it ok?*/ | |
899 | unlock_page(page); | |
a8f1418f | 900 | EXOFS_DBGMSG("__readpage failed\n"); |
beaec07b BH |
901 | } |
902 | } | |
2f246fd0 BH |
903 | out: |
904 | if (unlikely(ret)) | |
905 | _write_failed(mapping->host, pos + len); | |
beaec07b BH |
906 | |
907 | return ret; | |
908 | } | |
909 | ||
910 | static int exofs_write_begin_export(struct file *file, | |
911 | struct address_space *mapping, | |
912 | loff_t pos, unsigned len, unsigned flags, | |
913 | struct page **pagep, void **fsdata) | |
914 | { | |
915 | *pagep = NULL; | |
916 | ||
917 | return exofs_write_begin(file, mapping, pos, len, flags, pagep, | |
918 | fsdata); | |
919 | } | |
920 | ||
efd124b9 BH |
921 | static int exofs_write_end(struct file *file, struct address_space *mapping, |
922 | loff_t pos, unsigned len, unsigned copied, | |
923 | struct page *page, void *fsdata) | |
924 | { | |
925 | struct inode *inode = mapping->host; | |
926 | /* According to comment in simple_write_end i_mutex is held */ | |
927 | loff_t i_size = inode->i_size; | |
928 | int ret; | |
929 | ||
930 | ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata); | |
2f246fd0 BH |
931 | if (unlikely(ret)) |
932 | _write_failed(inode, pos + len); | |
933 | ||
934 | /* TODO: once simple_write_end marks inode dirty remove */ | |
efd124b9 BH |
935 | if (i_size != inode->i_size) |
936 | mark_inode_dirty(inode); | |
937 | return ret; | |
938 | } | |
939 | ||
200b0700 BH |
940 | static int exofs_releasepage(struct page *page, gfp_t gfp) |
941 | { | |
942 | EXOFS_DBGMSG("page 0x%lx\n", page->index); | |
943 | WARN_ON(1); | |
85dc7878 | 944 | return 0; |
200b0700 BH |
945 | } |
946 | ||
947 | static void exofs_invalidatepage(struct page *page, unsigned long offset) | |
948 | { | |
85dc7878 | 949 | EXOFS_DBGMSG("page 0x%lx offset 0x%lx\n", page->index, offset); |
200b0700 | 950 | WARN_ON(1); |
200b0700 BH |
951 | } |
952 | ||
beaec07b BH |
953 | const struct address_space_operations exofs_aops = { |
954 | .readpage = exofs_readpage, | |
955 | .readpages = exofs_readpages, | |
dd296619 | 956 | .writepage = NULL, |
beaec07b BH |
957 | .writepages = exofs_writepages, |
958 | .write_begin = exofs_write_begin_export, | |
efd124b9 | 959 | .write_end = exofs_write_end, |
200b0700 BH |
960 | .releasepage = exofs_releasepage, |
961 | .set_page_dirty = __set_page_dirty_nobuffers, | |
962 | .invalidatepage = exofs_invalidatepage, | |
963 | ||
964 | /* Not implemented Yet */ | |
965 | .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */ | |
966 | .direct_IO = NULL, /* TODO: Should be trivial to do */ | |
967 | ||
968 | /* With these NULL has special meaning or default is not exported */ | |
200b0700 BH |
969 | .get_xip_mem = NULL, |
970 | .migratepage = NULL, | |
971 | .launder_page = NULL, | |
972 | .is_partially_uptodate = NULL, | |
973 | .error_remove_page = NULL, | |
beaec07b BH |
974 | }; |
975 | ||
e8062719 BH |
976 | /****************************************************************************** |
977 | * INODE OPERATIONS | |
978 | *****************************************************************************/ | |
979 | ||
980 | /* | |
981 | * Test whether an inode is a fast symlink. | |
982 | */ | |
983 | static inline int exofs_inode_is_fast_symlink(struct inode *inode) | |
984 | { | |
985 | struct exofs_i_info *oi = exofs_i(inode); | |
986 | ||
987 | return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0); | |
988 | } | |
989 | ||
2f246fd0 | 990 | static int _do_truncate(struct inode *inode, loff_t newsize) |
06886a5a BH |
991 | { |
992 | struct exofs_i_info *oi = exofs_i(inode); | |
9e9db456 | 993 | struct exofs_sb_info *sbi = inode->i_sb->s_fs_info; |
06886a5a BH |
994 | int ret; |
995 | ||
996 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
997 | ||
5bf696da | 998 | ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize); |
2f246fd0 BH |
999 | if (likely(!ret)) |
1000 | truncate_setsize(inode, newsize); | |
06886a5a | 1001 | |
2f246fd0 BH |
1002 | EXOFS_DBGMSG("(0x%lx) size=0x%llx ret=>%d\n", |
1003 | inode->i_ino, newsize, ret); | |
06886a5a BH |
1004 | return ret; |
1005 | } | |
1006 | ||
e8062719 | 1007 | /* |
2f246fd0 BH |
1008 | * Set inode attributes - update size attribute on OSD if needed, |
1009 | * otherwise just call generic functions. | |
e8062719 BH |
1010 | */ |
1011 | int exofs_setattr(struct dentry *dentry, struct iattr *iattr) | |
1012 | { | |
1013 | struct inode *inode = dentry->d_inode; | |
1014 | int error; | |
1015 | ||
2f246fd0 BH |
1016 | /* if we are about to modify an object, and it hasn't been |
1017 | * created yet, wait | |
1018 | */ | |
1019 | error = wait_obj_created(exofs_i(inode)); | |
1020 | if (unlikely(error)) | |
1021 | return error; | |
1022 | ||
e8062719 | 1023 | error = inode_change_ok(inode, iattr); |
2f246fd0 | 1024 | if (unlikely(error)) |
e8062719 BH |
1025 | return error; |
1026 | ||
1025774c CH |
1027 | if ((iattr->ia_valid & ATTR_SIZE) && |
1028 | iattr->ia_size != i_size_read(inode)) { | |
2f246fd0 BH |
1029 | error = _do_truncate(inode, iattr->ia_size); |
1030 | if (unlikely(error)) | |
1025774c CH |
1031 | return error; |
1032 | } | |
1033 | ||
1034 | setattr_copy(inode, iattr); | |
1035 | mark_inode_dirty(inode); | |
1036 | return 0; | |
e8062719 | 1037 | } |
e6af00f1 | 1038 | |
d9c740d2 BH |
1039 | static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF( |
1040 | EXOFS_APAGE_FS_DATA, | |
1041 | EXOFS_ATTR_INODE_FILE_LAYOUT, | |
1042 | 0); | |
1043 | static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF( | |
1044 | EXOFS_APAGE_FS_DATA, | |
1045 | EXOFS_ATTR_INODE_DIR_LAYOUT, | |
1046 | 0); | |
1047 | ||
e6af00f1 | 1048 | /* |
5d952b83 BH |
1049 | * Read the Linux inode info from the OSD, and return it as is. In exofs the |
1050 | * inode info is in an application specific page/attribute of the osd-object. | |
e6af00f1 BH |
1051 | */ |
1052 | static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi, | |
5d952b83 | 1053 | struct exofs_fcb *inode) |
e6af00f1 BH |
1054 | { |
1055 | struct exofs_sb_info *sbi = sb->s_fs_info; | |
d9c740d2 BH |
1056 | struct osd_attr attrs[] = { |
1057 | [0] = g_attr_inode_data, | |
1058 | [1] = g_attr_inode_file_layout, | |
1059 | [2] = g_attr_inode_dir_layout, | |
d9c740d2 | 1060 | }; |
8ff660ab | 1061 | struct ore_io_state *ios; |
d9c740d2 | 1062 | struct exofs_on_disk_inode_layout *layout; |
e6af00f1 BH |
1063 | int ret; |
1064 | ||
5bf696da | 1065 | ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios); |
06886a5a | 1066 | if (unlikely(ret)) { |
8ff660ab | 1067 | EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__); |
06886a5a | 1068 | return ret; |
e6af00f1 | 1069 | } |
e6af00f1 | 1070 | |
5bf696da BH |
1071 | attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs); |
1072 | attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs); | |
d9c740d2 | 1073 | |
06886a5a BH |
1074 | ios->in_attr = attrs; |
1075 | ios->in_attr_len = ARRAY_SIZE(attrs); | |
e6af00f1 | 1076 | |
8ff660ab | 1077 | ret = ore_read(ios); |
96391e2b BH |
1078 | if (unlikely(ret)) { |
1079 | EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n", | |
9e9db456 | 1080 | _LLU(oi->one_comp.obj.id), ret); |
96391e2b BH |
1081 | memset(inode, 0, sizeof(*inode)); |
1082 | inode->i_mode = 0040000 | (0777 & ~022); | |
1083 | /* If object is lost on target we might as well enable it's | |
1084 | * delete. | |
1085 | */ | |
1086 | if ((ret == -ENOENT) || (ret == -EINVAL)) | |
1087 | ret = 0; | |
e6af00f1 | 1088 | goto out; |
96391e2b | 1089 | } |
e6af00f1 | 1090 | |
06886a5a | 1091 | ret = extract_attr_from_ios(ios, &attrs[0]); |
e6af00f1 | 1092 | if (ret) { |
06886a5a | 1093 | EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__); |
e6af00f1 BH |
1094 | goto out; |
1095 | } | |
06886a5a BH |
1096 | WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE); |
1097 | memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE); | |
e6af00f1 | 1098 | |
06886a5a | 1099 | ret = extract_attr_from_ios(ios, &attrs[1]); |
d9c740d2 BH |
1100 | if (ret) { |
1101 | EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__); | |
1102 | goto out; | |
1103 | } | |
1104 | if (attrs[1].len) { | |
1105 | layout = attrs[1].val_ptr; | |
1106 | if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) { | |
1107 | EXOFS_ERR("%s: unsupported files layout %d\n", | |
1108 | __func__, layout->gen_func); | |
1109 | ret = -ENOTSUPP; | |
1110 | goto out; | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | ret = extract_attr_from_ios(ios, &attrs[2]); | |
1115 | if (ret) { | |
1116 | EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__); | |
1117 | goto out; | |
1118 | } | |
1119 | if (attrs[2].len) { | |
1120 | layout = attrs[2].val_ptr; | |
1121 | if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) { | |
1122 | EXOFS_ERR("%s: unsupported meta-data layout %d\n", | |
1123 | __func__, layout->gen_func); | |
1124 | ret = -ENOTSUPP; | |
1125 | goto out; | |
1126 | } | |
1127 | } | |
1128 | ||
e6af00f1 | 1129 | out: |
8ff660ab | 1130 | ore_put_io_state(ios); |
e6af00f1 BH |
1131 | return ret; |
1132 | } | |
1133 | ||
9cfdc7aa BH |
1134 | static void __oi_init(struct exofs_i_info *oi) |
1135 | { | |
1136 | init_waitqueue_head(&oi->i_wq); | |
1137 | oi->i_flags = 0; | |
1138 | } | |
e6af00f1 BH |
1139 | /* |
1140 | * Fill in an inode read from the OSD and set it up for use | |
1141 | */ | |
1142 | struct inode *exofs_iget(struct super_block *sb, unsigned long ino) | |
1143 | { | |
1144 | struct exofs_i_info *oi; | |
1145 | struct exofs_fcb fcb; | |
1146 | struct inode *inode; | |
e6af00f1 BH |
1147 | int ret; |
1148 | ||
1149 | inode = iget_locked(sb, ino); | |
1150 | if (!inode) | |
1151 | return ERR_PTR(-ENOMEM); | |
1152 | if (!(inode->i_state & I_NEW)) | |
1153 | return inode; | |
1154 | oi = exofs_i(inode); | |
9cfdc7aa | 1155 | __oi_init(oi); |
5bf696da | 1156 | exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info, |
9e9db456 | 1157 | exofs_oi_objno(oi)); |
e6af00f1 BH |
1158 | |
1159 | /* read the inode from the osd */ | |
5d952b83 | 1160 | ret = exofs_get_inode(sb, oi, &fcb); |
e6af00f1 BH |
1161 | if (ret) |
1162 | goto bad_inode; | |
1163 | ||
e6af00f1 BH |
1164 | set_obj_created(oi); |
1165 | ||
1166 | /* copy stuff from on-disk struct to in-memory struct */ | |
1167 | inode->i_mode = le16_to_cpu(fcb.i_mode); | |
1168 | inode->i_uid = le32_to_cpu(fcb.i_uid); | |
1169 | inode->i_gid = le32_to_cpu(fcb.i_gid); | |
bfe86848 | 1170 | set_nlink(inode, le16_to_cpu(fcb.i_links_count)); |
e6af00f1 BH |
1171 | inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime); |
1172 | inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime); | |
1173 | inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime); | |
1174 | inode->i_ctime.tv_nsec = | |
1175 | inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0; | |
1176 | oi->i_commit_size = le64_to_cpu(fcb.i_size); | |
1177 | i_size_write(inode, oi->i_commit_size); | |
1178 | inode->i_blkbits = EXOFS_BLKSHIFT; | |
1179 | inode->i_generation = le32_to_cpu(fcb.i_generation); | |
1180 | ||
e6af00f1 BH |
1181 | oi->i_dir_start_lookup = 0; |
1182 | ||
1183 | if ((inode->i_nlink == 0) && (inode->i_mode == 0)) { | |
1184 | ret = -ESTALE; | |
1185 | goto bad_inode; | |
1186 | } | |
1187 | ||
1188 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
1189 | if (fcb.i_data[0]) | |
1190 | inode->i_rdev = | |
1191 | old_decode_dev(le32_to_cpu(fcb.i_data[0])); | |
1192 | else | |
1193 | inode->i_rdev = | |
1194 | new_decode_dev(le32_to_cpu(fcb.i_data[1])); | |
1195 | } else { | |
1196 | memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data)); | |
1197 | } | |
1198 | ||
66cd6cad | 1199 | inode->i_mapping->backing_dev_info = sb->s_bdi; |
e6af00f1 BH |
1200 | if (S_ISREG(inode->i_mode)) { |
1201 | inode->i_op = &exofs_file_inode_operations; | |
1202 | inode->i_fop = &exofs_file_operations; | |
1203 | inode->i_mapping->a_ops = &exofs_aops; | |
1204 | } else if (S_ISDIR(inode->i_mode)) { | |
1205 | inode->i_op = &exofs_dir_inode_operations; | |
1206 | inode->i_fop = &exofs_dir_operations; | |
1207 | inode->i_mapping->a_ops = &exofs_aops; | |
1208 | } else if (S_ISLNK(inode->i_mode)) { | |
1209 | if (exofs_inode_is_fast_symlink(inode)) | |
1210 | inode->i_op = &exofs_fast_symlink_inode_operations; | |
1211 | else { | |
1212 | inode->i_op = &exofs_symlink_inode_operations; | |
1213 | inode->i_mapping->a_ops = &exofs_aops; | |
1214 | } | |
1215 | } else { | |
1216 | inode->i_op = &exofs_special_inode_operations; | |
1217 | if (fcb.i_data[0]) | |
1218 | init_special_inode(inode, inode->i_mode, | |
1219 | old_decode_dev(le32_to_cpu(fcb.i_data[0]))); | |
1220 | else | |
1221 | init_special_inode(inode, inode->i_mode, | |
1222 | new_decode_dev(le32_to_cpu(fcb.i_data[1]))); | |
1223 | } | |
1224 | ||
1225 | unlock_new_inode(inode); | |
1226 | return inode; | |
1227 | ||
1228 | bad_inode: | |
1229 | iget_failed(inode); | |
1230 | return ERR_PTR(ret); | |
1231 | } | |
1232 | ||
1233 | int __exofs_wait_obj_created(struct exofs_i_info *oi) | |
1234 | { | |
1235 | if (!obj_created(oi)) { | |
fe2fd9ed | 1236 | EXOFS_DBGMSG("!obj_created\n"); |
e6af00f1 BH |
1237 | BUG_ON(!obj_2bcreated(oi)); |
1238 | wait_event(oi->i_wq, obj_created(oi)); | |
fe2fd9ed | 1239 | EXOFS_DBGMSG("wait_event done\n"); |
e6af00f1 BH |
1240 | } |
1241 | return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0; | |
1242 | } | |
1cea312a | 1243 | |
e6af00f1 BH |
1244 | /* |
1245 | * Callback function from exofs_new_inode(). The important thing is that we | |
1246 | * set the obj_created flag so that other methods know that the object exists on | |
1247 | * the OSD. | |
1248 | */ | |
8ff660ab | 1249 | static void create_done(struct ore_io_state *ios, void *p) |
e6af00f1 BH |
1250 | { |
1251 | struct inode *inode = p; | |
1252 | struct exofs_i_info *oi = exofs_i(inode); | |
1253 | struct exofs_sb_info *sbi = inode->i_sb->s_fs_info; | |
1254 | int ret; | |
1255 | ||
8ff660ab BH |
1256 | ret = ore_check_io(ios, NULL); |
1257 | ore_put_io_state(ios); | |
06886a5a | 1258 | |
e6af00f1 BH |
1259 | atomic_dec(&sbi->s_curr_pending); |
1260 | ||
1261 | if (unlikely(ret)) { | |
571f7f46 | 1262 | EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx", |
9e9db456 BH |
1263 | _LLU(exofs_oi_objno(oi)), |
1264 | _LLU(oi->one_comp.obj.partition)); | |
06886a5a BH |
1265 | /*TODO: When FS is corrupted creation can fail, object already |
1266 | * exist. Get rid of this asynchronous creation, if exist | |
1267 | * increment the obj counter and try the next object. Until we | |
1268 | * succeed. All these dangling objects will be made into lost | |
1269 | * files by chkfs.exofs | |
1270 | */ | |
1271 | } | |
1272 | ||
1273 | set_obj_created(oi); | |
e6af00f1 | 1274 | |
e6af00f1 BH |
1275 | wake_up(&oi->i_wq); |
1276 | } | |
1277 | ||
1278 | /* | |
1279 | * Set up a new inode and create an object for it on the OSD | |
1280 | */ | |
bef41c26 | 1281 | struct inode *exofs_new_inode(struct inode *dir, umode_t mode) |
e6af00f1 | 1282 | { |
9e9db456 BH |
1283 | struct super_block *sb = dir->i_sb; |
1284 | struct exofs_sb_info *sbi = sb->s_fs_info; | |
e6af00f1 BH |
1285 | struct inode *inode; |
1286 | struct exofs_i_info *oi; | |
8ff660ab | 1287 | struct ore_io_state *ios; |
e6af00f1 BH |
1288 | int ret; |
1289 | ||
e6af00f1 BH |
1290 | inode = new_inode(sb); |
1291 | if (!inode) | |
1292 | return ERR_PTR(-ENOMEM); | |
1293 | ||
1294 | oi = exofs_i(inode); | |
9cfdc7aa | 1295 | __oi_init(oi); |
e6af00f1 | 1296 | |
e6af00f1 BH |
1297 | set_obj_2bcreated(oi); |
1298 | ||
66cd6cad | 1299 | inode->i_mapping->backing_dev_info = sb->s_bdi; |
e00117f1 | 1300 | inode_init_owner(inode, dir, mode); |
e6af00f1 BH |
1301 | inode->i_ino = sbi->s_nextid++; |
1302 | inode->i_blkbits = EXOFS_BLKSHIFT; | |
1303 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
1304 | oi->i_commit_size = inode->i_size = 0; | |
1305 | spin_lock(&sbi->s_next_gen_lock); | |
1306 | inode->i_generation = sbi->s_next_generation++; | |
1307 | spin_unlock(&sbi->s_next_gen_lock); | |
1308 | insert_inode_hash(inode); | |
1309 | ||
5bf696da | 1310 | exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info, |
9e9db456 | 1311 | exofs_oi_objno(oi)); |
1cea312a BH |
1312 | exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */ |
1313 | ||
e6af00f1 BH |
1314 | mark_inode_dirty(inode); |
1315 | ||
5bf696da | 1316 | ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios); |
06886a5a | 1317 | if (unlikely(ret)) { |
8ff660ab | 1318 | EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n"); |
06886a5a | 1319 | return ERR_PTR(ret); |
e6af00f1 BH |
1320 | } |
1321 | ||
06886a5a BH |
1322 | ios->done = create_done; |
1323 | ios->private = inode; | |
9e9db456 | 1324 | |
8ff660ab | 1325 | ret = ore_create(ios); |
e6af00f1 | 1326 | if (ret) { |
8ff660ab | 1327 | ore_put_io_state(ios); |
06886a5a | 1328 | return ERR_PTR(ret); |
e6af00f1 BH |
1329 | } |
1330 | atomic_inc(&sbi->s_curr_pending); | |
1331 | ||
1332 | return inode; | |
1333 | } | |
ba9e5e98 BH |
1334 | |
1335 | /* | |
1336 | * struct to pass two arguments to update_inode's callback | |
1337 | */ | |
1338 | struct updatei_args { | |
1339 | struct exofs_sb_info *sbi; | |
1340 | struct exofs_fcb fcb; | |
1341 | }; | |
1342 | ||
1343 | /* | |
1344 | * Callback function from exofs_update_inode(). | |
1345 | */ | |
8ff660ab | 1346 | static void updatei_done(struct ore_io_state *ios, void *p) |
ba9e5e98 BH |
1347 | { |
1348 | struct updatei_args *args = p; | |
1349 | ||
8ff660ab | 1350 | ore_put_io_state(ios); |
ba9e5e98 BH |
1351 | |
1352 | atomic_dec(&args->sbi->s_curr_pending); | |
1353 | ||
1354 | kfree(args); | |
1355 | } | |
1356 | ||
1357 | /* | |
1358 | * Write the inode to the OSD. Just fill up the struct, and set the attribute | |
1359 | * synchronously or asynchronously depending on the do_sync flag. | |
1360 | */ | |
1361 | static int exofs_update_inode(struct inode *inode, int do_sync) | |
1362 | { | |
1363 | struct exofs_i_info *oi = exofs_i(inode); | |
1364 | struct super_block *sb = inode->i_sb; | |
1365 | struct exofs_sb_info *sbi = sb->s_fs_info; | |
8ff660ab | 1366 | struct ore_io_state *ios; |
ba9e5e98 BH |
1367 | struct osd_attr attr; |
1368 | struct exofs_fcb *fcb; | |
1369 | struct updatei_args *args; | |
1370 | int ret; | |
1371 | ||
1372 | args = kzalloc(sizeof(*args), GFP_KERNEL); | |
34ce4e7c | 1373 | if (!args) { |
571f7f46 | 1374 | EXOFS_DBGMSG("Failed kzalloc of args\n"); |
ba9e5e98 | 1375 | return -ENOMEM; |
34ce4e7c | 1376 | } |
ba9e5e98 BH |
1377 | |
1378 | fcb = &args->fcb; | |
1379 | ||
1380 | fcb->i_mode = cpu_to_le16(inode->i_mode); | |
1381 | fcb->i_uid = cpu_to_le32(inode->i_uid); | |
1382 | fcb->i_gid = cpu_to_le32(inode->i_gid); | |
1383 | fcb->i_links_count = cpu_to_le16(inode->i_nlink); | |
1384 | fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); | |
1385 | fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec); | |
1386 | fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); | |
1387 | oi->i_commit_size = i_size_read(inode); | |
1388 | fcb->i_size = cpu_to_le64(oi->i_commit_size); | |
1389 | fcb->i_generation = cpu_to_le32(inode->i_generation); | |
1390 | ||
1391 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
1392 | if (old_valid_dev(inode->i_rdev)) { | |
1393 | fcb->i_data[0] = | |
1394 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | |
1395 | fcb->i_data[1] = 0; | |
1396 | } else { | |
1397 | fcb->i_data[0] = 0; | |
1398 | fcb->i_data[1] = | |
1399 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | |
1400 | fcb->i_data[2] = 0; | |
1401 | } | |
1402 | } else | |
1403 | memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data)); | |
1404 | ||
5bf696da | 1405 | ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios); |
06886a5a | 1406 | if (unlikely(ret)) { |
8ff660ab | 1407 | EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__); |
ba9e5e98 BH |
1408 | goto free_args; |
1409 | } | |
1410 | ||
ba9e5e98 BH |
1411 | attr = g_attr_inode_data; |
1412 | attr.val_ptr = fcb; | |
06886a5a BH |
1413 | ios->out_attr_len = 1; |
1414 | ios->out_attr = &attr; | |
ba9e5e98 | 1415 | |
fe2fd9ed | 1416 | wait_obj_created(oi); |
ba9e5e98 | 1417 | |
06886a5a | 1418 | if (!do_sync) { |
ba9e5e98 | 1419 | args->sbi = sbi; |
06886a5a BH |
1420 | ios->done = updatei_done; |
1421 | ios->private = args; | |
1422 | } | |
ba9e5e98 | 1423 | |
8ff660ab | 1424 | ret = ore_write(ios); |
06886a5a | 1425 | if (!do_sync && !ret) { |
ba9e5e98 BH |
1426 | atomic_inc(&sbi->s_curr_pending); |
1427 | goto out; /* deallocation in updatei_done */ | |
1428 | } | |
1429 | ||
8ff660ab | 1430 | ore_put_io_state(ios); |
ba9e5e98 BH |
1431 | free_args: |
1432 | kfree(args); | |
1433 | out: | |
34ce4e7c BH |
1434 | EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n", |
1435 | inode->i_ino, do_sync, ret); | |
ba9e5e98 BH |
1436 | return ret; |
1437 | } | |
1438 | ||
a9185b41 | 1439 | int exofs_write_inode(struct inode *inode, struct writeback_control *wbc) |
ba9e5e98 | 1440 | { |
97178b7b NP |
1441 | /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */ |
1442 | return exofs_update_inode(inode, 1); | |
ba9e5e98 BH |
1443 | } |
1444 | ||
1445 | /* | |
1446 | * Callback function from exofs_delete_inode() - don't have much cleaning up to | |
1447 | * do. | |
1448 | */ | |
8ff660ab | 1449 | static void delete_done(struct ore_io_state *ios, void *p) |
ba9e5e98 | 1450 | { |
06886a5a BH |
1451 | struct exofs_sb_info *sbi = p; |
1452 | ||
8ff660ab | 1453 | ore_put_io_state(ios); |
06886a5a | 1454 | |
ba9e5e98 BH |
1455 | atomic_dec(&sbi->s_curr_pending); |
1456 | } | |
1457 | ||
1458 | /* | |
1459 | * Called when the refcount of an inode reaches zero. We remove the object | |
1460 | * from the OSD here. We make sure the object was created before we try and | |
1461 | * delete it. | |
1462 | */ | |
4ec70c9b | 1463 | void exofs_evict_inode(struct inode *inode) |
ba9e5e98 BH |
1464 | { |
1465 | struct exofs_i_info *oi = exofs_i(inode); | |
1466 | struct super_block *sb = inode->i_sb; | |
1467 | struct exofs_sb_info *sbi = sb->s_fs_info; | |
8ff660ab | 1468 | struct ore_io_state *ios; |
ba9e5e98 BH |
1469 | int ret; |
1470 | ||
1471 | truncate_inode_pages(&inode->i_data, 0); | |
1472 | ||
2f246fd0 | 1473 | /* TODO: should do better here */ |
4ec70c9b | 1474 | if (inode->i_nlink || is_bad_inode(inode)) |
ba9e5e98 BH |
1475 | goto no_delete; |
1476 | ||
ba9e5e98 | 1477 | inode->i_size = 0; |
dbd5768f | 1478 | clear_inode(inode); |
ba9e5e98 | 1479 | |
fe2fd9ed BH |
1480 | /* if we are deleting an obj that hasn't been created yet, wait. |
1481 | * This also makes sure that create_done cannot be called with an | |
1482 | * already evicted inode. | |
1483 | */ | |
1484 | wait_obj_created(oi); | |
1485 | /* ignore the error, attempt a remove anyway */ | |
2f246fd0 BH |
1486 | |
1487 | /* Now Remove the OSD objects */ | |
5bf696da | 1488 | ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios); |
2f246fd0 | 1489 | if (unlikely(ret)) { |
8ff660ab | 1490 | EXOFS_ERR("%s: ore_get_io_state failed\n", __func__); |
2f246fd0 | 1491 | return; |
ba9e5e98 BH |
1492 | } |
1493 | ||
06886a5a BH |
1494 | ios->done = delete_done; |
1495 | ios->private = sbi; | |
9e9db456 | 1496 | |
8ff660ab | 1497 | ret = ore_remove(ios); |
ba9e5e98 | 1498 | if (ret) { |
8ff660ab BH |
1499 | EXOFS_ERR("%s: ore_remove failed\n", __func__); |
1500 | ore_put_io_state(ios); | |
ba9e5e98 BH |
1501 | return; |
1502 | } | |
1503 | atomic_inc(&sbi->s_curr_pending); | |
1504 | ||
1505 | return; | |
1506 | ||
1507 | no_delete: | |
dbd5768f | 1508 | clear_inode(inode); |
ba9e5e98 | 1509 | } |