file.c 48 KB

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