inode.c 32.6 KB
Newer Older
1 2
/*
 * Copyright (C) 2005, 2006
B
Boaz Harrosh 已提交
3
 * Avishay Traeger (avishay@gmail.com)
4 5 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 34 35
 * Copyright (C) 2008, 2009
 * Boaz Harrosh <bharrosh@panasas.com>
 *
 * 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
 */

#include <linux/writeback.h>
#include <linux/buffer_head.h>
B
Boaz Harrosh 已提交
36
#include <scsi/scsi_device.h>
37 38 39

#include "exofs.h"

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

42 43
enum { BIO_MAX_PAGES_KMALLOC =
		(PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
44 45
	MAX_PAGES_KMALLOC =
		PAGE_SIZE / sizeof(struct page *),
46 47
};

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

54 55
	struct page **pages;
	unsigned alloc_pages;
B
Boaz Harrosh 已提交
56 57 58 59 60 61
	unsigned nr_pages;
	unsigned long length;
	loff_t pg_first; /* keep 64bit also in 32-arches */
};

static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
62
		       struct inode *inode)
B
Boaz Harrosh 已提交
63 64 65 66 67 68 69
{
	struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;

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

70
	pcol->ios = NULL;
71 72
	pcol->pages = NULL;
	pcol->alloc_pages = 0;
B
Boaz Harrosh 已提交
73 74 75 76 77 78 79 80 81
	pcol->nr_pages = 0;
	pcol->length = 0;
	pcol->pg_first = -1;
}

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

82 83
	pcol->pages = NULL;
	pcol->alloc_pages = 0;
B
Boaz Harrosh 已提交
84 85 86
	pcol->nr_pages = 0;
	pcol->length = 0;
	pcol->pg_first = -1;
87
	pcol->ios = NULL;
B
Boaz Harrosh 已提交
88 89 90 91 92

	/* 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)
93
		pcol->expected_pages = MAX_PAGES_KMALLOC;
B
Boaz Harrosh 已提交
94 95 96 97
}

static int pcol_try_alloc(struct page_collect *pcol)
{
98 99
	unsigned pages = min_t(unsigned, pcol->expected_pages,
			  MAX_PAGES_KMALLOC);
100 101

	if (!pcol->ios) { /* First time allocate io_state */
102
		int ret = exofs_get_io_state(&pcol->sbi->layout, &pcol->ios);
103 104 105 106

		if (ret)
			return ret;
	}
B
Boaz Harrosh 已提交
107

108 109 110 111
	/* TODO: easily support bio chaining */
	pages =  min_t(unsigned, pages,
		       pcol->sbi->layout.group_width * BIO_MAX_PAGES_KMALLOC);

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 132 133 134

	if (pcol->ios) {
		exofs_put_io_state(pcol->ios);
		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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	pcol->length += len;
	return 0;
}

static int update_read_page(struct page *page, int ret)
{
	if (ret == 0) {
		/* Everything is OK */
		SetPageUptodate(page);
		if (PageError(page))
			ClearPageError(page);
	} else if (ret == -EFAULT) {
		/* 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);
		ret = 0; /* recovered error */
		EXOFS_DBGMSG("recovered read error\n");
	} else /* Error */
		SetPageError(page);

	return ret;
}

static void update_write_page(struct page *page, int ret)
{
	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.
 */
186
static int __readpages_done(struct page_collect *pcol, bool do_unlock)
B
Boaz Harrosh 已提交
187 188 189 190 191
{
	int i;
	u64 resid;
	u64 good_bytes;
	u64 length = 0;
192
	int ret = exofs_check_io(pcol->ios, &resid);
B
Boaz Harrosh 已提交
193 194 195 196 197 198

	if (likely(!ret))
		good_bytes = pcol->length;
	else
		good_bytes = pcol->length - resid;

B
Boaz Harrosh 已提交
199
	EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
B
Boaz Harrosh 已提交
200 201 202 203
		     " length=0x%lx nr_pages=%u\n",
		     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
		     pcol->nr_pages);

204 205
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
206 207 208 209 210 211 212 213 214 215 216
		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 已提交
217
		EXOFS_DBGMSG2("    readpages_done(0x%lx, 0x%lx) %s\n",
B
Boaz Harrosh 已提交
218 219 220 221 222 223
			  inode->i_ino, page->index,
			  page_stat ? "bad_bytes" : "good_bytes");

		ret = update_read_page(page, page_stat);
		if (do_unlock)
			unlock_page(page);
224
		length += PAGE_SIZE;
B
Boaz Harrosh 已提交
225 226 227
	}

	pcol_free(pcol);
B
Boaz Harrosh 已提交
228
	EXOFS_DBGMSG2("readpages_done END\n");
B
Boaz Harrosh 已提交
229 230 231 232
	return ret;
}

/* callback of async reads */
233
static void readpages_done(struct exofs_io_state *ios, void *p)
B
Boaz Harrosh 已提交
234 235 236
{
	struct page_collect *pcol = p;

237
	__readpages_done(pcol, true);
B
Boaz Harrosh 已提交
238
	atomic_dec(&pcol->sbi->s_curr_pending);
239
	kfree(pcol);
B
Boaz Harrosh 已提交
240 241 242 243 244 245
}

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

246 247
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260

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

		unlock_page(page);
	}
}

static int read_exec(struct page_collect *pcol, bool is_sync)
{
	struct exofs_i_info *oi = exofs_i(pcol->inode);
261
	struct exofs_io_state *ios = pcol->ios;
B
Boaz Harrosh 已提交
262 263 264
	struct page_collect *pcol_copy = NULL;
	int ret;

265
	if (!pcol->pages)
B
Boaz Harrosh 已提交
266 267 268 269 270
		return 0;

	/* see comment in _readpage() about sync reads */
	WARN_ON(is_sync && (pcol->nr_pages != 1));

271 272
	ios->pages = pcol->pages;
	ios->nr_pages = pcol->nr_pages;
273 274
	ios->length = pcol->length;
	ios->offset = pcol->pg_first << PAGE_CACHE_SHIFT;
B
Boaz Harrosh 已提交
275 276

	if (is_sync) {
277 278
		exofs_oi_read(oi, pcol->ios);
		return __readpages_done(pcol, false);
B
Boaz Harrosh 已提交
279 280 281 282 283 284 285 286 287
	}

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

	*pcol_copy = *pcol;
288 289 290
	ios->done = readpages_done;
	ios->private = pcol_copy;
	ret = exofs_oi_read(oi, ios);
B
Boaz Harrosh 已提交
291 292 293 294 295
	if (unlikely(ret))
		goto err;

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

B
Boaz Harrosh 已提交
296
	EXOFS_DBGMSG2("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
297
		  ios->obj.id, _LLU(ios->offset), pcol->length);
B
Boaz Harrosh 已提交
298 299 300 301 302 303 304 305

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

err:
	if (!is_sync)
		_unlock_pcol_pages(pcol, ret, READ);
306 307

	pcol_free(pcol);
308

B
Boaz Harrosh 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	kfree(pcol_copy);
	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);
	pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
	size_t len;
	int ret;

	/* FIXME: Just for debugging, will be removed */
	if (PageUptodate(page))
		EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
			  page->index);

	if (page->index < end_index)
		len = PAGE_CACHE_SIZE;
	else if (page->index == end_index)
		len = i_size & ~PAGE_CACHE_MASK;
	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);

		unlock_page(page);
		EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page,"
			     " splitting\n", inode->i_ino, page->index);

		return read_exec(pcol, false);
	}

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 = read_exec(pcol, false);
		if (unlikely(ret))
			goto fail;
		goto try_again;
	}

372
	if (!pcol->pages) {
B
Boaz Harrosh 已提交
373 374 375 376 377 378 379 380
		ret = pcol_try_alloc(pcol);
		if (unlikely(ret))
			goto fail;
	}

	if (len != PAGE_CACHE_SIZE)
		zero_user(page, len, PAGE_CACHE_SIZE - len);

B
Boaz Harrosh 已提交
381
	EXOFS_DBGMSG2("    readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
B
Boaz Harrosh 已提交
382 383 384 385
		     inode->i_ino, page->index, len);

	ret = pcol_add_page(pcol, page, len);
	if (ret) {
B
Boaz Harrosh 已提交
386
		EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
B
Boaz Harrosh 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
			  "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 */
		ret = read_exec(pcol, false);
		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;
	}

	return read_exec(&pcol, false);
}

static int _readpage(struct page *page, bool is_sync)
{
	struct page_collect pcol;
	int ret;

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

430 431
	/* readpage_strip might call read_exec(,is_sync==false) at several
	 * places but not if we have a single page.
B
Boaz Harrosh 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	 */
	ret = readpage_strip(&pcol, page);
	if (ret) {
		EXOFS_ERR("_readpage => %d\n", ret);
		return ret;
	}

	return read_exec(&pcol, is_sync);
}

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

450 451
/* Callback for osd_write. All writes are asynchronous */
static void writepages_done(struct exofs_io_state *ios, void *p)
B
Boaz Harrosh 已提交
452 453 454 455 456 457
{
	struct page_collect *pcol = p;
	int i;
	u64 resid;
	u64  good_bytes;
	u64  length = 0;
458
	int ret = exofs_check_io(ios, &resid);
B
Boaz Harrosh 已提交
459 460 461 462 463 464 465 466

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

	if (likely(!ret))
		good_bytes = pcol->length;
	else
		good_bytes = pcol->length - resid;

B
Boaz Harrosh 已提交
467
	EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
B
Boaz Harrosh 已提交
468 469 470 471
		     " length=0x%lx nr_pages=%u\n",
		     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
		     pcol->nr_pages);

472 473
	for (i = 0; i < pcol->nr_pages; i++) {
		struct page *page = pcol->pages[i];
B
Boaz Harrosh 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486
		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 已提交
487
		EXOFS_DBGMSG2("    writepages_done(0x%lx, 0x%lx) status=%d\n",
B
Boaz Harrosh 已提交
488 489
			     inode->i_ino, page->index, page_stat);

490
		length += PAGE_SIZE;
B
Boaz Harrosh 已提交
491 492 493 494
	}

	pcol_free(pcol);
	kfree(pcol);
B
Boaz Harrosh 已提交
495
	EXOFS_DBGMSG2("writepages_done END\n");
B
Boaz Harrosh 已提交
496 497 498 499 500
}

static int write_exec(struct page_collect *pcol)
{
	struct exofs_i_info *oi = exofs_i(pcol->inode);
501
	struct exofs_io_state *ios = pcol->ios;
B
Boaz Harrosh 已提交
502 503 504
	struct page_collect *pcol_copy = NULL;
	int ret;

505
	if (!pcol->pages)
B
Boaz Harrosh 已提交
506 507 508 509 510 511 512 513 514 515 516
		return 0;

	pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
	if (!pcol_copy) {
		EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
		ret = -ENOMEM;
		goto err;
	}

	*pcol_copy = *pcol;

517 518
	ios->pages = pcol_copy->pages;
	ios->nr_pages = pcol_copy->nr_pages;
519 520 521 522 523 524
	ios->offset = pcol_copy->pg_first << PAGE_CACHE_SHIFT;
	ios->length = pcol_copy->length;
	ios->done = writepages_done;
	ios->private = pcol_copy;

	ret = exofs_oi_write(oi, ios);
B
Boaz Harrosh 已提交
525
	if (unlikely(ret)) {
526
		EXOFS_ERR("write_exec: exofs_oi_write() Faild\n");
B
Boaz Harrosh 已提交
527 528 529 530
		goto err;
	}

	atomic_inc(&pcol->sbi->s_curr_pending);
B
Boaz Harrosh 已提交
531
	EXOFS_DBGMSG2("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
532
		  pcol->inode->i_ino, pcol->pg_first, _LLU(ios->offset),
B
Boaz Harrosh 已提交
533 534 535 536 537 538 539
		  pcol->length);
	/* pages ownership was passed to pcol_copy */
	_pcol_reset(pcol);
	return 0;

err:
	_unlock_pcol_pages(pcol, ret, WRITE);
540
	pcol_free(pcol);
B
Boaz Harrosh 已提交
541
	kfree(pcol_copy);
542

B
Boaz Harrosh 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	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);
	pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
	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 */
		len = PAGE_CACHE_SIZE;
	else {
		len = i_size & ~PAGE_CACHE_MASK;

		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);
586 587 588
			EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
				     "outside the limits\n",
				     inode->i_ino, page->index);
B
Boaz Harrosh 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602
			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;
603 604 605

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

609
	if (!pcol->pages) {
B
Boaz Harrosh 已提交
610 611 612 613 614
		ret = pcol_try_alloc(pcol);
		if (unlikely(ret))
			goto fail;
	}

B
Boaz Harrosh 已提交
615
	EXOFS_DBGMSG2("    writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
B
Boaz Harrosh 已提交
616 617 618 619
		     inode->i_ino, page->index, len);

	ret = pcol_add_page(pcol, page, len);
	if (unlikely(ret)) {
B
Boaz Harrosh 已提交
620
		EXOFS_DBGMSG2("Failed pcol_add_page "
B
Boaz Harrosh 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
			     "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)) {
			EXOFS_DBGMSG("write_exec faild => %d", ret);
			goto fail;
		}

		goto try_again;
	}

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

	return 0;

fail:
640 641
	EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
		     inode->i_ino, page->index, ret);
B
Boaz Harrosh 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	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;

	start = wbc->range_start >> PAGE_CACHE_SHIFT;
	end = (wbc->range_end == LLONG_MAX) ?
			start + mapping->nrpages :
			wbc->range_end >> PAGE_CACHE_SHIFT;

	if (start || end)
660
		expected_pages = end - start + 1;
B
Boaz Harrosh 已提交
661 662 663
	else
		expected_pages = mapping->nrpages;

664 665 666
	if (expected_pages < 32L)
		expected_pages = 32L;

B
Boaz Harrosh 已提交
667
	EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
668
		     "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
B
Boaz Harrosh 已提交
669
		     mapping->host->i_ino, wbc->range_start, wbc->range_end,
670
		     mapping->nrpages, start, end, expected_pages);
B
Boaz Harrosh 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

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

	ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
	if (ret) {
		EXOFS_ERR("write_cache_pages => %d\n", ret);
		return ret;
	}

	return write_exec(&pcol);
}

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);
}

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) {
			EXOFS_DBGMSG("simple_write_begin faild\n");
			return ret;
		}

		page = *pagep;
	}

	 /* read modify write */
	if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
		ret = _readpage(page, true);
		if (ret) {
			/*SetPageError was done by _readpage. Is it ok?*/
			unlock_page(page);
			EXOFS_DBGMSG("__readpage_filler faild\n");
		}
	}

	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);
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
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);
	if (i_size != inode->i_size)
		mark_inode_dirty(inode);
	return ret;
}

B
Boaz Harrosh 已提交
757 758 759 760 761 762
const struct address_space_operations exofs_aops = {
	.readpage	= exofs_readpage,
	.readpages	= exofs_readpages,
	.writepage	= exofs_writepage,
	.writepages	= exofs_writepages,
	.write_begin	= exofs_write_begin_export,
763
	.write_end	= exofs_write_end,
B
Boaz Harrosh 已提交
764 765
};

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
/******************************************************************************
 * 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);
}

/*
 * get_block_t - Fill in a buffer_head
 * An OSD takes care of block allocation so we just fake an allocation by
 * putting in the inode's sector_t in the buffer_head.
 * TODO: What about the case of create==0 and @iblock does not exist in the
 * object?
 */
static int exofs_get_block(struct inode *inode, sector_t iblock,
		    struct buffer_head *bh_result, int create)
{
	map_bh(bh_result, inode->i_sb, iblock);
	return 0;
}

const struct osd_attr g_attr_logical_length = ATTR_DEF(
	OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
static int _do_truncate(struct inode *inode)
{
	struct exofs_i_info *oi = exofs_i(inode);
	loff_t isize = i_size_read(inode);
	int ret;

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

	nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);

	ret = exofs_oi_truncate(oi, (u64)isize);
	EXOFS_DBGMSG("(0x%lx) size=0x%llx\n", inode->i_ino, isize);
	return ret;
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
/*
 * Truncate a file to the specified size - all we have to do is set the size
 * attribute.  We make sure the object exists first.
 */
void exofs_truncate(struct inode *inode)
{
	struct exofs_i_info *oi = exofs_i(inode);
	int ret;

	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
	     || S_ISLNK(inode->i_mode)))
		return;
	if (exofs_inode_is_fast_symlink(inode))
		return;
	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
		return;

	/* if we are about to truncate an object, and it hasn't been
	 * created yet, wait
	 */
	if (unlikely(wait_obj_created(oi)))
		goto fail;

835
	ret = _do_truncate(inode);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	if (ret)
		goto fail;

out:
	mark_inode_dirty(inode);
	return;
fail:
	make_bad_inode(inode);
	goto out;
}

/*
 * Set inode attributes - just call generic functions.
 */
int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
{
	struct inode *inode = dentry->d_inode;
	int error;

	error = inode_change_ok(inode, iattr);
	if (error)
		return error;

	error = inode_setattr(inode, iattr);
	return error;
}
862

863 864 865 866 867 868 869 870 871
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);

872
/*
B
Boaz Harrosh 已提交
873 874
 * 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.
875 876
 */
static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
B
Boaz Harrosh 已提交
877
		    struct exofs_fcb *inode)
878 879
{
	struct exofs_sb_info *sbi = sb->s_fs_info;
880 881 882 883 884
	struct osd_attr attrs[] = {
		[0] = g_attr_inode_data,
		[1] = g_attr_inode_file_layout,
		[2] = g_attr_inode_dir_layout,
	};
885
	struct exofs_io_state *ios;
886
	struct exofs_on_disk_inode_layout *layout;
887 888
	int ret;

889
	ret = exofs_get_io_state(&sbi->layout, &ios);
890 891 892
	if (unlikely(ret)) {
		EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
		return ret;
893 894
	}

895 896 897
	ios->obj.id = exofs_oi_objno(oi);
	exofs_make_credential(oi->i_cred, &ios->obj);
	ios->cred = oi->i_cred;
898

899 900 901
	attrs[1].len = exofs_on_disk_inode_layout_size(sbi->layout.s_numdevs);
	attrs[2].len = exofs_on_disk_inode_layout_size(sbi->layout.s_numdevs);

902 903
	ios->in_attr = attrs;
	ios->in_attr_len = ARRAY_SIZE(attrs);
904

905
	ret = exofs_sbi_read(ios);
906 907 908 909 910 911 912 913 914 915
	if (unlikely(ret)) {
		EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
			  _LLU(ios->obj.id), ret);
		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.
		 */
		if ((ret == -ENOENT) || (ret == -EINVAL))
			ret = 0;
916
		goto out;
917
	}
918

919
	ret = extract_attr_from_ios(ios, &attrs[0]);
920
	if (ret) {
921
		EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
922 923
		goto out;
	}
924 925
	WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
	memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
926

927
	ret = extract_attr_from_ios(ios, &attrs[1]);
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	if (ret) {
		EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
		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) {
		EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
		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;
		}
	}

957
out:
958
	exofs_put_io_state(ios);
959 960 961
	return ret;
}

962 963 964 965 966
static void __oi_init(struct exofs_i_info *oi)
{
	init_waitqueue_head(&oi->i_wq);
	oi->i_flags = 0;
}
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
/*
 * 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);
983
	__oi_init(oi);
984 985

	/* read the inode from the osd */
B
Boaz Harrosh 已提交
986
	ret = exofs_get_inode(sb, oi, &fcb);
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	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);
	inode->i_uid = le32_to_cpu(fcb.i_uid);
	inode->i_gid = le32_to_cpu(fcb.i_gid);
	inode->i_nlink = le16_to_cpu(fcb.i_links_count);
	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)) {
		if (exofs_inode_is_fast_symlink(inode))
			inode->i_op = &exofs_fast_symlink_inode_operations;
		else {
			inode->i_op = &exofs_symlink_inode_operations;
			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)) {
		BUG_ON(!obj_2bcreated(oi));
		wait_event(oi->i_wq, obj_created(oi));
	}
	return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
}
/*
 * 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.
 */
1071
static void create_done(struct exofs_io_state *ios, void *p)
1072 1073 1074 1075 1076 1077
{
	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;

1078 1079 1080
	ret = exofs_check_io(ios, NULL);
	exofs_put_io_state(ios);

1081 1082 1083 1084
	atomic_dec(&sbi->s_curr_pending);

	if (unlikely(ret)) {
		EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
1085
			  _LLU(exofs_oi_objno(oi)), _LLU(sbi->layout.s_pid));
1086 1087 1088 1089 1090 1091 1092 1093 1094
		/*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);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

	atomic_dec(&inode->i_count);
	wake_up(&oi->i_wq);
}

/*
 * Set up a new inode and create an object for it on the OSD
 */
struct inode *exofs_new_inode(struct inode *dir, int mode)
{
	struct super_block *sb;
	struct inode *inode;
	struct exofs_i_info *oi;
	struct exofs_sb_info *sbi;
1109
	struct exofs_io_state *ios;
1110 1111 1112 1113 1114 1115 1116 1117
	int ret;

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

	oi = exofs_i(inode);
1118
	__oi_init(oi);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

	set_obj_2bcreated(oi);

	sbi = sb->s_fs_info;

	sb->s_dirt = 1;
	inode->i_uid = current->cred->fsuid;
	if (dir->i_mode & S_ISGID) {
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(mode))
			mode |= S_ISGID;
	} else {
		inode->i_gid = current->cred->fsgid;
	}
	inode->i_mode = mode;

	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);

	mark_inode_dirty(inode);

1146
	ret = exofs_get_io_state(&sbi->layout, &ios);
1147 1148 1149
	if (unlikely(ret)) {
		EXOFS_ERR("exofs_new_inode: exofs_get_io_state failed\n");
		return ERR_PTR(ret);
1150 1151
	}

1152 1153
	ios->obj.id = exofs_oi_objno(oi);
	exofs_make_credential(oi->i_cred, &ios->obj);
1154 1155 1156 1157 1158 1159

	/* increment the refcount so that the inode will still be around when we
	 * reach the callback
	 */
	atomic_inc(&inode->i_count);

1160 1161 1162 1163
	ios->done = create_done;
	ios->private = inode;
	ios->cred = oi->i_cred;
	ret = exofs_sbi_create(ios);
1164 1165
	if (ret) {
		atomic_dec(&inode->i_count);
1166 1167
		exofs_put_io_state(ios);
		return ERR_PTR(ret);
1168 1169 1170 1171 1172
	}
	atomic_inc(&sbi->s_curr_pending);

	return inode;
}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

/*
 * 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().
 */
1185
static void updatei_done(struct exofs_io_state *ios, void *p)
1186 1187 1188
{
	struct updatei_args *args = p;

1189
	exofs_put_io_state(ios);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

	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;
1205
	struct exofs_io_state *ios;
1206 1207 1208 1209 1210 1211
	struct osd_attr attr;
	struct exofs_fcb *fcb;
	struct updatei_args *args;
	int ret;

	args = kzalloc(sizeof(*args), GFP_KERNEL);
B
Boaz Harrosh 已提交
1212 1213
	if (!args) {
		EXOFS_DBGMSG("Faild kzalloc of args\n");
1214
		return -ENOMEM;
B
Boaz Harrosh 已提交
1215
	}
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

	fcb = &args->fcb;

	fcb->i_mode = cpu_to_le16(inode->i_mode);
	fcb->i_uid = cpu_to_le32(inode->i_uid);
	fcb->i_gid = cpu_to_le32(inode->i_gid);
	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));

1244
	ret = exofs_get_io_state(&sbi->layout, &ios);
1245 1246
	if (unlikely(ret)) {
		EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
1247 1248 1249 1250 1251
		goto free_args;
	}

	attr = g_attr_inode_data;
	attr.val_ptr = fcb;
1252 1253
	ios->out_attr_len = 1;
	ios->out_attr = &attr;
1254 1255 1256 1257 1258 1259 1260 1261

	if (!obj_created(oi)) {
		EXOFS_DBGMSG("!obj_created\n");
		BUG_ON(!obj_2bcreated(oi));
		wait_event(oi->i_wq, obj_created(oi));
		EXOFS_DBGMSG("wait_event done\n");
	}

1262
	if (!do_sync) {
1263
		args->sbi = sbi;
1264 1265 1266
		ios->done = updatei_done;
		ios->private = args;
	}
1267

1268 1269
	ret = exofs_oi_write(oi, ios);
	if (!do_sync && !ret) {
1270 1271 1272 1273
		atomic_inc(&sbi->s_curr_pending);
		goto out; /* deallocation in updatei_done */
	}

1274
	exofs_put_io_state(ios);
1275 1276 1277
free_args:
	kfree(args);
out:
B
Boaz Harrosh 已提交
1278 1279
	EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
		     inode->i_ino, do_sync, ret);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	return ret;
}

int exofs_write_inode(struct inode *inode, int wait)
{
	return exofs_update_inode(inode, wait);
}

/*
 * Callback function from exofs_delete_inode() - don't have much cleaning up to
 * do.
 */
1292
static void delete_done(struct exofs_io_state *ios, void *p)
1293
{
1294 1295 1296 1297
	struct exofs_sb_info *sbi = p;

	exofs_put_io_state(ios);

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	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.
 */
void exofs_delete_inode(struct inode *inode)
{
	struct exofs_i_info *oi = exofs_i(inode);
	struct super_block *sb = inode->i_sb;
	struct exofs_sb_info *sbi = sb->s_fs_info;
1311
	struct exofs_io_state *ios;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	int ret;

	truncate_inode_pages(&inode->i_data, 0);

	if (is_bad_inode(inode))
		goto no_delete;

	mark_inode_dirty(inode);
	exofs_update_inode(inode, inode_needs_sync(inode));

	inode->i_size = 0;
	if (inode->i_blocks)
		exofs_truncate(inode);

	clear_inode(inode);

1328
	ret = exofs_get_io_state(&sbi->layout, &ios);
1329 1330
	if (unlikely(ret)) {
		EXOFS_ERR("%s: exofs_get_io_state failed\n", __func__);
1331 1332 1333 1334 1335 1336 1337 1338 1339
		return;
	}

	/* if we are deleting an obj that hasn't been created yet, wait */
	if (!obj_created(oi)) {
		BUG_ON(!obj_2bcreated(oi));
		wait_event(oi->i_wq, obj_created(oi));
	}

1340 1341 1342 1343 1344
	ios->obj.id = exofs_oi_objno(oi);
	ios->done = delete_done;
	ios->private = sbi;
	ios->cred = oi->i_cred;
	ret = exofs_sbi_remove(ios);
1345
	if (ret) {
1346 1347
		EXOFS_ERR("%s: exofs_sbi_remove failed\n", __func__);
		exofs_put_io_state(ios);
1348 1349 1350 1351 1352 1353 1354 1355 1356
		return;
	}
	atomic_inc(&sbi->s_curr_pending);

	return;

no_delete:
	clear_inode(inode);
}