2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This file implements VFS file and inode operations of regular files, device
25 * nodes and symlinks as well as address space operations.
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.
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
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.
50 * This, for example means that there might be 2 concurrent '->writepage()'
51 * calls for the same inode, but different inode dirty pages.
55 #include <linux/mount.h>
57 static int read_block(struct inode
*inode
, void *addr
, unsigned int block
,
58 struct ubifs_data_node
*dn
)
60 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
61 int err
, len
, out_len
;
65 data_key_init(c
, &key
, inode
->i_ino
, block
);
66 err
= ubifs_tnc_lookup(c
, &key
, dn
);
69 /* Not found, so it must be a hole */
70 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
74 ubifs_assert(dn
->ch
.sqnum
> ubifs_inode(inode
)->creat_sqnum
);
76 len
= le32_to_cpu(dn
->size
);
77 if (len
<= 0 || len
> UBIFS_BLOCK_SIZE
)
80 dlen
= le32_to_cpu(dn
->ch
.len
) - UBIFS_DATA_NODE_SZ
;
81 out_len
= UBIFS_BLOCK_SIZE
;
82 err
= ubifs_decompress(&dn
->data
, dlen
, addr
, &out_len
,
83 le16_to_cpu(dn
->compr_type
));
84 if (err
|| len
!= out_len
)
88 * Data length can be less than a full block, even for blocks that are
89 * not the last in the file (e.g., as a result of making a hole and
90 * appending data). Ensure that the remainder is zeroed out.
92 if (len
< UBIFS_BLOCK_SIZE
)
93 memset(addr
+ len
, 0, UBIFS_BLOCK_SIZE
- len
);
98 ubifs_err("bad data node (block %u, inode %lu)",
100 dbg_dump_node(c
, dn
);
104 static int do_readpage(struct page
*page
)
108 unsigned int block
, beyond
;
109 struct ubifs_data_node
*dn
;
110 struct inode
*inode
= page
->mapping
->host
;
111 loff_t i_size
= i_size_read(inode
);
113 dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
114 inode
->i_ino
, page
->index
, i_size
, page
->flags
);
115 ubifs_assert(!PageChecked(page
));
116 ubifs_assert(!PagePrivate(page
));
120 block
= page
->index
<< UBIFS_BLOCKS_PER_PAGE_SHIFT
;
121 beyond
= (i_size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
122 if (block
>= beyond
) {
123 /* Reading beyond inode */
124 SetPageChecked(page
);
125 memset(addr
, 0, PAGE_CACHE_SIZE
);
129 dn
= kmalloc(UBIFS_MAX_DATA_NODE_SZ
, GFP_NOFS
);
139 if (block
>= beyond
) {
140 /* Reading beyond inode */
142 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
144 ret
= read_block(inode
, addr
, block
, dn
);
151 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
154 addr
+= UBIFS_BLOCK_SIZE
;
157 if (err
== -ENOENT
) {
158 /* Not found, so it must be a hole */
159 SetPageChecked(page
);
163 ubifs_err("cannot read page %lu of inode %lu, error %d",
164 page
->index
, inode
->i_ino
, err
);
171 SetPageUptodate(page
);
172 ClearPageError(page
);
173 flush_dcache_page(page
);
179 ClearPageUptodate(page
);
181 flush_dcache_page(page
);
187 * release_new_page_budget - release budget of a new page.
188 * @c: UBIFS file-system description object
190 * This is a helper function which releases budget corresponding to the budget
191 * of one new page of data.
193 static void release_new_page_budget(struct ubifs_info
*c
)
195 struct ubifs_budget_req req
= { .recalculate
= 1, .new_page
= 1 };
197 ubifs_release_budget(c
, &req
);
201 * release_existing_page_budget - release budget of an existing page.
202 * @c: UBIFS file-system description object
204 * This is a helper function which releases budget corresponding to the budget
205 * of changing one one page of data which already exists on the flash media.
207 static void release_existing_page_budget(struct ubifs_info
*c
)
209 struct ubifs_budget_req req
= { .dd_growth
= c
->page_budget
};
211 ubifs_release_budget(c
, &req
);
214 static int write_begin_slow(struct address_space
*mapping
,
215 loff_t pos
, unsigned len
, struct page
**pagep
)
217 struct inode
*inode
= mapping
->host
;
218 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
219 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
220 struct ubifs_budget_req req
= { .new_page
= 1 };
221 int uninitialized_var(err
), appending
= !!(pos
+ len
> inode
->i_size
);
224 dbg_gen("ino %lu, pos %llu, len %u, i_size %lld",
225 inode
->i_ino
, pos
, len
, inode
->i_size
);
228 * At the slow path we have to budget before locking the page, because
229 * budgeting may force write-back, which would wait on locked pages and
230 * deadlock if we had the page locked. At this point we do not know
231 * anything about the page, so assume that this is a new page which is
232 * written to a hole. This corresponds to largest budget. Later the
233 * budget will be amended if this is not true.
236 /* We are appending data, budget for inode change */
239 err
= ubifs_budget_space(c
, &req
);
243 page
= __grab_cache_page(mapping
, index
);
244 if (unlikely(!page
)) {
245 ubifs_release_budget(c
, &req
);
249 if (!PageUptodate(page
)) {
250 if (!(pos
& PAGE_CACHE_MASK
) && len
== PAGE_CACHE_SIZE
)
251 SetPageChecked(page
);
253 err
= do_readpage(page
);
256 page_cache_release(page
);
261 SetPageUptodate(page
);
262 ClearPageError(page
);
265 if (PagePrivate(page
))
267 * The page is dirty, which means it was budgeted twice:
268 * o first time the budget was allocated by the task which
269 * made the page dirty and set the PG_private flag;
270 * o and then we budgeted for it for the second time at the
271 * very beginning of this function.
273 * So what we have to do is to release the page budget we
276 release_new_page_budget(c
);
277 else if (!PageChecked(page
))
279 * We are changing a page which already exists on the media.
280 * This means that changing the page does not make the amount
281 * of indexing information larger, and this part of the budget
282 * which we have already acquired may be released.
284 ubifs_convert_page_budget(c
);
287 struct ubifs_inode
*ui
= ubifs_inode(inode
);
290 * 'ubifs_write_end()' is optimized from the fast-path part of
291 * 'ubifs_write_begin()' and expects the @ui_mutex to be locked
292 * if data is appended.
294 mutex_lock(&ui
->ui_mutex
);
297 * The inode is dirty already, so we may free the
298 * budget we allocated.
300 ubifs_release_dirty_inode_budget(c
, ui
);
308 * allocate_budget - allocate budget for 'ubifs_write_begin()'.
309 * @c: UBIFS file-system description object
310 * @page: page to allocate budget for
311 * @ui: UBIFS inode object the page belongs to
312 * @appending: non-zero if the page is appended
314 * This is a helper function for 'ubifs_write_begin()' which allocates budget
315 * for the operation. The budget is allocated differently depending on whether
316 * this is appending, whether the page is dirty or not, and so on. This
317 * function leaves the @ui->ui_mutex locked in case of appending. Returns zero
318 * in case of success and %-ENOSPC in case of failure.
320 static int allocate_budget(struct ubifs_info
*c
, struct page
*page
,
321 struct ubifs_inode
*ui
, int appending
)
323 struct ubifs_budget_req req
= { .fast
= 1 };
325 if (PagePrivate(page
)) {
328 * The page is dirty and we are not appending, which
329 * means no budget is needed at all.
333 mutex_lock(&ui
->ui_mutex
);
336 * The page is dirty and we are appending, so the inode
337 * has to be marked as dirty. However, it is already
338 * dirty, so we do not need any budget. We may return,
339 * but @ui->ui_mutex hast to be left locked because we
340 * should prevent write-back from flushing the inode
341 * and freeing the budget. The lock will be released in
342 * 'ubifs_write_end()'.
347 * The page is dirty, we are appending, the inode is clean, so
348 * we need to budget the inode change.
352 if (PageChecked(page
))
354 * The page corresponds to a hole and does not
355 * exist on the media. So changing it makes
356 * make the amount of indexing information
357 * larger, and we have to budget for a new
363 * Not a hole, the change will not add any new
364 * indexing information, budget for page
367 req
.dirtied_page
= 1;
370 mutex_lock(&ui
->ui_mutex
);
373 * The inode is clean but we will have to mark
374 * it as dirty because we are appending. This
381 return ubifs_budget_space(c
, &req
);
385 * This function is called when a page of data is going to be written. Since
386 * the page of data will not necessarily go to the flash straight away, UBIFS
387 * has to reserve space on the media for it, which is done by means of
390 * This is the hot-path of the file-system and we are trying to optimize it as
391 * much as possible. For this reasons it is split on 2 parts - slow and fast.
393 * There many budgeting cases:
394 * o a new page is appended - we have to budget for a new page and for
395 * changing the inode; however, if the inode is already dirty, there is
396 * no need to budget for it;
397 * o an existing clean page is changed - we have budget for it; if the page
398 * does not exist on the media (a hole), we have to budget for a new
399 * page; otherwise, we may budget for changing an existing page; the
400 * difference between these cases is that changing an existing page does
401 * not introduce anything new to the FS indexing information, so it does
402 * not grow, and smaller budget is acquired in this case;
403 * o an existing dirty page is changed - no need to budget at all, because
404 * the page budget has been acquired by earlier, when the page has been
407 * UBIFS budgeting sub-system may force write-back if it thinks there is no
408 * space to reserve. This imposes some locking restrictions and makes it
409 * impossible to take into account the above cases, and makes it impossible to
410 * optimize budgeting.
412 * The solution for this is that the fast path of 'ubifs_write_begin()' assumes
413 * there is a plenty of flash space and the budget will be acquired quickly,
414 * without forcing write-back. The slow path does not make this assumption.
416 static int ubifs_write_begin(struct file
*file
, struct address_space
*mapping
,
417 loff_t pos
, unsigned len
, unsigned flags
,
418 struct page
**pagep
, void **fsdata
)
420 struct inode
*inode
= mapping
->host
;
421 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
422 struct ubifs_inode
*ui
= ubifs_inode(inode
);
423 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
424 int uninitialized_var(err
), appending
= !!(pos
+ len
> inode
->i_size
);
428 ubifs_assert(ubifs_inode(inode
)->ui_size
== inode
->i_size
);
430 if (unlikely(c
->ro_media
))
433 /* Try out the fast-path part first */
434 page
= __grab_cache_page(mapping
, index
);
438 if (!PageUptodate(page
)) {
439 /* The page is not loaded from the flash */
440 if (!(pos
& PAGE_CACHE_MASK
) && len
== PAGE_CACHE_SIZE
)
442 * We change whole page so no need to load it. But we
443 * have to set the @PG_checked flag to make the further
444 * code the page is new. This might be not true, but it
445 * is better to budget more that to read the page from
448 SetPageChecked(page
);
450 err
= do_readpage(page
);
453 page_cache_release(page
);
458 SetPageUptodate(page
);
459 ClearPageError(page
);
462 err
= allocate_budget(c
, page
, ui
, appending
);
464 ubifs_assert(err
== -ENOSPC
);
466 * Budgeting failed which means it would have to force
467 * write-back but didn't, because we set the @fast flag in the
468 * request. Write-back cannot be done now, while we have the
469 * page locked, because it would deadlock. Unlock and free
470 * everything and fall-back to slow-path.
473 ubifs_assert(mutex_is_locked(&ui
->ui_mutex
));
474 mutex_unlock(&ui
->ui_mutex
);
477 page_cache_release(page
);
479 return write_begin_slow(mapping
, pos
, len
, pagep
);
483 * Whee, we aquired budgeting quickly - without involving
484 * garbage-collection, committing or forceing write-back. We return
485 * with @ui->ui_mutex locked if we are appending pages, and unlocked
486 * otherwise. This is an optimization (slightly hacky though).
494 * cancel_budget - cancel budget.
495 * @c: UBIFS file-system description object
496 * @page: page to cancel budget for
497 * @ui: UBIFS inode object the page belongs to
498 * @appending: non-zero if the page is appended
500 * This is a helper function for a page write operation. It unlocks the
501 * @ui->ui_mutex in case of appending.
503 static void cancel_budget(struct ubifs_info
*c
, struct page
*page
,
504 struct ubifs_inode
*ui
, int appending
)
508 ubifs_release_dirty_inode_budget(c
, ui
);
509 mutex_unlock(&ui
->ui_mutex
);
511 if (!PagePrivate(page
)) {
512 if (PageChecked(page
))
513 release_new_page_budget(c
);
515 release_existing_page_budget(c
);
519 static int ubifs_write_end(struct file
*file
, struct address_space
*mapping
,
520 loff_t pos
, unsigned len
, unsigned copied
,
521 struct page
*page
, void *fsdata
)
523 struct inode
*inode
= mapping
->host
;
524 struct ubifs_inode
*ui
= ubifs_inode(inode
);
525 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
526 loff_t end_pos
= pos
+ len
;
527 int appending
= !!(end_pos
> inode
->i_size
);
529 dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
530 inode
->i_ino
, pos
, page
->index
, len
, copied
, inode
->i_size
);
532 if (unlikely(copied
< len
&& len
== PAGE_CACHE_SIZE
)) {
534 * VFS copied less data to the page that it intended and
535 * declared in its '->write_begin()' call via the @len
536 * argument. If the page was not up-to-date, and @len was
537 * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did
538 * not load it from the media (for optimization reasons). This
539 * means that part of the page contains garbage. So read the
542 dbg_gen("copied %d instead of %d, read page and repeat",
544 cancel_budget(c
, page
, ui
, appending
);
547 * Return 0 to force VFS to repeat the whole operation, or the
548 * error code if 'do_readpage()' failes.
550 copied
= do_readpage(page
);
554 if (!PagePrivate(page
)) {
555 SetPagePrivate(page
);
556 atomic_long_inc(&c
->dirty_pg_cnt
);
557 __set_page_dirty_nobuffers(page
);
561 i_size_write(inode
, end_pos
);
562 ui
->ui_size
= end_pos
;
564 * Note, we do not set @I_DIRTY_PAGES (which means that the
565 * inode has dirty pages), this has been done in
566 * '__set_page_dirty_nobuffers()'.
568 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
569 ubifs_assert(mutex_is_locked(&ui
->ui_mutex
));
570 mutex_unlock(&ui
->ui_mutex
);
575 page_cache_release(page
);
579 static int ubifs_readpage(struct file
*file
, struct page
*page
)
586 static int do_writepage(struct page
*page
, int len
)
588 int err
= 0, i
, blen
;
592 struct inode
*inode
= page
->mapping
->host
;
593 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
596 spin_lock(&ui
->ui_lock
);
597 ubifs_assert(page
->index
<= ui
->synced_i_size
<< PAGE_CACHE_SIZE
);
598 spin_unlock(&ui
->ui_lock
);
601 /* Update radix tree tags */
602 set_page_writeback(page
);
605 block
= page
->index
<< UBIFS_BLOCKS_PER_PAGE_SHIFT
;
608 blen
= min_t(int, len
, UBIFS_BLOCK_SIZE
);
609 data_key_init(c
, &key
, inode
->i_ino
, block
);
610 err
= ubifs_jnl_write_data(c
, inode
, &key
, addr
, blen
);
613 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
621 ubifs_err("cannot write page %lu of inode %lu, error %d",
622 page
->index
, inode
->i_ino
, err
);
623 ubifs_ro_mode(c
, err
);
626 ubifs_assert(PagePrivate(page
));
627 if (PageChecked(page
))
628 release_new_page_budget(c
);
630 release_existing_page_budget(c
);
632 atomic_long_dec(&c
->dirty_pg_cnt
);
633 ClearPagePrivate(page
);
634 ClearPageChecked(page
);
638 end_page_writeback(page
);
643 * When writing-back dirty inodes, VFS first writes-back pages belonging to the
644 * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
645 * situation when a we have an inode with size 0, then a megabyte of data is
646 * appended to the inode, then write-back starts and flushes some amount of the
647 * dirty pages, the journal becomes full, commit happens and finishes, and then
648 * an unclean reboot happens. When the file system is mounted next time, the
649 * inode size would still be 0, but there would be many pages which are beyond
650 * the inode size, they would be indexed and consume flash space. Because the
651 * journal has been committed, the replay would not be able to detect this
652 * situation and correct the inode size. This means UBIFS would have to scan
653 * whole index and correct all inode sizes, which is long an unacceptable.
655 * To prevent situations like this, UBIFS writes pages back only if they are
656 * within last synchronized inode size, i.e. the the size which has been
657 * written to the flash media last time. Otherwise, UBIFS forces inode
658 * write-back, thus making sure the on-flash inode contains current inode size,
659 * and then keeps writing pages back.
661 * Some locking issues explanation. 'ubifs_writepage()' first is called with
662 * the page locked, and it locks @ui_mutex. However, write-back does take inode
663 * @i_mutex, which means other VFS operations may be run on this inode at the
664 * same time. And the problematic one is truncation to smaller size, from where
665 * we have to call 'vmtruncate()', which first changes @inode->i_size, then
666 * drops the truncated pages. And while dropping the pages, it takes the page
667 * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with
668 * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This
669 * means that @inode->i_size is changed while @ui_mutex is unlocked.
671 * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
672 * inode size. How do we do this if @inode->i_size may became smaller while we
673 * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
674 * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
675 * internally and updates it under @ui_mutex.
677 * Q: why we do not worry that if we race with truncation, we may end up with a
678 * situation when the inode is truncated while we are in the middle of
679 * 'do_writepage()', so we do write beyond inode size?
680 * A: If we are in the middle of 'do_writepage()', truncation would be locked
681 * on the page lock and it would not write the truncated inode node to the
682 * journal before we have finished.
684 static int ubifs_writepage(struct page
*page
, struct writeback_control
*wbc
)
686 struct inode
*inode
= page
->mapping
->host
;
687 struct ubifs_inode
*ui
= ubifs_inode(inode
);
688 loff_t i_size
= i_size_read(inode
), synced_i_size
;
689 pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
690 int err
, len
= i_size
& (PAGE_CACHE_SIZE
- 1);
693 dbg_gen("ino %lu, pg %lu, pg flags %#lx",
694 inode
->i_ino
, page
->index
, page
->flags
);
695 ubifs_assert(PagePrivate(page
));
697 /* Is the page fully outside @i_size? (truncate in progress) */
698 if (page
->index
> end_index
|| (page
->index
== end_index
&& !len
)) {
703 spin_lock(&ui
->ui_lock
);
704 synced_i_size
= ui
->synced_i_size
;
705 spin_unlock(&ui
->ui_lock
);
707 /* Is the page fully inside @i_size? */
708 if (page
->index
< end_index
) {
709 if (page
->index
>= synced_i_size
>> PAGE_CACHE_SHIFT
) {
710 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
714 * The inode has been written, but the write-buffer has
715 * not been synchronized, so in case of an unclean
716 * reboot we may end up with some pages beyond inode
717 * size, but they would be in the journal (because
718 * commit flushes write buffers) and recovery would deal
722 return do_writepage(page
, PAGE_CACHE_SIZE
);
726 * The page straddles @i_size. It must be zeroed out on each and every
727 * writepage invocation because it may be mmapped. "A file is mapped
728 * in multiples of the page size. For a file that is not a multiple of
729 * the page size, the remaining memory is zeroed when mapped, and
730 * writes to that region are not written out to the file."
732 kaddr
= kmap_atomic(page
, KM_USER0
);
733 memset(kaddr
+ len
, 0, PAGE_CACHE_SIZE
- len
);
734 flush_dcache_page(page
);
735 kunmap_atomic(kaddr
, KM_USER0
);
737 if (i_size
> synced_i_size
) {
738 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
743 return do_writepage(page
, len
);
751 * do_attr_changes - change inode attributes.
752 * @inode: inode to change attributes for
753 * @attr: describes attributes to change
755 static void do_attr_changes(struct inode
*inode
, const struct iattr
*attr
)
757 if (attr
->ia_valid
& ATTR_UID
)
758 inode
->i_uid
= attr
->ia_uid
;
759 if (attr
->ia_valid
& ATTR_GID
)
760 inode
->i_gid
= attr
->ia_gid
;
761 if (attr
->ia_valid
& ATTR_ATIME
)
762 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
763 inode
->i_sb
->s_time_gran
);
764 if (attr
->ia_valid
& ATTR_MTIME
)
765 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
766 inode
->i_sb
->s_time_gran
);
767 if (attr
->ia_valid
& ATTR_CTIME
)
768 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
769 inode
->i_sb
->s_time_gran
);
770 if (attr
->ia_valid
& ATTR_MODE
) {
771 umode_t mode
= attr
->ia_mode
;
773 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
775 inode
->i_mode
= mode
;
780 * do_truncation - truncate an inode.
781 * @c: UBIFS file-system description object
782 * @inode: inode to truncate
783 * @attr: inode attribute changes description
785 * This function implements VFS '->setattr()' call when the inode is truncated
786 * to a smaller size. Returns zero in case of success and a negative error code
787 * in case of failure.
789 static int do_truncation(struct ubifs_info
*c
, struct inode
*inode
,
790 const struct iattr
*attr
)
793 struct ubifs_budget_req req
;
794 loff_t old_size
= inode
->i_size
, new_size
= attr
->ia_size
;
795 int offset
= new_size
& (UBIFS_BLOCK_SIZE
- 1);
796 struct ubifs_inode
*ui
= ubifs_inode(inode
);
798 dbg_gen("ino %lu, size %lld -> %lld", inode
->i_ino
, old_size
, new_size
);
799 memset(&req
, 0, sizeof(struct ubifs_budget_req
));
802 * If this is truncation to a smaller size, and we do not truncate on a
803 * block boundary, budget for changing one data block, because the last
804 * block will be re-written.
806 if (new_size
& (UBIFS_BLOCK_SIZE
- 1))
807 req
.dirtied_page
= 1;
810 /* A funny way to budget for truncation node */
811 req
.dirtied_ino_d
= UBIFS_TRUN_NODE_SZ
;
812 err
= ubifs_budget_space(c
, &req
);
816 err
= vmtruncate(inode
, new_size
);
821 pgoff_t index
= new_size
>> PAGE_CACHE_SHIFT
;
824 page
= find_lock_page(inode
->i_mapping
, index
);
826 if (PageDirty(page
)) {
828 * 'ubifs_jnl_truncate()' will try to truncate
829 * the last data node, but it contains
830 * out-of-date data because the page is dirty.
831 * Write the page now, so that
832 * 'ubifs_jnl_truncate()' will see an already
833 * truncated (and up to date) data node.
835 ubifs_assert(PagePrivate(page
));
837 clear_page_dirty_for_io(page
);
838 if (UBIFS_BLOCKS_PER_PAGE_SHIFT
)
840 (PAGE_CACHE_SIZE
- 1);
841 err
= do_writepage(page
, offset
);
842 page_cache_release(page
);
846 * We could now tell 'ubifs_jnl_truncate()' not
847 * to read the last block.
851 * We could 'kmap()' the page and pass the data
852 * to 'ubifs_jnl_truncate()' to save it from
856 page_cache_release(page
);
861 mutex_lock(&ui
->ui_mutex
);
862 ui
->ui_size
= inode
->i_size
;
863 /* Truncation changes inode [mc]time */
864 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
865 /* The other attributes may be changed at the same time as well */
866 do_attr_changes(inode
, attr
);
868 err
= ubifs_jnl_truncate(c
, inode
, old_size
, new_size
);
869 mutex_unlock(&ui
->ui_mutex
);
871 ubifs_release_budget(c
, &req
);
876 * do_setattr - change inode attributes.
877 * @c: UBIFS file-system description object
878 * @inode: inode to change attributes for
879 * @attr: inode attribute changes description
881 * This function implements VFS '->setattr()' call for all cases except
882 * truncations to smaller size. Returns zero in case of success and a negative
883 * error code in case of failure.
885 static int do_setattr(struct ubifs_info
*c
, struct inode
*inode
,
886 const struct iattr
*attr
)
889 loff_t new_size
= attr
->ia_size
;
890 struct ubifs_inode
*ui
= ubifs_inode(inode
);
891 struct ubifs_budget_req req
= { .dirtied_ino
= 1,
892 .dirtied_ino_d
= ui
->data_len
};
894 err
= ubifs_budget_space(c
, &req
);
898 if (attr
->ia_valid
& ATTR_SIZE
) {
899 dbg_gen("size %lld -> %lld", inode
->i_size
, new_size
);
900 err
= vmtruncate(inode
, new_size
);
905 mutex_lock(&ui
->ui_mutex
);
906 if (attr
->ia_valid
& ATTR_SIZE
) {
907 /* Truncation changes inode [mc]time */
908 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
909 /* 'vmtruncate()' changed @i_size, update @ui_size */
910 ui
->ui_size
= inode
->i_size
;
913 do_attr_changes(inode
, attr
);
916 if (attr
->ia_valid
& ATTR_SIZE
)
918 * Inode length changed, so we have to make sure
919 * @I_DIRTY_DATASYNC is set.
921 __mark_inode_dirty(inode
, I_DIRTY_SYNC
| I_DIRTY_DATASYNC
);
923 mark_inode_dirty_sync(inode
);
924 mutex_unlock(&ui
->ui_mutex
);
927 ubifs_release_budget(c
, &req
);
929 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
933 ubifs_release_budget(c
, &req
);
937 int ubifs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
940 struct inode
*inode
= dentry
->d_inode
;
941 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
943 dbg_gen("ino %lu, ia_valid %#x", inode
->i_ino
, attr
->ia_valid
);
944 err
= inode_change_ok(inode
, attr
);
948 err
= dbg_check_synced_i_size(inode
);
952 if ((attr
->ia_valid
& ATTR_SIZE
) && attr
->ia_size
< inode
->i_size
)
953 /* Truncation to a smaller size */
954 err
= do_truncation(c
, inode
, attr
);
956 err
= do_setattr(c
, inode
, attr
);
961 static void ubifs_invalidatepage(struct page
*page
, unsigned long offset
)
963 struct inode
*inode
= page
->mapping
->host
;
964 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
966 ubifs_assert(PagePrivate(page
));
968 /* Partial page remains dirty */
971 if (PageChecked(page
))
972 release_new_page_budget(c
);
974 release_existing_page_budget(c
);
976 atomic_long_dec(&c
->dirty_pg_cnt
);
977 ClearPagePrivate(page
);
978 ClearPageChecked(page
);
981 static void *ubifs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
983 struct ubifs_inode
*ui
= ubifs_inode(dentry
->d_inode
);
985 nd_set_link(nd
, ui
->data
);
989 int ubifs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
991 struct inode
*inode
= dentry
->d_inode
;
992 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
995 dbg_gen("syncing inode %lu", inode
->i_ino
);
998 * VFS has already synchronized dirty pages for this inode. Synchronize
999 * the inode unless this is a 'datasync()' call.
1001 if (!datasync
|| (inode
->i_state
& I_DIRTY_DATASYNC
)) {
1002 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
1008 * Nodes related to this inode may still sit in a write-buffer. Flush
1011 err
= ubifs_sync_wbufs_by_inode(c
, inode
);
1019 * mctime_update_needed - check if mtime or ctime update is needed.
1020 * @inode: the inode to do the check for
1021 * @now: current time
1023 * This helper function checks if the inode mtime/ctime should be updated or
1024 * not. If current values of the time-stamps are within the UBIFS inode time
1025 * granularity, they are not updated. This is an optimization.
1027 static inline int mctime_update_needed(const struct inode
*inode
,
1028 const struct timespec
*now
)
1030 if (!timespec_equal(&inode
->i_mtime
, now
) ||
1031 !timespec_equal(&inode
->i_ctime
, now
))
1037 * update_ctime - update mtime and ctime of an inode.
1038 * @c: UBIFS file-system description object
1039 * @inode: inode to update
1041 * This function updates mtime and ctime of the inode if it is not equivalent to
1042 * current time. Returns zero in case of success and a negative error code in
1045 static int update_mctime(struct ubifs_info
*c
, struct inode
*inode
)
1047 struct timespec now
= ubifs_current_time(inode
);
1048 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1050 if (mctime_update_needed(inode
, &now
)) {
1052 struct ubifs_budget_req req
= { .dirtied_ino
= 1,
1053 .dirtied_ino_d
= ui
->data_len
};
1055 err
= ubifs_budget_space(c
, &req
);
1059 mutex_lock(&ui
->ui_mutex
);
1060 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
1061 release
= ui
->dirty
;
1062 mark_inode_dirty_sync(inode
);
1063 mutex_unlock(&ui
->ui_mutex
);
1065 ubifs_release_budget(c
, &req
);
1071 static ssize_t
ubifs_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1072 unsigned long nr_segs
, loff_t pos
)
1076 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
1077 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
1079 err
= update_mctime(c
, inode
);
1083 ret
= generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
1087 if (ret
> 0 && (IS_SYNC(inode
) || iocb
->ki_filp
->f_flags
& O_SYNC
)) {
1088 err
= ubifs_sync_wbufs_by_inode(c
, inode
);
1096 static int ubifs_set_page_dirty(struct page
*page
)
1100 ret
= __set_page_dirty_nobuffers(page
);
1102 * An attempt to dirty a page without budgeting for it - should not
1105 ubifs_assert(ret
== 0);
1109 static int ubifs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1112 * An attempt to release a dirty page without budgeting for it - should
1115 if (PageWriteback(page
))
1117 ubifs_assert(PagePrivate(page
));
1119 ClearPagePrivate(page
);
1120 ClearPageChecked(page
);
1125 * mmap()d file has taken write protection fault and is being made
1126 * writable. UBIFS must ensure page is budgeted for.
1128 static int ubifs_vm_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1130 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1131 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
1132 struct timespec now
= ubifs_current_time(inode
);
1133 struct ubifs_budget_req req
= { .new_page
= 1 };
1134 int err
, update_time
;
1136 dbg_gen("ino %lu, pg %lu, i_size %lld", inode
->i_ino
, page
->index
,
1137 i_size_read(inode
));
1138 ubifs_assert(!(inode
->i_sb
->s_flags
& MS_RDONLY
));
1140 if (unlikely(c
->ro_media
))
1144 * We have not locked @page so far so we may budget for changing the
1145 * page. Note, we cannot do this after we locked the page, because
1146 * budgeting may cause write-back which would cause deadlock.
1148 * At the moment we do not know whether the page is dirty or not, so we
1149 * assume that it is not and budget for a new page. We could look at
1150 * the @PG_private flag and figure this out, but we may race with write
1151 * back and the page state may change by the time we lock it, so this
1152 * would need additional care. We do not bother with this at the
1153 * moment, although it might be good idea to do. Instead, we allocate
1154 * budget for a new page and amend it later on if the page was in fact
1157 * The budgeting-related logic of this function is similar to what we
1158 * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
1159 * for more comments.
1161 update_time
= mctime_update_needed(inode
, &now
);
1164 * We have to change inode time stamp which requires extra
1167 req
.dirtied_ino
= 1;
1169 err
= ubifs_budget_space(c
, &req
);
1170 if (unlikely(err
)) {
1172 ubifs_warn("out of space for mmapped file "
1173 "(inode number %lu)", inode
->i_ino
);
1178 if (unlikely(page
->mapping
!= inode
->i_mapping
||
1179 page_offset(page
) > i_size_read(inode
))) {
1180 /* Page got truncated out from underneath us */
1185 if (PagePrivate(page
))
1186 release_new_page_budget(c
);
1188 if (!PageChecked(page
))
1189 ubifs_convert_page_budget(c
);
1190 SetPagePrivate(page
);
1191 atomic_long_inc(&c
->dirty_pg_cnt
);
1192 __set_page_dirty_nobuffers(page
);
1197 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1199 mutex_lock(&ui
->ui_mutex
);
1200 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
1201 release
= ui
->dirty
;
1202 mark_inode_dirty_sync(inode
);
1203 mutex_unlock(&ui
->ui_mutex
);
1205 ubifs_release_dirty_inode_budget(c
, ui
);
1213 ubifs_release_budget(c
, &req
);
1217 static struct vm_operations_struct ubifs_file_vm_ops
= {
1218 .fault
= filemap_fault
,
1219 .page_mkwrite
= ubifs_vm_page_mkwrite
,
1222 static int ubifs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1226 /* 'generic_file_mmap()' takes care of NOMMU case */
1227 err
= generic_file_mmap(file
, vma
);
1230 vma
->vm_ops
= &ubifs_file_vm_ops
;
1234 struct address_space_operations ubifs_file_address_operations
= {
1235 .readpage
= ubifs_readpage
,
1236 .writepage
= ubifs_writepage
,
1237 .write_begin
= ubifs_write_begin
,
1238 .write_end
= ubifs_write_end
,
1239 .invalidatepage
= ubifs_invalidatepage
,
1240 .set_page_dirty
= ubifs_set_page_dirty
,
1241 .releasepage
= ubifs_releasepage
,
1244 struct inode_operations ubifs_file_inode_operations
= {
1245 .setattr
= ubifs_setattr
,
1246 .getattr
= ubifs_getattr
,
1247 #ifdef CONFIG_UBIFS_FS_XATTR
1248 .setxattr
= ubifs_setxattr
,
1249 .getxattr
= ubifs_getxattr
,
1250 .listxattr
= ubifs_listxattr
,
1251 .removexattr
= ubifs_removexattr
,
1255 struct inode_operations ubifs_symlink_inode_operations
= {
1256 .readlink
= generic_readlink
,
1257 .follow_link
= ubifs_follow_link
,
1258 .setattr
= ubifs_setattr
,
1259 .getattr
= ubifs_getattr
,
1262 struct file_operations ubifs_file_operations
= {
1263 .llseek
= generic_file_llseek
,
1264 .read
= do_sync_read
,
1265 .write
= do_sync_write
,
1266 .aio_read
= generic_file_aio_read
,
1267 .aio_write
= ubifs_aio_write
,
1268 .mmap
= ubifs_file_mmap
,
1269 .fsync
= ubifs_fsync
,
1270 .unlocked_ioctl
= ubifs_ioctl
,
1271 .splice_read
= generic_file_splice_read
,
1272 #ifdef CONFIG_COMPAT
1273 .compat_ioctl
= ubifs_compat_ioctl
,