Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of UBIFS. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: Artem Bityutskiy (Битюцкий Артём) |
| 20 | * Adrian Hunter |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This file implements VFS file and inode operations of regular files, device |
| 25 | * nodes and symlinks as well as address space operations. |
| 26 | * |
| 27 | * UBIFS uses 2 page flags: PG_private and PG_checked. PG_private is set if the |
| 28 | * page is dirty and is used for budgeting purposes - dirty pages should not be |
| 29 | * budgeted. The PG_checked flag is set if full budgeting is required for the |
| 30 | * page e.g., when it corresponds to a file hole or it is just beyond the file |
| 31 | * size. The budgeting is done in 'ubifs_write_begin()', because it is OK to |
| 32 | * fail in this function, and the budget is released in 'ubifs_write_end()'. So |
| 33 | * the PG_private and PG_checked flags carry the information about how the page |
| 34 | * was budgeted, to make it possible to release the budget properly. |
| 35 | * |
| 36 | * A thing to keep in mind: inode's 'i_mutex' is locked in most VFS operations |
| 37 | * we implement. However, this is not true for '->writepage()', which might be |
| 38 | * called with 'i_mutex' unlocked. For example, when pdflush is performing |
| 39 | * write-back, it calls 'writepage()' with unlocked 'i_mutex', although the |
| 40 | * inode has 'I_LOCK' flag in this case. At "normal" work-paths 'i_mutex' is |
| 41 | * locked in '->writepage', e.g. in "sys_write -> alloc_pages -> direct reclaim |
| 42 | * path'. So, in '->writepage()' we are only guaranteed that the page is |
| 43 | * locked. |
| 44 | * |
| 45 | * Similarly, 'i_mutex' does not have to be locked in readpage(), e.g., |
| 46 | * readahead path does not have it locked ("sys_read -> generic_file_aio_read |
| 47 | * -> ondemand_readahead -> readpage"). In case of readahead, 'I_LOCK' flag is |
| 48 | * not set as well. However, UBIFS disables readahead. |
| 49 | * |
| 50 | * This, for example means that there might be 2 concurrent '->writepage()' |
| 51 | * calls for the same inode, but different inode dirty pages. |
| 52 | */ |
| 53 | |
| 54 | #include "ubifs.h" |
| 55 | #include <linux/mount.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 56 | #include <linux/namei.h> |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 57 | |
| 58 | static int read_block(struct inode *inode, void *addr, unsigned int block, |
| 59 | struct ubifs_data_node *dn) |
| 60 | { |
| 61 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 62 | int err, len, out_len; |
| 63 | union ubifs_key key; |
| 64 | unsigned int dlen; |
| 65 | |
| 66 | data_key_init(c, &key, inode->i_ino, block); |
| 67 | err = ubifs_tnc_lookup(c, &key, dn); |
| 68 | if (err) { |
| 69 | if (err == -ENOENT) |
| 70 | /* Not found, so it must be a hole */ |
| 71 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
| 72 | return err; |
| 73 | } |
| 74 | |
Harvey Harrison | 0ecb952 | 2008-10-24 10:52:57 -0700 | [diff] [blame] | 75 | ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 76 | |
| 77 | len = le32_to_cpu(dn->size); |
| 78 | if (len <= 0 || len > UBIFS_BLOCK_SIZE) |
| 79 | goto dump; |
| 80 | |
| 81 | dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; |
| 82 | out_len = UBIFS_BLOCK_SIZE; |
| 83 | err = ubifs_decompress(&dn->data, dlen, addr, &out_len, |
| 84 | le16_to_cpu(dn->compr_type)); |
| 85 | if (err || len != out_len) |
| 86 | goto dump; |
| 87 | |
| 88 | /* |
| 89 | * Data length can be less than a full block, even for blocks that are |
| 90 | * not the last in the file (e.g., as a result of making a hole and |
| 91 | * appending data). Ensure that the remainder is zeroed out. |
| 92 | */ |
| 93 | if (len < UBIFS_BLOCK_SIZE) |
| 94 | memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); |
| 95 | |
| 96 | return 0; |
| 97 | |
| 98 | dump: |
| 99 | ubifs_err("bad data node (block %u, inode %lu)", |
| 100 | block, inode->i_ino); |
| 101 | dbg_dump_node(c, dn); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | static int do_readpage(struct page *page) |
| 106 | { |
| 107 | void *addr; |
| 108 | int err = 0, i; |
| 109 | unsigned int block, beyond; |
| 110 | struct ubifs_data_node *dn; |
| 111 | struct inode *inode = page->mapping->host; |
| 112 | loff_t i_size = i_size_read(inode); |
| 113 | |
| 114 | dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx", |
| 115 | inode->i_ino, page->index, i_size, page->flags); |
| 116 | ubifs_assert(!PageChecked(page)); |
| 117 | ubifs_assert(!PagePrivate(page)); |
| 118 | |
| 119 | addr = kmap(page); |
| 120 | |
| 121 | block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; |
| 122 | beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; |
| 123 | if (block >= beyond) { |
| 124 | /* Reading beyond inode */ |
| 125 | SetPageChecked(page); |
| 126 | memset(addr, 0, PAGE_CACHE_SIZE); |
| 127 | goto out; |
| 128 | } |
| 129 | |
| 130 | dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS); |
| 131 | if (!dn) { |
| 132 | err = -ENOMEM; |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | i = 0; |
| 137 | while (1) { |
| 138 | int ret; |
| 139 | |
| 140 | if (block >= beyond) { |
| 141 | /* Reading beyond inode */ |
| 142 | err = -ENOENT; |
| 143 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
| 144 | } else { |
| 145 | ret = read_block(inode, addr, block, dn); |
| 146 | if (ret) { |
| 147 | err = ret; |
| 148 | if (err != -ENOENT) |
| 149 | break; |
Adrian Hunter | ed382d5 | 2008-09-05 16:17:42 +0300 | [diff] [blame] | 150 | } else if (block + 1 == beyond) { |
| 151 | int dlen = le32_to_cpu(dn->size); |
| 152 | int ilen = i_size & (UBIFS_BLOCK_SIZE - 1); |
| 153 | |
| 154 | if (ilen && ilen < dlen) |
| 155 | memset(addr + ilen, 0, dlen - ilen); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | if (++i >= UBIFS_BLOCKS_PER_PAGE) |
| 159 | break; |
| 160 | block += 1; |
| 161 | addr += UBIFS_BLOCK_SIZE; |
| 162 | } |
| 163 | if (err) { |
| 164 | if (err == -ENOENT) { |
| 165 | /* Not found, so it must be a hole */ |
| 166 | SetPageChecked(page); |
| 167 | dbg_gen("hole"); |
| 168 | goto out_free; |
| 169 | } |
| 170 | ubifs_err("cannot read page %lu of inode %lu, error %d", |
| 171 | page->index, inode->i_ino, err); |
| 172 | goto error; |
| 173 | } |
| 174 | |
| 175 | out_free: |
| 176 | kfree(dn); |
| 177 | out: |
| 178 | SetPageUptodate(page); |
| 179 | ClearPageError(page); |
| 180 | flush_dcache_page(page); |
| 181 | kunmap(page); |
| 182 | return 0; |
| 183 | |
| 184 | error: |
| 185 | kfree(dn); |
| 186 | ClearPageUptodate(page); |
| 187 | SetPageError(page); |
| 188 | flush_dcache_page(page); |
| 189 | kunmap(page); |
| 190 | return err; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * release_new_page_budget - release budget of a new page. |
| 195 | * @c: UBIFS file-system description object |
| 196 | * |
| 197 | * This is a helper function which releases budget corresponding to the budget |
| 198 | * of one new page of data. |
| 199 | */ |
| 200 | static void release_new_page_budget(struct ubifs_info *c) |
| 201 | { |
| 202 | struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 }; |
| 203 | |
| 204 | ubifs_release_budget(c, &req); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * release_existing_page_budget - release budget of an existing page. |
| 209 | * @c: UBIFS file-system description object |
| 210 | * |
| 211 | * This is a helper function which releases budget corresponding to the budget |
| 212 | * of changing one one page of data which already exists on the flash media. |
| 213 | */ |
| 214 | static void release_existing_page_budget(struct ubifs_info *c) |
| 215 | { |
| 216 | struct ubifs_budget_req req = { .dd_growth = c->page_budget}; |
| 217 | |
| 218 | ubifs_release_budget(c, &req); |
| 219 | } |
| 220 | |
| 221 | static int write_begin_slow(struct address_space *mapping, |
| 222 | loff_t pos, unsigned len, struct page **pagep) |
| 223 | { |
| 224 | struct inode *inode = mapping->host; |
| 225 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 226 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 227 | struct ubifs_budget_req req = { .new_page = 1 }; |
| 228 | int uninitialized_var(err), appending = !!(pos + len > inode->i_size); |
| 229 | struct page *page; |
| 230 | |
| 231 | dbg_gen("ino %lu, pos %llu, len %u, i_size %lld", |
| 232 | inode->i_ino, pos, len, inode->i_size); |
| 233 | |
| 234 | /* |
| 235 | * At the slow path we have to budget before locking the page, because |
| 236 | * budgeting may force write-back, which would wait on locked pages and |
| 237 | * deadlock if we had the page locked. At this point we do not know |
| 238 | * anything about the page, so assume that this is a new page which is |
| 239 | * written to a hole. This corresponds to largest budget. Later the |
| 240 | * budget will be amended if this is not true. |
| 241 | */ |
| 242 | if (appending) |
| 243 | /* We are appending data, budget for inode change */ |
| 244 | req.dirtied_ino = 1; |
| 245 | |
| 246 | err = ubifs_budget_space(c, &req); |
| 247 | if (unlikely(err)) |
| 248 | return err; |
| 249 | |
| 250 | page = __grab_cache_page(mapping, index); |
| 251 | if (unlikely(!page)) { |
| 252 | ubifs_release_budget(c, &req); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | |
| 256 | if (!PageUptodate(page)) { |
| 257 | if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) |
| 258 | SetPageChecked(page); |
| 259 | else { |
| 260 | err = do_readpage(page); |
| 261 | if (err) { |
| 262 | unlock_page(page); |
| 263 | page_cache_release(page); |
| 264 | return err; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | SetPageUptodate(page); |
| 269 | ClearPageError(page); |
| 270 | } |
| 271 | |
| 272 | if (PagePrivate(page)) |
| 273 | /* |
| 274 | * The page is dirty, which means it was budgeted twice: |
| 275 | * o first time the budget was allocated by the task which |
| 276 | * made the page dirty and set the PG_private flag; |
| 277 | * o and then we budgeted for it for the second time at the |
| 278 | * very beginning of this function. |
| 279 | * |
| 280 | * So what we have to do is to release the page budget we |
| 281 | * allocated. |
| 282 | */ |
| 283 | release_new_page_budget(c); |
| 284 | else if (!PageChecked(page)) |
| 285 | /* |
| 286 | * We are changing a page which already exists on the media. |
| 287 | * This means that changing the page does not make the amount |
| 288 | * of indexing information larger, and this part of the budget |
| 289 | * which we have already acquired may be released. |
| 290 | */ |
| 291 | ubifs_convert_page_budget(c); |
| 292 | |
| 293 | if (appending) { |
| 294 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 295 | |
| 296 | /* |
| 297 | * 'ubifs_write_end()' is optimized from the fast-path part of |
| 298 | * 'ubifs_write_begin()' and expects the @ui_mutex to be locked |
| 299 | * if data is appended. |
| 300 | */ |
| 301 | mutex_lock(&ui->ui_mutex); |
| 302 | if (ui->dirty) |
| 303 | /* |
| 304 | * The inode is dirty already, so we may free the |
| 305 | * budget we allocated. |
| 306 | */ |
| 307 | ubifs_release_dirty_inode_budget(c, ui); |
| 308 | } |
| 309 | |
| 310 | *pagep = page; |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * allocate_budget - allocate budget for 'ubifs_write_begin()'. |
| 316 | * @c: UBIFS file-system description object |
| 317 | * @page: page to allocate budget for |
| 318 | * @ui: UBIFS inode object the page belongs to |
| 319 | * @appending: non-zero if the page is appended |
| 320 | * |
| 321 | * This is a helper function for 'ubifs_write_begin()' which allocates budget |
| 322 | * for the operation. The budget is allocated differently depending on whether |
| 323 | * this is appending, whether the page is dirty or not, and so on. This |
| 324 | * function leaves the @ui->ui_mutex locked in case of appending. Returns zero |
| 325 | * in case of success and %-ENOSPC in case of failure. |
| 326 | */ |
| 327 | static int allocate_budget(struct ubifs_info *c, struct page *page, |
| 328 | struct ubifs_inode *ui, int appending) |
| 329 | { |
| 330 | struct ubifs_budget_req req = { .fast = 1 }; |
| 331 | |
| 332 | if (PagePrivate(page)) { |
| 333 | if (!appending) |
| 334 | /* |
| 335 | * The page is dirty and we are not appending, which |
| 336 | * means no budget is needed at all. |
| 337 | */ |
| 338 | return 0; |
| 339 | |
| 340 | mutex_lock(&ui->ui_mutex); |
| 341 | if (ui->dirty) |
| 342 | /* |
| 343 | * The page is dirty and we are appending, so the inode |
| 344 | * has to be marked as dirty. However, it is already |
| 345 | * dirty, so we do not need any budget. We may return, |
| 346 | * but @ui->ui_mutex hast to be left locked because we |
| 347 | * should prevent write-back from flushing the inode |
| 348 | * and freeing the budget. The lock will be released in |
| 349 | * 'ubifs_write_end()'. |
| 350 | */ |
| 351 | return 0; |
| 352 | |
| 353 | /* |
| 354 | * The page is dirty, we are appending, the inode is clean, so |
| 355 | * we need to budget the inode change. |
| 356 | */ |
| 357 | req.dirtied_ino = 1; |
| 358 | } else { |
| 359 | if (PageChecked(page)) |
| 360 | /* |
| 361 | * The page corresponds to a hole and does not |
| 362 | * exist on the media. So changing it makes |
| 363 | * make the amount of indexing information |
| 364 | * larger, and we have to budget for a new |
| 365 | * page. |
| 366 | */ |
| 367 | req.new_page = 1; |
| 368 | else |
| 369 | /* |
| 370 | * Not a hole, the change will not add any new |
| 371 | * indexing information, budget for page |
| 372 | * change. |
| 373 | */ |
| 374 | req.dirtied_page = 1; |
| 375 | |
| 376 | if (appending) { |
| 377 | mutex_lock(&ui->ui_mutex); |
| 378 | if (!ui->dirty) |
| 379 | /* |
| 380 | * The inode is clean but we will have to mark |
| 381 | * it as dirty because we are appending. This |
| 382 | * needs a budget. |
| 383 | */ |
| 384 | req.dirtied_ino = 1; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return ubifs_budget_space(c, &req); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * This function is called when a page of data is going to be written. Since |
| 393 | * the page of data will not necessarily go to the flash straight away, UBIFS |
| 394 | * has to reserve space on the media for it, which is done by means of |
| 395 | * budgeting. |
| 396 | * |
| 397 | * This is the hot-path of the file-system and we are trying to optimize it as |
| 398 | * much as possible. For this reasons it is split on 2 parts - slow and fast. |
| 399 | * |
| 400 | * There many budgeting cases: |
| 401 | * o a new page is appended - we have to budget for a new page and for |
| 402 | * changing the inode; however, if the inode is already dirty, there is |
| 403 | * no need to budget for it; |
| 404 | * o an existing clean page is changed - we have budget for it; if the page |
| 405 | * does not exist on the media (a hole), we have to budget for a new |
| 406 | * page; otherwise, we may budget for changing an existing page; the |
| 407 | * difference between these cases is that changing an existing page does |
| 408 | * not introduce anything new to the FS indexing information, so it does |
| 409 | * not grow, and smaller budget is acquired in this case; |
| 410 | * o an existing dirty page is changed - no need to budget at all, because |
| 411 | * the page budget has been acquired by earlier, when the page has been |
| 412 | * marked dirty. |
| 413 | * |
| 414 | * UBIFS budgeting sub-system may force write-back if it thinks there is no |
| 415 | * space to reserve. This imposes some locking restrictions and makes it |
| 416 | * impossible to take into account the above cases, and makes it impossible to |
| 417 | * optimize budgeting. |
| 418 | * |
| 419 | * The solution for this is that the fast path of 'ubifs_write_begin()' assumes |
| 420 | * there is a plenty of flash space and the budget will be acquired quickly, |
| 421 | * without forcing write-back. The slow path does not make this assumption. |
| 422 | */ |
| 423 | static int ubifs_write_begin(struct file *file, struct address_space *mapping, |
| 424 | loff_t pos, unsigned len, unsigned flags, |
| 425 | struct page **pagep, void **fsdata) |
| 426 | { |
| 427 | struct inode *inode = mapping->host; |
| 428 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 429 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 430 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 431 | int uninitialized_var(err), appending = !!(pos + len > inode->i_size); |
| 432 | struct page *page; |
| 433 | |
| 434 | |
| 435 | ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size); |
| 436 | |
| 437 | if (unlikely(c->ro_media)) |
| 438 | return -EROFS; |
| 439 | |
| 440 | /* Try out the fast-path part first */ |
| 441 | page = __grab_cache_page(mapping, index); |
| 442 | if (unlikely(!page)) |
| 443 | return -ENOMEM; |
| 444 | |
| 445 | if (!PageUptodate(page)) { |
| 446 | /* The page is not loaded from the flash */ |
| 447 | if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) |
| 448 | /* |
| 449 | * We change whole page so no need to load it. But we |
| 450 | * have to set the @PG_checked flag to make the further |
| 451 | * code the page is new. This might be not true, but it |
| 452 | * is better to budget more that to read the page from |
| 453 | * the media. |
| 454 | */ |
| 455 | SetPageChecked(page); |
| 456 | else { |
| 457 | err = do_readpage(page); |
| 458 | if (err) { |
| 459 | unlock_page(page); |
| 460 | page_cache_release(page); |
| 461 | return err; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | SetPageUptodate(page); |
| 466 | ClearPageError(page); |
| 467 | } |
| 468 | |
| 469 | err = allocate_budget(c, page, ui, appending); |
| 470 | if (unlikely(err)) { |
| 471 | ubifs_assert(err == -ENOSPC); |
| 472 | /* |
| 473 | * Budgeting failed which means it would have to force |
| 474 | * write-back but didn't, because we set the @fast flag in the |
| 475 | * request. Write-back cannot be done now, while we have the |
| 476 | * page locked, because it would deadlock. Unlock and free |
| 477 | * everything and fall-back to slow-path. |
| 478 | */ |
| 479 | if (appending) { |
| 480 | ubifs_assert(mutex_is_locked(&ui->ui_mutex)); |
| 481 | mutex_unlock(&ui->ui_mutex); |
| 482 | } |
| 483 | unlock_page(page); |
| 484 | page_cache_release(page); |
| 485 | |
| 486 | return write_begin_slow(mapping, pos, len, pagep); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Whee, we aquired budgeting quickly - without involving |
| 491 | * garbage-collection, committing or forceing write-back. We return |
| 492 | * with @ui->ui_mutex locked if we are appending pages, and unlocked |
| 493 | * otherwise. This is an optimization (slightly hacky though). |
| 494 | */ |
| 495 | *pagep = page; |
| 496 | return 0; |
| 497 | |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * cancel_budget - cancel budget. |
| 502 | * @c: UBIFS file-system description object |
| 503 | * @page: page to cancel budget for |
| 504 | * @ui: UBIFS inode object the page belongs to |
| 505 | * @appending: non-zero if the page is appended |
| 506 | * |
| 507 | * This is a helper function for a page write operation. It unlocks the |
| 508 | * @ui->ui_mutex in case of appending. |
| 509 | */ |
| 510 | static void cancel_budget(struct ubifs_info *c, struct page *page, |
| 511 | struct ubifs_inode *ui, int appending) |
| 512 | { |
| 513 | if (appending) { |
| 514 | if (!ui->dirty) |
| 515 | ubifs_release_dirty_inode_budget(c, ui); |
| 516 | mutex_unlock(&ui->ui_mutex); |
| 517 | } |
| 518 | if (!PagePrivate(page)) { |
| 519 | if (PageChecked(page)) |
| 520 | release_new_page_budget(c); |
| 521 | else |
| 522 | release_existing_page_budget(c); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static int ubifs_write_end(struct file *file, struct address_space *mapping, |
| 527 | loff_t pos, unsigned len, unsigned copied, |
| 528 | struct page *page, void *fsdata) |
| 529 | { |
| 530 | struct inode *inode = mapping->host; |
| 531 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 532 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 533 | loff_t end_pos = pos + len; |
| 534 | int appending = !!(end_pos > inode->i_size); |
| 535 | |
| 536 | dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld", |
| 537 | inode->i_ino, pos, page->index, len, copied, inode->i_size); |
| 538 | |
| 539 | if (unlikely(copied < len && len == PAGE_CACHE_SIZE)) { |
| 540 | /* |
| 541 | * VFS copied less data to the page that it intended and |
| 542 | * declared in its '->write_begin()' call via the @len |
| 543 | * argument. If the page was not up-to-date, and @len was |
| 544 | * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did |
| 545 | * not load it from the media (for optimization reasons). This |
| 546 | * means that part of the page contains garbage. So read the |
| 547 | * page now. |
| 548 | */ |
| 549 | dbg_gen("copied %d instead of %d, read page and repeat", |
| 550 | copied, len); |
| 551 | cancel_budget(c, page, ui, appending); |
| 552 | |
| 553 | /* |
| 554 | * Return 0 to force VFS to repeat the whole operation, or the |
| 555 | * error code if 'do_readpage()' failes. |
| 556 | */ |
| 557 | copied = do_readpage(page); |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | if (!PagePrivate(page)) { |
| 562 | SetPagePrivate(page); |
| 563 | atomic_long_inc(&c->dirty_pg_cnt); |
| 564 | __set_page_dirty_nobuffers(page); |
| 565 | } |
| 566 | |
| 567 | if (appending) { |
| 568 | i_size_write(inode, end_pos); |
| 569 | ui->ui_size = end_pos; |
| 570 | /* |
| 571 | * Note, we do not set @I_DIRTY_PAGES (which means that the |
| 572 | * inode has dirty pages), this has been done in |
| 573 | * '__set_page_dirty_nobuffers()'. |
| 574 | */ |
| 575 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
| 576 | ubifs_assert(mutex_is_locked(&ui->ui_mutex)); |
| 577 | mutex_unlock(&ui->ui_mutex); |
| 578 | } |
| 579 | |
| 580 | out: |
| 581 | unlock_page(page); |
| 582 | page_cache_release(page); |
| 583 | return copied; |
| 584 | } |
| 585 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 586 | /** |
| 587 | * populate_page - copy data nodes into a page for bulk-read. |
| 588 | * @c: UBIFS file-system description object |
| 589 | * @page: page |
| 590 | * @bu: bulk-read information |
| 591 | * @n: next zbranch slot |
| 592 | * |
| 593 | * This function returns %0 on success and a negative error code on failure. |
| 594 | */ |
| 595 | static int populate_page(struct ubifs_info *c, struct page *page, |
| 596 | struct bu_info *bu, int *n) |
| 597 | { |
Adrian Hunter | 5c0013c | 2008-09-12 10:34:51 +0300 | [diff] [blame] | 598 | int i = 0, nn = *n, offs = bu->zbranch[0].offs, hole = 0, read = 0; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 599 | struct inode *inode = page->mapping->host; |
| 600 | loff_t i_size = i_size_read(inode); |
| 601 | unsigned int page_block; |
| 602 | void *addr, *zaddr; |
| 603 | pgoff_t end_index; |
| 604 | |
| 605 | dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx", |
| 606 | inode->i_ino, page->index, i_size, page->flags); |
| 607 | |
| 608 | addr = zaddr = kmap(page); |
| 609 | |
Adrian Hunter | ed382d5 | 2008-09-05 16:17:42 +0300 | [diff] [blame] | 610 | end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 611 | if (!i_size || page->index > end_index) { |
Adrian Hunter | 5c0013c | 2008-09-12 10:34:51 +0300 | [diff] [blame] | 612 | hole = 1; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 613 | memset(addr, 0, PAGE_CACHE_SIZE); |
| 614 | goto out_hole; |
| 615 | } |
| 616 | |
| 617 | page_block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; |
| 618 | while (1) { |
| 619 | int err, len, out_len, dlen; |
| 620 | |
Adrian Hunter | 5c0013c | 2008-09-12 10:34:51 +0300 | [diff] [blame] | 621 | if (nn >= bu->cnt) { |
| 622 | hole = 1; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 623 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
Adrian Hunter | 5c0013c | 2008-09-12 10:34:51 +0300 | [diff] [blame] | 624 | } else if (key_block(c, &bu->zbranch[nn].key) == page_block) { |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 625 | struct ubifs_data_node *dn; |
| 626 | |
| 627 | dn = bu->buf + (bu->zbranch[nn].offs - offs); |
| 628 | |
Harvey Harrison | 0ecb952 | 2008-10-24 10:52:57 -0700 | [diff] [blame] | 629 | ubifs_assert(le64_to_cpu(dn->ch.sqnum) > |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 630 | ubifs_inode(inode)->creat_sqnum); |
| 631 | |
| 632 | len = le32_to_cpu(dn->size); |
| 633 | if (len <= 0 || len > UBIFS_BLOCK_SIZE) |
| 634 | goto out_err; |
| 635 | |
| 636 | dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; |
| 637 | out_len = UBIFS_BLOCK_SIZE; |
| 638 | err = ubifs_decompress(&dn->data, dlen, addr, &out_len, |
| 639 | le16_to_cpu(dn->compr_type)); |
| 640 | if (err || len != out_len) |
| 641 | goto out_err; |
| 642 | |
| 643 | if (len < UBIFS_BLOCK_SIZE) |
| 644 | memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); |
| 645 | |
| 646 | nn += 1; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 647 | read = (i << UBIFS_BLOCK_SHIFT) + len; |
Adrian Hunter | 5c0013c | 2008-09-12 10:34:51 +0300 | [diff] [blame] | 648 | } else if (key_block(c, &bu->zbranch[nn].key) < page_block) { |
| 649 | nn += 1; |
| 650 | continue; |
| 651 | } else { |
| 652 | hole = 1; |
| 653 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 654 | } |
| 655 | if (++i >= UBIFS_BLOCKS_PER_PAGE) |
| 656 | break; |
| 657 | addr += UBIFS_BLOCK_SIZE; |
| 658 | page_block += 1; |
| 659 | } |
| 660 | |
| 661 | if (end_index == page->index) { |
| 662 | int len = i_size & (PAGE_CACHE_SIZE - 1); |
| 663 | |
Adrian Hunter | ed382d5 | 2008-09-05 16:17:42 +0300 | [diff] [blame] | 664 | if (len && len < read) |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 665 | memset(zaddr + len, 0, read - len); |
| 666 | } |
| 667 | |
| 668 | out_hole: |
| 669 | if (hole) { |
| 670 | SetPageChecked(page); |
| 671 | dbg_gen("hole"); |
| 672 | } |
| 673 | |
| 674 | SetPageUptodate(page); |
| 675 | ClearPageError(page); |
| 676 | flush_dcache_page(page); |
| 677 | kunmap(page); |
| 678 | *n = nn; |
| 679 | return 0; |
| 680 | |
| 681 | out_err: |
| 682 | ClearPageUptodate(page); |
| 683 | SetPageError(page); |
| 684 | flush_dcache_page(page); |
| 685 | kunmap(page); |
| 686 | ubifs_err("bad data node (block %u, inode %lu)", |
| 687 | page_block, inode->i_ino); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * ubifs_do_bulk_read - do bulk-read. |
| 693 | * @c: UBIFS file-system description object |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 694 | * @bu: bulk-read information |
| 695 | * @page1: first page to read |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 696 | * |
| 697 | * This function returns %1 if the bulk-read is done, otherwise %0 is returned. |
| 698 | */ |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 699 | static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu, |
| 700 | struct page *page1) |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 701 | { |
| 702 | pgoff_t offset = page1->index, end_index; |
| 703 | struct address_space *mapping = page1->mapping; |
| 704 | struct inode *inode = mapping->host; |
| 705 | struct ubifs_inode *ui = ubifs_inode(inode); |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 706 | int err, page_idx, page_cnt, ret = 0, n = 0; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 707 | int allocate = bu->buf ? 0 : 1; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 708 | loff_t isize; |
| 709 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 710 | err = ubifs_tnc_get_bu_keys(c, bu); |
| 711 | if (err) |
| 712 | goto out_warn; |
| 713 | |
| 714 | if (bu->eof) { |
| 715 | /* Turn off bulk-read at the end of the file */ |
| 716 | ui->read_in_a_row = 1; |
| 717 | ui->bulk_read = 0; |
| 718 | } |
| 719 | |
| 720 | page_cnt = bu->blk_cnt >> UBIFS_BLOCKS_PER_PAGE_SHIFT; |
| 721 | if (!page_cnt) { |
| 722 | /* |
| 723 | * This happens when there are multiple blocks per page and the |
| 724 | * blocks for the first page we are looking for, are not |
| 725 | * together. If all the pages were like this, bulk-read would |
| 726 | * reduce performance, so we turn it off for a while. |
| 727 | */ |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 728 | goto out_bu_off; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | if (bu->cnt) { |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 732 | if (allocate) { |
| 733 | /* |
| 734 | * Allocate bulk-read buffer depending on how many data |
| 735 | * nodes we are going to read. |
| 736 | */ |
| 737 | bu->buf_len = bu->zbranch[bu->cnt - 1].offs + |
| 738 | bu->zbranch[bu->cnt - 1].len - |
| 739 | bu->zbranch[0].offs; |
| 740 | ubifs_assert(bu->buf_len > 0); |
| 741 | ubifs_assert(bu->buf_len <= c->leb_size); |
| 742 | bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN); |
| 743 | if (!bu->buf) |
| 744 | goto out_bu_off; |
| 745 | } |
| 746 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 747 | err = ubifs_tnc_bulk_read(c, bu); |
| 748 | if (err) |
| 749 | goto out_warn; |
| 750 | } |
| 751 | |
| 752 | err = populate_page(c, page1, bu, &n); |
| 753 | if (err) |
| 754 | goto out_warn; |
| 755 | |
| 756 | unlock_page(page1); |
| 757 | ret = 1; |
| 758 | |
| 759 | isize = i_size_read(inode); |
| 760 | if (isize == 0) |
| 761 | goto out_free; |
| 762 | end_index = ((isize - 1) >> PAGE_CACHE_SHIFT); |
| 763 | |
| 764 | for (page_idx = 1; page_idx < page_cnt; page_idx++) { |
| 765 | pgoff_t page_offset = offset + page_idx; |
| 766 | struct page *page; |
| 767 | |
| 768 | if (page_offset > end_index) |
| 769 | break; |
| 770 | page = find_or_create_page(mapping, page_offset, |
| 771 | GFP_NOFS | __GFP_COLD); |
| 772 | if (!page) |
| 773 | break; |
| 774 | if (!PageUptodate(page)) |
| 775 | err = populate_page(c, page, bu, &n); |
| 776 | unlock_page(page); |
| 777 | page_cache_release(page); |
| 778 | if (err) |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | ui->last_page_read = offset + page_idx - 1; |
| 783 | |
| 784 | out_free: |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 785 | if (allocate) |
| 786 | kfree(bu->buf); |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 787 | return ret; |
| 788 | |
| 789 | out_warn: |
| 790 | ubifs_warn("ignoring error %d and skipping bulk-read", err); |
| 791 | goto out_free; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 792 | |
| 793 | out_bu_off: |
| 794 | ui->read_in_a_row = ui->bulk_read = 0; |
| 795 | goto out_free; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /** |
| 799 | * ubifs_bulk_read - determine whether to bulk-read and, if so, do it. |
| 800 | * @page: page from which to start bulk-read. |
| 801 | * |
| 802 | * Some flash media are capable of reading sequentially at faster rates. UBIFS |
| 803 | * bulk-read facility is designed to take advantage of that, by reading in one |
| 804 | * go consecutive data nodes that are also located consecutively in the same |
| 805 | * LEB. This function returns %1 if a bulk-read is done and %0 otherwise. |
| 806 | */ |
| 807 | static int ubifs_bulk_read(struct page *page) |
| 808 | { |
| 809 | struct inode *inode = page->mapping->host; |
| 810 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 811 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 812 | pgoff_t index = page->index, last_page_read = ui->last_page_read; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 813 | struct bu_info *bu; |
Artem Bityutskiy | 3477d20 | 2008-11-19 11:53:15 +0200 | [diff] [blame] | 814 | int err = 0, allocated = 0; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 815 | |
| 816 | ui->last_page_read = index; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 817 | if (!c->bulk_read) |
| 818 | return 0; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 819 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 820 | /* |
Artem Bityutskiy | 3477d20 | 2008-11-19 11:53:15 +0200 | [diff] [blame] | 821 | * Bulk-read is protected by @ui->ui_mutex, but it is an optimization, |
| 822 | * so don't bother if we cannot lock the mutex. |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 823 | */ |
| 824 | if (!mutex_trylock(&ui->ui_mutex)) |
| 825 | return 0; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 826 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 827 | if (index != last_page_read + 1) { |
| 828 | /* Turn off bulk-read if we stop reading sequentially */ |
| 829 | ui->read_in_a_row = 1; |
| 830 | if (ui->bulk_read) |
| 831 | ui->bulk_read = 0; |
| 832 | goto out_unlock; |
| 833 | } |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 834 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 835 | if (!ui->bulk_read) { |
| 836 | ui->read_in_a_row += 1; |
| 837 | if (ui->read_in_a_row < 3) |
| 838 | goto out_unlock; |
| 839 | /* Three reads in a row, so switch on bulk-read */ |
| 840 | ui->bulk_read = 1; |
| 841 | } |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 842 | |
Artem Bityutskiy | 3477d20 | 2008-11-19 11:53:15 +0200 | [diff] [blame] | 843 | /* |
| 844 | * If possible, try to use pre-allocated bulk-read information, which |
| 845 | * is protected by @c->bu_mutex. |
| 846 | */ |
| 847 | if (mutex_trylock(&c->bu_mutex)) |
| 848 | bu = &c->bu; |
| 849 | else { |
| 850 | bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN); |
| 851 | if (!bu) |
| 852 | goto out_unlock; |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 853 | |
Artem Bityutskiy | 3477d20 | 2008-11-19 11:53:15 +0200 | [diff] [blame] | 854 | bu->buf = NULL; |
| 855 | allocated = 1; |
| 856 | } |
| 857 | |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 858 | bu->buf_len = c->max_bu_buf_len; |
| 859 | data_key_init(c, &bu->key, inode->i_ino, |
| 860 | page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT); |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 861 | err = ubifs_do_bulk_read(c, bu, page); |
Artem Bityutskiy | 3477d20 | 2008-11-19 11:53:15 +0200 | [diff] [blame] | 862 | |
| 863 | if (!allocated) |
| 864 | mutex_unlock(&c->bu_mutex); |
| 865 | else |
| 866 | kfree(bu); |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 867 | |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 868 | out_unlock: |
| 869 | mutex_unlock(&ui->ui_mutex); |
Artem Bityutskiy | 6c0c42c | 2008-11-18 20:20:05 +0200 | [diff] [blame] | 870 | return err; |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 871 | } |
| 872 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 873 | static int ubifs_readpage(struct file *file, struct page *page) |
| 874 | { |
Adrian Hunter | 4793e7c | 2008-09-02 16:29:46 +0300 | [diff] [blame] | 875 | if (ubifs_bulk_read(page)) |
| 876 | return 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 877 | do_readpage(page); |
| 878 | unlock_page(page); |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static int do_writepage(struct page *page, int len) |
| 883 | { |
| 884 | int err = 0, i, blen; |
| 885 | unsigned int block; |
| 886 | void *addr; |
| 887 | union ubifs_key key; |
| 888 | struct inode *inode = page->mapping->host; |
| 889 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 890 | |
| 891 | #ifdef UBIFS_DEBUG |
| 892 | spin_lock(&ui->ui_lock); |
| 893 | ubifs_assert(page->index <= ui->synced_i_size << PAGE_CACHE_SIZE); |
| 894 | spin_unlock(&ui->ui_lock); |
| 895 | #endif |
| 896 | |
| 897 | /* Update radix tree tags */ |
| 898 | set_page_writeback(page); |
| 899 | |
| 900 | addr = kmap(page); |
| 901 | block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; |
| 902 | i = 0; |
| 903 | while (len) { |
| 904 | blen = min_t(int, len, UBIFS_BLOCK_SIZE); |
| 905 | data_key_init(c, &key, inode->i_ino, block); |
| 906 | err = ubifs_jnl_write_data(c, inode, &key, addr, blen); |
| 907 | if (err) |
| 908 | break; |
| 909 | if (++i >= UBIFS_BLOCKS_PER_PAGE) |
| 910 | break; |
| 911 | block += 1; |
| 912 | addr += blen; |
| 913 | len -= blen; |
| 914 | } |
| 915 | if (err) { |
| 916 | SetPageError(page); |
| 917 | ubifs_err("cannot write page %lu of inode %lu, error %d", |
| 918 | page->index, inode->i_ino, err); |
| 919 | ubifs_ro_mode(c, err); |
| 920 | } |
| 921 | |
| 922 | ubifs_assert(PagePrivate(page)); |
| 923 | if (PageChecked(page)) |
| 924 | release_new_page_budget(c); |
| 925 | else |
| 926 | release_existing_page_budget(c); |
| 927 | |
| 928 | atomic_long_dec(&c->dirty_pg_cnt); |
| 929 | ClearPagePrivate(page); |
| 930 | ClearPageChecked(page); |
| 931 | |
| 932 | kunmap(page); |
| 933 | unlock_page(page); |
| 934 | end_page_writeback(page); |
| 935 | return err; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * When writing-back dirty inodes, VFS first writes-back pages belonging to the |
| 940 | * inode, then the inode itself. For UBIFS this may cause a problem. Consider a |
| 941 | * situation when a we have an inode with size 0, then a megabyte of data is |
| 942 | * appended to the inode, then write-back starts and flushes some amount of the |
| 943 | * dirty pages, the journal becomes full, commit happens and finishes, and then |
| 944 | * an unclean reboot happens. When the file system is mounted next time, the |
| 945 | * inode size would still be 0, but there would be many pages which are beyond |
| 946 | * the inode size, they would be indexed and consume flash space. Because the |
| 947 | * journal has been committed, the replay would not be able to detect this |
| 948 | * situation and correct the inode size. This means UBIFS would have to scan |
| 949 | * whole index and correct all inode sizes, which is long an unacceptable. |
| 950 | * |
| 951 | * To prevent situations like this, UBIFS writes pages back only if they are |
| 952 | * within last synchronized inode size, i.e. the the size which has been |
| 953 | * written to the flash media last time. Otherwise, UBIFS forces inode |
| 954 | * write-back, thus making sure the on-flash inode contains current inode size, |
| 955 | * and then keeps writing pages back. |
| 956 | * |
| 957 | * Some locking issues explanation. 'ubifs_writepage()' first is called with |
| 958 | * the page locked, and it locks @ui_mutex. However, write-back does take inode |
| 959 | * @i_mutex, which means other VFS operations may be run on this inode at the |
| 960 | * same time. And the problematic one is truncation to smaller size, from where |
| 961 | * we have to call 'vmtruncate()', which first changes @inode->i_size, then |
| 962 | * drops the truncated pages. And while dropping the pages, it takes the page |
| 963 | * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with |
| 964 | * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This |
| 965 | * means that @inode->i_size is changed while @ui_mutex is unlocked. |
| 966 | * |
| 967 | * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond |
| 968 | * inode size. How do we do this if @inode->i_size may became smaller while we |
| 969 | * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the |
| 970 | * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size |
| 971 | * internally and updates it under @ui_mutex. |
| 972 | * |
| 973 | * Q: why we do not worry that if we race with truncation, we may end up with a |
| 974 | * situation when the inode is truncated while we are in the middle of |
| 975 | * 'do_writepage()', so we do write beyond inode size? |
| 976 | * A: If we are in the middle of 'do_writepage()', truncation would be locked |
| 977 | * on the page lock and it would not write the truncated inode node to the |
| 978 | * journal before we have finished. |
| 979 | */ |
| 980 | static int ubifs_writepage(struct page *page, struct writeback_control *wbc) |
| 981 | { |
| 982 | struct inode *inode = page->mapping->host; |
| 983 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 984 | loff_t i_size = i_size_read(inode), synced_i_size; |
| 985 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 986 | int err, len = i_size & (PAGE_CACHE_SIZE - 1); |
| 987 | void *kaddr; |
| 988 | |
| 989 | dbg_gen("ino %lu, pg %lu, pg flags %#lx", |
| 990 | inode->i_ino, page->index, page->flags); |
| 991 | ubifs_assert(PagePrivate(page)); |
| 992 | |
| 993 | /* Is the page fully outside @i_size? (truncate in progress) */ |
| 994 | if (page->index > end_index || (page->index == end_index && !len)) { |
| 995 | err = 0; |
| 996 | goto out_unlock; |
| 997 | } |
| 998 | |
| 999 | spin_lock(&ui->ui_lock); |
| 1000 | synced_i_size = ui->synced_i_size; |
| 1001 | spin_unlock(&ui->ui_lock); |
| 1002 | |
| 1003 | /* Is the page fully inside @i_size? */ |
| 1004 | if (page->index < end_index) { |
| 1005 | if (page->index >= synced_i_size >> PAGE_CACHE_SHIFT) { |
| 1006 | err = inode->i_sb->s_op->write_inode(inode, 1); |
| 1007 | if (err) |
| 1008 | goto out_unlock; |
| 1009 | /* |
| 1010 | * The inode has been written, but the write-buffer has |
| 1011 | * not been synchronized, so in case of an unclean |
| 1012 | * reboot we may end up with some pages beyond inode |
| 1013 | * size, but they would be in the journal (because |
| 1014 | * commit flushes write buffers) and recovery would deal |
| 1015 | * with this. |
| 1016 | */ |
| 1017 | } |
| 1018 | return do_writepage(page, PAGE_CACHE_SIZE); |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * The page straddles @i_size. It must be zeroed out on each and every |
| 1023 | * writepage invocation because it may be mmapped. "A file is mapped |
| 1024 | * in multiples of the page size. For a file that is not a multiple of |
| 1025 | * the page size, the remaining memory is zeroed when mapped, and |
| 1026 | * writes to that region are not written out to the file." |
| 1027 | */ |
| 1028 | kaddr = kmap_atomic(page, KM_USER0); |
| 1029 | memset(kaddr + len, 0, PAGE_CACHE_SIZE - len); |
| 1030 | flush_dcache_page(page); |
| 1031 | kunmap_atomic(kaddr, KM_USER0); |
| 1032 | |
| 1033 | if (i_size > synced_i_size) { |
| 1034 | err = inode->i_sb->s_op->write_inode(inode, 1); |
| 1035 | if (err) |
| 1036 | goto out_unlock; |
| 1037 | } |
| 1038 | |
| 1039 | return do_writepage(page, len); |
| 1040 | |
| 1041 | out_unlock: |
| 1042 | unlock_page(page); |
| 1043 | return err; |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * do_attr_changes - change inode attributes. |
| 1048 | * @inode: inode to change attributes for |
| 1049 | * @attr: describes attributes to change |
| 1050 | */ |
| 1051 | static void do_attr_changes(struct inode *inode, const struct iattr *attr) |
| 1052 | { |
| 1053 | if (attr->ia_valid & ATTR_UID) |
| 1054 | inode->i_uid = attr->ia_uid; |
| 1055 | if (attr->ia_valid & ATTR_GID) |
| 1056 | inode->i_gid = attr->ia_gid; |
| 1057 | if (attr->ia_valid & ATTR_ATIME) |
| 1058 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 1059 | inode->i_sb->s_time_gran); |
| 1060 | if (attr->ia_valid & ATTR_MTIME) |
| 1061 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 1062 | inode->i_sb->s_time_gran); |
| 1063 | if (attr->ia_valid & ATTR_CTIME) |
| 1064 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 1065 | inode->i_sb->s_time_gran); |
| 1066 | if (attr->ia_valid & ATTR_MODE) { |
| 1067 | umode_t mode = attr->ia_mode; |
| 1068 | |
| 1069 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 1070 | mode &= ~S_ISGID; |
| 1071 | inode->i_mode = mode; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * do_truncation - truncate an inode. |
| 1077 | * @c: UBIFS file-system description object |
| 1078 | * @inode: inode to truncate |
| 1079 | * @attr: inode attribute changes description |
| 1080 | * |
| 1081 | * This function implements VFS '->setattr()' call when the inode is truncated |
| 1082 | * to a smaller size. Returns zero in case of success and a negative error code |
| 1083 | * in case of failure. |
| 1084 | */ |
| 1085 | static int do_truncation(struct ubifs_info *c, struct inode *inode, |
| 1086 | const struct iattr *attr) |
| 1087 | { |
| 1088 | int err; |
| 1089 | struct ubifs_budget_req req; |
| 1090 | loff_t old_size = inode->i_size, new_size = attr->ia_size; |
Artem Bityutskiy | 04da11b | 2008-08-20 17:16:34 +0300 | [diff] [blame] | 1091 | int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1092 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1093 | |
| 1094 | dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size); |
| 1095 | memset(&req, 0, sizeof(struct ubifs_budget_req)); |
| 1096 | |
| 1097 | /* |
| 1098 | * If this is truncation to a smaller size, and we do not truncate on a |
| 1099 | * block boundary, budget for changing one data block, because the last |
| 1100 | * block will be re-written. |
| 1101 | */ |
| 1102 | if (new_size & (UBIFS_BLOCK_SIZE - 1)) |
| 1103 | req.dirtied_page = 1; |
| 1104 | |
| 1105 | req.dirtied_ino = 1; |
| 1106 | /* A funny way to budget for truncation node */ |
| 1107 | req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ; |
| 1108 | err = ubifs_budget_space(c, &req); |
Artem Bityutskiy | 04da11b | 2008-08-20 17:16:34 +0300 | [diff] [blame] | 1109 | if (err) { |
| 1110 | /* |
| 1111 | * Treat truncations to zero as deletion and always allow them, |
| 1112 | * just like we do for '->unlink()'. |
| 1113 | */ |
| 1114 | if (new_size || err != -ENOSPC) |
| 1115 | return err; |
| 1116 | budgeted = 0; |
| 1117 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1118 | |
| 1119 | err = vmtruncate(inode, new_size); |
| 1120 | if (err) |
| 1121 | goto out_budg; |
| 1122 | |
| 1123 | if (offset) { |
| 1124 | pgoff_t index = new_size >> PAGE_CACHE_SHIFT; |
| 1125 | struct page *page; |
| 1126 | |
| 1127 | page = find_lock_page(inode->i_mapping, index); |
| 1128 | if (page) { |
| 1129 | if (PageDirty(page)) { |
| 1130 | /* |
| 1131 | * 'ubifs_jnl_truncate()' will try to truncate |
| 1132 | * the last data node, but it contains |
| 1133 | * out-of-date data because the page is dirty. |
| 1134 | * Write the page now, so that |
| 1135 | * 'ubifs_jnl_truncate()' will see an already |
| 1136 | * truncated (and up to date) data node. |
| 1137 | */ |
| 1138 | ubifs_assert(PagePrivate(page)); |
| 1139 | |
| 1140 | clear_page_dirty_for_io(page); |
| 1141 | if (UBIFS_BLOCKS_PER_PAGE_SHIFT) |
| 1142 | offset = new_size & |
| 1143 | (PAGE_CACHE_SIZE - 1); |
| 1144 | err = do_writepage(page, offset); |
| 1145 | page_cache_release(page); |
| 1146 | if (err) |
| 1147 | goto out_budg; |
| 1148 | /* |
| 1149 | * We could now tell 'ubifs_jnl_truncate()' not |
| 1150 | * to read the last block. |
| 1151 | */ |
| 1152 | } else { |
| 1153 | /* |
| 1154 | * We could 'kmap()' the page and pass the data |
| 1155 | * to 'ubifs_jnl_truncate()' to save it from |
| 1156 | * having to read it. |
| 1157 | */ |
| 1158 | unlock_page(page); |
| 1159 | page_cache_release(page); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | mutex_lock(&ui->ui_mutex); |
| 1165 | ui->ui_size = inode->i_size; |
| 1166 | /* Truncation changes inode [mc]time */ |
| 1167 | inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); |
| 1168 | /* The other attributes may be changed at the same time as well */ |
| 1169 | do_attr_changes(inode, attr); |
| 1170 | |
| 1171 | err = ubifs_jnl_truncate(c, inode, old_size, new_size); |
| 1172 | mutex_unlock(&ui->ui_mutex); |
| 1173 | out_budg: |
Artem Bityutskiy | 04da11b | 2008-08-20 17:16:34 +0300 | [diff] [blame] | 1174 | if (budgeted) |
| 1175 | ubifs_release_budget(c, &req); |
| 1176 | else { |
| 1177 | c->nospace = c->nospace_rp = 0; |
| 1178 | smp_wmb(); |
| 1179 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1180 | return err; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * do_setattr - change inode attributes. |
| 1185 | * @c: UBIFS file-system description object |
| 1186 | * @inode: inode to change attributes for |
| 1187 | * @attr: inode attribute changes description |
| 1188 | * |
| 1189 | * This function implements VFS '->setattr()' call for all cases except |
| 1190 | * truncations to smaller size. Returns zero in case of success and a negative |
| 1191 | * error code in case of failure. |
| 1192 | */ |
| 1193 | static int do_setattr(struct ubifs_info *c, struct inode *inode, |
| 1194 | const struct iattr *attr) |
| 1195 | { |
| 1196 | int err, release; |
| 1197 | loff_t new_size = attr->ia_size; |
| 1198 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1199 | struct ubifs_budget_req req = { .dirtied_ino = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1200 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1201 | |
| 1202 | err = ubifs_budget_space(c, &req); |
| 1203 | if (err) |
| 1204 | return err; |
| 1205 | |
| 1206 | if (attr->ia_valid & ATTR_SIZE) { |
| 1207 | dbg_gen("size %lld -> %lld", inode->i_size, new_size); |
| 1208 | err = vmtruncate(inode, new_size); |
| 1209 | if (err) |
| 1210 | goto out; |
| 1211 | } |
| 1212 | |
| 1213 | mutex_lock(&ui->ui_mutex); |
| 1214 | if (attr->ia_valid & ATTR_SIZE) { |
| 1215 | /* Truncation changes inode [mc]time */ |
| 1216 | inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); |
| 1217 | /* 'vmtruncate()' changed @i_size, update @ui_size */ |
| 1218 | ui->ui_size = inode->i_size; |
| 1219 | } |
| 1220 | |
| 1221 | do_attr_changes(inode, attr); |
| 1222 | |
| 1223 | release = ui->dirty; |
| 1224 | if (attr->ia_valid & ATTR_SIZE) |
| 1225 | /* |
| 1226 | * Inode length changed, so we have to make sure |
| 1227 | * @I_DIRTY_DATASYNC is set. |
| 1228 | */ |
| 1229 | __mark_inode_dirty(inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC); |
| 1230 | else |
| 1231 | mark_inode_dirty_sync(inode); |
| 1232 | mutex_unlock(&ui->ui_mutex); |
| 1233 | |
| 1234 | if (release) |
| 1235 | ubifs_release_budget(c, &req); |
| 1236 | if (IS_SYNC(inode)) |
| 1237 | err = inode->i_sb->s_op->write_inode(inode, 1); |
| 1238 | return err; |
| 1239 | |
| 1240 | out: |
| 1241 | ubifs_release_budget(c, &req); |
| 1242 | return err; |
| 1243 | } |
| 1244 | |
| 1245 | int ubifs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1246 | { |
| 1247 | int err; |
| 1248 | struct inode *inode = dentry->d_inode; |
| 1249 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 1250 | |
Artem Bityutskiy | 7d32c2b | 2008-07-18 18:54:29 +0300 | [diff] [blame] | 1251 | dbg_gen("ino %lu, mode %#x, ia_valid %#x", |
| 1252 | inode->i_ino, inode->i_mode, attr->ia_valid); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1253 | err = inode_change_ok(inode, attr); |
| 1254 | if (err) |
| 1255 | return err; |
| 1256 | |
| 1257 | err = dbg_check_synced_i_size(inode); |
| 1258 | if (err) |
| 1259 | return err; |
| 1260 | |
| 1261 | if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size) |
| 1262 | /* Truncation to a smaller size */ |
| 1263 | err = do_truncation(c, inode, attr); |
| 1264 | else |
| 1265 | err = do_setattr(c, inode, attr); |
| 1266 | |
| 1267 | return err; |
| 1268 | } |
| 1269 | |
| 1270 | static void ubifs_invalidatepage(struct page *page, unsigned long offset) |
| 1271 | { |
| 1272 | struct inode *inode = page->mapping->host; |
| 1273 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 1274 | |
| 1275 | ubifs_assert(PagePrivate(page)); |
| 1276 | if (offset) |
| 1277 | /* Partial page remains dirty */ |
| 1278 | return; |
| 1279 | |
| 1280 | if (PageChecked(page)) |
| 1281 | release_new_page_budget(c); |
| 1282 | else |
| 1283 | release_existing_page_budget(c); |
| 1284 | |
| 1285 | atomic_long_dec(&c->dirty_pg_cnt); |
| 1286 | ClearPagePrivate(page); |
| 1287 | ClearPageChecked(page); |
| 1288 | } |
| 1289 | |
| 1290 | static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1291 | { |
| 1292 | struct ubifs_inode *ui = ubifs_inode(dentry->d_inode); |
| 1293 | |
| 1294 | nd_set_link(nd, ui->data); |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
| 1298 | int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 1299 | { |
| 1300 | struct inode *inode = dentry->d_inode; |
| 1301 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 1302 | int err; |
| 1303 | |
| 1304 | dbg_gen("syncing inode %lu", inode->i_ino); |
| 1305 | |
| 1306 | /* |
| 1307 | * VFS has already synchronized dirty pages for this inode. Synchronize |
| 1308 | * the inode unless this is a 'datasync()' call. |
| 1309 | */ |
| 1310 | if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) { |
| 1311 | err = inode->i_sb->s_op->write_inode(inode, 1); |
| 1312 | if (err) |
| 1313 | return err; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * Nodes related to this inode may still sit in a write-buffer. Flush |
| 1318 | * them. |
| 1319 | */ |
| 1320 | err = ubifs_sync_wbufs_by_inode(c, inode); |
| 1321 | if (err) |
| 1322 | return err; |
| 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * mctime_update_needed - check if mtime or ctime update is needed. |
| 1329 | * @inode: the inode to do the check for |
| 1330 | * @now: current time |
| 1331 | * |
| 1332 | * This helper function checks if the inode mtime/ctime should be updated or |
| 1333 | * not. If current values of the time-stamps are within the UBIFS inode time |
| 1334 | * granularity, they are not updated. This is an optimization. |
| 1335 | */ |
| 1336 | static inline int mctime_update_needed(const struct inode *inode, |
| 1337 | const struct timespec *now) |
| 1338 | { |
| 1339 | if (!timespec_equal(&inode->i_mtime, now) || |
| 1340 | !timespec_equal(&inode->i_ctime, now)) |
| 1341 | return 1; |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * update_ctime - update mtime and ctime of an inode. |
| 1347 | * @c: UBIFS file-system description object |
| 1348 | * @inode: inode to update |
| 1349 | * |
| 1350 | * This function updates mtime and ctime of the inode if it is not equivalent to |
| 1351 | * current time. Returns zero in case of success and a negative error code in |
| 1352 | * case of failure. |
| 1353 | */ |
| 1354 | static int update_mctime(struct ubifs_info *c, struct inode *inode) |
| 1355 | { |
| 1356 | struct timespec now = ubifs_current_time(inode); |
| 1357 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1358 | |
| 1359 | if (mctime_update_needed(inode, &now)) { |
| 1360 | int err, release; |
| 1361 | struct ubifs_budget_req req = { .dirtied_ino = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1362 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1363 | |
| 1364 | err = ubifs_budget_space(c, &req); |
| 1365 | if (err) |
| 1366 | return err; |
| 1367 | |
| 1368 | mutex_lock(&ui->ui_mutex); |
| 1369 | inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); |
| 1370 | release = ui->dirty; |
| 1371 | mark_inode_dirty_sync(inode); |
| 1372 | mutex_unlock(&ui->ui_mutex); |
| 1373 | if (release) |
| 1374 | ubifs_release_budget(c, &req); |
| 1375 | } |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 1381 | unsigned long nr_segs, loff_t pos) |
| 1382 | { |
| 1383 | int err; |
| 1384 | ssize_t ret; |
| 1385 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 1386 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 1387 | |
| 1388 | err = update_mctime(c, inode); |
| 1389 | if (err) |
| 1390 | return err; |
| 1391 | |
| 1392 | ret = generic_file_aio_write(iocb, iov, nr_segs, pos); |
| 1393 | if (ret < 0) |
| 1394 | return ret; |
| 1395 | |
| 1396 | if (ret > 0 && (IS_SYNC(inode) || iocb->ki_filp->f_flags & O_SYNC)) { |
| 1397 | err = ubifs_sync_wbufs_by_inode(c, inode); |
| 1398 | if (err) |
| 1399 | return err; |
| 1400 | } |
| 1401 | |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
| 1405 | static int ubifs_set_page_dirty(struct page *page) |
| 1406 | { |
| 1407 | int ret; |
| 1408 | |
| 1409 | ret = __set_page_dirty_nobuffers(page); |
| 1410 | /* |
| 1411 | * An attempt to dirty a page without budgeting for it - should not |
| 1412 | * happen. |
| 1413 | */ |
| 1414 | ubifs_assert(ret == 0); |
| 1415 | return ret; |
| 1416 | } |
| 1417 | |
| 1418 | static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags) |
| 1419 | { |
| 1420 | /* |
| 1421 | * An attempt to release a dirty page without budgeting for it - should |
| 1422 | * not happen. |
| 1423 | */ |
| 1424 | if (PageWriteback(page)) |
| 1425 | return 0; |
| 1426 | ubifs_assert(PagePrivate(page)); |
| 1427 | ubifs_assert(0); |
| 1428 | ClearPagePrivate(page); |
| 1429 | ClearPageChecked(page); |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * mmap()d file has taken write protection fault and is being made |
| 1435 | * writable. UBIFS must ensure page is budgeted for. |
| 1436 | */ |
| 1437 | static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 1438 | { |
| 1439 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 1440 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 1441 | struct timespec now = ubifs_current_time(inode); |
| 1442 | struct ubifs_budget_req req = { .new_page = 1 }; |
| 1443 | int err, update_time; |
| 1444 | |
| 1445 | dbg_gen("ino %lu, pg %lu, i_size %lld", inode->i_ino, page->index, |
| 1446 | i_size_read(inode)); |
| 1447 | ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY)); |
| 1448 | |
| 1449 | if (unlikely(c->ro_media)) |
| 1450 | return -EROFS; |
| 1451 | |
| 1452 | /* |
| 1453 | * We have not locked @page so far so we may budget for changing the |
| 1454 | * page. Note, we cannot do this after we locked the page, because |
| 1455 | * budgeting may cause write-back which would cause deadlock. |
| 1456 | * |
| 1457 | * At the moment we do not know whether the page is dirty or not, so we |
| 1458 | * assume that it is not and budget for a new page. We could look at |
| 1459 | * the @PG_private flag and figure this out, but we may race with write |
| 1460 | * back and the page state may change by the time we lock it, so this |
| 1461 | * would need additional care. We do not bother with this at the |
| 1462 | * moment, although it might be good idea to do. Instead, we allocate |
| 1463 | * budget for a new page and amend it later on if the page was in fact |
| 1464 | * dirty. |
| 1465 | * |
| 1466 | * The budgeting-related logic of this function is similar to what we |
| 1467 | * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there |
| 1468 | * for more comments. |
| 1469 | */ |
| 1470 | update_time = mctime_update_needed(inode, &now); |
| 1471 | if (update_time) |
| 1472 | /* |
| 1473 | * We have to change inode time stamp which requires extra |
| 1474 | * budgeting. |
| 1475 | */ |
| 1476 | req.dirtied_ino = 1; |
| 1477 | |
| 1478 | err = ubifs_budget_space(c, &req); |
| 1479 | if (unlikely(err)) { |
| 1480 | if (err == -ENOSPC) |
| 1481 | ubifs_warn("out of space for mmapped file " |
| 1482 | "(inode number %lu)", inode->i_ino); |
| 1483 | return err; |
| 1484 | } |
| 1485 | |
| 1486 | lock_page(page); |
| 1487 | if (unlikely(page->mapping != inode->i_mapping || |
| 1488 | page_offset(page) > i_size_read(inode))) { |
| 1489 | /* Page got truncated out from underneath us */ |
| 1490 | err = -EINVAL; |
| 1491 | goto out_unlock; |
| 1492 | } |
| 1493 | |
| 1494 | if (PagePrivate(page)) |
| 1495 | release_new_page_budget(c); |
| 1496 | else { |
| 1497 | if (!PageChecked(page)) |
| 1498 | ubifs_convert_page_budget(c); |
| 1499 | SetPagePrivate(page); |
| 1500 | atomic_long_inc(&c->dirty_pg_cnt); |
| 1501 | __set_page_dirty_nobuffers(page); |
| 1502 | } |
| 1503 | |
| 1504 | if (update_time) { |
| 1505 | int release; |
| 1506 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1507 | |
| 1508 | mutex_lock(&ui->ui_mutex); |
| 1509 | inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); |
| 1510 | release = ui->dirty; |
| 1511 | mark_inode_dirty_sync(inode); |
| 1512 | mutex_unlock(&ui->ui_mutex); |
| 1513 | if (release) |
| 1514 | ubifs_release_dirty_inode_budget(c, ui); |
| 1515 | } |
| 1516 | |
| 1517 | unlock_page(page); |
| 1518 | return 0; |
| 1519 | |
| 1520 | out_unlock: |
| 1521 | unlock_page(page); |
| 1522 | ubifs_release_budget(c, &req); |
| 1523 | return err; |
| 1524 | } |
| 1525 | |
| 1526 | static struct vm_operations_struct ubifs_file_vm_ops = { |
| 1527 | .fault = filemap_fault, |
| 1528 | .page_mkwrite = ubifs_vm_page_mkwrite, |
| 1529 | }; |
| 1530 | |
| 1531 | static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 1532 | { |
| 1533 | int err; |
| 1534 | |
| 1535 | /* 'generic_file_mmap()' takes care of NOMMU case */ |
| 1536 | err = generic_file_mmap(file, vma); |
| 1537 | if (err) |
| 1538 | return err; |
| 1539 | vma->vm_ops = &ubifs_file_vm_ops; |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | struct address_space_operations ubifs_file_address_operations = { |
| 1544 | .readpage = ubifs_readpage, |
| 1545 | .writepage = ubifs_writepage, |
| 1546 | .write_begin = ubifs_write_begin, |
| 1547 | .write_end = ubifs_write_end, |
| 1548 | .invalidatepage = ubifs_invalidatepage, |
| 1549 | .set_page_dirty = ubifs_set_page_dirty, |
| 1550 | .releasepage = ubifs_releasepage, |
| 1551 | }; |
| 1552 | |
| 1553 | struct inode_operations ubifs_file_inode_operations = { |
| 1554 | .setattr = ubifs_setattr, |
| 1555 | .getattr = ubifs_getattr, |
| 1556 | #ifdef CONFIG_UBIFS_FS_XATTR |
| 1557 | .setxattr = ubifs_setxattr, |
| 1558 | .getxattr = ubifs_getxattr, |
| 1559 | .listxattr = ubifs_listxattr, |
| 1560 | .removexattr = ubifs_removexattr, |
| 1561 | #endif |
| 1562 | }; |
| 1563 | |
| 1564 | struct inode_operations ubifs_symlink_inode_operations = { |
| 1565 | .readlink = generic_readlink, |
| 1566 | .follow_link = ubifs_follow_link, |
| 1567 | .setattr = ubifs_setattr, |
| 1568 | .getattr = ubifs_getattr, |
| 1569 | }; |
| 1570 | |
| 1571 | struct file_operations ubifs_file_operations = { |
| 1572 | .llseek = generic_file_llseek, |
| 1573 | .read = do_sync_read, |
| 1574 | .write = do_sync_write, |
| 1575 | .aio_read = generic_file_aio_read, |
| 1576 | .aio_write = ubifs_aio_write, |
| 1577 | .mmap = ubifs_file_mmap, |
| 1578 | .fsync = ubifs_fsync, |
| 1579 | .unlocked_ioctl = ubifs_ioctl, |
| 1580 | .splice_read = generic_file_splice_read, |
Zoltan Sogor | 22bc7fa | 2008-07-28 16:28:49 +0200 | [diff] [blame] | 1581 | .splice_write = generic_file_splice_write, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1582 | #ifdef CONFIG_COMPAT |
| 1583 | .compat_ioctl = ubifs_compat_ioctl, |
| 1584 | #endif |
| 1585 | }; |