file.c 45 KB

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