blob: 86c0ac87b8e3c063047d48b7fd70a18aec7b90a1 [file] [log] [blame]
Boaz Harroshe8062712008-10-27 18:37:02 +02001/*
2 * Copyright (C) 2005, 2006
Boaz Harrosh27d2e142009-06-14 17:23:09 +03003 * Avishay Traeger (avishay@gmail.com)
Boaz Harroshe8062712008-10-27 18:37:02 +02004 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Boaz Harroshe8062712008-10-27 18:37:02 +020035
36#include "exofs.h"
37
Boaz Harroshfe33cc12009-11-01 18:28:14 +020038#define EXOFS_DBGMSG2(M...) do {} while (0)
39
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030040enum {MAX_PAGES_KMALLOC = PAGE_SIZE / sizeof(struct page *), };
Boaz Harrosh06886a52009-11-08 14:54:08 +020041
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070042unsigned exofs_max_io_pages(struct ore_layout *layout,
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -040043 unsigned expected_pages)
44{
45 unsigned pages = min_t(unsigned, expected_pages, MAX_PAGES_KMALLOC);
46
47 /* TODO: easily support bio chaining */
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030048 pages = min_t(unsigned, pages, layout->max_io_length / PAGE_SIZE);
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -040049 return pages;
50}
51
Boaz Harroshbeaec072008-10-27 19:31:34 +020052struct page_collect {
53 struct exofs_sb_info *sbi;
Boaz Harroshbeaec072008-10-27 19:31:34 +020054 struct inode *inode;
55 unsigned expected_pages;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070056 struct ore_io_state *ios;
Boaz Harroshbeaec072008-10-27 19:31:34 +020057
Boaz Harrosh86093aa2010-01-28 18:24:06 +020058 struct page **pages;
59 unsigned alloc_pages;
Boaz Harroshbeaec072008-10-27 19:31:34 +020060 unsigned nr_pages;
61 unsigned long length;
62 loff_t pg_first; /* keep 64bit also in 32-arches */
Boaz Harroshf17b1f92010-10-07 13:37:51 -040063 bool read_4_write; /* This means two things: that the read is sync
64 * And the pages should not be unlocked.
65 */
Boaz Harroshbeaec072008-10-27 19:31:34 +020066};
67
68static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
Boaz Harrosh06886a52009-11-08 14:54:08 +020069 struct inode *inode)
Boaz Harroshbeaec072008-10-27 19:31:34 +020070{
71 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
Boaz Harroshbeaec072008-10-27 19:31:34 +020072
73 pcol->sbi = sbi;
Boaz Harroshbeaec072008-10-27 19:31:34 +020074 pcol->inode = inode;
75 pcol->expected_pages = expected_pages;
76
Boaz Harrosh06886a52009-11-08 14:54:08 +020077 pcol->ios = NULL;
Boaz Harrosh86093aa2010-01-28 18:24:06 +020078 pcol->pages = NULL;
79 pcol->alloc_pages = 0;
Boaz Harroshbeaec072008-10-27 19:31:34 +020080 pcol->nr_pages = 0;
81 pcol->length = 0;
82 pcol->pg_first = -1;
Boaz Harroshf17b1f92010-10-07 13:37:51 -040083 pcol->read_4_write = false;
Boaz Harroshbeaec072008-10-27 19:31:34 +020084}
85
86static void _pcol_reset(struct page_collect *pcol)
87{
88 pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
89
Boaz Harrosh86093aa2010-01-28 18:24:06 +020090 pcol->pages = NULL;
91 pcol->alloc_pages = 0;
Boaz Harroshbeaec072008-10-27 19:31:34 +020092 pcol->nr_pages = 0;
93 pcol->length = 0;
94 pcol->pg_first = -1;
Boaz Harrosh06886a52009-11-08 14:54:08 +020095 pcol->ios = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +020096
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)
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200101 pcol->expected_pages = MAX_PAGES_KMALLOC;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200102}
103
104static int pcol_try_alloc(struct page_collect *pcol)
105{
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400106 unsigned pages;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200107
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200108 /* TODO: easily support bio chaining */
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400109 pages = exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200110
Boaz Harroshbeaec072008-10-27 19:31:34 +0200111 for (; pages; pages >>= 1) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200112 pcol->pages = kmalloc(pages * sizeof(struct page *),
113 GFP_KERNEL);
114 if (likely(pcol->pages)) {
115 pcol->alloc_pages = pages;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200116 return 0;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200117 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200118 }
119
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200120 EXOFS_ERR("Failed to kmalloc expected_pages=%u\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200121 pcol->expected_pages);
122 return -ENOMEM;
123}
124
125static void pcol_free(struct page_collect *pcol)
126{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200127 kfree(pcol->pages);
128 pcol->pages = NULL;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200129
130 if (pcol->ios) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700131 ore_put_io_state(pcol->ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200132 pcol->ios = NULL;
133 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200134}
135
136static int pcol_add_page(struct page_collect *pcol, struct page *page,
137 unsigned len)
138{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200139 if (unlikely(pcol->nr_pages >= pcol->alloc_pages))
Boaz Harroshbeaec072008-10-27 19:31:34 +0200140 return -ENOMEM;
141
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200142 pcol->pages[pcol->nr_pages++] = page;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200143 pcol->length += len;
144 return 0;
145}
146
Boaz Harrosh154a9302011-08-26 21:00:32 -0700147enum {PAGE_WAS_NOT_IN_IO = 17};
Boaz Harroshbeaec072008-10-27 19:31:34 +0200148static int update_read_page(struct page *page, int ret)
149{
Boaz Harrosh154a9302011-08-26 21:00:32 -0700150 switch (ret) {
151 case 0:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200152 /* Everything is OK */
153 SetPageUptodate(page);
154 if (PageError(page))
155 ClearPageError(page);
Boaz Harrosh154a9302011-08-26 21:00:32 -0700156 break;
157 case -EFAULT:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200158 /* 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);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200168 EXOFS_DBGMSG("recovered read error\n");
Boaz Harrosh154a9302011-08-26 21:00:32 -0700169 /* fall through */
170 case PAGE_WAS_NOT_IN_IO:
171 ret = 0; /* recovered error */
172 break;
173 default:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200174 SetPageError(page);
Boaz Harrosh154a9302011-08-26 21:00:32 -0700175 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200176 return ret;
177}
178
179static void update_write_page(struct page *page, int ret)
180{
Boaz Harrosh154a9302011-08-26 21:00:32 -0700181 if (unlikely(ret == PAGE_WAS_NOT_IN_IO))
182 return; /* don't pass start don't collect $200 */
183
Boaz Harroshbeaec072008-10-27 19:31:34 +0200184 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 */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400194static int __readpages_done(struct page_collect *pcol)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200195{
Boaz Harroshbeaec072008-10-27 19:31:34 +0200196 int i;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200197 u64 good_bytes;
198 u64 length = 0;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300199 int ret = ore_check_io(pcol->ios, NULL);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200200
Boaz Harrosh154a9302011-08-26 21:00:32 -0700201 if (likely(!ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200202 good_bytes = pcol->length;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700203 ret = PAGE_WAS_NOT_IN_IO;
204 } else {
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300205 good_bytes = 0;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700206 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200207
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200208 EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
Boaz Harroshbeaec072008-10-27 19:31:34 +0200209 " length=0x%lx nr_pages=%u\n",
210 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
211 pcol->nr_pages);
212
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200213 for (i = 0; i < pcol->nr_pages; i++) {
214 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200215 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
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200226 EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200227 inode->i_ino, page->index,
228 page_stat ? "bad_bytes" : "good_bytes");
229
230 ret = update_read_page(page, page_stat);
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400231 if (!pcol->read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200232 unlock_page(page);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200233 length += PAGE_SIZE;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200234 }
235
236 pcol_free(pcol);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200237 EXOFS_DBGMSG2("readpages_done END\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200238 return ret;
239}
240
241/* callback of async reads */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700242static void readpages_done(struct ore_io_state *ios, void *p)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200243{
244 struct page_collect *pcol = p;
245
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400246 __readpages_done(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200247 atomic_dec(&pcol->sbi->s_curr_pending);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200248 kfree(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200249}
250
251static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
252{
Boaz Harroshbeaec072008-10-27 19:31:34 +0200253 int i;
254
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200255 for (i = 0; i < pcol->nr_pages; i++) {
256 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200257
258 if (rw == READ)
259 update_read_page(page, ret);
260 else
261 update_write_page(page, ret);
262
263 unlock_page(page);
264 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200265}
266
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300267static 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
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400307static int read_exec(struct page_collect *pcol)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200308{
309 struct exofs_i_info *oi = exofs_i(pcol->inode);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700310 struct ore_io_state *ios;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200311 struct page_collect *pcol_copy = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200312 int ret;
313
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200314 if (!pcol->pages)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200315 return 0;
316
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200317 if (!pcol->ios) {
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300318 int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true,
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200319 pcol->pg_first << PAGE_CACHE_SHIFT,
320 pcol->length, &pcol->ios);
321
322 if (ret)
323 return ret;
324 }
325
326 ios = pcol->ios;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200327 ios->pages = pcol->pages;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200328
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400329 if (pcol->read_4_write) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700330 ore_read(pcol->ios);
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400331 return __readpages_done(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200332 }
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;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200341 ios->done = readpages_done;
342 ios->private = pcol_copy;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300343
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
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700354 ret = ore_read(ios);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200355 if (unlikely(ret))
356 goto err;
357
358 atomic_inc(&pcol->sbi->s_curr_pending);
359
Boaz Harroshbeaec072008-10-27 19:31:34 +0200360 return 0;
361
362err:
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400363 if (!pcol->read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200364 _unlock_pcol_pages(pcol, ret, READ);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200365
366 pcol_free(pcol);
Boaz Harroshb76a3f92009-06-08 19:28:41 +0300367
Boaz Harroshbeaec072008-10-27 19:31:34 +0200368 kfree(pcol_copy);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200369 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
Boaz Harroshf17b1f92010-10-07 13:37:51 -0400411 if (!pcol->read_4_write)
412 unlock_page(page);
Boaz Harrosha8f14182010-11-22 18:02:45 +0200413 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);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200417
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400418 return read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200419 }
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 */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400428 ret = read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200429 if (unlikely(ret))
430 goto fail;
431 goto try_again;
432 }
433
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200434 if (!pcol->pages) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200435 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
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200443 EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200444 inode->i_ino, page->index, len);
445
446 ret = pcol_add_page(pcol, page, len);
447 if (ret) {
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200448 EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
Boaz Harroshbeaec072008-10-27 19:31:34 +0200449 "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 */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400453 ret = read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200454 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
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300482 ret = read_exec(&pcol);
483 if (unlikely(ret))
484 return ret;
485
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400486 return read_exec(&pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200487}
488
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400489static int _readpage(struct page *page, bool read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200490{
491 struct page_collect pcol;
492 int ret;
493
494 _pcol_init(&pcol, 1, page->mapping->host);
495
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400496 pcol.read_4_write = read_4_write;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200497 ret = readpage_strip(&pcol, page);
498 if (ret) {
499 EXOFS_ERR("_readpage => %d\n", ret);
500 return ret;
501 }
502
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400503 return read_exec(&pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200504}
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
Boaz Harrosh06886a52009-11-08 14:54:08 +0200514/* Callback for osd_write. All writes are asynchronous */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700515static void writepages_done(struct ore_io_state *ios, void *p)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200516{
517 struct page_collect *pcol = p;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200518 int i;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200519 u64 good_bytes;
520 u64 length = 0;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300521 int ret = ore_check_io(ios, NULL);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200522
Boaz Harroshbeaec072008-10-27 19:31:34 +0200523 atomic_dec(&pcol->sbi->s_curr_pending);
524
Boaz Harrosh154a9302011-08-26 21:00:32 -0700525 if (likely(!ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200526 good_bytes = pcol->length;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700527 ret = PAGE_WAS_NOT_IN_IO;
528 } else {
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300529 good_bytes = 0;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700530 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200531
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200532 EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
Boaz Harroshbeaec072008-10-27 19:31:34 +0200533 " length=0x%lx nr_pages=%u\n",
534 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
535 pcol->nr_pages);
536
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200537 for (i = 0; i < pcol->nr_pages; i++) {
538 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200539 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);
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200552 EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200553 inode->i_ino, page->index, page_stat);
554
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200555 length += PAGE_SIZE;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200556 }
557
558 pcol_free(pcol);
559 kfree(pcol);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200560 EXOFS_DBGMSG2("writepages_done END\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200561}
562
563static int write_exec(struct page_collect *pcol)
564{
565 struct exofs_i_info *oi = exofs_i(pcol->inode);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700566 struct ore_io_state *ios;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200567 struct page_collect *pcol_copy = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200568 int ret;
569
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200570 if (!pcol->pages)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200571 return 0;
572
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200573 BUG_ON(pcol->ios);
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300574 ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false,
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200575 pcol->pg_first << PAGE_CACHE_SHIFT,
576 pcol->length, &pcol->ios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200577 if (unlikely(ret))
578 goto err;
579
Boaz Harroshbeaec072008-10-27 19:31:34 +0200580 pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
581 if (!pcol_copy) {
Joe Perches571f7f42010-10-21 22:17:17 -0700582 EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200583 ret = -ENOMEM;
584 goto err;
585 }
586
587 *pcol_copy = *pcol;
588
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200589 ios = pcol->ios;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200590 ios->pages = pcol_copy->pages;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200591 ios->done = writepages_done;
592 ios->private = pcol_copy;
593
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300594 /* 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
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700604 ret = ore_write(ios);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200605 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700606 EXOFS_ERR("write_exec: ore_write() Failed\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200607 goto err;
608 }
609
610 atomic_inc(&pcol->sbi->s_curr_pending);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200611 return 0;
612
613err:
614 _unlock_pcol_pages(pcol, ret, WRITE);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200615 pcol_free(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200616 kfree(pcol_copy);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200617
Boaz Harroshbeaec072008-10-27 19:31:34 +0200618 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);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200661 EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
662 "outside the limits\n",
663 inode->i_ino, page->index);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200664 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;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200678
679 EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
680 inode->i_ino, page->index);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200681 goto try_again;
682 }
683
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200684 if (!pcol->pages) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200685 ret = pcol_try_alloc(pcol);
686 if (unlikely(ret))
687 goto fail;
688 }
689
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200690 EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200691 inode->i_ino, page->index, len);
692
693 ret = pcol_add_page(pcol, page, len);
694 if (unlikely(ret)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200695 EXOFS_DBGMSG2("Failed pcol_add_page "
Boaz Harroshbeaec072008-10-27 19:31:34 +0200696 "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)) {
Joe Perches571f7f42010-10-21 22:17:17 -0700702 EXOFS_DBGMSG("write_exec failed => %d", ret);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200703 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:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200715 EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
716 inode->i_ino, page->index, ret);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200717 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)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200735 expected_pages = end - start + 1;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200736 else
737 expected_pages = mapping->nrpages;
738
Boaz Harrosh06886a52009-11-08 14:54:08 +0200739 if (expected_pages < 32L)
740 expected_pages = 32L;
741
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200742 EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
Boaz Harrosh06886a52009-11-08 14:54:08 +0200743 "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200744 mapping->host->i_ino, wbc->range_start, wbc->range_end,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200745 mapping->nrpages, start, end, expected_pages);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200746
747 _pcol_init(&pcol, expected_pages, mapping->host);
748
749 ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300750 if (unlikely(ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200751 EXOFS_ERR("write_cache_pages => %d\n", ret);
752 return ret;
753 }
754
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300755 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;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200774}
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
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300792/* 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
Boaz Harroshbeaec072008-10-27 19:31:34 +0200799int 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) {
Joe Perches571f7f42010-10-21 22:17:17 -0700811 EXOFS_DBGMSG("simple_write_begin failed\n");
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300812 goto out;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200813 }
814
815 page = *pagep;
816 }
817
818 /* read modify write */
819 if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
Boaz Harrosha8f14182010-11-22 18:02:45 +0200820 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
Boaz Harroshbeaec072008-10-27 19:31:34 +0200837 ret = _readpage(page, true);
838 if (ret) {
839 /*SetPageError was done by _readpage. Is it ok?*/
840 unlock_page(page);
Boaz Harrosha8f14182010-11-22 18:02:45 +0200841 EXOFS_DBGMSG("__readpage failed\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200842 }
843 }
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300844out:
845 if (unlikely(ret))
846 _write_failed(mapping->host, pos + len);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200847
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
Boaz Harroshefd124b2009-12-27 17:01:42 +0200862static 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);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300872 if (unlikely(ret))
873 _write_failed(inode, pos + len);
874
875 /* TODO: once simple_write_end marks inode dirty remove */
Boaz Harroshefd124b2009-12-27 17:01:42 +0200876 if (i_size != inode->i_size)
877 mark_inode_dirty(inode);
878 return ret;
879}
880
Boaz Harrosh200b07002010-03-22 11:23:40 +0200881static int exofs_releasepage(struct page *page, gfp_t gfp)
882{
883 EXOFS_DBGMSG("page 0x%lx\n", page->index);
884 WARN_ON(1);
Boaz Harrosh85dc7872010-05-31 18:55:43 +0300885 return 0;
Boaz Harrosh200b07002010-03-22 11:23:40 +0200886}
887
888static void exofs_invalidatepage(struct page *page, unsigned long offset)
889{
Boaz Harrosh85dc7872010-05-31 18:55:43 +0300890 EXOFS_DBGMSG("page 0x%lx offset 0x%lx\n", page->index, offset);
Boaz Harrosh200b07002010-03-22 11:23:40 +0200891 WARN_ON(1);
Boaz Harrosh200b07002010-03-22 11:23:40 +0200892}
893
Boaz Harroshbeaec072008-10-27 19:31:34 +0200894const 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,
Boaz Harroshefd124b2009-12-27 17:01:42 +0200900 .write_end = exofs_write_end,
Boaz Harrosh200b07002010-03-22 11:23:40 +0200901 .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 */
Boaz Harrosh200b07002010-03-22 11:23:40 +0200910 .get_xip_mem = NULL,
911 .migratepage = NULL,
912 .launder_page = NULL,
913 .is_partially_uptodate = NULL,
914 .error_remove_page = NULL,
Boaz Harroshbeaec072008-10-27 19:31:34 +0200915};
916
Boaz Harroshe8062712008-10-27 18:37:02 +0200917/******************************************************************************
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
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300931static int _do_truncate(struct inode *inode, loff_t newsize)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200932{
933 struct exofs_i_info *oi = exofs_i(inode);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700934 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200935 int ret;
936
937 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
938
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300939 ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300940 if (likely(!ret))
941 truncate_setsize(inode, newsize);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200942
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300943 EXOFS_DBGMSG("(0x%lx) size=0x%llx ret=>%d\n",
944 inode->i_ino, newsize, ret);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200945 return ret;
946}
947
Boaz Harroshe8062712008-10-27 18:37:02 +0200948/*
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300949 * Set inode attributes - update size attribute on OSD if needed,
950 * otherwise just call generic functions.
Boaz Harroshe8062712008-10-27 18:37:02 +0200951 */
952int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
953{
954 struct inode *inode = dentry->d_inode;
955 int error;
956
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300957 /* 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
Boaz Harroshe8062712008-10-27 18:37:02 +0200964 error = inode_change_ok(inode, iattr);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300965 if (unlikely(error))
Boaz Harroshe8062712008-10-27 18:37:02 +0200966 return error;
967
Christoph Hellwig10257742010-06-04 11:30:02 +0200968 if ((iattr->ia_valid & ATTR_SIZE) &&
969 iattr->ia_size != i_size_read(inode)) {
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300970 error = _do_truncate(inode, iattr->ia_size);
971 if (unlikely(error))
Christoph Hellwig10257742010-06-04 11:30:02 +0200972 return error;
973 }
974
975 setattr_copy(inode, iattr);
976 mark_inode_dirty(inode);
977 return 0;
Boaz Harroshe8062712008-10-27 18:37:02 +0200978}
Boaz Harroshe6af00f2008-10-28 15:38:12 +0200979
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200980static 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
Boaz Harroshe6af00f2008-10-28 15:38:12 +0200989/*
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200990 * 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.
Boaz Harroshe6af00f2008-10-28 15:38:12 +0200992 */
993static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200994 struct exofs_fcb *inode)
Boaz Harroshe6af00f2008-10-28 15:38:12 +0200995{
996 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200997 struct osd_attr attrs[] = {
998 [0] = g_attr_inode_data,
999 [1] = g_attr_inode_file_layout,
1000 [2] = g_attr_inode_dir_layout,
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001001 };
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001002 struct ore_io_state *ios;
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001003 struct exofs_on_disk_inode_layout *layout;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001004 int ret;
1005
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001006 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001007 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001008 EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001009 return ret;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001010 }
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001011
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001012 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);
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001014
Boaz Harrosh06886a52009-11-08 14:54:08 +02001015 ios->in_attr = attrs;
1016 ios->in_attr_len = ARRAY_SIZE(attrs);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001017
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001018 ret = ore_read(ios);
Boaz Harrosh96391e22010-02-09 11:43:21 +02001019 if (unlikely(ret)) {
1020 EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001021 _LLU(oi->one_comp.obj.id), ret);
Boaz Harrosh96391e22010-02-09 11:43:21 +02001022 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;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001029 goto out;
Boaz Harrosh96391e22010-02-09 11:43:21 +02001030 }
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001031
Boaz Harrosh06886a52009-11-08 14:54:08 +02001032 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001033 if (ret) {
Boaz Harrosh06886a52009-11-08 14:54:08 +02001034 EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001035 goto out;
1036 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001037 WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
1038 memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001039
Boaz Harrosh06886a52009-11-08 14:54:08 +02001040 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001041 if (ret) {
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001042 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
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001070out:
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001071 ore_put_io_state(ios);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001072 return ret;
1073}
1074
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001075static void __oi_init(struct exofs_i_info *oi)
1076{
1077 init_waitqueue_head(&oi->i_wq);
1078 oi->i_flags = 0;
1079}
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001080/*
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;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001088 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);
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001096 __oi_init(oi);
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001097 exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001098 exofs_oi_objno(oi));
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001099
1100 /* read the inode from the osd */
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001101 ret = exofs_get_inode(sb, oi, &fcb);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001102 if (ret)
1103 goto bad_inode;
1104
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001105 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
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001122 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
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -04001140 inode->i_mapping->backing_dev_info = sb->s_bdi;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001141 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)) {
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001177 EXOFS_DBGMSG("!obj_created\n");
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001178 BUG_ON(!obj_2bcreated(oi));
1179 wait_event(oi->i_wq, obj_created(oi));
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001180 EXOFS_DBGMSG("wait_event done\n");
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001181 }
1182 return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
1183}
Boaz Harrosh1cea3122011-02-03 17:53:25 +02001184
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001185/*
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 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001190static void create_done(struct ore_io_state *ios, void *p)
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001191{
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
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001197 ret = ore_check_io(ios, NULL);
1198 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001199
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001200 atomic_dec(&sbi->s_curr_pending);
1201
1202 if (unlikely(ret)) {
Joe Perches571f7f42010-10-21 22:17:17 -07001203 EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001204 _LLU(exofs_oi_objno(oi)),
1205 _LLU(oi->one_comp.obj.partition));
Boaz Harrosh06886a52009-11-08 14:54:08 +02001206 /*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);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001215
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001216 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{
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001224 struct super_block *sb = dir->i_sb;
1225 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001226 struct inode *inode;
1227 struct exofs_i_info *oi;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001228 struct ore_io_state *ios;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001229 int ret;
1230
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001231 inode = new_inode(sb);
1232 if (!inode)
1233 return ERR_PTR(-ENOMEM);
1234
1235 oi = exofs_i(inode);
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001236 __oi_init(oi);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001237
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001238 set_obj_2bcreated(oi);
1239
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -04001240 inode->i_mapping->backing_dev_info = sb->s_bdi;
Dmitry Monakhove00117f2010-03-04 17:31:48 +03001241 inode_init_owner(inode, dir, mode);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001242 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
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001251 exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001252 exofs_oi_objno(oi));
Boaz Harrosh1cea3122011-02-03 17:53:25 +02001253 exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
1254
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001255 mark_inode_dirty(inode);
1256
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001257 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001258 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001259 EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
Boaz Harrosh06886a52009-11-08 14:54:08 +02001260 return ERR_PTR(ret);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001261 }
1262
Boaz Harrosh06886a52009-11-08 14:54:08 +02001263 ios->done = create_done;
1264 ios->private = inode;
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001265
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001266 ret = ore_create(ios);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001267 if (ret) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001268 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001269 return ERR_PTR(ret);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001270 }
1271 atomic_inc(&sbi->s_curr_pending);
1272
1273 return inode;
1274}
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001275
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 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001287static void updatei_done(struct ore_io_state *ios, void *p)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001288{
1289 struct updatei_args *args = p;
1290
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001291 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001292
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;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001307 struct ore_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001308 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);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001314 if (!args) {
Joe Perches571f7f42010-10-21 22:17:17 -07001315 EXOFS_DBGMSG("Failed kzalloc of args\n");
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001316 return -ENOMEM;
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001317 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001318
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
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001346 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001347 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001348 EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001349 goto free_args;
1350 }
1351
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001352 attr = g_attr_inode_data;
1353 attr.val_ptr = fcb;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001354 ios->out_attr_len = 1;
1355 ios->out_attr = &attr;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001356
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001357 wait_obj_created(oi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001358
Boaz Harrosh06886a52009-11-08 14:54:08 +02001359 if (!do_sync) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001360 args->sbi = sbi;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001361 ios->done = updatei_done;
1362 ios->private = args;
1363 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001364
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001365 ret = ore_write(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001366 if (!do_sync && !ret) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001367 atomic_inc(&sbi->s_curr_pending);
1368 goto out; /* deallocation in updatei_done */
1369 }
1370
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001371 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001372free_args:
1373 kfree(args);
1374out:
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001375 EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
1376 inode->i_ino, do_sync, ret);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001377 return ret;
1378}
1379
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001380int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001381{
Nick Piggin97178b72010-11-25 12:47:15 +02001382 /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
1383 return exofs_update_inode(inode, 1);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001384}
1385
1386/*
1387 * Callback function from exofs_delete_inode() - don't have much cleaning up to
1388 * do.
1389 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001390static void delete_done(struct ore_io_state *ios, void *p)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001391{
Boaz Harrosh06886a52009-11-08 14:54:08 +02001392 struct exofs_sb_info *sbi = p;
1393
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001394 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001395
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001396 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 */
Al Viro4ec70c92010-06-07 11:42:26 -04001404void exofs_evict_inode(struct inode *inode)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001405{
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;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001409 struct ore_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001410 int ret;
1411
1412 truncate_inode_pages(&inode->i_data, 0);
1413
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001414 /* TODO: should do better here */
Al Viro4ec70c92010-06-07 11:42:26 -04001415 if (inode->i_nlink || is_bad_inode(inode))
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001416 goto no_delete;
1417
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001418 inode->i_size = 0;
Al Viro4ec70c92010-06-07 11:42:26 -04001419 end_writeback(inode);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001420
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001421 /* 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 */
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001427
1428 /* Now Remove the OSD objects */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001429 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001430 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001431 EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001432 return;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001433 }
1434
Boaz Harrosh06886a52009-11-08 14:54:08 +02001435 ios->done = delete_done;
1436 ios->private = sbi;
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001437
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001438 ret = ore_remove(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001439 if (ret) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001440 EXOFS_ERR("%s: ore_remove failed\n", __func__);
1441 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001442 return;
1443 }
1444 atomic_inc(&sbi->s_curr_pending);
1445
1446 return;
1447
1448no_delete:
Al Viro4ec70c92010-06-07 11:42:26 -04001449 end_writeback(inode);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001450}