inode.c 36.6 KB
Newer Older
1 2
/*
 * Copyright (C) 2005, 2006
B
Boaz Harrosh 已提交
3
 * Avishay Traeger (avishay@gmail.com)
4
 * Copyright (C) 2008, 2009
5
 * Boaz Harrosh <ooo@electrozaur.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Copyrights for code taken from ext2:
 *     Copyright (C) 1992, 1993, 1994, 1995
 *     Remy Card (card@masi.ibp.fr)
 *     Laboratoire MASI - Institut Blaise Pascal
 *     Universite Pierre et Marie Curie (Paris VI)
 *     from
 *     linux/fs/minix/inode.c
 *     Copyright (C) 1991, 1992  Linus Torvalds
 *
 * This file is part of exofs.
 *
 * exofs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.  Since it is based on ext2, and the only
 * valid version of GPL for the Linux kernel is version 2, the only valid
 * version of GPL for exofs is version 2.
 *
 * exofs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with exofs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

34
#include <linux/slab.h>
35 36 37

#include "exofs.h"

B
Boaz Harrosh 已提交
38 39
#define EXOFS_DBGMSG2(M...) do {} while (0)

40
unsigned exofs_max_io_pages(struct ore_layout *layout,
41 42
			    unsigned expected_pages)
{
43 44
	unsigned pages = min_t(unsigned, expected_pages,
			       layout->max_io_length / PAGE_SIZE);
45 46 47 48

	return pages;
}

B
Boaz Harrosh 已提交
49 50 51 52
struct page_collect {
	struct exofs_sb_info *sbi;
	struct inode *inode;
	unsigned expected_pages;
53
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
54

55 56
	struct page **pages;
	unsigned alloc_pages;
B
Boaz Harrosh 已提交
57 58 59
	unsigned nr_pages;
	unsigned long length;
	loff_t pg_first; /* keep 64bit also in 32-arches */
60 61 62
	bool read_4_write; /* This means two things: that the read is sync
			    * And the pages should not be unlocked.
			    */
63
	struct page *that_locked_page;
B
Boaz Harrosh 已提交
64 65 66
};

static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
67
		       struct inode *inode)
B
Boaz Harrosh 已提交
68 69 70 71 72 73 74
{
	struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;

	pcol->sbi = sbi;
	pcol->inode = inode;
	pcol->expected_pages = expected_pages;

75
	pcol->ios = NULL;
76 77
	pcol->pages = NULL;
	pcol->alloc_pages = 0;
B
Boaz Harrosh 已提交
78 79 80
	pcol->nr_pages = 0;
	pcol->length = 0;
	pcol->pg_first = -1;
81
	pcol->read_4_write = false;
82
	pcol->that_locked_page = NULL;
B
Boaz Harrosh 已提交
83 84 85 86 87 88
}

static void _pcol_reset(struct page_collect *pcol)
{
	pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);

89 90
	pcol->pages = NULL;
	pcol->alloc_pages = 0;
B
Boaz Harrosh 已提交
91 92 93
	pcol->nr_pages = 0;
	pcol->length = 0;
	pcol->pg_first = -1;
94
	pcol->ios = NULL;
95
	pcol->that_locked_page = NULL;
B
Boaz Harrosh 已提交
96 97 98 99 100

	/* this is probably the end of the loop but in writes
	 * it might not end here. don't be left with nothing
	 */
	if (!pcol->expected_pages)
101 102
		pcol->expected_pages =
				exofs_max_io_pages(&pcol->sbi->layout, ~0);
B
Boaz Harrosh 已提交
103 104 105 106
}

static int pcol_try_alloc(struct page_collect *pcol)
{
107
	unsigned pages;
108

109
	/* TODO: easily support bio chaining */
110
	pages =  exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages);
111

B
Boaz Harrosh 已提交
112
	for (; pages; pages >>= 1) {
113 114 115 116
		pcol->pages = kmalloc(pages * sizeof(struct page *),
				      GFP_KERNEL);
		if (likely(pcol->pages)) {
			pcol->alloc_pages = pages;
B
Boaz Harrosh 已提交
117
			return 0;
118
		}
B
Boaz Harrosh 已提交
119 120
	}

121
	EXOFS_ERR("Failed to kmalloc expected_pages=%u\n",
B
Boaz Harrosh 已提交
122 123 124 125 126 127
		  pcol->expected_pages);
	return -ENOMEM;
}

static void pcol_free(struct page_collect *pcol)
{
128 129
	kfree(pcol->pages);
	pcol->pages = NULL;
130 131

	if (pcol->ios) {
132
		ore_put_io_state(pcol->ios);
133 134
		pcol->ios = NULL;
	}
B
Boaz Harrosh 已提交
135 136 137 138 139
}

static int pcol_add_page(struct page_collect *pcol, struct page *page,
			 unsigned len)
{
140
	if (unlikely(pcol->nr_pages >= pcol->alloc_pages))
B
Boaz Harrosh 已提交
141 142
		return -ENOMEM;

143
	pcol->pages[pcol->nr_pages++] = page;
B
Boaz Harrosh 已提交
144 145 146 147
	pcol->length += len;
	return 0;
}

148
enum {PAGE_WAS_NOT_IN_IO = 17};
B
Boaz Harrosh 已提交
149 150
static int update_read_page(struct page *page, int ret)
{
151 152
	switch (ret) {
	case 0:
B
Boaz Harrosh 已提交
153 154 155 156
		/* Everything is OK */
		SetPageUptodate(page);
		if (PageError(page))
			ClearPageError(page);
157 158
		break;
	case -EFAULT:
B
Boaz Harrosh 已提交
159 160 161 162 163 164 165 166 167 168 169
		/* In this case we were trying to read something that wasn't on
		 * disk yet - return a page full of zeroes.  This should be OK,
		 * because the object should be empty (if there was a write
		 * before this read, the read would be waiting with the page
		 * locked */
		clear_highpage(page);

		SetPageUptodate(page);
		if (PageError(page))
			ClearPageError(page);
		EXOFS_DBGMSG("recovered read error\n");
170 171 172 173 174
		/* fall through */
	case PAGE_WAS_NOT_IN_IO:
		ret = 0; /* recovered error */
		break;
	default:
B
Boaz Harrosh 已提交
175
		SetPageError(page);
176
	}
B
Boaz Harrosh 已提交
177 178 179 180 181
	return ret;
}

static void update_write_page(struct page *page, int ret)
{
182 183 184
	if (unlikely(ret == PAGE_WAS_NOT_IN_IO))
		return; /* don't pass start don't collect $200 */

B
Boaz Harrosh 已提交
185 186 187 188 189 190 191 192 193 194
	if (ret) {
		mapping_set_error(page->mapping, ret);
		SetPageError(page);
	}
	end_page_writeback(page);
}

/* Called at the end of reads, to optionally unlock pages and update their
 * status.
 */
195
static int __readpages_done(struct page_collect *pcol)
B
Boaz Harrosh 已提交
196 197 198 199
{
	int i;
	u64 good_bytes;
	u64 length = 0;
B
Boaz Harrosh 已提交
200
	int ret = ore_check_io(pcol->ios, NULL);
B
Boaz Harrosh 已提交
201

202
	if (likely(!ret)) {
B
Boaz Harrosh 已提交
203
		good_bytes = pcol->length;
204 205
		ret = PAGE_WAS_NOT_IN_IO;
	} else {
B
Boaz Harrosh 已提交
206
		good_bytes = 0;
207
	}
B
Boaz Harrosh 已提交
208

B
Boaz Harrosh 已提交
209
	EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
B
Boaz Harrosh 已提交
210 211 212 213
		     " length=0x%lx nr_pages=%u\n",
		     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
		     pcol->nr_pages);

214 215
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
216 217 218 219 220 221 222 223 224 225 226
		struct inode *inode = page->mapping->host;
		int page_stat;

		if (inode != pcol->inode)
			continue; /* osd might add more pages at end */

		if (likely(length < good_bytes))
			page_stat = 0;
		else
			page_stat = ret;

B
Boaz Harrosh 已提交
227
		EXOFS_DBGMSG2("    readpages_done(0x%lx, 0x%lx) %s\n",
B
Boaz Harrosh 已提交
228 229 230 231
			  inode->i_ino, page->index,
			  page_stat ? "bad_bytes" : "good_bytes");

		ret = update_read_page(page, page_stat);
232
		if (!pcol->read_4_write)
B
Boaz Harrosh 已提交
233
			unlock_page(page);
234
		length += PAGE_SIZE;
B
Boaz Harrosh 已提交
235 236 237
	}

	pcol_free(pcol);
B
Boaz Harrosh 已提交
238
	EXOFS_DBGMSG2("readpages_done END\n");
B
Boaz Harrosh 已提交
239 240 241 242
	return ret;
}

/* callback of async reads */
243
static void readpages_done(struct ore_io_state *ios, void *p)
B
Boaz Harrosh 已提交
244 245 246
{
	struct page_collect *pcol = p;

247
	__readpages_done(pcol);
B
Boaz Harrosh 已提交
248
	atomic_dec(&pcol->sbi->s_curr_pending);
249
	kfree(pcol);
B
Boaz Harrosh 已提交
250 251 252 253 254 255
}

static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
{
	int i;

256 257
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
258 259 260 261 262 263 264 265 266 267

		if (rw == READ)
			update_read_page(page, ret);
		else
			update_write_page(page, ret);

		unlock_page(page);
	}
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static int _maybe_not_all_in_one_io(struct ore_io_state *ios,
	struct page_collect *pcol_src, struct page_collect *pcol)
{
	/* length was wrong or offset was not page aligned */
	BUG_ON(pcol_src->nr_pages < ios->nr_pages);

	if (pcol_src->nr_pages > ios->nr_pages) {
		struct page **src_page;
		unsigned pages_less = pcol_src->nr_pages - ios->nr_pages;
		unsigned long len_less = pcol_src->length - ios->length;
		unsigned i;
		int ret;

		/* This IO was trimmed */
		pcol_src->nr_pages = ios->nr_pages;
		pcol_src->length = ios->length;

		/* Left over pages are passed to the next io */
		pcol->expected_pages += pages_less;
		pcol->nr_pages = pages_less;
		pcol->length = len_less;
		src_page = pcol_src->pages + pcol_src->nr_pages;
		pcol->pg_first = (*src_page)->index;

		ret = pcol_try_alloc(pcol);
		if (unlikely(ret))
			return ret;

		for (i = 0; i < pages_less; ++i)
			pcol->pages[i] = *src_page++;

		EXOFS_DBGMSG("Length was adjusted nr_pages=0x%x "
			"pages_less=0x%x expected_pages=0x%x "
			"next_offset=0x%llx next_len=0x%lx\n",
			pcol_src->nr_pages, pages_less, pcol->expected_pages,
			pcol->pg_first * PAGE_SIZE, pcol->length);
	}
	return 0;
}

308
static int read_exec(struct page_collect *pcol)
B
Boaz Harrosh 已提交
309 310
{
	struct exofs_i_info *oi = exofs_i(pcol->inode);
311
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
312 313 314
	struct page_collect *pcol_copy = NULL;
	int ret;

315
	if (!pcol->pages)
B
Boaz Harrosh 已提交
316 317
		return 0;

318
	if (!pcol->ios) {
319
		int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true,
320
					     pcol->pg_first << PAGE_SHIFT,
321 322 323 324 325 326 327
					     pcol->length, &pcol->ios);

		if (ret)
			return ret;
	}

	ios = pcol->ios;
328
	ios->pages = pcol->pages;
B
Boaz Harrosh 已提交
329

330
	if (pcol->read_4_write) {
331
		ore_read(pcol->ios);
332
		return __readpages_done(pcol);
B
Boaz Harrosh 已提交
333 334 335 336 337 338 339 340 341
	}

	pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
	if (!pcol_copy) {
		ret = -ENOMEM;
		goto err;
	}

	*pcol_copy = *pcol;
342 343
	ios->done = readpages_done;
	ios->private = pcol_copy;
344 345 346 347 348 349 350 351 352 353 354

	/* pages ownership was passed to pcol_copy */
	_pcol_reset(pcol);

	ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
	if (unlikely(ret))
		goto err;

	EXOFS_DBGMSG2("read_exec(0x%lx) offset=0x%llx length=0x%llx\n",
		pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));

355
	ret = ore_read(ios);
B
Boaz Harrosh 已提交
356 357 358 359 360 361 362 363
	if (unlikely(ret))
		goto err;

	atomic_inc(&pcol->sbi->s_curr_pending);

	return 0;

err:
364 365 366 367
	if (!pcol_copy) /* Failed before ownership transfer */
		pcol_copy = pcol;
	_unlock_pcol_pages(pcol_copy, ret, READ);
	pcol_free(pcol_copy);
B
Boaz Harrosh 已提交
368
	kfree(pcol_copy);
369

B
Boaz Harrosh 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	return ret;
}

/* readpage_strip is called either directly from readpage() or by the VFS from
 * within read_cache_pages(), to add one more page to be read. It will try to
 * collect as many contiguous pages as posible. If a discontinuity is
 * encountered, or it runs out of resources, it will submit the previous segment
 * and will start a new collection. Eventually caller must submit the last
 * segment if present.
 */
static int readpage_strip(void *data, struct page *page)
{
	struct page_collect *pcol = data;
	struct inode *inode = pcol->inode;
	struct exofs_i_info *oi = exofs_i(inode);
	loff_t i_size = i_size_read(inode);
386
	pgoff_t end_index = i_size >> PAGE_SHIFT;
B
Boaz Harrosh 已提交
387 388 389
	size_t len;
	int ret;

390 391
	BUG_ON(!PageLocked(page));

B
Boaz Harrosh 已提交
392 393 394 395 396
	/* FIXME: Just for debugging, will be removed */
	if (PageUptodate(page))
		EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
			  page->index);

397 398
	pcol->that_locked_page = page;

B
Boaz Harrosh 已提交
399
	if (page->index < end_index)
400
		len = PAGE_SIZE;
B
Boaz Harrosh 已提交
401
	else if (page->index == end_index)
402
		len = i_size & ~PAGE_MASK;
B
Boaz Harrosh 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415
	else
		len = 0;

	if (!len || !obj_created(oi)) {
		/* this will be out of bounds, or doesn't exist yet.
		 * Current page is cleared and the request is split
		 */
		clear_highpage(page);

		SetPageUptodate(page);
		if (PageError(page))
			ClearPageError(page);

416 417
		if (!pcol->read_4_write)
			unlock_page(page);
B
Boaz Harrosh 已提交
418 419 420 421
		EXOFS_DBGMSG("readpage_strip(0x%lx) empty page len=%zx "
			     "read_4_write=%d index=0x%lx end_index=0x%lx "
			     "splitting\n", inode->i_ino, len,
			     pcol->read_4_write, page->index, end_index);
B
Boaz Harrosh 已提交
422

423
		return read_exec(pcol);
B
Boaz Harrosh 已提交
424 425 426 427 428 429 430 431 432
	}

try_again:

	if (unlikely(pcol->pg_first == -1)) {
		pcol->pg_first = page->index;
	} else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
		   page->index)) {
		/* Discontinuity detected, split the request */
433
		ret = read_exec(pcol);
B
Boaz Harrosh 已提交
434 435 436 437 438
		if (unlikely(ret))
			goto fail;
		goto try_again;
	}

439
	if (!pcol->pages) {
B
Boaz Harrosh 已提交
440 441 442 443 444
		ret = pcol_try_alloc(pcol);
		if (unlikely(ret))
			goto fail;
	}

445 446
	if (len != PAGE_SIZE)
		zero_user(page, len, PAGE_SIZE - len);
B
Boaz Harrosh 已提交
447

B
Boaz Harrosh 已提交
448
	EXOFS_DBGMSG2("    readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
B
Boaz Harrosh 已提交
449 450 451 452
		     inode->i_ino, page->index, len);

	ret = pcol_add_page(pcol, page, len);
	if (ret) {
B
Boaz Harrosh 已提交
453
		EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
B
Boaz Harrosh 已提交
454 455 456 457
			  "this_len=0x%zx nr_pages=%u length=0x%lx\n",
			  page, len, pcol->nr_pages, pcol->length);

		/* split the request, and start again with current page */
458
		ret = read_exec(pcol);
B
Boaz Harrosh 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
		if (unlikely(ret))
			goto fail;

		goto try_again;
	}

	return 0;

fail:
	/* SetPageError(page); ??? */
	unlock_page(page);
	return ret;
}

static int exofs_readpages(struct file *file, struct address_space *mapping,
			   struct list_head *pages, unsigned nr_pages)
{
	struct page_collect pcol;
	int ret;

	_pcol_init(&pcol, nr_pages, mapping->host);

	ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
	if (ret) {
		EXOFS_ERR("read_cache_pages => %d\n", ret);
		return ret;
	}

487 488 489 490
	ret = read_exec(&pcol);
	if (unlikely(ret))
		return ret;

491
	return read_exec(&pcol);
B
Boaz Harrosh 已提交
492 493
}

494
static int _readpage(struct page *page, bool read_4_write)
B
Boaz Harrosh 已提交
495 496 497 498 499 500
{
	struct page_collect pcol;
	int ret;

	_pcol_init(&pcol, 1, page->mapping->host);

501
	pcol.read_4_write = read_4_write;
B
Boaz Harrosh 已提交
502 503 504 505 506 507
	ret = readpage_strip(&pcol, page);
	if (ret) {
		EXOFS_ERR("_readpage => %d\n", ret);
		return ret;
	}

508
	return read_exec(&pcol);
B
Boaz Harrosh 已提交
509 510 511 512 513 514 515 516 517 518
}

/*
 * We don't need the file
 */
static int exofs_readpage(struct file *file, struct page *page)
{
	return _readpage(page, false);
}

519
/* Callback for osd_write. All writes are asynchronous */
520
static void writepages_done(struct ore_io_state *ios, void *p)
B
Boaz Harrosh 已提交
521 522 523 524 525
{
	struct page_collect *pcol = p;
	int i;
	u64  good_bytes;
	u64  length = 0;
B
Boaz Harrosh 已提交
526
	int ret = ore_check_io(ios, NULL);
B
Boaz Harrosh 已提交
527 528 529

	atomic_dec(&pcol->sbi->s_curr_pending);

530
	if (likely(!ret)) {
B
Boaz Harrosh 已提交
531
		good_bytes = pcol->length;
532 533
		ret = PAGE_WAS_NOT_IN_IO;
	} else {
B
Boaz Harrosh 已提交
534
		good_bytes = 0;
535
	}
B
Boaz Harrosh 已提交
536

B
Boaz Harrosh 已提交
537
	EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
B
Boaz Harrosh 已提交
538 539 540 541
		     " length=0x%lx nr_pages=%u\n",
		     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
		     pcol->nr_pages);

542 543
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556
		struct inode *inode = page->mapping->host;
		int page_stat;

		if (inode != pcol->inode)
			continue; /* osd might add more pages to a bio */

		if (likely(length < good_bytes))
			page_stat = 0;
		else
			page_stat = ret;

		update_write_page(page, page_stat);
		unlock_page(page);
B
Boaz Harrosh 已提交
557
		EXOFS_DBGMSG2("    writepages_done(0x%lx, 0x%lx) status=%d\n",
B
Boaz Harrosh 已提交
558 559
			     inode->i_ino, page->index, page_stat);

560
		length += PAGE_SIZE;
B
Boaz Harrosh 已提交
561 562 563 564
	}

	pcol_free(pcol);
	kfree(pcol);
B
Boaz Harrosh 已提交
565
	EXOFS_DBGMSG2("writepages_done END\n");
B
Boaz Harrosh 已提交
566 567
}

568 569 570 571 572 573 574
static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
{
	struct page_collect *pcol = priv;
	pgoff_t index = offset / PAGE_SIZE;

	if (!pcol->that_locked_page ||
	    (pcol->that_locked_page->index != index)) {
575 576
		struct page *page;
		loff_t i_size = i_size_read(pcol->inode);
577

578 579
		if (offset >= i_size) {
			*uptodate = true;
B
Boaz Harrosh 已提交
580
			EXOFS_DBGMSG2("offset >= i_size index=0x%lx\n", index);
581 582 583 584
			return ZERO_PAGE(0);
		}

		page =  find_get_page(pcol->inode->i_mapping, index);
585 586 587 588 589 590 591 592 593 594
		if (!page) {
			page = find_or_create_page(pcol->inode->i_mapping,
						   index, GFP_NOFS);
			if (unlikely(!page)) {
				EXOFS_DBGMSG("grab_cache_page Failed "
					"index=0x%llx\n", _LLU(index));
				return NULL;
			}
			unlock_page(page);
		}
595
		*uptodate = PageUptodate(page);
B
Boaz Harrosh 已提交
596
		EXOFS_DBGMSG2("index=0x%lx uptodate=%d\n", index, *uptodate);
597 598
		return page;
	} else {
B
Boaz Harrosh 已提交
599
		EXOFS_DBGMSG2("YES that_locked_page index=0x%lx\n",
600 601 602 603 604 605 606 607 608 609
			     pcol->that_locked_page->index);
		*uptodate = true;
		return pcol->that_locked_page;
	}
}

static void __r4w_put_page(void *priv, struct page *page)
{
	struct page_collect *pcol = priv;

610
	if ((pcol->that_locked_page != page) && (ZERO_PAGE(0) != page)) {
B
Boaz Harrosh 已提交
611
		EXOFS_DBGMSG2("index=0x%lx\n", page->index);
612
		put_page(page);
613 614
		return;
	}
B
Boaz Harrosh 已提交
615
	EXOFS_DBGMSG2("that_locked_page index=0x%lx\n",
616
		     ZERO_PAGE(0) == page ? -1 : page->index);
617 618 619 620 621 622 623
}

static const struct _ore_r4w_op _r4w_op = {
	.get_page = &__r4w_get_page,
	.put_page = &__r4w_put_page,
};

B
Boaz Harrosh 已提交
624 625 626
static int write_exec(struct page_collect *pcol)
{
	struct exofs_i_info *oi = exofs_i(pcol->inode);
627
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
628 629 630
	struct page_collect *pcol_copy = NULL;
	int ret;

631
	if (!pcol->pages)
B
Boaz Harrosh 已提交
632 633
		return 0;

634
	BUG_ON(pcol->ios);
635
	ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false,
636
				 pcol->pg_first << PAGE_SHIFT,
637 638 639 640
				 pcol->length, &pcol->ios);
	if (unlikely(ret))
		goto err;

B
Boaz Harrosh 已提交
641 642
	pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
	if (!pcol_copy) {
643
		EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n");
B
Boaz Harrosh 已提交
644 645 646 647 648 649
		ret = -ENOMEM;
		goto err;
	}

	*pcol_copy = *pcol;

650
	ios = pcol->ios;
651
	ios->pages = pcol_copy->pages;
652
	ios->done = writepages_done;
653
	ios->r4w = &_r4w_op;
654 655
	ios->private = pcol_copy;

656 657 658 659 660 661 662 663 664 665
	/* pages ownership was passed to pcol_copy */
	_pcol_reset(pcol);

	ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
	if (unlikely(ret))
		goto err;

	EXOFS_DBGMSG2("write_exec(0x%lx) offset=0x%llx length=0x%llx\n",
		pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));

666
	ret = ore_write(ios);
B
Boaz Harrosh 已提交
667
	if (unlikely(ret)) {
668
		EXOFS_ERR("write_exec: ore_write() Failed\n");
B
Boaz Harrosh 已提交
669 670 671 672 673 674 675
		goto err;
	}

	atomic_inc(&pcol->sbi->s_curr_pending);
	return 0;

err:
676 677 678 679
	if (!pcol_copy) /* Failed before ownership transfer */
		pcol_copy = pcol;
	_unlock_pcol_pages(pcol_copy, ret, WRITE);
	pcol_free(pcol_copy);
B
Boaz Harrosh 已提交
680
	kfree(pcol_copy);
681

B
Boaz Harrosh 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	return ret;
}

/* writepage_strip is called either directly from writepage() or by the VFS from
 * within write_cache_pages(), to add one more page to be written to storage.
 * It will try to collect as many contiguous pages as possible. If a
 * discontinuity is encountered or it runs out of resources it will submit the
 * previous segment and will start a new collection.
 * Eventually caller must submit the last segment if present.
 */
static int writepage_strip(struct page *page,
			   struct writeback_control *wbc_unused, void *data)
{
	struct page_collect *pcol = data;
	struct inode *inode = pcol->inode;
	struct exofs_i_info *oi = exofs_i(inode);
	loff_t i_size = i_size_read(inode);
699
	pgoff_t end_index = i_size >> PAGE_SHIFT;
B
Boaz Harrosh 已提交
700 701 702 703 704 705 706 707 708 709 710
	size_t len;
	int ret;

	BUG_ON(!PageLocked(page));

	ret = wait_obj_created(oi);
	if (unlikely(ret))
		goto fail;

	if (page->index < end_index)
		/* in this case, the page is within the limits of the file */
711
		len = PAGE_SIZE;
B
Boaz Harrosh 已提交
712
	else {
713
		len = i_size & ~PAGE_MASK;
B
Boaz Harrosh 已提交
714 715 716 717 718 719 720 721 722 723 724

		if (page->index > end_index || !len) {
			/* in this case, the page is outside the limits
			 * (truncate in progress)
			 */
			ret = write_exec(pcol);
			if (unlikely(ret))
				goto fail;
			if (PageError(page))
				ClearPageError(page);
			unlock_page(page);
725 726 727
			EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
				     "outside the limits\n",
				     inode->i_ino, page->index);
B
Boaz Harrosh 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741
			return 0;
		}
	}

try_again:

	if (unlikely(pcol->pg_first == -1)) {
		pcol->pg_first = page->index;
	} else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
		   page->index)) {
		/* Discontinuity detected, split the request */
		ret = write_exec(pcol);
		if (unlikely(ret))
			goto fail;
742 743 744

		EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
			     inode->i_ino, page->index);
B
Boaz Harrosh 已提交
745 746 747
		goto try_again;
	}

748
	if (!pcol->pages) {
B
Boaz Harrosh 已提交
749 750 751 752 753
		ret = pcol_try_alloc(pcol);
		if (unlikely(ret))
			goto fail;
	}

B
Boaz Harrosh 已提交
754
	EXOFS_DBGMSG2("    writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
B
Boaz Harrosh 已提交
755 756 757 758
		     inode->i_ino, page->index, len);

	ret = pcol_add_page(pcol, page, len);
	if (unlikely(ret)) {
B
Boaz Harrosh 已提交
759
		EXOFS_DBGMSG2("Failed pcol_add_page "
B
Boaz Harrosh 已提交
760 761 762 763 764 765
			     "nr_pages=%u total_length=0x%lx\n",
			     pcol->nr_pages, pcol->length);

		/* split the request, next loop will start again */
		ret = write_exec(pcol);
		if (unlikely(ret)) {
766
			EXOFS_DBGMSG("write_exec failed => %d", ret);
B
Boaz Harrosh 已提交
767 768 769 770 771 772 773 774 775 776 777 778
			goto fail;
		}

		goto try_again;
	}

	BUG_ON(PageWriteback(page));
	set_page_writeback(page);

	return 0;

fail:
779 780
	EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
		     inode->i_ino, page->index, ret);
B
Boaz Harrosh 已提交
781 782 783 784 785 786 787 788 789 790 791 792
	set_bit(AS_EIO, &page->mapping->flags);
	unlock_page(page);
	return ret;
}

static int exofs_writepages(struct address_space *mapping,
		       struct writeback_control *wbc)
{
	struct page_collect pcol;
	long start, end, expected_pages;
	int ret;

793
	start = wbc->range_start >> PAGE_SHIFT;
B
Boaz Harrosh 已提交
794 795
	end = (wbc->range_end == LLONG_MAX) ?
			start + mapping->nrpages :
796
			wbc->range_end >> PAGE_SHIFT;
B
Boaz Harrosh 已提交
797 798

	if (start || end)
799
		expected_pages = end - start + 1;
B
Boaz Harrosh 已提交
800 801 802
	else
		expected_pages = mapping->nrpages;

803 804 805
	if (expected_pages < 32L)
		expected_pages = 32L;

B
Boaz Harrosh 已提交
806
	EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
807
		     "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
B
Boaz Harrosh 已提交
808
		     mapping->host->i_ino, wbc->range_start, wbc->range_end,
809
		     mapping->nrpages, start, end, expected_pages);
B
Boaz Harrosh 已提交
810 811 812 813

	_pcol_init(&pcol, expected_pages, mapping->host);

	ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
814
	if (unlikely(ret)) {
B
Boaz Harrosh 已提交
815 816 817 818
		EXOFS_ERR("write_cache_pages => %d\n", ret);
		return ret;
	}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	ret = write_exec(&pcol);
	if (unlikely(ret))
		return ret;

	if (wbc->sync_mode == WB_SYNC_ALL) {
		return write_exec(&pcol); /* pump the last reminder */
	} else if (pcol.nr_pages) {
		/* not SYNC let the reminder join the next writeout */
		unsigned i;

		for (i = 0; i < pcol.nr_pages; i++) {
			struct page *page = pcol.pages[i];

			end_page_writeback(page);
			set_page_dirty(page);
			unlock_page(page);
		}
	}
	return 0;
B
Boaz Harrosh 已提交
838 839
}

840
/*
B
Boaz Harrosh 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
static int exofs_writepage(struct page *page, struct writeback_control *wbc)
{
	struct page_collect pcol;
	int ret;

	_pcol_init(&pcol, 1, page->mapping->host);

	ret = writepage_strip(page, NULL, &pcol);
	if (ret) {
		EXOFS_ERR("exofs_writepage => %d\n", ret);
		return ret;
	}

	return write_exec(&pcol);
}
856
*/
B
Boaz Harrosh 已提交
857 858 859 860
/* i_mutex held using inode->i_size directly */
static void _write_failed(struct inode *inode, loff_t to)
{
	if (to > inode->i_size)
861
		truncate_pagecache(inode, inode->i_size);
B
Boaz Harrosh 已提交
862 863
}

B
Boaz Harrosh 已提交
864 865 866 867 868 869 870 871 872 873 874 875
int exofs_write_begin(struct file *file, struct address_space *mapping,
		loff_t pos, unsigned len, unsigned flags,
		struct page **pagep, void **fsdata)
{
	int ret = 0;
	struct page *page;

	page = *pagep;
	if (page == NULL) {
		ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
					 fsdata);
		if (ret) {
876
			EXOFS_DBGMSG("simple_write_begin failed\n");
B
Boaz Harrosh 已提交
877
			goto out;
B
Boaz Harrosh 已提交
878 879 880 881 882 883
		}

		page = *pagep;
	}

	 /* read modify write */
884
	if (!PageUptodate(page) && (len != PAGE_SIZE)) {
B
Boaz Harrosh 已提交
885
		loff_t i_size = i_size_read(mapping->host);
886
		pgoff_t end_index = i_size >> PAGE_SHIFT;
B
Boaz Harrosh 已提交
887 888 889
		size_t rlen;

		if (page->index < end_index)
890
			rlen = PAGE_SIZE;
B
Boaz Harrosh 已提交
891
		else if (page->index == end_index)
892
			rlen = i_size & ~PAGE_MASK;
B
Boaz Harrosh 已提交
893 894 895 896 897 898 899 900 901
		else
			rlen = 0;

		if (!rlen) {
			clear_highpage(page);
			SetPageUptodate(page);
			goto out;
		}

B
Boaz Harrosh 已提交
902 903 904 905
		ret = _readpage(page, true);
		if (ret) {
			/*SetPageError was done by _readpage. Is it ok?*/
			unlock_page(page);
B
Boaz Harrosh 已提交
906
			EXOFS_DBGMSG("__readpage failed\n");
B
Boaz Harrosh 已提交
907 908
		}
	}
B
Boaz Harrosh 已提交
909 910 911
out:
	if (unlikely(ret))
		_write_failed(mapping->host, pos + len);
B
Boaz Harrosh 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

	return ret;
}

static int exofs_write_begin_export(struct file *file,
		struct address_space *mapping,
		loff_t pos, unsigned len, unsigned flags,
		struct page **pagep, void **fsdata)
{
	*pagep = NULL;

	return exofs_write_begin(file, mapping, pos, len, flags, pagep,
					fsdata);
}

927 928 929 930 931 932 933 934 935 936
static int exofs_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
{
	struct inode *inode = mapping->host;
	/* According to comment in simple_write_end i_mutex is held */
	loff_t i_size = inode->i_size;
	int ret;

	ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
B
Boaz Harrosh 已提交
937 938 939 940
	if (unlikely(ret))
		_write_failed(inode, pos + len);

	/* TODO: once simple_write_end marks inode dirty remove */
941 942 943 944 945
	if (i_size != inode->i_size)
		mark_inode_dirty(inode);
	return ret;
}

946 947 948 949
static int exofs_releasepage(struct page *page, gfp_t gfp)
{
	EXOFS_DBGMSG("page 0x%lx\n", page->index);
	WARN_ON(1);
950
	return 0;
951 952
}

953 954
static void exofs_invalidatepage(struct page *page, unsigned int offset,
				 unsigned int length)
955
{
956 957
	EXOFS_DBGMSG("page 0x%lx offset 0x%x length 0x%x\n",
		     page->index, offset, length);
958 959 960
	WARN_ON(1);
}

B
Boaz Harrosh 已提交
961 962

 /* TODO: Should be easy enough to do proprly */
963 964
static ssize_t exofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
			       loff_t offset)
B
Boaz Harrosh 已提交
965 966 967 968
{
	return 0;
}

B
Boaz Harrosh 已提交
969 970 971
const struct address_space_operations exofs_aops = {
	.readpage	= exofs_readpage,
	.readpages	= exofs_readpages,
972
	.writepage	= NULL,
B
Boaz Harrosh 已提交
973 974
	.writepages	= exofs_writepages,
	.write_begin	= exofs_write_begin_export,
975
	.write_end	= exofs_write_end,
976 977 978 979 980 981
	.releasepage	= exofs_releasepage,
	.set_page_dirty	= __set_page_dirty_nobuffers,
	.invalidatepage = exofs_invalidatepage,

	/* Not implemented Yet */
	.bmap		= NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
B
Boaz Harrosh 已提交
982
	.direct_IO	= exofs_direct_IO,
983 984 985 986 987 988

	/* With these NULL has special meaning or default is not exported */
	.migratepage	= NULL,
	.launder_page	= NULL,
	.is_partially_uptodate = NULL,
	.error_remove_page = NULL,
B
Boaz Harrosh 已提交
989 990
};

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/******************************************************************************
 * INODE OPERATIONS
 *****************************************************************************/

/*
 * Test whether an inode is a fast symlink.
 */
static inline int exofs_inode_is_fast_symlink(struct inode *inode)
{
	struct exofs_i_info *oi = exofs_i(inode);

	return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
}

B
Boaz Harrosh 已提交
1005
static int _do_truncate(struct inode *inode, loff_t newsize)
1006 1007
{
	struct exofs_i_info *oi = exofs_i(inode);
1008
	struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
1009 1010 1011 1012
	int ret;

	inode->i_mtime = inode->i_ctime = CURRENT_TIME;

1013
	ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
B
Boaz Harrosh 已提交
1014 1015
	if (likely(!ret))
		truncate_setsize(inode, newsize);
1016

B
Boaz Harrosh 已提交
1017
	EXOFS_DBGMSG2("(0x%lx) size=0x%llx ret=>%d\n",
B
Boaz Harrosh 已提交
1018
		     inode->i_ino, newsize, ret);
1019 1020 1021
	return ret;
}

1022
/*
B
Boaz Harrosh 已提交
1023 1024
 * Set inode attributes - update size attribute on OSD if needed,
 *                        otherwise just call generic functions.
1025 1026 1027
 */
int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
{
1028
	struct inode *inode = d_inode(dentry);
1029 1030
	int error;

B
Boaz Harrosh 已提交
1031 1032 1033 1034 1035 1036 1037
	/* if we are about to modify an object, and it hasn't been
	 * created yet, wait
	 */
	error = wait_obj_created(exofs_i(inode));
	if (unlikely(error))
		return error;

1038
	error = inode_change_ok(inode, iattr);
B
Boaz Harrosh 已提交
1039
	if (unlikely(error))
1040 1041
		return error;

C
Christoph Hellwig 已提交
1042 1043
	if ((iattr->ia_valid & ATTR_SIZE) &&
	    iattr->ia_size != i_size_read(inode)) {
B
Boaz Harrosh 已提交
1044 1045
		error = _do_truncate(inode, iattr->ia_size);
		if (unlikely(error))
C
Christoph Hellwig 已提交
1046 1047 1048 1049 1050 1051
			return error;
	}

	setattr_copy(inode, iattr);
	mark_inode_dirty(inode);
	return 0;
1052
}
1053

1054 1055 1056 1057 1058 1059 1060 1061 1062
static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
	EXOFS_APAGE_FS_DATA,
	EXOFS_ATTR_INODE_FILE_LAYOUT,
	0);
static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
	EXOFS_APAGE_FS_DATA,
	EXOFS_ATTR_INODE_DIR_LAYOUT,
	0);

1063
/*
B
Boaz Harrosh 已提交
1064 1065
 * Read the Linux inode info from the OSD, and return it as is. In exofs the
 * inode info is in an application specific page/attribute of the osd-object.
1066 1067
 */
static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
B
Boaz Harrosh 已提交
1068
		    struct exofs_fcb *inode)
1069 1070
{
	struct exofs_sb_info *sbi = sb->s_fs_info;
1071 1072 1073 1074 1075
	struct osd_attr attrs[] = {
		[0] = g_attr_inode_data,
		[1] = g_attr_inode_file_layout,
		[2] = g_attr_inode_dir_layout,
	};
1076
	struct ore_io_state *ios;
1077
	struct exofs_on_disk_inode_layout *layout;
1078 1079
	int ret;

1080
	ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
1081
	if (unlikely(ret)) {
1082
		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
1083
		return ret;
1084 1085
	}

1086 1087
	attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
	attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
1088

1089 1090
	ios->in_attr = attrs;
	ios->in_attr_len = ARRAY_SIZE(attrs);
1091

1092
	ret = ore_read(ios);
1093 1094
	if (unlikely(ret)) {
		EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
1095
			  _LLU(oi->one_comp.obj.id), ret);
1096 1097 1098 1099 1100
		memset(inode, 0, sizeof(*inode));
		inode->i_mode = 0040000 | (0777 & ~022);
		/* If object is lost on target we might as well enable it's
		 * delete.
		 */
1101
		ret = 0;
1102
		goto out;
1103
	}
1104

1105
	ret = extract_attr_from_ios(ios, &attrs[0]);
1106
	if (ret) {
B
Boaz Harrosh 已提交
1107
		EXOFS_ERR("%s: extract_attr 0 of inode failed\n", __func__);
1108 1109
		goto out;
	}
1110 1111
	WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
	memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
1112

1113
	ret = extract_attr_from_ios(ios, &attrs[1]);
1114
	if (ret) {
B
Boaz Harrosh 已提交
1115
		EXOFS_ERR("%s: extract_attr 1 of inode failed\n", __func__);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		goto out;
	}
	if (attrs[1].len) {
		layout = attrs[1].val_ptr;
		if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
			EXOFS_ERR("%s: unsupported files layout %d\n",
				__func__, layout->gen_func);
			ret = -ENOTSUPP;
			goto out;
		}
	}

	ret = extract_attr_from_ios(ios, &attrs[2]);
	if (ret) {
B
Boaz Harrosh 已提交
1130
		EXOFS_ERR("%s: extract_attr 2 of inode failed\n", __func__);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		goto out;
	}
	if (attrs[2].len) {
		layout = attrs[2].val_ptr;
		if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
			EXOFS_ERR("%s: unsupported meta-data layout %d\n",
				__func__, layout->gen_func);
			ret = -ENOTSUPP;
			goto out;
		}
	}

1143
out:
1144
	ore_put_io_state(ios);
1145 1146 1147
	return ret;
}

1148 1149 1150 1151 1152
static void __oi_init(struct exofs_i_info *oi)
{
	init_waitqueue_head(&oi->i_wq);
	oi->i_flags = 0;
}
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
/*
 * Fill in an inode read from the OSD and set it up for use
 */
struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
{
	struct exofs_i_info *oi;
	struct exofs_fcb fcb;
	struct inode *inode;
	int ret;

	inode = iget_locked(sb, ino);
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (!(inode->i_state & I_NEW))
		return inode;
	oi = exofs_i(inode);
1169
	__oi_init(oi);
1170
	exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
1171
			 exofs_oi_objno(oi));
1172 1173

	/* read the inode from the osd */
B
Boaz Harrosh 已提交
1174
	ret = exofs_get_inode(sb, oi, &fcb);
1175 1176 1177 1178 1179 1180 1181
	if (ret)
		goto bad_inode;

	set_obj_created(oi);

	/* copy stuff from on-disk struct to in-memory struct */
	inode->i_mode = le16_to_cpu(fcb.i_mode);
1182 1183
	i_uid_write(inode, le32_to_cpu(fcb.i_uid));
	i_gid_write(inode, le32_to_cpu(fcb.i_gid));
M
Miklos Szeredi 已提交
1184
	set_nlink(inode, le16_to_cpu(fcb.i_links_count));
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
	inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
	inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
	inode->i_ctime.tv_nsec =
		inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
	oi->i_commit_size = le64_to_cpu(fcb.i_size);
	i_size_write(inode, oi->i_commit_size);
	inode->i_blkbits = EXOFS_BLKSHIFT;
	inode->i_generation = le32_to_cpu(fcb.i_generation);

	oi->i_dir_start_lookup = 0;

	if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
		ret = -ESTALE;
		goto bad_inode;
	}

	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
		if (fcb.i_data[0])
			inode->i_rdev =
				old_decode_dev(le32_to_cpu(fcb.i_data[0]));
		else
			inode->i_rdev =
				new_decode_dev(le32_to_cpu(fcb.i_data[1]));
	} else {
		memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
	}

	if (S_ISREG(inode->i_mode)) {
		inode->i_op = &exofs_file_inode_operations;
		inode->i_fop = &exofs_file_operations;
		inode->i_mapping->a_ops = &exofs_aops;
	} else if (S_ISDIR(inode->i_mode)) {
		inode->i_op = &exofs_dir_inode_operations;
		inode->i_fop = &exofs_dir_operations;
		inode->i_mapping->a_ops = &exofs_aops;
	} else if (S_ISLNK(inode->i_mode)) {
1222 1223 1224 1225 1226
		if (exofs_inode_is_fast_symlink(inode)) {
			inode->i_op = &simple_symlink_inode_operations;
			inode->i_link = (char *)oi->i_data;
		} else {
			inode->i_op = &page_symlink_inode_operations;
1227
			inode_nohighmem(inode);
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
			inode->i_mapping->a_ops = &exofs_aops;
		}
	} else {
		inode->i_op = &exofs_special_inode_operations;
		if (fcb.i_data[0])
			init_special_inode(inode, inode->i_mode,
			   old_decode_dev(le32_to_cpu(fcb.i_data[0])));
		else
			init_special_inode(inode, inode->i_mode,
			   new_decode_dev(le32_to_cpu(fcb.i_data[1])));
	}

	unlock_new_inode(inode);
	return inode;

bad_inode:
	iget_failed(inode);
	return ERR_PTR(ret);
}

int __exofs_wait_obj_created(struct exofs_i_info *oi)
{
	if (!obj_created(oi)) {
1251
		EXOFS_DBGMSG("!obj_created\n");
1252 1253
		BUG_ON(!obj_2bcreated(oi));
		wait_event(oi->i_wq, obj_created(oi));
1254
		EXOFS_DBGMSG("wait_event done\n");
1255 1256 1257
	}
	return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
}
1258

1259 1260 1261 1262 1263
/*
 * Callback function from exofs_new_inode().  The important thing is that we
 * set the obj_created flag so that other methods know that the object exists on
 * the OSD.
 */
1264
static void create_done(struct ore_io_state *ios, void *p)
1265 1266 1267 1268 1269 1270
{
	struct inode *inode = p;
	struct exofs_i_info *oi = exofs_i(inode);
	struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
	int ret;

1271 1272
	ret = ore_check_io(ios, NULL);
	ore_put_io_state(ios);
1273

1274 1275 1276
	atomic_dec(&sbi->s_curr_pending);

	if (unlikely(ret)) {
1277
		EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
1278 1279
			  _LLU(exofs_oi_objno(oi)),
			  _LLU(oi->one_comp.obj.partition));
1280 1281 1282 1283 1284 1285 1286 1287 1288
		/*TODO: When FS is corrupted creation can fail, object already
		 * exist. Get rid of this asynchronous creation, if exist
		 * increment the obj counter and try the next object. Until we
		 * succeed. All these dangling objects will be made into lost
		 * files by chkfs.exofs
		 */
	}

	set_obj_created(oi);
1289 1290 1291 1292 1293 1294 1295

	wake_up(&oi->i_wq);
}

/*
 * Set up a new inode and create an object for it on the OSD
 */
A
Al Viro 已提交
1296
struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
1297
{
1298 1299
	struct super_block *sb = dir->i_sb;
	struct exofs_sb_info *sbi = sb->s_fs_info;
1300 1301
	struct inode *inode;
	struct exofs_i_info *oi;
1302
	struct ore_io_state *ios;
1303 1304 1305 1306 1307 1308 1309
	int ret;

	inode = new_inode(sb);
	if (!inode)
		return ERR_PTR(-ENOMEM);

	oi = exofs_i(inode);
1310
	__oi_init(oi);
1311 1312 1313

	set_obj_2bcreated(oi);

1314
	inode_init_owner(inode, dir, mode);
1315 1316 1317 1318 1319 1320 1321 1322 1323
	inode->i_ino = sbi->s_nextid++;
	inode->i_blkbits = EXOFS_BLKSHIFT;
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
	oi->i_commit_size = inode->i_size = 0;
	spin_lock(&sbi->s_next_gen_lock);
	inode->i_generation = sbi->s_next_generation++;
	spin_unlock(&sbi->s_next_gen_lock);
	insert_inode_hash(inode);

1324
	exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
1325
			 exofs_oi_objno(oi));
1326 1327
	exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */

1328 1329
	mark_inode_dirty(inode);

1330
	ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
1331
	if (unlikely(ret)) {
1332
		EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
1333
		return ERR_PTR(ret);
1334 1335
	}

1336 1337
	ios->done = create_done;
	ios->private = inode;
1338

1339
	ret = ore_create(ios);
1340
	if (ret) {
1341
		ore_put_io_state(ios);
1342
		return ERR_PTR(ret);
1343 1344 1345 1346 1347
	}
	atomic_inc(&sbi->s_curr_pending);

	return inode;
}
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

/*
 * struct to pass two arguments to update_inode's callback
 */
struct updatei_args {
	struct exofs_sb_info	*sbi;
	struct exofs_fcb	fcb;
};

/*
 * Callback function from exofs_update_inode().
 */
1360
static void updatei_done(struct ore_io_state *ios, void *p)
1361 1362 1363
{
	struct updatei_args *args = p;

1364
	ore_put_io_state(ios);
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379

	atomic_dec(&args->sbi->s_curr_pending);

	kfree(args);
}

/*
 * Write the inode to the OSD.  Just fill up the struct, and set the attribute
 * synchronously or asynchronously depending on the do_sync flag.
 */
static int exofs_update_inode(struct inode *inode, int do_sync)
{
	struct exofs_i_info *oi = exofs_i(inode);
	struct super_block *sb = inode->i_sb;
	struct exofs_sb_info *sbi = sb->s_fs_info;
1380
	struct ore_io_state *ios;
1381 1382 1383 1384 1385 1386
	struct osd_attr attr;
	struct exofs_fcb *fcb;
	struct updatei_args *args;
	int ret;

	args = kzalloc(sizeof(*args), GFP_KERNEL);
B
Boaz Harrosh 已提交
1387
	if (!args) {
1388
		EXOFS_DBGMSG("Failed kzalloc of args\n");
1389
		return -ENOMEM;
B
Boaz Harrosh 已提交
1390
	}
1391 1392 1393 1394

	fcb = &args->fcb;

	fcb->i_mode = cpu_to_le16(inode->i_mode);
1395 1396
	fcb->i_uid = cpu_to_le32(i_uid_read(inode));
	fcb->i_gid = cpu_to_le32(i_gid_read(inode));
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	fcb->i_links_count = cpu_to_le16(inode->i_nlink);
	fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
	fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
	fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
	oi->i_commit_size = i_size_read(inode);
	fcb->i_size = cpu_to_le64(oi->i_commit_size);
	fcb->i_generation = cpu_to_le32(inode->i_generation);

	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
		if (old_valid_dev(inode->i_rdev)) {
			fcb->i_data[0] =
				cpu_to_le32(old_encode_dev(inode->i_rdev));
			fcb->i_data[1] = 0;
		} else {
			fcb->i_data[0] = 0;
			fcb->i_data[1] =
				cpu_to_le32(new_encode_dev(inode->i_rdev));
			fcb->i_data[2] = 0;
		}
	} else
		memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));

1419
	ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
1420
	if (unlikely(ret)) {
1421
		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
1422 1423 1424 1425 1426
		goto free_args;
	}

	attr = g_attr_inode_data;
	attr.val_ptr = fcb;
1427 1428
	ios->out_attr_len = 1;
	ios->out_attr = &attr;
1429

1430
	wait_obj_created(oi);
1431

1432
	if (!do_sync) {
1433
		args->sbi = sbi;
1434 1435 1436
		ios->done = updatei_done;
		ios->private = args;
	}
1437

1438
	ret = ore_write(ios);
1439
	if (!do_sync && !ret) {
1440 1441 1442 1443
		atomic_inc(&sbi->s_curr_pending);
		goto out; /* deallocation in updatei_done */
	}

1444
	ore_put_io_state(ios);
1445 1446 1447
free_args:
	kfree(args);
out:
B
Boaz Harrosh 已提交
1448 1449
	EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
		     inode->i_ino, do_sync, ret);
1450 1451 1452
	return ret;
}

1453
int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
1454
{
N
Nick Piggin 已提交
1455 1456
	/* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
	return exofs_update_inode(inode, 1);
1457 1458 1459 1460 1461 1462
}

/*
 * Callback function from exofs_delete_inode() - don't have much cleaning up to
 * do.
 */
1463
static void delete_done(struct ore_io_state *ios, void *p)
1464
{
1465 1466
	struct exofs_sb_info *sbi = p;

1467
	ore_put_io_state(ios);
1468

1469 1470 1471 1472 1473 1474 1475 1476
	atomic_dec(&sbi->s_curr_pending);
}

/*
 * Called when the refcount of an inode reaches zero.  We remove the object
 * from the OSD here.  We make sure the object was created before we try and
 * delete it.
 */
A
Al Viro 已提交
1477
void exofs_evict_inode(struct inode *inode)
1478 1479 1480 1481
{
	struct exofs_i_info *oi = exofs_i(inode);
	struct super_block *sb = inode->i_sb;
	struct exofs_sb_info *sbi = sb->s_fs_info;
1482
	struct ore_io_state *ios;
1483 1484
	int ret;

1485
	truncate_inode_pages_final(&inode->i_data);
1486

B
Boaz Harrosh 已提交
1487
	/* TODO: should do better here */
A
Al Viro 已提交
1488
	if (inode->i_nlink || is_bad_inode(inode))
1489 1490 1491
		goto no_delete;

	inode->i_size = 0;
1492
	clear_inode(inode);
1493

1494 1495 1496 1497 1498 1499
	/* if we are deleting an obj that hasn't been created yet, wait.
	 * This also makes sure that create_done cannot be called with an
	 * already evicted inode.
	 */
	wait_obj_created(oi);
	/* ignore the error, attempt a remove anyway */
B
Boaz Harrosh 已提交
1500 1501

	/* Now Remove the OSD objects */
1502
	ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
B
Boaz Harrosh 已提交
1503
	if (unlikely(ret)) {
1504
		EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
B
Boaz Harrosh 已提交
1505
		return;
1506 1507
	}

1508 1509
	ios->done = delete_done;
	ios->private = sbi;
1510

1511
	ret = ore_remove(ios);
1512
	if (ret) {
1513 1514
		EXOFS_ERR("%s: ore_remove failed\n", __func__);
		ore_put_io_state(ios);
1515 1516 1517 1518 1519 1520 1521
		return;
	}
	atomic_inc(&sbi->s_curr_pending);

	return;

no_delete:
1522
	clear_inode(inode);
1523
}