file.c 47 KB

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