Revert "module: fix __module_ref_addr()"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / exofs / inode.c
blob6f7df0f7d2821acfdd41b72188a53e0c48a7f4c5
1 /*
2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com)
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
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
16 * This file is part of exofs.
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.
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.
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
34 #include <linux/writeback.h>
35 #include <linux/buffer_head.h>
36 #include <scsi/scsi_device.h>
38 #include "exofs.h"
40 #ifdef CONFIG_EXOFS_DEBUG
41 # define EXOFS_DEBUG_OBJ_ISIZE 1
42 #endif
44 struct page_collect {
45 struct exofs_sb_info *sbi;
46 struct request_queue *req_q;
47 struct inode *inode;
48 unsigned expected_pages;
50 struct bio *bio;
51 unsigned nr_pages;
52 unsigned long length;
53 loff_t pg_first; /* keep 64bit also in 32-arches */
56 static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
57 struct inode *inode)
59 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
61 pcol->sbi = sbi;
62 pcol->req_q = osd_request_queue(sbi->s_dev);
63 pcol->inode = inode;
64 pcol->expected_pages = expected_pages;
66 pcol->bio = NULL;
67 pcol->nr_pages = 0;
68 pcol->length = 0;
69 pcol->pg_first = -1;
71 EXOFS_DBGMSG("_pcol_init ino=0x%lx expected_pages=%u\n", inode->i_ino,
72 expected_pages);
75 static void _pcol_reset(struct page_collect *pcol)
77 pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
79 pcol->bio = NULL;
80 pcol->nr_pages = 0;
81 pcol->length = 0;
82 pcol->pg_first = -1;
83 EXOFS_DBGMSG("_pcol_reset ino=0x%lx expected_pages=%u\n",
84 pcol->inode->i_ino, pcol->expected_pages);
86 /* this is probably the end of the loop but in writes
87 * it might not end here. don't be left with nothing
89 if (!pcol->expected_pages)
90 pcol->expected_pages = 128;
93 static int pcol_try_alloc(struct page_collect *pcol)
95 int pages = min_t(unsigned, pcol->expected_pages, BIO_MAX_PAGES);
97 for (; pages; pages >>= 1) {
98 pcol->bio = bio_alloc(GFP_KERNEL, pages);
99 if (likely(pcol->bio))
100 return 0;
103 EXOFS_ERR("Failed to kcalloc expected_pages=%u\n",
104 pcol->expected_pages);
105 return -ENOMEM;
108 static void pcol_free(struct page_collect *pcol)
110 bio_put(pcol->bio);
111 pcol->bio = NULL;
114 static int pcol_add_page(struct page_collect *pcol, struct page *page,
115 unsigned len)
117 int added_len = bio_add_pc_page(pcol->req_q, pcol->bio, page, len, 0);
118 if (unlikely(len != added_len))
119 return -ENOMEM;
121 ++pcol->nr_pages;
122 pcol->length += len;
123 return 0;
126 static int update_read_page(struct page *page, int ret)
128 if (ret == 0) {
129 /* Everything is OK */
130 SetPageUptodate(page);
131 if (PageError(page))
132 ClearPageError(page);
133 } else if (ret == -EFAULT) {
134 /* In this case we were trying to read something that wasn't on
135 * disk yet - return a page full of zeroes. This should be OK,
136 * because the object should be empty (if there was a write
137 * before this read, the read would be waiting with the page
138 * locked */
139 clear_highpage(page);
141 SetPageUptodate(page);
142 if (PageError(page))
143 ClearPageError(page);
144 ret = 0; /* recovered error */
145 EXOFS_DBGMSG("recovered read error\n");
146 } else /* Error */
147 SetPageError(page);
149 return ret;
152 static void update_write_page(struct page *page, int ret)
154 if (ret) {
155 mapping_set_error(page->mapping, ret);
156 SetPageError(page);
158 end_page_writeback(page);
161 /* Called at the end of reads, to optionally unlock pages and update their
162 * status.
164 static int __readpages_done(struct osd_request *or, struct page_collect *pcol,
165 bool do_unlock)
167 struct bio_vec *bvec;
168 int i;
169 u64 resid;
170 u64 good_bytes;
171 u64 length = 0;
172 int ret = exofs_check_ok_resid(or, &resid, NULL);
174 osd_end_request(or);
176 if (likely(!ret))
177 good_bytes = pcol->length;
178 else if (!resid)
179 good_bytes = 0;
180 else
181 good_bytes = pcol->length - resid;
183 EXOFS_DBGMSG("readpages_done(0x%lx) good_bytes=0x%llx"
184 " length=0x%lx nr_pages=%u\n",
185 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
186 pcol->nr_pages);
188 __bio_for_each_segment(bvec, pcol->bio, i, 0) {
189 struct page *page = bvec->bv_page;
190 struct inode *inode = page->mapping->host;
191 int page_stat;
193 if (inode != pcol->inode)
194 continue; /* osd might add more pages at end */
196 if (likely(length < good_bytes))
197 page_stat = 0;
198 else
199 page_stat = ret;
201 EXOFS_DBGMSG(" readpages_done(0x%lx, 0x%lx) %s\n",
202 inode->i_ino, page->index,
203 page_stat ? "bad_bytes" : "good_bytes");
205 ret = update_read_page(page, page_stat);
206 if (do_unlock)
207 unlock_page(page);
208 length += bvec->bv_len;
211 pcol_free(pcol);
212 EXOFS_DBGMSG("readpages_done END\n");
213 return ret;
216 /* callback of async reads */
217 static void readpages_done(struct osd_request *or, void *p)
219 struct page_collect *pcol = p;
221 __readpages_done(or, pcol, true);
222 atomic_dec(&pcol->sbi->s_curr_pending);
223 kfree(p);
226 static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
228 struct bio_vec *bvec;
229 int i;
231 __bio_for_each_segment(bvec, pcol->bio, i, 0) {
232 struct page *page = bvec->bv_page;
234 if (rw == READ)
235 update_read_page(page, ret);
236 else
237 update_write_page(page, ret);
239 unlock_page(page);
241 pcol_free(pcol);
244 static int read_exec(struct page_collect *pcol, bool is_sync)
246 struct exofs_i_info *oi = exofs_i(pcol->inode);
247 struct osd_obj_id obj = {pcol->sbi->s_pid,
248 pcol->inode->i_ino + EXOFS_OBJ_OFF};
249 struct osd_request *or = NULL;
250 struct page_collect *pcol_copy = NULL;
251 loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
252 int ret;
254 if (!pcol->bio)
255 return 0;
257 /* see comment in _readpage() about sync reads */
258 WARN_ON(is_sync && (pcol->nr_pages != 1));
260 or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
261 if (unlikely(!or)) {
262 ret = -ENOMEM;
263 goto err;
266 osd_req_read(or, &obj, i_start, pcol->bio, pcol->length);
268 if (is_sync) {
269 exofs_sync_op(or, pcol->sbi->s_timeout, oi->i_cred);
270 return __readpages_done(or, pcol, false);
273 pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
274 if (!pcol_copy) {
275 ret = -ENOMEM;
276 goto err;
279 *pcol_copy = *pcol;
280 ret = exofs_async_op(or, readpages_done, pcol_copy, oi->i_cred);
281 if (unlikely(ret))
282 goto err;
284 atomic_inc(&pcol->sbi->s_curr_pending);
286 EXOFS_DBGMSG("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
287 obj.id, _LLU(i_start), pcol->length);
289 /* pages ownership was passed to pcol_copy */
290 _pcol_reset(pcol);
291 return 0;
293 err:
294 if (!is_sync)
295 _unlock_pcol_pages(pcol, ret, READ);
296 else /* Pages unlocked by caller in sync mode only free bio */
297 pcol_free(pcol);
299 kfree(pcol_copy);
300 if (or)
301 osd_end_request(or);
302 return ret;
305 /* readpage_strip is called either directly from readpage() or by the VFS from
306 * within read_cache_pages(), to add one more page to be read. It will try to
307 * collect as many contiguous pages as posible. If a discontinuity is
308 * encountered, or it runs out of resources, it will submit the previous segment
309 * and will start a new collection. Eventually caller must submit the last
310 * segment if present.
312 static int readpage_strip(void *data, struct page *page)
314 struct page_collect *pcol = data;
315 struct inode *inode = pcol->inode;
316 struct exofs_i_info *oi = exofs_i(inode);
317 loff_t i_size = i_size_read(inode);
318 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
319 size_t len;
320 int ret;
322 /* FIXME: Just for debugging, will be removed */
323 if (PageUptodate(page))
324 EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
325 page->index);
327 if (page->index < end_index)
328 len = PAGE_CACHE_SIZE;
329 else if (page->index == end_index)
330 len = i_size & ~PAGE_CACHE_MASK;
331 else
332 len = 0;
334 if (!len || !obj_created(oi)) {
335 /* this will be out of bounds, or doesn't exist yet.
336 * Current page is cleared and the request is split
338 clear_highpage(page);
340 SetPageUptodate(page);
341 if (PageError(page))
342 ClearPageError(page);
344 unlock_page(page);
345 EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page,"
346 " splitting\n", inode->i_ino, page->index);
348 return read_exec(pcol, false);
351 try_again:
353 if (unlikely(pcol->pg_first == -1)) {
354 pcol->pg_first = page->index;
355 } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
356 page->index)) {
357 /* Discontinuity detected, split the request */
358 ret = read_exec(pcol, false);
359 if (unlikely(ret))
360 goto fail;
361 goto try_again;
364 if (!pcol->bio) {
365 ret = pcol_try_alloc(pcol);
366 if (unlikely(ret))
367 goto fail;
370 if (len != PAGE_CACHE_SIZE)
371 zero_user(page, len, PAGE_CACHE_SIZE - len);
373 EXOFS_DBGMSG(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
374 inode->i_ino, page->index, len);
376 ret = pcol_add_page(pcol, page, len);
377 if (ret) {
378 EXOFS_DBGMSG("Failed pcol_add_page pages[i]=%p "
379 "this_len=0x%zx nr_pages=%u length=0x%lx\n",
380 page, len, pcol->nr_pages, pcol->length);
382 /* split the request, and start again with current page */
383 ret = read_exec(pcol, false);
384 if (unlikely(ret))
385 goto fail;
387 goto try_again;
390 return 0;
392 fail:
393 /* SetPageError(page); ??? */
394 unlock_page(page);
395 return ret;
398 static int exofs_readpages(struct file *file, struct address_space *mapping,
399 struct list_head *pages, unsigned nr_pages)
401 struct page_collect pcol;
402 int ret;
404 _pcol_init(&pcol, nr_pages, mapping->host);
406 ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
407 if (ret) {
408 EXOFS_ERR("read_cache_pages => %d\n", ret);
409 return ret;
412 return read_exec(&pcol, false);
415 static int _readpage(struct page *page, bool is_sync)
417 struct page_collect pcol;
418 int ret;
420 _pcol_init(&pcol, 1, page->mapping->host);
422 /* readpage_strip might call read_exec(,async) inside at several places
423 * but this is safe for is_async=0 since read_exec will not do anything
424 * when we have a single page.
426 ret = readpage_strip(&pcol, page);
427 if (ret) {
428 EXOFS_ERR("_readpage => %d\n", ret);
429 return ret;
432 return read_exec(&pcol, is_sync);
436 * We don't need the file
438 static int exofs_readpage(struct file *file, struct page *page)
440 return _readpage(page, false);
443 /* Callback for osd_write. All writes are asynchronouse */
444 static void writepages_done(struct osd_request *or, void *p)
446 struct page_collect *pcol = p;
447 struct bio_vec *bvec;
448 int i;
449 u64 resid;
450 u64 good_bytes;
451 u64 length = 0;
453 int ret = exofs_check_ok_resid(or, NULL, &resid);
455 osd_end_request(or);
456 atomic_dec(&pcol->sbi->s_curr_pending);
458 if (likely(!ret))
459 good_bytes = pcol->length;
460 else if (!resid)
461 good_bytes = 0;
462 else
463 good_bytes = pcol->length - resid;
465 EXOFS_DBGMSG("writepages_done(0x%lx) good_bytes=0x%llx"
466 " length=0x%lx nr_pages=%u\n",
467 pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
468 pcol->nr_pages);
470 __bio_for_each_segment(bvec, pcol->bio, i, 0) {
471 struct page *page = bvec->bv_page;
472 struct inode *inode = page->mapping->host;
473 int page_stat;
475 if (inode != pcol->inode)
476 continue; /* osd might add more pages to a bio */
478 if (likely(length < good_bytes))
479 page_stat = 0;
480 else
481 page_stat = ret;
483 update_write_page(page, page_stat);
484 unlock_page(page);
485 EXOFS_DBGMSG(" writepages_done(0x%lx, 0x%lx) status=%d\n",
486 inode->i_ino, page->index, page_stat);
488 length += bvec->bv_len;
491 pcol_free(pcol);
492 kfree(pcol);
493 EXOFS_DBGMSG("writepages_done END\n");
496 static int write_exec(struct page_collect *pcol)
498 struct exofs_i_info *oi = exofs_i(pcol->inode);
499 struct osd_obj_id obj = {pcol->sbi->s_pid,
500 pcol->inode->i_ino + EXOFS_OBJ_OFF};
501 struct osd_request *or = NULL;
502 struct page_collect *pcol_copy = NULL;
503 loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
504 int ret;
506 if (!pcol->bio)
507 return 0;
509 or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
510 if (unlikely(!or)) {
511 EXOFS_ERR("write_exec: Faild to osd_start_request()\n");
512 ret = -ENOMEM;
513 goto err;
516 pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
517 if (!pcol_copy) {
518 EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
519 ret = -ENOMEM;
520 goto err;
523 *pcol_copy = *pcol;
525 pcol_copy->bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
526 osd_req_write(or, &obj, i_start, pcol_copy->bio, pcol_copy->length);
527 ret = exofs_async_op(or, writepages_done, pcol_copy, oi->i_cred);
528 if (unlikely(ret)) {
529 EXOFS_ERR("write_exec: exofs_async_op() Faild\n");
530 goto err;
533 atomic_inc(&pcol->sbi->s_curr_pending);
534 EXOFS_DBGMSG("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
535 pcol->inode->i_ino, pcol->pg_first, _LLU(i_start),
536 pcol->length);
537 /* pages ownership was passed to pcol_copy */
538 _pcol_reset(pcol);
539 return 0;
541 err:
542 _unlock_pcol_pages(pcol, ret, WRITE);
543 kfree(pcol_copy);
544 if (or)
545 osd_end_request(or);
546 return ret;
549 /* writepage_strip is called either directly from writepage() or by the VFS from
550 * within write_cache_pages(), to add one more page to be written to storage.
551 * It will try to collect as many contiguous pages as possible. If a
552 * discontinuity is encountered or it runs out of resources it will submit the
553 * previous segment and will start a new collection.
554 * Eventually caller must submit the last segment if present.
556 static int writepage_strip(struct page *page,
557 struct writeback_control *wbc_unused, void *data)
559 struct page_collect *pcol = data;
560 struct inode *inode = pcol->inode;
561 struct exofs_i_info *oi = exofs_i(inode);
562 loff_t i_size = i_size_read(inode);
563 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
564 size_t len;
565 int ret;
567 BUG_ON(!PageLocked(page));
569 ret = wait_obj_created(oi);
570 if (unlikely(ret))
571 goto fail;
573 if (page->index < end_index)
574 /* in this case, the page is within the limits of the file */
575 len = PAGE_CACHE_SIZE;
576 else {
577 len = i_size & ~PAGE_CACHE_MASK;
579 if (page->index > end_index || !len) {
580 /* in this case, the page is outside the limits
581 * (truncate in progress)
583 ret = write_exec(pcol);
584 if (unlikely(ret))
585 goto fail;
586 if (PageError(page))
587 ClearPageError(page);
588 unlock_page(page);
589 return 0;
593 try_again:
595 if (unlikely(pcol->pg_first == -1)) {
596 pcol->pg_first = page->index;
597 } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
598 page->index)) {
599 /* Discontinuity detected, split the request */
600 ret = write_exec(pcol);
601 if (unlikely(ret))
602 goto fail;
603 goto try_again;
606 if (!pcol->bio) {
607 ret = pcol_try_alloc(pcol);
608 if (unlikely(ret))
609 goto fail;
612 EXOFS_DBGMSG(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
613 inode->i_ino, page->index, len);
615 ret = pcol_add_page(pcol, page, len);
616 if (unlikely(ret)) {
617 EXOFS_DBGMSG("Failed pcol_add_page "
618 "nr_pages=%u total_length=0x%lx\n",
619 pcol->nr_pages, pcol->length);
621 /* split the request, next loop will start again */
622 ret = write_exec(pcol);
623 if (unlikely(ret)) {
624 EXOFS_DBGMSG("write_exec faild => %d", ret);
625 goto fail;
628 goto try_again;
631 BUG_ON(PageWriteback(page));
632 set_page_writeback(page);
634 return 0;
636 fail:
637 set_bit(AS_EIO, &page->mapping->flags);
638 unlock_page(page);
639 return ret;
642 static int exofs_writepages(struct address_space *mapping,
643 struct writeback_control *wbc)
645 struct page_collect pcol;
646 long start, end, expected_pages;
647 int ret;
649 start = wbc->range_start >> PAGE_CACHE_SHIFT;
650 end = (wbc->range_end == LLONG_MAX) ?
651 start + mapping->nrpages :
652 wbc->range_end >> PAGE_CACHE_SHIFT;
654 if (start || end)
655 expected_pages = min(end - start + 1, 32L);
656 else
657 expected_pages = mapping->nrpages;
659 EXOFS_DBGMSG("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx"
660 " m->nrpages=%lu start=0x%lx end=0x%lx\n",
661 mapping->host->i_ino, wbc->range_start, wbc->range_end,
662 mapping->nrpages, start, end);
664 _pcol_init(&pcol, expected_pages, mapping->host);
666 ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
667 if (ret) {
668 EXOFS_ERR("write_cache_pages => %d\n", ret);
669 return ret;
672 return write_exec(&pcol);
675 static int exofs_writepage(struct page *page, struct writeback_control *wbc)
677 struct page_collect pcol;
678 int ret;
680 _pcol_init(&pcol, 1, page->mapping->host);
682 ret = writepage_strip(page, NULL, &pcol);
683 if (ret) {
684 EXOFS_ERR("exofs_writepage => %d\n", ret);
685 return ret;
688 return write_exec(&pcol);
691 int exofs_write_begin(struct file *file, struct address_space *mapping,
692 loff_t pos, unsigned len, unsigned flags,
693 struct page **pagep, void **fsdata)
695 int ret = 0;
696 struct page *page;
698 page = *pagep;
699 if (page == NULL) {
700 ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
701 fsdata);
702 if (ret) {
703 EXOFS_DBGMSG("simple_write_begin faild\n");
704 return ret;
707 page = *pagep;
710 /* read modify write */
711 if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
712 ret = _readpage(page, true);
713 if (ret) {
714 /*SetPageError was done by _readpage. Is it ok?*/
715 unlock_page(page);
716 EXOFS_DBGMSG("__readpage_filler faild\n");
720 return ret;
723 static int exofs_write_begin_export(struct file *file,
724 struct address_space *mapping,
725 loff_t pos, unsigned len, unsigned flags,
726 struct page **pagep, void **fsdata)
728 *pagep = NULL;
730 return exofs_write_begin(file, mapping, pos, len, flags, pagep,
731 fsdata);
734 static int exofs_write_end(struct file *file, struct address_space *mapping,
735 loff_t pos, unsigned len, unsigned copied,
736 struct page *page, void *fsdata)
738 struct inode *inode = mapping->host;
739 /* According to comment in simple_write_end i_mutex is held */
740 loff_t i_size = inode->i_size;
741 int ret;
743 ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
744 if (i_size != inode->i_size)
745 mark_inode_dirty(inode);
746 return ret;
749 const struct address_space_operations exofs_aops = {
750 .readpage = exofs_readpage,
751 .readpages = exofs_readpages,
752 .writepage = exofs_writepage,
753 .writepages = exofs_writepages,
754 .write_begin = exofs_write_begin_export,
755 .write_end = exofs_write_end,
758 /******************************************************************************
759 * INODE OPERATIONS
760 *****************************************************************************/
763 * Test whether an inode is a fast symlink.
765 static inline int exofs_inode_is_fast_symlink(struct inode *inode)
767 struct exofs_i_info *oi = exofs_i(inode);
769 return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
773 * get_block_t - Fill in a buffer_head
774 * An OSD takes care of block allocation so we just fake an allocation by
775 * putting in the inode's sector_t in the buffer_head.
776 * TODO: What about the case of create==0 and @iblock does not exist in the
777 * object?
779 static int exofs_get_block(struct inode *inode, sector_t iblock,
780 struct buffer_head *bh_result, int create)
782 map_bh(bh_result, inode->i_sb, iblock);
783 return 0;
786 const struct osd_attr g_attr_logical_length = ATTR_DEF(
787 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
790 * Truncate a file to the specified size - all we have to do is set the size
791 * attribute. We make sure the object exists first.
793 void exofs_truncate(struct inode *inode)
795 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
796 struct exofs_i_info *oi = exofs_i(inode);
797 struct osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
798 struct osd_request *or;
799 struct osd_attr attr;
800 loff_t isize = i_size_read(inode);
801 __be64 newsize;
802 int ret;
804 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
805 || S_ISLNK(inode->i_mode)))
806 return;
807 if (exofs_inode_is_fast_symlink(inode))
808 return;
809 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
810 return;
811 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
813 nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
815 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
816 if (unlikely(!or)) {
817 EXOFS_ERR("ERROR: exofs_truncate: osd_start_request failed\n");
818 goto fail;
821 osd_req_set_attributes(or, &obj);
823 newsize = cpu_to_be64((u64)isize);
824 attr = g_attr_logical_length;
825 attr.val_ptr = &newsize;
826 osd_req_add_set_attr_list(or, &attr, 1);
828 /* if we are about to truncate an object, and it hasn't been
829 * created yet, wait
831 if (unlikely(wait_obj_created(oi)))
832 goto fail;
834 ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
835 osd_end_request(or);
836 if (ret)
837 goto fail;
839 out:
840 mark_inode_dirty(inode);
841 return;
842 fail:
843 make_bad_inode(inode);
844 goto out;
848 * Set inode attributes - just call generic functions.
850 int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
852 struct inode *inode = dentry->d_inode;
853 int error;
855 error = inode_change_ok(inode, iattr);
856 if (error)
857 return error;
859 error = inode_setattr(inode, iattr);
860 return error;
864 * Read an inode from the OSD, and return it as is. We also return the size
865 * attribute in the 'sanity' argument if we got compiled with debugging turned
866 * on.
868 static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
869 struct exofs_fcb *inode, uint64_t *sanity)
871 struct exofs_sb_info *sbi = sb->s_fs_info;
872 struct osd_request *or;
873 struct osd_attr attr;
874 struct osd_obj_id obj = {sbi->s_pid,
875 oi->vfs_inode.i_ino + EXOFS_OBJ_OFF};
876 int ret;
878 exofs_make_credential(oi->i_cred, &obj);
880 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
881 if (unlikely(!or)) {
882 EXOFS_ERR("exofs_get_inode: osd_start_request failed.\n");
883 return -ENOMEM;
885 osd_req_get_attributes(or, &obj);
887 /* we need the inode attribute */
888 osd_req_add_get_attr_list(or, &g_attr_inode_data, 1);
890 #ifdef EXOFS_DEBUG_OBJ_ISIZE
891 /* we get the size attributes to do a sanity check */
892 osd_req_add_get_attr_list(or, &g_attr_logical_length, 1);
893 #endif
895 ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
896 if (ret)
897 goto out;
899 attr = g_attr_inode_data;
900 ret = extract_attr_from_req(or, &attr);
901 if (ret) {
902 EXOFS_ERR("exofs_get_inode: extract_attr_from_req failed\n");
903 goto out;
906 WARN_ON(attr.len != EXOFS_INO_ATTR_SIZE);
907 memcpy(inode, attr.val_ptr, EXOFS_INO_ATTR_SIZE);
909 #ifdef EXOFS_DEBUG_OBJ_ISIZE
910 attr = g_attr_logical_length;
911 ret = extract_attr_from_req(or, &attr);
912 if (ret) {
913 EXOFS_ERR("ERROR: extract attr from or failed\n");
914 goto out;
916 *sanity = get_unaligned_be64(attr.val_ptr);
917 #endif
919 out:
920 osd_end_request(or);
921 return ret;
925 * Fill in an inode read from the OSD and set it up for use
927 struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
929 struct exofs_i_info *oi;
930 struct exofs_fcb fcb;
931 struct inode *inode;
932 uint64_t uninitialized_var(sanity);
933 int ret;
935 inode = iget_locked(sb, ino);
936 if (!inode)
937 return ERR_PTR(-ENOMEM);
938 if (!(inode->i_state & I_NEW))
939 return inode;
940 oi = exofs_i(inode);
942 /* read the inode from the osd */
943 ret = exofs_get_inode(sb, oi, &fcb, &sanity);
944 if (ret)
945 goto bad_inode;
947 init_waitqueue_head(&oi->i_wq);
948 set_obj_created(oi);
950 /* copy stuff from on-disk struct to in-memory struct */
951 inode->i_mode = le16_to_cpu(fcb.i_mode);
952 inode->i_uid = le32_to_cpu(fcb.i_uid);
953 inode->i_gid = le32_to_cpu(fcb.i_gid);
954 inode->i_nlink = le16_to_cpu(fcb.i_links_count);
955 inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
956 inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
957 inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
958 inode->i_ctime.tv_nsec =
959 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
960 oi->i_commit_size = le64_to_cpu(fcb.i_size);
961 i_size_write(inode, oi->i_commit_size);
962 inode->i_blkbits = EXOFS_BLKSHIFT;
963 inode->i_generation = le32_to_cpu(fcb.i_generation);
965 #ifdef EXOFS_DEBUG_OBJ_ISIZE
966 if ((inode->i_size != sanity) &&
967 (!exofs_inode_is_fast_symlink(inode))) {
968 EXOFS_ERR("WARNING: Size of object from inode and "
969 "attributes differ (%lld != %llu)\n",
970 inode->i_size, _LLU(sanity));
972 #endif
974 oi->i_dir_start_lookup = 0;
976 if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
977 ret = -ESTALE;
978 goto bad_inode;
981 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
982 if (fcb.i_data[0])
983 inode->i_rdev =
984 old_decode_dev(le32_to_cpu(fcb.i_data[0]));
985 else
986 inode->i_rdev =
987 new_decode_dev(le32_to_cpu(fcb.i_data[1]));
988 } else {
989 memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
992 if (S_ISREG(inode->i_mode)) {
993 inode->i_op = &exofs_file_inode_operations;
994 inode->i_fop = &exofs_file_operations;
995 inode->i_mapping->a_ops = &exofs_aops;
996 } else if (S_ISDIR(inode->i_mode)) {
997 inode->i_op = &exofs_dir_inode_operations;
998 inode->i_fop = &exofs_dir_operations;
999 inode->i_mapping->a_ops = &exofs_aops;
1000 } else if (S_ISLNK(inode->i_mode)) {
1001 if (exofs_inode_is_fast_symlink(inode))
1002 inode->i_op = &exofs_fast_symlink_inode_operations;
1003 else {
1004 inode->i_op = &exofs_symlink_inode_operations;
1005 inode->i_mapping->a_ops = &exofs_aops;
1007 } else {
1008 inode->i_op = &exofs_special_inode_operations;
1009 if (fcb.i_data[0])
1010 init_special_inode(inode, inode->i_mode,
1011 old_decode_dev(le32_to_cpu(fcb.i_data[0])));
1012 else
1013 init_special_inode(inode, inode->i_mode,
1014 new_decode_dev(le32_to_cpu(fcb.i_data[1])));
1017 unlock_new_inode(inode);
1018 return inode;
1020 bad_inode:
1021 iget_failed(inode);
1022 return ERR_PTR(ret);
1025 int __exofs_wait_obj_created(struct exofs_i_info *oi)
1027 if (!obj_created(oi)) {
1028 BUG_ON(!obj_2bcreated(oi));
1029 wait_event(oi->i_wq, obj_created(oi));
1031 return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
1034 * Callback function from exofs_new_inode(). The important thing is that we
1035 * set the obj_created flag so that other methods know that the object exists on
1036 * the OSD.
1038 static void create_done(struct osd_request *or, void *p)
1040 struct inode *inode = p;
1041 struct exofs_i_info *oi = exofs_i(inode);
1042 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
1043 int ret;
1045 ret = exofs_check_ok(or);
1046 osd_end_request(or);
1047 atomic_dec(&sbi->s_curr_pending);
1049 if (unlikely(ret)) {
1050 EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
1051 _LLU(sbi->s_pid), _LLU(inode->i_ino + EXOFS_OBJ_OFF));
1052 make_bad_inode(inode);
1053 } else
1054 set_obj_created(oi);
1056 atomic_dec(&inode->i_count);
1057 wake_up(&oi->i_wq);
1061 * Set up a new inode and create an object for it on the OSD
1063 struct inode *exofs_new_inode(struct inode *dir, int mode)
1065 struct super_block *sb;
1066 struct inode *inode;
1067 struct exofs_i_info *oi;
1068 struct exofs_sb_info *sbi;
1069 struct osd_request *or;
1070 struct osd_obj_id obj;
1071 int ret;
1073 sb = dir->i_sb;
1074 inode = new_inode(sb);
1075 if (!inode)
1076 return ERR_PTR(-ENOMEM);
1078 oi = exofs_i(inode);
1080 init_waitqueue_head(&oi->i_wq);
1081 set_obj_2bcreated(oi);
1083 sbi = sb->s_fs_info;
1085 sb->s_dirt = 1;
1086 inode->i_uid = current->cred->fsuid;
1087 if (dir->i_mode & S_ISGID) {
1088 inode->i_gid = dir->i_gid;
1089 if (S_ISDIR(mode))
1090 mode |= S_ISGID;
1091 } else {
1092 inode->i_gid = current->cred->fsgid;
1094 inode->i_mode = mode;
1096 inode->i_ino = sbi->s_nextid++;
1097 inode->i_blkbits = EXOFS_BLKSHIFT;
1098 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1099 oi->i_commit_size = inode->i_size = 0;
1100 spin_lock(&sbi->s_next_gen_lock);
1101 inode->i_generation = sbi->s_next_generation++;
1102 spin_unlock(&sbi->s_next_gen_lock);
1103 insert_inode_hash(inode);
1105 mark_inode_dirty(inode);
1107 obj.partition = sbi->s_pid;
1108 obj.id = inode->i_ino + EXOFS_OBJ_OFF;
1109 exofs_make_credential(oi->i_cred, &obj);
1111 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
1112 if (unlikely(!or)) {
1113 EXOFS_ERR("exofs_new_inode: osd_start_request failed\n");
1114 return ERR_PTR(-ENOMEM);
1117 osd_req_create_object(or, &obj);
1119 /* increment the refcount so that the inode will still be around when we
1120 * reach the callback
1122 atomic_inc(&inode->i_count);
1124 ret = exofs_async_op(or, create_done, inode, oi->i_cred);
1125 if (ret) {
1126 atomic_dec(&inode->i_count);
1127 osd_end_request(or);
1128 return ERR_PTR(-EIO);
1130 atomic_inc(&sbi->s_curr_pending);
1132 return inode;
1136 * struct to pass two arguments to update_inode's callback
1138 struct updatei_args {
1139 struct exofs_sb_info *sbi;
1140 struct exofs_fcb fcb;
1144 * Callback function from exofs_update_inode().
1146 static void updatei_done(struct osd_request *or, void *p)
1148 struct updatei_args *args = p;
1150 osd_end_request(or);
1152 atomic_dec(&args->sbi->s_curr_pending);
1154 kfree(args);
1158 * Write the inode to the OSD. Just fill up the struct, and set the attribute
1159 * synchronously or asynchronously depending on the do_sync flag.
1161 static int exofs_update_inode(struct inode *inode, int do_sync)
1163 struct exofs_i_info *oi = exofs_i(inode);
1164 struct super_block *sb = inode->i_sb;
1165 struct exofs_sb_info *sbi = sb->s_fs_info;
1166 struct osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
1167 struct osd_request *or;
1168 struct osd_attr attr;
1169 struct exofs_fcb *fcb;
1170 struct updatei_args *args;
1171 int ret;
1173 args = kzalloc(sizeof(*args), GFP_KERNEL);
1174 if (!args)
1175 return -ENOMEM;
1177 fcb = &args->fcb;
1179 fcb->i_mode = cpu_to_le16(inode->i_mode);
1180 fcb->i_uid = cpu_to_le32(inode->i_uid);
1181 fcb->i_gid = cpu_to_le32(inode->i_gid);
1182 fcb->i_links_count = cpu_to_le16(inode->i_nlink);
1183 fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1184 fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1185 fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1186 oi->i_commit_size = i_size_read(inode);
1187 fcb->i_size = cpu_to_le64(oi->i_commit_size);
1188 fcb->i_generation = cpu_to_le32(inode->i_generation);
1190 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1191 if (old_valid_dev(inode->i_rdev)) {
1192 fcb->i_data[0] =
1193 cpu_to_le32(old_encode_dev(inode->i_rdev));
1194 fcb->i_data[1] = 0;
1195 } else {
1196 fcb->i_data[0] = 0;
1197 fcb->i_data[1] =
1198 cpu_to_le32(new_encode_dev(inode->i_rdev));
1199 fcb->i_data[2] = 0;
1201 } else
1202 memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
1204 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
1205 if (unlikely(!or)) {
1206 EXOFS_ERR("exofs_update_inode: osd_start_request failed.\n");
1207 ret = -ENOMEM;
1208 goto free_args;
1211 osd_req_set_attributes(or, &obj);
1213 attr = g_attr_inode_data;
1214 attr.val_ptr = fcb;
1215 osd_req_add_set_attr_list(or, &attr, 1);
1217 if (!obj_created(oi)) {
1218 EXOFS_DBGMSG("!obj_created\n");
1219 BUG_ON(!obj_2bcreated(oi));
1220 wait_event(oi->i_wq, obj_created(oi));
1221 EXOFS_DBGMSG("wait_event done\n");
1224 if (do_sync) {
1225 ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
1226 osd_end_request(or);
1227 goto free_args;
1228 } else {
1229 args->sbi = sbi;
1231 ret = exofs_async_op(or, updatei_done, args, oi->i_cred);
1232 if (ret) {
1233 osd_end_request(or);
1234 goto free_args;
1236 atomic_inc(&sbi->s_curr_pending);
1237 goto out; /* deallocation in updatei_done */
1240 free_args:
1241 kfree(args);
1242 out:
1243 EXOFS_DBGMSG("ret=>%d\n", ret);
1244 return ret;
1247 int exofs_write_inode(struct inode *inode, int wait)
1249 return exofs_update_inode(inode, wait);
1253 * Callback function from exofs_delete_inode() - don't have much cleaning up to
1254 * do.
1256 static void delete_done(struct osd_request *or, void *p)
1258 struct exofs_sb_info *sbi;
1259 osd_end_request(or);
1260 sbi = p;
1261 atomic_dec(&sbi->s_curr_pending);
1265 * Called when the refcount of an inode reaches zero. We remove the object
1266 * from the OSD here. We make sure the object was created before we try and
1267 * delete it.
1269 void exofs_delete_inode(struct inode *inode)
1271 struct exofs_i_info *oi = exofs_i(inode);
1272 struct super_block *sb = inode->i_sb;
1273 struct exofs_sb_info *sbi = sb->s_fs_info;
1274 struct osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
1275 struct osd_request *or;
1276 int ret;
1278 truncate_inode_pages(&inode->i_data, 0);
1280 if (is_bad_inode(inode))
1281 goto no_delete;
1283 mark_inode_dirty(inode);
1284 exofs_update_inode(inode, inode_needs_sync(inode));
1286 inode->i_size = 0;
1287 if (inode->i_blocks)
1288 exofs_truncate(inode);
1290 clear_inode(inode);
1292 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
1293 if (unlikely(!or)) {
1294 EXOFS_ERR("exofs_delete_inode: osd_start_request failed\n");
1295 return;
1298 osd_req_remove_object(or, &obj);
1300 /* if we are deleting an obj that hasn't been created yet, wait */
1301 if (!obj_created(oi)) {
1302 BUG_ON(!obj_2bcreated(oi));
1303 wait_event(oi->i_wq, obj_created(oi));
1306 ret = exofs_async_op(or, delete_done, sbi, oi->i_cred);
1307 if (ret) {
1308 EXOFS_ERR(
1309 "ERROR: @exofs_delete_inode exofs_async_op failed\n");
1310 osd_end_request(or);
1311 return;
1313 atomic_inc(&sbi->s_curr_pending);
1315 return;
1317 no_delete:
1318 clear_inode(inode);