file.c 70 KB

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