file.c 69 KB

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