file.c 69 KB

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