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