file.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  1. /*
  2. * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/buffer_head.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/pagevec.h>
  24. #include <linux/sched.h>
  25. #include <linux/swap.h>
  26. #include <linux/uio.h>
  27. #include <linux/writeback.h>
  28. #include <asm/page.h>
  29. #include <asm/uaccess.h>
  30. #include "attrib.h"
  31. #include "bitmap.h"
  32. #include "inode.h"
  33. #include "debug.h"
  34. #include "lcnalloc.h"
  35. #include "malloc.h"
  36. #include "mft.h"
  37. #include "ntfs.h"
  38. /**
  39. * ntfs_file_open - called when an inode is about to be opened
  40. * @vi: inode to be opened
  41. * @filp: file structure describing the inode
  42. *
  43. * Limit file size to the page cache limit on architectures where unsigned long
  44. * is 32-bits. This is the most we can do for now without overflowing the page
  45. * cache page index. Doing it this way means we don't run into problems because
  46. * of existing too large files. It would be better to allow the user to read
  47. * the beginning of the file but I doubt very much anyone is going to hit this
  48. * check on a 32-bit architecture, so there is no point in adding the extra
  49. * complexity required to support this.
  50. *
  51. * On 64-bit architectures, the check is hopefully optimized away by the
  52. * compiler.
  53. *
  54. * After the check passes, just call generic_file_open() to do its work.
  55. */
  56. static int ntfs_file_open(struct inode *vi, struct file *filp)
  57. {
  58. if (sizeof(unsigned long) < 8) {
  59. if (i_size_read(vi) > MAX_LFS_FILESIZE)
  60. return -EFBIG;
  61. }
  62. return generic_file_open(vi, filp);
  63. }
  64. #ifdef NTFS_RW
  65. /**
  66. * ntfs_attr_extend_initialized - extend the initialized size of an attribute
  67. * @ni: ntfs inode of the attribute to extend
  68. * @new_init_size: requested new initialized size in bytes
  69. * @cached_page: store any allocated but unused page here
  70. * @lru_pvec: lru-buffering pagevec of the caller
  71. *
  72. * Extend the initialized size of an attribute described by the ntfs inode @ni
  73. * to @new_init_size bytes. This involves zeroing any non-sparse space between
  74. * the old initialized size and @new_init_size both in the page cache and on
  75. * disk (if relevant complete pages are already uptodate in the page cache then
  76. * these are simply marked dirty).
  77. *
  78. * As a side-effect, the file size (vfs inode->i_size) may be incremented as,
  79. * in the resident attribute case, it is tied to the initialized size and, in
  80. * the non-resident attribute case, it may not fall below the initialized size.
  81. *
  82. * Note that if the attribute is resident, we do not need to touch the page
  83. * cache at all. This is because if the page cache page is not uptodate we
  84. * bring it uptodate later, when doing the write to the mft record since we
  85. * then already have the page mapped. And if the page is uptodate, the
  86. * non-initialized region will already have been zeroed when the page was
  87. * brought uptodate and the region may in fact already have been overwritten
  88. * with new data via mmap() based writes, so we cannot just zero it. And since
  89. * POSIX specifies that the behaviour of resizing a file whilst it is mmap()ped
  90. * is unspecified, we choose not to do zeroing and thus we do not need to touch
  91. * the page at all. For a more detailed explanation see ntfs_truncate() in
  92. * fs/ntfs/inode.c.
  93. *
  94. * @cached_page and @lru_pvec are just optimizations for dealing with multiple
  95. * pages.
  96. *
  97. * Return 0 on success and -errno on error. In the case that an error is
  98. * encountered it is possible that the initialized size will already have been
  99. * incremented some way towards @new_init_size but it is guaranteed that if
  100. * this is the case, the necessary zeroing will also have happened and that all
  101. * metadata is self-consistent.
  102. *
  103. * Locking: i_sem on the vfs inode corrseponsind to the ntfs inode @ni must be
  104. * held by the caller.
  105. */
  106. static int ntfs_attr_extend_initialized(ntfs_inode *ni, const s64 new_init_size,
  107. struct page **cached_page, struct pagevec *lru_pvec)
  108. {
  109. s64 old_init_size;
  110. loff_t old_i_size;
  111. pgoff_t index, end_index;
  112. unsigned long flags;
  113. struct inode *vi = VFS_I(ni);
  114. ntfs_inode *base_ni;
  115. MFT_RECORD *m = NULL;
  116. ATTR_RECORD *a;
  117. ntfs_attr_search_ctx *ctx = NULL;
  118. struct address_space *mapping;
  119. struct page *page = NULL;
  120. u8 *kattr;
  121. int err;
  122. u32 attr_len;
  123. read_lock_irqsave(&ni->size_lock, flags);
  124. old_init_size = ni->initialized_size;
  125. old_i_size = i_size_read(vi);
  126. BUG_ON(new_init_size > ni->allocated_size);
  127. read_unlock_irqrestore(&ni->size_lock, flags);
  128. ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
  129. "old_initialized_size 0x%llx, "
  130. "new_initialized_size 0x%llx, i_size 0x%llx.",
  131. vi->i_ino, (unsigned)le32_to_cpu(ni->type),
  132. (unsigned long long)old_init_size,
  133. (unsigned long long)new_init_size, old_i_size);
  134. if (!NInoAttr(ni))
  135. base_ni = ni;
  136. else
  137. base_ni = ni->ext.base_ntfs_ino;
  138. /* Use goto to reduce indentation and we need the label below anyway. */
  139. if (NInoNonResident(ni))
  140. goto do_non_resident_extend;
  141. BUG_ON(old_init_size != old_i_size);
  142. m = map_mft_record(base_ni);
  143. if (IS_ERR(m)) {
  144. err = PTR_ERR(m);
  145. m = NULL;
  146. goto err_out;
  147. }
  148. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  149. if (unlikely(!ctx)) {
  150. err = -ENOMEM;
  151. goto err_out;
  152. }
  153. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  154. CASE_SENSITIVE, 0, NULL, 0, ctx);
  155. if (unlikely(err)) {
  156. if (err == -ENOENT)
  157. err = -EIO;
  158. goto err_out;
  159. }
  160. m = ctx->mrec;
  161. a = ctx->attr;
  162. BUG_ON(a->non_resident);
  163. /* The total length of the attribute value. */
  164. attr_len = le32_to_cpu(a->data.resident.value_length);
  165. BUG_ON(old_i_size != (loff_t)attr_len);
  166. /*
  167. * Do the zeroing in the mft record and update the attribute size in
  168. * the mft record.
  169. */
  170. kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
  171. memset(kattr + attr_len, 0, new_init_size - attr_len);
  172. a->data.resident.value_length = cpu_to_le32((u32)new_init_size);
  173. /* Finally, update the sizes in the vfs and ntfs inodes. */
  174. write_lock_irqsave(&ni->size_lock, flags);
  175. i_size_write(vi, new_init_size);
  176. ni->initialized_size = new_init_size;
  177. write_unlock_irqrestore(&ni->size_lock, flags);
  178. goto done;
  179. do_non_resident_extend:
  180. /*
  181. * If the new initialized size @new_init_size exceeds the current file
  182. * size (vfs inode->i_size), we need to extend the file size to the
  183. * new initialized size.
  184. */
  185. if (new_init_size > old_i_size) {
  186. m = map_mft_record(base_ni);
  187. if (IS_ERR(m)) {
  188. err = PTR_ERR(m);
  189. m = NULL;
  190. goto err_out;
  191. }
  192. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  193. if (unlikely(!ctx)) {
  194. err = -ENOMEM;
  195. goto err_out;
  196. }
  197. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  198. CASE_SENSITIVE, 0, NULL, 0, ctx);
  199. if (unlikely(err)) {
  200. if (err == -ENOENT)
  201. err = -EIO;
  202. goto err_out;
  203. }
  204. m = ctx->mrec;
  205. a = ctx->attr;
  206. BUG_ON(!a->non_resident);
  207. BUG_ON(old_i_size != (loff_t)
  208. sle64_to_cpu(a->data.non_resident.data_size));
  209. a->data.non_resident.data_size = cpu_to_sle64(new_init_size);
  210. flush_dcache_mft_record_page(ctx->ntfs_ino);
  211. mark_mft_record_dirty(ctx->ntfs_ino);
  212. /* Update the file size in the vfs inode. */
  213. i_size_write(vi, new_init_size);
  214. ntfs_attr_put_search_ctx(ctx);
  215. ctx = NULL;
  216. unmap_mft_record(base_ni);
  217. m = NULL;
  218. }
  219. mapping = vi->i_mapping;
  220. index = old_init_size >> PAGE_CACHE_SHIFT;
  221. end_index = (new_init_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  222. do {
  223. /*
  224. * Read the page. If the page is not present, this will zero
  225. * the uninitialized regions for us.
  226. */
  227. page = read_cache_page(mapping, index,
  228. (filler_t*)mapping->a_ops->readpage, NULL);
  229. if (IS_ERR(page)) {
  230. err = PTR_ERR(page);
  231. goto init_err_out;
  232. }
  233. wait_on_page_locked(page);
  234. if (unlikely(!PageUptodate(page) || PageError(page))) {
  235. page_cache_release(page);
  236. err = -EIO;
  237. goto init_err_out;
  238. }
  239. /*
  240. * Update the initialized size in the ntfs inode. This is
  241. * enough to make ntfs_writepage() work.
  242. */
  243. write_lock_irqsave(&ni->size_lock, flags);
  244. ni->initialized_size = (index + 1) << PAGE_CACHE_SHIFT;
  245. if (ni->initialized_size > new_init_size)
  246. ni->initialized_size = new_init_size;
  247. write_unlock_irqrestore(&ni->size_lock, flags);
  248. /* Set the page dirty so it gets written out. */
  249. set_page_dirty(page);
  250. page_cache_release(page);
  251. /*
  252. * Play nice with the vm and the rest of the system. This is
  253. * very much needed as we can potentially be modifying the
  254. * initialised size from a very small value to a really huge
  255. * value, e.g.
  256. * f = open(somefile, O_TRUNC);
  257. * truncate(f, 10GiB);
  258. * seek(f, 10GiB);
  259. * write(f, 1);
  260. * And this would mean we would be marking dirty hundreds of
  261. * thousands of pages or as in the above example more than
  262. * two and a half million pages!
  263. *
  264. * TODO: For sparse pages could optimize this workload by using
  265. * the FsMisc / MiscFs page bit as a "PageIsSparse" bit. This
  266. * would be set in readpage for sparse pages and here we would
  267. * not need to mark dirty any pages which have this bit set.
  268. * The only caveat is that we have to clear the bit everywhere
  269. * where we allocate any clusters that lie in the page or that
  270. * contain the page.
  271. *
  272. * TODO: An even greater optimization would be for us to only
  273. * call readpage() on pages which are not in sparse regions as
  274. * determined from the runlist. This would greatly reduce the
  275. * number of pages we read and make dirty in the case of sparse
  276. * files.
  277. */
  278. balance_dirty_pages_ratelimited(mapping);
  279. cond_resched();
  280. } while (++index < end_index);
  281. read_lock_irqsave(&ni->size_lock, flags);
  282. BUG_ON(ni->initialized_size != new_init_size);
  283. read_unlock_irqrestore(&ni->size_lock, flags);
  284. /* Now bring in sync the initialized_size in the mft record. */
  285. m = map_mft_record(base_ni);
  286. if (IS_ERR(m)) {
  287. err = PTR_ERR(m);
  288. m = NULL;
  289. goto init_err_out;
  290. }
  291. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  292. if (unlikely(!ctx)) {
  293. err = -ENOMEM;
  294. goto init_err_out;
  295. }
  296. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  297. CASE_SENSITIVE, 0, NULL, 0, ctx);
  298. if (unlikely(err)) {
  299. if (err == -ENOENT)
  300. err = -EIO;
  301. goto init_err_out;
  302. }
  303. m = ctx->mrec;
  304. a = ctx->attr;
  305. BUG_ON(!a->non_resident);
  306. a->data.non_resident.initialized_size = cpu_to_sle64(new_init_size);
  307. done:
  308. flush_dcache_mft_record_page(ctx->ntfs_ino);
  309. mark_mft_record_dirty(ctx->ntfs_ino);
  310. if (ctx)
  311. ntfs_attr_put_search_ctx(ctx);
  312. if (m)
  313. unmap_mft_record(base_ni);
  314. ntfs_debug("Done, initialized_size 0x%llx, i_size 0x%llx.",
  315. (unsigned long long)new_init_size, i_size_read(vi));
  316. return 0;
  317. init_err_out:
  318. write_lock_irqsave(&ni->size_lock, flags);
  319. ni->initialized_size = old_init_size;
  320. write_unlock_irqrestore(&ni->size_lock, flags);
  321. err_out:
  322. if (ctx)
  323. ntfs_attr_put_search_ctx(ctx);
  324. if (m)
  325. unmap_mft_record(base_ni);
  326. ntfs_debug("Failed. Returning error code %i.", err);
  327. return err;
  328. }
  329. /**
  330. * ntfs_fault_in_pages_readable -
  331. *
  332. * Fault a number of userspace pages into pagetables.
  333. *
  334. * Unlike include/linux/pagemap.h::fault_in_pages_readable(), this one copes
  335. * with more than two userspace pages as well as handling the single page case
  336. * elegantly.
  337. *
  338. * If you find this difficult to understand, then think of the while loop being
  339. * the following code, except that we do without the integer variable ret:
  340. *
  341. * do {
  342. * ret = __get_user(c, uaddr);
  343. * uaddr += PAGE_SIZE;
  344. * } while (!ret && uaddr < end);
  345. *
  346. * Note, the final __get_user() may well run out-of-bounds of the user buffer,
  347. * but _not_ out-of-bounds of the page the user buffer belongs to, and since
  348. * this is only a read and not a write, and since it is still in the same page,
  349. * it should not matter and this makes the code much simpler.
  350. */
  351. static inline void ntfs_fault_in_pages_readable(const char __user *uaddr,
  352. int bytes)
  353. {
  354. const char __user *end;
  355. volatile char c;
  356. /* Set @end to the first byte outside the last page we care about. */
  357. end = (const char __user*)PAGE_ALIGN((ptrdiff_t __user)uaddr + bytes);
  358. while (!__get_user(c, uaddr) && (uaddr += PAGE_SIZE, uaddr < end))
  359. ;
  360. }
  361. /**
  362. * ntfs_fault_in_pages_readable_iovec -
  363. *
  364. * Same as ntfs_fault_in_pages_readable() but operates on an array of iovecs.
  365. */
  366. static inline void ntfs_fault_in_pages_readable_iovec(const struct iovec *iov,
  367. size_t iov_ofs, int bytes)
  368. {
  369. do {
  370. const char __user *buf;
  371. unsigned len;
  372. buf = iov->iov_base + iov_ofs;
  373. len = iov->iov_len - iov_ofs;
  374. if (len > bytes)
  375. len = bytes;
  376. ntfs_fault_in_pages_readable(buf, len);
  377. bytes -= len;
  378. iov++;
  379. iov_ofs = 0;
  380. } while (bytes);
  381. }
  382. /**
  383. * __ntfs_grab_cache_pages - obtain a number of locked pages
  384. * @mapping: address space mapping from which to obtain page cache pages
  385. * @index: starting index in @mapping at which to begin obtaining pages
  386. * @nr_pages: number of page cache pages to obtain
  387. * @pages: array of pages in which to return the obtained page cache pages
  388. * @cached_page: allocated but as yet unused page
  389. * @lru_pvec: lru-buffering pagevec of caller
  390. *
  391. * Obtain @nr_pages locked page cache pages from the mapping @maping and
  392. * starting at index @index.
  393. *
  394. * If a page is newly created, increment its refcount and add it to the
  395. * caller's lru-buffering pagevec @lru_pvec.
  396. *
  397. * This is the same as mm/filemap.c::__grab_cache_page(), except that @nr_pages
  398. * are obtained at once instead of just one page and that 0 is returned on
  399. * success and -errno on error.
  400. *
  401. * Note, the page locks are obtained in ascending page index order.
  402. */
  403. static inline int __ntfs_grab_cache_pages(struct address_space *mapping,
  404. pgoff_t index, const unsigned nr_pages, struct page **pages,
  405. struct page **cached_page, struct pagevec *lru_pvec)
  406. {
  407. int err, nr;
  408. BUG_ON(!nr_pages);
  409. err = nr = 0;
  410. do {
  411. pages[nr] = find_lock_page(mapping, index);
  412. if (!pages[nr]) {
  413. if (!*cached_page) {
  414. *cached_page = page_cache_alloc(mapping);
  415. if (unlikely(!*cached_page)) {
  416. err = -ENOMEM;
  417. goto err_out;
  418. }
  419. }
  420. err = add_to_page_cache(*cached_page, mapping, index,
  421. GFP_KERNEL);
  422. if (unlikely(err)) {
  423. if (err == -EEXIST)
  424. continue;
  425. goto err_out;
  426. }
  427. pages[nr] = *cached_page;
  428. page_cache_get(*cached_page);
  429. if (unlikely(!pagevec_add(lru_pvec, *cached_page)))
  430. __pagevec_lru_add(lru_pvec);
  431. *cached_page = NULL;
  432. }
  433. index++;
  434. nr++;
  435. } while (nr < nr_pages);
  436. out:
  437. return err;
  438. err_out:
  439. while (nr > 0) {
  440. unlock_page(pages[--nr]);
  441. page_cache_release(pages[nr]);
  442. }
  443. goto out;
  444. }
  445. static inline int ntfs_submit_bh_for_read(struct buffer_head *bh)
  446. {
  447. lock_buffer(bh);
  448. get_bh(bh);
  449. bh->b_end_io = end_buffer_read_sync;
  450. return submit_bh(READ, bh);
  451. }
  452. /**
  453. * ntfs_prepare_pages_for_non_resident_write - prepare pages for receiving data
  454. * @pages: array of destination pages
  455. * @nr_pages: number of pages in @pages
  456. * @pos: byte position in file at which the write begins
  457. * @bytes: number of bytes to be written
  458. *
  459. * This is called for non-resident attributes from ntfs_file_buffered_write()
  460. * with i_sem held on the inode (@pages[0]->mapping->host). There are
  461. * @nr_pages pages in @pages which are locked but not kmap()ped. The source
  462. * data has not yet been copied into the @pages.
  463. *
  464. * Need to fill any holes with actual clusters, allocate buffers if necessary,
  465. * ensure all the buffers are mapped, and bring uptodate any buffers that are
  466. * only partially being written to.
  467. *
  468. * If @nr_pages is greater than one, we are guaranteed that the cluster size is
  469. * greater than PAGE_CACHE_SIZE, that all pages in @pages are entirely inside
  470. * the same cluster and that they are the entirety of that cluster, and that
  471. * the cluster is sparse, i.e. we need to allocate a cluster to fill the hole.
  472. *
  473. * i_size is not to be modified yet.
  474. *
  475. * Return 0 on success or -errno on error.
  476. */
  477. static int ntfs_prepare_pages_for_non_resident_write(struct page **pages,
  478. unsigned nr_pages, s64 pos, size_t bytes)
  479. {
  480. VCN vcn, highest_vcn = 0, cpos, cend, bh_cpos, bh_cend;
  481. LCN lcn;
  482. s64 bh_pos, vcn_len, end, initialized_size;
  483. sector_t lcn_block;
  484. struct page *page;
  485. struct inode *vi;
  486. ntfs_inode *ni, *base_ni = NULL;
  487. ntfs_volume *vol;
  488. runlist_element *rl, *rl2;
  489. struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
  490. ntfs_attr_search_ctx *ctx = NULL;
  491. MFT_RECORD *m = NULL;
  492. ATTR_RECORD *a = NULL;
  493. unsigned long flags;
  494. u32 attr_rec_len = 0;
  495. unsigned blocksize, u;
  496. int err, mp_size;
  497. BOOL rl_write_locked, was_hole, is_retry;
  498. unsigned char blocksize_bits;
  499. struct {
  500. u8 runlist_merged:1;
  501. u8 mft_attr_mapped:1;
  502. u8 mp_rebuilt:1;
  503. u8 attr_switched:1;
  504. } status = { 0, 0, 0, 0 };
  505. BUG_ON(!nr_pages);
  506. BUG_ON(!pages);
  507. BUG_ON(!*pages);
  508. vi = pages[0]->mapping->host;
  509. ni = NTFS_I(vi);
  510. vol = ni->vol;
  511. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
  512. "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.",
  513. vi->i_ino, ni->type, pages[0]->index, nr_pages,
  514. (long long)pos, bytes);
  515. blocksize_bits = vi->i_blkbits;
  516. blocksize = 1 << blocksize_bits;
  517. u = 0;
  518. do {
  519. struct page *page = pages[u];
  520. /*
  521. * create_empty_buffers() will create uptodate/dirty buffers if
  522. * the page is uptodate/dirty.
  523. */
  524. if (!page_has_buffers(page)) {
  525. create_empty_buffers(page, blocksize, 0);
  526. if (unlikely(!page_has_buffers(page)))
  527. return -ENOMEM;
  528. }
  529. } while (++u < nr_pages);
  530. rl_write_locked = FALSE;
  531. rl = NULL;
  532. err = 0;
  533. vcn = lcn = -1;
  534. vcn_len = 0;
  535. lcn_block = -1;
  536. was_hole = FALSE;
  537. cpos = pos >> vol->cluster_size_bits;
  538. end = pos + bytes;
  539. cend = (end + vol->cluster_size - 1) >> vol->cluster_size_bits;
  540. /*
  541. * Loop over each page and for each page over each buffer. Use goto to
  542. * reduce indentation.
  543. */
  544. u = 0;
  545. do_next_page:
  546. page = pages[u];
  547. bh_pos = (s64)page->index << PAGE_CACHE_SHIFT;
  548. bh = head = page_buffers(page);
  549. do {
  550. VCN cdelta;
  551. s64 bh_end;
  552. unsigned bh_cofs;
  553. /* Clear buffer_new on all buffers to reinitialise state. */
  554. if (buffer_new(bh))
  555. clear_buffer_new(bh);
  556. bh_end = bh_pos + blocksize;
  557. bh_cpos = bh_pos >> vol->cluster_size_bits;
  558. bh_cofs = bh_pos & vol->cluster_size_mask;
  559. if (buffer_mapped(bh)) {
  560. /*
  561. * The buffer is already mapped. If it is uptodate,
  562. * ignore it.
  563. */
  564. if (buffer_uptodate(bh))
  565. continue;
  566. /*
  567. * The buffer is not uptodate. If the page is uptodate
  568. * set the buffer uptodate and otherwise ignore it.
  569. */
  570. if (PageUptodate(page)) {
  571. set_buffer_uptodate(bh);
  572. continue;
  573. }
  574. /*
  575. * Neither the page nor the buffer are uptodate. If
  576. * the buffer is only partially being written to, we
  577. * need to read it in before the write, i.e. now.
  578. */
  579. if ((bh_pos < pos && bh_end > pos) ||
  580. (bh_pos < end && bh_end > end)) {
  581. /*
  582. * If the buffer is fully or partially within
  583. * the initialized size, do an actual read.
  584. * Otherwise, simply zero the buffer.
  585. */
  586. read_lock_irqsave(&ni->size_lock, flags);
  587. initialized_size = ni->initialized_size;
  588. read_unlock_irqrestore(&ni->size_lock, flags);
  589. if (bh_pos < initialized_size) {
  590. ntfs_submit_bh_for_read(bh);
  591. *wait_bh++ = bh;
  592. } else {
  593. u8 *kaddr = kmap_atomic(page, KM_USER0);
  594. memset(kaddr + bh_offset(bh), 0,
  595. blocksize);
  596. kunmap_atomic(kaddr, KM_USER0);
  597. flush_dcache_page(page);
  598. set_buffer_uptodate(bh);
  599. }
  600. }
  601. continue;
  602. }
  603. /* Unmapped buffer. Need to map it. */
  604. bh->b_bdev = vol->sb->s_bdev;
  605. /*
  606. * If the current buffer is in the same clusters as the map
  607. * cache, there is no need to check the runlist again. The
  608. * map cache is made up of @vcn, which is the first cached file
  609. * cluster, @vcn_len which is the number of cached file
  610. * clusters, @lcn is the device cluster corresponding to @vcn,
  611. * and @lcn_block is the block number corresponding to @lcn.
  612. */
  613. cdelta = bh_cpos - vcn;
  614. if (likely(!cdelta || (cdelta > 0 && cdelta < vcn_len))) {
  615. map_buffer_cached:
  616. BUG_ON(lcn < 0);
  617. bh->b_blocknr = lcn_block +
  618. (cdelta << (vol->cluster_size_bits -
  619. blocksize_bits)) +
  620. (bh_cofs >> blocksize_bits);
  621. set_buffer_mapped(bh);
  622. /*
  623. * If the page is uptodate so is the buffer. If the
  624. * buffer is fully outside the write, we ignore it if
  625. * it was already allocated and we mark it dirty so it
  626. * gets written out if we allocated it. On the other
  627. * hand, if we allocated the buffer but we are not
  628. * marking it dirty we set buffer_new so we can do
  629. * error recovery.
  630. */
  631. if (PageUptodate(page)) {
  632. if (!buffer_uptodate(bh))
  633. set_buffer_uptodate(bh);
  634. if (unlikely(was_hole)) {
  635. /* We allocated the buffer. */
  636. unmap_underlying_metadata(bh->b_bdev,
  637. bh->b_blocknr);
  638. if (bh_end <= pos || bh_pos >= end)
  639. mark_buffer_dirty(bh);
  640. else
  641. set_buffer_new(bh);
  642. }
  643. continue;
  644. }
  645. /* Page is _not_ uptodate. */
  646. if (likely(!was_hole)) {
  647. /*
  648. * Buffer was already allocated. If it is not
  649. * uptodate and is only partially being written
  650. * to, we need to read it in before the write,
  651. * i.e. now.
  652. */
  653. if (!buffer_uptodate(bh) && ((bh_pos < pos &&
  654. bh_end > pos) ||
  655. (bh_end > end &&
  656. bh_end > end))) {
  657. /*
  658. * If the buffer is fully or partially
  659. * within the initialized size, do an
  660. * actual read. Otherwise, simply zero
  661. * the buffer.
  662. */
  663. read_lock_irqsave(&ni->size_lock,
  664. flags);
  665. initialized_size = ni->initialized_size;
  666. read_unlock_irqrestore(&ni->size_lock,
  667. flags);
  668. if (bh_pos < initialized_size) {
  669. ntfs_submit_bh_for_read(bh);
  670. *wait_bh++ = bh;
  671. } else {
  672. u8 *kaddr = kmap_atomic(page,
  673. KM_USER0);
  674. memset(kaddr + bh_offset(bh),
  675. 0, blocksize);
  676. kunmap_atomic(kaddr, KM_USER0);
  677. flush_dcache_page(page);
  678. set_buffer_uptodate(bh);
  679. }
  680. }
  681. continue;
  682. }
  683. /* We allocated the buffer. */
  684. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  685. /*
  686. * If the buffer is fully outside the write, zero it,
  687. * set it uptodate, and mark it dirty so it gets
  688. * written out. If it is partially being written to,
  689. * zero region surrounding the write but leave it to
  690. * commit write to do anything else. Finally, if the
  691. * buffer is fully being overwritten, do nothing.
  692. */
  693. if (bh_end <= pos || bh_pos >= end) {
  694. if (!buffer_uptodate(bh)) {
  695. u8 *kaddr = kmap_atomic(page, KM_USER0);
  696. memset(kaddr + bh_offset(bh), 0,
  697. blocksize);
  698. kunmap_atomic(kaddr, KM_USER0);
  699. flush_dcache_page(page);
  700. set_buffer_uptodate(bh);
  701. }
  702. mark_buffer_dirty(bh);
  703. continue;
  704. }
  705. set_buffer_new(bh);
  706. if (!buffer_uptodate(bh) &&
  707. (bh_pos < pos || bh_end > end)) {
  708. u8 *kaddr;
  709. unsigned pofs;
  710. kaddr = kmap_atomic(page, KM_USER0);
  711. if (bh_pos < pos) {
  712. pofs = bh_pos & ~PAGE_CACHE_MASK;
  713. memset(kaddr + pofs, 0, pos - bh_pos);
  714. }
  715. if (bh_end > end) {
  716. pofs = end & ~PAGE_CACHE_MASK;
  717. memset(kaddr + pofs, 0, bh_end - end);
  718. }
  719. kunmap_atomic(kaddr, KM_USER0);
  720. flush_dcache_page(page);
  721. }
  722. continue;
  723. }
  724. /*
  725. * Slow path: this is the first buffer in the cluster. If it
  726. * is outside allocated size and is not uptodate, zero it and
  727. * set it uptodate.
  728. */
  729. read_lock_irqsave(&ni->size_lock, flags);
  730. initialized_size = ni->allocated_size;
  731. read_unlock_irqrestore(&ni->size_lock, flags);
  732. if (bh_pos > initialized_size) {
  733. if (PageUptodate(page)) {
  734. if (!buffer_uptodate(bh))
  735. set_buffer_uptodate(bh);
  736. } else if (!buffer_uptodate(bh)) {
  737. u8 *kaddr = kmap_atomic(page, KM_USER0);
  738. memset(kaddr + bh_offset(bh), 0, blocksize);
  739. kunmap_atomic(kaddr, KM_USER0);
  740. flush_dcache_page(page);
  741. set_buffer_uptodate(bh);
  742. }
  743. continue;
  744. }
  745. is_retry = FALSE;
  746. if (!rl) {
  747. down_read(&ni->runlist.lock);
  748. retry_remap:
  749. rl = ni->runlist.rl;
  750. }
  751. if (likely(rl != NULL)) {
  752. /* Seek to element containing target cluster. */
  753. while (rl->length && rl[1].vcn <= bh_cpos)
  754. rl++;
  755. lcn = ntfs_rl_vcn_to_lcn(rl, bh_cpos);
  756. if (likely(lcn >= 0)) {
  757. /*
  758. * Successful remap, setup the map cache and
  759. * use that to deal with the buffer.
  760. */
  761. was_hole = FALSE;
  762. vcn = bh_cpos;
  763. vcn_len = rl[1].vcn - vcn;
  764. lcn_block = lcn << (vol->cluster_size_bits -
  765. blocksize_bits);
  766. cdelta = 0;
  767. /*
  768. * If the number of remaining clusters in the
  769. * @pages is smaller or equal to the number of
  770. * cached clusters, unlock the runlist as the
  771. * map cache will be used from now on.
  772. */
  773. if (likely(vcn + vcn_len >= cend)) {
  774. if (rl_write_locked) {
  775. up_write(&ni->runlist.lock);
  776. rl_write_locked = FALSE;
  777. } else
  778. up_read(&ni->runlist.lock);
  779. rl = NULL;
  780. }
  781. goto map_buffer_cached;
  782. }
  783. } else
  784. lcn = LCN_RL_NOT_MAPPED;
  785. /*
  786. * If it is not a hole and not out of bounds, the runlist is
  787. * probably unmapped so try to map it now.
  788. */
  789. if (unlikely(lcn != LCN_HOLE && lcn != LCN_ENOENT)) {
  790. if (likely(!is_retry && lcn == LCN_RL_NOT_MAPPED)) {
  791. /* Attempt to map runlist. */
  792. if (!rl_write_locked) {
  793. /*
  794. * We need the runlist locked for
  795. * writing, so if it is locked for
  796. * reading relock it now and retry in
  797. * case it changed whilst we dropped
  798. * the lock.
  799. */
  800. up_read(&ni->runlist.lock);
  801. down_write(&ni->runlist.lock);
  802. rl_write_locked = TRUE;
  803. goto retry_remap;
  804. }
  805. err = ntfs_map_runlist_nolock(ni, bh_cpos,
  806. NULL);
  807. if (likely(!err)) {
  808. is_retry = TRUE;
  809. goto retry_remap;
  810. }
  811. /*
  812. * If @vcn is out of bounds, pretend @lcn is
  813. * LCN_ENOENT. As long as the buffer is out
  814. * of bounds this will work fine.
  815. */
  816. if (err == -ENOENT) {
  817. lcn = LCN_ENOENT;
  818. err = 0;
  819. goto rl_not_mapped_enoent;
  820. }
  821. } else
  822. err = -EIO;
  823. /* Failed to map the buffer, even after retrying. */
  824. bh->b_blocknr = -1;
  825. ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
  826. "attribute type 0x%x, vcn 0x%llx, "
  827. "vcn offset 0x%x, because its "
  828. "location on disk could not be "
  829. "determined%s (error code %i).",
  830. ni->mft_no, ni->type,
  831. (unsigned long long)bh_cpos,
  832. (unsigned)bh_pos &
  833. vol->cluster_size_mask,
  834. is_retry ? " even after retrying" : "",
  835. err);
  836. break;
  837. }
  838. rl_not_mapped_enoent:
  839. /*
  840. * The buffer is in a hole or out of bounds. We need to fill
  841. * the hole, unless the buffer is in a cluster which is not
  842. * touched by the write, in which case we just leave the buffer
  843. * unmapped. This can only happen when the cluster size is
  844. * less than the page cache size.
  845. */
  846. if (unlikely(vol->cluster_size < PAGE_CACHE_SIZE)) {
  847. bh_cend = (bh_end + vol->cluster_size - 1) >>
  848. vol->cluster_size_bits;
  849. if ((bh_cend <= cpos || bh_cpos >= cend)) {
  850. bh->b_blocknr = -1;
  851. /*
  852. * If the buffer is uptodate we skip it. If it
  853. * is not but the page is uptodate, we can set
  854. * the buffer uptodate. If the page is not
  855. * uptodate, we can clear the buffer and set it
  856. * uptodate. Whether this is worthwhile is
  857. * debatable and this could be removed.
  858. */
  859. if (PageUptodate(page)) {
  860. if (!buffer_uptodate(bh))
  861. set_buffer_uptodate(bh);
  862. } else if (!buffer_uptodate(bh)) {
  863. u8 *kaddr = kmap_atomic(page, KM_USER0);
  864. memset(kaddr + bh_offset(bh), 0,
  865. blocksize);
  866. kunmap_atomic(kaddr, KM_USER0);
  867. flush_dcache_page(page);
  868. set_buffer_uptodate(bh);
  869. }
  870. continue;
  871. }
  872. }
  873. /*
  874. * Out of bounds buffer is invalid if it was not really out of
  875. * bounds.
  876. */
  877. BUG_ON(lcn != LCN_HOLE);
  878. /*
  879. * We need the runlist locked for writing, so if it is locked
  880. * for reading relock it now and retry in case it changed
  881. * whilst we dropped the lock.
  882. */
  883. BUG_ON(!rl);
  884. if (!rl_write_locked) {
  885. up_read(&ni->runlist.lock);
  886. down_write(&ni->runlist.lock);
  887. rl_write_locked = TRUE;
  888. goto retry_remap;
  889. }
  890. /* Find the previous last allocated cluster. */
  891. BUG_ON(rl->lcn != LCN_HOLE);
  892. lcn = -1;
  893. rl2 = rl;
  894. while (--rl2 >= ni->runlist.rl) {
  895. if (rl2->lcn >= 0) {
  896. lcn = rl2->lcn + rl2->length;
  897. break;
  898. }
  899. }
  900. rl2 = ntfs_cluster_alloc(vol, bh_cpos, 1, lcn, DATA_ZONE,
  901. FALSE);
  902. if (IS_ERR(rl2)) {
  903. err = PTR_ERR(rl2);
  904. ntfs_debug("Failed to allocate cluster, error code %i.",
  905. err);
  906. break;
  907. }
  908. lcn = rl2->lcn;
  909. rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
  910. if (IS_ERR(rl)) {
  911. err = PTR_ERR(rl);
  912. if (err != -ENOMEM)
  913. err = -EIO;
  914. if (ntfs_cluster_free_from_rl(vol, rl2)) {
  915. ntfs_error(vol->sb, "Failed to release "
  916. "allocated cluster in error "
  917. "code path. Run chkdsk to "
  918. "recover the lost cluster.");
  919. NVolSetErrors(vol);
  920. }
  921. ntfs_free(rl2);
  922. break;
  923. }
  924. ni->runlist.rl = rl;
  925. status.runlist_merged = 1;
  926. ntfs_debug("Allocated cluster, lcn 0x%llx.", lcn);
  927. /* Map and lock the mft record and get the attribute record. */
  928. if (!NInoAttr(ni))
  929. base_ni = ni;
  930. else
  931. base_ni = ni->ext.base_ntfs_ino;
  932. m = map_mft_record(base_ni);
  933. if (IS_ERR(m)) {
  934. err = PTR_ERR(m);
  935. break;
  936. }
  937. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  938. if (unlikely(!ctx)) {
  939. err = -ENOMEM;
  940. unmap_mft_record(base_ni);
  941. break;
  942. }
  943. status.mft_attr_mapped = 1;
  944. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  945. CASE_SENSITIVE, bh_cpos, NULL, 0, ctx);
  946. if (unlikely(err)) {
  947. if (err == -ENOENT)
  948. err = -EIO;
  949. break;
  950. }
  951. m = ctx->mrec;
  952. a = ctx->attr;
  953. /*
  954. * Find the runlist element with which the attribute extent
  955. * starts. Note, we cannot use the _attr_ version because we
  956. * have mapped the mft record. That is ok because we know the
  957. * runlist fragment must be mapped already to have ever gotten
  958. * here, so we can just use the _rl_ version.
  959. */
  960. vcn = sle64_to_cpu(a->data.non_resident.lowest_vcn);
  961. rl2 = ntfs_rl_find_vcn_nolock(rl, vcn);
  962. BUG_ON(!rl2);
  963. BUG_ON(!rl2->length);
  964. BUG_ON(rl2->lcn < LCN_HOLE);
  965. highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  966. /*
  967. * If @highest_vcn is zero, calculate the real highest_vcn
  968. * (which can really be zero).
  969. */
  970. if (!highest_vcn)
  971. highest_vcn = (sle64_to_cpu(
  972. a->data.non_resident.allocated_size) >>
  973. vol->cluster_size_bits) - 1;
  974. /*
  975. * Determine the size of the mapping pairs array for the new
  976. * extent, i.e. the old extent with the hole filled.
  977. */
  978. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, vcn,
  979. highest_vcn);
  980. if (unlikely(mp_size <= 0)) {
  981. if (!(err = mp_size))
  982. err = -EIO;
  983. ntfs_debug("Failed to get size for mapping pairs "
  984. "array, error code %i.", err);
  985. break;
  986. }
  987. /*
  988. * Resize the attribute record to fit the new mapping pairs
  989. * array.
  990. */
  991. attr_rec_len = le32_to_cpu(a->length);
  992. err = ntfs_attr_record_resize(m, a, mp_size + le16_to_cpu(
  993. a->data.non_resident.mapping_pairs_offset));
  994. if (unlikely(err)) {
  995. BUG_ON(err != -ENOSPC);
  996. // TODO: Deal with this by using the current attribute
  997. // and fill it with as much of the mapping pairs
  998. // array as possible. Then loop over each attribute
  999. // extent rewriting the mapping pairs arrays as we go
  1000. // along and if when we reach the end we have not
  1001. // enough space, try to resize the last attribute
  1002. // extent and if even that fails, add a new attribute
  1003. // extent.
  1004. // We could also try to resize at each step in the hope
  1005. // that we will not need to rewrite every single extent.
  1006. // Note, we may need to decompress some extents to fill
  1007. // the runlist as we are walking the extents...
  1008. ntfs_error(vol->sb, "Not enough space in the mft "
  1009. "record for the extended attribute "
  1010. "record. This case is not "
  1011. "implemented yet.");
  1012. err = -EOPNOTSUPP;
  1013. break ;
  1014. }
  1015. status.mp_rebuilt = 1;
  1016. /*
  1017. * Generate the mapping pairs array directly into the attribute
  1018. * record.
  1019. */
  1020. err = ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
  1021. a->data.non_resident.mapping_pairs_offset),
  1022. mp_size, rl2, vcn, highest_vcn, NULL);
  1023. if (unlikely(err)) {
  1024. ntfs_error(vol->sb, "Cannot fill hole in inode 0x%lx, "
  1025. "attribute type 0x%x, because building "
  1026. "the mapping pairs failed with error "
  1027. "code %i.", vi->i_ino,
  1028. (unsigned)le32_to_cpu(ni->type), err);
  1029. err = -EIO;
  1030. break;
  1031. }
  1032. /* Update the highest_vcn but only if it was not set. */
  1033. if (unlikely(!a->data.non_resident.highest_vcn))
  1034. a->data.non_resident.highest_vcn =
  1035. cpu_to_sle64(highest_vcn);
  1036. /*
  1037. * If the attribute is sparse/compressed, update the compressed
  1038. * size in the ntfs_inode structure and the attribute record.
  1039. */
  1040. if (likely(NInoSparse(ni) || NInoCompressed(ni))) {
  1041. /*
  1042. * If we are not in the first attribute extent, switch
  1043. * to it, but first ensure the changes will make it to
  1044. * disk later.
  1045. */
  1046. if (a->data.non_resident.lowest_vcn) {
  1047. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1048. mark_mft_record_dirty(ctx->ntfs_ino);
  1049. ntfs_attr_reinit_search_ctx(ctx);
  1050. err = ntfs_attr_lookup(ni->type, ni->name,
  1051. ni->name_len, CASE_SENSITIVE,
  1052. 0, NULL, 0, ctx);
  1053. if (unlikely(err)) {
  1054. status.attr_switched = 1;
  1055. break;
  1056. }
  1057. /* @m is not used any more so do not set it. */
  1058. a = ctx->attr;
  1059. }
  1060. write_lock_irqsave(&ni->size_lock, flags);
  1061. ni->itype.compressed.size += vol->cluster_size;
  1062. a->data.non_resident.compressed_size =
  1063. cpu_to_sle64(ni->itype.compressed.size);
  1064. write_unlock_irqrestore(&ni->size_lock, flags);
  1065. }
  1066. /* Ensure the changes make it to disk. */
  1067. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1068. mark_mft_record_dirty(ctx->ntfs_ino);
  1069. ntfs_attr_put_search_ctx(ctx);
  1070. unmap_mft_record(base_ni);
  1071. /* Successfully filled the hole. */
  1072. status.runlist_merged = 0;
  1073. status.mft_attr_mapped = 0;
  1074. status.mp_rebuilt = 0;
  1075. /* Setup the map cache and use that to deal with the buffer. */
  1076. was_hole = TRUE;
  1077. vcn = bh_cpos;
  1078. vcn_len = 1;
  1079. lcn_block = lcn << (vol->cluster_size_bits - blocksize_bits);
  1080. cdelta = 0;
  1081. /*
  1082. * If the number of remaining clusters in the @pages is smaller
  1083. * or equal to the number of cached clusters, unlock the
  1084. * runlist as the map cache will be used from now on.
  1085. */
  1086. if (likely(vcn + vcn_len >= cend)) {
  1087. up_write(&ni->runlist.lock);
  1088. rl_write_locked = FALSE;
  1089. rl = NULL;
  1090. }
  1091. goto map_buffer_cached;
  1092. } while (bh_pos += blocksize, (bh = bh->b_this_page) != head);
  1093. /* If there are no errors, do the next page. */
  1094. if (likely(!err && ++u < nr_pages))
  1095. goto do_next_page;
  1096. /* If there are no errors, release the runlist lock if we took it. */
  1097. if (likely(!err)) {
  1098. if (unlikely(rl_write_locked)) {
  1099. up_write(&ni->runlist.lock);
  1100. rl_write_locked = FALSE;
  1101. } else if (unlikely(rl))
  1102. up_read(&ni->runlist.lock);
  1103. rl = NULL;
  1104. }
  1105. /* If we issued read requests, let them complete. */
  1106. read_lock_irqsave(&ni->size_lock, flags);
  1107. initialized_size = ni->initialized_size;
  1108. read_unlock_irqrestore(&ni->size_lock, flags);
  1109. while (wait_bh > wait) {
  1110. bh = *--wait_bh;
  1111. wait_on_buffer(bh);
  1112. if (likely(buffer_uptodate(bh))) {
  1113. page = bh->b_page;
  1114. bh_pos = ((s64)page->index << PAGE_CACHE_SHIFT) +
  1115. bh_offset(bh);
  1116. /*
  1117. * If the buffer overflows the initialized size, need
  1118. * to zero the overflowing region.
  1119. */
  1120. if (unlikely(bh_pos + blocksize > initialized_size)) {
  1121. u8 *kaddr;
  1122. int ofs = 0;
  1123. if (likely(bh_pos < initialized_size))
  1124. ofs = initialized_size - bh_pos;
  1125. kaddr = kmap_atomic(page, KM_USER0);
  1126. memset(kaddr + bh_offset(bh) + ofs, 0,
  1127. blocksize - ofs);
  1128. kunmap_atomic(kaddr, KM_USER0);
  1129. flush_dcache_page(page);
  1130. }
  1131. } else /* if (unlikely(!buffer_uptodate(bh))) */
  1132. err = -EIO;
  1133. }
  1134. if (likely(!err)) {
  1135. /* Clear buffer_new on all buffers. */
  1136. u = 0;
  1137. do {
  1138. bh = head = page_buffers(pages[u]);
  1139. do {
  1140. if (buffer_new(bh))
  1141. clear_buffer_new(bh);
  1142. } while ((bh = bh->b_this_page) != head);
  1143. } while (++u < nr_pages);
  1144. ntfs_debug("Done.");
  1145. return err;
  1146. }
  1147. if (status.attr_switched) {
  1148. /* Get back to the attribute extent we modified. */
  1149. ntfs_attr_reinit_search_ctx(ctx);
  1150. if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1151. CASE_SENSITIVE, bh_cpos, NULL, 0, ctx)) {
  1152. ntfs_error(vol->sb, "Failed to find required "
  1153. "attribute extent of attribute in "
  1154. "error code path. Run chkdsk to "
  1155. "recover.");
  1156. write_lock_irqsave(&ni->size_lock, flags);
  1157. ni->itype.compressed.size += vol->cluster_size;
  1158. write_unlock_irqrestore(&ni->size_lock, flags);
  1159. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1160. mark_mft_record_dirty(ctx->ntfs_ino);
  1161. /*
  1162. * The only thing that is now wrong is the compressed
  1163. * size of the base attribute extent which chkdsk
  1164. * should be able to fix.
  1165. */
  1166. NVolSetErrors(vol);
  1167. } else {
  1168. m = ctx->mrec;
  1169. a = ctx->attr;
  1170. status.attr_switched = 0;
  1171. }
  1172. }
  1173. /*
  1174. * If the runlist has been modified, need to restore it by punching a
  1175. * hole into it and we then need to deallocate the on-disk cluster as
  1176. * well. Note, we only modify the runlist if we are able to generate a
  1177. * new mapping pairs array, i.e. only when the mapped attribute extent
  1178. * is not switched.
  1179. */
  1180. if (status.runlist_merged && !status.attr_switched) {
  1181. BUG_ON(!rl_write_locked);
  1182. /* Make the file cluster we allocated sparse in the runlist. */
  1183. if (ntfs_rl_punch_nolock(vol, &ni->runlist, bh_cpos, 1)) {
  1184. ntfs_error(vol->sb, "Failed to punch hole into "
  1185. "attribute runlist in error code "
  1186. "path. Run chkdsk to recover the "
  1187. "lost cluster.");
  1188. make_bad_inode(vi);
  1189. make_bad_inode(VFS_I(base_ni));
  1190. NVolSetErrors(vol);
  1191. } else /* if (success) */ {
  1192. status.runlist_merged = 0;
  1193. /*
  1194. * Deallocate the on-disk cluster we allocated but only
  1195. * if we succeeded in punching its vcn out of the
  1196. * runlist.
  1197. */
  1198. down_write(&vol->lcnbmp_lock);
  1199. if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
  1200. ntfs_error(vol->sb, "Failed to release "
  1201. "allocated cluster in error "
  1202. "code path. Run chkdsk to "
  1203. "recover the lost cluster.");
  1204. NVolSetErrors(vol);
  1205. }
  1206. up_write(&vol->lcnbmp_lock);
  1207. }
  1208. }
  1209. /*
  1210. * Resize the attribute record to its old size and rebuild the mapping
  1211. * pairs array. Note, we only can do this if the runlist has been
  1212. * restored to its old state which also implies that the mapped
  1213. * attribute extent is not switched.
  1214. */
  1215. if (status.mp_rebuilt && !status.runlist_merged) {
  1216. if (ntfs_attr_record_resize(m, a, attr_rec_len)) {
  1217. ntfs_error(vol->sb, "Failed to restore attribute "
  1218. "record in error code path. Run "
  1219. "chkdsk to recover.");
  1220. make_bad_inode(vi);
  1221. make_bad_inode(VFS_I(base_ni));
  1222. NVolSetErrors(vol);
  1223. } else /* if (success) */ {
  1224. if (ntfs_mapping_pairs_build(vol, (u8*)a +
  1225. le16_to_cpu(a->data.non_resident.
  1226. mapping_pairs_offset), attr_rec_len -
  1227. le16_to_cpu(a->data.non_resident.
  1228. mapping_pairs_offset), ni->runlist.rl,
  1229. vcn, highest_vcn, NULL)) {
  1230. ntfs_error(vol->sb, "Failed to restore "
  1231. "mapping pairs array in error "
  1232. "code path. Run chkdsk to "
  1233. "recover.");
  1234. make_bad_inode(vi);
  1235. make_bad_inode(VFS_I(base_ni));
  1236. NVolSetErrors(vol);
  1237. }
  1238. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1239. mark_mft_record_dirty(ctx->ntfs_ino);
  1240. }
  1241. }
  1242. /* Release the mft record and the attribute. */
  1243. if (status.mft_attr_mapped) {
  1244. ntfs_attr_put_search_ctx(ctx);
  1245. unmap_mft_record(base_ni);
  1246. }
  1247. /* Release the runlist lock. */
  1248. if (rl_write_locked)
  1249. up_write(&ni->runlist.lock);
  1250. else if (rl)
  1251. up_read(&ni->runlist.lock);
  1252. /*
  1253. * Zero out any newly allocated blocks to avoid exposing stale data.
  1254. * If BH_New is set, we know that the block was newly allocated above
  1255. * and that it has not been fully zeroed and marked dirty yet.
  1256. */
  1257. nr_pages = u;
  1258. u = 0;
  1259. end = bh_cpos << vol->cluster_size_bits;
  1260. do {
  1261. page = pages[u];
  1262. bh = head = page_buffers(page);
  1263. do {
  1264. if (u == nr_pages &&
  1265. ((s64)page->index << PAGE_CACHE_SHIFT) +
  1266. bh_offset(bh) >= end)
  1267. break;
  1268. if (!buffer_new(bh))
  1269. continue;
  1270. clear_buffer_new(bh);
  1271. if (!buffer_uptodate(bh)) {
  1272. if (PageUptodate(page))
  1273. set_buffer_uptodate(bh);
  1274. else {
  1275. u8 *kaddr = kmap_atomic(page, KM_USER0);
  1276. memset(kaddr + bh_offset(bh), 0,
  1277. blocksize);
  1278. kunmap_atomic(kaddr, KM_USER0);
  1279. flush_dcache_page(page);
  1280. set_buffer_uptodate(bh);
  1281. }
  1282. }
  1283. mark_buffer_dirty(bh);
  1284. } while ((bh = bh->b_this_page) != head);
  1285. } while (++u <= nr_pages);
  1286. ntfs_error(vol->sb, "Failed. Returning error code %i.", err);
  1287. return err;
  1288. }
  1289. /*
  1290. * Copy as much as we can into the pages and return the number of bytes which
  1291. * were sucessfully copied. If a fault is encountered then clear the pages
  1292. * out to (ofs + bytes) and return the number of bytes which were copied.
  1293. */
  1294. static inline size_t ntfs_copy_from_user(struct page **pages,
  1295. unsigned nr_pages, unsigned ofs, const char __user *buf,
  1296. size_t bytes)
  1297. {
  1298. struct page **last_page = pages + nr_pages;
  1299. char *kaddr;
  1300. size_t total = 0;
  1301. unsigned len;
  1302. int left;
  1303. do {
  1304. len = PAGE_CACHE_SIZE - ofs;
  1305. if (len > bytes)
  1306. len = bytes;
  1307. kaddr = kmap_atomic(*pages, KM_USER0);
  1308. left = __copy_from_user_inatomic(kaddr + ofs, buf, len);
  1309. kunmap_atomic(kaddr, KM_USER0);
  1310. if (unlikely(left)) {
  1311. /* Do it the slow way. */
  1312. kaddr = kmap(*pages);
  1313. left = __copy_from_user(kaddr + ofs, buf, len);
  1314. kunmap(*pages);
  1315. if (unlikely(left))
  1316. goto err_out;
  1317. }
  1318. total += len;
  1319. bytes -= len;
  1320. if (!bytes)
  1321. break;
  1322. buf += len;
  1323. ofs = 0;
  1324. } while (++pages < last_page);
  1325. out:
  1326. return total;
  1327. err_out:
  1328. total += len - left;
  1329. /* Zero the rest of the target like __copy_from_user(). */
  1330. while (++pages < last_page) {
  1331. bytes -= len;
  1332. if (!bytes)
  1333. break;
  1334. len = PAGE_CACHE_SIZE;
  1335. if (len > bytes)
  1336. len = bytes;
  1337. kaddr = kmap_atomic(*pages, KM_USER0);
  1338. memset(kaddr, 0, len);
  1339. kunmap_atomic(kaddr, KM_USER0);
  1340. }
  1341. goto out;
  1342. }
  1343. static size_t __ntfs_copy_from_user_iovec(char *vaddr,
  1344. const struct iovec *iov, size_t iov_ofs, size_t bytes)
  1345. {
  1346. size_t total = 0;
  1347. while (1) {
  1348. const char __user *buf = iov->iov_base + iov_ofs;
  1349. unsigned len;
  1350. size_t left;
  1351. len = iov->iov_len - iov_ofs;
  1352. if (len > bytes)
  1353. len = bytes;
  1354. left = __copy_from_user_inatomic(vaddr, buf, len);
  1355. total += len;
  1356. bytes -= len;
  1357. vaddr += len;
  1358. if (unlikely(left)) {
  1359. /*
  1360. * Zero the rest of the target like __copy_from_user().
  1361. */
  1362. memset(vaddr, 0, bytes);
  1363. total -= left;
  1364. break;
  1365. }
  1366. if (!bytes)
  1367. break;
  1368. iov++;
  1369. iov_ofs = 0;
  1370. }
  1371. return total;
  1372. }
  1373. static inline void ntfs_set_next_iovec(const struct iovec **iovp,
  1374. size_t *iov_ofsp, size_t bytes)
  1375. {
  1376. const struct iovec *iov = *iovp;
  1377. size_t iov_ofs = *iov_ofsp;
  1378. while (bytes) {
  1379. unsigned len;
  1380. len = iov->iov_len - iov_ofs;
  1381. if (len > bytes)
  1382. len = bytes;
  1383. bytes -= len;
  1384. iov_ofs += len;
  1385. if (iov->iov_len == iov_ofs) {
  1386. iov++;
  1387. iov_ofs = 0;
  1388. }
  1389. }
  1390. *iovp = iov;
  1391. *iov_ofsp = iov_ofs;
  1392. }
  1393. /*
  1394. * This has the same side-effects and return value as ntfs_copy_from_user().
  1395. * The difference is that on a fault we need to memset the remainder of the
  1396. * pages (out to offset + bytes), to emulate ntfs_copy_from_user()'s
  1397. * single-segment behaviour.
  1398. *
  1399. * We call the same helper (__ntfs_copy_from_user_iovec()) both when atomic and
  1400. * when not atomic. This is ok because __ntfs_copy_from_user_iovec() calls
  1401. * __copy_from_user_inatomic() and it is ok to call this when non-atomic. In
  1402. * fact, the only difference between __copy_from_user_inatomic() and
  1403. * __copy_from_user() is that the latter calls might_sleep(). And on many
  1404. * architectures __copy_from_user_inatomic() is just defined to
  1405. * __copy_from_user() so it makes no difference at all on those architectures.
  1406. */
  1407. static inline size_t ntfs_copy_from_user_iovec(struct page **pages,
  1408. unsigned nr_pages, unsigned ofs, const struct iovec **iov,
  1409. size_t *iov_ofs, size_t bytes)
  1410. {
  1411. struct page **last_page = pages + nr_pages;
  1412. char *kaddr;
  1413. size_t copied, len, total = 0;
  1414. do {
  1415. len = PAGE_CACHE_SIZE - ofs;
  1416. if (len > bytes)
  1417. len = bytes;
  1418. kaddr = kmap_atomic(*pages, KM_USER0);
  1419. copied = __ntfs_copy_from_user_iovec(kaddr + ofs,
  1420. *iov, *iov_ofs, len);
  1421. kunmap_atomic(kaddr, KM_USER0);
  1422. if (unlikely(copied != len)) {
  1423. /* Do it the slow way. */
  1424. kaddr = kmap(*pages);
  1425. copied = __ntfs_copy_from_user_iovec(kaddr + ofs,
  1426. *iov, *iov_ofs, len);
  1427. kunmap(*pages);
  1428. if (unlikely(copied != len))
  1429. goto err_out;
  1430. }
  1431. total += len;
  1432. bytes -= len;
  1433. if (!bytes)
  1434. break;
  1435. ntfs_set_next_iovec(iov, iov_ofs, len);
  1436. ofs = 0;
  1437. } while (++pages < last_page);
  1438. out:
  1439. return total;
  1440. err_out:
  1441. total += copied;
  1442. /* Zero the rest of the target like __copy_from_user(). */
  1443. while (++pages < last_page) {
  1444. bytes -= len;
  1445. if (!bytes)
  1446. break;
  1447. len = PAGE_CACHE_SIZE;
  1448. if (len > bytes)
  1449. len = bytes;
  1450. kaddr = kmap_atomic(*pages, KM_USER0);
  1451. memset(kaddr, 0, len);
  1452. kunmap_atomic(kaddr, KM_USER0);
  1453. }
  1454. goto out;
  1455. }
  1456. static inline void ntfs_flush_dcache_pages(struct page **pages,
  1457. unsigned nr_pages)
  1458. {
  1459. BUG_ON(!nr_pages);
  1460. do {
  1461. /*
  1462. * Warning: Do not do the decrement at the same time as the
  1463. * call because flush_dcache_page() is a NULL macro on i386
  1464. * and hence the decrement never happens.
  1465. */
  1466. flush_dcache_page(pages[nr_pages]);
  1467. } while (--nr_pages > 0);
  1468. }
  1469. /**
  1470. * ntfs_commit_pages_after_non_resident_write - commit the received data
  1471. * @pages: array of destination pages
  1472. * @nr_pages: number of pages in @pages
  1473. * @pos: byte position in file at which the write begins
  1474. * @bytes: number of bytes to be written
  1475. *
  1476. * See description of ntfs_commit_pages_after_write(), below.
  1477. */
  1478. static inline int ntfs_commit_pages_after_non_resident_write(
  1479. struct page **pages, const unsigned nr_pages,
  1480. s64 pos, size_t bytes)
  1481. {
  1482. s64 end, initialized_size;
  1483. struct inode *vi;
  1484. ntfs_inode *ni, *base_ni;
  1485. struct buffer_head *bh, *head;
  1486. ntfs_attr_search_ctx *ctx;
  1487. MFT_RECORD *m;
  1488. ATTR_RECORD *a;
  1489. unsigned long flags;
  1490. unsigned blocksize, u;
  1491. int err;
  1492. vi = pages[0]->mapping->host;
  1493. ni = NTFS_I(vi);
  1494. blocksize = 1 << vi->i_blkbits;
  1495. end = pos + bytes;
  1496. u = 0;
  1497. do {
  1498. s64 bh_pos;
  1499. struct page *page;
  1500. BOOL partial;
  1501. page = pages[u];
  1502. bh_pos = (s64)page->index << PAGE_CACHE_SHIFT;
  1503. bh = head = page_buffers(page);
  1504. partial = FALSE;
  1505. do {
  1506. s64 bh_end;
  1507. bh_end = bh_pos + blocksize;
  1508. if (bh_end <= pos || bh_pos >= end) {
  1509. if (!buffer_uptodate(bh))
  1510. partial = TRUE;
  1511. } else {
  1512. set_buffer_uptodate(bh);
  1513. mark_buffer_dirty(bh);
  1514. }
  1515. } while (bh_pos += blocksize, (bh = bh->b_this_page) != head);
  1516. /*
  1517. * If all buffers are now uptodate but the page is not, set the
  1518. * page uptodate.
  1519. */
  1520. if (!partial && !PageUptodate(page))
  1521. SetPageUptodate(page);
  1522. } while (++u < nr_pages);
  1523. /*
  1524. * Finally, if we do not need to update initialized_size or i_size we
  1525. * are finished.
  1526. */
  1527. read_lock_irqsave(&ni->size_lock, flags);
  1528. initialized_size = ni->initialized_size;
  1529. read_unlock_irqrestore(&ni->size_lock, flags);
  1530. if (end <= initialized_size) {
  1531. ntfs_debug("Done.");
  1532. return 0;
  1533. }
  1534. /*
  1535. * Update initialized_size/i_size as appropriate, both in the inode and
  1536. * the mft record.
  1537. */
  1538. if (!NInoAttr(ni))
  1539. base_ni = ni;
  1540. else
  1541. base_ni = ni->ext.base_ntfs_ino;
  1542. /* Map, pin, and lock the mft record. */
  1543. m = map_mft_record(base_ni);
  1544. if (IS_ERR(m)) {
  1545. err = PTR_ERR(m);
  1546. m = NULL;
  1547. ctx = NULL;
  1548. goto err_out;
  1549. }
  1550. BUG_ON(!NInoNonResident(ni));
  1551. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1552. if (unlikely(!ctx)) {
  1553. err = -ENOMEM;
  1554. goto err_out;
  1555. }
  1556. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1557. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1558. if (unlikely(err)) {
  1559. if (err == -ENOENT)
  1560. err = -EIO;
  1561. goto err_out;
  1562. }
  1563. a = ctx->attr;
  1564. BUG_ON(!a->non_resident);
  1565. write_lock_irqsave(&ni->size_lock, flags);
  1566. BUG_ON(end > ni->allocated_size);
  1567. ni->initialized_size = end;
  1568. a->data.non_resident.initialized_size = cpu_to_sle64(end);
  1569. if (end > i_size_read(vi)) {
  1570. i_size_write(vi, end);
  1571. a->data.non_resident.data_size =
  1572. a->data.non_resident.initialized_size;
  1573. }
  1574. write_unlock_irqrestore(&ni->size_lock, flags);
  1575. /* Mark the mft record dirty, so it gets written back. */
  1576. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1577. mark_mft_record_dirty(ctx->ntfs_ino);
  1578. ntfs_attr_put_search_ctx(ctx);
  1579. unmap_mft_record(base_ni);
  1580. ntfs_debug("Done.");
  1581. return 0;
  1582. err_out:
  1583. if (ctx)
  1584. ntfs_attr_put_search_ctx(ctx);
  1585. if (m)
  1586. unmap_mft_record(base_ni);
  1587. ntfs_error(vi->i_sb, "Failed to update initialized_size/i_size (error "
  1588. "code %i).", err);
  1589. if (err != -ENOMEM) {
  1590. NVolSetErrors(ni->vol);
  1591. make_bad_inode(VFS_I(base_ni));
  1592. make_bad_inode(vi);
  1593. }
  1594. return err;
  1595. }
  1596. /**
  1597. * ntfs_commit_pages_after_write - commit the received data
  1598. * @pages: array of destination pages
  1599. * @nr_pages: number of pages in @pages
  1600. * @pos: byte position in file at which the write begins
  1601. * @bytes: number of bytes to be written
  1602. *
  1603. * This is called from ntfs_file_buffered_write() with i_sem held on the inode
  1604. * (@pages[0]->mapping->host). There are @nr_pages pages in @pages which are
  1605. * locked but not kmap()ped. The source data has already been copied into the
  1606. * @page. ntfs_prepare_pages_for_non_resident_write() has been called before
  1607. * the data was copied (for non-resident attributes only) and it returned
  1608. * success.
  1609. *
  1610. * Need to set uptodate and mark dirty all buffers within the boundary of the
  1611. * write. If all buffers in a page are uptodate we set the page uptodate, too.
  1612. *
  1613. * Setting the buffers dirty ensures that they get written out later when
  1614. * ntfs_writepage() is invoked by the VM.
  1615. *
  1616. * Finally, we need to update i_size and initialized_size as appropriate both
  1617. * in the inode and the mft record.
  1618. *
  1619. * This is modelled after fs/buffer.c::generic_commit_write(), which marks
  1620. * buffers uptodate and dirty, sets the page uptodate if all buffers in the
  1621. * page are uptodate, and updates i_size if the end of io is beyond i_size. In
  1622. * that case, it also marks the inode dirty.
  1623. *
  1624. * If things have gone as outlined in
  1625. * ntfs_prepare_pages_for_non_resident_write(), we do not need to do any page
  1626. * content modifications here for non-resident attributes. For resident
  1627. * attributes we need to do the uptodate bringing here which we combine with
  1628. * the copying into the mft record which means we save one atomic kmap.
  1629. *
  1630. * Return 0 on success or -errno on error.
  1631. */
  1632. static int ntfs_commit_pages_after_write(struct page **pages,
  1633. const unsigned nr_pages, s64 pos, size_t bytes)
  1634. {
  1635. s64 end, initialized_size;
  1636. loff_t i_size;
  1637. struct inode *vi;
  1638. ntfs_inode *ni, *base_ni;
  1639. struct page *page;
  1640. ntfs_attr_search_ctx *ctx;
  1641. MFT_RECORD *m;
  1642. ATTR_RECORD *a;
  1643. char *kattr, *kaddr;
  1644. unsigned long flags;
  1645. u32 attr_len;
  1646. int err;
  1647. BUG_ON(!nr_pages);
  1648. BUG_ON(!pages);
  1649. page = pages[0];
  1650. BUG_ON(!page);
  1651. vi = page->mapping->host;
  1652. ni = NTFS_I(vi);
  1653. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
  1654. "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.",
  1655. vi->i_ino, ni->type, page->index, nr_pages,
  1656. (long long)pos, bytes);
  1657. if (NInoNonResident(ni))
  1658. return ntfs_commit_pages_after_non_resident_write(pages,
  1659. nr_pages, pos, bytes);
  1660. BUG_ON(nr_pages > 1);
  1661. /*
  1662. * Attribute is resident, implying it is not compressed, encrypted, or
  1663. * sparse.
  1664. */
  1665. if (!NInoAttr(ni))
  1666. base_ni = ni;
  1667. else
  1668. base_ni = ni->ext.base_ntfs_ino;
  1669. BUG_ON(NInoNonResident(ni));
  1670. /* Map, pin, and lock the mft record. */
  1671. m = map_mft_record(base_ni);
  1672. if (IS_ERR(m)) {
  1673. err = PTR_ERR(m);
  1674. m = NULL;
  1675. ctx = NULL;
  1676. goto err_out;
  1677. }
  1678. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1679. if (unlikely(!ctx)) {
  1680. err = -ENOMEM;
  1681. goto err_out;
  1682. }
  1683. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1684. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1685. if (unlikely(err)) {
  1686. if (err == -ENOENT)
  1687. err = -EIO;
  1688. goto err_out;
  1689. }
  1690. a = ctx->attr;
  1691. BUG_ON(a->non_resident);
  1692. /* The total length of the attribute value. */
  1693. attr_len = le32_to_cpu(a->data.resident.value_length);
  1694. i_size = i_size_read(vi);
  1695. BUG_ON(attr_len != i_size);
  1696. BUG_ON(pos > attr_len);
  1697. end = pos + bytes;
  1698. BUG_ON(end > le32_to_cpu(a->length) -
  1699. le16_to_cpu(a->data.resident.value_offset));
  1700. kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
  1701. kaddr = kmap_atomic(page, KM_USER0);
  1702. /* Copy the received data from the page to the mft record. */
  1703. memcpy(kattr + pos, kaddr + pos, bytes);
  1704. /* Update the attribute length if necessary. */
  1705. if (end > attr_len) {
  1706. attr_len = end;
  1707. a->data.resident.value_length = cpu_to_le32(attr_len);
  1708. }
  1709. /*
  1710. * If the page is not uptodate, bring the out of bounds area(s)
  1711. * uptodate by copying data from the mft record to the page.
  1712. */
  1713. if (!PageUptodate(page)) {
  1714. if (pos > 0)
  1715. memcpy(kaddr, kattr, pos);
  1716. if (end < attr_len)
  1717. memcpy(kaddr + end, kattr + end, attr_len - end);
  1718. /* Zero the region outside the end of the attribute value. */
  1719. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1720. flush_dcache_page(page);
  1721. SetPageUptodate(page);
  1722. }
  1723. kunmap_atomic(kaddr, KM_USER0);
  1724. /* Update initialized_size/i_size if necessary. */
  1725. read_lock_irqsave(&ni->size_lock, flags);
  1726. initialized_size = ni->initialized_size;
  1727. BUG_ON(end > ni->allocated_size);
  1728. read_unlock_irqrestore(&ni->size_lock, flags);
  1729. BUG_ON(initialized_size != i_size);
  1730. if (end > initialized_size) {
  1731. unsigned long flags;
  1732. write_lock_irqsave(&ni->size_lock, flags);
  1733. ni->initialized_size = end;
  1734. i_size_write(vi, end);
  1735. write_unlock_irqrestore(&ni->size_lock, flags);
  1736. }
  1737. /* Mark the mft record dirty, so it gets written back. */
  1738. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1739. mark_mft_record_dirty(ctx->ntfs_ino);
  1740. ntfs_attr_put_search_ctx(ctx);
  1741. unmap_mft_record(base_ni);
  1742. ntfs_debug("Done.");
  1743. return 0;
  1744. err_out:
  1745. if (err == -ENOMEM) {
  1746. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  1747. "commit the write.");
  1748. if (PageUptodate(page)) {
  1749. ntfs_warning(vi->i_sb, "Page is uptodate, setting "
  1750. "dirty so the write will be retried "
  1751. "later on by the VM.");
  1752. /*
  1753. * Put the page on mapping->dirty_pages, but leave its
  1754. * buffers' dirty state as-is.
  1755. */
  1756. __set_page_dirty_nobuffers(page);
  1757. err = 0;
  1758. } else
  1759. ntfs_error(vi->i_sb, "Page is not uptodate. Written "
  1760. "data has been lost.");
  1761. } else {
  1762. ntfs_error(vi->i_sb, "Resident attribute commit write failed "
  1763. "with error %i.", err);
  1764. NVolSetErrors(ni->vol);
  1765. make_bad_inode(VFS_I(base_ni));
  1766. make_bad_inode(vi);
  1767. }
  1768. if (ctx)
  1769. ntfs_attr_put_search_ctx(ctx);
  1770. if (m)
  1771. unmap_mft_record(base_ni);
  1772. return err;
  1773. }
  1774. /**
  1775. * ntfs_file_buffered_write -
  1776. *
  1777. * Locking: The vfs is holding ->i_sem on the inode.
  1778. */
  1779. static ssize_t ntfs_file_buffered_write(struct kiocb *iocb,
  1780. const struct iovec *iov, unsigned long nr_segs,
  1781. loff_t pos, loff_t *ppos, size_t count)
  1782. {
  1783. struct file *file = iocb->ki_filp;
  1784. struct address_space *mapping = file->f_mapping;
  1785. struct inode *vi = mapping->host;
  1786. ntfs_inode *ni = NTFS_I(vi);
  1787. ntfs_volume *vol = ni->vol;
  1788. struct page *pages[NTFS_MAX_PAGES_PER_CLUSTER];
  1789. struct page *cached_page = NULL;
  1790. char __user *buf = NULL;
  1791. s64 end, ll;
  1792. VCN last_vcn;
  1793. LCN lcn;
  1794. unsigned long flags;
  1795. size_t bytes, iov_ofs = 0; /* Offset in the current iovec. */
  1796. ssize_t status, written;
  1797. unsigned nr_pages;
  1798. int err;
  1799. struct pagevec lru_pvec;
  1800. ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
  1801. "pos 0x%llx, count 0x%lx.",
  1802. vi->i_ino, (unsigned)le32_to_cpu(ni->type),
  1803. (unsigned long long)pos, (unsigned long)count);
  1804. if (unlikely(!count))
  1805. return 0;
  1806. BUG_ON(NInoMstProtected(ni));
  1807. /*
  1808. * If the attribute is not an index root and it is encrypted or
  1809. * compressed, we cannot write to it yet. Note we need to check for
  1810. * AT_INDEX_ALLOCATION since this is the type of both directory and
  1811. * index inodes.
  1812. */
  1813. if (ni->type != AT_INDEX_ALLOCATION) {
  1814. /* If file is encrypted, deny access, just like NT4. */
  1815. if (NInoEncrypted(ni)) {
  1816. /*
  1817. * Reminder for later: Encrypted files are _always_
  1818. * non-resident so that the content can always be
  1819. * encrypted.
  1820. */
  1821. ntfs_debug("Denying write access to encrypted file.");
  1822. return -EACCES;
  1823. }
  1824. if (NInoCompressed(ni)) {
  1825. /* Only unnamed $DATA attribute can be compressed. */
  1826. BUG_ON(ni->type != AT_DATA);
  1827. BUG_ON(ni->name_len);
  1828. /*
  1829. * Reminder for later: If resident, the data is not
  1830. * actually compressed. Only on the switch to non-
  1831. * resident does compression kick in. This is in
  1832. * contrast to encrypted files (see above).
  1833. */
  1834. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1835. "not implemented yet. Sorry.");
  1836. return -EOPNOTSUPP;
  1837. }
  1838. }
  1839. /*
  1840. * If a previous ntfs_truncate() failed, repeat it and abort if it
  1841. * fails again.
  1842. */
  1843. if (unlikely(NInoTruncateFailed(ni))) {
  1844. down_write(&vi->i_alloc_sem);
  1845. err = ntfs_truncate(vi);
  1846. up_write(&vi->i_alloc_sem);
  1847. if (err || NInoTruncateFailed(ni)) {
  1848. if (!err)
  1849. err = -EIO;
  1850. ntfs_error(vol->sb, "Cannot perform write to inode "
  1851. "0x%lx, attribute type 0x%x, because "
  1852. "ntfs_truncate() failed (error code "
  1853. "%i).", vi->i_ino,
  1854. (unsigned)le32_to_cpu(ni->type), err);
  1855. return err;
  1856. }
  1857. }
  1858. /* The first byte after the write. */
  1859. end = pos + count;
  1860. /*
  1861. * If the write goes beyond the allocated size, extend the allocation
  1862. * to cover the whole of the write, rounded up to the nearest cluster.
  1863. */
  1864. read_lock_irqsave(&ni->size_lock, flags);
  1865. ll = ni->allocated_size;
  1866. read_unlock_irqrestore(&ni->size_lock, flags);
  1867. if (end > ll) {
  1868. /* Extend the allocation without changing the data size. */
  1869. ll = ntfs_attr_extend_allocation(ni, end, -1, pos);
  1870. if (likely(ll >= 0)) {
  1871. BUG_ON(pos >= ll);
  1872. /* If the extension was partial truncate the write. */
  1873. if (end > ll) {
  1874. ntfs_debug("Truncating write to inode 0x%lx, "
  1875. "attribute type 0x%x, because "
  1876. "the allocation was only "
  1877. "partially extended.",
  1878. vi->i_ino, (unsigned)
  1879. le32_to_cpu(ni->type));
  1880. end = ll;
  1881. count = ll - pos;
  1882. }
  1883. } else {
  1884. err = ll;
  1885. read_lock_irqsave(&ni->size_lock, flags);
  1886. ll = ni->allocated_size;
  1887. read_unlock_irqrestore(&ni->size_lock, flags);
  1888. /* Perform a partial write if possible or fail. */
  1889. if (pos < ll) {
  1890. ntfs_debug("Truncating write to inode 0x%lx, "
  1891. "attribute type 0x%x, because "
  1892. "extending the allocation "
  1893. "failed (error code %i).",
  1894. vi->i_ino, (unsigned)
  1895. le32_to_cpu(ni->type), err);
  1896. end = ll;
  1897. count = ll - pos;
  1898. } else {
  1899. ntfs_error(vol->sb, "Cannot perform write to "
  1900. "inode 0x%lx, attribute type "
  1901. "0x%x, because extending the "
  1902. "allocation failed (error "
  1903. "code %i).", vi->i_ino,
  1904. (unsigned)
  1905. le32_to_cpu(ni->type), err);
  1906. return err;
  1907. }
  1908. }
  1909. }
  1910. pagevec_init(&lru_pvec, 0);
  1911. written = 0;
  1912. /*
  1913. * If the write starts beyond the initialized size, extend it up to the
  1914. * beginning of the write and initialize all non-sparse space between
  1915. * the old initialized size and the new one. This automatically also
  1916. * increments the vfs inode->i_size to keep it above or equal to the
  1917. * initialized_size.
  1918. */
  1919. read_lock_irqsave(&ni->size_lock, flags);
  1920. ll = ni->initialized_size;
  1921. read_unlock_irqrestore(&ni->size_lock, flags);
  1922. if (pos > ll) {
  1923. err = ntfs_attr_extend_initialized(ni, pos, &cached_page,
  1924. &lru_pvec);
  1925. if (err < 0) {
  1926. ntfs_error(vol->sb, "Cannot perform write to inode "
  1927. "0x%lx, attribute type 0x%x, because "
  1928. "extending the initialized size "
  1929. "failed (error code %i).", vi->i_ino,
  1930. (unsigned)le32_to_cpu(ni->type), err);
  1931. status = err;
  1932. goto err_out;
  1933. }
  1934. }
  1935. /*
  1936. * Determine the number of pages per cluster for non-resident
  1937. * attributes.
  1938. */
  1939. nr_pages = 1;
  1940. if (vol->cluster_size > PAGE_CACHE_SIZE && NInoNonResident(ni))
  1941. nr_pages = vol->cluster_size >> PAGE_CACHE_SHIFT;
  1942. /* Finally, perform the actual write. */
  1943. last_vcn = -1;
  1944. if (likely(nr_segs == 1))
  1945. buf = iov->iov_base;
  1946. do {
  1947. VCN vcn;
  1948. pgoff_t idx, start_idx;
  1949. unsigned ofs, do_pages, u;
  1950. size_t copied;
  1951. start_idx = idx = pos >> PAGE_CACHE_SHIFT;
  1952. ofs = pos & ~PAGE_CACHE_MASK;
  1953. bytes = PAGE_CACHE_SIZE - ofs;
  1954. do_pages = 1;
  1955. if (nr_pages > 1) {
  1956. vcn = pos >> vol->cluster_size_bits;
  1957. if (vcn != last_vcn) {
  1958. last_vcn = vcn;
  1959. /*
  1960. * Get the lcn of the vcn the write is in. If
  1961. * it is a hole, need to lock down all pages in
  1962. * the cluster.
  1963. */
  1964. down_read(&ni->runlist.lock);
  1965. lcn = ntfs_attr_vcn_to_lcn_nolock(ni, pos >>
  1966. vol->cluster_size_bits, FALSE);
  1967. up_read(&ni->runlist.lock);
  1968. if (unlikely(lcn < LCN_HOLE)) {
  1969. status = -EIO;
  1970. if (lcn == LCN_ENOMEM)
  1971. status = -ENOMEM;
  1972. else
  1973. ntfs_error(vol->sb, "Cannot "
  1974. "perform write to "
  1975. "inode 0x%lx, "
  1976. "attribute type 0x%x, "
  1977. "because the attribute "
  1978. "is corrupt.",
  1979. vi->i_ino, (unsigned)
  1980. le32_to_cpu(ni->type));
  1981. break;
  1982. }
  1983. if (lcn == LCN_HOLE) {
  1984. start_idx = (pos & ~(s64)
  1985. vol->cluster_size_mask)
  1986. >> PAGE_CACHE_SHIFT;
  1987. bytes = vol->cluster_size - (pos &
  1988. vol->cluster_size_mask);
  1989. do_pages = nr_pages;
  1990. }
  1991. }
  1992. }
  1993. if (bytes > count)
  1994. bytes = count;
  1995. /*
  1996. * Bring in the user page(s) that we will copy from _first_.
  1997. * Otherwise there is a nasty deadlock on copying from the same
  1998. * page(s) as we are writing to, without it/them being marked
  1999. * up-to-date. Note, at present there is nothing to stop the
  2000. * pages being swapped out between us bringing them into memory
  2001. * and doing the actual copying.
  2002. */
  2003. if (likely(nr_segs == 1))
  2004. ntfs_fault_in_pages_readable(buf, bytes);
  2005. else
  2006. ntfs_fault_in_pages_readable_iovec(iov, iov_ofs, bytes);
  2007. /* Get and lock @do_pages starting at index @start_idx. */
  2008. status = __ntfs_grab_cache_pages(mapping, start_idx, do_pages,
  2009. pages, &cached_page, &lru_pvec);
  2010. if (unlikely(status))
  2011. break;
  2012. /*
  2013. * For non-resident attributes, we need to fill any holes with
  2014. * actual clusters and ensure all bufferes are mapped. We also
  2015. * need to bring uptodate any buffers that are only partially
  2016. * being written to.
  2017. */
  2018. if (NInoNonResident(ni)) {
  2019. status = ntfs_prepare_pages_for_non_resident_write(
  2020. pages, do_pages, pos, bytes);
  2021. if (unlikely(status)) {
  2022. loff_t i_size;
  2023. do {
  2024. unlock_page(pages[--do_pages]);
  2025. page_cache_release(pages[do_pages]);
  2026. } while (do_pages);
  2027. /*
  2028. * The write preparation may have instantiated
  2029. * allocated space outside i_size. Trim this
  2030. * off again. We can ignore any errors in this
  2031. * case as we will just be waisting a bit of
  2032. * allocated space, which is not a disaster.
  2033. */
  2034. i_size = i_size_read(vi);
  2035. if (pos + bytes > i_size)
  2036. vmtruncate(vi, i_size);
  2037. break;
  2038. }
  2039. }
  2040. u = (pos >> PAGE_CACHE_SHIFT) - pages[0]->index;
  2041. if (likely(nr_segs == 1)) {
  2042. copied = ntfs_copy_from_user(pages + u, do_pages - u,
  2043. ofs, buf, bytes);
  2044. buf += copied;
  2045. } else
  2046. copied = ntfs_copy_from_user_iovec(pages + u,
  2047. do_pages - u, ofs, &iov, &iov_ofs,
  2048. bytes);
  2049. ntfs_flush_dcache_pages(pages + u, do_pages - u);
  2050. status = ntfs_commit_pages_after_write(pages, do_pages, pos,
  2051. bytes);
  2052. if (likely(!status)) {
  2053. written += copied;
  2054. count -= copied;
  2055. pos += copied;
  2056. if (unlikely(copied != bytes))
  2057. status = -EFAULT;
  2058. }
  2059. do {
  2060. unlock_page(pages[--do_pages]);
  2061. mark_page_accessed(pages[do_pages]);
  2062. page_cache_release(pages[do_pages]);
  2063. } while (do_pages);
  2064. if (unlikely(status))
  2065. break;
  2066. balance_dirty_pages_ratelimited(mapping);
  2067. cond_resched();
  2068. } while (count);
  2069. err_out:
  2070. *ppos = pos;
  2071. if (cached_page)
  2072. page_cache_release(cached_page);
  2073. /* For now, when the user asks for O_SYNC, we actually give O_DSYNC. */
  2074. if (likely(!status)) {
  2075. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(vi))) {
  2076. if (!mapping->a_ops->writepage || !is_sync_kiocb(iocb))
  2077. status = generic_osync_inode(vi, mapping,
  2078. OSYNC_METADATA|OSYNC_DATA);
  2079. }
  2080. }
  2081. pagevec_lru_add(&lru_pvec);
  2082. ntfs_debug("Done. Returning %s (written 0x%lx, status %li).",
  2083. written ? "written" : "status", (unsigned long)written,
  2084. (long)status);
  2085. return written ? written : status;
  2086. }
  2087. /**
  2088. * ntfs_file_aio_write_nolock -
  2089. */
  2090. static ssize_t ntfs_file_aio_write_nolock(struct kiocb *iocb,
  2091. const struct iovec *iov, unsigned long nr_segs, loff_t *ppos)
  2092. {
  2093. struct file *file = iocb->ki_filp;
  2094. struct address_space *mapping = file->f_mapping;
  2095. struct inode *inode = mapping->host;
  2096. loff_t pos;
  2097. unsigned long seg;
  2098. size_t count; /* after file limit checks */
  2099. ssize_t written, err;
  2100. count = 0;
  2101. for (seg = 0; seg < nr_segs; seg++) {
  2102. const struct iovec *iv = &iov[seg];
  2103. /*
  2104. * If any segment has a negative length, or the cumulative
  2105. * length ever wraps negative then return -EINVAL.
  2106. */
  2107. count += iv->iov_len;
  2108. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  2109. return -EINVAL;
  2110. if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
  2111. continue;
  2112. if (!seg)
  2113. return -EFAULT;
  2114. nr_segs = seg;
  2115. count -= iv->iov_len; /* This segment is no good */
  2116. break;
  2117. }
  2118. pos = *ppos;
  2119. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  2120. /* We can write back this queue in page reclaim. */
  2121. current->backing_dev_info = mapping->backing_dev_info;
  2122. written = 0;
  2123. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  2124. if (err)
  2125. goto out;
  2126. if (!count)
  2127. goto out;
  2128. err = remove_suid(file->f_dentry);
  2129. if (err)
  2130. goto out;
  2131. inode_update_time(inode, 1);
  2132. written = ntfs_file_buffered_write(iocb, iov, nr_segs, pos, ppos,
  2133. count);
  2134. out:
  2135. current->backing_dev_info = NULL;
  2136. return written ? written : err;
  2137. }
  2138. /**
  2139. * ntfs_file_aio_write -
  2140. */
  2141. static ssize_t ntfs_file_aio_write(struct kiocb *iocb, const char __user *buf,
  2142. size_t count, loff_t pos)
  2143. {
  2144. struct file *file = iocb->ki_filp;
  2145. struct address_space *mapping = file->f_mapping;
  2146. struct inode *inode = mapping->host;
  2147. ssize_t ret;
  2148. struct iovec local_iov = { .iov_base = (void __user *)buf,
  2149. .iov_len = count };
  2150. BUG_ON(iocb->ki_pos != pos);
  2151. down(&inode->i_sem);
  2152. ret = ntfs_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  2153. up(&inode->i_sem);
  2154. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2155. int err = sync_page_range(inode, mapping, pos, ret);
  2156. if (err < 0)
  2157. ret = err;
  2158. }
  2159. return ret;
  2160. }
  2161. /**
  2162. * ntfs_file_writev -
  2163. *
  2164. * Basically the same as generic_file_writev() except that it ends up calling
  2165. * ntfs_file_aio_write_nolock() instead of __generic_file_aio_write_nolock().
  2166. */
  2167. static ssize_t ntfs_file_writev(struct file *file, const struct iovec *iov,
  2168. unsigned long nr_segs, loff_t *ppos)
  2169. {
  2170. struct address_space *mapping = file->f_mapping;
  2171. struct inode *inode = mapping->host;
  2172. struct kiocb kiocb;
  2173. ssize_t ret;
  2174. down(&inode->i_sem);
  2175. init_sync_kiocb(&kiocb, file);
  2176. ret = ntfs_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
  2177. if (ret == -EIOCBQUEUED)
  2178. ret = wait_on_sync_kiocb(&kiocb);
  2179. up(&inode->i_sem);
  2180. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2181. int err = sync_page_range(inode, mapping, *ppos - ret, ret);
  2182. if (err < 0)
  2183. ret = err;
  2184. }
  2185. return ret;
  2186. }
  2187. /**
  2188. * ntfs_file_write - simple wrapper for ntfs_file_writev()
  2189. */
  2190. static ssize_t ntfs_file_write(struct file *file, const char __user *buf,
  2191. size_t count, loff_t *ppos)
  2192. {
  2193. struct iovec local_iov = { .iov_base = (void __user *)buf,
  2194. .iov_len = count };
  2195. return ntfs_file_writev(file, &local_iov, 1, ppos);
  2196. }
  2197. /**
  2198. * ntfs_file_fsync - sync a file to disk
  2199. * @filp: file to be synced
  2200. * @dentry: dentry describing the file to sync
  2201. * @datasync: if non-zero only flush user data and not metadata
  2202. *
  2203. * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
  2204. * system calls. This function is inspired by fs/buffer.c::file_fsync().
  2205. *
  2206. * If @datasync is false, write the mft record and all associated extent mft
  2207. * records as well as the $DATA attribute and then sync the block device.
  2208. *
  2209. * If @datasync is true and the attribute is non-resident, we skip the writing
  2210. * of the mft record and all associated extent mft records (this might still
  2211. * happen due to the write_inode_now() call).
  2212. *
  2213. * Also, if @datasync is true, we do not wait on the inode to be written out
  2214. * but we always wait on the page cache pages to be written out.
  2215. *
  2216. * Note: In the past @filp could be NULL so we ignore it as we don't need it
  2217. * anyway.
  2218. *
  2219. * Locking: Caller must hold i_sem on the inode.
  2220. *
  2221. * TODO: We should probably also write all attribute/index inodes associated
  2222. * with this inode but since we have no simple way of getting to them we ignore
  2223. * this problem for now.
  2224. */
  2225. static int ntfs_file_fsync(struct file *filp, struct dentry *dentry,
  2226. int datasync)
  2227. {
  2228. struct inode *vi = dentry->d_inode;
  2229. int err, ret = 0;
  2230. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  2231. BUG_ON(S_ISDIR(vi->i_mode));
  2232. if (!datasync || !NInoNonResident(NTFS_I(vi)))
  2233. ret = ntfs_write_inode(vi, 1);
  2234. write_inode_now(vi, !datasync);
  2235. /*
  2236. * NOTE: If we were to use mapping->private_list (see ext2 and
  2237. * fs/buffer.c) for dirty blocks then we could optimize the below to be
  2238. * sync_mapping_buffers(vi->i_mapping).
  2239. */
  2240. err = sync_blockdev(vi->i_sb->s_bdev);
  2241. if (unlikely(err && !ret))
  2242. ret = err;
  2243. if (likely(!ret))
  2244. ntfs_debug("Done.");
  2245. else
  2246. ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
  2247. "%u.", datasync ? "data" : "", vi->i_ino, -ret);
  2248. return ret;
  2249. }
  2250. #endif /* NTFS_RW */
  2251. struct file_operations ntfs_file_ops = {
  2252. .llseek = generic_file_llseek, /* Seek inside file. */
  2253. .read = generic_file_read, /* Read from file. */
  2254. .aio_read = generic_file_aio_read, /* Async read from file. */
  2255. .readv = generic_file_readv, /* Read from file. */
  2256. #ifdef NTFS_RW
  2257. .write = ntfs_file_write, /* Write to file. */
  2258. .aio_write = ntfs_file_aio_write, /* Async write to file. */
  2259. .writev = ntfs_file_writev, /* Write to file. */
  2260. /*.release = ,*/ /* Last file is closed. See
  2261. fs/ext2/file.c::
  2262. ext2_release_file() for
  2263. how to use this to discard
  2264. preallocated space for
  2265. write opened files. */
  2266. .fsync = ntfs_file_fsync, /* Sync a file to disk. */
  2267. /*.aio_fsync = ,*/ /* Sync all outstanding async
  2268. i/o operations on a
  2269. kiocb. */
  2270. #endif /* NTFS_RW */
  2271. /*.ioctl = ,*/ /* Perform function on the
  2272. mounted filesystem. */
  2273. .mmap = generic_file_mmap, /* Mmap file. */
  2274. .open = ntfs_file_open, /* Open file. */
  2275. .sendfile = generic_file_sendfile, /* Zero-copy data send with
  2276. the data source being on
  2277. the ntfs partition. We do
  2278. not need to care about the
  2279. data destination. */
  2280. /*.sendpage = ,*/ /* Zero-copy data send with
  2281. the data destination being
  2282. on the ntfs partition. We
  2283. do not need to care about
  2284. the data source. */
  2285. };
  2286. struct inode_operations ntfs_file_inode_ops = {
  2287. #ifdef NTFS_RW
  2288. .truncate = ntfs_truncate_vfs,
  2289. .setattr = ntfs_setattr,
  2290. #endif /* NTFS_RW */
  2291. };
  2292. struct file_operations ntfs_empty_file_ops = {};
  2293. struct inode_operations ntfs_empty_inode_ops = {};