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