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