file.c 47 KB

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