file.c 46 KB

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