aops.c 70 KB

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