file.c 47 KB

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