file.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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, allocated = 0;
  722. ui->last_page_read = index;
  723. if (!c->bulk_read)
  724. return 0;
  725. /*
  726. * Bulk-read is protected by @ui->ui_mutex, but it is an optimization,
  727. * so 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. /*
  746. * If possible, try to use pre-allocated bulk-read information, which
  747. * is protected by @c->bu_mutex.
  748. */
  749. if (mutex_trylock(&c->bu_mutex))
  750. bu = &c->bu;
  751. else {
  752. bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN);
  753. if (!bu)
  754. goto out_unlock;
  755. bu->buf = NULL;
  756. allocated = 1;
  757. }
  758. bu->buf_len = c->max_bu_buf_len;
  759. data_key_init(c, &bu->key, inode->i_ino,
  760. page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT);
  761. err = ubifs_do_bulk_read(c, bu, page);
  762. if (!allocated)
  763. mutex_unlock(&c->bu_mutex);
  764. else
  765. kfree(bu);
  766. out_unlock:
  767. mutex_unlock(&ui->ui_mutex);
  768. return err;
  769. }
  770. static int ubifs_readpage(struct file *file, struct page *page)
  771. {
  772. if (ubifs_bulk_read(page))
  773. return 0;
  774. do_readpage(page);
  775. unlock_page(page);
  776. return 0;
  777. }
  778. static int do_writepage(struct page *page, int len)
  779. {
  780. int err = 0, i, blen;
  781. unsigned int block;
  782. void *addr;
  783. union ubifs_key key;
  784. struct inode *inode = page->mapping->host;
  785. struct ubifs_info *c = inode->i_sb->s_fs_info;
  786. #ifdef UBIFS_DEBUG
  787. spin_lock(&ui->ui_lock);
  788. ubifs_assert(page->index <= ui->synced_i_size << PAGE_CACHE_SIZE);
  789. spin_unlock(&ui->ui_lock);
  790. #endif
  791. /* Update radix tree tags */
  792. set_page_writeback(page);
  793. addr = kmap(page);
  794. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  795. i = 0;
  796. while (len) {
  797. blen = min_t(int, len, UBIFS_BLOCK_SIZE);
  798. data_key_init(c, &key, inode->i_ino, block);
  799. err = ubifs_jnl_write_data(c, inode, &key, addr, blen);
  800. if (err)
  801. break;
  802. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  803. break;
  804. block += 1;
  805. addr += blen;
  806. len -= blen;
  807. }
  808. if (err) {
  809. SetPageError(page);
  810. ubifs_err("cannot write page %lu of inode %lu, error %d",
  811. page->index, inode->i_ino, err);
  812. ubifs_ro_mode(c, err);
  813. }
  814. ubifs_assert(PagePrivate(page));
  815. if (PageChecked(page))
  816. release_new_page_budget(c);
  817. else
  818. release_existing_page_budget(c);
  819. atomic_long_dec(&c->dirty_pg_cnt);
  820. ClearPagePrivate(page);
  821. ClearPageChecked(page);
  822. kunmap(page);
  823. unlock_page(page);
  824. end_page_writeback(page);
  825. return err;
  826. }
  827. /*
  828. * When writing-back dirty inodes, VFS first writes-back pages belonging to the
  829. * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
  830. * situation when a we have an inode with size 0, then a megabyte of data is
  831. * appended to the inode, then write-back starts and flushes some amount of the
  832. * dirty pages, the journal becomes full, commit happens and finishes, and then
  833. * an unclean reboot happens. When the file system is mounted next time, the
  834. * inode size would still be 0, but there would be many pages which are beyond
  835. * the inode size, they would be indexed and consume flash space. Because the
  836. * journal has been committed, the replay would not be able to detect this
  837. * situation and correct the inode size. This means UBIFS would have to scan
  838. * whole index and correct all inode sizes, which is long an unacceptable.
  839. *
  840. * To prevent situations like this, UBIFS writes pages back only if they are
  841. * within last synchronized inode size, i.e. the the size which has been
  842. * written to the flash media last time. Otherwise, UBIFS forces inode
  843. * write-back, thus making sure the on-flash inode contains current inode size,
  844. * and then keeps writing pages back.
  845. *
  846. * Some locking issues explanation. 'ubifs_writepage()' first is called with
  847. * the page locked, and it locks @ui_mutex. However, write-back does take inode
  848. * @i_mutex, which means other VFS operations may be run on this inode at the
  849. * same time. And the problematic one is truncation to smaller size, from where
  850. * we have to call 'vmtruncate()', which first changes @inode->i_size, then
  851. * drops the truncated pages. And while dropping the pages, it takes the page
  852. * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with
  853. * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This
  854. * means that @inode->i_size is changed while @ui_mutex is unlocked.
  855. *
  856. * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
  857. * inode size. How do we do this if @inode->i_size may became smaller while we
  858. * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
  859. * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
  860. * internally and updates it under @ui_mutex.
  861. *
  862. * Q: why we do not worry that if we race with truncation, we may end up with a
  863. * situation when the inode is truncated while we are in the middle of
  864. * 'do_writepage()', so we do write beyond inode size?
  865. * A: If we are in the middle of 'do_writepage()', truncation would be locked
  866. * on the page lock and it would not write the truncated inode node to the
  867. * journal before we have finished.
  868. */
  869. static int ubifs_writepage(struct page *page, struct writeback_control *wbc)
  870. {
  871. struct inode *inode = page->mapping->host;
  872. struct ubifs_inode *ui = ubifs_inode(inode);
  873. loff_t i_size = i_size_read(inode), synced_i_size;
  874. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  875. int err, len = i_size & (PAGE_CACHE_SIZE - 1);
  876. void *kaddr;
  877. dbg_gen("ino %lu, pg %lu, pg flags %#lx",
  878. inode->i_ino, page->index, page->flags);
  879. ubifs_assert(PagePrivate(page));
  880. /* Is the page fully outside @i_size? (truncate in progress) */
  881. if (page->index > end_index || (page->index == end_index && !len)) {
  882. err = 0;
  883. goto out_unlock;
  884. }
  885. spin_lock(&ui->ui_lock);
  886. synced_i_size = ui->synced_i_size;
  887. spin_unlock(&ui->ui_lock);
  888. /* Is the page fully inside @i_size? */
  889. if (page->index < end_index) {
  890. if (page->index >= synced_i_size >> PAGE_CACHE_SHIFT) {
  891. err = inode->i_sb->s_op->write_inode(inode, 1);
  892. if (err)
  893. goto out_unlock;
  894. /*
  895. * The inode has been written, but the write-buffer has
  896. * not been synchronized, so in case of an unclean
  897. * reboot we may end up with some pages beyond inode
  898. * size, but they would be in the journal (because
  899. * commit flushes write buffers) and recovery would deal
  900. * with this.
  901. */
  902. }
  903. return do_writepage(page, PAGE_CACHE_SIZE);
  904. }
  905. /*
  906. * The page straddles @i_size. It must be zeroed out on each and every
  907. * writepage invocation because it may be mmapped. "A file is mapped
  908. * in multiples of the page size. For a file that is not a multiple of
  909. * the page size, the remaining memory is zeroed when mapped, and
  910. * writes to that region are not written out to the file."
  911. */
  912. kaddr = kmap_atomic(page, KM_USER0);
  913. memset(kaddr + len, 0, PAGE_CACHE_SIZE - len);
  914. flush_dcache_page(page);
  915. kunmap_atomic(kaddr, KM_USER0);
  916. if (i_size > synced_i_size) {
  917. err = inode->i_sb->s_op->write_inode(inode, 1);
  918. if (err)
  919. goto out_unlock;
  920. }
  921. return do_writepage(page, len);
  922. out_unlock:
  923. unlock_page(page);
  924. return err;
  925. }
  926. /**
  927. * do_attr_changes - change inode attributes.
  928. * @inode: inode to change attributes for
  929. * @attr: describes attributes to change
  930. */
  931. static void do_attr_changes(struct inode *inode, const struct iattr *attr)
  932. {
  933. if (attr->ia_valid & ATTR_UID)
  934. inode->i_uid = attr->ia_uid;
  935. if (attr->ia_valid & ATTR_GID)
  936. inode->i_gid = attr->ia_gid;
  937. if (attr->ia_valid & ATTR_ATIME)
  938. inode->i_atime = timespec_trunc(attr->ia_atime,
  939. inode->i_sb->s_time_gran);
  940. if (attr->ia_valid & ATTR_MTIME)
  941. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  942. inode->i_sb->s_time_gran);
  943. if (attr->ia_valid & ATTR_CTIME)
  944. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  945. inode->i_sb->s_time_gran);
  946. if (attr->ia_valid & ATTR_MODE) {
  947. umode_t mode = attr->ia_mode;
  948. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  949. mode &= ~S_ISGID;
  950. inode->i_mode = mode;
  951. }
  952. }
  953. /**
  954. * do_truncation - truncate an inode.
  955. * @c: UBIFS file-system description object
  956. * @inode: inode to truncate
  957. * @attr: inode attribute changes description
  958. *
  959. * This function implements VFS '->setattr()' call when the inode is truncated
  960. * to a smaller size. Returns zero in case of success and a negative error code
  961. * in case of failure.
  962. */
  963. static int do_truncation(struct ubifs_info *c, struct inode *inode,
  964. const struct iattr *attr)
  965. {
  966. int err;
  967. struct ubifs_budget_req req;
  968. loff_t old_size = inode->i_size, new_size = attr->ia_size;
  969. int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1;
  970. struct ubifs_inode *ui = ubifs_inode(inode);
  971. dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size);
  972. memset(&req, 0, sizeof(struct ubifs_budget_req));
  973. /*
  974. * If this is truncation to a smaller size, and we do not truncate on a
  975. * block boundary, budget for changing one data block, because the last
  976. * block will be re-written.
  977. */
  978. if (new_size & (UBIFS_BLOCK_SIZE - 1))
  979. req.dirtied_page = 1;
  980. req.dirtied_ino = 1;
  981. /* A funny way to budget for truncation node */
  982. req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ;
  983. err = ubifs_budget_space(c, &req);
  984. if (err) {
  985. /*
  986. * Treat truncations to zero as deletion and always allow them,
  987. * just like we do for '->unlink()'.
  988. */
  989. if (new_size || err != -ENOSPC)
  990. return err;
  991. budgeted = 0;
  992. }
  993. err = vmtruncate(inode, new_size);
  994. if (err)
  995. goto out_budg;
  996. if (offset) {
  997. pgoff_t index = new_size >> PAGE_CACHE_SHIFT;
  998. struct page *page;
  999. page = find_lock_page(inode->i_mapping, index);
  1000. if (page) {
  1001. if (PageDirty(page)) {
  1002. /*
  1003. * 'ubifs_jnl_truncate()' will try to truncate
  1004. * the last data node, but it contains
  1005. * out-of-date data because the page is dirty.
  1006. * Write the page now, so that
  1007. * 'ubifs_jnl_truncate()' will see an already
  1008. * truncated (and up to date) data node.
  1009. */
  1010. ubifs_assert(PagePrivate(page));
  1011. clear_page_dirty_for_io(page);
  1012. if (UBIFS_BLOCKS_PER_PAGE_SHIFT)
  1013. offset = new_size &
  1014. (PAGE_CACHE_SIZE - 1);
  1015. err = do_writepage(page, offset);
  1016. page_cache_release(page);
  1017. if (err)
  1018. goto out_budg;
  1019. /*
  1020. * We could now tell 'ubifs_jnl_truncate()' not
  1021. * to read the last block.
  1022. */
  1023. } else {
  1024. /*
  1025. * We could 'kmap()' the page and pass the data
  1026. * to 'ubifs_jnl_truncate()' to save it from
  1027. * having to read it.
  1028. */
  1029. unlock_page(page);
  1030. page_cache_release(page);
  1031. }
  1032. }
  1033. }
  1034. mutex_lock(&ui->ui_mutex);
  1035. ui->ui_size = inode->i_size;
  1036. /* Truncation changes inode [mc]time */
  1037. inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
  1038. /* The other attributes may be changed at the same time as well */
  1039. do_attr_changes(inode, attr);
  1040. err = ubifs_jnl_truncate(c, inode, old_size, new_size);
  1041. mutex_unlock(&ui->ui_mutex);
  1042. out_budg:
  1043. if (budgeted)
  1044. ubifs_release_budget(c, &req);
  1045. else {
  1046. c->nospace = c->nospace_rp = 0;
  1047. smp_wmb();
  1048. }
  1049. return err;
  1050. }
  1051. /**
  1052. * do_setattr - change inode attributes.
  1053. * @c: UBIFS file-system description object
  1054. * @inode: inode to change attributes for
  1055. * @attr: inode attribute changes description
  1056. *
  1057. * This function implements VFS '->setattr()' call for all cases except
  1058. * truncations to smaller size. Returns zero in case of success and a negative
  1059. * error code in case of failure.
  1060. */
  1061. static int do_setattr(struct ubifs_info *c, struct inode *inode,
  1062. const struct iattr *attr)
  1063. {
  1064. int err, release;
  1065. loff_t new_size = attr->ia_size;
  1066. struct ubifs_inode *ui = ubifs_inode(inode);
  1067. struct ubifs_budget_req req = { .dirtied_ino = 1,
  1068. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  1069. err = ubifs_budget_space(c, &req);
  1070. if (err)
  1071. return err;
  1072. if (attr->ia_valid & ATTR_SIZE) {
  1073. dbg_gen("size %lld -> %lld", inode->i_size, new_size);
  1074. err = vmtruncate(inode, new_size);
  1075. if (err)
  1076. goto out;
  1077. }
  1078. mutex_lock(&ui->ui_mutex);
  1079. if (attr->ia_valid & ATTR_SIZE) {
  1080. /* Truncation changes inode [mc]time */
  1081. inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
  1082. /* 'vmtruncate()' changed @i_size, update @ui_size */
  1083. ui->ui_size = inode->i_size;
  1084. }
  1085. do_attr_changes(inode, attr);
  1086. release = ui->dirty;
  1087. if (attr->ia_valid & ATTR_SIZE)
  1088. /*
  1089. * Inode length changed, so we have to make sure
  1090. * @I_DIRTY_DATASYNC is set.
  1091. */
  1092. __mark_inode_dirty(inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  1093. else
  1094. mark_inode_dirty_sync(inode);
  1095. mutex_unlock(&ui->ui_mutex);
  1096. if (release)
  1097. ubifs_release_budget(c, &req);
  1098. if (IS_SYNC(inode))
  1099. err = inode->i_sb->s_op->write_inode(inode, 1);
  1100. return err;
  1101. out:
  1102. ubifs_release_budget(c, &req);
  1103. return err;
  1104. }
  1105. int ubifs_setattr(struct dentry *dentry, struct iattr *attr)
  1106. {
  1107. int err;
  1108. struct inode *inode = dentry->d_inode;
  1109. struct ubifs_info *c = inode->i_sb->s_fs_info;
  1110. dbg_gen("ino %lu, mode %#x, ia_valid %#x",
  1111. inode->i_ino, inode->i_mode, attr->ia_valid);
  1112. err = inode_change_ok(inode, attr);
  1113. if (err)
  1114. return err;
  1115. err = dbg_check_synced_i_size(inode);
  1116. if (err)
  1117. return err;
  1118. if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size)
  1119. /* Truncation to a smaller size */
  1120. err = do_truncation(c, inode, attr);
  1121. else
  1122. err = do_setattr(c, inode, attr);
  1123. return err;
  1124. }
  1125. static void ubifs_invalidatepage(struct page *page, unsigned long offset)
  1126. {
  1127. struct inode *inode = page->mapping->host;
  1128. struct ubifs_info *c = inode->i_sb->s_fs_info;
  1129. ubifs_assert(PagePrivate(page));
  1130. if (offset)
  1131. /* Partial page remains dirty */
  1132. return;
  1133. if (PageChecked(page))
  1134. release_new_page_budget(c);
  1135. else
  1136. release_existing_page_budget(c);
  1137. atomic_long_dec(&c->dirty_pg_cnt);
  1138. ClearPagePrivate(page);
  1139. ClearPageChecked(page);
  1140. }
  1141. static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1142. {
  1143. struct ubifs_inode *ui = ubifs_inode(dentry->d_inode);
  1144. nd_set_link(nd, ui->data);
  1145. return NULL;
  1146. }
  1147. int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync)
  1148. {
  1149. struct inode *inode = dentry->d_inode;
  1150. struct ubifs_info *c = inode->i_sb->s_fs_info;
  1151. int err;
  1152. dbg_gen("syncing inode %lu", inode->i_ino);
  1153. /*
  1154. * VFS has already synchronized dirty pages for this inode. Synchronize
  1155. * the inode unless this is a 'datasync()' call.
  1156. */
  1157. if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) {
  1158. err = inode->i_sb->s_op->write_inode(inode, 1);
  1159. if (err)
  1160. return err;
  1161. }
  1162. /*
  1163. * Nodes related to this inode may still sit in a write-buffer. Flush
  1164. * them.
  1165. */
  1166. err = ubifs_sync_wbufs_by_inode(c, inode);
  1167. if (err)
  1168. return err;
  1169. return 0;
  1170. }
  1171. /**
  1172. * mctime_update_needed - check if mtime or ctime update is needed.
  1173. * @inode: the inode to do the check for
  1174. * @now: current time
  1175. *
  1176. * This helper function checks if the inode mtime/ctime should be updated or
  1177. * not. If current values of the time-stamps are within the UBIFS inode time
  1178. * granularity, they are not updated. This is an optimization.
  1179. */
  1180. static inline int mctime_update_needed(const struct inode *inode,
  1181. const struct timespec *now)
  1182. {
  1183. if (!timespec_equal(&inode->i_mtime, now) ||
  1184. !timespec_equal(&inode->i_ctime, now))
  1185. return 1;
  1186. return 0;
  1187. }
  1188. /**
  1189. * update_ctime - update mtime and ctime of an inode.
  1190. * @c: UBIFS file-system description object
  1191. * @inode: inode to update
  1192. *
  1193. * This function updates mtime and ctime of the inode if it is not equivalent to
  1194. * current time. Returns zero in case of success and a negative error code in
  1195. * case of failure.
  1196. */
  1197. static int update_mctime(struct ubifs_info *c, struct inode *inode)
  1198. {
  1199. struct timespec now = ubifs_current_time(inode);
  1200. struct ubifs_inode *ui = ubifs_inode(inode);
  1201. if (mctime_update_needed(inode, &now)) {
  1202. int err, release;
  1203. struct ubifs_budget_req req = { .dirtied_ino = 1,
  1204. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  1205. err = ubifs_budget_space(c, &req);
  1206. if (err)
  1207. return err;
  1208. mutex_lock(&ui->ui_mutex);
  1209. inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
  1210. release = ui->dirty;
  1211. mark_inode_dirty_sync(inode);
  1212. mutex_unlock(&ui->ui_mutex);
  1213. if (release)
  1214. ubifs_release_budget(c, &req);
  1215. }
  1216. return 0;
  1217. }
  1218. static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1219. unsigned long nr_segs, loff_t pos)
  1220. {
  1221. int err;
  1222. ssize_t ret;
  1223. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1224. struct ubifs_info *c = inode->i_sb->s_fs_info;
  1225. err = update_mctime(c, inode);
  1226. if (err)
  1227. return err;
  1228. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  1229. if (ret < 0)
  1230. return ret;
  1231. if (ret > 0 && (IS_SYNC(inode) || iocb->ki_filp->f_flags & O_SYNC)) {
  1232. err = ubifs_sync_wbufs_by_inode(c, inode);
  1233. if (err)
  1234. return err;
  1235. }
  1236. return ret;
  1237. }
  1238. static int ubifs_set_page_dirty(struct page *page)
  1239. {
  1240. int ret;
  1241. ret = __set_page_dirty_nobuffers(page);
  1242. /*
  1243. * An attempt to dirty a page without budgeting for it - should not
  1244. * happen.
  1245. */
  1246. ubifs_assert(ret == 0);
  1247. return ret;
  1248. }
  1249. static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags)
  1250. {
  1251. /*
  1252. * An attempt to release a dirty page without budgeting for it - should
  1253. * not happen.
  1254. */
  1255. if (PageWriteback(page))
  1256. return 0;
  1257. ubifs_assert(PagePrivate(page));
  1258. ubifs_assert(0);
  1259. ClearPagePrivate(page);
  1260. ClearPageChecked(page);
  1261. return 1;
  1262. }
  1263. /*
  1264. * mmap()d file has taken write protection fault and is being made
  1265. * writable. UBIFS must ensure page is budgeted for.
  1266. */
  1267. static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  1268. {
  1269. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1270. struct ubifs_info *c = inode->i_sb->s_fs_info;
  1271. struct timespec now = ubifs_current_time(inode);
  1272. struct ubifs_budget_req req = { .new_page = 1 };
  1273. int err, update_time;
  1274. dbg_gen("ino %lu, pg %lu, i_size %lld", inode->i_ino, page->index,
  1275. i_size_read(inode));
  1276. ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY));
  1277. if (unlikely(c->ro_media))
  1278. return -EROFS;
  1279. /*
  1280. * We have not locked @page so far so we may budget for changing the
  1281. * page. Note, we cannot do this after we locked the page, because
  1282. * budgeting may cause write-back which would cause deadlock.
  1283. *
  1284. * At the moment we do not know whether the page is dirty or not, so we
  1285. * assume that it is not and budget for a new page. We could look at
  1286. * the @PG_private flag and figure this out, but we may race with write
  1287. * back and the page state may change by the time we lock it, so this
  1288. * would need additional care. We do not bother with this at the
  1289. * moment, although it might be good idea to do. Instead, we allocate
  1290. * budget for a new page and amend it later on if the page was in fact
  1291. * dirty.
  1292. *
  1293. * The budgeting-related logic of this function is similar to what we
  1294. * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
  1295. * for more comments.
  1296. */
  1297. update_time = mctime_update_needed(inode, &now);
  1298. if (update_time)
  1299. /*
  1300. * We have to change inode time stamp which requires extra
  1301. * budgeting.
  1302. */
  1303. req.dirtied_ino = 1;
  1304. err = ubifs_budget_space(c, &req);
  1305. if (unlikely(err)) {
  1306. if (err == -ENOSPC)
  1307. ubifs_warn("out of space for mmapped file "
  1308. "(inode number %lu)", inode->i_ino);
  1309. return err;
  1310. }
  1311. lock_page(page);
  1312. if (unlikely(page->mapping != inode->i_mapping ||
  1313. page_offset(page) > i_size_read(inode))) {
  1314. /* Page got truncated out from underneath us */
  1315. err = -EINVAL;
  1316. goto out_unlock;
  1317. }
  1318. if (PagePrivate(page))
  1319. release_new_page_budget(c);
  1320. else {
  1321. if (!PageChecked(page))
  1322. ubifs_convert_page_budget(c);
  1323. SetPagePrivate(page);
  1324. atomic_long_inc(&c->dirty_pg_cnt);
  1325. __set_page_dirty_nobuffers(page);
  1326. }
  1327. if (update_time) {
  1328. int release;
  1329. struct ubifs_inode *ui = ubifs_inode(inode);
  1330. mutex_lock(&ui->ui_mutex);
  1331. inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
  1332. release = ui->dirty;
  1333. mark_inode_dirty_sync(inode);
  1334. mutex_unlock(&ui->ui_mutex);
  1335. if (release)
  1336. ubifs_release_dirty_inode_budget(c, ui);
  1337. }
  1338. unlock_page(page);
  1339. return 0;
  1340. out_unlock:
  1341. unlock_page(page);
  1342. ubifs_release_budget(c, &req);
  1343. return err;
  1344. }
  1345. static struct vm_operations_struct ubifs_file_vm_ops = {
  1346. .fault = filemap_fault,
  1347. .page_mkwrite = ubifs_vm_page_mkwrite,
  1348. };
  1349. static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  1350. {
  1351. int err;
  1352. /* 'generic_file_mmap()' takes care of NOMMU case */
  1353. err = generic_file_mmap(file, vma);
  1354. if (err)
  1355. return err;
  1356. vma->vm_ops = &ubifs_file_vm_ops;
  1357. return 0;
  1358. }
  1359. struct address_space_operations ubifs_file_address_operations = {
  1360. .readpage = ubifs_readpage,
  1361. .writepage = ubifs_writepage,
  1362. .write_begin = ubifs_write_begin,
  1363. .write_end = ubifs_write_end,
  1364. .invalidatepage = ubifs_invalidatepage,
  1365. .set_page_dirty = ubifs_set_page_dirty,
  1366. .releasepage = ubifs_releasepage,
  1367. };
  1368. struct inode_operations ubifs_file_inode_operations = {
  1369. .setattr = ubifs_setattr,
  1370. .getattr = ubifs_getattr,
  1371. #ifdef CONFIG_UBIFS_FS_XATTR
  1372. .setxattr = ubifs_setxattr,
  1373. .getxattr = ubifs_getxattr,
  1374. .listxattr = ubifs_listxattr,
  1375. .removexattr = ubifs_removexattr,
  1376. #endif
  1377. };
  1378. struct inode_operations ubifs_symlink_inode_operations = {
  1379. .readlink = generic_readlink,
  1380. .follow_link = ubifs_follow_link,
  1381. .setattr = ubifs_setattr,
  1382. .getattr = ubifs_getattr,
  1383. };
  1384. struct file_operations ubifs_file_operations = {
  1385. .llseek = generic_file_llseek,
  1386. .read = do_sync_read,
  1387. .write = do_sync_write,
  1388. .aio_read = generic_file_aio_read,
  1389. .aio_write = ubifs_aio_write,
  1390. .mmap = ubifs_file_mmap,
  1391. .fsync = ubifs_fsync,
  1392. .unlocked_ioctl = ubifs_ioctl,
  1393. .splice_read = generic_file_splice_read,
  1394. .splice_write = generic_file_splice_write,
  1395. #ifdef CONFIG_COMPAT
  1396. .compat_ioctl = ubifs_compat_ioctl,
  1397. #endif
  1398. };