file.c 44 KB

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