file.c 45 KB

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