blob: 9a5ed30b8a354e47d4a32e62dfd3fc6246ab068f [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 Harroshdd296612011-10-12 15:42:07 +020066 struct page *that_locked_page;
Boaz Harroshbeaec072008-10-27 19:31:34 +020067};
68
69static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
Boaz Harrosh06886a52009-11-08 14:54:08 +020070 struct inode *inode)
Boaz Harroshbeaec072008-10-27 19:31:34 +020071{
72 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
Boaz Harroshbeaec072008-10-27 19:31:34 +020073
74 pcol->sbi = sbi;
Boaz Harroshbeaec072008-10-27 19:31:34 +020075 pcol->inode = inode;
76 pcol->expected_pages = expected_pages;
77
Boaz Harrosh06886a52009-11-08 14:54:08 +020078 pcol->ios = NULL;
Boaz Harrosh86093aa2010-01-28 18:24:06 +020079 pcol->pages = NULL;
80 pcol->alloc_pages = 0;
Boaz Harroshbeaec072008-10-27 19:31:34 +020081 pcol->nr_pages = 0;
82 pcol->length = 0;
83 pcol->pg_first = -1;
Boaz Harroshf17b1f92010-10-07 13:37:51 -040084 pcol->read_4_write = false;
Boaz Harroshdd296612011-10-12 15:42:07 +020085 pcol->that_locked_page = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +020086}
87
88static void _pcol_reset(struct page_collect *pcol)
89{
90 pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
91
Boaz Harrosh86093aa2010-01-28 18:24:06 +020092 pcol->pages = NULL;
93 pcol->alloc_pages = 0;
Boaz Harroshbeaec072008-10-27 19:31:34 +020094 pcol->nr_pages = 0;
95 pcol->length = 0;
96 pcol->pg_first = -1;
Boaz Harrosh06886a52009-11-08 14:54:08 +020097 pcol->ios = NULL;
Boaz Harroshdd296612011-10-12 15:42:07 +020098 pcol->that_locked_page = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +020099
100 /* this is probably the end of the loop but in writes
101 * it might not end here. don't be left with nothing
102 */
103 if (!pcol->expected_pages)
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200104 pcol->expected_pages = MAX_PAGES_KMALLOC;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200105}
106
107static int pcol_try_alloc(struct page_collect *pcol)
108{
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400109 unsigned pages;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200110
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200111 /* TODO: easily support bio chaining */
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400112 pages = exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200113
Boaz Harroshbeaec072008-10-27 19:31:34 +0200114 for (; pages; pages >>= 1) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200115 pcol->pages = kmalloc(pages * sizeof(struct page *),
116 GFP_KERNEL);
117 if (likely(pcol->pages)) {
118 pcol->alloc_pages = pages;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200119 return 0;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200120 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200121 }
122
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200123 EXOFS_ERR("Failed to kmalloc expected_pages=%u\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200124 pcol->expected_pages);
125 return -ENOMEM;
126}
127
128static void pcol_free(struct page_collect *pcol)
129{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200130 kfree(pcol->pages);
131 pcol->pages = NULL;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200132
133 if (pcol->ios) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700134 ore_put_io_state(pcol->ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200135 pcol->ios = NULL;
136 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200137}
138
139static int pcol_add_page(struct page_collect *pcol, struct page *page,
140 unsigned len)
141{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200142 if (unlikely(pcol->nr_pages >= pcol->alloc_pages))
Boaz Harroshbeaec072008-10-27 19:31:34 +0200143 return -ENOMEM;
144
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200145 pcol->pages[pcol->nr_pages++] = page;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200146 pcol->length += len;
147 return 0;
148}
149
Boaz Harrosh154a9302011-08-26 21:00:32 -0700150enum {PAGE_WAS_NOT_IN_IO = 17};
Boaz Harroshbeaec072008-10-27 19:31:34 +0200151static int update_read_page(struct page *page, int ret)
152{
Boaz Harrosh154a9302011-08-26 21:00:32 -0700153 switch (ret) {
154 case 0:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200155 /* Everything is OK */
156 SetPageUptodate(page);
157 if (PageError(page))
158 ClearPageError(page);
Boaz Harrosh154a9302011-08-26 21:00:32 -0700159 break;
160 case -EFAULT:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200161 /* In this case we were trying to read something that wasn't on
162 * disk yet - return a page full of zeroes. This should be OK,
163 * because the object should be empty (if there was a write
164 * before this read, the read would be waiting with the page
165 * locked */
166 clear_highpage(page);
167
168 SetPageUptodate(page);
169 if (PageError(page))
170 ClearPageError(page);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200171 EXOFS_DBGMSG("recovered read error\n");
Boaz Harrosh154a9302011-08-26 21:00:32 -0700172 /* fall through */
173 case PAGE_WAS_NOT_IN_IO:
174 ret = 0; /* recovered error */
175 break;
176 default:
Boaz Harroshbeaec072008-10-27 19:31:34 +0200177 SetPageError(page);
Boaz Harrosh154a9302011-08-26 21:00:32 -0700178 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200179 return ret;
180}
181
182static void update_write_page(struct page *page, int ret)
183{
Boaz Harrosh154a9302011-08-26 21:00:32 -0700184 if (unlikely(ret == PAGE_WAS_NOT_IN_IO))
185 return; /* don't pass start don't collect $200 */
186
Boaz Harroshbeaec072008-10-27 19:31:34 +0200187 if (ret) {
188 mapping_set_error(page->mapping, ret);
189 SetPageError(page);
190 }
191 end_page_writeback(page);
192}
193
194/* Called at the end of reads, to optionally unlock pages and update their
195 * status.
196 */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400197static int __readpages_done(struct page_collect *pcol)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200198{
Boaz Harroshbeaec072008-10-27 19:31:34 +0200199 int i;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200200 u64 good_bytes;
201 u64 length = 0;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300202 int ret = ore_check_io(pcol->ios, NULL);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200203
Boaz Harrosh154a9302011-08-26 21:00:32 -0700204 if (likely(!ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200205 good_bytes = pcol->length;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700206 ret = PAGE_WAS_NOT_IN_IO;
207 } else {
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300208 good_bytes = 0;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700209 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200210
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200211 EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
Boaz Harroshbeaec072008-10-27 19:31:34 +0200212 " length=0x%lx nr_pages=%u\n",
213 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
214 pcol->nr_pages);
215
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200216 for (i = 0; i < pcol->nr_pages; i++) {
217 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200218 struct inode *inode = page->mapping->host;
219 int page_stat;
220
221 if (inode != pcol->inode)
222 continue; /* osd might add more pages at end */
223
224 if (likely(length < good_bytes))
225 page_stat = 0;
226 else
227 page_stat = ret;
228
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200229 EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200230 inode->i_ino, page->index,
231 page_stat ? "bad_bytes" : "good_bytes");
232
233 ret = update_read_page(page, page_stat);
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400234 if (!pcol->read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200235 unlock_page(page);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200236 length += PAGE_SIZE;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200237 }
238
239 pcol_free(pcol);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200240 EXOFS_DBGMSG2("readpages_done END\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200241 return ret;
242}
243
244/* callback of async reads */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700245static void readpages_done(struct ore_io_state *ios, void *p)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200246{
247 struct page_collect *pcol = p;
248
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400249 __readpages_done(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200250 atomic_dec(&pcol->sbi->s_curr_pending);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200251 kfree(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200252}
253
254static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
255{
Boaz Harroshbeaec072008-10-27 19:31:34 +0200256 int i;
257
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200258 for (i = 0; i < pcol->nr_pages; i++) {
259 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200260
261 if (rw == READ)
262 update_read_page(page, ret);
263 else
264 update_write_page(page, ret);
265
266 unlock_page(page);
267 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200268}
269
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300270static int _maybe_not_all_in_one_io(struct ore_io_state *ios,
271 struct page_collect *pcol_src, struct page_collect *pcol)
272{
273 /* length was wrong or offset was not page aligned */
274 BUG_ON(pcol_src->nr_pages < ios->nr_pages);
275
276 if (pcol_src->nr_pages > ios->nr_pages) {
277 struct page **src_page;
278 unsigned pages_less = pcol_src->nr_pages - ios->nr_pages;
279 unsigned long len_less = pcol_src->length - ios->length;
280 unsigned i;
281 int ret;
282
283 /* This IO was trimmed */
284 pcol_src->nr_pages = ios->nr_pages;
285 pcol_src->length = ios->length;
286
287 /* Left over pages are passed to the next io */
288 pcol->expected_pages += pages_less;
289 pcol->nr_pages = pages_less;
290 pcol->length = len_less;
291 src_page = pcol_src->pages + pcol_src->nr_pages;
292 pcol->pg_first = (*src_page)->index;
293
294 ret = pcol_try_alloc(pcol);
295 if (unlikely(ret))
296 return ret;
297
298 for (i = 0; i < pages_less; ++i)
299 pcol->pages[i] = *src_page++;
300
301 EXOFS_DBGMSG("Length was adjusted nr_pages=0x%x "
302 "pages_less=0x%x expected_pages=0x%x "
303 "next_offset=0x%llx next_len=0x%lx\n",
304 pcol_src->nr_pages, pages_less, pcol->expected_pages,
305 pcol->pg_first * PAGE_SIZE, pcol->length);
306 }
307 return 0;
308}
309
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400310static int read_exec(struct page_collect *pcol)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200311{
312 struct exofs_i_info *oi = exofs_i(pcol->inode);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700313 struct ore_io_state *ios;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200314 struct page_collect *pcol_copy = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200315 int ret;
316
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200317 if (!pcol->pages)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200318 return 0;
319
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200320 if (!pcol->ios) {
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300321 int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true,
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200322 pcol->pg_first << PAGE_CACHE_SHIFT,
323 pcol->length, &pcol->ios);
324
325 if (ret)
326 return ret;
327 }
328
329 ios = pcol->ios;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200330 ios->pages = pcol->pages;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200331
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400332 if (pcol->read_4_write) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700333 ore_read(pcol->ios);
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400334 return __readpages_done(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200335 }
336
337 pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
338 if (!pcol_copy) {
339 ret = -ENOMEM;
340 goto err;
341 }
342
343 *pcol_copy = *pcol;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200344 ios->done = readpages_done;
345 ios->private = pcol_copy;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300346
347 /* pages ownership was passed to pcol_copy */
348 _pcol_reset(pcol);
349
350 ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
351 if (unlikely(ret))
352 goto err;
353
354 EXOFS_DBGMSG2("read_exec(0x%lx) offset=0x%llx length=0x%llx\n",
355 pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));
356
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700357 ret = ore_read(ios);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200358 if (unlikely(ret))
359 goto err;
360
361 atomic_inc(&pcol->sbi->s_curr_pending);
362
Boaz Harroshbeaec072008-10-27 19:31:34 +0200363 return 0;
364
365err:
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400366 if (!pcol->read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200367 _unlock_pcol_pages(pcol, ret, READ);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200368
369 pcol_free(pcol);
Boaz Harroshb76a3f92009-06-08 19:28:41 +0300370
Boaz Harroshbeaec072008-10-27 19:31:34 +0200371 kfree(pcol_copy);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200372 return ret;
373}
374
375/* readpage_strip is called either directly from readpage() or by the VFS from
376 * within read_cache_pages(), to add one more page to be read. It will try to
377 * collect as many contiguous pages as posible. If a discontinuity is
378 * encountered, or it runs out of resources, it will submit the previous segment
379 * and will start a new collection. Eventually caller must submit the last
380 * segment if present.
381 */
382static int readpage_strip(void *data, struct page *page)
383{
384 struct page_collect *pcol = data;
385 struct inode *inode = pcol->inode;
386 struct exofs_i_info *oi = exofs_i(inode);
387 loff_t i_size = i_size_read(inode);
388 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
389 size_t len;
390 int ret;
391
Kautuk Consul0e8d96d2012-02-20 03:46:12 -0500392 BUG_ON(!PageLocked(page));
393
Boaz Harroshbeaec072008-10-27 19:31:34 +0200394 /* FIXME: Just for debugging, will be removed */
395 if (PageUptodate(page))
396 EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
397 page->index);
398
Boaz Harroshdd296612011-10-12 15:42:07 +0200399 pcol->that_locked_page = page;
400
Boaz Harroshbeaec072008-10-27 19:31:34 +0200401 if (page->index < end_index)
402 len = PAGE_CACHE_SIZE;
403 else if (page->index == end_index)
404 len = i_size & ~PAGE_CACHE_MASK;
405 else
406 len = 0;
407
408 if (!len || !obj_created(oi)) {
409 /* this will be out of bounds, or doesn't exist yet.
410 * Current page is cleared and the request is split
411 */
412 clear_highpage(page);
413
414 SetPageUptodate(page);
415 if (PageError(page))
416 ClearPageError(page);
417
Boaz Harroshf17b1f92010-10-07 13:37:51 -0400418 if (!pcol->read_4_write)
419 unlock_page(page);
Boaz Harrosha8f14182010-11-22 18:02:45 +0200420 EXOFS_DBGMSG("readpage_strip(0x%lx) empty page len=%zx "
421 "read_4_write=%d index=0x%lx end_index=0x%lx "
422 "splitting\n", inode->i_ino, len,
423 pcol->read_4_write, page->index, end_index);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200424
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400425 return read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200426 }
427
428try_again:
429
430 if (unlikely(pcol->pg_first == -1)) {
431 pcol->pg_first = page->index;
432 } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
433 page->index)) {
434 /* Discontinuity detected, split the request */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400435 ret = read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200436 if (unlikely(ret))
437 goto fail;
438 goto try_again;
439 }
440
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200441 if (!pcol->pages) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200442 ret = pcol_try_alloc(pcol);
443 if (unlikely(ret))
444 goto fail;
445 }
446
447 if (len != PAGE_CACHE_SIZE)
448 zero_user(page, len, PAGE_CACHE_SIZE - len);
449
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200450 EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200451 inode->i_ino, page->index, len);
452
453 ret = pcol_add_page(pcol, page, len);
454 if (ret) {
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200455 EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
Boaz Harroshbeaec072008-10-27 19:31:34 +0200456 "this_len=0x%zx nr_pages=%u length=0x%lx\n",
457 page, len, pcol->nr_pages, pcol->length);
458
459 /* split the request, and start again with current page */
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400460 ret = read_exec(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200461 if (unlikely(ret))
462 goto fail;
463
464 goto try_again;
465 }
466
467 return 0;
468
469fail:
470 /* SetPageError(page); ??? */
471 unlock_page(page);
472 return ret;
473}
474
475static int exofs_readpages(struct file *file, struct address_space *mapping,
476 struct list_head *pages, unsigned nr_pages)
477{
478 struct page_collect pcol;
479 int ret;
480
481 _pcol_init(&pcol, nr_pages, mapping->host);
482
483 ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
484 if (ret) {
485 EXOFS_ERR("read_cache_pages => %d\n", ret);
486 return ret;
487 }
488
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300489 ret = read_exec(&pcol);
490 if (unlikely(ret))
491 return ret;
492
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400493 return read_exec(&pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200494}
495
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400496static int _readpage(struct page *page, bool read_4_write)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200497{
498 struct page_collect pcol;
499 int ret;
500
501 _pcol_init(&pcol, 1, page->mapping->host);
502
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400503 pcol.read_4_write = read_4_write;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200504 ret = readpage_strip(&pcol, page);
505 if (ret) {
506 EXOFS_ERR("_readpage => %d\n", ret);
507 return ret;
508 }
509
Boaz Harrosh7aebf412010-10-13 12:55:43 -0400510 return read_exec(&pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200511}
512
513/*
514 * We don't need the file
515 */
516static int exofs_readpage(struct file *file, struct page *page)
517{
518 return _readpage(page, false);
519}
520
Boaz Harrosh06886a52009-11-08 14:54:08 +0200521/* Callback for osd_write. All writes are asynchronous */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700522static void writepages_done(struct ore_io_state *ios, void *p)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200523{
524 struct page_collect *pcol = p;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200525 int i;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200526 u64 good_bytes;
527 u64 length = 0;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300528 int ret = ore_check_io(ios, NULL);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200529
Boaz Harroshbeaec072008-10-27 19:31:34 +0200530 atomic_dec(&pcol->sbi->s_curr_pending);
531
Boaz Harrosh154a9302011-08-26 21:00:32 -0700532 if (likely(!ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200533 good_bytes = pcol->length;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700534 ret = PAGE_WAS_NOT_IN_IO;
535 } else {
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300536 good_bytes = 0;
Boaz Harrosh154a9302011-08-26 21:00:32 -0700537 }
Boaz Harroshbeaec072008-10-27 19:31:34 +0200538
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200539 EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
Boaz Harroshbeaec072008-10-27 19:31:34 +0200540 " length=0x%lx nr_pages=%u\n",
541 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
542 pcol->nr_pages);
543
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200544 for (i = 0; i < pcol->nr_pages; i++) {
545 struct page *page = pcol->pages[i];
Boaz Harroshbeaec072008-10-27 19:31:34 +0200546 struct inode *inode = page->mapping->host;
547 int page_stat;
548
549 if (inode != pcol->inode)
550 continue; /* osd might add more pages to a bio */
551
552 if (likely(length < good_bytes))
553 page_stat = 0;
554 else
555 page_stat = ret;
556
557 update_write_page(page, page_stat);
558 unlock_page(page);
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200559 EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200560 inode->i_ino, page->index, page_stat);
561
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200562 length += PAGE_SIZE;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200563 }
564
565 pcol_free(pcol);
566 kfree(pcol);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200567 EXOFS_DBGMSG2("writepages_done END\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200568}
569
Boaz Harroshdd296612011-10-12 15:42:07 +0200570static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
571{
572 struct page_collect *pcol = priv;
573 pgoff_t index = offset / PAGE_SIZE;
574
575 if (!pcol->that_locked_page ||
576 (pcol->that_locked_page->index != index)) {
577 struct page *page = find_get_page(pcol->inode->i_mapping, index);
578
579 if (!page) {
580 page = find_or_create_page(pcol->inode->i_mapping,
581 index, GFP_NOFS);
582 if (unlikely(!page)) {
583 EXOFS_DBGMSG("grab_cache_page Failed "
584 "index=0x%llx\n", _LLU(index));
585 return NULL;
586 }
587 unlock_page(page);
588 }
589 if (PageDirty(page) || PageWriteback(page))
590 *uptodate = true;
591 else
592 *uptodate = PageUptodate(page);
593 EXOFS_DBGMSG("index=0x%lx uptodate=%d\n", index, *uptodate);
594 return page;
595 } else {
596 EXOFS_DBGMSG("YES that_locked_page index=0x%lx\n",
597 pcol->that_locked_page->index);
598 *uptodate = true;
599 return pcol->that_locked_page;
600 }
601}
602
603static void __r4w_put_page(void *priv, struct page *page)
604{
605 struct page_collect *pcol = priv;
606
607 if (pcol->that_locked_page != page) {
608 EXOFS_DBGMSG("index=0x%lx\n", page->index);
609 page_cache_release(page);
610 return;
611 }
612 EXOFS_DBGMSG("that_locked_page index=0x%lx\n", page->index);
613}
614
615static const struct _ore_r4w_op _r4w_op = {
616 .get_page = &__r4w_get_page,
617 .put_page = &__r4w_put_page,
618};
619
Boaz Harroshbeaec072008-10-27 19:31:34 +0200620static int write_exec(struct page_collect *pcol)
621{
622 struct exofs_i_info *oi = exofs_i(pcol->inode);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700623 struct ore_io_state *ios;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200624 struct page_collect *pcol_copy = NULL;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200625 int ret;
626
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200627 if (!pcol->pages)
Boaz Harroshbeaec072008-10-27 19:31:34 +0200628 return 0;
629
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200630 BUG_ON(pcol->ios);
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300631 ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false,
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200632 pcol->pg_first << PAGE_CACHE_SHIFT,
633 pcol->length, &pcol->ios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200634 if (unlikely(ret))
635 goto err;
636
Boaz Harroshbeaec072008-10-27 19:31:34 +0200637 pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
638 if (!pcol_copy) {
Joe Perches571f7f42010-10-21 22:17:17 -0700639 EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200640 ret = -ENOMEM;
641 goto err;
642 }
643
644 *pcol_copy = *pcol;
645
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200646 ios = pcol->ios;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200647 ios->pages = pcol_copy->pages;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200648 ios->done = writepages_done;
Boaz Harroshdd296612011-10-12 15:42:07 +0200649 ios->r4w = &_r4w_op;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200650 ios->private = pcol_copy;
651
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300652 /* pages ownership was passed to pcol_copy */
653 _pcol_reset(pcol);
654
655 ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
656 if (unlikely(ret))
657 goto err;
658
659 EXOFS_DBGMSG2("write_exec(0x%lx) offset=0x%llx length=0x%llx\n",
660 pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));
661
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700662 ret = ore_write(ios);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200663 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700664 EXOFS_ERR("write_exec: ore_write() Failed\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200665 goto err;
666 }
667
668 atomic_inc(&pcol->sbi->s_curr_pending);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200669 return 0;
670
671err:
672 _unlock_pcol_pages(pcol, ret, WRITE);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200673 pcol_free(pcol);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200674 kfree(pcol_copy);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200675
Boaz Harroshbeaec072008-10-27 19:31:34 +0200676 return ret;
677}
678
679/* writepage_strip is called either directly from writepage() or by the VFS from
680 * within write_cache_pages(), to add one more page to be written to storage.
681 * It will try to collect as many contiguous pages as possible. If a
682 * discontinuity is encountered or it runs out of resources it will submit the
683 * previous segment and will start a new collection.
684 * Eventually caller must submit the last segment if present.
685 */
686static int writepage_strip(struct page *page,
687 struct writeback_control *wbc_unused, void *data)
688{
689 struct page_collect *pcol = data;
690 struct inode *inode = pcol->inode;
691 struct exofs_i_info *oi = exofs_i(inode);
692 loff_t i_size = i_size_read(inode);
693 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
694 size_t len;
695 int ret;
696
697 BUG_ON(!PageLocked(page));
698
699 ret = wait_obj_created(oi);
700 if (unlikely(ret))
701 goto fail;
702
703 if (page->index < end_index)
704 /* in this case, the page is within the limits of the file */
705 len = PAGE_CACHE_SIZE;
706 else {
707 len = i_size & ~PAGE_CACHE_MASK;
708
709 if (page->index > end_index || !len) {
710 /* in this case, the page is outside the limits
711 * (truncate in progress)
712 */
713 ret = write_exec(pcol);
714 if (unlikely(ret))
715 goto fail;
716 if (PageError(page))
717 ClearPageError(page);
718 unlock_page(page);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200719 EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
720 "outside the limits\n",
721 inode->i_ino, page->index);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200722 return 0;
723 }
724 }
725
726try_again:
727
728 if (unlikely(pcol->pg_first == -1)) {
729 pcol->pg_first = page->index;
730 } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
731 page->index)) {
732 /* Discontinuity detected, split the request */
733 ret = write_exec(pcol);
734 if (unlikely(ret))
735 goto fail;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200736
737 EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
738 inode->i_ino, page->index);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200739 goto try_again;
740 }
741
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200742 if (!pcol->pages) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200743 ret = pcol_try_alloc(pcol);
744 if (unlikely(ret))
745 goto fail;
746 }
747
Boaz Harroshfe33cc12009-11-01 18:28:14 +0200748 EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200749 inode->i_ino, page->index, len);
750
751 ret = pcol_add_page(pcol, page, len);
752 if (unlikely(ret)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200753 EXOFS_DBGMSG2("Failed pcol_add_page "
Boaz Harroshbeaec072008-10-27 19:31:34 +0200754 "nr_pages=%u total_length=0x%lx\n",
755 pcol->nr_pages, pcol->length);
756
757 /* split the request, next loop will start again */
758 ret = write_exec(pcol);
759 if (unlikely(ret)) {
Joe Perches571f7f42010-10-21 22:17:17 -0700760 EXOFS_DBGMSG("write_exec failed => %d", ret);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200761 goto fail;
762 }
763
764 goto try_again;
765 }
766
767 BUG_ON(PageWriteback(page));
768 set_page_writeback(page);
769
770 return 0;
771
772fail:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200773 EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
774 inode->i_ino, page->index, ret);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200775 set_bit(AS_EIO, &page->mapping->flags);
776 unlock_page(page);
777 return ret;
778}
779
780static int exofs_writepages(struct address_space *mapping,
781 struct writeback_control *wbc)
782{
783 struct page_collect pcol;
784 long start, end, expected_pages;
785 int ret;
786
787 start = wbc->range_start >> PAGE_CACHE_SHIFT;
788 end = (wbc->range_end == LLONG_MAX) ?
789 start + mapping->nrpages :
790 wbc->range_end >> PAGE_CACHE_SHIFT;
791
792 if (start || end)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200793 expected_pages = end - start + 1;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200794 else
795 expected_pages = mapping->nrpages;
796
Boaz Harrosh06886a52009-11-08 14:54:08 +0200797 if (expected_pages < 32L)
798 expected_pages = 32L;
799
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200800 EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
Boaz Harrosh06886a52009-11-08 14:54:08 +0200801 "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
Boaz Harroshbeaec072008-10-27 19:31:34 +0200802 mapping->host->i_ino, wbc->range_start, wbc->range_end,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200803 mapping->nrpages, start, end, expected_pages);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200804
805 _pcol_init(&pcol, expected_pages, mapping->host);
806
807 ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300808 if (unlikely(ret)) {
Boaz Harroshbeaec072008-10-27 19:31:34 +0200809 EXOFS_ERR("write_cache_pages => %d\n", ret);
810 return ret;
811 }
812
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300813 ret = write_exec(&pcol);
814 if (unlikely(ret))
815 return ret;
816
817 if (wbc->sync_mode == WB_SYNC_ALL) {
818 return write_exec(&pcol); /* pump the last reminder */
819 } else if (pcol.nr_pages) {
820 /* not SYNC let the reminder join the next writeout */
821 unsigned i;
822
823 for (i = 0; i < pcol.nr_pages; i++) {
824 struct page *page = pcol.pages[i];
825
826 end_page_writeback(page);
827 set_page_dirty(page);
828 unlock_page(page);
829 }
830 }
831 return 0;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200832}
833
Boaz Harroshdd296612011-10-12 15:42:07 +0200834/*
Boaz Harroshbeaec072008-10-27 19:31:34 +0200835static int exofs_writepage(struct page *page, struct writeback_control *wbc)
836{
837 struct page_collect pcol;
838 int ret;
839
840 _pcol_init(&pcol, 1, page->mapping->host);
841
842 ret = writepage_strip(page, NULL, &pcol);
843 if (ret) {
844 EXOFS_ERR("exofs_writepage => %d\n", ret);
845 return ret;
846 }
847
848 return write_exec(&pcol);
849}
Boaz Harroshdd296612011-10-12 15:42:07 +0200850*/
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300851/* i_mutex held using inode->i_size directly */
852static void _write_failed(struct inode *inode, loff_t to)
853{
854 if (to > inode->i_size)
855 truncate_pagecache(inode, to, inode->i_size);
856}
857
Boaz Harroshbeaec072008-10-27 19:31:34 +0200858int exofs_write_begin(struct file *file, struct address_space *mapping,
859 loff_t pos, unsigned len, unsigned flags,
860 struct page **pagep, void **fsdata)
861{
862 int ret = 0;
863 struct page *page;
864
865 page = *pagep;
866 if (page == NULL) {
867 ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
868 fsdata);
869 if (ret) {
Joe Perches571f7f42010-10-21 22:17:17 -0700870 EXOFS_DBGMSG("simple_write_begin failed\n");
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300871 goto out;
Boaz Harroshbeaec072008-10-27 19:31:34 +0200872 }
873
874 page = *pagep;
875 }
876
877 /* read modify write */
878 if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
Boaz Harrosha8f14182010-11-22 18:02:45 +0200879 loff_t i_size = i_size_read(mapping->host);
880 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
881 size_t rlen;
882
883 if (page->index < end_index)
884 rlen = PAGE_CACHE_SIZE;
885 else if (page->index == end_index)
886 rlen = i_size & ~PAGE_CACHE_MASK;
887 else
888 rlen = 0;
889
890 if (!rlen) {
891 clear_highpage(page);
892 SetPageUptodate(page);
893 goto out;
894 }
895
Boaz Harroshbeaec072008-10-27 19:31:34 +0200896 ret = _readpage(page, true);
897 if (ret) {
898 /*SetPageError was done by _readpage. Is it ok?*/
899 unlock_page(page);
Boaz Harrosha8f14182010-11-22 18:02:45 +0200900 EXOFS_DBGMSG("__readpage failed\n");
Boaz Harroshbeaec072008-10-27 19:31:34 +0200901 }
902 }
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300903out:
904 if (unlikely(ret))
905 _write_failed(mapping->host, pos + len);
Boaz Harroshbeaec072008-10-27 19:31:34 +0200906
907 return ret;
908}
909
910static int exofs_write_begin_export(struct file *file,
911 struct address_space *mapping,
912 loff_t pos, unsigned len, unsigned flags,
913 struct page **pagep, void **fsdata)
914{
915 *pagep = NULL;
916
917 return exofs_write_begin(file, mapping, pos, len, flags, pagep,
918 fsdata);
919}
920
Boaz Harroshefd124b2009-12-27 17:01:42 +0200921static int exofs_write_end(struct file *file, struct address_space *mapping,
922 loff_t pos, unsigned len, unsigned copied,
923 struct page *page, void *fsdata)
924{
925 struct inode *inode = mapping->host;
926 /* According to comment in simple_write_end i_mutex is held */
927 loff_t i_size = inode->i_size;
928 int ret;
929
930 ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300931 if (unlikely(ret))
932 _write_failed(inode, pos + len);
933
934 /* TODO: once simple_write_end marks inode dirty remove */
Boaz Harroshefd124b2009-12-27 17:01:42 +0200935 if (i_size != inode->i_size)
936 mark_inode_dirty(inode);
937 return ret;
938}
939
Boaz Harrosh200b07002010-03-22 11:23:40 +0200940static int exofs_releasepage(struct page *page, gfp_t gfp)
941{
942 EXOFS_DBGMSG("page 0x%lx\n", page->index);
943 WARN_ON(1);
Boaz Harrosh85dc7872010-05-31 18:55:43 +0300944 return 0;
Boaz Harrosh200b07002010-03-22 11:23:40 +0200945}
946
947static void exofs_invalidatepage(struct page *page, unsigned long offset)
948{
Boaz Harrosh85dc7872010-05-31 18:55:43 +0300949 EXOFS_DBGMSG("page 0x%lx offset 0x%lx\n", page->index, offset);
Boaz Harrosh200b07002010-03-22 11:23:40 +0200950 WARN_ON(1);
Boaz Harrosh200b07002010-03-22 11:23:40 +0200951}
952
Boaz Harroshbeaec072008-10-27 19:31:34 +0200953const struct address_space_operations exofs_aops = {
954 .readpage = exofs_readpage,
955 .readpages = exofs_readpages,
Boaz Harroshdd296612011-10-12 15:42:07 +0200956 .writepage = NULL,
Boaz Harroshbeaec072008-10-27 19:31:34 +0200957 .writepages = exofs_writepages,
958 .write_begin = exofs_write_begin_export,
Boaz Harroshefd124b2009-12-27 17:01:42 +0200959 .write_end = exofs_write_end,
Boaz Harrosh200b07002010-03-22 11:23:40 +0200960 .releasepage = exofs_releasepage,
961 .set_page_dirty = __set_page_dirty_nobuffers,
962 .invalidatepage = exofs_invalidatepage,
963
964 /* Not implemented Yet */
965 .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
966 .direct_IO = NULL, /* TODO: Should be trivial to do */
967
968 /* With these NULL has special meaning or default is not exported */
Boaz Harrosh200b07002010-03-22 11:23:40 +0200969 .get_xip_mem = NULL,
970 .migratepage = NULL,
971 .launder_page = NULL,
972 .is_partially_uptodate = NULL,
973 .error_remove_page = NULL,
Boaz Harroshbeaec072008-10-27 19:31:34 +0200974};
975
Boaz Harroshe8062712008-10-27 18:37:02 +0200976/******************************************************************************
977 * INODE OPERATIONS
978 *****************************************************************************/
979
980/*
981 * Test whether an inode is a fast symlink.
982 */
983static inline int exofs_inode_is_fast_symlink(struct inode *inode)
984{
985 struct exofs_i_info *oi = exofs_i(inode);
986
987 return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
988}
989
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300990static int _do_truncate(struct inode *inode, loff_t newsize)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200991{
992 struct exofs_i_info *oi = exofs_i(inode);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700993 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200994 int ret;
995
996 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
997
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300998 ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +0300999 if (likely(!ret))
1000 truncate_setsize(inode, newsize);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001001
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001002 EXOFS_DBGMSG("(0x%lx) size=0x%llx ret=>%d\n",
1003 inode->i_ino, newsize, ret);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001004 return ret;
1005}
1006
Boaz Harroshe8062712008-10-27 18:37:02 +02001007/*
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001008 * Set inode attributes - update size attribute on OSD if needed,
1009 * otherwise just call generic functions.
Boaz Harroshe8062712008-10-27 18:37:02 +02001010 */
1011int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
1012{
1013 struct inode *inode = dentry->d_inode;
1014 int error;
1015
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001016 /* if we are about to modify an object, and it hasn't been
1017 * created yet, wait
1018 */
1019 error = wait_obj_created(exofs_i(inode));
1020 if (unlikely(error))
1021 return error;
1022
Boaz Harroshe8062712008-10-27 18:37:02 +02001023 error = inode_change_ok(inode, iattr);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001024 if (unlikely(error))
Boaz Harroshe8062712008-10-27 18:37:02 +02001025 return error;
1026
Christoph Hellwig10257742010-06-04 11:30:02 +02001027 if ((iattr->ia_valid & ATTR_SIZE) &&
1028 iattr->ia_size != i_size_read(inode)) {
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001029 error = _do_truncate(inode, iattr->ia_size);
1030 if (unlikely(error))
Christoph Hellwig10257742010-06-04 11:30:02 +02001031 return error;
1032 }
1033
1034 setattr_copy(inode, iattr);
1035 mark_inode_dirty(inode);
1036 return 0;
Boaz Harroshe8062712008-10-27 18:37:02 +02001037}
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001038
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001039static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
1040 EXOFS_APAGE_FS_DATA,
1041 EXOFS_ATTR_INODE_FILE_LAYOUT,
1042 0);
1043static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
1044 EXOFS_APAGE_FS_DATA,
1045 EXOFS_ATTR_INODE_DIR_LAYOUT,
1046 0);
1047
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001048/*
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001049 * Read the Linux inode info from the OSD, and return it as is. In exofs the
1050 * inode info is in an application specific page/attribute of the osd-object.
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001051 */
1052static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001053 struct exofs_fcb *inode)
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001054{
1055 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001056 struct osd_attr attrs[] = {
1057 [0] = g_attr_inode_data,
1058 [1] = g_attr_inode_file_layout,
1059 [2] = g_attr_inode_dir_layout,
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001060 };
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001061 struct ore_io_state *ios;
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001062 struct exofs_on_disk_inode_layout *layout;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001063 int ret;
1064
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001065 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001066 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001067 EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001068 return ret;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001069 }
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001070
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001071 attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
1072 attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001073
Boaz Harrosh06886a52009-11-08 14:54:08 +02001074 ios->in_attr = attrs;
1075 ios->in_attr_len = ARRAY_SIZE(attrs);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001076
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001077 ret = ore_read(ios);
Boaz Harrosh96391e22010-02-09 11:43:21 +02001078 if (unlikely(ret)) {
1079 EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001080 _LLU(oi->one_comp.obj.id), ret);
Boaz Harrosh96391e22010-02-09 11:43:21 +02001081 memset(inode, 0, sizeof(*inode));
1082 inode->i_mode = 0040000 | (0777 & ~022);
1083 /* If object is lost on target we might as well enable it's
1084 * delete.
1085 */
1086 if ((ret == -ENOENT) || (ret == -EINVAL))
1087 ret = 0;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001088 goto out;
Boaz Harrosh96391e22010-02-09 11:43:21 +02001089 }
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001090
Boaz Harrosh06886a52009-11-08 14:54:08 +02001091 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001092 if (ret) {
Boaz Harrosh06886a52009-11-08 14:54:08 +02001093 EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001094 goto out;
1095 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001096 WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
1097 memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001098
Boaz Harrosh06886a52009-11-08 14:54:08 +02001099 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001100 if (ret) {
Boaz Harroshd9c740d2010-01-28 11:58:08 +02001101 EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
1102 goto out;
1103 }
1104 if (attrs[1].len) {
1105 layout = attrs[1].val_ptr;
1106 if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
1107 EXOFS_ERR("%s: unsupported files layout %d\n",
1108 __func__, layout->gen_func);
1109 ret = -ENOTSUPP;
1110 goto out;
1111 }
1112 }
1113
1114 ret = extract_attr_from_ios(ios, &attrs[2]);
1115 if (ret) {
1116 EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
1117 goto out;
1118 }
1119 if (attrs[2].len) {
1120 layout = attrs[2].val_ptr;
1121 if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
1122 EXOFS_ERR("%s: unsupported meta-data layout %d\n",
1123 __func__, layout->gen_func);
1124 ret = -ENOTSUPP;
1125 goto out;
1126 }
1127 }
1128
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001129out:
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001130 ore_put_io_state(ios);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001131 return ret;
1132}
1133
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001134static void __oi_init(struct exofs_i_info *oi)
1135{
1136 init_waitqueue_head(&oi->i_wq);
1137 oi->i_flags = 0;
1138}
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001139/*
1140 * Fill in an inode read from the OSD and set it up for use
1141 */
1142struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
1143{
1144 struct exofs_i_info *oi;
1145 struct exofs_fcb fcb;
1146 struct inode *inode;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001147 int ret;
1148
1149 inode = iget_locked(sb, ino);
1150 if (!inode)
1151 return ERR_PTR(-ENOMEM);
1152 if (!(inode->i_state & I_NEW))
1153 return inode;
1154 oi = exofs_i(inode);
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001155 __oi_init(oi);
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001156 exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001157 exofs_oi_objno(oi));
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001158
1159 /* read the inode from the osd */
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001160 ret = exofs_get_inode(sb, oi, &fcb);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001161 if (ret)
1162 goto bad_inode;
1163
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001164 set_obj_created(oi);
1165
1166 /* copy stuff from on-disk struct to in-memory struct */
1167 inode->i_mode = le16_to_cpu(fcb.i_mode);
1168 inode->i_uid = le32_to_cpu(fcb.i_uid);
1169 inode->i_gid = le32_to_cpu(fcb.i_gid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001170 set_nlink(inode, le16_to_cpu(fcb.i_links_count));
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001171 inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
1172 inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
1173 inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
1174 inode->i_ctime.tv_nsec =
1175 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
1176 oi->i_commit_size = le64_to_cpu(fcb.i_size);
1177 i_size_write(inode, oi->i_commit_size);
1178 inode->i_blkbits = EXOFS_BLKSHIFT;
1179 inode->i_generation = le32_to_cpu(fcb.i_generation);
1180
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001181 oi->i_dir_start_lookup = 0;
1182
1183 if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
1184 ret = -ESTALE;
1185 goto bad_inode;
1186 }
1187
1188 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1189 if (fcb.i_data[0])
1190 inode->i_rdev =
1191 old_decode_dev(le32_to_cpu(fcb.i_data[0]));
1192 else
1193 inode->i_rdev =
1194 new_decode_dev(le32_to_cpu(fcb.i_data[1]));
1195 } else {
1196 memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
1197 }
1198
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -04001199 inode->i_mapping->backing_dev_info = sb->s_bdi;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001200 if (S_ISREG(inode->i_mode)) {
1201 inode->i_op = &exofs_file_inode_operations;
1202 inode->i_fop = &exofs_file_operations;
1203 inode->i_mapping->a_ops = &exofs_aops;
1204 } else if (S_ISDIR(inode->i_mode)) {
1205 inode->i_op = &exofs_dir_inode_operations;
1206 inode->i_fop = &exofs_dir_operations;
1207 inode->i_mapping->a_ops = &exofs_aops;
1208 } else if (S_ISLNK(inode->i_mode)) {
1209 if (exofs_inode_is_fast_symlink(inode))
1210 inode->i_op = &exofs_fast_symlink_inode_operations;
1211 else {
1212 inode->i_op = &exofs_symlink_inode_operations;
1213 inode->i_mapping->a_ops = &exofs_aops;
1214 }
1215 } else {
1216 inode->i_op = &exofs_special_inode_operations;
1217 if (fcb.i_data[0])
1218 init_special_inode(inode, inode->i_mode,
1219 old_decode_dev(le32_to_cpu(fcb.i_data[0])));
1220 else
1221 init_special_inode(inode, inode->i_mode,
1222 new_decode_dev(le32_to_cpu(fcb.i_data[1])));
1223 }
1224
1225 unlock_new_inode(inode);
1226 return inode;
1227
1228bad_inode:
1229 iget_failed(inode);
1230 return ERR_PTR(ret);
1231}
1232
1233int __exofs_wait_obj_created(struct exofs_i_info *oi)
1234{
1235 if (!obj_created(oi)) {
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001236 EXOFS_DBGMSG("!obj_created\n");
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001237 BUG_ON(!obj_2bcreated(oi));
1238 wait_event(oi->i_wq, obj_created(oi));
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001239 EXOFS_DBGMSG("wait_event done\n");
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001240 }
1241 return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
1242}
Boaz Harrosh1cea3122011-02-03 17:53:25 +02001243
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001244/*
1245 * Callback function from exofs_new_inode(). The important thing is that we
1246 * set the obj_created flag so that other methods know that the object exists on
1247 * the OSD.
1248 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001249static void create_done(struct ore_io_state *ios, void *p)
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001250{
1251 struct inode *inode = p;
1252 struct exofs_i_info *oi = exofs_i(inode);
1253 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
1254 int ret;
1255
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001256 ret = ore_check_io(ios, NULL);
1257 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001258
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001259 atomic_dec(&sbi->s_curr_pending);
1260
1261 if (unlikely(ret)) {
Joe Perches571f7f42010-10-21 22:17:17 -07001262 EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001263 _LLU(exofs_oi_objno(oi)),
1264 _LLU(oi->one_comp.obj.partition));
Boaz Harrosh06886a52009-11-08 14:54:08 +02001265 /*TODO: When FS is corrupted creation can fail, object already
1266 * exist. Get rid of this asynchronous creation, if exist
1267 * increment the obj counter and try the next object. Until we
1268 * succeed. All these dangling objects will be made into lost
1269 * files by chkfs.exofs
1270 */
1271 }
1272
1273 set_obj_created(oi);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001274
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001275 wake_up(&oi->i_wq);
1276}
1277
1278/*
1279 * Set up a new inode and create an object for it on the OSD
1280 */
Al Virobef41c22011-07-26 03:07:49 -04001281struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001282{
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001283 struct super_block *sb = dir->i_sb;
1284 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001285 struct inode *inode;
1286 struct exofs_i_info *oi;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001287 struct ore_io_state *ios;
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001288 int ret;
1289
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001290 inode = new_inode(sb);
1291 if (!inode)
1292 return ERR_PTR(-ENOMEM);
1293
1294 oi = exofs_i(inode);
Boaz Harrosh9cfdc7a2009-08-04 20:40:29 +03001295 __oi_init(oi);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001296
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001297 set_obj_2bcreated(oi);
1298
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -04001299 inode->i_mapping->backing_dev_info = sb->s_bdi;
Dmitry Monakhove00117f2010-03-04 17:31:48 +03001300 inode_init_owner(inode, dir, mode);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001301 inode->i_ino = sbi->s_nextid++;
1302 inode->i_blkbits = EXOFS_BLKSHIFT;
1303 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1304 oi->i_commit_size = inode->i_size = 0;
1305 spin_lock(&sbi->s_next_gen_lock);
1306 inode->i_generation = sbi->s_next_generation++;
1307 spin_unlock(&sbi->s_next_gen_lock);
1308 insert_inode_hash(inode);
1309
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001310 exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001311 exofs_oi_objno(oi));
Boaz Harrosh1cea3122011-02-03 17:53:25 +02001312 exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
1313
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001314 mark_inode_dirty(inode);
1315
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001316 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001317 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001318 EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
Boaz Harrosh06886a52009-11-08 14:54:08 +02001319 return ERR_PTR(ret);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001320 }
1321
Boaz Harrosh06886a52009-11-08 14:54:08 +02001322 ios->done = create_done;
1323 ios->private = inode;
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001324
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001325 ret = ore_create(ios);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001326 if (ret) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001327 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001328 return ERR_PTR(ret);
Boaz Harroshe6af00f2008-10-28 15:38:12 +02001329 }
1330 atomic_inc(&sbi->s_curr_pending);
1331
1332 return inode;
1333}
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001334
1335/*
1336 * struct to pass two arguments to update_inode's callback
1337 */
1338struct updatei_args {
1339 struct exofs_sb_info *sbi;
1340 struct exofs_fcb fcb;
1341};
1342
1343/*
1344 * Callback function from exofs_update_inode().
1345 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001346static void updatei_done(struct ore_io_state *ios, void *p)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001347{
1348 struct updatei_args *args = p;
1349
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001350 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001351
1352 atomic_dec(&args->sbi->s_curr_pending);
1353
1354 kfree(args);
1355}
1356
1357/*
1358 * Write the inode to the OSD. Just fill up the struct, and set the attribute
1359 * synchronously or asynchronously depending on the do_sync flag.
1360 */
1361static int exofs_update_inode(struct inode *inode, int do_sync)
1362{
1363 struct exofs_i_info *oi = exofs_i(inode);
1364 struct super_block *sb = inode->i_sb;
1365 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001366 struct ore_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001367 struct osd_attr attr;
1368 struct exofs_fcb *fcb;
1369 struct updatei_args *args;
1370 int ret;
1371
1372 args = kzalloc(sizeof(*args), GFP_KERNEL);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001373 if (!args) {
Joe Perches571f7f42010-10-21 22:17:17 -07001374 EXOFS_DBGMSG("Failed kzalloc of args\n");
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001375 return -ENOMEM;
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001376 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001377
1378 fcb = &args->fcb;
1379
1380 fcb->i_mode = cpu_to_le16(inode->i_mode);
1381 fcb->i_uid = cpu_to_le32(inode->i_uid);
1382 fcb->i_gid = cpu_to_le32(inode->i_gid);
1383 fcb->i_links_count = cpu_to_le16(inode->i_nlink);
1384 fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1385 fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1386 fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1387 oi->i_commit_size = i_size_read(inode);
1388 fcb->i_size = cpu_to_le64(oi->i_commit_size);
1389 fcb->i_generation = cpu_to_le32(inode->i_generation);
1390
1391 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1392 if (old_valid_dev(inode->i_rdev)) {
1393 fcb->i_data[0] =
1394 cpu_to_le32(old_encode_dev(inode->i_rdev));
1395 fcb->i_data[1] = 0;
1396 } else {
1397 fcb->i_data[0] = 0;
1398 fcb->i_data[1] =
1399 cpu_to_le32(new_encode_dev(inode->i_rdev));
1400 fcb->i_data[2] = 0;
1401 }
1402 } else
1403 memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
1404
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001405 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001406 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001407 EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001408 goto free_args;
1409 }
1410
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001411 attr = g_attr_inode_data;
1412 attr.val_ptr = fcb;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001413 ios->out_attr_len = 1;
1414 ios->out_attr = &attr;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001415
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001416 wait_obj_created(oi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001417
Boaz Harrosh06886a52009-11-08 14:54:08 +02001418 if (!do_sync) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001419 args->sbi = sbi;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001420 ios->done = updatei_done;
1421 ios->private = args;
1422 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001423
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001424 ret = ore_write(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001425 if (!do_sync && !ret) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001426 atomic_inc(&sbi->s_curr_pending);
1427 goto out; /* deallocation in updatei_done */
1428 }
1429
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001430 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001431free_args:
1432 kfree(args);
1433out:
Boaz Harrosh34ce4e72009-12-15 19:34:17 +02001434 EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
1435 inode->i_ino, do_sync, ret);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001436 return ret;
1437}
1438
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001439int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001440{
Nick Piggin97178b72010-11-25 12:47:15 +02001441 /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
1442 return exofs_update_inode(inode, 1);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001443}
1444
1445/*
1446 * Callback function from exofs_delete_inode() - don't have much cleaning up to
1447 * do.
1448 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001449static void delete_done(struct ore_io_state *ios, void *p)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001450{
Boaz Harrosh06886a52009-11-08 14:54:08 +02001451 struct exofs_sb_info *sbi = p;
1452
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001453 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001454
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001455 atomic_dec(&sbi->s_curr_pending);
1456}
1457
1458/*
1459 * Called when the refcount of an inode reaches zero. We remove the object
1460 * from the OSD here. We make sure the object was created before we try and
1461 * delete it.
1462 */
Al Viro4ec70c92010-06-07 11:42:26 -04001463void exofs_evict_inode(struct inode *inode)
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001464{
1465 struct exofs_i_info *oi = exofs_i(inode);
1466 struct super_block *sb = inode->i_sb;
1467 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001468 struct ore_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001469 int ret;
1470
1471 truncate_inode_pages(&inode->i_data, 0);
1472
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001473 /* TODO: should do better here */
Al Viro4ec70c92010-06-07 11:42:26 -04001474 if (inode->i_nlink || is_bad_inode(inode))
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001475 goto no_delete;
1476
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001477 inode->i_size = 0;
Jan Karadbd57682012-05-03 14:48:02 +02001478 clear_inode(inode);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001479
Boaz Harroshfe2fd9e2010-10-16 19:14:01 +11001480 /* if we are deleting an obj that hasn't been created yet, wait.
1481 * This also makes sure that create_done cannot be called with an
1482 * already evicted inode.
1483 */
1484 wait_obj_created(oi);
1485 /* ignore the error, attempt a remove anyway */
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001486
1487 /* Now Remove the OSD objects */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001488 ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001489 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001490 EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
Boaz Harrosh2f246fd2010-06-09 18:23:18 +03001491 return;
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001492 }
1493
Boaz Harrosh06886a52009-11-08 14:54:08 +02001494 ios->done = delete_done;
1495 ios->private = sbi;
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001496
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001497 ret = ore_remove(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001498 if (ret) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001499 EXOFS_ERR("%s: ore_remove failed\n", __func__);
1500 ore_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001501 return;
1502 }
1503 atomic_inc(&sbi->s_curr_pending);
1504
1505 return;
1506
1507no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02001508 clear_inode(inode);
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001509}