file.c 70 KB

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