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