diff options
Diffstat (limited to 'fs/ubifs/file.c')
-rw-r--r-- | fs/ubifs/file.c | 1275 |
1 files changed, 1275 insertions, 0 deletions
diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c new file mode 100644 index 000000000000..005a3b854d96 --- /dev/null +++ b/fs/ubifs/file.c | |||
@@ -0,0 +1,1275 @@ | |||
1 | /* | ||
2 | * This file is part of UBIFS. | ||
3 | * | ||
4 | * Copyright (C) 2006-2008 Nokia Corporation. | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License version 2 as published by | ||
8 | * the Free Software Foundation. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
13 | * more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License along with | ||
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | ||
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | * | ||
19 | * Authors: Artem Bityutskiy (Битюцкий Артём) | ||
20 | * Adrian Hunter | ||
21 | */ | ||
22 | |||
23 | /* | ||
24 | * This file implements VFS file and inode operations of regular files, device | ||
25 | * nodes and symlinks as well as address space operations. | ||
26 | * | ||
27 | * UBIFS uses 2 page flags: PG_private and PG_checked. PG_private is set if the | ||
28 | * page is dirty and is used for budgeting purposes - dirty pages should not be | ||
29 | * budgeted. The PG_checked flag is set if full budgeting is required for the | ||
30 | * page e.g., when it corresponds to a file hole or it is just beyond the file | ||
31 | * size. The budgeting is done in 'ubifs_write_begin()', because it is OK to | ||
32 | * fail in this function, and the budget is released in 'ubifs_write_end()'. So | ||
33 | * the PG_private and PG_checked flags carry the information about how the page | ||
34 | * was budgeted, to make it possible to release the budget properly. | ||
35 | * | ||
36 | * A thing to keep in mind: inode's 'i_mutex' is locked in most VFS operations | ||
37 | * we implement. However, this is not true for '->writepage()', which might be | ||
38 | * called with 'i_mutex' unlocked. For example, when pdflush is performing | ||
39 | * write-back, it calls 'writepage()' with unlocked 'i_mutex', although the | ||
40 | * inode has 'I_LOCK' flag in this case. At "normal" work-paths 'i_mutex' is | ||
41 | * locked in '->writepage', e.g. in "sys_write -> alloc_pages -> direct reclaim | ||
42 | * path'. So, in '->writepage()' we are only guaranteed that the page is | ||
43 | * locked. | ||
44 | * | ||
45 | * Similarly, 'i_mutex' does not have to be locked in readpage(), e.g., | ||
46 | * readahead path does not have it locked ("sys_read -> generic_file_aio_read | ||
47 | * -> ondemand_readahead -> readpage"). In case of readahead, 'I_LOCK' flag is | ||
48 | * not set as well. However, UBIFS disables readahead. | ||
49 | * | ||
50 | * This, for example means that there might be 2 concurrent '->writepage()' | ||
51 | * calls for the same inode, but different inode dirty pages. | ||
52 | */ | ||
53 | |||
54 | #include "ubifs.h" | ||
55 | #include <linux/mount.h> | ||
56 | |||
57 | static int read_block(struct inode *inode, void *addr, unsigned int block, | ||
58 | struct ubifs_data_node *dn) | ||
59 | { | ||
60 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
61 | int err, len, out_len; | ||
62 | union ubifs_key key; | ||
63 | unsigned int dlen; | ||
64 | |||
65 | data_key_init(c, &key, inode->i_ino, block); | ||
66 | err = ubifs_tnc_lookup(c, &key, dn); | ||
67 | if (err) { | ||
68 | if (err == -ENOENT) | ||
69 | /* Not found, so it must be a hole */ | ||
70 | memset(addr, 0, UBIFS_BLOCK_SIZE); | ||
71 | return err; | ||
72 | } | ||
73 | |||
74 | ubifs_assert(dn->ch.sqnum > ubifs_inode(inode)->creat_sqnum); | ||
75 | |||
76 | len = le32_to_cpu(dn->size); | ||
77 | if (len <= 0 || len > UBIFS_BLOCK_SIZE) | ||
78 | goto dump; | ||
79 | |||
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) | ||
85 | goto dump; | ||
86 | |||
87 | /* | ||
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. | ||
91 | */ | ||
92 | if (len < UBIFS_BLOCK_SIZE) | ||
93 | memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); | ||
94 | |||
95 | return 0; | ||
96 | |||
97 | dump: | ||
98 | ubifs_err("bad data node (block %u, inode %lu)", | ||
99 | block, inode->i_ino); | ||
100 | dbg_dump_node(c, dn); | ||
101 | return -EINVAL; | ||
102 | } | ||
103 | |||
104 | static int do_readpage(struct page *page) | ||
105 | { | ||
106 | void *addr; | ||
107 | int err = 0, i; | ||
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); | ||
112 | |||
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)); | ||
117 | |||
118 | addr = kmap(page); | ||
119 | |||
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); | ||
126 | goto out; | ||
127 | } | ||
128 | |||
129 | dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS); | ||
130 | if (!dn) { | ||
131 | err = -ENOMEM; | ||
132 | goto error; | ||
133 | } | ||
134 | |||
135 | i = 0; | ||
136 | while (1) { | ||
137 | int ret; | ||
138 | |||
139 | if (block >= beyond) { | ||
140 | /* Reading beyond inode */ | ||
141 | err = -ENOENT; | ||
142 | memset(addr, 0, UBIFS_BLOCK_SIZE); | ||
143 | } else { | ||
144 | ret = read_block(inode, addr, block, dn); | ||
145 | if (ret) { | ||
146 | err = ret; | ||
147 | if (err != -ENOENT) | ||
148 | break; | ||
149 | } | ||
150 | } | ||
151 | if (++i >= UBIFS_BLOCKS_PER_PAGE) | ||
152 | break; | ||
153 | block += 1; | ||
154 | addr += UBIFS_BLOCK_SIZE; | ||
155 | } | ||
156 | if (err) { | ||
157 | if (err == -ENOENT) { | ||
158 | /* Not found, so it must be a hole */ | ||
159 | SetPageChecked(page); | ||
160 | dbg_gen("hole"); | ||
161 | goto out_free; | ||
162 | } | ||
163 | ubifs_err("cannot read page %lu of inode %lu, error %d", | ||
164 | page->index, inode->i_ino, err); | ||
165 | goto error; | ||
166 | } | ||
167 | |||
168 | out_free: | ||
169 | kfree(dn); | ||
170 | out: | ||
171 | SetPageUptodate(page); | ||
172 | ClearPageError(page); | ||
173 | flush_dcache_page(page); | ||
174 | kunmap(page); | ||
175 | return 0; | ||
176 | |||
177 | error: | ||
178 | kfree(dn); | ||
179 | ClearPageUptodate(page); | ||
180 | SetPageError(page); | ||
181 | flush_dcache_page(page); | ||
182 | kunmap(page); | ||
183 | return err; | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * release_new_page_budget - release budget of a new page. | ||
188 | * @c: UBIFS file-system description object | ||
189 | * | ||
190 | * This is a helper function which releases budget corresponding to the budget | ||
191 | * of one new page of data. | ||
192 | */ | ||
193 | static void release_new_page_budget(struct ubifs_info *c) | ||
194 | { | ||
195 | struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 }; | ||
196 | |||
197 | ubifs_release_budget(c, &req); | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * release_existing_page_budget - release budget of an existing page. | ||
202 | * @c: UBIFS file-system description object | ||
203 | * | ||
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. | ||
206 | */ | ||
207 | static void release_existing_page_budget(struct ubifs_info *c) | ||
208 | { | ||
209 | struct ubifs_budget_req req = { .dd_growth = c->page_budget}; | ||
210 | |||
211 | ubifs_release_budget(c, &req); | ||
212 | } | ||
213 | |||
214 | static int write_begin_slow(struct address_space *mapping, | ||
215 | loff_t pos, unsigned len, struct page **pagep) | ||
216 | { | ||
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); | ||
222 | struct page *page; | ||
223 | |||
224 | dbg_gen("ino %lu, pos %llu, len %u, i_size %lld", | ||
225 | inode->i_ino, pos, len, inode->i_size); | ||
226 | |||
227 | /* | ||
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. | ||
234 | */ | ||
235 | if (appending) | ||
236 | /* We are appending data, budget for inode change */ | ||
237 | req.dirtied_ino = 1; | ||
238 | |||
239 | err = ubifs_budget_space(c, &req); | ||
240 | if (unlikely(err)) | ||
241 | return err; | ||
242 | |||
243 | page = __grab_cache_page(mapping, index); | ||
244 | if (unlikely(!page)) { | ||
245 | ubifs_release_budget(c, &req); | ||
246 | return -ENOMEM; | ||
247 | } | ||
248 | |||
249 | if (!PageUptodate(page)) { | ||
250 | if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) | ||
251 | SetPageChecked(page); | ||
252 | else { | ||
253 | err = do_readpage(page); | ||
254 | if (err) { | ||
255 | unlock_page(page); | ||
256 | page_cache_release(page); | ||
257 | return err; | ||
258 | } | ||
259 | } | ||
260 | |||
261 | SetPageUptodate(page); | ||
262 | ClearPageError(page); | ||
263 | } | ||
264 | |||
265 | if (PagePrivate(page)) | ||
266 | /* | ||
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. | ||
272 | * | ||
273 | * So what we have to do is to release the page budget we | ||
274 | * allocated. | ||
275 | */ | ||
276 | release_new_page_budget(c); | ||
277 | else if (!PageChecked(page)) | ||
278 | /* | ||
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. | ||
283 | */ | ||
284 | ubifs_convert_page_budget(c); | ||
285 | |||
286 | if (appending) { | ||
287 | struct ubifs_inode *ui = ubifs_inode(inode); | ||
288 | |||
289 | /* | ||
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. | ||
293 | */ | ||
294 | mutex_lock(&ui->ui_mutex); | ||
295 | if (ui->dirty) | ||
296 | /* | ||
297 | * The inode is dirty already, so we may free the | ||
298 | * budget we allocated. | ||
299 | */ | ||
300 | ubifs_release_dirty_inode_budget(c, ui); | ||
301 | } | ||
302 | |||
303 | *pagep = page; | ||
304 | return 0; | ||
305 | } | ||
306 | |||
307 | /** | ||
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 | ||
313 | * | ||
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. | ||
319 | */ | ||
320 | static int allocate_budget(struct ubifs_info *c, struct page *page, | ||
321 | struct ubifs_inode *ui, int appending) | ||
322 | { | ||
323 | struct ubifs_budget_req req = { .fast = 1 }; | ||
324 | |||
325 | if (PagePrivate(page)) { | ||
326 | if (!appending) | ||
327 | /* | ||
328 | * The page is dirty and we are not appending, which | ||
329 | * means no budget is needed at all. | ||
330 | */ | ||
331 | return 0; | ||
332 | |||
333 | mutex_lock(&ui->ui_mutex); | ||
334 | if (ui->dirty) | ||
335 | /* | ||
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()'. | ||
343 | */ | ||
344 | return 0; | ||
345 | |||
346 | /* | ||
347 | * The page is dirty, we are appending, the inode is clean, so | ||
348 | * we need to budget the inode change. | ||
349 | */ | ||
350 | req.dirtied_ino = 1; | ||
351 | } else { | ||
352 | if (PageChecked(page)) | ||
353 | /* | ||
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 | ||
358 | * page. | ||
359 | */ | ||
360 | req.new_page = 1; | ||
361 | else | ||
362 | /* | ||
363 | * Not a hole, the change will not add any new | ||
364 | * indexing information, budget for page | ||
365 | * change. | ||
366 | */ | ||
367 | req.dirtied_page = 1; | ||
368 | |||
369 | if (appending) { | ||
370 | mutex_lock(&ui->ui_mutex); | ||
371 | if (!ui->dirty) | ||
372 | /* | ||
373 | * The inode is clean but we will have to mark | ||
374 | * it as dirty because we are appending. This | ||
375 | * needs a budget. | ||
376 | */ | ||
377 | req.dirtied_ino = 1; | ||
378 | } | ||
379 | } | ||
380 | |||
381 | return ubifs_budget_space(c, &req); | ||
382 | } | ||
383 | |||
384 | /* | ||
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 | ||
388 | * budgeting. | ||
389 | * | ||
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. | ||
392 | * | ||
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 | ||
405 | * marked dirty. | ||
406 | * | ||
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. | ||
411 | * | ||
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. | ||
415 | */ | ||
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) | ||
419 | { | ||
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); | ||
425 | struct page *page; | ||
426 | |||
427 | |||
428 | ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size); | ||
429 | |||
430 | if (unlikely(c->ro_media)) | ||
431 | return -EROFS; | ||
432 | |||
433 | /* Try out the fast-path part first */ | ||
434 | page = __grab_cache_page(mapping, index); | ||
435 | if (unlikely(!page)) | ||
436 | return -ENOMEM; | ||
437 | |||
438 | if (!PageUptodate(page)) { | ||
439 | /* The page is not loaded from the flash */ | ||
440 | if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) | ||
441 | /* | ||
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 | ||
446 | * the media. | ||
447 | */ | ||
448 | SetPageChecked(page); | ||
449 | else { | ||
450 | err = do_readpage(page); | ||
451 | if (err) { | ||
452 | unlock_page(page); | ||
453 | page_cache_release(page); | ||
454 | return err; | ||
455 | } | ||
456 | } | ||
457 | |||
458 | SetPageUptodate(page); | ||
459 | ClearPageError(page); | ||
460 | } | ||
461 | |||
462 | err = allocate_budget(c, page, ui, appending); | ||
463 | if (unlikely(err)) { | ||
464 | ubifs_assert(err == -ENOSPC); | ||
465 | /* | ||
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. | ||
471 | */ | ||
472 | if (appending) { | ||
473 | ubifs_assert(mutex_is_locked(&ui->ui_mutex)); | ||
474 | mutex_unlock(&ui->ui_mutex); | ||
475 | } | ||
476 | unlock_page(page); | ||
477 | page_cache_release(page); | ||
478 | |||
479 | return write_begin_slow(mapping, pos, len, pagep); | ||
480 | } | ||
481 | |||
482 | /* | ||
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). | ||
487 | */ | ||
488 | *pagep = page; | ||
489 | return 0; | ||
490 | |||
491 | } | ||
492 | |||
493 | /** | ||
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 | ||
499 | * | ||
500 | * This is a helper function for a page write operation. It unlocks the | ||
501 | * @ui->ui_mutex in case of appending. | ||
502 | */ | ||
503 | static void cancel_budget(struct ubifs_info *c, struct page *page, | ||
504 | struct ubifs_inode *ui, int appending) | ||
505 | { | ||
506 | if (appending) { | ||
507 | if (!ui->dirty) | ||
508 | ubifs_release_dirty_inode_budget(c, ui); | ||
509 | mutex_unlock(&ui->ui_mutex); | ||
510 | } | ||
511 | if (!PagePrivate(page)) { | ||
512 | if (PageChecked(page)) | ||
513 | release_new_page_budget(c); | ||
514 | else | ||
515 | release_existing_page_budget(c); | ||
516 | } | ||
517 | } | ||
518 | |||
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) | ||
522 | { | ||
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); | ||
528 | |||
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); | ||
531 | |||
532 | if (unlikely(copied < len && len == PAGE_CACHE_SIZE)) { | ||
533 | /* | ||
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 | ||
540 | * page now. | ||
541 | */ | ||
542 | dbg_gen("copied %d instead of %d, read page and repeat", | ||
543 | copied, len); | ||
544 | cancel_budget(c, page, ui, appending); | ||
545 | |||
546 | /* | ||
547 | * Return 0 to force VFS to repeat the whole operation, or the | ||
548 | * error code if 'do_readpage()' failes. | ||
549 | */ | ||
550 | copied = do_readpage(page); | ||
551 | goto out; | ||
552 | } | ||
553 | |||
554 | if (!PagePrivate(page)) { | ||
555 | SetPagePrivate(page); | ||
556 | atomic_long_inc(&c->dirty_pg_cnt); | ||
557 | __set_page_dirty_nobuffers(page); | ||
558 | } | ||
559 | |||
560 | if (appending) { | ||
561 | i_size_write(inode, end_pos); | ||
562 | ui->ui_size = end_pos; | ||
563 | /* | ||
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()'. | ||
567 | */ | ||
568 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); | ||
569 | ubifs_assert(mutex_is_locked(&ui->ui_mutex)); | ||
570 | mutex_unlock(&ui->ui_mutex); | ||
571 | } | ||
572 | |||
573 | out: | ||
574 | unlock_page(page); | ||
575 | page_cache_release(page); | ||
576 | return copied; | ||
577 | } | ||
578 | |||
579 | static int ubifs_readpage(struct file *file, struct page *page) | ||
580 | { | ||
581 | do_readpage(page); | ||
582 | unlock_page(page); | ||
583 | return 0; | ||
584 | } | ||
585 | |||
586 | static int do_writepage(struct page *page, int len) | ||
587 | { | ||
588 | int err = 0, i, blen; | ||
589 | unsigned int block; | ||
590 | void *addr; | ||
591 | union ubifs_key key; | ||
592 | struct inode *inode = page->mapping->host; | ||
593 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
594 | |||
595 | #ifdef UBIFS_DEBUG | ||
596 | spin_lock(&ui->ui_lock); | ||
597 | ubifs_assert(page->index <= ui->synced_i_size << PAGE_CACHE_SIZE); | ||
598 | spin_unlock(&ui->ui_lock); | ||
599 | #endif | ||
600 | |||
601 | /* Update radix tree tags */ | ||
602 | set_page_writeback(page); | ||
603 | |||
604 | addr = kmap(page); | ||
605 | block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; | ||
606 | i = 0; | ||
607 | while (len) { | ||
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); | ||
611 | if (err) | ||
612 | break; | ||
613 | if (++i >= UBIFS_BLOCKS_PER_PAGE) | ||
614 | break; | ||
615 | block += 1; | ||
616 | addr += blen; | ||
617 | len -= blen; | ||
618 | } | ||
619 | if (err) { | ||
620 | SetPageError(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); | ||
624 | } | ||
625 | |||
626 | ubifs_assert(PagePrivate(page)); | ||
627 | if (PageChecked(page)) | ||
628 | release_new_page_budget(c); | ||
629 | else | ||
630 | release_existing_page_budget(c); | ||
631 | |||
632 | atomic_long_dec(&c->dirty_pg_cnt); | ||
633 | ClearPagePrivate(page); | ||
634 | ClearPageChecked(page); | ||
635 | |||
636 | kunmap(page); | ||
637 | unlock_page(page); | ||
638 | end_page_writeback(page); | ||
639 | return err; | ||
640 | } | ||
641 | |||
642 | /* | ||
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. | ||
654 | * | ||
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. | ||
660 | * | ||
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. | ||
670 | * | ||
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. | ||
676 | * | ||
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. | ||
683 | */ | ||
684 | static int ubifs_writepage(struct page *page, struct writeback_control *wbc) | ||
685 | { | ||
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); | ||
691 | void *kaddr; | ||
692 | |||
693 | dbg_gen("ino %lu, pg %lu, pg flags %#lx", | ||
694 | inode->i_ino, page->index, page->flags); | ||
695 | ubifs_assert(PagePrivate(page)); | ||
696 | |||
697 | /* Is the page fully outside @i_size? (truncate in progress) */ | ||
698 | if (page->index > end_index || (page->index == end_index && !len)) { | ||
699 | err = 0; | ||
700 | goto out_unlock; | ||
701 | } | ||
702 | |||
703 | spin_lock(&ui->ui_lock); | ||
704 | synced_i_size = ui->synced_i_size; | ||
705 | spin_unlock(&ui->ui_lock); | ||
706 | |||
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); | ||
711 | if (err) | ||
712 | goto out_unlock; | ||
713 | /* | ||
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 | ||
719 | * with this. | ||
720 | */ | ||
721 | } | ||
722 | return do_writepage(page, PAGE_CACHE_SIZE); | ||
723 | } | ||
724 | |||
725 | /* | ||
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." | ||
731 | */ | ||
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); | ||
736 | |||
737 | if (i_size > synced_i_size) { | ||
738 | err = inode->i_sb->s_op->write_inode(inode, 1); | ||
739 | if (err) | ||
740 | goto out_unlock; | ||
741 | } | ||
742 | |||
743 | return do_writepage(page, len); | ||
744 | |||
745 | out_unlock: | ||
746 | unlock_page(page); | ||
747 | return err; | ||
748 | } | ||
749 | |||
750 | /** | ||
751 | * do_attr_changes - change inode attributes. | ||
752 | * @inode: inode to change attributes for | ||
753 | * @attr: describes attributes to change | ||
754 | */ | ||
755 | static void do_attr_changes(struct inode *inode, const struct iattr *attr) | ||
756 | { | ||
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; | ||
772 | |||
773 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | ||
774 | mode &= ~S_ISGID; | ||
775 | inode->i_mode = mode; | ||
776 | } | ||
777 | } | ||
778 | |||
779 | /** | ||
780 | * do_truncation - truncate an inode. | ||
781 | * @c: UBIFS file-system description object | ||
782 | * @inode: inode to truncate | ||
783 | * @attr: inode attribute changes description | ||
784 | * | ||
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. | ||
788 | */ | ||
789 | static int do_truncation(struct ubifs_info *c, struct inode *inode, | ||
790 | const struct iattr *attr) | ||
791 | { | ||
792 | int err; | ||
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); | ||
797 | |||
798 | dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size); | ||
799 | memset(&req, 0, sizeof(struct ubifs_budget_req)); | ||
800 | |||
801 | /* | ||
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. | ||
805 | */ | ||
806 | if (new_size & (UBIFS_BLOCK_SIZE - 1)) | ||
807 | req.dirtied_page = 1; | ||
808 | |||
809 | req.dirtied_ino = 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); | ||
813 | if (err) | ||
814 | return err; | ||
815 | |||
816 | err = vmtruncate(inode, new_size); | ||
817 | if (err) | ||
818 | goto out_budg; | ||
819 | |||
820 | if (offset) { | ||
821 | pgoff_t index = new_size >> PAGE_CACHE_SHIFT; | ||
822 | struct page *page; | ||
823 | |||
824 | page = find_lock_page(inode->i_mapping, index); | ||
825 | if (page) { | ||
826 | if (PageDirty(page)) { | ||
827 | /* | ||
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. | ||
834 | */ | ||
835 | ubifs_assert(PagePrivate(page)); | ||
836 | |||
837 | clear_page_dirty_for_io(page); | ||
838 | if (UBIFS_BLOCKS_PER_PAGE_SHIFT) | ||
839 | offset = new_size & | ||
840 | (PAGE_CACHE_SIZE - 1); | ||
841 | err = do_writepage(page, offset); | ||
842 | page_cache_release(page); | ||
843 | if (err) | ||
844 | goto out_budg; | ||
845 | /* | ||
846 | * We could now tell 'ubifs_jnl_truncate()' not | ||
847 | * to read the last block. | ||
848 | */ | ||
849 | } else { | ||
850 | /* | ||
851 | * We could 'kmap()' the page and pass the data | ||
852 | * to 'ubifs_jnl_truncate()' to save it from | ||
853 | * having to read it. | ||
854 | */ | ||
855 | unlock_page(page); | ||
856 | page_cache_release(page); | ||
857 | } | ||
858 | } | ||
859 | } | ||
860 | |||
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); | ||
867 | |||
868 | err = ubifs_jnl_truncate(c, inode, old_size, new_size); | ||
869 | mutex_unlock(&ui->ui_mutex); | ||
870 | out_budg: | ||
871 | ubifs_release_budget(c, &req); | ||
872 | return err; | ||
873 | } | ||
874 | |||
875 | /** | ||
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 | ||
880 | * | ||
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. | ||
884 | */ | ||
885 | static int do_setattr(struct ubifs_info *c, struct inode *inode, | ||
886 | const struct iattr *attr) | ||
887 | { | ||
888 | int err, release; | ||
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 }; | ||
893 | |||
894 | err = ubifs_budget_space(c, &req); | ||
895 | if (err) | ||
896 | return err; | ||
897 | |||
898 | if (attr->ia_valid & ATTR_SIZE) { | ||
899 | dbg_gen("size %lld -> %lld", inode->i_size, new_size); | ||
900 | err = vmtruncate(inode, new_size); | ||
901 | if (err) | ||
902 | goto out; | ||
903 | } | ||
904 | |||
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; | ||
911 | } | ||
912 | |||
913 | do_attr_changes(inode, attr); | ||
914 | |||
915 | release = ui->dirty; | ||
916 | if (attr->ia_valid & ATTR_SIZE) | ||
917 | /* | ||
918 | * Inode length changed, so we have to make sure | ||
919 | * @I_DIRTY_DATASYNC is set. | ||
920 | */ | ||
921 | __mark_inode_dirty(inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC); | ||
922 | else | ||
923 | mark_inode_dirty_sync(inode); | ||
924 | mutex_unlock(&ui->ui_mutex); | ||
925 | |||
926 | if (release) | ||
927 | ubifs_release_budget(c, &req); | ||
928 | if (IS_SYNC(inode)) | ||
929 | err = inode->i_sb->s_op->write_inode(inode, 1); | ||
930 | return err; | ||
931 | |||
932 | out: | ||
933 | ubifs_release_budget(c, &req); | ||
934 | return err; | ||
935 | } | ||
936 | |||
937 | int ubifs_setattr(struct dentry *dentry, struct iattr *attr) | ||
938 | { | ||
939 | int err; | ||
940 | struct inode *inode = dentry->d_inode; | ||
941 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
942 | |||
943 | dbg_gen("ino %lu, ia_valid %#x", inode->i_ino, attr->ia_valid); | ||
944 | err = inode_change_ok(inode, attr); | ||
945 | if (err) | ||
946 | return err; | ||
947 | |||
948 | err = dbg_check_synced_i_size(inode); | ||
949 | if (err) | ||
950 | return err; | ||
951 | |||
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); | ||
955 | else | ||
956 | err = do_setattr(c, inode, attr); | ||
957 | |||
958 | return err; | ||
959 | } | ||
960 | |||
961 | static void ubifs_invalidatepage(struct page *page, unsigned long offset) | ||
962 | { | ||
963 | struct inode *inode = page->mapping->host; | ||
964 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
965 | |||
966 | ubifs_assert(PagePrivate(page)); | ||
967 | if (offset) | ||
968 | /* Partial page remains dirty */ | ||
969 | return; | ||
970 | |||
971 | if (PageChecked(page)) | ||
972 | release_new_page_budget(c); | ||
973 | else | ||
974 | release_existing_page_budget(c); | ||
975 | |||
976 | atomic_long_dec(&c->dirty_pg_cnt); | ||
977 | ClearPagePrivate(page); | ||
978 | ClearPageChecked(page); | ||
979 | } | ||
980 | |||
981 | static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd) | ||
982 | { | ||
983 | struct ubifs_inode *ui = ubifs_inode(dentry->d_inode); | ||
984 | |||
985 | nd_set_link(nd, ui->data); | ||
986 | return NULL; | ||
987 | } | ||
988 | |||
989 | int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync) | ||
990 | { | ||
991 | struct inode *inode = dentry->d_inode; | ||
992 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
993 | int err; | ||
994 | |||
995 | dbg_gen("syncing inode %lu", inode->i_ino); | ||
996 | |||
997 | /* | ||
998 | * VFS has already synchronized dirty pages for this inode. Synchronize | ||
999 | * the inode unless this is a 'datasync()' call. | ||
1000 | */ | ||
1001 | if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) { | ||
1002 | err = inode->i_sb->s_op->write_inode(inode, 1); | ||
1003 | if (err) | ||
1004 | return err; | ||
1005 | } | ||
1006 | |||
1007 | /* | ||
1008 | * Nodes related to this inode may still sit in a write-buffer. Flush | ||
1009 | * them. | ||
1010 | */ | ||
1011 | err = ubifs_sync_wbufs_by_inode(c, inode); | ||
1012 | if (err) | ||
1013 | return err; | ||
1014 | |||
1015 | return 0; | ||
1016 | } | ||
1017 | |||
1018 | /** | ||
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 | ||
1022 | * | ||
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. | ||
1026 | */ | ||
1027 | static inline int mctime_update_needed(const struct inode *inode, | ||
1028 | const struct timespec *now) | ||
1029 | { | ||
1030 | if (!timespec_equal(&inode->i_mtime, now) || | ||
1031 | !timespec_equal(&inode->i_ctime, now)) | ||
1032 | return 1; | ||
1033 | return 0; | ||
1034 | } | ||
1035 | |||
1036 | /** | ||
1037 | * update_ctime - update mtime and ctime of an inode. | ||
1038 | * @c: UBIFS file-system description object | ||
1039 | * @inode: inode to update | ||
1040 | * | ||
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 | ||
1043 | * case of failure. | ||
1044 | */ | ||
1045 | static int update_mctime(struct ubifs_info *c, struct inode *inode) | ||
1046 | { | ||
1047 | struct timespec now = ubifs_current_time(inode); | ||
1048 | struct ubifs_inode *ui = ubifs_inode(inode); | ||
1049 | |||
1050 | if (mctime_update_needed(inode, &now)) { | ||
1051 | int err, release; | ||
1052 | struct ubifs_budget_req req = { .dirtied_ino = 1, | ||
1053 | .dirtied_ino_d = ui->data_len }; | ||
1054 | |||
1055 | err = ubifs_budget_space(c, &req); | ||
1056 | if (err) | ||
1057 | return err; | ||
1058 | |||
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); | ||
1064 | if (release) | ||
1065 | ubifs_release_budget(c, &req); | ||
1066 | } | ||
1067 | |||
1068 | return 0; | ||
1069 | } | ||
1070 | |||
1071 | static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov, | ||
1072 | unsigned long nr_segs, loff_t pos) | ||
1073 | { | ||
1074 | int err; | ||
1075 | ssize_t ret; | ||
1076 | struct inode *inode = iocb->ki_filp->f_mapping->host; | ||
1077 | struct ubifs_info *c = inode->i_sb->s_fs_info; | ||
1078 | |||
1079 | err = update_mctime(c, inode); | ||
1080 | if (err) | ||
1081 | return err; | ||
1082 | |||
1083 | ret = generic_file_aio_write(iocb, iov, nr_segs, pos); | ||
1084 | if (ret < 0) | ||
1085 | return ret; | ||
1086 | |||
1087 | if (ret > 0 && (IS_SYNC(inode) || iocb->ki_filp->f_flags & O_SYNC)) { | ||
1088 | err = ubifs_sync_wbufs_by_inode(c, inode); | ||
1089 | if (err) | ||
1090 | return err; | ||
1091 | } | ||
1092 | |||
1093 | return ret; | ||
1094 | } | ||
1095 | |||
1096 | static int ubifs_set_page_dirty(struct page *page) | ||
1097 | { | ||
1098 | int ret; | ||
1099 | |||
1100 | ret = __set_page_dirty_nobuffers(page); | ||
1101 | /* | ||
1102 | * An attempt to dirty a page without budgeting for it - should not | ||
1103 | * happen. | ||
1104 | */ | ||
1105 | ubifs_assert(ret == 0); | ||
1106 | return ret; | ||
1107 | } | ||
1108 | |||
1109 | static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags) | ||
1110 | { | ||
1111 | /* | ||
1112 | * An attempt to release a dirty page without budgeting for it - should | ||
1113 | * not happen. | ||
1114 | */ | ||
1115 | if (PageWriteback(page)) | ||
1116 | return 0; | ||
1117 | ubifs_assert(PagePrivate(page)); | ||
1118 | ubifs_assert(0); | ||
1119 | ClearPagePrivate(page); | ||
1120 | ClearPageChecked(page); | ||
1121 | return 1; | ||
1122 | } | ||
1123 | |||
1124 | /* | ||
1125 | * mmap()d file has taken write protection fault and is being made | ||
1126 | * writable. UBIFS must ensure page is budgeted for. | ||
1127 | */ | ||
1128 | static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) | ||
1129 | { | ||
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; | ||
1135 | |||
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)); | ||
1139 | |||
1140 | if (unlikely(c->ro_media)) | ||
1141 | return -EROFS; | ||
1142 | |||
1143 | /* | ||
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. | ||
1147 | * | ||
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 | ||
1155 | * dirty. | ||
1156 | * | ||
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. | ||
1160 | */ | ||
1161 | update_time = mctime_update_needed(inode, &now); | ||
1162 | if (update_time) | ||
1163 | /* | ||
1164 | * We have to change inode time stamp which requires extra | ||
1165 | * budgeting. | ||
1166 | */ | ||
1167 | req.dirtied_ino = 1; | ||
1168 | |||
1169 | err = ubifs_budget_space(c, &req); | ||
1170 | if (unlikely(err)) { | ||
1171 | if (err == -ENOSPC) | ||
1172 | ubifs_warn("out of space for mmapped file " | ||
1173 | "(inode number %lu)", inode->i_ino); | ||
1174 | return err; | ||
1175 | } | ||
1176 | |||
1177 | lock_page(page); | ||
1178 | if (unlikely(page->mapping != inode->i_mapping || | ||
1179 | page_offset(page) > i_size_read(inode))) { | ||
1180 | /* Page got truncated out from underneath us */ | ||
1181 | err = -EINVAL; | ||
1182 | goto out_unlock; | ||
1183 | } | ||
1184 | |||
1185 | if (PagePrivate(page)) | ||
1186 | release_new_page_budget(c); | ||
1187 | else { | ||
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); | ||
1193 | } | ||
1194 | |||
1195 | if (update_time) { | ||
1196 | int release; | ||
1197 | struct ubifs_inode *ui = ubifs_inode(inode); | ||
1198 | |||
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); | ||
1204 | if (release) | ||
1205 | ubifs_release_dirty_inode_budget(c, ui); | ||
1206 | } | ||
1207 | |||
1208 | unlock_page(page); | ||
1209 | return 0; | ||
1210 | |||
1211 | out_unlock: | ||
1212 | unlock_page(page); | ||
1213 | ubifs_release_budget(c, &req); | ||
1214 | return err; | ||
1215 | } | ||
1216 | |||
1217 | static struct vm_operations_struct ubifs_file_vm_ops = { | ||
1218 | .fault = filemap_fault, | ||
1219 | .page_mkwrite = ubifs_vm_page_mkwrite, | ||
1220 | }; | ||
1221 | |||
1222 | static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma) | ||
1223 | { | ||
1224 | int err; | ||
1225 | |||
1226 | /* 'generic_file_mmap()' takes care of NOMMU case */ | ||
1227 | err = generic_file_mmap(file, vma); | ||
1228 | if (err) | ||
1229 | return err; | ||
1230 | vma->vm_ops = &ubifs_file_vm_ops; | ||
1231 | return 0; | ||
1232 | } | ||
1233 | |||
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, | ||
1242 | }; | ||
1243 | |||
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, | ||
1252 | #endif | ||
1253 | }; | ||
1254 | |||
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, | ||
1260 | }; | ||
1261 | |||
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, | ||
1274 | #endif | ||
1275 | }; | ||