file.c 45 KB

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