aops.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /**
  2. * aops.c - NTFS kernel address space operations and page cache handling.
  3. * Part of the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2001-2005 Anton Altaparmakov
  6. * Copyright (c) 2002 Richard Russon
  7. *
  8. * This program/include file is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program/include file is distributed in the hope that it will be
  14. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program (in the main directory of the Linux-NTFS
  20. * distribution in the file COPYING); if not, write to the Free Software
  21. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/mm.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/swap.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/writeback.h>
  29. #include <linux/bit_spinlock.h>
  30. #include "aops.h"
  31. #include "attrib.h"
  32. #include "debug.h"
  33. #include "inode.h"
  34. #include "mft.h"
  35. #include "runlist.h"
  36. #include "types.h"
  37. #include "ntfs.h"
  38. /**
  39. * ntfs_end_buffer_async_read - async io completion for reading attributes
  40. * @bh: buffer head on which io is completed
  41. * @uptodate: whether @bh is now uptodate or not
  42. *
  43. * Asynchronous I/O completion handler for reading pages belonging to the
  44. * attribute address space of an inode. The inodes can either be files or
  45. * directories or they can be fake inodes describing some attribute.
  46. *
  47. * If NInoMstProtected(), perform the post read mst fixups when all IO on the
  48. * page has been completed and mark the page uptodate or set the error bit on
  49. * the page. To determine the size of the records that need fixing up, we
  50. * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
  51. * record size, and index_block_size_bits, to the log(base 2) of the ntfs
  52. * record size.
  53. */
  54. static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  55. {
  56. unsigned long flags;
  57. struct buffer_head *first, *tmp;
  58. struct page *page;
  59. struct inode *vi;
  60. ntfs_inode *ni;
  61. int page_uptodate = 1;
  62. page = bh->b_page;
  63. vi = page->mapping->host;
  64. ni = NTFS_I(vi);
  65. if (likely(uptodate)) {
  66. loff_t i_size;
  67. s64 file_ofs, init_size;
  68. set_buffer_uptodate(bh);
  69. file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
  70. bh_offset(bh);
  71. read_lock_irqsave(&ni->size_lock, flags);
  72. init_size = ni->initialized_size;
  73. i_size = i_size_read(vi);
  74. read_unlock_irqrestore(&ni->size_lock, flags);
  75. if (unlikely(init_size > i_size)) {
  76. /* Race with shrinking truncate. */
  77. init_size = i_size;
  78. }
  79. /* Check for the current buffer head overflowing. */
  80. if (unlikely(file_ofs + bh->b_size > init_size)) {
  81. u8 *kaddr;
  82. int ofs;
  83. ofs = 0;
  84. if (file_ofs < init_size)
  85. ofs = init_size - file_ofs;
  86. kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  87. memset(kaddr + bh_offset(bh) + ofs, 0,
  88. bh->b_size - ofs);
  89. kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
  90. flush_dcache_page(page);
  91. }
  92. } else {
  93. clear_buffer_uptodate(bh);
  94. SetPageError(page);
  95. ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
  96. "0x%llx.", (unsigned long long)bh->b_blocknr);
  97. }
  98. first = page_buffers(page);
  99. local_irq_save(flags);
  100. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  101. clear_buffer_async_read(bh);
  102. unlock_buffer(bh);
  103. tmp = bh;
  104. do {
  105. if (!buffer_uptodate(tmp))
  106. page_uptodate = 0;
  107. if (buffer_async_read(tmp)) {
  108. if (likely(buffer_locked(tmp)))
  109. goto still_busy;
  110. /* Async buffers must be locked. */
  111. BUG();
  112. }
  113. tmp = tmp->b_this_page;
  114. } while (tmp != bh);
  115. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  116. local_irq_restore(flags);
  117. /*
  118. * If none of the buffers had errors then we can set the page uptodate,
  119. * but we first have to perform the post read mst fixups, if the
  120. * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
  121. * Note we ignore fixup errors as those are detected when
  122. * map_mft_record() is called which gives us per record granularity
  123. * rather than per page granularity.
  124. */
  125. if (!NInoMstProtected(ni)) {
  126. if (likely(page_uptodate && !PageError(page)))
  127. SetPageUptodate(page);
  128. } else {
  129. u8 *kaddr;
  130. unsigned int i, recs;
  131. u32 rec_size;
  132. rec_size = ni->itype.index.block_size;
  133. recs = PAGE_CACHE_SIZE / rec_size;
  134. /* Should have been verified before we got here... */
  135. BUG_ON(!recs);
  136. kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  137. for (i = 0; i < recs; i++)
  138. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  139. i * rec_size), rec_size);
  140. kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
  141. flush_dcache_page(page);
  142. if (likely(page_uptodate && !PageError(page)))
  143. SetPageUptodate(page);
  144. }
  145. unlock_page(page);
  146. return;
  147. still_busy:
  148. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  149. local_irq_restore(flags);
  150. return;
  151. }
  152. /**
  153. * ntfs_read_block - fill a @page of an address space with data
  154. * @page: page cache page to fill with data
  155. *
  156. * Fill the page @page of the address space belonging to the @page->host inode.
  157. * We read each buffer asynchronously and when all buffers are read in, our io
  158. * completion handler ntfs_end_buffer_read_async(), if required, automatically
  159. * applies the mst fixups to the page before finally marking it uptodate and
  160. * unlocking it.
  161. *
  162. * We only enforce allocated_size limit because i_size is checked for in
  163. * generic_file_read().
  164. *
  165. * Return 0 on success and -errno on error.
  166. *
  167. * Contains an adapted version of fs/buffer.c::block_read_full_page().
  168. */
  169. static int ntfs_read_block(struct page *page)
  170. {
  171. loff_t i_size;
  172. VCN vcn;
  173. LCN lcn;
  174. s64 init_size;
  175. struct inode *vi;
  176. ntfs_inode *ni;
  177. ntfs_volume *vol;
  178. runlist_element *rl;
  179. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  180. sector_t iblock, lblock, zblock;
  181. unsigned long flags;
  182. unsigned int blocksize, vcn_ofs;
  183. int i, nr;
  184. unsigned char blocksize_bits;
  185. vi = page->mapping->host;
  186. ni = NTFS_I(vi);
  187. vol = ni->vol;
  188. /* $MFT/$DATA must have its complete runlist in memory at all times. */
  189. BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
  190. blocksize_bits = VFS_I(ni)->i_blkbits;
  191. blocksize = 1 << blocksize_bits;
  192. if (!page_has_buffers(page)) {
  193. create_empty_buffers(page, blocksize, 0);
  194. if (unlikely(!page_has_buffers(page))) {
  195. unlock_page(page);
  196. return -ENOMEM;
  197. }
  198. }
  199. bh = head = page_buffers(page);
  200. BUG_ON(!bh);
  201. /*
  202. * We may be racing with truncate. To avoid some of the problems we
  203. * now take a snapshot of the various sizes and use those for the whole
  204. * of the function. In case of an extending truncate it just means we
  205. * may leave some buffers unmapped which are now allocated. This is
  206. * not a problem since these buffers will just get mapped when a write
  207. * occurs. In case of a shrinking truncate, we will detect this later
  208. * on due to the runlist being incomplete and if the page is being
  209. * fully truncated, truncate will throw it away as soon as we unlock
  210. * it so no need to worry what we do with it.
  211. */
  212. iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  213. read_lock_irqsave(&ni->size_lock, flags);
  214. lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
  215. init_size = ni->initialized_size;
  216. i_size = i_size_read(vi);
  217. read_unlock_irqrestore(&ni->size_lock, flags);
  218. if (unlikely(init_size > i_size)) {
  219. /* Race with shrinking truncate. */
  220. init_size = i_size;
  221. }
  222. zblock = (init_size + blocksize - 1) >> blocksize_bits;
  223. /* Loop through all the buffers in the page. */
  224. rl = NULL;
  225. nr = i = 0;
  226. do {
  227. u8 *kaddr;
  228. int err;
  229. if (unlikely(buffer_uptodate(bh)))
  230. continue;
  231. if (unlikely(buffer_mapped(bh))) {
  232. arr[nr++] = bh;
  233. continue;
  234. }
  235. err = 0;
  236. bh->b_bdev = vol->sb->s_bdev;
  237. /* Is the block within the allowed limits? */
  238. if (iblock < lblock) {
  239. BOOL is_retry = FALSE;
  240. /* Convert iblock into corresponding vcn and offset. */
  241. vcn = (VCN)iblock << blocksize_bits >>
  242. vol->cluster_size_bits;
  243. vcn_ofs = ((VCN)iblock << blocksize_bits) &
  244. vol->cluster_size_mask;
  245. if (!rl) {
  246. lock_retry_remap:
  247. down_read(&ni->runlist.lock);
  248. rl = ni->runlist.rl;
  249. }
  250. if (likely(rl != NULL)) {
  251. /* Seek to element containing target vcn. */
  252. while (rl->length && rl[1].vcn <= vcn)
  253. rl++;
  254. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  255. } else
  256. lcn = LCN_RL_NOT_MAPPED;
  257. /* Successful remap. */
  258. if (lcn >= 0) {
  259. /* Setup buffer head to correct block. */
  260. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  261. + vcn_ofs) >> blocksize_bits;
  262. set_buffer_mapped(bh);
  263. /* Only read initialized data blocks. */
  264. if (iblock < zblock) {
  265. arr[nr++] = bh;
  266. continue;
  267. }
  268. /* Fully non-initialized data block, zero it. */
  269. goto handle_zblock;
  270. }
  271. /* It is a hole, need to zero it. */
  272. if (lcn == LCN_HOLE)
  273. goto handle_hole;
  274. /* If first try and runlist unmapped, map and retry. */
  275. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  276. is_retry = TRUE;
  277. /*
  278. * Attempt to map runlist, dropping lock for
  279. * the duration.
  280. */
  281. up_read(&ni->runlist.lock);
  282. err = ntfs_map_runlist(ni, vcn);
  283. if (likely(!err))
  284. goto lock_retry_remap;
  285. rl = NULL;
  286. } else if (!rl)
  287. up_read(&ni->runlist.lock);
  288. /*
  289. * If buffer is outside the runlist, treat it as a
  290. * hole. This can happen due to concurrent truncate
  291. * for example.
  292. */
  293. if (err == -ENOENT || lcn == LCN_ENOENT) {
  294. err = 0;
  295. goto handle_hole;
  296. }
  297. /* Hard error, zero out region. */
  298. if (!err)
  299. err = -EIO;
  300. bh->b_blocknr = -1;
  301. SetPageError(page);
  302. ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
  303. "attribute type 0x%x, vcn 0x%llx, "
  304. "offset 0x%x because its location on "
  305. "disk could not be determined%s "
  306. "(error code %i).", ni->mft_no,
  307. ni->type, (unsigned long long)vcn,
  308. vcn_ofs, is_retry ? " even after "
  309. "retrying" : "", err);
  310. }
  311. /*
  312. * Either iblock was outside lblock limits or
  313. * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
  314. * of the page and set the buffer uptodate.
  315. */
  316. handle_hole:
  317. bh->b_blocknr = -1UL;
  318. clear_buffer_mapped(bh);
  319. handle_zblock:
  320. kaddr = kmap_atomic(page, KM_USER0);
  321. memset(kaddr + i * blocksize, 0, blocksize);
  322. kunmap_atomic(kaddr, KM_USER0);
  323. flush_dcache_page(page);
  324. if (likely(!err))
  325. set_buffer_uptodate(bh);
  326. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  327. /* Release the lock if we took it. */
  328. if (rl)
  329. up_read(&ni->runlist.lock);
  330. /* Check we have at least one buffer ready for i/o. */
  331. if (nr) {
  332. struct buffer_head *tbh;
  333. /* Lock the buffers. */
  334. for (i = 0; i < nr; i++) {
  335. tbh = arr[i];
  336. lock_buffer(tbh);
  337. tbh->b_end_io = ntfs_end_buffer_async_read;
  338. set_buffer_async_read(tbh);
  339. }
  340. /* Finally, start i/o on the buffers. */
  341. for (i = 0; i < nr; i++) {
  342. tbh = arr[i];
  343. if (likely(!buffer_uptodate(tbh)))
  344. submit_bh(READ, tbh);
  345. else
  346. ntfs_end_buffer_async_read(tbh, 1);
  347. }
  348. return 0;
  349. }
  350. /* No i/o was scheduled on any of the buffers. */
  351. if (likely(!PageError(page)))
  352. SetPageUptodate(page);
  353. else /* Signal synchronous i/o error. */
  354. nr = -EIO;
  355. unlock_page(page);
  356. return nr;
  357. }
  358. /**
  359. * ntfs_readpage - fill a @page of a @file with data from the device
  360. * @file: open file to which the page @page belongs or NULL
  361. * @page: page cache page to fill with data
  362. *
  363. * For non-resident attributes, ntfs_readpage() fills the @page of the open
  364. * file @file by calling the ntfs version of the generic block_read_full_page()
  365. * function, ntfs_read_block(), which in turn creates and reads in the buffers
  366. * associated with the page asynchronously.
  367. *
  368. * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
  369. * data from the mft record (which at this stage is most likely in memory) and
  370. * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
  371. * even if the mft record is not cached at this point in time, we need to wait
  372. * for it to be read in before we can do the copy.
  373. *
  374. * Return 0 on success and -errno on error.
  375. */
  376. static int ntfs_readpage(struct file *file, struct page *page)
  377. {
  378. loff_t i_size;
  379. struct inode *vi;
  380. ntfs_inode *ni, *base_ni;
  381. u8 *kaddr;
  382. ntfs_attr_search_ctx *ctx;
  383. MFT_RECORD *mrec;
  384. unsigned long flags;
  385. u32 attr_len;
  386. int err = 0;
  387. retry_readpage:
  388. BUG_ON(!PageLocked(page));
  389. /*
  390. * This can potentially happen because we clear PageUptodate() during
  391. * ntfs_writepage() of MstProtected() attributes.
  392. */
  393. if (PageUptodate(page)) {
  394. unlock_page(page);
  395. return 0;
  396. }
  397. vi = page->mapping->host;
  398. ni = NTFS_I(vi);
  399. /*
  400. * Only $DATA attributes can be encrypted and only unnamed $DATA
  401. * attributes can be compressed. Index root can have the flags set but
  402. * this means to create compressed/encrypted files, not that the
  403. * attribute is compressed/encrypted. Note we need to check for
  404. * AT_INDEX_ALLOCATION since this is the type of both directory and
  405. * index inodes.
  406. */
  407. if (ni->type != AT_INDEX_ALLOCATION) {
  408. /* If attribute is encrypted, deny access, just like NT4. */
  409. if (NInoEncrypted(ni)) {
  410. BUG_ON(ni->type != AT_DATA);
  411. err = -EACCES;
  412. goto err_out;
  413. }
  414. /* Compressed data streams are handled in compress.c. */
  415. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  416. BUG_ON(ni->type != AT_DATA);
  417. BUG_ON(ni->name_len);
  418. return ntfs_read_compressed_block(page);
  419. }
  420. }
  421. /* NInoNonResident() == NInoIndexAllocPresent() */
  422. if (NInoNonResident(ni)) {
  423. /* Normal, non-resident data stream. */
  424. return ntfs_read_block(page);
  425. }
  426. /*
  427. * Attribute is resident, implying it is not compressed or encrypted.
  428. * This also means the attribute is smaller than an mft record and
  429. * hence smaller than a page, so can simply zero out any pages with
  430. * index above 0. Note the attribute can actually be marked compressed
  431. * but if it is resident the actual data is not compressed so we are
  432. * ok to ignore the compressed flag here.
  433. */
  434. if (unlikely(page->index > 0)) {
  435. kaddr = kmap_atomic(page, KM_USER0);
  436. memset(kaddr, 0, PAGE_CACHE_SIZE);
  437. flush_dcache_page(page);
  438. kunmap_atomic(kaddr, KM_USER0);
  439. goto done;
  440. }
  441. if (!NInoAttr(ni))
  442. base_ni = ni;
  443. else
  444. base_ni = ni->ext.base_ntfs_ino;
  445. /* Map, pin, and lock the mft record. */
  446. mrec = map_mft_record(base_ni);
  447. if (IS_ERR(mrec)) {
  448. err = PTR_ERR(mrec);
  449. goto err_out;
  450. }
  451. /*
  452. * If a parallel write made the attribute non-resident, drop the mft
  453. * record and retry the readpage.
  454. */
  455. if (unlikely(NInoNonResident(ni))) {
  456. unmap_mft_record(base_ni);
  457. goto retry_readpage;
  458. }
  459. ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
  460. if (unlikely(!ctx)) {
  461. err = -ENOMEM;
  462. goto unm_err_out;
  463. }
  464. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  465. CASE_SENSITIVE, 0, NULL, 0, ctx);
  466. if (unlikely(err))
  467. goto put_unm_err_out;
  468. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  469. read_lock_irqsave(&ni->size_lock, flags);
  470. if (unlikely(attr_len > ni->initialized_size))
  471. attr_len = ni->initialized_size;
  472. i_size = i_size_read(vi);
  473. read_unlock_irqrestore(&ni->size_lock, flags);
  474. if (unlikely(attr_len > i_size)) {
  475. /* Race with shrinking truncate. */
  476. attr_len = i_size;
  477. }
  478. kaddr = kmap_atomic(page, KM_USER0);
  479. /* Copy the data to the page. */
  480. memcpy(kaddr, (u8*)ctx->attr +
  481. le16_to_cpu(ctx->attr->data.resident.value_offset),
  482. attr_len);
  483. /* Zero the remainder of the page. */
  484. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  485. flush_dcache_page(page);
  486. kunmap_atomic(kaddr, KM_USER0);
  487. put_unm_err_out:
  488. ntfs_attr_put_search_ctx(ctx);
  489. unm_err_out:
  490. unmap_mft_record(base_ni);
  491. done:
  492. SetPageUptodate(page);
  493. err_out:
  494. unlock_page(page);
  495. return err;
  496. }
  497. #ifdef NTFS_RW
  498. /**
  499. * ntfs_write_block - write a @page to the backing store
  500. * @page: page cache page to write out
  501. * @wbc: writeback control structure
  502. *
  503. * This function is for writing pages belonging to non-resident, non-mst
  504. * protected attributes to their backing store.
  505. *
  506. * For a page with buffers, map and write the dirty buffers asynchronously
  507. * under page writeback. For a page without buffers, create buffers for the
  508. * page, then proceed as above.
  509. *
  510. * If a page doesn't have buffers the page dirty state is definitive. If a page
  511. * does have buffers, the page dirty state is just a hint, and the buffer dirty
  512. * state is definitive. (A hint which has rules: dirty buffers against a clean
  513. * page is illegal. Other combinations are legal and need to be handled. In
  514. * particular a dirty page containing clean buffers for example.)
  515. *
  516. * Return 0 on success and -errno on error.
  517. *
  518. * Based on ntfs_read_block() and __block_write_full_page().
  519. */
  520. static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
  521. {
  522. VCN vcn;
  523. LCN lcn;
  524. s64 initialized_size;
  525. loff_t i_size;
  526. sector_t block, dblock, iblock;
  527. struct inode *vi;
  528. ntfs_inode *ni;
  529. ntfs_volume *vol;
  530. runlist_element *rl;
  531. struct buffer_head *bh, *head;
  532. unsigned long flags;
  533. unsigned int blocksize, vcn_ofs;
  534. int err;
  535. BOOL need_end_writeback;
  536. unsigned char blocksize_bits;
  537. vi = page->mapping->host;
  538. ni = NTFS_I(vi);
  539. vol = ni->vol;
  540. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  541. "0x%lx.", ni->mft_no, ni->type, page->index);
  542. BUG_ON(!NInoNonResident(ni));
  543. BUG_ON(NInoMstProtected(ni));
  544. blocksize_bits = vi->i_blkbits;
  545. blocksize = 1 << blocksize_bits;
  546. if (!page_has_buffers(page)) {
  547. BUG_ON(!PageUptodate(page));
  548. create_empty_buffers(page, blocksize,
  549. (1 << BH_Uptodate) | (1 << BH_Dirty));
  550. if (unlikely(!page_has_buffers(page))) {
  551. ntfs_warning(vol->sb, "Error allocating page "
  552. "buffers. Redirtying page so we try "
  553. "again later.");
  554. /*
  555. * Put the page back on mapping->dirty_pages, but leave
  556. * its buffers' dirty state as-is.
  557. */
  558. redirty_page_for_writepage(wbc, page);
  559. unlock_page(page);
  560. return 0;
  561. }
  562. }
  563. bh = head = page_buffers(page);
  564. BUG_ON(!bh);
  565. /* NOTE: Different naming scheme to ntfs_read_block()! */
  566. /* The first block in the page. */
  567. block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  568. read_lock_irqsave(&ni->size_lock, flags);
  569. i_size = i_size_read(vi);
  570. initialized_size = ni->initialized_size;
  571. read_unlock_irqrestore(&ni->size_lock, flags);
  572. /* The first out of bounds block for the data size. */
  573. dblock = (i_size + blocksize - 1) >> blocksize_bits;
  574. /* The last (fully or partially) initialized block. */
  575. iblock = initialized_size >> blocksize_bits;
  576. /*
  577. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  578. * here, and the (potentially unmapped) buffers may become dirty at
  579. * any time. If a buffer becomes dirty here after we've inspected it
  580. * then we just miss that fact, and the page stays dirty.
  581. *
  582. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  583. * handle that here by just cleaning them.
  584. */
  585. /*
  586. * Loop through all the buffers in the page, mapping all the dirty
  587. * buffers to disk addresses and handling any aliases from the
  588. * underlying block device's mapping.
  589. */
  590. rl = NULL;
  591. err = 0;
  592. do {
  593. BOOL is_retry = FALSE;
  594. if (unlikely(block >= dblock)) {
  595. /*
  596. * Mapped buffers outside i_size will occur, because
  597. * this page can be outside i_size when there is a
  598. * truncate in progress. The contents of such buffers
  599. * were zeroed by ntfs_writepage().
  600. *
  601. * FIXME: What about the small race window where
  602. * ntfs_writepage() has not done any clearing because
  603. * the page was within i_size but before we get here,
  604. * vmtruncate() modifies i_size?
  605. */
  606. clear_buffer_dirty(bh);
  607. set_buffer_uptodate(bh);
  608. continue;
  609. }
  610. /* Clean buffers are not written out, so no need to map them. */
  611. if (!buffer_dirty(bh))
  612. continue;
  613. /* Make sure we have enough initialized size. */
  614. if (unlikely((block >= iblock) &&
  615. (initialized_size < i_size))) {
  616. /*
  617. * If this page is fully outside initialized size, zero
  618. * out all pages between the current initialized size
  619. * and the current page. Just use ntfs_readpage() to do
  620. * the zeroing transparently.
  621. */
  622. if (block > iblock) {
  623. // TODO:
  624. // For each page do:
  625. // - read_cache_page()
  626. // Again for each page do:
  627. // - wait_on_page_locked()
  628. // - Check (PageUptodate(page) &&
  629. // !PageError(page))
  630. // Update initialized size in the attribute and
  631. // in the inode.
  632. // Again, for each page do:
  633. // __set_page_dirty_buffers();
  634. // page_cache_release()
  635. // We don't need to wait on the writes.
  636. // Update iblock.
  637. }
  638. /*
  639. * The current page straddles initialized size. Zero
  640. * all non-uptodate buffers and set them uptodate (and
  641. * dirty?). Note, there aren't any non-uptodate buffers
  642. * if the page is uptodate.
  643. * FIXME: For an uptodate page, the buffers may need to
  644. * be written out because they were not initialized on
  645. * disk before.
  646. */
  647. if (!PageUptodate(page)) {
  648. // TODO:
  649. // Zero any non-uptodate buffers up to i_size.
  650. // Set them uptodate and dirty.
  651. }
  652. // TODO:
  653. // Update initialized size in the attribute and in the
  654. // inode (up to i_size).
  655. // Update iblock.
  656. // FIXME: This is inefficient. Try to batch the two
  657. // size changes to happen in one go.
  658. ntfs_error(vol->sb, "Writing beyond initialized size "
  659. "is not supported yet. Sorry.");
  660. err = -EOPNOTSUPP;
  661. break;
  662. // Do NOT set_buffer_new() BUT DO clear buffer range
  663. // outside write request range.
  664. // set_buffer_uptodate() on complete buffers as well as
  665. // set_buffer_dirty().
  666. }
  667. /* No need to map buffers that are already mapped. */
  668. if (buffer_mapped(bh))
  669. continue;
  670. /* Unmapped, dirty buffer. Need to map it. */
  671. bh->b_bdev = vol->sb->s_bdev;
  672. /* Convert block into corresponding vcn and offset. */
  673. vcn = (VCN)block << blocksize_bits;
  674. vcn_ofs = vcn & vol->cluster_size_mask;
  675. vcn >>= vol->cluster_size_bits;
  676. if (!rl) {
  677. lock_retry_remap:
  678. down_read(&ni->runlist.lock);
  679. rl = ni->runlist.rl;
  680. }
  681. if (likely(rl != NULL)) {
  682. /* Seek to element containing target vcn. */
  683. while (rl->length && rl[1].vcn <= vcn)
  684. rl++;
  685. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  686. } else
  687. lcn = LCN_RL_NOT_MAPPED;
  688. /* Successful remap. */
  689. if (lcn >= 0) {
  690. /* Setup buffer head to point to correct block. */
  691. bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
  692. vcn_ofs) >> blocksize_bits;
  693. set_buffer_mapped(bh);
  694. continue;
  695. }
  696. /* It is a hole, need to instantiate it. */
  697. if (lcn == LCN_HOLE) {
  698. u8 *kaddr;
  699. unsigned long *bpos, *bend;
  700. /* Check if the buffer is zero. */
  701. kaddr = kmap_atomic(page, KM_USER0);
  702. bpos = (unsigned long *)(kaddr + bh_offset(bh));
  703. bend = (unsigned long *)((u8*)bpos + blocksize);
  704. do {
  705. if (unlikely(*bpos))
  706. break;
  707. } while (likely(++bpos < bend));
  708. kunmap_atomic(kaddr, KM_USER0);
  709. if (bpos == bend) {
  710. /*
  711. * Buffer is zero and sparse, no need to write
  712. * it.
  713. */
  714. bh->b_blocknr = -1;
  715. clear_buffer_dirty(bh);
  716. continue;
  717. }
  718. // TODO: Instantiate the hole.
  719. // clear_buffer_new(bh);
  720. // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  721. ntfs_error(vol->sb, "Writing into sparse regions is "
  722. "not supported yet. Sorry.");
  723. err = -EOPNOTSUPP;
  724. break;
  725. }
  726. /* If first try and runlist unmapped, map and retry. */
  727. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  728. is_retry = TRUE;
  729. /*
  730. * Attempt to map runlist, dropping lock for
  731. * the duration.
  732. */
  733. up_read(&ni->runlist.lock);
  734. err = ntfs_map_runlist(ni, vcn);
  735. if (likely(!err))
  736. goto lock_retry_remap;
  737. rl = NULL;
  738. } else if (!rl)
  739. up_read(&ni->runlist.lock);
  740. /*
  741. * If buffer is outside the runlist, truncate has cut it out
  742. * of the runlist. Just clean and clear the buffer and set it
  743. * uptodate so it can get discarded by the VM.
  744. */
  745. if (err == -ENOENT || lcn == LCN_ENOENT) {
  746. u8 *kaddr;
  747. bh->b_blocknr = -1;
  748. clear_buffer_dirty(bh);
  749. kaddr = kmap_atomic(page, KM_USER0);
  750. memset(kaddr + bh_offset(bh), 0, blocksize);
  751. kunmap_atomic(kaddr, KM_USER0);
  752. flush_dcache_page(page);
  753. set_buffer_uptodate(bh);
  754. err = 0;
  755. continue;
  756. }
  757. /* Failed to map the buffer, even after retrying. */
  758. if (!err)
  759. err = -EIO;
  760. bh->b_blocknr = -1;
  761. ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
  762. "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
  763. "because its location on disk could not be "
  764. "determined%s (error code %i).", ni->mft_no,
  765. ni->type, (unsigned long long)vcn,
  766. vcn_ofs, is_retry ? " even after "
  767. "retrying" : "", err);
  768. break;
  769. } while (block++, (bh = bh->b_this_page) != head);
  770. /* Release the lock if we took it. */
  771. if (rl)
  772. up_read(&ni->runlist.lock);
  773. /* For the error case, need to reset bh to the beginning. */
  774. bh = head;
  775. /* Just an optimization, so ->readpage() is not called later. */
  776. if (unlikely(!PageUptodate(page))) {
  777. int uptodate = 1;
  778. do {
  779. if (!buffer_uptodate(bh)) {
  780. uptodate = 0;
  781. bh = head;
  782. break;
  783. }
  784. } while ((bh = bh->b_this_page) != head);
  785. if (uptodate)
  786. SetPageUptodate(page);
  787. }
  788. /* Setup all mapped, dirty buffers for async write i/o. */
  789. do {
  790. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  791. lock_buffer(bh);
  792. if (test_clear_buffer_dirty(bh)) {
  793. BUG_ON(!buffer_uptodate(bh));
  794. mark_buffer_async_write(bh);
  795. } else
  796. unlock_buffer(bh);
  797. } else if (unlikely(err)) {
  798. /*
  799. * For the error case. The buffer may have been set
  800. * dirty during attachment to a dirty page.
  801. */
  802. if (err != -ENOMEM)
  803. clear_buffer_dirty(bh);
  804. }
  805. } while ((bh = bh->b_this_page) != head);
  806. if (unlikely(err)) {
  807. // TODO: Remove the -EOPNOTSUPP check later on...
  808. if (unlikely(err == -EOPNOTSUPP))
  809. err = 0;
  810. else if (err == -ENOMEM) {
  811. ntfs_warning(vol->sb, "Error allocating memory. "
  812. "Redirtying page so we try again "
  813. "later.");
  814. /*
  815. * Put the page back on mapping->dirty_pages, but
  816. * leave its buffer's dirty state as-is.
  817. */
  818. redirty_page_for_writepage(wbc, page);
  819. err = 0;
  820. } else
  821. SetPageError(page);
  822. }
  823. BUG_ON(PageWriteback(page));
  824. set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
  825. /* Submit the prepared buffers for i/o. */
  826. need_end_writeback = TRUE;
  827. do {
  828. struct buffer_head *next = bh->b_this_page;
  829. if (buffer_async_write(bh)) {
  830. submit_bh(WRITE, bh);
  831. need_end_writeback = FALSE;
  832. }
  833. bh = next;
  834. } while (bh != head);
  835. unlock_page(page);
  836. /* If no i/o was started, need to end_page_writeback(). */
  837. if (unlikely(need_end_writeback))
  838. end_page_writeback(page);
  839. ntfs_debug("Done.");
  840. return err;
  841. }
  842. /**
  843. * ntfs_write_mst_block - write a @page to the backing store
  844. * @page: page cache page to write out
  845. * @wbc: writeback control structure
  846. *
  847. * This function is for writing pages belonging to non-resident, mst protected
  848. * attributes to their backing store. The only supported attributes are index
  849. * allocation and $MFT/$DATA. Both directory inodes and index inodes are
  850. * supported for the index allocation case.
  851. *
  852. * The page must remain locked for the duration of the write because we apply
  853. * the mst fixups, write, and then undo the fixups, so if we were to unlock the
  854. * page before undoing the fixups, any other user of the page will see the
  855. * page contents as corrupt.
  856. *
  857. * We clear the page uptodate flag for the duration of the function to ensure
  858. * exclusion for the $MFT/$DATA case against someone mapping an mft record we
  859. * are about to apply the mst fixups to.
  860. *
  861. * Return 0 on success and -errno on error.
  862. *
  863. * Based on ntfs_write_block(), ntfs_mft_writepage(), and
  864. * write_mft_record_nolock().
  865. */
  866. static int ntfs_write_mst_block(struct page *page,
  867. struct writeback_control *wbc)
  868. {
  869. sector_t block, dblock, rec_block;
  870. struct inode *vi = page->mapping->host;
  871. ntfs_inode *ni = NTFS_I(vi);
  872. ntfs_volume *vol = ni->vol;
  873. u8 *kaddr;
  874. unsigned int rec_size = ni->itype.index.block_size;
  875. ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
  876. struct buffer_head *bh, *head, *tbh, *rec_start_bh;
  877. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  878. runlist_element *rl;
  879. int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
  880. unsigned bh_size, rec_size_bits;
  881. BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
  882. unsigned char bh_size_bits;
  883. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  884. "0x%lx.", vi->i_ino, ni->type, page->index);
  885. BUG_ON(!NInoNonResident(ni));
  886. BUG_ON(!NInoMstProtected(ni));
  887. is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
  888. /*
  889. * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
  890. * in its page cache were to be marked dirty. However this should
  891. * never happen with the current driver and considering we do not
  892. * handle this case here we do want to BUG(), at least for now.
  893. */
  894. BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
  895. (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
  896. bh_size_bits = vi->i_blkbits;
  897. bh_size = 1 << bh_size_bits;
  898. max_bhs = PAGE_CACHE_SIZE / bh_size;
  899. BUG_ON(!max_bhs);
  900. BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
  901. /* Were we called for sync purposes? */
  902. sync = (wbc->sync_mode == WB_SYNC_ALL);
  903. /* Make sure we have mapped buffers. */
  904. bh = head = page_buffers(page);
  905. BUG_ON(!bh);
  906. rec_size_bits = ni->itype.index.block_size_bits;
  907. BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
  908. bhs_per_rec = rec_size >> bh_size_bits;
  909. BUG_ON(!bhs_per_rec);
  910. /* The first block in the page. */
  911. rec_block = block = (sector_t)page->index <<
  912. (PAGE_CACHE_SHIFT - bh_size_bits);
  913. /* The first out of bounds block for the data size. */
  914. dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
  915. rl = NULL;
  916. err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
  917. page_is_dirty = rec_is_dirty = FALSE;
  918. rec_start_bh = NULL;
  919. do {
  920. BOOL is_retry = FALSE;
  921. if (likely(block < rec_block)) {
  922. if (unlikely(block >= dblock)) {
  923. clear_buffer_dirty(bh);
  924. set_buffer_uptodate(bh);
  925. continue;
  926. }
  927. /*
  928. * This block is not the first one in the record. We
  929. * ignore the buffer's dirty state because we could
  930. * have raced with a parallel mark_ntfs_record_dirty().
  931. */
  932. if (!rec_is_dirty)
  933. continue;
  934. if (unlikely(err2)) {
  935. if (err2 != -ENOMEM)
  936. clear_buffer_dirty(bh);
  937. continue;
  938. }
  939. } else /* if (block == rec_block) */ {
  940. BUG_ON(block > rec_block);
  941. /* This block is the first one in the record. */
  942. rec_block += bhs_per_rec;
  943. err2 = 0;
  944. if (unlikely(block >= dblock)) {
  945. clear_buffer_dirty(bh);
  946. continue;
  947. }
  948. if (!buffer_dirty(bh)) {
  949. /* Clean records are not written out. */
  950. rec_is_dirty = FALSE;
  951. continue;
  952. }
  953. rec_is_dirty = TRUE;
  954. rec_start_bh = bh;
  955. }
  956. /* Need to map the buffer if it is not mapped already. */
  957. if (unlikely(!buffer_mapped(bh))) {
  958. VCN vcn;
  959. LCN lcn;
  960. unsigned int vcn_ofs;
  961. bh->b_bdev = vol->sb->s_bdev;
  962. /* Obtain the vcn and offset of the current block. */
  963. vcn = (VCN)block << bh_size_bits;
  964. vcn_ofs = vcn & vol->cluster_size_mask;
  965. vcn >>= vol->cluster_size_bits;
  966. if (!rl) {
  967. lock_retry_remap:
  968. down_read(&ni->runlist.lock);
  969. rl = ni->runlist.rl;
  970. }
  971. if (likely(rl != NULL)) {
  972. /* Seek to element containing target vcn. */
  973. while (rl->length && rl[1].vcn <= vcn)
  974. rl++;
  975. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  976. } else
  977. lcn = LCN_RL_NOT_MAPPED;
  978. /* Successful remap. */
  979. if (likely(lcn >= 0)) {
  980. /* Setup buffer head to correct block. */
  981. bh->b_blocknr = ((lcn <<
  982. vol->cluster_size_bits) +
  983. vcn_ofs) >> bh_size_bits;
  984. set_buffer_mapped(bh);
  985. } else {
  986. /*
  987. * Remap failed. Retry to map the runlist once
  988. * unless we are working on $MFT which always
  989. * has the whole of its runlist in memory.
  990. */
  991. if (!is_mft && !is_retry &&
  992. lcn == LCN_RL_NOT_MAPPED) {
  993. is_retry = TRUE;
  994. /*
  995. * Attempt to map runlist, dropping
  996. * lock for the duration.
  997. */
  998. up_read(&ni->runlist.lock);
  999. err2 = ntfs_map_runlist(ni, vcn);
  1000. if (likely(!err2))
  1001. goto lock_retry_remap;
  1002. if (err2 == -ENOMEM)
  1003. page_is_dirty = TRUE;
  1004. lcn = err2;
  1005. } else {
  1006. err2 = -EIO;
  1007. if (!rl)
  1008. up_read(&ni->runlist.lock);
  1009. }
  1010. /* Hard error. Abort writing this record. */
  1011. if (!err || err == -ENOMEM)
  1012. err = err2;
  1013. bh->b_blocknr = -1;
  1014. ntfs_error(vol->sb, "Cannot write ntfs record "
  1015. "0x%llx (inode 0x%lx, "
  1016. "attribute type 0x%x) because "
  1017. "its location on disk could "
  1018. "not be determined (error "
  1019. "code %lli).",
  1020. (long long)block <<
  1021. bh_size_bits >>
  1022. vol->mft_record_size_bits,
  1023. ni->mft_no, ni->type,
  1024. (long long)lcn);
  1025. /*
  1026. * If this is not the first buffer, remove the
  1027. * buffers in this record from the list of
  1028. * buffers to write and clear their dirty bit
  1029. * if not error -ENOMEM.
  1030. */
  1031. if (rec_start_bh != bh) {
  1032. while (bhs[--nr_bhs] != rec_start_bh)
  1033. ;
  1034. if (err2 != -ENOMEM) {
  1035. do {
  1036. clear_buffer_dirty(
  1037. rec_start_bh);
  1038. } while ((rec_start_bh =
  1039. rec_start_bh->
  1040. b_this_page) !=
  1041. bh);
  1042. }
  1043. }
  1044. continue;
  1045. }
  1046. }
  1047. BUG_ON(!buffer_uptodate(bh));
  1048. BUG_ON(nr_bhs >= max_bhs);
  1049. bhs[nr_bhs++] = bh;
  1050. } while (block++, (bh = bh->b_this_page) != head);
  1051. if (unlikely(rl))
  1052. up_read(&ni->runlist.lock);
  1053. /* If there were no dirty buffers, we are done. */
  1054. if (!nr_bhs)
  1055. goto done;
  1056. /* Map the page so we can access its contents. */
  1057. kaddr = kmap(page);
  1058. /* Clear the page uptodate flag whilst the mst fixups are applied. */
  1059. BUG_ON(!PageUptodate(page));
  1060. ClearPageUptodate(page);
  1061. for (i = 0; i < nr_bhs; i++) {
  1062. unsigned int ofs;
  1063. /* Skip buffers which are not at the beginning of records. */
  1064. if (i % bhs_per_rec)
  1065. continue;
  1066. tbh = bhs[i];
  1067. ofs = bh_offset(tbh);
  1068. if (is_mft) {
  1069. ntfs_inode *tni;
  1070. unsigned long mft_no;
  1071. /* Get the mft record number. */
  1072. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1073. >> rec_size_bits;
  1074. /* Check whether to write this mft record. */
  1075. tni = NULL;
  1076. if (!ntfs_may_write_mft_record(vol, mft_no,
  1077. (MFT_RECORD*)(kaddr + ofs), &tni)) {
  1078. /*
  1079. * The record should not be written. This
  1080. * means we need to redirty the page before
  1081. * returning.
  1082. */
  1083. page_is_dirty = TRUE;
  1084. /*
  1085. * Remove the buffers in this mft record from
  1086. * the list of buffers to write.
  1087. */
  1088. do {
  1089. bhs[i] = NULL;
  1090. } while (++i % bhs_per_rec);
  1091. continue;
  1092. }
  1093. /*
  1094. * The record should be written. If a locked ntfs
  1095. * inode was returned, add it to the array of locked
  1096. * ntfs inodes.
  1097. */
  1098. if (tni)
  1099. locked_nis[nr_locked_nis++] = tni;
  1100. }
  1101. /* Apply the mst protection fixups. */
  1102. err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
  1103. rec_size);
  1104. if (unlikely(err2)) {
  1105. if (!err || err == -ENOMEM)
  1106. err = -EIO;
  1107. ntfs_error(vol->sb, "Failed to apply mst fixups "
  1108. "(inode 0x%lx, attribute type 0x%x, "
  1109. "page index 0x%lx, page offset 0x%x)!"
  1110. " Unmount and run chkdsk.", vi->i_ino,
  1111. ni->type, page->index, ofs);
  1112. /*
  1113. * Mark all the buffers in this record clean as we do
  1114. * not want to write corrupt data to disk.
  1115. */
  1116. do {
  1117. clear_buffer_dirty(bhs[i]);
  1118. bhs[i] = NULL;
  1119. } while (++i % bhs_per_rec);
  1120. continue;
  1121. }
  1122. nr_recs++;
  1123. }
  1124. /* If no records are to be written out, we are done. */
  1125. if (!nr_recs)
  1126. goto unm_done;
  1127. flush_dcache_page(page);
  1128. /* Lock buffers and start synchronous write i/o on them. */
  1129. for (i = 0; i < nr_bhs; i++) {
  1130. tbh = bhs[i];
  1131. if (!tbh)
  1132. continue;
  1133. if (unlikely(test_set_buffer_locked(tbh)))
  1134. BUG();
  1135. /* The buffer dirty state is now irrelevant, just clean it. */
  1136. clear_buffer_dirty(tbh);
  1137. BUG_ON(!buffer_uptodate(tbh));
  1138. BUG_ON(!buffer_mapped(tbh));
  1139. get_bh(tbh);
  1140. tbh->b_end_io = end_buffer_write_sync;
  1141. submit_bh(WRITE, tbh);
  1142. }
  1143. /* Synchronize the mft mirror now if not @sync. */
  1144. if (is_mft && !sync)
  1145. goto do_mirror;
  1146. do_wait:
  1147. /* Wait on i/o completion of buffers. */
  1148. for (i = 0; i < nr_bhs; i++) {
  1149. tbh = bhs[i];
  1150. if (!tbh)
  1151. continue;
  1152. wait_on_buffer(tbh);
  1153. if (unlikely(!buffer_uptodate(tbh))) {
  1154. ntfs_error(vol->sb, "I/O error while writing ntfs "
  1155. "record buffer (inode 0x%lx, "
  1156. "attribute type 0x%x, page index "
  1157. "0x%lx, page offset 0x%lx)! Unmount "
  1158. "and run chkdsk.", vi->i_ino, ni->type,
  1159. page->index, bh_offset(tbh));
  1160. if (!err || err == -ENOMEM)
  1161. err = -EIO;
  1162. /*
  1163. * Set the buffer uptodate so the page and buffer
  1164. * states do not become out of sync.
  1165. */
  1166. set_buffer_uptodate(tbh);
  1167. }
  1168. }
  1169. /* If @sync, now synchronize the mft mirror. */
  1170. if (is_mft && sync) {
  1171. do_mirror:
  1172. for (i = 0; i < nr_bhs; i++) {
  1173. unsigned long mft_no;
  1174. unsigned int ofs;
  1175. /*
  1176. * Skip buffers which are not at the beginning of
  1177. * records.
  1178. */
  1179. if (i % bhs_per_rec)
  1180. continue;
  1181. tbh = bhs[i];
  1182. /* Skip removed buffers (and hence records). */
  1183. if (!tbh)
  1184. continue;
  1185. ofs = bh_offset(tbh);
  1186. /* Get the mft record number. */
  1187. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1188. >> rec_size_bits;
  1189. if (mft_no < vol->mftmirr_size)
  1190. ntfs_sync_mft_mirror(vol, mft_no,
  1191. (MFT_RECORD*)(kaddr + ofs),
  1192. sync);
  1193. }
  1194. if (!sync)
  1195. goto do_wait;
  1196. }
  1197. /* Remove the mst protection fixups again. */
  1198. for (i = 0; i < nr_bhs; i++) {
  1199. if (!(i % bhs_per_rec)) {
  1200. tbh = bhs[i];
  1201. if (!tbh)
  1202. continue;
  1203. post_write_mst_fixup((NTFS_RECORD*)(kaddr +
  1204. bh_offset(tbh)));
  1205. }
  1206. }
  1207. flush_dcache_page(page);
  1208. unm_done:
  1209. /* Unlock any locked inodes. */
  1210. while (nr_locked_nis-- > 0) {
  1211. ntfs_inode *tni, *base_tni;
  1212. tni = locked_nis[nr_locked_nis];
  1213. /* Get the base inode. */
  1214. down(&tni->extent_lock);
  1215. if (tni->nr_extents >= 0)
  1216. base_tni = tni;
  1217. else {
  1218. base_tni = tni->ext.base_ntfs_ino;
  1219. BUG_ON(!base_tni);
  1220. }
  1221. up(&tni->extent_lock);
  1222. ntfs_debug("Unlocking %s inode 0x%lx.",
  1223. tni == base_tni ? "base" : "extent",
  1224. tni->mft_no);
  1225. up(&tni->mrec_lock);
  1226. atomic_dec(&tni->count);
  1227. iput(VFS_I(base_tni));
  1228. }
  1229. SetPageUptodate(page);
  1230. kunmap(page);
  1231. done:
  1232. if (unlikely(err && err != -ENOMEM)) {
  1233. /*
  1234. * Set page error if there is only one ntfs record in the page.
  1235. * Otherwise we would loose per-record granularity.
  1236. */
  1237. if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
  1238. SetPageError(page);
  1239. NVolSetErrors(vol);
  1240. }
  1241. if (page_is_dirty) {
  1242. ntfs_debug("Page still contains one or more dirty ntfs "
  1243. "records. Redirtying the page starting at "
  1244. "record 0x%lx.", page->index <<
  1245. (PAGE_CACHE_SHIFT - rec_size_bits));
  1246. redirty_page_for_writepage(wbc, page);
  1247. unlock_page(page);
  1248. } else {
  1249. /*
  1250. * Keep the VM happy. This must be done otherwise the
  1251. * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
  1252. * the page is clean.
  1253. */
  1254. BUG_ON(PageWriteback(page));
  1255. set_page_writeback(page);
  1256. unlock_page(page);
  1257. end_page_writeback(page);
  1258. }
  1259. if (likely(!err))
  1260. ntfs_debug("Done.");
  1261. return err;
  1262. }
  1263. /**
  1264. * ntfs_writepage - write a @page to the backing store
  1265. * @page: page cache page to write out
  1266. * @wbc: writeback control structure
  1267. *
  1268. * This is called from the VM when it wants to have a dirty ntfs page cache
  1269. * page cleaned. The VM has already locked the page and marked it clean.
  1270. *
  1271. * For non-resident attributes, ntfs_writepage() writes the @page by calling
  1272. * the ntfs version of the generic block_write_full_page() function,
  1273. * ntfs_write_block(), which in turn if necessary creates and writes the
  1274. * buffers associated with the page asynchronously.
  1275. *
  1276. * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
  1277. * the data to the mft record (which at this stage is most likely in memory).
  1278. * The mft record is then marked dirty and written out asynchronously via the
  1279. * vfs inode dirty code path for the inode the mft record belongs to or via the
  1280. * vm page dirty code path for the page the mft record is in.
  1281. *
  1282. * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
  1283. *
  1284. * Return 0 on success and -errno on error.
  1285. */
  1286. static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
  1287. {
  1288. loff_t i_size;
  1289. struct inode *vi = page->mapping->host;
  1290. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1291. char *kaddr;
  1292. ntfs_attr_search_ctx *ctx = NULL;
  1293. MFT_RECORD *m = NULL;
  1294. u32 attr_len;
  1295. int err;
  1296. retry_writepage:
  1297. BUG_ON(!PageLocked(page));
  1298. i_size = i_size_read(vi);
  1299. /* Is the page fully outside i_size? (truncate in progress) */
  1300. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  1301. PAGE_CACHE_SHIFT)) {
  1302. /*
  1303. * The page may have dirty, unmapped buffers. Make them
  1304. * freeable here, so the page does not leak.
  1305. */
  1306. block_invalidatepage(page, 0);
  1307. unlock_page(page);
  1308. ntfs_debug("Write outside i_size - truncated?");
  1309. return 0;
  1310. }
  1311. /*
  1312. * Only $DATA attributes can be encrypted and only unnamed $DATA
  1313. * attributes can be compressed. Index root can have the flags set but
  1314. * this means to create compressed/encrypted files, not that the
  1315. * attribute is compressed/encrypted. Note we need to check for
  1316. * AT_INDEX_ALLOCATION since this is the type of both directory and
  1317. * index inodes.
  1318. */
  1319. if (ni->type != AT_INDEX_ALLOCATION) {
  1320. /* If file is encrypted, deny access, just like NT4. */
  1321. if (NInoEncrypted(ni)) {
  1322. unlock_page(page);
  1323. BUG_ON(ni->type != AT_DATA);
  1324. ntfs_debug("Denying write access to encrypted "
  1325. "file.");
  1326. return -EACCES;
  1327. }
  1328. /* Compressed data streams are handled in compress.c. */
  1329. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  1330. BUG_ON(ni->type != AT_DATA);
  1331. BUG_ON(ni->name_len);
  1332. // TODO: Implement and replace this with
  1333. // return ntfs_write_compressed_block(page);
  1334. unlock_page(page);
  1335. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1336. "not supported yet. Sorry.");
  1337. return -EOPNOTSUPP;
  1338. }
  1339. // TODO: Implement and remove this check.
  1340. if (NInoNonResident(ni) && NInoSparse(ni)) {
  1341. unlock_page(page);
  1342. ntfs_error(vi->i_sb, "Writing to sparse files is not "
  1343. "supported yet. Sorry.");
  1344. return -EOPNOTSUPP;
  1345. }
  1346. }
  1347. /* NInoNonResident() == NInoIndexAllocPresent() */
  1348. if (NInoNonResident(ni)) {
  1349. /* We have to zero every time due to mmap-at-end-of-file. */
  1350. if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
  1351. /* The page straddles i_size. */
  1352. unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
  1353. kaddr = kmap_atomic(page, KM_USER0);
  1354. memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
  1355. kunmap_atomic(kaddr, KM_USER0);
  1356. flush_dcache_page(page);
  1357. }
  1358. /* Handle mst protected attributes. */
  1359. if (NInoMstProtected(ni))
  1360. return ntfs_write_mst_block(page, wbc);
  1361. /* Normal, non-resident data stream. */
  1362. return ntfs_write_block(page, wbc);
  1363. }
  1364. /*
  1365. * Attribute is resident, implying it is not compressed, encrypted, or
  1366. * mst protected. This also means the attribute is smaller than an mft
  1367. * record and hence smaller than a page, so can simply return error on
  1368. * any pages with index above 0. Note the attribute can actually be
  1369. * marked compressed but if it is resident the actual data is not
  1370. * compressed so we are ok to ignore the compressed flag here.
  1371. */
  1372. BUG_ON(page_has_buffers(page));
  1373. BUG_ON(!PageUptodate(page));
  1374. if (unlikely(page->index > 0)) {
  1375. ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
  1376. "Aborting write.", page->index);
  1377. BUG_ON(PageWriteback(page));
  1378. set_page_writeback(page);
  1379. unlock_page(page);
  1380. end_page_writeback(page);
  1381. return -EIO;
  1382. }
  1383. if (!NInoAttr(ni))
  1384. base_ni = ni;
  1385. else
  1386. base_ni = ni->ext.base_ntfs_ino;
  1387. /* Map, pin, and lock the mft record. */
  1388. m = map_mft_record(base_ni);
  1389. if (IS_ERR(m)) {
  1390. err = PTR_ERR(m);
  1391. m = NULL;
  1392. ctx = NULL;
  1393. goto err_out;
  1394. }
  1395. /*
  1396. * If a parallel write made the attribute non-resident, drop the mft
  1397. * record and retry the writepage.
  1398. */
  1399. if (unlikely(NInoNonResident(ni))) {
  1400. unmap_mft_record(base_ni);
  1401. goto retry_writepage;
  1402. }
  1403. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1404. if (unlikely(!ctx)) {
  1405. err = -ENOMEM;
  1406. goto err_out;
  1407. }
  1408. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1409. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1410. if (unlikely(err))
  1411. goto err_out;
  1412. /*
  1413. * Keep the VM happy. This must be done otherwise the radix-tree tag
  1414. * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
  1415. */
  1416. BUG_ON(PageWriteback(page));
  1417. set_page_writeback(page);
  1418. unlock_page(page);
  1419. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  1420. i_size = i_size_read(vi);
  1421. if (unlikely(attr_len > i_size)) {
  1422. /* Race with shrinking truncate or a failed truncate. */
  1423. attr_len = i_size;
  1424. /*
  1425. * If the truncate failed, fix it up now. If a concurrent
  1426. * truncate, we do its job, so it does not have to do anything.
  1427. */
  1428. err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
  1429. attr_len);
  1430. /* Shrinking cannot fail. */
  1431. BUG_ON(err);
  1432. }
  1433. kaddr = kmap_atomic(page, KM_USER0);
  1434. /* Copy the data from the page to the mft record. */
  1435. memcpy((u8*)ctx->attr +
  1436. le16_to_cpu(ctx->attr->data.resident.value_offset),
  1437. kaddr, attr_len);
  1438. /* Zero out of bounds area in the page cache page. */
  1439. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1440. kunmap_atomic(kaddr, KM_USER0);
  1441. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1442. flush_dcache_page(page);
  1443. /* We are done with the page. */
  1444. end_page_writeback(page);
  1445. /* Finally, mark the mft record dirty, so it gets written back. */
  1446. mark_mft_record_dirty(ctx->ntfs_ino);
  1447. ntfs_attr_put_search_ctx(ctx);
  1448. unmap_mft_record(base_ni);
  1449. return 0;
  1450. err_out:
  1451. if (err == -ENOMEM) {
  1452. ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
  1453. "page so we try again later.");
  1454. /*
  1455. * Put the page back on mapping->dirty_pages, but leave its
  1456. * buffers' dirty state as-is.
  1457. */
  1458. redirty_page_for_writepage(wbc, page);
  1459. err = 0;
  1460. } else {
  1461. ntfs_error(vi->i_sb, "Resident attribute write failed with "
  1462. "error %i.", err);
  1463. SetPageError(page);
  1464. NVolSetErrors(ni->vol);
  1465. make_bad_inode(vi);
  1466. }
  1467. unlock_page(page);
  1468. if (ctx)
  1469. ntfs_attr_put_search_ctx(ctx);
  1470. if (m)
  1471. unmap_mft_record(base_ni);
  1472. return err;
  1473. }
  1474. /**
  1475. * ntfs_prepare_nonresident_write -
  1476. *
  1477. */
  1478. static int ntfs_prepare_nonresident_write(struct page *page,
  1479. unsigned from, unsigned to)
  1480. {
  1481. VCN vcn;
  1482. LCN lcn;
  1483. s64 initialized_size;
  1484. loff_t i_size;
  1485. sector_t block, ablock, iblock;
  1486. struct inode *vi;
  1487. ntfs_inode *ni;
  1488. ntfs_volume *vol;
  1489. runlist_element *rl;
  1490. struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
  1491. unsigned long flags;
  1492. unsigned int vcn_ofs, block_start, block_end, blocksize;
  1493. int err;
  1494. BOOL is_retry;
  1495. unsigned char blocksize_bits;
  1496. vi = page->mapping->host;
  1497. ni = NTFS_I(vi);
  1498. vol = ni->vol;
  1499. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  1500. "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
  1501. page->index, from, to);
  1502. BUG_ON(!NInoNonResident(ni));
  1503. blocksize_bits = vi->i_blkbits;
  1504. blocksize = 1 << blocksize_bits;
  1505. /*
  1506. * create_empty_buffers() will create uptodate/dirty buffers if the
  1507. * page is uptodate/dirty.
  1508. */
  1509. if (!page_has_buffers(page))
  1510. create_empty_buffers(page, blocksize, 0);
  1511. bh = head = page_buffers(page);
  1512. if (unlikely(!bh))
  1513. return -ENOMEM;
  1514. /* The first block in the page. */
  1515. block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  1516. read_lock_irqsave(&ni->size_lock, flags);
  1517. /*
  1518. * The first out of bounds block for the allocated size. No need to
  1519. * round up as allocated_size is in multiples of cluster size and the
  1520. * minimum cluster size is 512 bytes, which is equal to the smallest
  1521. * blocksize.
  1522. */
  1523. ablock = ni->allocated_size >> blocksize_bits;
  1524. i_size = i_size_read(vi);
  1525. initialized_size = ni->initialized_size;
  1526. read_unlock_irqrestore(&ni->size_lock, flags);
  1527. /* The last (fully or partially) initialized block. */
  1528. iblock = initialized_size >> blocksize_bits;
  1529. /* Loop through all the buffers in the page. */
  1530. block_start = 0;
  1531. rl = NULL;
  1532. err = 0;
  1533. do {
  1534. block_end = block_start + blocksize;
  1535. /*
  1536. * If buffer @bh is outside the write, just mark it uptodate
  1537. * if the page is uptodate and continue with the next buffer.
  1538. */
  1539. if (block_end <= from || block_start >= to) {
  1540. if (PageUptodate(page)) {
  1541. if (!buffer_uptodate(bh))
  1542. set_buffer_uptodate(bh);
  1543. }
  1544. continue;
  1545. }
  1546. /*
  1547. * @bh is at least partially being written to.
  1548. * Make sure it is not marked as new.
  1549. */
  1550. //if (buffer_new(bh))
  1551. // clear_buffer_new(bh);
  1552. if (block >= ablock) {
  1553. // TODO: block is above allocated_size, need to
  1554. // allocate it. Best done in one go to accommodate not
  1555. // only block but all above blocks up to and including:
  1556. // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
  1557. // - 1) >> blobksize_bits. Obviously will need to round
  1558. // up to next cluster boundary, too. This should be
  1559. // done with a helper function, so it can be reused.
  1560. ntfs_error(vol->sb, "Writing beyond allocated size "
  1561. "is not supported yet. Sorry.");
  1562. err = -EOPNOTSUPP;
  1563. goto err_out;
  1564. // Need to update ablock.
  1565. // Need to set_buffer_new() on all block bhs that are
  1566. // newly allocated.
  1567. }
  1568. /*
  1569. * Now we have enough allocated size to fulfill the whole
  1570. * request, i.e. block < ablock is true.
  1571. */
  1572. if (unlikely((block >= iblock) &&
  1573. (initialized_size < i_size))) {
  1574. /*
  1575. * If this page is fully outside initialized size, zero
  1576. * out all pages between the current initialized size
  1577. * and the current page. Just use ntfs_readpage() to do
  1578. * the zeroing transparently.
  1579. */
  1580. if (block > iblock) {
  1581. // TODO:
  1582. // For each page do:
  1583. // - read_cache_page()
  1584. // Again for each page do:
  1585. // - wait_on_page_locked()
  1586. // - Check (PageUptodate(page) &&
  1587. // !PageError(page))
  1588. // Update initialized size in the attribute and
  1589. // in the inode.
  1590. // Again, for each page do:
  1591. // __set_page_dirty_buffers();
  1592. // page_cache_release()
  1593. // We don't need to wait on the writes.
  1594. // Update iblock.
  1595. }
  1596. /*
  1597. * The current page straddles initialized size. Zero
  1598. * all non-uptodate buffers and set them uptodate (and
  1599. * dirty?). Note, there aren't any non-uptodate buffers
  1600. * if the page is uptodate.
  1601. * FIXME: For an uptodate page, the buffers may need to
  1602. * be written out because they were not initialized on
  1603. * disk before.
  1604. */
  1605. if (!PageUptodate(page)) {
  1606. // TODO:
  1607. // Zero any non-uptodate buffers up to i_size.
  1608. // Set them uptodate and dirty.
  1609. }
  1610. // TODO:
  1611. // Update initialized size in the attribute and in the
  1612. // inode (up to i_size).
  1613. // Update iblock.
  1614. // FIXME: This is inefficient. Try to batch the two
  1615. // size changes to happen in one go.
  1616. ntfs_error(vol->sb, "Writing beyond initialized size "
  1617. "is not supported yet. Sorry.");
  1618. err = -EOPNOTSUPP;
  1619. goto err_out;
  1620. // Do NOT set_buffer_new() BUT DO clear buffer range
  1621. // outside write request range.
  1622. // set_buffer_uptodate() on complete buffers as well as
  1623. // set_buffer_dirty().
  1624. }
  1625. /* Need to map unmapped buffers. */
  1626. if (!buffer_mapped(bh)) {
  1627. /* Unmapped buffer. Need to map it. */
  1628. bh->b_bdev = vol->sb->s_bdev;
  1629. /* Convert block into corresponding vcn and offset. */
  1630. vcn = (VCN)block << blocksize_bits >>
  1631. vol->cluster_size_bits;
  1632. vcn_ofs = ((VCN)block << blocksize_bits) &
  1633. vol->cluster_size_mask;
  1634. is_retry = FALSE;
  1635. if (!rl) {
  1636. lock_retry_remap:
  1637. down_read(&ni->runlist.lock);
  1638. rl = ni->runlist.rl;
  1639. }
  1640. if (likely(rl != NULL)) {
  1641. /* Seek to element containing target vcn. */
  1642. while (rl->length && rl[1].vcn <= vcn)
  1643. rl++;
  1644. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  1645. } else
  1646. lcn = LCN_RL_NOT_MAPPED;
  1647. if (unlikely(lcn < 0)) {
  1648. /*
  1649. * We extended the attribute allocation above.
  1650. * If we hit an ENOENT here it means that the
  1651. * allocation was insufficient which is a bug.
  1652. */
  1653. BUG_ON(lcn == LCN_ENOENT);
  1654. /* It is a hole, need to instantiate it. */
  1655. if (lcn == LCN_HOLE) {
  1656. // TODO: Instantiate the hole.
  1657. // clear_buffer_new(bh);
  1658. // unmap_underlying_metadata(bh->b_bdev,
  1659. // bh->b_blocknr);
  1660. // For non-uptodate buffers, need to
  1661. // zero out the region outside the
  1662. // request in this bh or all bhs,
  1663. // depending on what we implemented
  1664. // above.
  1665. // Need to flush_dcache_page().
  1666. // Or could use set_buffer_new()
  1667. // instead?
  1668. ntfs_error(vol->sb, "Writing into "
  1669. "sparse regions is "
  1670. "not supported yet. "
  1671. "Sorry.");
  1672. err = -EOPNOTSUPP;
  1673. if (!rl)
  1674. up_read(&ni->runlist.lock);
  1675. goto err_out;
  1676. } else if (!is_retry &&
  1677. lcn == LCN_RL_NOT_MAPPED) {
  1678. is_retry = TRUE;
  1679. /*
  1680. * Attempt to map runlist, dropping
  1681. * lock for the duration.
  1682. */
  1683. up_read(&ni->runlist.lock);
  1684. err = ntfs_map_runlist(ni, vcn);
  1685. if (likely(!err))
  1686. goto lock_retry_remap;
  1687. rl = NULL;
  1688. } else if (!rl)
  1689. up_read(&ni->runlist.lock);
  1690. /*
  1691. * Failed to map the buffer, even after
  1692. * retrying.
  1693. */
  1694. if (!err)
  1695. err = -EIO;
  1696. bh->b_blocknr = -1;
  1697. ntfs_error(vol->sb, "Failed to write to inode "
  1698. "0x%lx, attribute type 0x%x, "
  1699. "vcn 0x%llx, offset 0x%x "
  1700. "because its location on disk "
  1701. "could not be determined%s "
  1702. "(error code %i).",
  1703. ni->mft_no, ni->type,
  1704. (unsigned long long)vcn,
  1705. vcn_ofs, is_retry ? " even "
  1706. "after retrying" : "", err);
  1707. goto err_out;
  1708. }
  1709. /* We now have a successful remap, i.e. lcn >= 0. */
  1710. /* Setup buffer head to correct block. */
  1711. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  1712. + vcn_ofs) >> blocksize_bits;
  1713. set_buffer_mapped(bh);
  1714. // FIXME: Something analogous to this is needed for
  1715. // each newly allocated block, i.e. BH_New.
  1716. // FIXME: Might need to take this out of the
  1717. // if (!buffer_mapped(bh)) {}, depending on how we
  1718. // implement things during the allocated_size and
  1719. // initialized_size extension code above.
  1720. if (buffer_new(bh)) {
  1721. clear_buffer_new(bh);
  1722. unmap_underlying_metadata(bh->b_bdev,
  1723. bh->b_blocknr);
  1724. if (PageUptodate(page)) {
  1725. set_buffer_uptodate(bh);
  1726. continue;
  1727. }
  1728. /*
  1729. * Page is _not_ uptodate, zero surrounding
  1730. * region. NOTE: This is how we decide if to
  1731. * zero or not!
  1732. */
  1733. if (block_end > to || block_start < from) {
  1734. void *kaddr;
  1735. kaddr = kmap_atomic(page, KM_USER0);
  1736. if (block_end > to)
  1737. memset(kaddr + to, 0,
  1738. block_end - to);
  1739. if (block_start < from)
  1740. memset(kaddr + block_start, 0,
  1741. from -
  1742. block_start);
  1743. flush_dcache_page(page);
  1744. kunmap_atomic(kaddr, KM_USER0);
  1745. }
  1746. continue;
  1747. }
  1748. }
  1749. /* @bh is mapped, set it uptodate if the page is uptodate. */
  1750. if (PageUptodate(page)) {
  1751. if (!buffer_uptodate(bh))
  1752. set_buffer_uptodate(bh);
  1753. continue;
  1754. }
  1755. /*
  1756. * The page is not uptodate. The buffer is mapped. If it is not
  1757. * uptodate, and it is only partially being written to, we need
  1758. * to read the buffer in before the write, i.e. right now.
  1759. */
  1760. if (!buffer_uptodate(bh) &&
  1761. (block_start < from || block_end > to)) {
  1762. ll_rw_block(READ, 1, &bh);
  1763. *wait_bh++ = bh;
  1764. }
  1765. } while (block++, block_start = block_end,
  1766. (bh = bh->b_this_page) != head);
  1767. /* Release the lock if we took it. */
  1768. if (rl) {
  1769. up_read(&ni->runlist.lock);
  1770. rl = NULL;
  1771. }
  1772. /* If we issued read requests, let them complete. */
  1773. while (wait_bh > wait) {
  1774. wait_on_buffer(*--wait_bh);
  1775. if (!buffer_uptodate(*wait_bh))
  1776. return -EIO;
  1777. }
  1778. ntfs_debug("Done.");
  1779. return 0;
  1780. err_out:
  1781. /*
  1782. * Zero out any newly allocated blocks to avoid exposing stale data.
  1783. * If BH_New is set, we know that the block was newly allocated in the
  1784. * above loop.
  1785. * FIXME: What about initialized_size increments? Have we done all the
  1786. * required zeroing above? If not this error handling is broken, and
  1787. * in particular the if (block_end <= from) check is completely bogus.
  1788. */
  1789. bh = head;
  1790. block_start = 0;
  1791. is_retry = FALSE;
  1792. do {
  1793. block_end = block_start + blocksize;
  1794. if (block_end <= from)
  1795. continue;
  1796. if (block_start >= to)
  1797. break;
  1798. if (buffer_new(bh)) {
  1799. void *kaddr;
  1800. clear_buffer_new(bh);
  1801. kaddr = kmap_atomic(page, KM_USER0);
  1802. memset(kaddr + block_start, 0, bh->b_size);
  1803. kunmap_atomic(kaddr, KM_USER0);
  1804. set_buffer_uptodate(bh);
  1805. mark_buffer_dirty(bh);
  1806. is_retry = TRUE;
  1807. }
  1808. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  1809. if (is_retry)
  1810. flush_dcache_page(page);
  1811. if (rl)
  1812. up_read(&ni->runlist.lock);
  1813. return err;
  1814. }
  1815. /**
  1816. * ntfs_prepare_write - prepare a page for receiving data
  1817. *
  1818. * This is called from generic_file_write() with i_sem held on the inode
  1819. * (@page->mapping->host). The @page is locked but not kmap()ped. The source
  1820. * data has not yet been copied into the @page.
  1821. *
  1822. * Need to extend the attribute/fill in holes if necessary, create blocks and
  1823. * make partially overwritten blocks uptodate,
  1824. *
  1825. * i_size is not to be modified yet.
  1826. *
  1827. * Return 0 on success or -errno on error.
  1828. *
  1829. * Should be using block_prepare_write() [support for sparse files] or
  1830. * cont_prepare_write() [no support for sparse files]. Cannot do that due to
  1831. * ntfs specifics but can look at them for implementation guidance.
  1832. *
  1833. * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
  1834. * the first byte in the page that will be written to and @to is the first byte
  1835. * after the last byte that will be written to.
  1836. */
  1837. static int ntfs_prepare_write(struct file *file, struct page *page,
  1838. unsigned from, unsigned to)
  1839. {
  1840. s64 new_size;
  1841. loff_t i_size;
  1842. struct inode *vi = page->mapping->host;
  1843. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1844. ntfs_volume *vol = ni->vol;
  1845. ntfs_attr_search_ctx *ctx = NULL;
  1846. MFT_RECORD *m = NULL;
  1847. ATTR_RECORD *a;
  1848. u8 *kaddr;
  1849. u32 attr_len;
  1850. int err;
  1851. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  1852. "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
  1853. page->index, from, to);
  1854. BUG_ON(!PageLocked(page));
  1855. BUG_ON(from > PAGE_CACHE_SIZE);
  1856. BUG_ON(to > PAGE_CACHE_SIZE);
  1857. BUG_ON(from > to);
  1858. BUG_ON(NInoMstProtected(ni));
  1859. /*
  1860. * If a previous ntfs_truncate() failed, repeat it and abort if it
  1861. * fails again.
  1862. */
  1863. if (unlikely(NInoTruncateFailed(ni))) {
  1864. down_write(&vi->i_alloc_sem);
  1865. err = ntfs_truncate(vi);
  1866. up_write(&vi->i_alloc_sem);
  1867. if (err || NInoTruncateFailed(ni)) {
  1868. if (!err)
  1869. err = -EIO;
  1870. goto err_out;
  1871. }
  1872. }
  1873. /* If the attribute is not resident, deal with it elsewhere. */
  1874. if (NInoNonResident(ni)) {
  1875. /*
  1876. * Only unnamed $DATA attributes can be compressed, encrypted,
  1877. * and/or sparse.
  1878. */
  1879. if (ni->type == AT_DATA && !ni->name_len) {
  1880. /* If file is encrypted, deny access, just like NT4. */
  1881. if (NInoEncrypted(ni)) {
  1882. ntfs_debug("Denying write access to encrypted "
  1883. "file.");
  1884. return -EACCES;
  1885. }
  1886. /* Compressed data streams are handled in compress.c. */
  1887. if (NInoCompressed(ni)) {
  1888. // TODO: Implement and replace this check with
  1889. // return ntfs_write_compressed_block(page);
  1890. ntfs_error(vi->i_sb, "Writing to compressed "
  1891. "files is not supported yet. "
  1892. "Sorry.");
  1893. return -EOPNOTSUPP;
  1894. }
  1895. // TODO: Implement and remove this check.
  1896. if (NInoSparse(ni)) {
  1897. ntfs_error(vi->i_sb, "Writing to sparse files "
  1898. "is not supported yet. Sorry.");
  1899. return -EOPNOTSUPP;
  1900. }
  1901. }
  1902. /* Normal data stream. */
  1903. return ntfs_prepare_nonresident_write(page, from, to);
  1904. }
  1905. /*
  1906. * Attribute is resident, implying it is not compressed, encrypted, or
  1907. * sparse.
  1908. */
  1909. BUG_ON(page_has_buffers(page));
  1910. new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
  1911. /* If we do not need to resize the attribute allocation we are done. */
  1912. if (new_size <= i_size_read(vi))
  1913. goto done;
  1914. /* Map, pin, and lock the (base) mft record. */
  1915. if (!NInoAttr(ni))
  1916. base_ni = ni;
  1917. else
  1918. base_ni = ni->ext.base_ntfs_ino;
  1919. m = map_mft_record(base_ni);
  1920. if (IS_ERR(m)) {
  1921. err = PTR_ERR(m);
  1922. m = NULL;
  1923. ctx = NULL;
  1924. goto err_out;
  1925. }
  1926. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1927. if (unlikely(!ctx)) {
  1928. err = -ENOMEM;
  1929. goto err_out;
  1930. }
  1931. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1932. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1933. if (unlikely(err)) {
  1934. if (err == -ENOENT)
  1935. err = -EIO;
  1936. goto err_out;
  1937. }
  1938. m = ctx->mrec;
  1939. a = ctx->attr;
  1940. /* The total length of the attribute value. */
  1941. attr_len = le32_to_cpu(a->data.resident.value_length);
  1942. /* Fix an eventual previous failure of ntfs_commit_write(). */
  1943. i_size = i_size_read(vi);
  1944. if (unlikely(attr_len > i_size)) {
  1945. attr_len = i_size;
  1946. a->data.resident.value_length = cpu_to_le32(attr_len);
  1947. }
  1948. /* If we do not need to resize the attribute allocation we are done. */
  1949. if (new_size <= attr_len)
  1950. goto done_unm;
  1951. /* Check if new size is allowed in $AttrDef. */
  1952. err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
  1953. if (unlikely(err)) {
  1954. if (err == -ERANGE) {
  1955. ntfs_error(vol->sb, "Write would cause the inode "
  1956. "0x%lx to exceed the maximum size for "
  1957. "its attribute type (0x%x). Aborting "
  1958. "write.", vi->i_ino,
  1959. le32_to_cpu(ni->type));
  1960. } else {
  1961. ntfs_error(vol->sb, "Inode 0x%lx has unknown "
  1962. "attribute type 0x%x. Aborting "
  1963. "write.", vi->i_ino,
  1964. le32_to_cpu(ni->type));
  1965. err = -EIO;
  1966. }
  1967. goto err_out2;
  1968. }
  1969. /*
  1970. * Extend the attribute record to be able to store the new attribute
  1971. * size.
  1972. */
  1973. if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
  1974. le16_to_cpu(a->data.resident.value_offset) +
  1975. new_size)) {
  1976. /* Not enough space in the mft record. */
  1977. ntfs_error(vol->sb, "Not enough space in the mft record for "
  1978. "the resized attribute value. This is not "
  1979. "supported yet. Aborting write.");
  1980. err = -EOPNOTSUPP;
  1981. goto err_out2;
  1982. }
  1983. /*
  1984. * We have enough space in the mft record to fit the write. This
  1985. * implies the attribute is smaller than the mft record and hence the
  1986. * attribute must be in a single page and hence page->index must be 0.
  1987. */
  1988. BUG_ON(page->index);
  1989. /*
  1990. * If the beginning of the write is past the old size, enlarge the
  1991. * attribute value up to the beginning of the write and fill it with
  1992. * zeroes.
  1993. */
  1994. if (from > attr_len) {
  1995. memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
  1996. attr_len, 0, from - attr_len);
  1997. a->data.resident.value_length = cpu_to_le32(from);
  1998. /* Zero the corresponding area in the page as well. */
  1999. if (PageUptodate(page)) {
  2000. kaddr = kmap_atomic(page, KM_USER0);
  2001. memset(kaddr + attr_len, 0, from - attr_len);
  2002. kunmap_atomic(kaddr, KM_USER0);
  2003. flush_dcache_page(page);
  2004. }
  2005. }
  2006. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2007. mark_mft_record_dirty(ctx->ntfs_ino);
  2008. done_unm:
  2009. ntfs_attr_put_search_ctx(ctx);
  2010. unmap_mft_record(base_ni);
  2011. /*
  2012. * Because resident attributes are handled by memcpy() to/from the
  2013. * corresponding MFT record, and because this form of i/o is byte
  2014. * aligned rather than block aligned, there is no need to bring the
  2015. * page uptodate here as in the non-resident case where we need to
  2016. * bring the buffers straddled by the write uptodate before
  2017. * generic_file_write() does the copying from userspace.
  2018. *
  2019. * We thus defer the uptodate bringing of the page region outside the
  2020. * region written to to ntfs_commit_write(), which makes the code
  2021. * simpler and saves one atomic kmap which is good.
  2022. */
  2023. done:
  2024. ntfs_debug("Done.");
  2025. return 0;
  2026. err_out:
  2027. if (err == -ENOMEM)
  2028. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  2029. "prepare the write.");
  2030. else {
  2031. ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
  2032. "with error %i.", err);
  2033. NVolSetErrors(vol);
  2034. make_bad_inode(vi);
  2035. }
  2036. err_out2:
  2037. if (ctx)
  2038. ntfs_attr_put_search_ctx(ctx);
  2039. if (m)
  2040. unmap_mft_record(base_ni);
  2041. return err;
  2042. }
  2043. /**
  2044. * ntfs_commit_nonresident_write -
  2045. *
  2046. */
  2047. static int ntfs_commit_nonresident_write(struct page *page,
  2048. unsigned from, unsigned to)
  2049. {
  2050. s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
  2051. struct inode *vi = page->mapping->host;
  2052. struct buffer_head *bh, *head;
  2053. unsigned int block_start, block_end, blocksize;
  2054. BOOL partial;
  2055. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  2056. "0x%lx, from = %u, to = %u.", vi->i_ino,
  2057. NTFS_I(vi)->type, page->index, from, to);
  2058. blocksize = 1 << vi->i_blkbits;
  2059. // FIXME: We need a whole slew of special cases in here for compressed
  2060. // files for example...
  2061. // For now, we know ntfs_prepare_write() would have failed so we can't
  2062. // get here in any of the cases which we have to special case, so we
  2063. // are just a ripped off, unrolled generic_commit_write().
  2064. bh = head = page_buffers(page);
  2065. block_start = 0;
  2066. partial = FALSE;
  2067. do {
  2068. block_end = block_start + blocksize;
  2069. if (block_end <= from || block_start >= to) {
  2070. if (!buffer_uptodate(bh))
  2071. partial = TRUE;
  2072. } else {
  2073. set_buffer_uptodate(bh);
  2074. mark_buffer_dirty(bh);
  2075. }
  2076. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  2077. /*
  2078. * If this is a partial write which happened to make all buffers
  2079. * uptodate then we can optimize away a bogus ->readpage() for the next
  2080. * read(). Here we 'discover' whether the page went uptodate as a
  2081. * result of this (potentially partial) write.
  2082. */
  2083. if (!partial)
  2084. SetPageUptodate(page);
  2085. /*
  2086. * Not convinced about this at all. See disparity comment above. For
  2087. * now we know ntfs_prepare_write() would have failed in the write
  2088. * exceeds i_size case, so this will never trigger which is fine.
  2089. */
  2090. if (pos > i_size_read(vi)) {
  2091. ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
  2092. "not supported yet. Sorry.");
  2093. return -EOPNOTSUPP;
  2094. // vi->i_size = pos;
  2095. // mark_inode_dirty(vi);
  2096. }
  2097. ntfs_debug("Done.");
  2098. return 0;
  2099. }
  2100. /**
  2101. * ntfs_commit_write - commit the received data
  2102. *
  2103. * This is called from generic_file_write() with i_sem held on the inode
  2104. * (@page->mapping->host). The @page is locked but not kmap()ped. The source
  2105. * data has already been copied into the @page. ntfs_prepare_write() has been
  2106. * called before the data copied and it returned success so we can take the
  2107. * results of various BUG checks and some error handling for granted.
  2108. *
  2109. * Need to mark modified blocks dirty so they get written out later when
  2110. * ntfs_writepage() is invoked by the VM.
  2111. *
  2112. * Return 0 on success or -errno on error.
  2113. *
  2114. * Should be using generic_commit_write(). This marks buffers uptodate and
  2115. * dirty, sets the page uptodate if all buffers in the page are uptodate, and
  2116. * updates i_size if the end of io is beyond i_size. In that case, it also
  2117. * marks the inode dirty.
  2118. *
  2119. * Cannot use generic_commit_write() due to ntfs specialities but can look at
  2120. * it for implementation guidance.
  2121. *
  2122. * If things have gone as outlined in ntfs_prepare_write(), then we do not
  2123. * need to do any page content modifications here at all, except in the write
  2124. * to resident attribute case, where we need to do the uptodate bringing here
  2125. * which we combine with the copying into the mft record which means we save
  2126. * one atomic kmap.
  2127. */
  2128. static int ntfs_commit_write(struct file *file, struct page *page,
  2129. unsigned from, unsigned to)
  2130. {
  2131. struct inode *vi = page->mapping->host;
  2132. ntfs_inode *base_ni, *ni = NTFS_I(vi);
  2133. char *kaddr, *kattr;
  2134. ntfs_attr_search_ctx *ctx;
  2135. MFT_RECORD *m;
  2136. ATTR_RECORD *a;
  2137. u32 attr_len;
  2138. int err;
  2139. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  2140. "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
  2141. page->index, from, to);
  2142. /* If the attribute is not resident, deal with it elsewhere. */
  2143. if (NInoNonResident(ni)) {
  2144. /* Only unnamed $DATA attributes can be compressed/encrypted. */
  2145. if (ni->type == AT_DATA && !ni->name_len) {
  2146. /* Encrypted files need separate handling. */
  2147. if (NInoEncrypted(ni)) {
  2148. // We never get here at present!
  2149. BUG();
  2150. }
  2151. /* Compressed data streams are handled in compress.c. */
  2152. if (NInoCompressed(ni)) {
  2153. // TODO: Implement this!
  2154. // return ntfs_write_compressed_block(page);
  2155. // We never get here at present!
  2156. BUG();
  2157. }
  2158. }
  2159. /* Normal data stream. */
  2160. return ntfs_commit_nonresident_write(page, from, to);
  2161. }
  2162. /*
  2163. * Attribute is resident, implying it is not compressed, encrypted, or
  2164. * sparse.
  2165. */
  2166. if (!NInoAttr(ni))
  2167. base_ni = ni;
  2168. else
  2169. base_ni = ni->ext.base_ntfs_ino;
  2170. /* Map, pin, and lock the mft record. */
  2171. m = map_mft_record(base_ni);
  2172. if (IS_ERR(m)) {
  2173. err = PTR_ERR(m);
  2174. m = NULL;
  2175. ctx = NULL;
  2176. goto err_out;
  2177. }
  2178. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  2179. if (unlikely(!ctx)) {
  2180. err = -ENOMEM;
  2181. goto err_out;
  2182. }
  2183. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2184. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2185. if (unlikely(err)) {
  2186. if (err == -ENOENT)
  2187. err = -EIO;
  2188. goto err_out;
  2189. }
  2190. a = ctx->attr;
  2191. /* The total length of the attribute value. */
  2192. attr_len = le32_to_cpu(a->data.resident.value_length);
  2193. BUG_ON(from > attr_len);
  2194. kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
  2195. kaddr = kmap_atomic(page, KM_USER0);
  2196. /* Copy the received data from the page to the mft record. */
  2197. memcpy(kattr + from, kaddr + from, to - from);
  2198. /* Update the attribute length if necessary. */
  2199. if (to > attr_len) {
  2200. attr_len = to;
  2201. a->data.resident.value_length = cpu_to_le32(attr_len);
  2202. }
  2203. /*
  2204. * If the page is not uptodate, bring the out of bounds area(s)
  2205. * uptodate by copying data from the mft record to the page.
  2206. */
  2207. if (!PageUptodate(page)) {
  2208. if (from > 0)
  2209. memcpy(kaddr, kattr, from);
  2210. if (to < attr_len)
  2211. memcpy(kaddr + to, kattr + to, attr_len - to);
  2212. /* Zero the region outside the end of the attribute value. */
  2213. if (attr_len < PAGE_CACHE_SIZE)
  2214. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  2215. /*
  2216. * The probability of not having done any of the above is
  2217. * extremely small, so we just flush unconditionally.
  2218. */
  2219. flush_dcache_page(page);
  2220. SetPageUptodate(page);
  2221. }
  2222. kunmap_atomic(kaddr, KM_USER0);
  2223. /* Update i_size if necessary. */
  2224. if (i_size_read(vi) < attr_len) {
  2225. unsigned long flags;
  2226. write_lock_irqsave(&ni->size_lock, flags);
  2227. ni->allocated_size = ni->initialized_size = attr_len;
  2228. i_size_write(vi, attr_len);
  2229. write_unlock_irqrestore(&ni->size_lock, flags);
  2230. }
  2231. /* Mark the mft record dirty, so it gets written back. */
  2232. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2233. mark_mft_record_dirty(ctx->ntfs_ino);
  2234. ntfs_attr_put_search_ctx(ctx);
  2235. unmap_mft_record(base_ni);
  2236. ntfs_debug("Done.");
  2237. return 0;
  2238. err_out:
  2239. if (err == -ENOMEM) {
  2240. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  2241. "commit the write.");
  2242. if (PageUptodate(page)) {
  2243. ntfs_warning(vi->i_sb, "Page is uptodate, setting "
  2244. "dirty so the write will be retried "
  2245. "later on by the VM.");
  2246. /*
  2247. * Put the page on mapping->dirty_pages, but leave its
  2248. * buffers' dirty state as-is.
  2249. */
  2250. __set_page_dirty_nobuffers(page);
  2251. err = 0;
  2252. } else
  2253. ntfs_error(vi->i_sb, "Page is not uptodate. Written "
  2254. "data has been lost.");
  2255. } else {
  2256. ntfs_error(vi->i_sb, "Resident attribute commit write failed "
  2257. "with error %i.", err);
  2258. NVolSetErrors(ni->vol);
  2259. make_bad_inode(vi);
  2260. }
  2261. if (ctx)
  2262. ntfs_attr_put_search_ctx(ctx);
  2263. if (m)
  2264. unmap_mft_record(base_ni);
  2265. return err;
  2266. }
  2267. #endif /* NTFS_RW */
  2268. /**
  2269. * ntfs_aops - general address space operations for inodes and attributes
  2270. */
  2271. struct address_space_operations ntfs_aops = {
  2272. .readpage = ntfs_readpage, /* Fill page with data. */
  2273. .sync_page = block_sync_page, /* Currently, just unplugs the
  2274. disk request queue. */
  2275. #ifdef NTFS_RW
  2276. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  2277. .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
  2278. ready to receive data. */
  2279. .commit_write = ntfs_commit_write, /* Commit received data. */
  2280. #endif /* NTFS_RW */
  2281. };
  2282. /**
  2283. * ntfs_mst_aops - general address space operations for mst protecteed inodes
  2284. * and attributes
  2285. */
  2286. struct address_space_operations ntfs_mst_aops = {
  2287. .readpage = ntfs_readpage, /* Fill page with data. */
  2288. .sync_page = block_sync_page, /* Currently, just unplugs the
  2289. disk request queue. */
  2290. #ifdef NTFS_RW
  2291. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  2292. .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
  2293. without touching the buffers
  2294. belonging to the page. */
  2295. #endif /* NTFS_RW */
  2296. };
  2297. #ifdef NTFS_RW
  2298. /**
  2299. * mark_ntfs_record_dirty - mark an ntfs record dirty
  2300. * @page: page containing the ntfs record to mark dirty
  2301. * @ofs: byte offset within @page at which the ntfs record begins
  2302. *
  2303. * Set the buffers and the page in which the ntfs record is located dirty.
  2304. *
  2305. * The latter also marks the vfs inode the ntfs record belongs to dirty
  2306. * (I_DIRTY_PAGES only).
  2307. *
  2308. * If the page does not have buffers, we create them and set them uptodate.
  2309. * The page may not be locked which is why we need to handle the buffers under
  2310. * the mapping->private_lock. Once the buffers are marked dirty we no longer
  2311. * need the lock since try_to_free_buffers() does not free dirty buffers.
  2312. */
  2313. void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
  2314. struct address_space *mapping = page->mapping;
  2315. ntfs_inode *ni = NTFS_I(mapping->host);
  2316. struct buffer_head *bh, *head, *buffers_to_free = NULL;
  2317. unsigned int end, bh_size, bh_ofs;
  2318. BUG_ON(!PageUptodate(page));
  2319. end = ofs + ni->itype.index.block_size;
  2320. bh_size = 1 << VFS_I(ni)->i_blkbits;
  2321. spin_lock(&mapping->private_lock);
  2322. if (unlikely(!page_has_buffers(page))) {
  2323. spin_unlock(&mapping->private_lock);
  2324. bh = head = alloc_page_buffers(page, bh_size, 1);
  2325. spin_lock(&mapping->private_lock);
  2326. if (likely(!page_has_buffers(page))) {
  2327. struct buffer_head *tail;
  2328. do {
  2329. set_buffer_uptodate(bh);
  2330. tail = bh;
  2331. bh = bh->b_this_page;
  2332. } while (bh);
  2333. tail->b_this_page = head;
  2334. attach_page_buffers(page, head);
  2335. } else
  2336. buffers_to_free = bh;
  2337. }
  2338. bh = head = page_buffers(page);
  2339. BUG_ON(!bh);
  2340. do {
  2341. bh_ofs = bh_offset(bh);
  2342. if (bh_ofs + bh_size <= ofs)
  2343. continue;
  2344. if (unlikely(bh_ofs >= end))
  2345. break;
  2346. set_buffer_dirty(bh);
  2347. } while ((bh = bh->b_this_page) != head);
  2348. spin_unlock(&mapping->private_lock);
  2349. __set_page_dirty_nobuffers(page);
  2350. if (unlikely(buffers_to_free)) {
  2351. do {
  2352. bh = buffers_to_free->b_this_page;
  2353. free_buffer_head(buffers_to_free);
  2354. buffers_to_free = bh;
  2355. } while (buffers_to_free);
  2356. }
  2357. }
  2358. #endif /* NTFS_RW */