file.c 60 KB

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