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>
56 #include <linux/namei.h>
58 static int read_block(struct inode
*inode
, void *addr
, unsigned int block
,
59 struct ubifs_data_node
*dn
)
61 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
62 int err
, len
, out_len
;
66 data_key_init(c
, &key
, inode
->i_ino
, block
);
67 err
= ubifs_tnc_lookup(c
, &key
, dn
);
70 /* Not found, so it must be a hole */
71 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
75 ubifs_assert(dn
->ch
.sqnum
> ubifs_inode(inode
)->creat_sqnum
);
77 len
= le32_to_cpu(dn
->size
);
78 if (len
<= 0 || len
> UBIFS_BLOCK_SIZE
)
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
)
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.
93 if (len
< UBIFS_BLOCK_SIZE
)
94 memset(addr
+ len
, 0, UBIFS_BLOCK_SIZE
- len
);
99 ubifs_err("bad data node (block %u, inode %lu)",
100 block
, inode
->i_ino
);
101 dbg_dump_node(c
, dn
);
105 static int do_readpage(struct page
*page
)
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
);
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
));
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
);
130 dn
= kmalloc(UBIFS_MAX_DATA_NODE_SZ
, GFP_NOFS
);
140 if (block
>= beyond
) {
141 /* Reading beyond inode */
143 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
145 ret
= read_block(inode
, addr
, block
, dn
);
152 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
155 addr
+= UBIFS_BLOCK_SIZE
;
158 if (err
== -ENOENT
) {
159 /* Not found, so it must be a hole */
160 SetPageChecked(page
);
164 ubifs_err("cannot read page %lu of inode %lu, error %d",
165 page
->index
, inode
->i_ino
, err
);
172 SetPageUptodate(page
);
173 ClearPageError(page
);
174 flush_dcache_page(page
);
180 ClearPageUptodate(page
);
182 flush_dcache_page(page
);
188 * release_new_page_budget - release budget of a new page.
189 * @c: UBIFS file-system description object
191 * This is a helper function which releases budget corresponding to the budget
192 * of one new page of data.
194 static void release_new_page_budget(struct ubifs_info
*c
)
196 struct ubifs_budget_req req
= { .recalculate
= 1, .new_page
= 1 };
198 ubifs_release_budget(c
, &req
);
202 * release_existing_page_budget - release budget of an existing page.
203 * @c: UBIFS file-system description object
205 * This is a helper function which releases budget corresponding to the budget
206 * of changing one one page of data which already exists on the flash media.
208 static void release_existing_page_budget(struct ubifs_info
*c
)
210 struct ubifs_budget_req req
= { .dd_growth
= c
->page_budget
};
212 ubifs_release_budget(c
, &req
);
215 static int write_begin_slow(struct address_space
*mapping
,
216 loff_t pos
, unsigned len
, struct page
**pagep
)
218 struct inode
*inode
= mapping
->host
;
219 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
220 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
221 struct ubifs_budget_req req
= { .new_page
= 1 };
222 int uninitialized_var(err
), appending
= !!(pos
+ len
> inode
->i_size
);
225 dbg_gen("ino %lu, pos %llu, len %u, i_size %lld",
226 inode
->i_ino
, pos
, len
, inode
->i_size
);
229 * At the slow path we have to budget before locking the page, because
230 * budgeting may force write-back, which would wait on locked pages and
231 * deadlock if we had the page locked. At this point we do not know
232 * anything about the page, so assume that this is a new page which is
233 * written to a hole. This corresponds to largest budget. Later the
234 * budget will be amended if this is not true.
237 /* We are appending data, budget for inode change */
240 err
= ubifs_budget_space(c
, &req
);
244 page
= __grab_cache_page(mapping
, index
);
245 if (unlikely(!page
)) {
246 ubifs_release_budget(c
, &req
);
250 if (!PageUptodate(page
)) {
251 if (!(pos
& PAGE_CACHE_MASK
) && len
== PAGE_CACHE_SIZE
)
252 SetPageChecked(page
);
254 err
= do_readpage(page
);
257 page_cache_release(page
);
262 SetPageUptodate(page
);
263 ClearPageError(page
);
266 if (PagePrivate(page
))
268 * The page is dirty, which means it was budgeted twice:
269 * o first time the budget was allocated by the task which
270 * made the page dirty and set the PG_private flag;
271 * o and then we budgeted for it for the second time at the
272 * very beginning of this function.
274 * So what we have to do is to release the page budget we
277 release_new_page_budget(c
);
278 else if (!PageChecked(page
))
280 * We are changing a page which already exists on the media.
281 * This means that changing the page does not make the amount
282 * of indexing information larger, and this part of the budget
283 * which we have already acquired may be released.
285 ubifs_convert_page_budget(c
);
288 struct ubifs_inode
*ui
= ubifs_inode(inode
);
291 * 'ubifs_write_end()' is optimized from the fast-path part of
292 * 'ubifs_write_begin()' and expects the @ui_mutex to be locked
293 * if data is appended.
295 mutex_lock(&ui
->ui_mutex
);
298 * The inode is dirty already, so we may free the
299 * budget we allocated.
301 ubifs_release_dirty_inode_budget(c
, ui
);
309 * allocate_budget - allocate budget for 'ubifs_write_begin()'.
310 * @c: UBIFS file-system description object
311 * @page: page to allocate budget for
312 * @ui: UBIFS inode object the page belongs to
313 * @appending: non-zero if the page is appended
315 * This is a helper function for 'ubifs_write_begin()' which allocates budget
316 * for the operation. The budget is allocated differently depending on whether
317 * this is appending, whether the page is dirty or not, and so on. This
318 * function leaves the @ui->ui_mutex locked in case of appending. Returns zero
319 * in case of success and %-ENOSPC in case of failure.
321 static int allocate_budget(struct ubifs_info
*c
, struct page
*page
,
322 struct ubifs_inode
*ui
, int appending
)
324 struct ubifs_budget_req req
= { .fast
= 1 };
326 if (PagePrivate(page
)) {
329 * The page is dirty and we are not appending, which
330 * means no budget is needed at all.
334 mutex_lock(&ui
->ui_mutex
);
337 * The page is dirty and we are appending, so the inode
338 * has to be marked as dirty. However, it is already
339 * dirty, so we do not need any budget. We may return,
340 * but @ui->ui_mutex hast to be left locked because we
341 * should prevent write-back from flushing the inode
342 * and freeing the budget. The lock will be released in
343 * 'ubifs_write_end()'.
348 * The page is dirty, we are appending, the inode is clean, so
349 * we need to budget the inode change.
353 if (PageChecked(page
))
355 * The page corresponds to a hole and does not
356 * exist on the media. So changing it makes
357 * make the amount of indexing information
358 * larger, and we have to budget for a new
364 * Not a hole, the change will not add any new
365 * indexing information, budget for page
368 req
.dirtied_page
= 1;
371 mutex_lock(&ui
->ui_mutex
);
374 * The inode is clean but we will have to mark
375 * it as dirty because we are appending. This
382 return ubifs_budget_space(c
, &req
);
386 * This function is called when a page of data is going to be written. Since
387 * the page of data will not necessarily go to the flash straight away, UBIFS
388 * has to reserve space on the media for it, which is done by means of
391 * This is the hot-path of the file-system and we are trying to optimize it as
392 * much as possible. For this reasons it is split on 2 parts - slow and fast.
394 * There many budgeting cases:
395 * o a new page is appended - we have to budget for a new page and for
396 * changing the inode; however, if the inode is already dirty, there is
397 * no need to budget for it;
398 * o an existing clean page is changed - we have budget for it; if the page
399 * does not exist on the media (a hole), we have to budget for a new
400 * page; otherwise, we may budget for changing an existing page; the
401 * difference between these cases is that changing an existing page does
402 * not introduce anything new to the FS indexing information, so it does
403 * not grow, and smaller budget is acquired in this case;
404 * o an existing dirty page is changed - no need to budget at all, because
405 * the page budget has been acquired by earlier, when the page has been
408 * UBIFS budgeting sub-system may force write-back if it thinks there is no
409 * space to reserve. This imposes some locking restrictions and makes it
410 * impossible to take into account the above cases, and makes it impossible to
411 * optimize budgeting.
413 * The solution for this is that the fast path of 'ubifs_write_begin()' assumes
414 * there is a plenty of flash space and the budget will be acquired quickly,
415 * without forcing write-back. The slow path does not make this assumption.
417 static int ubifs_write_begin(struct file
*file
, struct address_space
*mapping
,
418 loff_t pos
, unsigned len
, unsigned flags
,
419 struct page
**pagep
, void **fsdata
)
421 struct inode
*inode
= mapping
->host
;
422 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
423 struct ubifs_inode
*ui
= ubifs_inode(inode
);
424 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
425 int uninitialized_var(err
), appending
= !!(pos
+ len
> inode
->i_size
);
429 ubifs_assert(ubifs_inode(inode
)->ui_size
== inode
->i_size
);
431 if (unlikely(c
->ro_media
))
434 /* Try out the fast-path part first */
435 page
= __grab_cache_page(mapping
, index
);
439 if (!PageUptodate(page
)) {
440 /* The page is not loaded from the flash */
441 if (!(pos
& PAGE_CACHE_MASK
) && len
== PAGE_CACHE_SIZE
)
443 * We change whole page so no need to load it. But we
444 * have to set the @PG_checked flag to make the further
445 * code the page is new. This might be not true, but it
446 * is better to budget more that to read the page from
449 SetPageChecked(page
);
451 err
= do_readpage(page
);
454 page_cache_release(page
);
459 SetPageUptodate(page
);
460 ClearPageError(page
);
463 err
= allocate_budget(c
, page
, ui
, appending
);
465 ubifs_assert(err
== -ENOSPC
);
467 * Budgeting failed which means it would have to force
468 * write-back but didn't, because we set the @fast flag in the
469 * request. Write-back cannot be done now, while we have the
470 * page locked, because it would deadlock. Unlock and free
471 * everything and fall-back to slow-path.
474 ubifs_assert(mutex_is_locked(&ui
->ui_mutex
));
475 mutex_unlock(&ui
->ui_mutex
);
478 page_cache_release(page
);
480 return write_begin_slow(mapping
, pos
, len
, pagep
);
484 * Whee, we aquired budgeting quickly - without involving
485 * garbage-collection, committing or forceing write-back. We return
486 * with @ui->ui_mutex locked if we are appending pages, and unlocked
487 * otherwise. This is an optimization (slightly hacky though).
495 * cancel_budget - cancel budget.
496 * @c: UBIFS file-system description object
497 * @page: page to cancel budget for
498 * @ui: UBIFS inode object the page belongs to
499 * @appending: non-zero if the page is appended
501 * This is a helper function for a page write operation. It unlocks the
502 * @ui->ui_mutex in case of appending.
504 static void cancel_budget(struct ubifs_info
*c
, struct page
*page
,
505 struct ubifs_inode
*ui
, int appending
)
509 ubifs_release_dirty_inode_budget(c
, ui
);
510 mutex_unlock(&ui
->ui_mutex
);
512 if (!PagePrivate(page
)) {
513 if (PageChecked(page
))
514 release_new_page_budget(c
);
516 release_existing_page_budget(c
);
520 static int ubifs_write_end(struct file
*file
, struct address_space
*mapping
,
521 loff_t pos
, unsigned len
, unsigned copied
,
522 struct page
*page
, void *fsdata
)
524 struct inode
*inode
= mapping
->host
;
525 struct ubifs_inode
*ui
= ubifs_inode(inode
);
526 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
527 loff_t end_pos
= pos
+ len
;
528 int appending
= !!(end_pos
> inode
->i_size
);
530 dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
531 inode
->i_ino
, pos
, page
->index
, len
, copied
, inode
->i_size
);
533 if (unlikely(copied
< len
&& len
== PAGE_CACHE_SIZE
)) {
535 * VFS copied less data to the page that it intended and
536 * declared in its '->write_begin()' call via the @len
537 * argument. If the page was not up-to-date, and @len was
538 * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did
539 * not load it from the media (for optimization reasons). This
540 * means that part of the page contains garbage. So read the
543 dbg_gen("copied %d instead of %d, read page and repeat",
545 cancel_budget(c
, page
, ui
, appending
);
548 * Return 0 to force VFS to repeat the whole operation, or the
549 * error code if 'do_readpage()' failes.
551 copied
= do_readpage(page
);
555 if (!PagePrivate(page
)) {
556 SetPagePrivate(page
);
557 atomic_long_inc(&c
->dirty_pg_cnt
);
558 __set_page_dirty_nobuffers(page
);
562 i_size_write(inode
, end_pos
);
563 ui
->ui_size
= end_pos
;
565 * Note, we do not set @I_DIRTY_PAGES (which means that the
566 * inode has dirty pages), this has been done in
567 * '__set_page_dirty_nobuffers()'.
569 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
570 ubifs_assert(mutex_is_locked(&ui
->ui_mutex
));
571 mutex_unlock(&ui
->ui_mutex
);
576 page_cache_release(page
);
580 static int ubifs_readpage(struct file
*file
, struct page
*page
)
587 static int do_writepage(struct page
*page
, int len
)
589 int err
= 0, i
, blen
;
593 struct inode
*inode
= page
->mapping
->host
;
594 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
597 spin_lock(&ui
->ui_lock
);
598 ubifs_assert(page
->index
<= ui
->synced_i_size
<< PAGE_CACHE_SIZE
);
599 spin_unlock(&ui
->ui_lock
);
602 /* Update radix tree tags */
603 set_page_writeback(page
);
606 block
= page
->index
<< UBIFS_BLOCKS_PER_PAGE_SHIFT
;
609 blen
= min_t(int, len
, UBIFS_BLOCK_SIZE
);
610 data_key_init(c
, &key
, inode
->i_ino
, block
);
611 err
= ubifs_jnl_write_data(c
, inode
, &key
, addr
, blen
);
614 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
622 ubifs_err("cannot write page %lu of inode %lu, error %d",
623 page
->index
, inode
->i_ino
, err
);
624 ubifs_ro_mode(c
, err
);
627 ubifs_assert(PagePrivate(page
));
628 if (PageChecked(page
))
629 release_new_page_budget(c
);
631 release_existing_page_budget(c
);
633 atomic_long_dec(&c
->dirty_pg_cnt
);
634 ClearPagePrivate(page
);
635 ClearPageChecked(page
);
639 end_page_writeback(page
);
644 * When writing-back dirty inodes, VFS first writes-back pages belonging to the
645 * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
646 * situation when a we have an inode with size 0, then a megabyte of data is
647 * appended to the inode, then write-back starts and flushes some amount of the
648 * dirty pages, the journal becomes full, commit happens and finishes, and then
649 * an unclean reboot happens. When the file system is mounted next time, the
650 * inode size would still be 0, but there would be many pages which are beyond
651 * the inode size, they would be indexed and consume flash space. Because the
652 * journal has been committed, the replay would not be able to detect this
653 * situation and correct the inode size. This means UBIFS would have to scan
654 * whole index and correct all inode sizes, which is long an unacceptable.
656 * To prevent situations like this, UBIFS writes pages back only if they are
657 * within last synchronized inode size, i.e. the the size which has been
658 * written to the flash media last time. Otherwise, UBIFS forces inode
659 * write-back, thus making sure the on-flash inode contains current inode size,
660 * and then keeps writing pages back.
662 * Some locking issues explanation. 'ubifs_writepage()' first is called with
663 * the page locked, and it locks @ui_mutex. However, write-back does take inode
664 * @i_mutex, which means other VFS operations may be run on this inode at the
665 * same time. And the problematic one is truncation to smaller size, from where
666 * we have to call 'vmtruncate()', which first changes @inode->i_size, then
667 * drops the truncated pages. And while dropping the pages, it takes the page
668 * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with
669 * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This
670 * means that @inode->i_size is changed while @ui_mutex is unlocked.
672 * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
673 * inode size. How do we do this if @inode->i_size may became smaller while we
674 * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
675 * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
676 * internally and updates it under @ui_mutex.
678 * Q: why we do not worry that if we race with truncation, we may end up with a
679 * situation when the inode is truncated while we are in the middle of
680 * 'do_writepage()', so we do write beyond inode size?
681 * A: If we are in the middle of 'do_writepage()', truncation would be locked
682 * on the page lock and it would not write the truncated inode node to the
683 * journal before we have finished.
685 static int ubifs_writepage(struct page
*page
, struct writeback_control
*wbc
)
687 struct inode
*inode
= page
->mapping
->host
;
688 struct ubifs_inode
*ui
= ubifs_inode(inode
);
689 loff_t i_size
= i_size_read(inode
), synced_i_size
;
690 pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
691 int err
, len
= i_size
& (PAGE_CACHE_SIZE
- 1);
694 dbg_gen("ino %lu, pg %lu, pg flags %#lx",
695 inode
->i_ino
, page
->index
, page
->flags
);
696 ubifs_assert(PagePrivate(page
));
698 /* Is the page fully outside @i_size? (truncate in progress) */
699 if (page
->index
> end_index
|| (page
->index
== end_index
&& !len
)) {
704 spin_lock(&ui
->ui_lock
);
705 synced_i_size
= ui
->synced_i_size
;
706 spin_unlock(&ui
->ui_lock
);
708 /* Is the page fully inside @i_size? */
709 if (page
->index
< end_index
) {
710 if (page
->index
>= synced_i_size
>> PAGE_CACHE_SHIFT
) {
711 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
715 * The inode has been written, but the write-buffer has
716 * not been synchronized, so in case of an unclean
717 * reboot we may end up with some pages beyond inode
718 * size, but they would be in the journal (because
719 * commit flushes write buffers) and recovery would deal
723 return do_writepage(page
, PAGE_CACHE_SIZE
);
727 * The page straddles @i_size. It must be zeroed out on each and every
728 * writepage invocation because it may be mmapped. "A file is mapped
729 * in multiples of the page size. For a file that is not a multiple of
730 * the page size, the remaining memory is zeroed when mapped, and
731 * writes to that region are not written out to the file."
733 kaddr
= kmap_atomic(page
, KM_USER0
);
734 memset(kaddr
+ len
, 0, PAGE_CACHE_SIZE
- len
);
735 flush_dcache_page(page
);
736 kunmap_atomic(kaddr
, KM_USER0
);
738 if (i_size
> synced_i_size
) {
739 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
744 return do_writepage(page
, len
);
752 * do_attr_changes - change inode attributes.
753 * @inode: inode to change attributes for
754 * @attr: describes attributes to change
756 static void do_attr_changes(struct inode
*inode
, const struct iattr
*attr
)
758 if (attr
->ia_valid
& ATTR_UID
)
759 inode
->i_uid
= attr
->ia_uid
;
760 if (attr
->ia_valid
& ATTR_GID
)
761 inode
->i_gid
= attr
->ia_gid
;
762 if (attr
->ia_valid
& ATTR_ATIME
)
763 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
764 inode
->i_sb
->s_time_gran
);
765 if (attr
->ia_valid
& ATTR_MTIME
)
766 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
767 inode
->i_sb
->s_time_gran
);
768 if (attr
->ia_valid
& ATTR_CTIME
)
769 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
770 inode
->i_sb
->s_time_gran
);
771 if (attr
->ia_valid
& ATTR_MODE
) {
772 umode_t mode
= attr
->ia_mode
;
774 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
776 inode
->i_mode
= mode
;
781 * do_truncation - truncate an inode.
782 * @c: UBIFS file-system description object
783 * @inode: inode to truncate
784 * @attr: inode attribute changes description
786 * This function implements VFS '->setattr()' call when the inode is truncated
787 * to a smaller size. Returns zero in case of success and a negative error code
788 * in case of failure.
790 static int do_truncation(struct ubifs_info
*c
, struct inode
*inode
,
791 const struct iattr
*attr
)
794 struct ubifs_budget_req req
;
795 loff_t old_size
= inode
->i_size
, new_size
= attr
->ia_size
;
796 int offset
= new_size
& (UBIFS_BLOCK_SIZE
- 1), budgeted
= 1;
797 struct ubifs_inode
*ui
= ubifs_inode(inode
);
799 dbg_gen("ino %lu, size %lld -> %lld", inode
->i_ino
, old_size
, new_size
);
800 memset(&req
, 0, sizeof(struct ubifs_budget_req
));
803 * If this is truncation to a smaller size, and we do not truncate on a
804 * block boundary, budget for changing one data block, because the last
805 * block will be re-written.
807 if (new_size
& (UBIFS_BLOCK_SIZE
- 1))
808 req
.dirtied_page
= 1;
811 /* A funny way to budget for truncation node */
812 req
.dirtied_ino_d
= UBIFS_TRUN_NODE_SZ
;
813 err
= ubifs_budget_space(c
, &req
);
816 * Treat truncations to zero as deletion and always allow them,
817 * just like we do for '->unlink()'.
819 if (new_size
|| err
!= -ENOSPC
)
824 err
= vmtruncate(inode
, new_size
);
829 pgoff_t index
= new_size
>> PAGE_CACHE_SHIFT
;
832 page
= find_lock_page(inode
->i_mapping
, index
);
834 if (PageDirty(page
)) {
836 * 'ubifs_jnl_truncate()' will try to truncate
837 * the last data node, but it contains
838 * out-of-date data because the page is dirty.
839 * Write the page now, so that
840 * 'ubifs_jnl_truncate()' will see an already
841 * truncated (and up to date) data node.
843 ubifs_assert(PagePrivate(page
));
845 clear_page_dirty_for_io(page
);
846 if (UBIFS_BLOCKS_PER_PAGE_SHIFT
)
848 (PAGE_CACHE_SIZE
- 1);
849 err
= do_writepage(page
, offset
);
850 page_cache_release(page
);
854 * We could now tell 'ubifs_jnl_truncate()' not
855 * to read the last block.
859 * We could 'kmap()' the page and pass the data
860 * to 'ubifs_jnl_truncate()' to save it from
864 page_cache_release(page
);
869 mutex_lock(&ui
->ui_mutex
);
870 ui
->ui_size
= inode
->i_size
;
871 /* Truncation changes inode [mc]time */
872 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
873 /* The other attributes may be changed at the same time as well */
874 do_attr_changes(inode
, attr
);
876 err
= ubifs_jnl_truncate(c
, inode
, old_size
, new_size
);
877 mutex_unlock(&ui
->ui_mutex
);
880 ubifs_release_budget(c
, &req
);
882 c
->nospace
= c
->nospace_rp
= 0;
889 * do_setattr - change inode attributes.
890 * @c: UBIFS file-system description object
891 * @inode: inode to change attributes for
892 * @attr: inode attribute changes description
894 * This function implements VFS '->setattr()' call for all cases except
895 * truncations to smaller size. Returns zero in case of success and a negative
896 * error code in case of failure.
898 static int do_setattr(struct ubifs_info
*c
, struct inode
*inode
,
899 const struct iattr
*attr
)
902 loff_t new_size
= attr
->ia_size
;
903 struct ubifs_inode
*ui
= ubifs_inode(inode
);
904 struct ubifs_budget_req req
= { .dirtied_ino
= 1,
905 .dirtied_ino_d
= ALIGN(ui
->data_len
, 8) };
907 err
= ubifs_budget_space(c
, &req
);
911 if (attr
->ia_valid
& ATTR_SIZE
) {
912 dbg_gen("size %lld -> %lld", inode
->i_size
, new_size
);
913 err
= vmtruncate(inode
, new_size
);
918 mutex_lock(&ui
->ui_mutex
);
919 if (attr
->ia_valid
& ATTR_SIZE
) {
920 /* Truncation changes inode [mc]time */
921 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
922 /* 'vmtruncate()' changed @i_size, update @ui_size */
923 ui
->ui_size
= inode
->i_size
;
926 do_attr_changes(inode
, attr
);
929 if (attr
->ia_valid
& ATTR_SIZE
)
931 * Inode length changed, so we have to make sure
932 * @I_DIRTY_DATASYNC is set.
934 __mark_inode_dirty(inode
, I_DIRTY_SYNC
| I_DIRTY_DATASYNC
);
936 mark_inode_dirty_sync(inode
);
937 mutex_unlock(&ui
->ui_mutex
);
940 ubifs_release_budget(c
, &req
);
942 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
946 ubifs_release_budget(c
, &req
);
950 int ubifs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
953 struct inode
*inode
= dentry
->d_inode
;
954 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
956 dbg_gen("ino %lu, mode %#x, ia_valid %#x",
957 inode
->i_ino
, inode
->i_mode
, attr
->ia_valid
);
958 err
= inode_change_ok(inode
, attr
);
962 err
= dbg_check_synced_i_size(inode
);
966 if ((attr
->ia_valid
& ATTR_SIZE
) && attr
->ia_size
< inode
->i_size
)
967 /* Truncation to a smaller size */
968 err
= do_truncation(c
, inode
, attr
);
970 err
= do_setattr(c
, inode
, attr
);
975 static void ubifs_invalidatepage(struct page
*page
, unsigned long offset
)
977 struct inode
*inode
= page
->mapping
->host
;
978 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
980 ubifs_assert(PagePrivate(page
));
982 /* Partial page remains dirty */
985 if (PageChecked(page
))
986 release_new_page_budget(c
);
988 release_existing_page_budget(c
);
990 atomic_long_dec(&c
->dirty_pg_cnt
);
991 ClearPagePrivate(page
);
992 ClearPageChecked(page
);
995 static void *ubifs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
997 struct ubifs_inode
*ui
= ubifs_inode(dentry
->d_inode
);
999 nd_set_link(nd
, ui
->data
);
1003 int ubifs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
1005 struct inode
*inode
= dentry
->d_inode
;
1006 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
1009 dbg_gen("syncing inode %lu", inode
->i_ino
);
1012 * VFS has already synchronized dirty pages for this inode. Synchronize
1013 * the inode unless this is a 'datasync()' call.
1015 if (!datasync
|| (inode
->i_state
& I_DIRTY_DATASYNC
)) {
1016 err
= inode
->i_sb
->s_op
->write_inode(inode
, 1);
1022 * Nodes related to this inode may still sit in a write-buffer. Flush
1025 err
= ubifs_sync_wbufs_by_inode(c
, inode
);
1033 * mctime_update_needed - check if mtime or ctime update is needed.
1034 * @inode: the inode to do the check for
1035 * @now: current time
1037 * This helper function checks if the inode mtime/ctime should be updated or
1038 * not. If current values of the time-stamps are within the UBIFS inode time
1039 * granularity, they are not updated. This is an optimization.
1041 static inline int mctime_update_needed(const struct inode
*inode
,
1042 const struct timespec
*now
)
1044 if (!timespec_equal(&inode
->i_mtime
, now
) ||
1045 !timespec_equal(&inode
->i_ctime
, now
))
1051 * update_ctime - update mtime and ctime of an inode.
1052 * @c: UBIFS file-system description object
1053 * @inode: inode to update
1055 * This function updates mtime and ctime of the inode if it is not equivalent to
1056 * current time. Returns zero in case of success and a negative error code in
1059 static int update_mctime(struct ubifs_info
*c
, struct inode
*inode
)
1061 struct timespec now
= ubifs_current_time(inode
);
1062 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1064 if (mctime_update_needed(inode
, &now
)) {
1066 struct ubifs_budget_req req
= { .dirtied_ino
= 1,
1067 .dirtied_ino_d
= ALIGN(ui
->data_len
, 8) };
1069 err
= ubifs_budget_space(c
, &req
);
1073 mutex_lock(&ui
->ui_mutex
);
1074 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
1075 release
= ui
->dirty
;
1076 mark_inode_dirty_sync(inode
);
1077 mutex_unlock(&ui
->ui_mutex
);
1079 ubifs_release_budget(c
, &req
);
1085 static ssize_t
ubifs_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1086 unsigned long nr_segs
, loff_t pos
)
1090 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
1091 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
1093 err
= update_mctime(c
, inode
);
1097 ret
= generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
1101 if (ret
> 0 && (IS_SYNC(inode
) || iocb
->ki_filp
->f_flags
& O_SYNC
)) {
1102 err
= ubifs_sync_wbufs_by_inode(c
, inode
);
1110 static int ubifs_set_page_dirty(struct page
*page
)
1114 ret
= __set_page_dirty_nobuffers(page
);
1116 * An attempt to dirty a page without budgeting for it - should not
1119 ubifs_assert(ret
== 0);
1123 static int ubifs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1126 * An attempt to release a dirty page without budgeting for it - should
1129 if (PageWriteback(page
))
1131 ubifs_assert(PagePrivate(page
));
1133 ClearPagePrivate(page
);
1134 ClearPageChecked(page
);
1139 * mmap()d file has taken write protection fault and is being made
1140 * writable. UBIFS must ensure page is budgeted for.
1142 static int ubifs_vm_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1144 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1145 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
1146 struct timespec now
= ubifs_current_time(inode
);
1147 struct ubifs_budget_req req
= { .new_page
= 1 };
1148 int err
, update_time
;
1150 dbg_gen("ino %lu, pg %lu, i_size %lld", inode
->i_ino
, page
->index
,
1151 i_size_read(inode
));
1152 ubifs_assert(!(inode
->i_sb
->s_flags
& MS_RDONLY
));
1154 if (unlikely(c
->ro_media
))
1158 * We have not locked @page so far so we may budget for changing the
1159 * page. Note, we cannot do this after we locked the page, because
1160 * budgeting may cause write-back which would cause deadlock.
1162 * At the moment we do not know whether the page is dirty or not, so we
1163 * assume that it is not and budget for a new page. We could look at
1164 * the @PG_private flag and figure this out, but we may race with write
1165 * back and the page state may change by the time we lock it, so this
1166 * would need additional care. We do not bother with this at the
1167 * moment, although it might be good idea to do. Instead, we allocate
1168 * budget for a new page and amend it later on if the page was in fact
1171 * The budgeting-related logic of this function is similar to what we
1172 * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
1173 * for more comments.
1175 update_time
= mctime_update_needed(inode
, &now
);
1178 * We have to change inode time stamp which requires extra
1181 req
.dirtied_ino
= 1;
1183 err
= ubifs_budget_space(c
, &req
);
1184 if (unlikely(err
)) {
1186 ubifs_warn("out of space for mmapped file "
1187 "(inode number %lu)", inode
->i_ino
);
1192 if (unlikely(page
->mapping
!= inode
->i_mapping
||
1193 page_offset(page
) > i_size_read(inode
))) {
1194 /* Page got truncated out from underneath us */
1199 if (PagePrivate(page
))
1200 release_new_page_budget(c
);
1202 if (!PageChecked(page
))
1203 ubifs_convert_page_budget(c
);
1204 SetPagePrivate(page
);
1205 atomic_long_inc(&c
->dirty_pg_cnt
);
1206 __set_page_dirty_nobuffers(page
);
1211 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1213 mutex_lock(&ui
->ui_mutex
);
1214 inode
->i_mtime
= inode
->i_ctime
= ubifs_current_time(inode
);
1215 release
= ui
->dirty
;
1216 mark_inode_dirty_sync(inode
);
1217 mutex_unlock(&ui
->ui_mutex
);
1219 ubifs_release_dirty_inode_budget(c
, ui
);
1227 ubifs_release_budget(c
, &req
);
1231 static struct vm_operations_struct ubifs_file_vm_ops
= {
1232 .fault
= filemap_fault
,
1233 .page_mkwrite
= ubifs_vm_page_mkwrite
,
1236 static int ubifs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1240 /* 'generic_file_mmap()' takes care of NOMMU case */
1241 err
= generic_file_mmap(file
, vma
);
1244 vma
->vm_ops
= &ubifs_file_vm_ops
;
1248 struct address_space_operations ubifs_file_address_operations
= {
1249 .readpage
= ubifs_readpage
,
1250 .writepage
= ubifs_writepage
,
1251 .write_begin
= ubifs_write_begin
,
1252 .write_end
= ubifs_write_end
,
1253 .invalidatepage
= ubifs_invalidatepage
,
1254 .set_page_dirty
= ubifs_set_page_dirty
,
1255 .releasepage
= ubifs_releasepage
,
1258 struct inode_operations ubifs_file_inode_operations
= {
1259 .setattr
= ubifs_setattr
,
1260 .getattr
= ubifs_getattr
,
1261 #ifdef CONFIG_UBIFS_FS_XATTR
1262 .setxattr
= ubifs_setxattr
,
1263 .getxattr
= ubifs_getxattr
,
1264 .listxattr
= ubifs_listxattr
,
1265 .removexattr
= ubifs_removexattr
,
1269 struct inode_operations ubifs_symlink_inode_operations
= {
1270 .readlink
= generic_readlink
,
1271 .follow_link
= ubifs_follow_link
,
1272 .setattr
= ubifs_setattr
,
1273 .getattr
= ubifs_getattr
,
1276 struct file_operations ubifs_file_operations
= {
1277 .llseek
= generic_file_llseek
,
1278 .read
= do_sync_read
,
1279 .write
= do_sync_write
,
1280 .aio_read
= generic_file_aio_read
,
1281 .aio_write
= ubifs_aio_write
,
1282 .mmap
= ubifs_file_mmap
,
1283 .fsync
= ubifs_fsync
,
1284 .unlocked_ioctl
= ubifs_ioctl
,
1285 .splice_read
= generic_file_splice_read
,
1286 .splice_write
= generic_file_splice_write
,
1287 #ifdef CONFIG_COMPAT
1288 .compat_ioctl
= ubifs_compat_ioctl
,