file.c 68 KB

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