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