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