file.c 48 KB

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