aops.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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 file.");
  1325. return -EACCES;
  1326. }
  1327. /* Compressed data streams are handled in compress.c. */
  1328. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  1329. BUG_ON(ni->type != AT_DATA);
  1330. BUG_ON(ni->name_len);
  1331. // TODO: Implement and replace this with
  1332. // return ntfs_write_compressed_block(page);
  1333. unlock_page(page);
  1334. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1335. "not supported yet. Sorry.");
  1336. return -EOPNOTSUPP;
  1337. }
  1338. // TODO: Implement and remove this check.
  1339. if (NInoNonResident(ni) && NInoSparse(ni)) {
  1340. unlock_page(page);
  1341. ntfs_error(vi->i_sb, "Writing to sparse files is not "
  1342. "supported yet. Sorry.");
  1343. return -EOPNOTSUPP;
  1344. }
  1345. }
  1346. /* NInoNonResident() == NInoIndexAllocPresent() */
  1347. if (NInoNonResident(ni)) {
  1348. /* We have to zero every time due to mmap-at-end-of-file. */
  1349. if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
  1350. /* The page straddles i_size. */
  1351. unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
  1352. kaddr = kmap_atomic(page, KM_USER0);
  1353. memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
  1354. kunmap_atomic(kaddr, KM_USER0);
  1355. flush_dcache_page(page);
  1356. }
  1357. /* Handle mst protected attributes. */
  1358. if (NInoMstProtected(ni))
  1359. return ntfs_write_mst_block(page, wbc);
  1360. /* Normal, non-resident data stream. */
  1361. return ntfs_write_block(page, wbc);
  1362. }
  1363. /*
  1364. * Attribute is resident, implying it is not compressed, encrypted, or
  1365. * mst protected. This also means the attribute is smaller than an mft
  1366. * record and hence smaller than a page, so can simply return error on
  1367. * any pages with index above 0. Note the attribute can actually be
  1368. * marked compressed but if it is resident the actual data is not
  1369. * compressed so we are ok to ignore the compressed flag here.
  1370. */
  1371. BUG_ON(page_has_buffers(page));
  1372. BUG_ON(!PageUptodate(page));
  1373. if (unlikely(page->index > 0)) {
  1374. ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
  1375. "Aborting write.", page->index);
  1376. BUG_ON(PageWriteback(page));
  1377. set_page_writeback(page);
  1378. unlock_page(page);
  1379. end_page_writeback(page);
  1380. return -EIO;
  1381. }
  1382. if (!NInoAttr(ni))
  1383. base_ni = ni;
  1384. else
  1385. base_ni = ni->ext.base_ntfs_ino;
  1386. /* Map, pin, and lock the mft record. */
  1387. m = map_mft_record(base_ni);
  1388. if (IS_ERR(m)) {
  1389. err = PTR_ERR(m);
  1390. m = NULL;
  1391. ctx = NULL;
  1392. goto err_out;
  1393. }
  1394. /*
  1395. * If a parallel write made the attribute non-resident, drop the mft
  1396. * record and retry the writepage.
  1397. */
  1398. if (unlikely(NInoNonResident(ni))) {
  1399. unmap_mft_record(base_ni);
  1400. goto retry_writepage;
  1401. }
  1402. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1403. if (unlikely(!ctx)) {
  1404. err = -ENOMEM;
  1405. goto err_out;
  1406. }
  1407. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1408. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1409. if (unlikely(err))
  1410. goto err_out;
  1411. /*
  1412. * Keep the VM happy. This must be done otherwise the radix-tree tag
  1413. * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
  1414. */
  1415. BUG_ON(PageWriteback(page));
  1416. set_page_writeback(page);
  1417. unlock_page(page);
  1418. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  1419. i_size = i_size_read(vi);
  1420. if (unlikely(attr_len > i_size)) {
  1421. /* Race with shrinking truncate or a failed truncate. */
  1422. attr_len = i_size;
  1423. /*
  1424. * If the truncate failed, fix it up now. If a concurrent
  1425. * truncate, we do its job, so it does not have to do anything.
  1426. */
  1427. err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
  1428. attr_len);
  1429. /* Shrinking cannot fail. */
  1430. BUG_ON(err);
  1431. }
  1432. kaddr = kmap_atomic(page, KM_USER0);
  1433. /* Copy the data from the page to the mft record. */
  1434. memcpy((u8*)ctx->attr +
  1435. le16_to_cpu(ctx->attr->data.resident.value_offset),
  1436. kaddr, attr_len);
  1437. /* Zero out of bounds area in the page cache page. */
  1438. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1439. kunmap_atomic(kaddr, KM_USER0);
  1440. flush_dcache_page(page);
  1441. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1442. /* We are done with the page. */
  1443. end_page_writeback(page);
  1444. /* Finally, mark the mft record dirty, so it gets written back. */
  1445. mark_mft_record_dirty(ctx->ntfs_ino);
  1446. ntfs_attr_put_search_ctx(ctx);
  1447. unmap_mft_record(base_ni);
  1448. return 0;
  1449. err_out:
  1450. if (err == -ENOMEM) {
  1451. ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
  1452. "page so we try again later.");
  1453. /*
  1454. * Put the page back on mapping->dirty_pages, but leave its
  1455. * buffers' dirty state as-is.
  1456. */
  1457. redirty_page_for_writepage(wbc, page);
  1458. err = 0;
  1459. } else {
  1460. ntfs_error(vi->i_sb, "Resident attribute write failed with "
  1461. "error %i.", err);
  1462. SetPageError(page);
  1463. NVolSetErrors(ni->vol);
  1464. make_bad_inode(vi);
  1465. }
  1466. unlock_page(page);
  1467. if (ctx)
  1468. ntfs_attr_put_search_ctx(ctx);
  1469. if (m)
  1470. unmap_mft_record(base_ni);
  1471. return err;
  1472. }
  1473. #endif /* NTFS_RW */
  1474. /**
  1475. * ntfs_aops - general address space operations for inodes and attributes
  1476. */
  1477. struct address_space_operations ntfs_aops = {
  1478. .readpage = ntfs_readpage, /* Fill page with data. */
  1479. .sync_page = block_sync_page, /* Currently, just unplugs the
  1480. disk request queue. */
  1481. #ifdef NTFS_RW
  1482. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  1483. #endif /* NTFS_RW */
  1484. };
  1485. /**
  1486. * ntfs_mst_aops - general address space operations for mst protecteed inodes
  1487. * and attributes
  1488. */
  1489. struct address_space_operations ntfs_mst_aops = {
  1490. .readpage = ntfs_readpage, /* Fill page with data. */
  1491. .sync_page = block_sync_page, /* Currently, just unplugs the
  1492. disk request queue. */
  1493. #ifdef NTFS_RW
  1494. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  1495. .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
  1496. without touching the buffers
  1497. belonging to the page. */
  1498. #endif /* NTFS_RW */
  1499. };
  1500. #ifdef NTFS_RW
  1501. /**
  1502. * mark_ntfs_record_dirty - mark an ntfs record dirty
  1503. * @page: page containing the ntfs record to mark dirty
  1504. * @ofs: byte offset within @page at which the ntfs record begins
  1505. *
  1506. * Set the buffers and the page in which the ntfs record is located dirty.
  1507. *
  1508. * The latter also marks the vfs inode the ntfs record belongs to dirty
  1509. * (I_DIRTY_PAGES only).
  1510. *
  1511. * If the page does not have buffers, we create them and set them uptodate.
  1512. * The page may not be locked which is why we need to handle the buffers under
  1513. * the mapping->private_lock. Once the buffers are marked dirty we no longer
  1514. * need the lock since try_to_free_buffers() does not free dirty buffers.
  1515. */
  1516. void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
  1517. struct address_space *mapping = page->mapping;
  1518. ntfs_inode *ni = NTFS_I(mapping->host);
  1519. struct buffer_head *bh, *head, *buffers_to_free = NULL;
  1520. unsigned int end, bh_size, bh_ofs;
  1521. BUG_ON(!PageUptodate(page));
  1522. end = ofs + ni->itype.index.block_size;
  1523. bh_size = 1 << VFS_I(ni)->i_blkbits;
  1524. spin_lock(&mapping->private_lock);
  1525. if (unlikely(!page_has_buffers(page))) {
  1526. spin_unlock(&mapping->private_lock);
  1527. bh = head = alloc_page_buffers(page, bh_size, 1);
  1528. spin_lock(&mapping->private_lock);
  1529. if (likely(!page_has_buffers(page))) {
  1530. struct buffer_head *tail;
  1531. do {
  1532. set_buffer_uptodate(bh);
  1533. tail = bh;
  1534. bh = bh->b_this_page;
  1535. } while (bh);
  1536. tail->b_this_page = head;
  1537. attach_page_buffers(page, head);
  1538. } else
  1539. buffers_to_free = bh;
  1540. }
  1541. bh = head = page_buffers(page);
  1542. BUG_ON(!bh);
  1543. do {
  1544. bh_ofs = bh_offset(bh);
  1545. if (bh_ofs + bh_size <= ofs)
  1546. continue;
  1547. if (unlikely(bh_ofs >= end))
  1548. break;
  1549. set_buffer_dirty(bh);
  1550. } while ((bh = bh->b_this_page) != head);
  1551. spin_unlock(&mapping->private_lock);
  1552. __set_page_dirty_nobuffers(page);
  1553. if (unlikely(buffers_to_free)) {
  1554. do {
  1555. bh = buffers_to_free->b_this_page;
  1556. free_buffer_head(buffers_to_free);
  1557. buffers_to_free = bh;
  1558. } while (buffers_to_free);
  1559. }
  1560. }
  1561. #endif /* NTFS_RW */