file.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  1. /*
  2. * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 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 -EOVERFLOW;
  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_mutex 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_mapping_page(mapping, index, NULL);
  228. if (IS_ERR(page)) {
  229. err = PTR_ERR(page);
  230. goto init_err_out;
  231. }
  232. if (unlikely(PageError(page))) {
  233. page_cache_release(page);
  234. err = -EIO;
  235. goto init_err_out;
  236. }
  237. /*
  238. * Update the initialized size in the ntfs inode. This is
  239. * enough to make ntfs_writepage() work.
  240. */
  241. write_lock_irqsave(&ni->size_lock, flags);
  242. ni->initialized_size = (s64)(index + 1) << PAGE_CACHE_SHIFT;
  243. if (ni->initialized_size > new_init_size)
  244. ni->initialized_size = new_init_size;
  245. write_unlock_irqrestore(&ni->size_lock, flags);
  246. /* Set the page dirty so it gets written out. */
  247. set_page_dirty(page);
  248. page_cache_release(page);
  249. /*
  250. * Play nice with the vm and the rest of the system. This is
  251. * very much needed as we can potentially be modifying the
  252. * initialised size from a very small value to a really huge
  253. * value, e.g.
  254. * f = open(somefile, O_TRUNC);
  255. * truncate(f, 10GiB);
  256. * seek(f, 10GiB);
  257. * write(f, 1);
  258. * And this would mean we would be marking dirty hundreds of
  259. * thousands of pages or as in the above example more than
  260. * two and a half million pages!
  261. *
  262. * TODO: For sparse pages could optimize this workload by using
  263. * the FsMisc / MiscFs page bit as a "PageIsSparse" bit. This
  264. * would be set in readpage for sparse pages and here we would
  265. * not need to mark dirty any pages which have this bit set.
  266. * The only caveat is that we have to clear the bit everywhere
  267. * where we allocate any clusters that lie in the page or that
  268. * contain the page.
  269. *
  270. * TODO: An even greater optimization would be for us to only
  271. * call readpage() on pages which are not in sparse regions as
  272. * determined from the runlist. This would greatly reduce the
  273. * number of pages we read and make dirty in the case of sparse
  274. * files.
  275. */
  276. balance_dirty_pages_ratelimited(mapping);
  277. cond_resched();
  278. } while (++index < end_index);
  279. read_lock_irqsave(&ni->size_lock, flags);
  280. BUG_ON(ni->initialized_size != new_init_size);
  281. read_unlock_irqrestore(&ni->size_lock, flags);
  282. /* Now bring in sync the initialized_size in the mft record. */
  283. m = map_mft_record(base_ni);
  284. if (IS_ERR(m)) {
  285. err = PTR_ERR(m);
  286. m = NULL;
  287. goto init_err_out;
  288. }
  289. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  290. if (unlikely(!ctx)) {
  291. err = -ENOMEM;
  292. goto init_err_out;
  293. }
  294. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  295. CASE_SENSITIVE, 0, NULL, 0, ctx);
  296. if (unlikely(err)) {
  297. if (err == -ENOENT)
  298. err = -EIO;
  299. goto init_err_out;
  300. }
  301. m = ctx->mrec;
  302. a = ctx->attr;
  303. BUG_ON(!a->non_resident);
  304. a->data.non_resident.initialized_size = cpu_to_sle64(new_init_size);
  305. done:
  306. flush_dcache_mft_record_page(ctx->ntfs_ino);
  307. mark_mft_record_dirty(ctx->ntfs_ino);
  308. if (ctx)
  309. ntfs_attr_put_search_ctx(ctx);
  310. if (m)
  311. unmap_mft_record(base_ni);
  312. ntfs_debug("Done, initialized_size 0x%llx, i_size 0x%llx.",
  313. (unsigned long long)new_init_size, i_size_read(vi));
  314. return 0;
  315. init_err_out:
  316. write_lock_irqsave(&ni->size_lock, flags);
  317. ni->initialized_size = old_init_size;
  318. write_unlock_irqrestore(&ni->size_lock, flags);
  319. err_out:
  320. if (ctx)
  321. ntfs_attr_put_search_ctx(ctx);
  322. if (m)
  323. unmap_mft_record(base_ni);
  324. ntfs_debug("Failed. Returning error code %i.", err);
  325. return err;
  326. }
  327. /**
  328. * ntfs_fault_in_pages_readable -
  329. *
  330. * Fault a number of userspace pages into pagetables.
  331. *
  332. * Unlike include/linux/pagemap.h::fault_in_pages_readable(), this one copes
  333. * with more than two userspace pages as well as handling the single page case
  334. * elegantly.
  335. *
  336. * If you find this difficult to understand, then think of the while loop being
  337. * the following code, except that we do without the integer variable ret:
  338. *
  339. * do {
  340. * ret = __get_user(c, uaddr);
  341. * uaddr += PAGE_SIZE;
  342. * } while (!ret && uaddr < end);
  343. *
  344. * Note, the final __get_user() may well run out-of-bounds of the user buffer,
  345. * but _not_ out-of-bounds of the page the user buffer belongs to, and since
  346. * this is only a read and not a write, and since it is still in the same page,
  347. * it should not matter and this makes the code much simpler.
  348. */
  349. static inline void ntfs_fault_in_pages_readable(const char __user *uaddr,
  350. int bytes)
  351. {
  352. const char __user *end;
  353. volatile char c;
  354. /* Set @end to the first byte outside the last page we care about. */
  355. end = (const char __user*)PAGE_ALIGN((unsigned long)uaddr + bytes);
  356. while (!__get_user(c, uaddr) && (uaddr += PAGE_SIZE, uaddr < end))
  357. ;
  358. }
  359. /**
  360. * ntfs_fault_in_pages_readable_iovec -
  361. *
  362. * Same as ntfs_fault_in_pages_readable() but operates on an array of iovecs.
  363. */
  364. static inline void ntfs_fault_in_pages_readable_iovec(const struct iovec *iov,
  365. size_t iov_ofs, int bytes)
  366. {
  367. do {
  368. const char __user *buf;
  369. unsigned len;
  370. buf = iov->iov_base + iov_ofs;
  371. len = iov->iov_len - iov_ofs;
  372. if (len > bytes)
  373. len = bytes;
  374. ntfs_fault_in_pages_readable(buf, len);
  375. bytes -= len;
  376. iov++;
  377. iov_ofs = 0;
  378. } while (bytes);
  379. }
  380. /**
  381. * __ntfs_grab_cache_pages - obtain a number of locked pages
  382. * @mapping: address space mapping from which to obtain page cache pages
  383. * @index: starting index in @mapping at which to begin obtaining pages
  384. * @nr_pages: number of page cache pages to obtain
  385. * @pages: array of pages in which to return the obtained page cache pages
  386. * @cached_page: allocated but as yet unused page
  387. * @lru_pvec: lru-buffering pagevec of caller
  388. *
  389. * Obtain @nr_pages locked page cache pages from the mapping @maping and
  390. * starting at index @index.
  391. *
  392. * If a page is newly created, increment its refcount and add it to the
  393. * caller's lru-buffering pagevec @lru_pvec.
  394. *
  395. * This is the same as mm/filemap.c::__grab_cache_page(), except that @nr_pages
  396. * are obtained at once instead of just one page and that 0 is returned on
  397. * success and -errno on error.
  398. *
  399. * Note, the page locks are obtained in ascending page index order.
  400. */
  401. static inline int __ntfs_grab_cache_pages(struct address_space *mapping,
  402. pgoff_t index, const unsigned nr_pages, struct page **pages,
  403. struct page **cached_page, struct pagevec *lru_pvec)
  404. {
  405. int err, nr;
  406. BUG_ON(!nr_pages);
  407. err = nr = 0;
  408. do {
  409. pages[nr] = find_lock_page(mapping, index);
  410. if (!pages[nr]) {
  411. if (!*cached_page) {
  412. *cached_page = page_cache_alloc(mapping);
  413. if (unlikely(!*cached_page)) {
  414. err = -ENOMEM;
  415. goto err_out;
  416. }
  417. }
  418. err = add_to_page_cache(*cached_page, mapping, index,
  419. GFP_KERNEL);
  420. if (unlikely(err)) {
  421. if (err == -EEXIST)
  422. continue;
  423. goto err_out;
  424. }
  425. pages[nr] = *cached_page;
  426. page_cache_get(*cached_page);
  427. if (unlikely(!pagevec_add(lru_pvec, *cached_page)))
  428. __pagevec_lru_add(lru_pvec);
  429. *cached_page = NULL;
  430. }
  431. index++;
  432. nr++;
  433. } while (nr < nr_pages);
  434. out:
  435. return err;
  436. err_out:
  437. while (nr > 0) {
  438. unlock_page(pages[--nr]);
  439. page_cache_release(pages[nr]);
  440. }
  441. goto out;
  442. }
  443. static inline int ntfs_submit_bh_for_read(struct buffer_head *bh)
  444. {
  445. lock_buffer(bh);
  446. get_bh(bh);
  447. bh->b_end_io = end_buffer_read_sync;
  448. return submit_bh(READ, bh);
  449. }
  450. /**
  451. * ntfs_prepare_pages_for_non_resident_write - prepare pages for receiving data
  452. * @pages: array of destination pages
  453. * @nr_pages: number of pages in @pages
  454. * @pos: byte position in file at which the write begins
  455. * @bytes: number of bytes to be written
  456. *
  457. * This is called for non-resident attributes from ntfs_file_buffered_write()
  458. * with i_mutex held on the inode (@pages[0]->mapping->host). There are
  459. * @nr_pages pages in @pages which are locked but not kmap()ped. The source
  460. * data has not yet been copied into the @pages.
  461. *
  462. * Need to fill any holes with actual clusters, allocate buffers if necessary,
  463. * ensure all the buffers are mapped, and bring uptodate any buffers that are
  464. * only partially being written to.
  465. *
  466. * If @nr_pages is greater than one, we are guaranteed that the cluster size is
  467. * greater than PAGE_CACHE_SIZE, that all pages in @pages are entirely inside
  468. * the same cluster and that they are the entirety of that cluster, and that
  469. * the cluster is sparse, i.e. we need to allocate a cluster to fill the hole.
  470. *
  471. * i_size is not to be modified yet.
  472. *
  473. * Return 0 on success or -errno on error.
  474. */
  475. static int ntfs_prepare_pages_for_non_resident_write(struct page **pages,
  476. unsigned nr_pages, s64 pos, size_t bytes)
  477. {
  478. VCN vcn, highest_vcn = 0, cpos, cend, bh_cpos, bh_cend;
  479. LCN lcn;
  480. s64 bh_pos, vcn_len, end, initialized_size;
  481. sector_t lcn_block;
  482. struct page *page;
  483. struct inode *vi;
  484. ntfs_inode *ni, *base_ni = NULL;
  485. ntfs_volume *vol;
  486. runlist_element *rl, *rl2;
  487. struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
  488. ntfs_attr_search_ctx *ctx = NULL;
  489. MFT_RECORD *m = NULL;
  490. ATTR_RECORD *a = NULL;
  491. unsigned long flags;
  492. u32 attr_rec_len = 0;
  493. unsigned blocksize, u;
  494. int err, mp_size;
  495. bool rl_write_locked, was_hole, is_retry;
  496. unsigned char blocksize_bits;
  497. struct {
  498. u8 runlist_merged:1;
  499. u8 mft_attr_mapped:1;
  500. u8 mp_rebuilt:1;
  501. u8 attr_switched:1;
  502. } status = { 0, 0, 0, 0 };
  503. BUG_ON(!nr_pages);
  504. BUG_ON(!pages);
  505. BUG_ON(!*pages);
  506. vi = pages[0]->mapping->host;
  507. ni = NTFS_I(vi);
  508. vol = ni->vol;
  509. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
  510. "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.",
  511. vi->i_ino, ni->type, pages[0]->index, nr_pages,
  512. (long long)pos, bytes);
  513. blocksize = vol->sb->s_blocksize;
  514. blocksize_bits = vol->sb->s_blocksize_bits;
  515. u = 0;
  516. do {
  517. page = pages[u];
  518. BUG_ON(!page);
  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 *addr;
  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. addr = kmap_atomic(*pages, KM_USER0);
  1282. left = __copy_from_user_inatomic(addr + ofs, buf, len);
  1283. kunmap_atomic(addr, KM_USER0);
  1284. if (unlikely(left)) {
  1285. /* Do it the slow way. */
  1286. addr = kmap(*pages);
  1287. left = __copy_from_user(addr + 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 *addr;
  1383. size_t copied, len, total = 0;
  1384. do {
  1385. len = PAGE_CACHE_SIZE - ofs;
  1386. if (len > bytes)
  1387. len = bytes;
  1388. addr = kmap_atomic(*pages, KM_USER0);
  1389. copied = __ntfs_copy_from_user_iovec_inatomic(addr + ofs,
  1390. *iov, *iov_ofs, len);
  1391. kunmap_atomic(addr, KM_USER0);
  1392. if (unlikely(copied != len)) {
  1393. /* Do it the slow way. */
  1394. addr = kmap(*pages);
  1395. copied = __ntfs_copy_from_user_iovec_inatomic(addr + ofs,
  1396. *iov, *iov_ofs, len);
  1397. /*
  1398. * Zero the rest of the target like __copy_from_user().
  1399. */
  1400. memset(addr + 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. write_lock_irqsave(&ni->size_lock, flags);
  1702. ni->initialized_size = end;
  1703. i_size_write(vi, end);
  1704. write_unlock_irqrestore(&ni->size_lock, flags);
  1705. }
  1706. /* Mark the mft record dirty, so it gets written back. */
  1707. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1708. mark_mft_record_dirty(ctx->ntfs_ino);
  1709. ntfs_attr_put_search_ctx(ctx);
  1710. unmap_mft_record(base_ni);
  1711. ntfs_debug("Done.");
  1712. return 0;
  1713. err_out:
  1714. if (err == -ENOMEM) {
  1715. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  1716. "commit the write.");
  1717. if (PageUptodate(page)) {
  1718. ntfs_warning(vi->i_sb, "Page is uptodate, setting "
  1719. "dirty so the write will be retried "
  1720. "later on by the VM.");
  1721. /*
  1722. * Put the page on mapping->dirty_pages, but leave its
  1723. * buffers' dirty state as-is.
  1724. */
  1725. __set_page_dirty_nobuffers(page);
  1726. err = 0;
  1727. } else
  1728. ntfs_error(vi->i_sb, "Page is not uptodate. Written "
  1729. "data has been lost.");
  1730. } else {
  1731. ntfs_error(vi->i_sb, "Resident attribute commit write failed "
  1732. "with error %i.", err);
  1733. NVolSetErrors(ni->vol);
  1734. }
  1735. if (ctx)
  1736. ntfs_attr_put_search_ctx(ctx);
  1737. if (m)
  1738. unmap_mft_record(base_ni);
  1739. return err;
  1740. }
  1741. /**
  1742. * ntfs_file_buffered_write -
  1743. *
  1744. * Locking: The vfs is holding ->i_mutex on the inode.
  1745. */
  1746. static ssize_t ntfs_file_buffered_write(struct kiocb *iocb,
  1747. const struct iovec *iov, unsigned long nr_segs,
  1748. loff_t pos, loff_t *ppos, size_t count)
  1749. {
  1750. struct file *file = iocb->ki_filp;
  1751. struct address_space *mapping = file->f_mapping;
  1752. struct inode *vi = mapping->host;
  1753. ntfs_inode *ni = NTFS_I(vi);
  1754. ntfs_volume *vol = ni->vol;
  1755. struct page *pages[NTFS_MAX_PAGES_PER_CLUSTER];
  1756. struct page *cached_page = NULL;
  1757. char __user *buf = NULL;
  1758. s64 end, ll;
  1759. VCN last_vcn;
  1760. LCN lcn;
  1761. unsigned long flags;
  1762. size_t bytes, iov_ofs = 0; /* Offset in the current iovec. */
  1763. ssize_t status, written;
  1764. unsigned nr_pages;
  1765. int err;
  1766. struct pagevec lru_pvec;
  1767. ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
  1768. "pos 0x%llx, count 0x%lx.",
  1769. vi->i_ino, (unsigned)le32_to_cpu(ni->type),
  1770. (unsigned long long)pos, (unsigned long)count);
  1771. if (unlikely(!count))
  1772. return 0;
  1773. BUG_ON(NInoMstProtected(ni));
  1774. /*
  1775. * If the attribute is not an index root and it is encrypted or
  1776. * compressed, we cannot write to it yet. Note we need to check for
  1777. * AT_INDEX_ALLOCATION since this is the type of both directory and
  1778. * index inodes.
  1779. */
  1780. if (ni->type != AT_INDEX_ALLOCATION) {
  1781. /* If file is encrypted, deny access, just like NT4. */
  1782. if (NInoEncrypted(ni)) {
  1783. /*
  1784. * Reminder for later: Encrypted files are _always_
  1785. * non-resident so that the content can always be
  1786. * encrypted.
  1787. */
  1788. ntfs_debug("Denying write access to encrypted file.");
  1789. return -EACCES;
  1790. }
  1791. if (NInoCompressed(ni)) {
  1792. /* Only unnamed $DATA attribute can be compressed. */
  1793. BUG_ON(ni->type != AT_DATA);
  1794. BUG_ON(ni->name_len);
  1795. /*
  1796. * Reminder for later: If resident, the data is not
  1797. * actually compressed. Only on the switch to non-
  1798. * resident does compression kick in. This is in
  1799. * contrast to encrypted files (see above).
  1800. */
  1801. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1802. "not implemented yet. Sorry.");
  1803. return -EOPNOTSUPP;
  1804. }
  1805. }
  1806. /*
  1807. * If a previous ntfs_truncate() failed, repeat it and abort if it
  1808. * fails again.
  1809. */
  1810. if (unlikely(NInoTruncateFailed(ni))) {
  1811. down_write(&vi->i_alloc_sem);
  1812. err = ntfs_truncate(vi);
  1813. up_write(&vi->i_alloc_sem);
  1814. if (err || NInoTruncateFailed(ni)) {
  1815. if (!err)
  1816. err = -EIO;
  1817. ntfs_error(vol->sb, "Cannot perform write to inode "
  1818. "0x%lx, attribute type 0x%x, because "
  1819. "ntfs_truncate() failed (error code "
  1820. "%i).", vi->i_ino,
  1821. (unsigned)le32_to_cpu(ni->type), err);
  1822. return err;
  1823. }
  1824. }
  1825. /* The first byte after the write. */
  1826. end = pos + count;
  1827. /*
  1828. * If the write goes beyond the allocated size, extend the allocation
  1829. * to cover the whole of the write, rounded up to the nearest cluster.
  1830. */
  1831. read_lock_irqsave(&ni->size_lock, flags);
  1832. ll = ni->allocated_size;
  1833. read_unlock_irqrestore(&ni->size_lock, flags);
  1834. if (end > ll) {
  1835. /* Extend the allocation without changing the data size. */
  1836. ll = ntfs_attr_extend_allocation(ni, end, -1, pos);
  1837. if (likely(ll >= 0)) {
  1838. BUG_ON(pos >= ll);
  1839. /* If the extension was partial truncate the write. */
  1840. if (end > ll) {
  1841. ntfs_debug("Truncating write to inode 0x%lx, "
  1842. "attribute type 0x%x, because "
  1843. "the allocation was only "
  1844. "partially extended.",
  1845. vi->i_ino, (unsigned)
  1846. le32_to_cpu(ni->type));
  1847. end = ll;
  1848. count = ll - pos;
  1849. }
  1850. } else {
  1851. err = ll;
  1852. read_lock_irqsave(&ni->size_lock, flags);
  1853. ll = ni->allocated_size;
  1854. read_unlock_irqrestore(&ni->size_lock, flags);
  1855. /* Perform a partial write if possible or fail. */
  1856. if (pos < ll) {
  1857. ntfs_debug("Truncating write to inode 0x%lx, "
  1858. "attribute type 0x%x, because "
  1859. "extending the allocation "
  1860. "failed (error code %i).",
  1861. vi->i_ino, (unsigned)
  1862. le32_to_cpu(ni->type), err);
  1863. end = ll;
  1864. count = ll - pos;
  1865. } else {
  1866. ntfs_error(vol->sb, "Cannot perform write to "
  1867. "inode 0x%lx, attribute type "
  1868. "0x%x, because extending the "
  1869. "allocation failed (error "
  1870. "code %i).", vi->i_ino,
  1871. (unsigned)
  1872. le32_to_cpu(ni->type), err);
  1873. return err;
  1874. }
  1875. }
  1876. }
  1877. pagevec_init(&lru_pvec, 0);
  1878. written = 0;
  1879. /*
  1880. * If the write starts beyond the initialized size, extend it up to the
  1881. * beginning of the write and initialize all non-sparse space between
  1882. * the old initialized size and the new one. This automatically also
  1883. * increments the vfs inode->i_size to keep it above or equal to the
  1884. * initialized_size.
  1885. */
  1886. read_lock_irqsave(&ni->size_lock, flags);
  1887. ll = ni->initialized_size;
  1888. read_unlock_irqrestore(&ni->size_lock, flags);
  1889. if (pos > ll) {
  1890. err = ntfs_attr_extend_initialized(ni, pos, &cached_page,
  1891. &lru_pvec);
  1892. if (err < 0) {
  1893. ntfs_error(vol->sb, "Cannot perform write to inode "
  1894. "0x%lx, attribute type 0x%x, because "
  1895. "extending the initialized size "
  1896. "failed (error code %i).", vi->i_ino,
  1897. (unsigned)le32_to_cpu(ni->type), err);
  1898. status = err;
  1899. goto err_out;
  1900. }
  1901. }
  1902. /*
  1903. * Determine the number of pages per cluster for non-resident
  1904. * attributes.
  1905. */
  1906. nr_pages = 1;
  1907. if (vol->cluster_size > PAGE_CACHE_SIZE && NInoNonResident(ni))
  1908. nr_pages = vol->cluster_size >> PAGE_CACHE_SHIFT;
  1909. /* Finally, perform the actual write. */
  1910. last_vcn = -1;
  1911. if (likely(nr_segs == 1))
  1912. buf = iov->iov_base;
  1913. do {
  1914. VCN vcn;
  1915. pgoff_t idx, start_idx;
  1916. unsigned ofs, do_pages, u;
  1917. size_t copied;
  1918. start_idx = idx = pos >> PAGE_CACHE_SHIFT;
  1919. ofs = pos & ~PAGE_CACHE_MASK;
  1920. bytes = PAGE_CACHE_SIZE - ofs;
  1921. do_pages = 1;
  1922. if (nr_pages > 1) {
  1923. vcn = pos >> vol->cluster_size_bits;
  1924. if (vcn != last_vcn) {
  1925. last_vcn = vcn;
  1926. /*
  1927. * Get the lcn of the vcn the write is in. If
  1928. * it is a hole, need to lock down all pages in
  1929. * the cluster.
  1930. */
  1931. down_read(&ni->runlist.lock);
  1932. lcn = ntfs_attr_vcn_to_lcn_nolock(ni, pos >>
  1933. vol->cluster_size_bits, false);
  1934. up_read(&ni->runlist.lock);
  1935. if (unlikely(lcn < LCN_HOLE)) {
  1936. status = -EIO;
  1937. if (lcn == LCN_ENOMEM)
  1938. status = -ENOMEM;
  1939. else
  1940. ntfs_error(vol->sb, "Cannot "
  1941. "perform write to "
  1942. "inode 0x%lx, "
  1943. "attribute type 0x%x, "
  1944. "because the attribute "
  1945. "is corrupt.",
  1946. vi->i_ino, (unsigned)
  1947. le32_to_cpu(ni->type));
  1948. break;
  1949. }
  1950. if (lcn == LCN_HOLE) {
  1951. start_idx = (pos & ~(s64)
  1952. vol->cluster_size_mask)
  1953. >> PAGE_CACHE_SHIFT;
  1954. bytes = vol->cluster_size - (pos &
  1955. vol->cluster_size_mask);
  1956. do_pages = nr_pages;
  1957. }
  1958. }
  1959. }
  1960. if (bytes > count)
  1961. bytes = count;
  1962. /*
  1963. * Bring in the user page(s) that we will copy from _first_.
  1964. * Otherwise there is a nasty deadlock on copying from the same
  1965. * page(s) as we are writing to, without it/them being marked
  1966. * up-to-date. Note, at present there is nothing to stop the
  1967. * pages being swapped out between us bringing them into memory
  1968. * and doing the actual copying.
  1969. */
  1970. if (likely(nr_segs == 1))
  1971. ntfs_fault_in_pages_readable(buf, bytes);
  1972. else
  1973. ntfs_fault_in_pages_readable_iovec(iov, iov_ofs, bytes);
  1974. /* Get and lock @do_pages starting at index @start_idx. */
  1975. status = __ntfs_grab_cache_pages(mapping, start_idx, do_pages,
  1976. pages, &cached_page, &lru_pvec);
  1977. if (unlikely(status))
  1978. break;
  1979. /*
  1980. * For non-resident attributes, we need to fill any holes with
  1981. * actual clusters and ensure all bufferes are mapped. We also
  1982. * need to bring uptodate any buffers that are only partially
  1983. * being written to.
  1984. */
  1985. if (NInoNonResident(ni)) {
  1986. status = ntfs_prepare_pages_for_non_resident_write(
  1987. pages, do_pages, pos, bytes);
  1988. if (unlikely(status)) {
  1989. loff_t i_size;
  1990. do {
  1991. unlock_page(pages[--do_pages]);
  1992. page_cache_release(pages[do_pages]);
  1993. } while (do_pages);
  1994. /*
  1995. * The write preparation may have instantiated
  1996. * allocated space outside i_size. Trim this
  1997. * off again. We can ignore any errors in this
  1998. * case as we will just be waisting a bit of
  1999. * allocated space, which is not a disaster.
  2000. */
  2001. i_size = i_size_read(vi);
  2002. if (pos + bytes > i_size)
  2003. vmtruncate(vi, i_size);
  2004. break;
  2005. }
  2006. }
  2007. u = (pos >> PAGE_CACHE_SHIFT) - pages[0]->index;
  2008. if (likely(nr_segs == 1)) {
  2009. copied = ntfs_copy_from_user(pages + u, do_pages - u,
  2010. ofs, buf, bytes);
  2011. buf += copied;
  2012. } else
  2013. copied = ntfs_copy_from_user_iovec(pages + u,
  2014. do_pages - u, ofs, &iov, &iov_ofs,
  2015. bytes);
  2016. ntfs_flush_dcache_pages(pages + u, do_pages - u);
  2017. status = ntfs_commit_pages_after_write(pages, do_pages, pos,
  2018. bytes);
  2019. if (likely(!status)) {
  2020. written += copied;
  2021. count -= copied;
  2022. pos += copied;
  2023. if (unlikely(copied != bytes))
  2024. status = -EFAULT;
  2025. }
  2026. do {
  2027. unlock_page(pages[--do_pages]);
  2028. mark_page_accessed(pages[do_pages]);
  2029. page_cache_release(pages[do_pages]);
  2030. } while (do_pages);
  2031. if (unlikely(status))
  2032. break;
  2033. balance_dirty_pages_ratelimited(mapping);
  2034. cond_resched();
  2035. } while (count);
  2036. err_out:
  2037. *ppos = pos;
  2038. if (cached_page)
  2039. page_cache_release(cached_page);
  2040. /* For now, when the user asks for O_SYNC, we actually give O_DSYNC. */
  2041. if (likely(!status)) {
  2042. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(vi))) {
  2043. if (!mapping->a_ops->writepage || !is_sync_kiocb(iocb))
  2044. status = generic_osync_inode(vi, mapping,
  2045. OSYNC_METADATA|OSYNC_DATA);
  2046. }
  2047. }
  2048. pagevec_lru_add(&lru_pvec);
  2049. ntfs_debug("Done. Returning %s (written 0x%lx, status %li).",
  2050. written ? "written" : "status", (unsigned long)written,
  2051. (long)status);
  2052. return written ? written : status;
  2053. }
  2054. /**
  2055. * ntfs_file_aio_write_nolock -
  2056. */
  2057. static ssize_t ntfs_file_aio_write_nolock(struct kiocb *iocb,
  2058. const struct iovec *iov, unsigned long nr_segs, loff_t *ppos)
  2059. {
  2060. struct file *file = iocb->ki_filp;
  2061. struct address_space *mapping = file->f_mapping;
  2062. struct inode *inode = mapping->host;
  2063. loff_t pos;
  2064. size_t count; /* after file limit checks */
  2065. ssize_t written, err;
  2066. count = 0;
  2067. err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  2068. if (err)
  2069. return err;
  2070. pos = *ppos;
  2071. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  2072. /* We can write back this queue in page reclaim. */
  2073. current->backing_dev_info = mapping->backing_dev_info;
  2074. written = 0;
  2075. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  2076. if (err)
  2077. goto out;
  2078. if (!count)
  2079. goto out;
  2080. err = remove_suid(file->f_path.dentry);
  2081. if (err)
  2082. goto out;
  2083. file_update_time(file);
  2084. written = ntfs_file_buffered_write(iocb, iov, nr_segs, pos, ppos,
  2085. count);
  2086. out:
  2087. current->backing_dev_info = NULL;
  2088. return written ? written : err;
  2089. }
  2090. /**
  2091. * ntfs_file_aio_write -
  2092. */
  2093. static ssize_t ntfs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  2094. unsigned long nr_segs, loff_t pos)
  2095. {
  2096. struct file *file = iocb->ki_filp;
  2097. struct address_space *mapping = file->f_mapping;
  2098. struct inode *inode = mapping->host;
  2099. ssize_t ret;
  2100. BUG_ON(iocb->ki_pos != pos);
  2101. mutex_lock(&inode->i_mutex);
  2102. ret = ntfs_file_aio_write_nolock(iocb, iov, nr_segs, &iocb->ki_pos);
  2103. mutex_unlock(&inode->i_mutex);
  2104. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2105. int err = sync_page_range(inode, mapping, pos, ret);
  2106. if (err < 0)
  2107. ret = err;
  2108. }
  2109. return ret;
  2110. }
  2111. /**
  2112. * ntfs_file_writev -
  2113. *
  2114. * Basically the same as generic_file_writev() except that it ends up calling
  2115. * ntfs_file_aio_write_nolock() instead of __generic_file_aio_write_nolock().
  2116. */
  2117. static ssize_t ntfs_file_writev(struct file *file, const struct iovec *iov,
  2118. unsigned long nr_segs, loff_t *ppos)
  2119. {
  2120. struct address_space *mapping = file->f_mapping;
  2121. struct inode *inode = mapping->host;
  2122. struct kiocb kiocb;
  2123. ssize_t ret;
  2124. mutex_lock(&inode->i_mutex);
  2125. init_sync_kiocb(&kiocb, file);
  2126. ret = ntfs_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
  2127. if (ret == -EIOCBQUEUED)
  2128. ret = wait_on_sync_kiocb(&kiocb);
  2129. mutex_unlock(&inode->i_mutex);
  2130. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2131. int err = sync_page_range(inode, mapping, *ppos - ret, ret);
  2132. if (err < 0)
  2133. ret = err;
  2134. }
  2135. return ret;
  2136. }
  2137. /**
  2138. * ntfs_file_write - simple wrapper for ntfs_file_writev()
  2139. */
  2140. static ssize_t ntfs_file_write(struct file *file, const char __user *buf,
  2141. size_t count, loff_t *ppos)
  2142. {
  2143. struct iovec local_iov = { .iov_base = (void __user *)buf,
  2144. .iov_len = count };
  2145. return ntfs_file_writev(file, &local_iov, 1, ppos);
  2146. }
  2147. /**
  2148. * ntfs_file_fsync - sync a file to disk
  2149. * @filp: file to be synced
  2150. * @dentry: dentry describing the file to sync
  2151. * @datasync: if non-zero only flush user data and not metadata
  2152. *
  2153. * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
  2154. * system calls. This function is inspired by fs/buffer.c::file_fsync().
  2155. *
  2156. * If @datasync is false, write the mft record and all associated extent mft
  2157. * records as well as the $DATA attribute and then sync the block device.
  2158. *
  2159. * If @datasync is true and the attribute is non-resident, we skip the writing
  2160. * of the mft record and all associated extent mft records (this might still
  2161. * happen due to the write_inode_now() call).
  2162. *
  2163. * Also, if @datasync is true, we do not wait on the inode to be written out
  2164. * but we always wait on the page cache pages to be written out.
  2165. *
  2166. * Note: In the past @filp could be NULL so we ignore it as we don't need it
  2167. * anyway.
  2168. *
  2169. * Locking: Caller must hold i_mutex on the inode.
  2170. *
  2171. * TODO: We should probably also write all attribute/index inodes associated
  2172. * with this inode but since we have no simple way of getting to them we ignore
  2173. * this problem for now.
  2174. */
  2175. static int ntfs_file_fsync(struct file *filp, struct dentry *dentry,
  2176. int datasync)
  2177. {
  2178. struct inode *vi = dentry->d_inode;
  2179. int err, ret = 0;
  2180. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  2181. BUG_ON(S_ISDIR(vi->i_mode));
  2182. if (!datasync || !NInoNonResident(NTFS_I(vi)))
  2183. ret = ntfs_write_inode(vi, 1);
  2184. write_inode_now(vi, !datasync);
  2185. /*
  2186. * NOTE: If we were to use mapping->private_list (see ext2 and
  2187. * fs/buffer.c) for dirty blocks then we could optimize the below to be
  2188. * sync_mapping_buffers(vi->i_mapping).
  2189. */
  2190. err = sync_blockdev(vi->i_sb->s_bdev);
  2191. if (unlikely(err && !ret))
  2192. ret = err;
  2193. if (likely(!ret))
  2194. ntfs_debug("Done.");
  2195. else
  2196. ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
  2197. "%u.", datasync ? "data" : "", vi->i_ino, -ret);
  2198. return ret;
  2199. }
  2200. #endif /* NTFS_RW */
  2201. const struct file_operations ntfs_file_ops = {
  2202. .llseek = generic_file_llseek, /* Seek inside file. */
  2203. .read = do_sync_read, /* Read from file. */
  2204. .aio_read = generic_file_aio_read, /* Async read from file. */
  2205. #ifdef NTFS_RW
  2206. .write = ntfs_file_write, /* Write to file. */
  2207. .aio_write = ntfs_file_aio_write, /* Async write to file. */
  2208. /*.release = ,*/ /* Last file is closed. See
  2209. fs/ext2/file.c::
  2210. ext2_release_file() for
  2211. how to use this to discard
  2212. preallocated space for
  2213. write opened files. */
  2214. .fsync = ntfs_file_fsync, /* Sync a file to disk. */
  2215. /*.aio_fsync = ,*/ /* Sync all outstanding async
  2216. i/o operations on a
  2217. kiocb. */
  2218. #endif /* NTFS_RW */
  2219. /*.ioctl = ,*/ /* Perform function on the
  2220. mounted filesystem. */
  2221. .mmap = generic_file_mmap, /* Mmap file. */
  2222. .open = ntfs_file_open, /* Open file. */
  2223. .splice_read = generic_file_splice_read /* Zero-copy data send with
  2224. the data source being on
  2225. the ntfs partition. We do
  2226. not need to care about the
  2227. data destination. */
  2228. /*.sendpage = ,*/ /* Zero-copy data send with
  2229. the data destination being
  2230. on the ntfs partition. We
  2231. do not need to care about
  2232. the data source. */
  2233. };
  2234. const struct inode_operations ntfs_file_inode_ops = {
  2235. #ifdef NTFS_RW
  2236. .truncate = ntfs_truncate_vfs,
  2237. .setattr = ntfs_setattr,
  2238. #endif /* NTFS_RW */
  2239. };
  2240. const struct file_operations ntfs_empty_file_ops = {};
  2241. const struct inode_operations ntfs_empty_inode_ops = {};