file.c 44 KB

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