file.c 70 KB

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