file.c 47 KB

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