file.c 44 KB

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