Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005, 2006 |
Boaz Harrosh | 27d2e14 | 2009-06-14 17:23:09 +0300 | [diff] [blame] | 3 | * Avishay Traeger (avishay@gmail.com) |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009 |
| 5 | * Boaz Harrosh <bharrosh@panasas.com> |
| 6 | * |
| 7 | * Copyrights for code taken from ext2: |
| 8 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 9 | * Remy Card (card@masi.ibp.fr) |
| 10 | * Laboratoire MASI - Institut Blaise Pascal |
| 11 | * Universite Pierre et Marie Curie (Paris VI) |
| 12 | * from |
| 13 | * linux/fs/minix/inode.c |
| 14 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 15 | * |
| 16 | * This file is part of exofs. |
| 17 | * |
| 18 | * exofs is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation. Since it is based on ext2, and the only |
| 21 | * valid version of GPL for the Linux kernel is version 2, the only valid |
| 22 | * version of GPL for exofs is version 2. |
| 23 | * |
| 24 | * exofs is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with exofs; if not, write to the Free Software |
| 31 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | */ |
| 33 | |
| 34 | #include <linux/writeback.h> |
| 35 | #include <linux/buffer_head.h> |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 36 | #include <scsi/scsi_device.h> |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include "exofs.h" |
| 39 | |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 40 | #define EXOFS_DBGMSG2(M...) do {} while (0) |
| 41 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 42 | enum { BIO_MAX_PAGES_KMALLOC = |
| 43 | (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec), |
| 44 | }; |
| 45 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 46 | struct page_collect { |
| 47 | struct exofs_sb_info *sbi; |
| 48 | struct request_queue *req_q; |
| 49 | struct inode *inode; |
| 50 | unsigned expected_pages; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 51 | struct exofs_io_state *ios; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 52 | |
| 53 | struct bio *bio; |
| 54 | unsigned nr_pages; |
| 55 | unsigned long length; |
| 56 | loff_t pg_first; /* keep 64bit also in 32-arches */ |
| 57 | }; |
| 58 | |
| 59 | static void _pcol_init(struct page_collect *pcol, unsigned expected_pages, |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 60 | struct inode *inode) |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 61 | { |
| 62 | struct exofs_sb_info *sbi = inode->i_sb->s_fs_info; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 63 | |
| 64 | pcol->sbi = sbi; |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 65 | /* Create master bios on first Q, later on cloning, each clone will be |
| 66 | * allocated on it's destination Q |
| 67 | */ |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 68 | pcol->req_q = osd_request_queue(sbi->layout.s_ods[0]); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 69 | pcol->inode = inode; |
| 70 | pcol->expected_pages = expected_pages; |
| 71 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 72 | pcol->ios = NULL; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 73 | pcol->bio = NULL; |
| 74 | pcol->nr_pages = 0; |
| 75 | pcol->length = 0; |
| 76 | pcol->pg_first = -1; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void _pcol_reset(struct page_collect *pcol) |
| 80 | { |
| 81 | pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages); |
| 82 | |
| 83 | pcol->bio = NULL; |
| 84 | pcol->nr_pages = 0; |
| 85 | pcol->length = 0; |
| 86 | pcol->pg_first = -1; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 87 | pcol->ios = NULL; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 88 | |
| 89 | /* this is probably the end of the loop but in writes |
| 90 | * it might not end here. don't be left with nothing |
| 91 | */ |
| 92 | if (!pcol->expected_pages) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 93 | pcol->expected_pages = BIO_MAX_PAGES_KMALLOC; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int pcol_try_alloc(struct page_collect *pcol) |
| 97 | { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 98 | int pages = min_t(unsigned, pcol->expected_pages, |
| 99 | BIO_MAX_PAGES_KMALLOC); |
| 100 | |
| 101 | if (!pcol->ios) { /* First time allocate io_state */ |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 102 | int ret = exofs_get_io_state(&pcol->sbi->layout, &pcol->ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 103 | |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | } |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 107 | |
| 108 | for (; pages; pages >>= 1) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 109 | pcol->bio = bio_kmalloc(GFP_KERNEL, pages); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 110 | if (likely(pcol->bio)) |
| 111 | return 0; |
| 112 | } |
| 113 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 114 | EXOFS_ERR("Failed to bio_kmalloc expected_pages=%u\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 115 | pcol->expected_pages); |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | static void pcol_free(struct page_collect *pcol) |
| 120 | { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 121 | if (pcol->bio) { |
| 122 | bio_put(pcol->bio); |
| 123 | pcol->bio = NULL; |
| 124 | } |
| 125 | |
| 126 | if (pcol->ios) { |
| 127 | exofs_put_io_state(pcol->ios); |
| 128 | pcol->ios = NULL; |
| 129 | } |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int pcol_add_page(struct page_collect *pcol, struct page *page, |
| 133 | unsigned len) |
| 134 | { |
| 135 | int added_len = bio_add_pc_page(pcol->req_q, pcol->bio, page, len, 0); |
| 136 | if (unlikely(len != added_len)) |
| 137 | return -ENOMEM; |
| 138 | |
| 139 | ++pcol->nr_pages; |
| 140 | pcol->length += len; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int update_read_page(struct page *page, int ret) |
| 145 | { |
| 146 | if (ret == 0) { |
| 147 | /* Everything is OK */ |
| 148 | SetPageUptodate(page); |
| 149 | if (PageError(page)) |
| 150 | ClearPageError(page); |
| 151 | } else if (ret == -EFAULT) { |
| 152 | /* In this case we were trying to read something that wasn't on |
| 153 | * disk yet - return a page full of zeroes. This should be OK, |
| 154 | * because the object should be empty (if there was a write |
| 155 | * before this read, the read would be waiting with the page |
| 156 | * locked */ |
| 157 | clear_highpage(page); |
| 158 | |
| 159 | SetPageUptodate(page); |
| 160 | if (PageError(page)) |
| 161 | ClearPageError(page); |
| 162 | ret = 0; /* recovered error */ |
| 163 | EXOFS_DBGMSG("recovered read error\n"); |
| 164 | } else /* Error */ |
| 165 | SetPageError(page); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static void update_write_page(struct page *page, int ret) |
| 171 | { |
| 172 | if (ret) { |
| 173 | mapping_set_error(page->mapping, ret); |
| 174 | SetPageError(page); |
| 175 | } |
| 176 | end_page_writeback(page); |
| 177 | } |
| 178 | |
| 179 | /* Called at the end of reads, to optionally unlock pages and update their |
| 180 | * status. |
| 181 | */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 182 | static int __readpages_done(struct page_collect *pcol, bool do_unlock) |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 183 | { |
| 184 | struct bio_vec *bvec; |
| 185 | int i; |
| 186 | u64 resid; |
| 187 | u64 good_bytes; |
| 188 | u64 length = 0; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 189 | int ret = exofs_check_io(pcol->ios, &resid); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 190 | |
| 191 | if (likely(!ret)) |
| 192 | good_bytes = pcol->length; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 193 | else |
| 194 | good_bytes = pcol->length - resid; |
| 195 | |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 196 | EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx" |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 197 | " length=0x%lx nr_pages=%u\n", |
| 198 | pcol->inode->i_ino, _LLU(good_bytes), pcol->length, |
| 199 | pcol->nr_pages); |
| 200 | |
| 201 | __bio_for_each_segment(bvec, pcol->bio, i, 0) { |
| 202 | struct page *page = bvec->bv_page; |
| 203 | struct inode *inode = page->mapping->host; |
| 204 | int page_stat; |
| 205 | |
| 206 | if (inode != pcol->inode) |
| 207 | continue; /* osd might add more pages at end */ |
| 208 | |
| 209 | if (likely(length < good_bytes)) |
| 210 | page_stat = 0; |
| 211 | else |
| 212 | page_stat = ret; |
| 213 | |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 214 | EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 215 | inode->i_ino, page->index, |
| 216 | page_stat ? "bad_bytes" : "good_bytes"); |
| 217 | |
| 218 | ret = update_read_page(page, page_stat); |
| 219 | if (do_unlock) |
| 220 | unlock_page(page); |
| 221 | length += bvec->bv_len; |
| 222 | } |
| 223 | |
| 224 | pcol_free(pcol); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 225 | EXOFS_DBGMSG2("readpages_done END\n"); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /* callback of async reads */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 230 | static void readpages_done(struct exofs_io_state *ios, void *p) |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 231 | { |
| 232 | struct page_collect *pcol = p; |
| 233 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 234 | __readpages_done(pcol, true); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 235 | atomic_dec(&pcol->sbi->s_curr_pending); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 236 | kfree(pcol); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw) |
| 240 | { |
| 241 | struct bio_vec *bvec; |
| 242 | int i; |
| 243 | |
| 244 | __bio_for_each_segment(bvec, pcol->bio, i, 0) { |
| 245 | struct page *page = bvec->bv_page; |
| 246 | |
| 247 | if (rw == READ) |
| 248 | update_read_page(page, ret); |
| 249 | else |
| 250 | update_write_page(page, ret); |
| 251 | |
| 252 | unlock_page(page); |
| 253 | } |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static int read_exec(struct page_collect *pcol, bool is_sync) |
| 257 | { |
| 258 | struct exofs_i_info *oi = exofs_i(pcol->inode); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 259 | struct exofs_io_state *ios = pcol->ios; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 260 | struct page_collect *pcol_copy = NULL; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 261 | int ret; |
| 262 | |
| 263 | if (!pcol->bio) |
| 264 | return 0; |
| 265 | |
| 266 | /* see comment in _readpage() about sync reads */ |
| 267 | WARN_ON(is_sync && (pcol->nr_pages != 1)); |
| 268 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 269 | ios->bio = pcol->bio; |
| 270 | ios->length = pcol->length; |
| 271 | ios->offset = pcol->pg_first << PAGE_CACHE_SHIFT; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 272 | |
| 273 | if (is_sync) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 274 | exofs_oi_read(oi, pcol->ios); |
| 275 | return __readpages_done(pcol, false); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL); |
| 279 | if (!pcol_copy) { |
| 280 | ret = -ENOMEM; |
| 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | *pcol_copy = *pcol; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 285 | ios->done = readpages_done; |
| 286 | ios->private = pcol_copy; |
| 287 | ret = exofs_oi_read(oi, ios); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 288 | if (unlikely(ret)) |
| 289 | goto err; |
| 290 | |
| 291 | atomic_inc(&pcol->sbi->s_curr_pending); |
| 292 | |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 293 | EXOFS_DBGMSG2("read_exec obj=0x%llx start=0x%llx length=0x%lx\n", |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 294 | ios->obj.id, _LLU(ios->offset), pcol->length); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 295 | |
| 296 | /* pages ownership was passed to pcol_copy */ |
| 297 | _pcol_reset(pcol); |
| 298 | return 0; |
| 299 | |
| 300 | err: |
| 301 | if (!is_sync) |
| 302 | _unlock_pcol_pages(pcol, ret, READ); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 303 | |
| 304 | pcol_free(pcol); |
Boaz Harrosh | b76a3f9 | 2009-06-08 19:28:41 +0300 | [diff] [blame] | 305 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 306 | kfree(pcol_copy); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* readpage_strip is called either directly from readpage() or by the VFS from |
| 311 | * within read_cache_pages(), to add one more page to be read. It will try to |
| 312 | * collect as many contiguous pages as posible. If a discontinuity is |
| 313 | * encountered, or it runs out of resources, it will submit the previous segment |
| 314 | * and will start a new collection. Eventually caller must submit the last |
| 315 | * segment if present. |
| 316 | */ |
| 317 | static int readpage_strip(void *data, struct page *page) |
| 318 | { |
| 319 | struct page_collect *pcol = data; |
| 320 | struct inode *inode = pcol->inode; |
| 321 | struct exofs_i_info *oi = exofs_i(inode); |
| 322 | loff_t i_size = i_size_read(inode); |
| 323 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 324 | size_t len; |
| 325 | int ret; |
| 326 | |
| 327 | /* FIXME: Just for debugging, will be removed */ |
| 328 | if (PageUptodate(page)) |
| 329 | EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino, |
| 330 | page->index); |
| 331 | |
| 332 | if (page->index < end_index) |
| 333 | len = PAGE_CACHE_SIZE; |
| 334 | else if (page->index == end_index) |
| 335 | len = i_size & ~PAGE_CACHE_MASK; |
| 336 | else |
| 337 | len = 0; |
| 338 | |
| 339 | if (!len || !obj_created(oi)) { |
| 340 | /* this will be out of bounds, or doesn't exist yet. |
| 341 | * Current page is cleared and the request is split |
| 342 | */ |
| 343 | clear_highpage(page); |
| 344 | |
| 345 | SetPageUptodate(page); |
| 346 | if (PageError(page)) |
| 347 | ClearPageError(page); |
| 348 | |
| 349 | unlock_page(page); |
| 350 | EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page," |
| 351 | " splitting\n", inode->i_ino, page->index); |
| 352 | |
| 353 | return read_exec(pcol, false); |
| 354 | } |
| 355 | |
| 356 | try_again: |
| 357 | |
| 358 | if (unlikely(pcol->pg_first == -1)) { |
| 359 | pcol->pg_first = page->index; |
| 360 | } else if (unlikely((pcol->pg_first + pcol->nr_pages) != |
| 361 | page->index)) { |
| 362 | /* Discontinuity detected, split the request */ |
| 363 | ret = read_exec(pcol, false); |
| 364 | if (unlikely(ret)) |
| 365 | goto fail; |
| 366 | goto try_again; |
| 367 | } |
| 368 | |
| 369 | if (!pcol->bio) { |
| 370 | ret = pcol_try_alloc(pcol); |
| 371 | if (unlikely(ret)) |
| 372 | goto fail; |
| 373 | } |
| 374 | |
| 375 | if (len != PAGE_CACHE_SIZE) |
| 376 | zero_user(page, len, PAGE_CACHE_SIZE - len); |
| 377 | |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 378 | EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 379 | inode->i_ino, page->index, len); |
| 380 | |
| 381 | ret = pcol_add_page(pcol, page, len); |
| 382 | if (ret) { |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 383 | EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p " |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 384 | "this_len=0x%zx nr_pages=%u length=0x%lx\n", |
| 385 | page, len, pcol->nr_pages, pcol->length); |
| 386 | |
| 387 | /* split the request, and start again with current page */ |
| 388 | ret = read_exec(pcol, false); |
| 389 | if (unlikely(ret)) |
| 390 | goto fail; |
| 391 | |
| 392 | goto try_again; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | |
| 397 | fail: |
| 398 | /* SetPageError(page); ??? */ |
| 399 | unlock_page(page); |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static int exofs_readpages(struct file *file, struct address_space *mapping, |
| 404 | struct list_head *pages, unsigned nr_pages) |
| 405 | { |
| 406 | struct page_collect pcol; |
| 407 | int ret; |
| 408 | |
| 409 | _pcol_init(&pcol, nr_pages, mapping->host); |
| 410 | |
| 411 | ret = read_cache_pages(mapping, pages, readpage_strip, &pcol); |
| 412 | if (ret) { |
| 413 | EXOFS_ERR("read_cache_pages => %d\n", ret); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | return read_exec(&pcol, false); |
| 418 | } |
| 419 | |
| 420 | static int _readpage(struct page *page, bool is_sync) |
| 421 | { |
| 422 | struct page_collect pcol; |
| 423 | int ret; |
| 424 | |
| 425 | _pcol_init(&pcol, 1, page->mapping->host); |
| 426 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 427 | /* readpage_strip might call read_exec(,is_sync==false) at several |
| 428 | * places but not if we have a single page. |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 429 | */ |
| 430 | ret = readpage_strip(&pcol, page); |
| 431 | if (ret) { |
| 432 | EXOFS_ERR("_readpage => %d\n", ret); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | return read_exec(&pcol, is_sync); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * We don't need the file |
| 441 | */ |
| 442 | static int exofs_readpage(struct file *file, struct page *page) |
| 443 | { |
| 444 | return _readpage(page, false); |
| 445 | } |
| 446 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 447 | /* Callback for osd_write. All writes are asynchronous */ |
| 448 | static void writepages_done(struct exofs_io_state *ios, void *p) |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 449 | { |
| 450 | struct page_collect *pcol = p; |
| 451 | struct bio_vec *bvec; |
| 452 | int i; |
| 453 | u64 resid; |
| 454 | u64 good_bytes; |
| 455 | u64 length = 0; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 456 | int ret = exofs_check_io(ios, &resid); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 457 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 458 | atomic_dec(&pcol->sbi->s_curr_pending); |
| 459 | |
| 460 | if (likely(!ret)) |
| 461 | good_bytes = pcol->length; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 462 | else |
| 463 | good_bytes = pcol->length - resid; |
| 464 | |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 465 | EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx" |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 466 | " length=0x%lx nr_pages=%u\n", |
| 467 | pcol->inode->i_ino, _LLU(good_bytes), pcol->length, |
| 468 | pcol->nr_pages); |
| 469 | |
| 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; |
| 474 | |
| 475 | if (inode != pcol->inode) |
| 476 | continue; /* osd might add more pages to a bio */ |
| 477 | |
| 478 | if (likely(length < good_bytes)) |
| 479 | page_stat = 0; |
| 480 | else |
| 481 | page_stat = ret; |
| 482 | |
| 483 | update_write_page(page, page_stat); |
| 484 | unlock_page(page); |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 485 | EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 486 | inode->i_ino, page->index, page_stat); |
| 487 | |
| 488 | length += bvec->bv_len; |
| 489 | } |
| 490 | |
| 491 | pcol_free(pcol); |
| 492 | kfree(pcol); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 493 | EXOFS_DBGMSG2("writepages_done END\n"); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static int write_exec(struct page_collect *pcol) |
| 497 | { |
| 498 | struct exofs_i_info *oi = exofs_i(pcol->inode); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 499 | struct exofs_io_state *ios = pcol->ios; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 500 | struct page_collect *pcol_copy = NULL; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 501 | int ret; |
| 502 | |
| 503 | if (!pcol->bio) |
| 504 | return 0; |
| 505 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 506 | pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL); |
| 507 | if (!pcol_copy) { |
| 508 | EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n"); |
| 509 | ret = -ENOMEM; |
| 510 | goto err; |
| 511 | } |
| 512 | |
| 513 | *pcol_copy = *pcol; |
| 514 | |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 515 | pcol_copy->bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 516 | |
| 517 | ios->bio = pcol_copy->bio; |
| 518 | ios->offset = pcol_copy->pg_first << PAGE_CACHE_SHIFT; |
| 519 | ios->length = pcol_copy->length; |
| 520 | ios->done = writepages_done; |
| 521 | ios->private = pcol_copy; |
| 522 | |
| 523 | ret = exofs_oi_write(oi, ios); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 524 | if (unlikely(ret)) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 525 | EXOFS_ERR("write_exec: exofs_oi_write() Faild\n"); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 526 | goto err; |
| 527 | } |
| 528 | |
| 529 | atomic_inc(&pcol->sbi->s_curr_pending); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 530 | EXOFS_DBGMSG2("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n", |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 531 | pcol->inode->i_ino, pcol->pg_first, _LLU(ios->offset), |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 532 | pcol->length); |
| 533 | /* pages ownership was passed to pcol_copy */ |
| 534 | _pcol_reset(pcol); |
| 535 | return 0; |
| 536 | |
| 537 | err: |
| 538 | _unlock_pcol_pages(pcol, ret, WRITE); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 539 | pcol_free(pcol); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 540 | kfree(pcol_copy); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 541 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | /* writepage_strip is called either directly from writepage() or by the VFS from |
| 546 | * within write_cache_pages(), to add one more page to be written to storage. |
| 547 | * It will try to collect as many contiguous pages as possible. If a |
| 548 | * discontinuity is encountered or it runs out of resources it will submit the |
| 549 | * previous segment and will start a new collection. |
| 550 | * Eventually caller must submit the last segment if present. |
| 551 | */ |
| 552 | static int writepage_strip(struct page *page, |
| 553 | struct writeback_control *wbc_unused, void *data) |
| 554 | { |
| 555 | struct page_collect *pcol = data; |
| 556 | struct inode *inode = pcol->inode; |
| 557 | struct exofs_i_info *oi = exofs_i(inode); |
| 558 | loff_t i_size = i_size_read(inode); |
| 559 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 560 | size_t len; |
| 561 | int ret; |
| 562 | |
| 563 | BUG_ON(!PageLocked(page)); |
| 564 | |
| 565 | ret = wait_obj_created(oi); |
| 566 | if (unlikely(ret)) |
| 567 | goto fail; |
| 568 | |
| 569 | if (page->index < end_index) |
| 570 | /* in this case, the page is within the limits of the file */ |
| 571 | len = PAGE_CACHE_SIZE; |
| 572 | else { |
| 573 | len = i_size & ~PAGE_CACHE_MASK; |
| 574 | |
| 575 | if (page->index > end_index || !len) { |
| 576 | /* in this case, the page is outside the limits |
| 577 | * (truncate in progress) |
| 578 | */ |
| 579 | ret = write_exec(pcol); |
| 580 | if (unlikely(ret)) |
| 581 | goto fail; |
| 582 | if (PageError(page)) |
| 583 | ClearPageError(page); |
| 584 | unlock_page(page); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 585 | EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) " |
| 586 | "outside the limits\n", |
| 587 | inode->i_ino, page->index); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | try_again: |
| 593 | |
| 594 | if (unlikely(pcol->pg_first == -1)) { |
| 595 | pcol->pg_first = page->index; |
| 596 | } else if (unlikely((pcol->pg_first + pcol->nr_pages) != |
| 597 | page->index)) { |
| 598 | /* Discontinuity detected, split the request */ |
| 599 | ret = write_exec(pcol); |
| 600 | if (unlikely(ret)) |
| 601 | goto fail; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 602 | |
| 603 | EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n", |
| 604 | inode->i_ino, page->index); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 605 | goto try_again; |
| 606 | } |
| 607 | |
| 608 | if (!pcol->bio) { |
| 609 | ret = pcol_try_alloc(pcol); |
| 610 | if (unlikely(ret)) |
| 611 | goto fail; |
| 612 | } |
| 613 | |
Boaz Harrosh | fe33cc1 | 2009-11-01 18:28:14 +0200 | [diff] [blame] | 614 | EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 615 | inode->i_ino, page->index, len); |
| 616 | |
| 617 | ret = pcol_add_page(pcol, page, len); |
| 618 | if (unlikely(ret)) { |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 619 | EXOFS_DBGMSG2("Failed pcol_add_page " |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 620 | "nr_pages=%u total_length=0x%lx\n", |
| 621 | pcol->nr_pages, pcol->length); |
| 622 | |
| 623 | /* split the request, next loop will start again */ |
| 624 | ret = write_exec(pcol); |
| 625 | if (unlikely(ret)) { |
| 626 | EXOFS_DBGMSG("write_exec faild => %d", ret); |
| 627 | goto fail; |
| 628 | } |
| 629 | |
| 630 | goto try_again; |
| 631 | } |
| 632 | |
| 633 | BUG_ON(PageWriteback(page)); |
| 634 | set_page_writeback(page); |
| 635 | |
| 636 | return 0; |
| 637 | |
| 638 | fail: |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 639 | EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n", |
| 640 | inode->i_ino, page->index, ret); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 641 | set_bit(AS_EIO, &page->mapping->flags); |
| 642 | unlock_page(page); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static int exofs_writepages(struct address_space *mapping, |
| 647 | struct writeback_control *wbc) |
| 648 | { |
| 649 | struct page_collect pcol; |
| 650 | long start, end, expected_pages; |
| 651 | int ret; |
| 652 | |
| 653 | start = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 654 | end = (wbc->range_end == LLONG_MAX) ? |
| 655 | start + mapping->nrpages : |
| 656 | wbc->range_end >> PAGE_CACHE_SHIFT; |
| 657 | |
| 658 | if (start || end) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 659 | expected_pages = end - start + 1; |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 660 | else |
| 661 | expected_pages = mapping->nrpages; |
| 662 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 663 | if (expected_pages < 32L) |
| 664 | expected_pages = 32L; |
| 665 | |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 666 | EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx " |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 667 | "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n", |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 668 | mapping->host->i_ino, wbc->range_start, wbc->range_end, |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 669 | mapping->nrpages, start, end, expected_pages); |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 670 | |
| 671 | _pcol_init(&pcol, expected_pages, mapping->host); |
| 672 | |
| 673 | ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol); |
| 674 | if (ret) { |
| 675 | EXOFS_ERR("write_cache_pages => %d\n", ret); |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | return write_exec(&pcol); |
| 680 | } |
| 681 | |
| 682 | static int exofs_writepage(struct page *page, struct writeback_control *wbc) |
| 683 | { |
| 684 | struct page_collect pcol; |
| 685 | int ret; |
| 686 | |
| 687 | _pcol_init(&pcol, 1, page->mapping->host); |
| 688 | |
| 689 | ret = writepage_strip(page, NULL, &pcol); |
| 690 | if (ret) { |
| 691 | EXOFS_ERR("exofs_writepage => %d\n", ret); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | return write_exec(&pcol); |
| 696 | } |
| 697 | |
| 698 | int exofs_write_begin(struct file *file, struct address_space *mapping, |
| 699 | loff_t pos, unsigned len, unsigned flags, |
| 700 | struct page **pagep, void **fsdata) |
| 701 | { |
| 702 | int ret = 0; |
| 703 | struct page *page; |
| 704 | |
| 705 | page = *pagep; |
| 706 | if (page == NULL) { |
| 707 | ret = simple_write_begin(file, mapping, pos, len, flags, pagep, |
| 708 | fsdata); |
| 709 | if (ret) { |
| 710 | EXOFS_DBGMSG("simple_write_begin faild\n"); |
| 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | page = *pagep; |
| 715 | } |
| 716 | |
| 717 | /* read modify write */ |
| 718 | if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) { |
| 719 | ret = _readpage(page, true); |
| 720 | if (ret) { |
| 721 | /*SetPageError was done by _readpage. Is it ok?*/ |
| 722 | unlock_page(page); |
| 723 | EXOFS_DBGMSG("__readpage_filler faild\n"); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | static int exofs_write_begin_export(struct file *file, |
| 731 | struct address_space *mapping, |
| 732 | loff_t pos, unsigned len, unsigned flags, |
| 733 | struct page **pagep, void **fsdata) |
| 734 | { |
| 735 | *pagep = NULL; |
| 736 | |
| 737 | return exofs_write_begin(file, mapping, pos, len, flags, pagep, |
| 738 | fsdata); |
| 739 | } |
| 740 | |
Boaz Harrosh | efd124b | 2009-12-27 17:01:42 +0200 | [diff] [blame] | 741 | static int exofs_write_end(struct file *file, struct address_space *mapping, |
| 742 | loff_t pos, unsigned len, unsigned copied, |
| 743 | struct page *page, void *fsdata) |
| 744 | { |
| 745 | struct inode *inode = mapping->host; |
| 746 | /* According to comment in simple_write_end i_mutex is held */ |
| 747 | loff_t i_size = inode->i_size; |
| 748 | int ret; |
| 749 | |
| 750 | ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata); |
| 751 | if (i_size != inode->i_size) |
| 752 | mark_inode_dirty(inode); |
| 753 | return ret; |
| 754 | } |
| 755 | |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 756 | const struct address_space_operations exofs_aops = { |
| 757 | .readpage = exofs_readpage, |
| 758 | .readpages = exofs_readpages, |
| 759 | .writepage = exofs_writepage, |
| 760 | .writepages = exofs_writepages, |
| 761 | .write_begin = exofs_write_begin_export, |
Boaz Harrosh | efd124b | 2009-12-27 17:01:42 +0200 | [diff] [blame] | 762 | .write_end = exofs_write_end, |
Boaz Harrosh | beaec07 | 2008-10-27 19:31:34 +0200 | [diff] [blame] | 763 | }; |
| 764 | |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 765 | /****************************************************************************** |
| 766 | * INODE OPERATIONS |
| 767 | *****************************************************************************/ |
| 768 | |
| 769 | /* |
| 770 | * Test whether an inode is a fast symlink. |
| 771 | */ |
| 772 | static inline int exofs_inode_is_fast_symlink(struct inode *inode) |
| 773 | { |
| 774 | struct exofs_i_info *oi = exofs_i(inode); |
| 775 | |
| 776 | return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0); |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * get_block_t - Fill in a buffer_head |
| 781 | * An OSD takes care of block allocation so we just fake an allocation by |
| 782 | * putting in the inode's sector_t in the buffer_head. |
| 783 | * TODO: What about the case of create==0 and @iblock does not exist in the |
| 784 | * object? |
| 785 | */ |
| 786 | static int exofs_get_block(struct inode *inode, sector_t iblock, |
| 787 | struct buffer_head *bh_result, int create) |
| 788 | { |
| 789 | map_bh(bh_result, inode->i_sb, iblock); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | const struct osd_attr g_attr_logical_length = ATTR_DEF( |
| 794 | OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8); |
| 795 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 796 | static int _do_truncate(struct inode *inode) |
| 797 | { |
| 798 | struct exofs_i_info *oi = exofs_i(inode); |
| 799 | loff_t isize = i_size_read(inode); |
| 800 | int ret; |
| 801 | |
| 802 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 803 | |
| 804 | nobh_truncate_page(inode->i_mapping, isize, exofs_get_block); |
| 805 | |
| 806 | ret = exofs_oi_truncate(oi, (u64)isize); |
| 807 | EXOFS_DBGMSG("(0x%lx) size=0x%llx\n", inode->i_ino, isize); |
| 808 | return ret; |
| 809 | } |
| 810 | |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 811 | /* |
| 812 | * Truncate a file to the specified size - all we have to do is set the size |
| 813 | * attribute. We make sure the object exists first. |
| 814 | */ |
| 815 | void exofs_truncate(struct inode *inode) |
| 816 | { |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 817 | struct exofs_i_info *oi = exofs_i(inode); |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 818 | int ret; |
| 819 | |
| 820 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) |
| 821 | || S_ISLNK(inode->i_mode))) |
| 822 | return; |
| 823 | if (exofs_inode_is_fast_symlink(inode)) |
| 824 | return; |
| 825 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 826 | return; |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 827 | |
| 828 | /* if we are about to truncate an object, and it hasn't been |
| 829 | * created yet, wait |
| 830 | */ |
| 831 | if (unlikely(wait_obj_created(oi))) |
| 832 | goto fail; |
| 833 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 834 | ret = _do_truncate(inode); |
Boaz Harrosh | e806271 | 2008-10-27 18:37:02 +0200 | [diff] [blame] | 835 | if (ret) |
| 836 | goto fail; |
| 837 | |
| 838 | out: |
| 839 | mark_inode_dirty(inode); |
| 840 | return; |
| 841 | fail: |
| 842 | make_bad_inode(inode); |
| 843 | goto out; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Set inode attributes - just call generic functions. |
| 848 | */ |
| 849 | int exofs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 850 | { |
| 851 | struct inode *inode = dentry->d_inode; |
| 852 | int error; |
| 853 | |
| 854 | error = inode_change_ok(inode, iattr); |
| 855 | if (error) |
| 856 | return error; |
| 857 | |
| 858 | error = inode_setattr(inode, iattr); |
| 859 | return error; |
| 860 | } |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 861 | |
| 862 | /* |
| 863 | * Read an inode from the OSD, and return it as is. We also return the size |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 864 | * attribute in the 'obj_size' argument. |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 865 | */ |
| 866 | static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi, |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 867 | struct exofs_fcb *inode, uint64_t *obj_size) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 868 | { |
| 869 | struct exofs_sb_info *sbi = sb->s_fs_info; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 870 | struct osd_attr attrs[2]; |
| 871 | struct exofs_io_state *ios; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 872 | int ret; |
| 873 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 874 | *obj_size = ~0; |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 875 | ret = exofs_get_io_state(&sbi->layout, &ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 876 | if (unlikely(ret)) { |
| 877 | EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__); |
| 878 | return ret; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 879 | } |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 880 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 881 | ios->obj.id = exofs_oi_objno(oi); |
| 882 | exofs_make_credential(oi->i_cred, &ios->obj); |
| 883 | ios->cred = oi->i_cred; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 884 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 885 | attrs[0] = g_attr_inode_data; |
| 886 | attrs[1] = g_attr_logical_length; |
| 887 | ios->in_attr = attrs; |
| 888 | ios->in_attr_len = ARRAY_SIZE(attrs); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 889 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 890 | ret = exofs_sbi_read(ios); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 891 | if (ret) |
| 892 | goto out; |
| 893 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 894 | ret = extract_attr_from_ios(ios, &attrs[0]); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 895 | if (ret) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 896 | EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 897 | goto out; |
| 898 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 899 | WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE); |
| 900 | memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 901 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 902 | ret = extract_attr_from_ios(ios, &attrs[1]); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 903 | if (ret) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 904 | EXOFS_ERR("%s: extract_attr of logical_length failed\n", |
| 905 | __func__); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 906 | goto out; |
| 907 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 908 | *obj_size = get_unaligned_be64(attrs[1].val_ptr); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 909 | |
| 910 | out: |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 911 | exofs_put_io_state(ios); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 912 | return ret; |
| 913 | } |
| 914 | |
Boaz Harrosh | 9cfdc7a | 2009-08-04 20:40:29 +0300 | [diff] [blame] | 915 | static void __oi_init(struct exofs_i_info *oi) |
| 916 | { |
| 917 | init_waitqueue_head(&oi->i_wq); |
| 918 | oi->i_flags = 0; |
| 919 | } |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 920 | /* |
| 921 | * Fill in an inode read from the OSD and set it up for use |
| 922 | */ |
| 923 | struct inode *exofs_iget(struct super_block *sb, unsigned long ino) |
| 924 | { |
| 925 | struct exofs_i_info *oi; |
| 926 | struct exofs_fcb fcb; |
| 927 | struct inode *inode; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 928 | uint64_t obj_size; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 929 | int ret; |
| 930 | |
| 931 | inode = iget_locked(sb, ino); |
| 932 | if (!inode) |
| 933 | return ERR_PTR(-ENOMEM); |
| 934 | if (!(inode->i_state & I_NEW)) |
| 935 | return inode; |
| 936 | oi = exofs_i(inode); |
Boaz Harrosh | 9cfdc7a | 2009-08-04 20:40:29 +0300 | [diff] [blame] | 937 | __oi_init(oi); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 938 | |
| 939 | /* read the inode from the osd */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 940 | ret = exofs_get_inode(sb, oi, &fcb, &obj_size); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 941 | if (ret) |
| 942 | goto bad_inode; |
| 943 | |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 944 | set_obj_created(oi); |
| 945 | |
| 946 | /* copy stuff from on-disk struct to in-memory struct */ |
| 947 | inode->i_mode = le16_to_cpu(fcb.i_mode); |
| 948 | inode->i_uid = le32_to_cpu(fcb.i_uid); |
| 949 | inode->i_gid = le32_to_cpu(fcb.i_gid); |
| 950 | inode->i_nlink = le16_to_cpu(fcb.i_links_count); |
| 951 | inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime); |
| 952 | inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime); |
| 953 | inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime); |
| 954 | inode->i_ctime.tv_nsec = |
| 955 | inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0; |
| 956 | oi->i_commit_size = le64_to_cpu(fcb.i_size); |
| 957 | i_size_write(inode, oi->i_commit_size); |
| 958 | inode->i_blkbits = EXOFS_BLKSHIFT; |
| 959 | inode->i_generation = le32_to_cpu(fcb.i_generation); |
| 960 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 961 | if ((inode->i_size != obj_size) && |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 962 | (!exofs_inode_is_fast_symlink(inode))) { |
Boaz Harrosh | 58311c43 | 2009-07-14 11:06:08 +0300 | [diff] [blame] | 963 | EXOFS_ERR("WARNING: Size of inode=%llu != object=%llu\n", |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 964 | inode->i_size, _LLU(obj_size)); |
| 965 | /* FIXME: call exofs_inode_recovery() */ |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 966 | } |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 967 | |
| 968 | oi->i_dir_start_lookup = 0; |
| 969 | |
| 970 | if ((inode->i_nlink == 0) && (inode->i_mode == 0)) { |
| 971 | ret = -ESTALE; |
| 972 | goto bad_inode; |
| 973 | } |
| 974 | |
| 975 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 976 | if (fcb.i_data[0]) |
| 977 | inode->i_rdev = |
| 978 | old_decode_dev(le32_to_cpu(fcb.i_data[0])); |
| 979 | else |
| 980 | inode->i_rdev = |
| 981 | new_decode_dev(le32_to_cpu(fcb.i_data[1])); |
| 982 | } else { |
| 983 | memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data)); |
| 984 | } |
| 985 | |
| 986 | if (S_ISREG(inode->i_mode)) { |
| 987 | inode->i_op = &exofs_file_inode_operations; |
| 988 | inode->i_fop = &exofs_file_operations; |
| 989 | inode->i_mapping->a_ops = &exofs_aops; |
| 990 | } else if (S_ISDIR(inode->i_mode)) { |
| 991 | inode->i_op = &exofs_dir_inode_operations; |
| 992 | inode->i_fop = &exofs_dir_operations; |
| 993 | inode->i_mapping->a_ops = &exofs_aops; |
| 994 | } else if (S_ISLNK(inode->i_mode)) { |
| 995 | if (exofs_inode_is_fast_symlink(inode)) |
| 996 | inode->i_op = &exofs_fast_symlink_inode_operations; |
| 997 | else { |
| 998 | inode->i_op = &exofs_symlink_inode_operations; |
| 999 | inode->i_mapping->a_ops = &exofs_aops; |
| 1000 | } |
| 1001 | } else { |
| 1002 | inode->i_op = &exofs_special_inode_operations; |
| 1003 | if (fcb.i_data[0]) |
| 1004 | init_special_inode(inode, inode->i_mode, |
| 1005 | old_decode_dev(le32_to_cpu(fcb.i_data[0]))); |
| 1006 | else |
| 1007 | init_special_inode(inode, inode->i_mode, |
| 1008 | new_decode_dev(le32_to_cpu(fcb.i_data[1]))); |
| 1009 | } |
| 1010 | |
| 1011 | unlock_new_inode(inode); |
| 1012 | return inode; |
| 1013 | |
| 1014 | bad_inode: |
| 1015 | iget_failed(inode); |
| 1016 | return ERR_PTR(ret); |
| 1017 | } |
| 1018 | |
| 1019 | int __exofs_wait_obj_created(struct exofs_i_info *oi) |
| 1020 | { |
| 1021 | if (!obj_created(oi)) { |
| 1022 | BUG_ON(!obj_2bcreated(oi)); |
| 1023 | wait_event(oi->i_wq, obj_created(oi)); |
| 1024 | } |
| 1025 | return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0; |
| 1026 | } |
| 1027 | /* |
| 1028 | * Callback function from exofs_new_inode(). The important thing is that we |
| 1029 | * set the obj_created flag so that other methods know that the object exists on |
| 1030 | * the OSD. |
| 1031 | */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1032 | static void create_done(struct exofs_io_state *ios, void *p) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1033 | { |
| 1034 | struct inode *inode = p; |
| 1035 | struct exofs_i_info *oi = exofs_i(inode); |
| 1036 | struct exofs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 1037 | int ret; |
| 1038 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1039 | ret = exofs_check_io(ios, NULL); |
| 1040 | exofs_put_io_state(ios); |
| 1041 | |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1042 | atomic_dec(&sbi->s_curr_pending); |
| 1043 | |
| 1044 | if (unlikely(ret)) { |
| 1045 | EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx", |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 1046 | _LLU(exofs_oi_objno(oi)), _LLU(sbi->layout.s_pid)); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1047 | /*TODO: When FS is corrupted creation can fail, object already |
| 1048 | * exist. Get rid of this asynchronous creation, if exist |
| 1049 | * increment the obj counter and try the next object. Until we |
| 1050 | * succeed. All these dangling objects will be made into lost |
| 1051 | * files by chkfs.exofs |
| 1052 | */ |
| 1053 | } |
| 1054 | |
| 1055 | set_obj_created(oi); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1056 | |
| 1057 | atomic_dec(&inode->i_count); |
| 1058 | wake_up(&oi->i_wq); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Set up a new inode and create an object for it on the OSD |
| 1063 | */ |
| 1064 | struct inode *exofs_new_inode(struct inode *dir, int mode) |
| 1065 | { |
| 1066 | struct super_block *sb; |
| 1067 | struct inode *inode; |
| 1068 | struct exofs_i_info *oi; |
| 1069 | struct exofs_sb_info *sbi; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1070 | struct exofs_io_state *ios; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1071 | int ret; |
| 1072 | |
| 1073 | sb = dir->i_sb; |
| 1074 | inode = new_inode(sb); |
| 1075 | if (!inode) |
| 1076 | return ERR_PTR(-ENOMEM); |
| 1077 | |
| 1078 | oi = exofs_i(inode); |
Boaz Harrosh | 9cfdc7a | 2009-08-04 20:40:29 +0300 | [diff] [blame] | 1079 | __oi_init(oi); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1080 | |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1081 | set_obj_2bcreated(oi); |
| 1082 | |
| 1083 | sbi = sb->s_fs_info; |
| 1084 | |
| 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; |
| 1093 | } |
| 1094 | inode->i_mode = mode; |
| 1095 | |
| 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); |
| 1104 | |
| 1105 | mark_inode_dirty(inode); |
| 1106 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 1107 | ret = exofs_get_io_state(&sbi->layout, &ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1108 | if (unlikely(ret)) { |
| 1109 | EXOFS_ERR("exofs_new_inode: exofs_get_io_state failed\n"); |
| 1110 | return ERR_PTR(ret); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1113 | ios->obj.id = exofs_oi_objno(oi); |
| 1114 | exofs_make_credential(oi->i_cred, &ios->obj); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* increment the refcount so that the inode will still be around when we |
| 1117 | * reach the callback |
| 1118 | */ |
| 1119 | atomic_inc(&inode->i_count); |
| 1120 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1121 | ios->done = create_done; |
| 1122 | ios->private = inode; |
| 1123 | ios->cred = oi->i_cred; |
| 1124 | ret = exofs_sbi_create(ios); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1125 | if (ret) { |
| 1126 | atomic_dec(&inode->i_count); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1127 | exofs_put_io_state(ios); |
| 1128 | return ERR_PTR(ret); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1129 | } |
| 1130 | atomic_inc(&sbi->s_curr_pending); |
| 1131 | |
| 1132 | return inode; |
| 1133 | } |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * struct to pass two arguments to update_inode's callback |
| 1137 | */ |
| 1138 | struct updatei_args { |
| 1139 | struct exofs_sb_info *sbi; |
| 1140 | struct exofs_fcb fcb; |
| 1141 | }; |
| 1142 | |
| 1143 | /* |
| 1144 | * Callback function from exofs_update_inode(). |
| 1145 | */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1146 | static void updatei_done(struct exofs_io_state *ios, void *p) |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1147 | { |
| 1148 | struct updatei_args *args = p; |
| 1149 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1150 | exofs_put_io_state(ios); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1151 | |
| 1152 | atomic_dec(&args->sbi->s_curr_pending); |
| 1153 | |
| 1154 | kfree(args); |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 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. |
| 1160 | */ |
| 1161 | static int exofs_update_inode(struct inode *inode, int do_sync) |
| 1162 | { |
| 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; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1166 | struct exofs_io_state *ios; |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1167 | struct osd_attr attr; |
| 1168 | struct exofs_fcb *fcb; |
| 1169 | struct updatei_args *args; |
| 1170 | int ret; |
| 1171 | |
| 1172 | args = kzalloc(sizeof(*args), GFP_KERNEL); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 1173 | if (!args) { |
| 1174 | EXOFS_DBGMSG("Faild kzalloc of args\n"); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1175 | return -ENOMEM; |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 1176 | } |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1177 | |
| 1178 | fcb = &args->fcb; |
| 1179 | |
| 1180 | fcb->i_mode = cpu_to_le16(inode->i_mode); |
| 1181 | fcb->i_uid = cpu_to_le32(inode->i_uid); |
| 1182 | fcb->i_gid = cpu_to_le32(inode->i_gid); |
| 1183 | fcb->i_links_count = cpu_to_le16(inode->i_nlink); |
| 1184 | fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); |
| 1185 | fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec); |
| 1186 | fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); |
| 1187 | oi->i_commit_size = i_size_read(inode); |
| 1188 | fcb->i_size = cpu_to_le64(oi->i_commit_size); |
| 1189 | fcb->i_generation = cpu_to_le32(inode->i_generation); |
| 1190 | |
| 1191 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 1192 | if (old_valid_dev(inode->i_rdev)) { |
| 1193 | fcb->i_data[0] = |
| 1194 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 1195 | fcb->i_data[1] = 0; |
| 1196 | } else { |
| 1197 | fcb->i_data[0] = 0; |
| 1198 | fcb->i_data[1] = |
| 1199 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 1200 | fcb->i_data[2] = 0; |
| 1201 | } |
| 1202 | } else |
| 1203 | memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data)); |
| 1204 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 1205 | ret = exofs_get_io_state(&sbi->layout, &ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1206 | if (unlikely(ret)) { |
| 1207 | EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1208 | goto free_args; |
| 1209 | } |
| 1210 | |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1211 | attr = g_attr_inode_data; |
| 1212 | attr.val_ptr = fcb; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1213 | ios->out_attr_len = 1; |
| 1214 | ios->out_attr = &attr; |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1215 | |
| 1216 | if (!obj_created(oi)) { |
| 1217 | EXOFS_DBGMSG("!obj_created\n"); |
| 1218 | BUG_ON(!obj_2bcreated(oi)); |
| 1219 | wait_event(oi->i_wq, obj_created(oi)); |
| 1220 | EXOFS_DBGMSG("wait_event done\n"); |
| 1221 | } |
| 1222 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1223 | if (!do_sync) { |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1224 | args->sbi = sbi; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1225 | ios->done = updatei_done; |
| 1226 | ios->private = args; |
| 1227 | } |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1228 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1229 | ret = exofs_oi_write(oi, ios); |
| 1230 | if (!do_sync && !ret) { |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1231 | atomic_inc(&sbi->s_curr_pending); |
| 1232 | goto out; /* deallocation in updatei_done */ |
| 1233 | } |
| 1234 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1235 | exofs_put_io_state(ios); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1236 | free_args: |
| 1237 | kfree(args); |
| 1238 | out: |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 1239 | EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n", |
| 1240 | inode->i_ino, do_sync, ret); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1241 | return ret; |
| 1242 | } |
| 1243 | |
| 1244 | int exofs_write_inode(struct inode *inode, int wait) |
| 1245 | { |
| 1246 | return exofs_update_inode(inode, wait); |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Callback function from exofs_delete_inode() - don't have much cleaning up to |
| 1251 | * do. |
| 1252 | */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1253 | static void delete_done(struct exofs_io_state *ios, void *p) |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1254 | { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1255 | struct exofs_sb_info *sbi = p; |
| 1256 | |
| 1257 | exofs_put_io_state(ios); |
| 1258 | |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1259 | atomic_dec(&sbi->s_curr_pending); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Called when the refcount of an inode reaches zero. We remove the object |
| 1264 | * from the OSD here. We make sure the object was created before we try and |
| 1265 | * delete it. |
| 1266 | */ |
| 1267 | void exofs_delete_inode(struct inode *inode) |
| 1268 | { |
| 1269 | struct exofs_i_info *oi = exofs_i(inode); |
| 1270 | struct super_block *sb = inode->i_sb; |
| 1271 | struct exofs_sb_info *sbi = sb->s_fs_info; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1272 | struct exofs_io_state *ios; |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1273 | int ret; |
| 1274 | |
| 1275 | truncate_inode_pages(&inode->i_data, 0); |
| 1276 | |
| 1277 | if (is_bad_inode(inode)) |
| 1278 | goto no_delete; |
| 1279 | |
| 1280 | mark_inode_dirty(inode); |
| 1281 | exofs_update_inode(inode, inode_needs_sync(inode)); |
| 1282 | |
| 1283 | inode->i_size = 0; |
| 1284 | if (inode->i_blocks) |
| 1285 | exofs_truncate(inode); |
| 1286 | |
| 1287 | clear_inode(inode); |
| 1288 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame^] | 1289 | ret = exofs_get_io_state(&sbi->layout, &ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1290 | if (unlikely(ret)) { |
| 1291 | EXOFS_ERR("%s: exofs_get_io_state failed\n", __func__); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1295 | /* if we are deleting an obj that hasn't been created yet, wait */ |
| 1296 | if (!obj_created(oi)) { |
| 1297 | BUG_ON(!obj_2bcreated(oi)); |
| 1298 | wait_event(oi->i_wq, obj_created(oi)); |
| 1299 | } |
| 1300 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1301 | ios->obj.id = exofs_oi_objno(oi); |
| 1302 | ios->done = delete_done; |
| 1303 | ios->private = sbi; |
| 1304 | ios->cred = oi->i_cred; |
| 1305 | ret = exofs_sbi_remove(ios); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1306 | if (ret) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1307 | EXOFS_ERR("%s: exofs_sbi_remove failed\n", __func__); |
| 1308 | exofs_put_io_state(ios); |
Boaz Harrosh | ba9e5e9 | 2008-10-28 16:11:41 +0200 | [diff] [blame] | 1309 | return; |
| 1310 | } |
| 1311 | atomic_inc(&sbi->s_curr_pending); |
| 1312 | |
| 1313 | return; |
| 1314 | |
| 1315 | no_delete: |
| 1316 | clear_inode(inode); |
| 1317 | } |