file.c 46 KB

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