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