file.c 47 KB

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