file.c 45 KB

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