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-2007 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/fs.h>
  25. #include <linux/gfp.h>
  26. #include <linux/mm.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/swap.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/writeback.h>
  31. #include <linux/bit_spinlock.h>
  32. #include "aops.h"
  33. #include "attrib.h"
  34. #include "debug.h"
  35. #include "inode.h"
  36. #include "mft.h"
  37. #include "runlist.h"
  38. #include "types.h"
  39. #include "ntfs.h"
  40. /**
  41. * ntfs_end_buffer_async_read - async io completion for reading attributes
  42. * @bh: buffer head on which io is completed
  43. * @uptodate: whether @bh is now uptodate or not
  44. *
  45. * Asynchronous I/O completion handler for reading pages belonging to the
  46. * attribute address space of an inode. The inodes can either be files or
  47. * directories or they can be fake inodes describing some attribute.
  48. *
  49. * If NInoMstProtected(), perform the post read mst fixups when all IO on the
  50. * page has been completed and mark the page uptodate or set the error bit on
  51. * the page. To determine the size of the records that need fixing up, we
  52. * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
  53. * record size, and index_block_size_bits, to the log(base 2) of the ntfs
  54. * record size.
  55. */
  56. static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  57. {
  58. unsigned long flags;
  59. struct buffer_head *first, *tmp;
  60. struct page *page;
  61. struct inode *vi;
  62. ntfs_inode *ni;
  63. int page_uptodate = 1;
  64. page = bh->b_page;
  65. vi = page->mapping->host;
  66. ni = NTFS_I(vi);
  67. if (likely(uptodate)) {
  68. loff_t i_size;
  69. s64 file_ofs, init_size;
  70. set_buffer_uptodate(bh);
  71. file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
  72. bh_offset(bh);
  73. read_lock_irqsave(&ni->size_lock, flags);
  74. init_size = ni->initialized_size;
  75. i_size = i_size_read(vi);
  76. read_unlock_irqrestore(&ni->size_lock, flags);
  77. if (unlikely(init_size > i_size)) {
  78. /* Race with shrinking truncate. */
  79. init_size = i_size;
  80. }
  81. /* Check for the current buffer head overflowing. */
  82. if (unlikely(file_ofs + bh->b_size > init_size)) {
  83. int ofs;
  84. void *kaddr;
  85. ofs = 0;
  86. if (file_ofs < init_size)
  87. ofs = init_size - file_ofs;
  88. local_irq_save(flags);
  89. kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  90. memset(kaddr + bh_offset(bh) + ofs, 0,
  91. bh->b_size - ofs);
  92. flush_dcache_page(page);
  93. kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
  94. local_irq_restore(flags);
  95. }
  96. } else {
  97. clear_buffer_uptodate(bh);
  98. SetPageError(page);
  99. ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
  100. "0x%llx.", (unsigned long long)bh->b_blocknr);
  101. }
  102. first = page_buffers(page);
  103. local_irq_save(flags);
  104. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  105. clear_buffer_async_read(bh);
  106. unlock_buffer(bh);
  107. tmp = bh;
  108. do {
  109. if (!buffer_uptodate(tmp))
  110. page_uptodate = 0;
  111. if (buffer_async_read(tmp)) {
  112. if (likely(buffer_locked(tmp)))
  113. goto still_busy;
  114. /* Async buffers must be locked. */
  115. BUG();
  116. }
  117. tmp = tmp->b_this_page;
  118. } while (tmp != bh);
  119. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  120. local_irq_restore(flags);
  121. /*
  122. * If none of the buffers had errors then we can set the page uptodate,
  123. * but we first have to perform the post read mst fixups, if the
  124. * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
  125. * Note we ignore fixup errors as those are detected when
  126. * map_mft_record() is called which gives us per record granularity
  127. * rather than per page granularity.
  128. */
  129. if (!NInoMstProtected(ni)) {
  130. if (likely(page_uptodate && !PageError(page)))
  131. SetPageUptodate(page);
  132. } else {
  133. u8 *kaddr;
  134. unsigned int i, recs;
  135. u32 rec_size;
  136. rec_size = ni->itype.index.block_size;
  137. recs = PAGE_CACHE_SIZE / rec_size;
  138. /* Should have been verified before we got here... */
  139. BUG_ON(!recs);
  140. local_irq_save(flags);
  141. kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  142. for (i = 0; i < recs; i++)
  143. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  144. i * rec_size), rec_size);
  145. kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
  146. local_irq_restore(flags);
  147. flush_dcache_page(page);
  148. if (likely(page_uptodate && !PageError(page)))
  149. SetPageUptodate(page);
  150. }
  151. unlock_page(page);
  152. return;
  153. still_busy:
  154. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  155. local_irq_restore(flags);
  156. return;
  157. }
  158. /**
  159. * ntfs_read_block - fill a @page of an address space with data
  160. * @page: page cache page to fill with data
  161. *
  162. * Fill the page @page of the address space belonging to the @page->host inode.
  163. * We read each buffer asynchronously and when all buffers are read in, our io
  164. * completion handler ntfs_end_buffer_read_async(), if required, automatically
  165. * applies the mst fixups to the page before finally marking it uptodate and
  166. * unlocking it.
  167. *
  168. * We only enforce allocated_size limit because i_size is checked for in
  169. * generic_file_read().
  170. *
  171. * Return 0 on success and -errno on error.
  172. *
  173. * Contains an adapted version of fs/buffer.c::block_read_full_page().
  174. */
  175. static int ntfs_read_block(struct page *page)
  176. {
  177. loff_t i_size;
  178. VCN vcn;
  179. LCN lcn;
  180. s64 init_size;
  181. struct inode *vi;
  182. ntfs_inode *ni;
  183. ntfs_volume *vol;
  184. runlist_element *rl;
  185. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  186. sector_t iblock, lblock, zblock;
  187. unsigned long flags;
  188. unsigned int blocksize, vcn_ofs;
  189. int i, nr;
  190. unsigned char blocksize_bits;
  191. vi = page->mapping->host;
  192. ni = NTFS_I(vi);
  193. vol = ni->vol;
  194. /* $MFT/$DATA must have its complete runlist in memory at all times. */
  195. BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
  196. blocksize = vol->sb->s_blocksize;
  197. blocksize_bits = vol->sb->s_blocksize_bits;
  198. if (!page_has_buffers(page)) {
  199. create_empty_buffers(page, blocksize, 0);
  200. if (unlikely(!page_has_buffers(page))) {
  201. unlock_page(page);
  202. return -ENOMEM;
  203. }
  204. }
  205. bh = head = page_buffers(page);
  206. BUG_ON(!bh);
  207. /*
  208. * We may be racing with truncate. To avoid some of the problems we
  209. * now take a snapshot of the various sizes and use those for the whole
  210. * of the function. In case of an extending truncate it just means we
  211. * may leave some buffers unmapped which are now allocated. This is
  212. * not a problem since these buffers will just get mapped when a write
  213. * occurs. In case of a shrinking truncate, we will detect this later
  214. * on due to the runlist being incomplete and if the page is being
  215. * fully truncated, truncate will throw it away as soon as we unlock
  216. * it so no need to worry what we do with it.
  217. */
  218. iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  219. read_lock_irqsave(&ni->size_lock, flags);
  220. lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
  221. init_size = ni->initialized_size;
  222. i_size = i_size_read(vi);
  223. read_unlock_irqrestore(&ni->size_lock, flags);
  224. if (unlikely(init_size > i_size)) {
  225. /* Race with shrinking truncate. */
  226. init_size = i_size;
  227. }
  228. zblock = (init_size + blocksize - 1) >> blocksize_bits;
  229. /* Loop through all the buffers in the page. */
  230. rl = NULL;
  231. nr = i = 0;
  232. do {
  233. int err = 0;
  234. if (unlikely(buffer_uptodate(bh)))
  235. continue;
  236. if (unlikely(buffer_mapped(bh))) {
  237. arr[nr++] = bh;
  238. continue;
  239. }
  240. bh->b_bdev = vol->sb->s_bdev;
  241. /* Is the block within the allowed limits? */
  242. if (iblock < lblock) {
  243. bool is_retry = false;
  244. /* Convert iblock into corresponding vcn and offset. */
  245. vcn = (VCN)iblock << blocksize_bits >>
  246. vol->cluster_size_bits;
  247. vcn_ofs = ((VCN)iblock << blocksize_bits) &
  248. vol->cluster_size_mask;
  249. if (!rl) {
  250. lock_retry_remap:
  251. down_read(&ni->runlist.lock);
  252. rl = ni->runlist.rl;
  253. }
  254. if (likely(rl != NULL)) {
  255. /* Seek to element containing target vcn. */
  256. while (rl->length && rl[1].vcn <= vcn)
  257. rl++;
  258. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  259. } else
  260. lcn = LCN_RL_NOT_MAPPED;
  261. /* Successful remap. */
  262. if (lcn >= 0) {
  263. /* Setup buffer head to correct block. */
  264. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  265. + vcn_ofs) >> blocksize_bits;
  266. set_buffer_mapped(bh);
  267. /* Only read initialized data blocks. */
  268. if (iblock < zblock) {
  269. arr[nr++] = bh;
  270. continue;
  271. }
  272. /* Fully non-initialized data block, zero it. */
  273. goto handle_zblock;
  274. }
  275. /* It is a hole, need to zero it. */
  276. if (lcn == LCN_HOLE)
  277. goto handle_hole;
  278. /* If first try and runlist unmapped, map and retry. */
  279. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  280. is_retry = true;
  281. /*
  282. * Attempt to map runlist, dropping lock for
  283. * the duration.
  284. */
  285. up_read(&ni->runlist.lock);
  286. err = ntfs_map_runlist(ni, vcn);
  287. if (likely(!err))
  288. goto lock_retry_remap;
  289. rl = NULL;
  290. } else if (!rl)
  291. up_read(&ni->runlist.lock);
  292. /*
  293. * If buffer is outside the runlist, treat it as a
  294. * hole. This can happen due to concurrent truncate
  295. * for example.
  296. */
  297. if (err == -ENOENT || lcn == LCN_ENOENT) {
  298. err = 0;
  299. goto handle_hole;
  300. }
  301. /* Hard error, zero out region. */
  302. if (!err)
  303. err = -EIO;
  304. bh->b_blocknr = -1;
  305. SetPageError(page);
  306. ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
  307. "attribute type 0x%x, vcn 0x%llx, "
  308. "offset 0x%x because its location on "
  309. "disk could not be determined%s "
  310. "(error code %i).", ni->mft_no,
  311. ni->type, (unsigned long long)vcn,
  312. vcn_ofs, is_retry ? " even after "
  313. "retrying" : "", err);
  314. }
  315. /*
  316. * Either iblock was outside lblock limits or
  317. * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
  318. * of the page and set the buffer uptodate.
  319. */
  320. handle_hole:
  321. bh->b_blocknr = -1UL;
  322. clear_buffer_mapped(bh);
  323. handle_zblock:
  324. zero_user(page, i * blocksize, blocksize);
  325. if (likely(!err))
  326. set_buffer_uptodate(bh);
  327. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  328. /* Release the lock if we took it. */
  329. if (rl)
  330. up_read(&ni->runlist.lock);
  331. /* Check we have at least one buffer ready for i/o. */
  332. if (nr) {
  333. struct buffer_head *tbh;
  334. /* Lock the buffers. */
  335. for (i = 0; i < nr; i++) {
  336. tbh = arr[i];
  337. lock_buffer(tbh);
  338. tbh->b_end_io = ntfs_end_buffer_async_read;
  339. set_buffer_async_read(tbh);
  340. }
  341. /* Finally, start i/o on the buffers. */
  342. for (i = 0; i < nr; i++) {
  343. tbh = arr[i];
  344. if (likely(!buffer_uptodate(tbh)))
  345. submit_bh(READ, tbh);
  346. else
  347. ntfs_end_buffer_async_read(tbh, 1);
  348. }
  349. return 0;
  350. }
  351. /* No i/o was scheduled on any of the buffers. */
  352. if (likely(!PageError(page)))
  353. SetPageUptodate(page);
  354. else /* Signal synchronous i/o error. */
  355. nr = -EIO;
  356. unlock_page(page);
  357. return nr;
  358. }
  359. /**
  360. * ntfs_readpage - fill a @page of a @file with data from the device
  361. * @file: open file to which the page @page belongs or NULL
  362. * @page: page cache page to fill with data
  363. *
  364. * For non-resident attributes, ntfs_readpage() fills the @page of the open
  365. * file @file by calling the ntfs version of the generic block_read_full_page()
  366. * function, ntfs_read_block(), which in turn creates and reads in the buffers
  367. * associated with the page asynchronously.
  368. *
  369. * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
  370. * data from the mft record (which at this stage is most likely in memory) and
  371. * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
  372. * even if the mft record is not cached at this point in time, we need to wait
  373. * for it to be read in before we can do the copy.
  374. *
  375. * Return 0 on success and -errno on error.
  376. */
  377. static int ntfs_readpage(struct file *file, struct page *page)
  378. {
  379. loff_t i_size;
  380. struct inode *vi;
  381. ntfs_inode *ni, *base_ni;
  382. u8 *addr;
  383. ntfs_attr_search_ctx *ctx;
  384. MFT_RECORD *mrec;
  385. unsigned long flags;
  386. u32 attr_len;
  387. int err = 0;
  388. retry_readpage:
  389. BUG_ON(!PageLocked(page));
  390. vi = page->mapping->host;
  391. i_size = i_size_read(vi);
  392. /* Is the page fully outside i_size? (truncate in progress) */
  393. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  394. PAGE_CACHE_SHIFT)) {
  395. zero_user(page, 0, PAGE_CACHE_SIZE);
  396. ntfs_debug("Read outside i_size - truncated?");
  397. goto done;
  398. }
  399. /*
  400. * This can potentially happen because we clear PageUptodate() during
  401. * ntfs_writepage() of MstProtected() attributes.
  402. */
  403. if (PageUptodate(page)) {
  404. unlock_page(page);
  405. return 0;
  406. }
  407. ni = NTFS_I(vi);
  408. /*
  409. * Only $DATA attributes can be encrypted and only unnamed $DATA
  410. * attributes can be compressed. Index root can have the flags set but
  411. * this means to create compressed/encrypted files, not that the
  412. * attribute is compressed/encrypted. Note we need to check for
  413. * AT_INDEX_ALLOCATION since this is the type of both directory and
  414. * index inodes.
  415. */
  416. if (ni->type != AT_INDEX_ALLOCATION) {
  417. /* If attribute is encrypted, deny access, just like NT4. */
  418. if (NInoEncrypted(ni)) {
  419. BUG_ON(ni->type != AT_DATA);
  420. err = -EACCES;
  421. goto err_out;
  422. }
  423. /* Compressed data streams are handled in compress.c. */
  424. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  425. BUG_ON(ni->type != AT_DATA);
  426. BUG_ON(ni->name_len);
  427. return ntfs_read_compressed_block(page);
  428. }
  429. }
  430. /* NInoNonResident() == NInoIndexAllocPresent() */
  431. if (NInoNonResident(ni)) {
  432. /* Normal, non-resident data stream. */
  433. return ntfs_read_block(page);
  434. }
  435. /*
  436. * Attribute is resident, implying it is not compressed or encrypted.
  437. * This also means the attribute is smaller than an mft record and
  438. * hence smaller than a page, so can simply zero out any pages with
  439. * index above 0. Note the attribute can actually be marked compressed
  440. * but if it is resident the actual data is not compressed so we are
  441. * ok to ignore the compressed flag here.
  442. */
  443. if (unlikely(page->index > 0)) {
  444. zero_user(page, 0, PAGE_CACHE_SIZE);
  445. goto done;
  446. }
  447. if (!NInoAttr(ni))
  448. base_ni = ni;
  449. else
  450. base_ni = ni->ext.base_ntfs_ino;
  451. /* Map, pin, and lock the mft record. */
  452. mrec = map_mft_record(base_ni);
  453. if (IS_ERR(mrec)) {
  454. err = PTR_ERR(mrec);
  455. goto err_out;
  456. }
  457. /*
  458. * If a parallel write made the attribute non-resident, drop the mft
  459. * record and retry the readpage.
  460. */
  461. if (unlikely(NInoNonResident(ni))) {
  462. unmap_mft_record(base_ni);
  463. goto retry_readpage;
  464. }
  465. ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
  466. if (unlikely(!ctx)) {
  467. err = -ENOMEM;
  468. goto unm_err_out;
  469. }
  470. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  471. CASE_SENSITIVE, 0, NULL, 0, ctx);
  472. if (unlikely(err))
  473. goto put_unm_err_out;
  474. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  475. read_lock_irqsave(&ni->size_lock, flags);
  476. if (unlikely(attr_len > ni->initialized_size))
  477. attr_len = ni->initialized_size;
  478. i_size = i_size_read(vi);
  479. read_unlock_irqrestore(&ni->size_lock, flags);
  480. if (unlikely(attr_len > i_size)) {
  481. /* Race with shrinking truncate. */
  482. attr_len = i_size;
  483. }
  484. addr = kmap_atomic(page, KM_USER0);
  485. /* Copy the data to the page. */
  486. memcpy(addr, (u8*)ctx->attr +
  487. le16_to_cpu(ctx->attr->data.resident.value_offset),
  488. attr_len);
  489. /* Zero the remainder of the page. */
  490. memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  491. flush_dcache_page(page);
  492. kunmap_atomic(addr, KM_USER0);
  493. put_unm_err_out:
  494. ntfs_attr_put_search_ctx(ctx);
  495. unm_err_out:
  496. unmap_mft_record(base_ni);
  497. done:
  498. SetPageUptodate(page);
  499. err_out:
  500. unlock_page(page);
  501. return err;
  502. }
  503. #ifdef NTFS_RW
  504. /**
  505. * ntfs_write_block - write a @page to the backing store
  506. * @page: page cache page to write out
  507. * @wbc: writeback control structure
  508. *
  509. * This function is for writing pages belonging to non-resident, non-mst
  510. * protected attributes to their backing store.
  511. *
  512. * For a page with buffers, map and write the dirty buffers asynchronously
  513. * under page writeback. For a page without buffers, create buffers for the
  514. * page, then proceed as above.
  515. *
  516. * If a page doesn't have buffers the page dirty state is definitive. If a page
  517. * does have buffers, the page dirty state is just a hint, and the buffer dirty
  518. * state is definitive. (A hint which has rules: dirty buffers against a clean
  519. * page is illegal. Other combinations are legal and need to be handled. In
  520. * particular a dirty page containing clean buffers for example.)
  521. *
  522. * Return 0 on success and -errno on error.
  523. *
  524. * Based on ntfs_read_block() and __block_write_full_page().
  525. */
  526. static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
  527. {
  528. VCN vcn;
  529. LCN lcn;
  530. s64 initialized_size;
  531. loff_t i_size;
  532. sector_t block, dblock, iblock;
  533. struct inode *vi;
  534. ntfs_inode *ni;
  535. ntfs_volume *vol;
  536. runlist_element *rl;
  537. struct buffer_head *bh, *head;
  538. unsigned long flags;
  539. unsigned int blocksize, vcn_ofs;
  540. int err;
  541. bool need_end_writeback;
  542. unsigned char blocksize_bits;
  543. vi = page->mapping->host;
  544. ni = NTFS_I(vi);
  545. vol = ni->vol;
  546. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  547. "0x%lx.", ni->mft_no, ni->type, page->index);
  548. BUG_ON(!NInoNonResident(ni));
  549. BUG_ON(NInoMstProtected(ni));
  550. blocksize = vol->sb->s_blocksize;
  551. blocksize_bits = vol->sb->s_blocksize_bits;
  552. if (!page_has_buffers(page)) {
  553. BUG_ON(!PageUptodate(page));
  554. create_empty_buffers(page, blocksize,
  555. (1 << BH_Uptodate) | (1 << BH_Dirty));
  556. if (unlikely(!page_has_buffers(page))) {
  557. ntfs_warning(vol->sb, "Error allocating page "
  558. "buffers. Redirtying page so we try "
  559. "again later.");
  560. /*
  561. * Put the page back on mapping->dirty_pages, but leave
  562. * its buffers' dirty state as-is.
  563. */
  564. redirty_page_for_writepage(wbc, page);
  565. unlock_page(page);
  566. return 0;
  567. }
  568. }
  569. bh = head = page_buffers(page);
  570. BUG_ON(!bh);
  571. /* NOTE: Different naming scheme to ntfs_read_block()! */
  572. /* The first block in the page. */
  573. block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  574. read_lock_irqsave(&ni->size_lock, flags);
  575. i_size = i_size_read(vi);
  576. initialized_size = ni->initialized_size;
  577. read_unlock_irqrestore(&ni->size_lock, flags);
  578. /* The first out of bounds block for the data size. */
  579. dblock = (i_size + blocksize - 1) >> blocksize_bits;
  580. /* The last (fully or partially) initialized block. */
  581. iblock = initialized_size >> blocksize_bits;
  582. /*
  583. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  584. * here, and the (potentially unmapped) buffers may become dirty at
  585. * any time. If a buffer becomes dirty here after we've inspected it
  586. * then we just miss that fact, and the page stays dirty.
  587. *
  588. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  589. * handle that here by just cleaning them.
  590. */
  591. /*
  592. * Loop through all the buffers in the page, mapping all the dirty
  593. * buffers to disk addresses and handling any aliases from the
  594. * underlying block device's mapping.
  595. */
  596. rl = NULL;
  597. err = 0;
  598. do {
  599. bool is_retry = false;
  600. if (unlikely(block >= dblock)) {
  601. /*
  602. * Mapped buffers outside i_size will occur, because
  603. * this page can be outside i_size when there is a
  604. * truncate in progress. The contents of such buffers
  605. * were zeroed by ntfs_writepage().
  606. *
  607. * FIXME: What about the small race window where
  608. * ntfs_writepage() has not done any clearing because
  609. * the page was within i_size but before we get here,
  610. * vmtruncate() modifies i_size?
  611. */
  612. clear_buffer_dirty(bh);
  613. set_buffer_uptodate(bh);
  614. continue;
  615. }
  616. /* Clean buffers are not written out, so no need to map them. */
  617. if (!buffer_dirty(bh))
  618. continue;
  619. /* Make sure we have enough initialized size. */
  620. if (unlikely((block >= iblock) &&
  621. (initialized_size < i_size))) {
  622. /*
  623. * If this page is fully outside initialized size, zero
  624. * out all pages between the current initialized size
  625. * and the current page. Just use ntfs_readpage() to do
  626. * the zeroing transparently.
  627. */
  628. if (block > iblock) {
  629. // TODO:
  630. // For each page do:
  631. // - read_cache_page()
  632. // Again for each page do:
  633. // - wait_on_page_locked()
  634. // - Check (PageUptodate(page) &&
  635. // !PageError(page))
  636. // Update initialized size in the attribute and
  637. // in the inode.
  638. // Again, for each page do:
  639. // __set_page_dirty_buffers();
  640. // page_cache_release()
  641. // We don't need to wait on the writes.
  642. // Update iblock.
  643. }
  644. /*
  645. * The current page straddles initialized size. Zero
  646. * all non-uptodate buffers and set them uptodate (and
  647. * dirty?). Note, there aren't any non-uptodate buffers
  648. * if the page is uptodate.
  649. * FIXME: For an uptodate page, the buffers may need to
  650. * be written out because they were not initialized on
  651. * disk before.
  652. */
  653. if (!PageUptodate(page)) {
  654. // TODO:
  655. // Zero any non-uptodate buffers up to i_size.
  656. // Set them uptodate and dirty.
  657. }
  658. // TODO:
  659. // Update initialized size in the attribute and in the
  660. // inode (up to i_size).
  661. // Update iblock.
  662. // FIXME: This is inefficient. Try to batch the two
  663. // size changes to happen in one go.
  664. ntfs_error(vol->sb, "Writing beyond initialized size "
  665. "is not supported yet. Sorry.");
  666. err = -EOPNOTSUPP;
  667. break;
  668. // Do NOT set_buffer_new() BUT DO clear buffer range
  669. // outside write request range.
  670. // set_buffer_uptodate() on complete buffers as well as
  671. // set_buffer_dirty().
  672. }
  673. /* No need to map buffers that are already mapped. */
  674. if (buffer_mapped(bh))
  675. continue;
  676. /* Unmapped, dirty buffer. Need to map it. */
  677. bh->b_bdev = vol->sb->s_bdev;
  678. /* Convert block into corresponding vcn and offset. */
  679. vcn = (VCN)block << blocksize_bits;
  680. vcn_ofs = vcn & vol->cluster_size_mask;
  681. vcn >>= vol->cluster_size_bits;
  682. if (!rl) {
  683. lock_retry_remap:
  684. down_read(&ni->runlist.lock);
  685. rl = ni->runlist.rl;
  686. }
  687. if (likely(rl != NULL)) {
  688. /* Seek to element containing target vcn. */
  689. while (rl->length && rl[1].vcn <= vcn)
  690. rl++;
  691. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  692. } else
  693. lcn = LCN_RL_NOT_MAPPED;
  694. /* Successful remap. */
  695. if (lcn >= 0) {
  696. /* Setup buffer head to point to correct block. */
  697. bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
  698. vcn_ofs) >> blocksize_bits;
  699. set_buffer_mapped(bh);
  700. continue;
  701. }
  702. /* It is a hole, need to instantiate it. */
  703. if (lcn == LCN_HOLE) {
  704. u8 *kaddr;
  705. unsigned long *bpos, *bend;
  706. /* Check if the buffer is zero. */
  707. kaddr = kmap_atomic(page, KM_USER0);
  708. bpos = (unsigned long *)(kaddr + bh_offset(bh));
  709. bend = (unsigned long *)((u8*)bpos + blocksize);
  710. do {
  711. if (unlikely(*bpos))
  712. break;
  713. } while (likely(++bpos < bend));
  714. kunmap_atomic(kaddr, KM_USER0);
  715. if (bpos == bend) {
  716. /*
  717. * Buffer is zero and sparse, no need to write
  718. * it.
  719. */
  720. bh->b_blocknr = -1;
  721. clear_buffer_dirty(bh);
  722. continue;
  723. }
  724. // TODO: Instantiate the hole.
  725. // clear_buffer_new(bh);
  726. // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  727. ntfs_error(vol->sb, "Writing into sparse regions is "
  728. "not supported yet. Sorry.");
  729. err = -EOPNOTSUPP;
  730. break;
  731. }
  732. /* If first try and runlist unmapped, map and retry. */
  733. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  734. is_retry = true;
  735. /*
  736. * Attempt to map runlist, dropping lock for
  737. * the duration.
  738. */
  739. up_read(&ni->runlist.lock);
  740. err = ntfs_map_runlist(ni, vcn);
  741. if (likely(!err))
  742. goto lock_retry_remap;
  743. rl = NULL;
  744. } else if (!rl)
  745. up_read(&ni->runlist.lock);
  746. /*
  747. * If buffer is outside the runlist, truncate has cut it out
  748. * of the runlist. Just clean and clear the buffer and set it
  749. * uptodate so it can get discarded by the VM.
  750. */
  751. if (err == -ENOENT || lcn == LCN_ENOENT) {
  752. bh->b_blocknr = -1;
  753. clear_buffer_dirty(bh);
  754. zero_user(page, bh_offset(bh), blocksize);
  755. set_buffer_uptodate(bh);
  756. err = 0;
  757. continue;
  758. }
  759. /* Failed to map the buffer, even after retrying. */
  760. if (!err)
  761. err = -EIO;
  762. bh->b_blocknr = -1;
  763. ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
  764. "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
  765. "because its location on disk could not be "
  766. "determined%s (error code %i).", ni->mft_no,
  767. ni->type, (unsigned long long)vcn,
  768. vcn_ofs, is_retry ? " even after "
  769. "retrying" : "", err);
  770. break;
  771. } while (block++, (bh = bh->b_this_page) != head);
  772. /* Release the lock if we took it. */
  773. if (rl)
  774. up_read(&ni->runlist.lock);
  775. /* For the error case, need to reset bh to the beginning. */
  776. bh = head;
  777. /* Just an optimization, so ->readpage() is not called later. */
  778. if (unlikely(!PageUptodate(page))) {
  779. int uptodate = 1;
  780. do {
  781. if (!buffer_uptodate(bh)) {
  782. uptodate = 0;
  783. bh = head;
  784. break;
  785. }
  786. } while ((bh = bh->b_this_page) != head);
  787. if (uptodate)
  788. SetPageUptodate(page);
  789. }
  790. /* Setup all mapped, dirty buffers for async write i/o. */
  791. do {
  792. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  793. lock_buffer(bh);
  794. if (test_clear_buffer_dirty(bh)) {
  795. BUG_ON(!buffer_uptodate(bh));
  796. mark_buffer_async_write(bh);
  797. } else
  798. unlock_buffer(bh);
  799. } else if (unlikely(err)) {
  800. /*
  801. * For the error case. The buffer may have been set
  802. * dirty during attachment to a dirty page.
  803. */
  804. if (err != -ENOMEM)
  805. clear_buffer_dirty(bh);
  806. }
  807. } while ((bh = bh->b_this_page) != head);
  808. if (unlikely(err)) {
  809. // TODO: Remove the -EOPNOTSUPP check later on...
  810. if (unlikely(err == -EOPNOTSUPP))
  811. err = 0;
  812. else if (err == -ENOMEM) {
  813. ntfs_warning(vol->sb, "Error allocating memory. "
  814. "Redirtying page so we try again "
  815. "later.");
  816. /*
  817. * Put the page back on mapping->dirty_pages, but
  818. * leave its buffer's dirty state as-is.
  819. */
  820. redirty_page_for_writepage(wbc, page);
  821. err = 0;
  822. } else
  823. SetPageError(page);
  824. }
  825. BUG_ON(PageWriteback(page));
  826. set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
  827. /* Submit the prepared buffers for i/o. */
  828. need_end_writeback = true;
  829. do {
  830. struct buffer_head *next = bh->b_this_page;
  831. if (buffer_async_write(bh)) {
  832. submit_bh(WRITE, bh);
  833. need_end_writeback = false;
  834. }
  835. bh = next;
  836. } while (bh != head);
  837. unlock_page(page);
  838. /* If no i/o was started, need to end_page_writeback(). */
  839. if (unlikely(need_end_writeback))
  840. end_page_writeback(page);
  841. ntfs_debug("Done.");
  842. return err;
  843. }
  844. /**
  845. * ntfs_write_mst_block - write a @page to the backing store
  846. * @page: page cache page to write out
  847. * @wbc: writeback control structure
  848. *
  849. * This function is for writing pages belonging to non-resident, mst protected
  850. * attributes to their backing store. The only supported attributes are index
  851. * allocation and $MFT/$DATA. Both directory inodes and index inodes are
  852. * supported for the index allocation case.
  853. *
  854. * The page must remain locked for the duration of the write because we apply
  855. * the mst fixups, write, and then undo the fixups, so if we were to unlock the
  856. * page before undoing the fixups, any other user of the page will see the
  857. * page contents as corrupt.
  858. *
  859. * We clear the page uptodate flag for the duration of the function to ensure
  860. * exclusion for the $MFT/$DATA case against someone mapping an mft record we
  861. * are about to apply the mst fixups to.
  862. *
  863. * Return 0 on success and -errno on error.
  864. *
  865. * Based on ntfs_write_block(), ntfs_mft_writepage(), and
  866. * write_mft_record_nolock().
  867. */
  868. static int ntfs_write_mst_block(struct page *page,
  869. struct writeback_control *wbc)
  870. {
  871. sector_t block, dblock, rec_block;
  872. struct inode *vi = page->mapping->host;
  873. ntfs_inode *ni = NTFS_I(vi);
  874. ntfs_volume *vol = ni->vol;
  875. u8 *kaddr;
  876. unsigned int rec_size = ni->itype.index.block_size;
  877. ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
  878. struct buffer_head *bh, *head, *tbh, *rec_start_bh;
  879. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  880. runlist_element *rl;
  881. int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
  882. unsigned bh_size, rec_size_bits;
  883. bool sync, is_mft, page_is_dirty, rec_is_dirty;
  884. unsigned char bh_size_bits;
  885. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  886. "0x%lx.", vi->i_ino, ni->type, page->index);
  887. BUG_ON(!NInoNonResident(ni));
  888. BUG_ON(!NInoMstProtected(ni));
  889. is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
  890. /*
  891. * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
  892. * in its page cache were to be marked dirty. However this should
  893. * never happen with the current driver and considering we do not
  894. * handle this case here we do want to BUG(), at least for now.
  895. */
  896. BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
  897. (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
  898. bh_size = vol->sb->s_blocksize;
  899. bh_size_bits = vol->sb->s_blocksize_bits;
  900. max_bhs = PAGE_CACHE_SIZE / bh_size;
  901. BUG_ON(!max_bhs);
  902. BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
  903. /* Were we called for sync purposes? */
  904. sync = (wbc->sync_mode == WB_SYNC_ALL);
  905. /* Make sure we have mapped buffers. */
  906. bh = head = page_buffers(page);
  907. BUG_ON(!bh);
  908. rec_size_bits = ni->itype.index.block_size_bits;
  909. BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
  910. bhs_per_rec = rec_size >> bh_size_bits;
  911. BUG_ON(!bhs_per_rec);
  912. /* The first block in the page. */
  913. rec_block = block = (sector_t)page->index <<
  914. (PAGE_CACHE_SHIFT - bh_size_bits);
  915. /* The first out of bounds block for the data size. */
  916. dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
  917. rl = NULL;
  918. err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
  919. page_is_dirty = rec_is_dirty = false;
  920. rec_start_bh = NULL;
  921. do {
  922. bool is_retry = false;
  923. if (likely(block < rec_block)) {
  924. if (unlikely(block >= dblock)) {
  925. clear_buffer_dirty(bh);
  926. set_buffer_uptodate(bh);
  927. continue;
  928. }
  929. /*
  930. * This block is not the first one in the record. We
  931. * ignore the buffer's dirty state because we could
  932. * have raced with a parallel mark_ntfs_record_dirty().
  933. */
  934. if (!rec_is_dirty)
  935. continue;
  936. if (unlikely(err2)) {
  937. if (err2 != -ENOMEM)
  938. clear_buffer_dirty(bh);
  939. continue;
  940. }
  941. } else /* if (block == rec_block) */ {
  942. BUG_ON(block > rec_block);
  943. /* This block is the first one in the record. */
  944. rec_block += bhs_per_rec;
  945. err2 = 0;
  946. if (unlikely(block >= dblock)) {
  947. clear_buffer_dirty(bh);
  948. continue;
  949. }
  950. if (!buffer_dirty(bh)) {
  951. /* Clean records are not written out. */
  952. rec_is_dirty = false;
  953. continue;
  954. }
  955. rec_is_dirty = true;
  956. rec_start_bh = bh;
  957. }
  958. /* Need to map the buffer if it is not mapped already. */
  959. if (unlikely(!buffer_mapped(bh))) {
  960. VCN vcn;
  961. LCN lcn;
  962. unsigned int vcn_ofs;
  963. bh->b_bdev = vol->sb->s_bdev;
  964. /* Obtain the vcn and offset of the current block. */
  965. vcn = (VCN)block << bh_size_bits;
  966. vcn_ofs = vcn & vol->cluster_size_mask;
  967. vcn >>= vol->cluster_size_bits;
  968. if (!rl) {
  969. lock_retry_remap:
  970. down_read(&ni->runlist.lock);
  971. rl = ni->runlist.rl;
  972. }
  973. if (likely(rl != NULL)) {
  974. /* Seek to element containing target vcn. */
  975. while (rl->length && rl[1].vcn <= vcn)
  976. rl++;
  977. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  978. } else
  979. lcn = LCN_RL_NOT_MAPPED;
  980. /* Successful remap. */
  981. if (likely(lcn >= 0)) {
  982. /* Setup buffer head to correct block. */
  983. bh->b_blocknr = ((lcn <<
  984. vol->cluster_size_bits) +
  985. vcn_ofs) >> bh_size_bits;
  986. set_buffer_mapped(bh);
  987. } else {
  988. /*
  989. * Remap failed. Retry to map the runlist once
  990. * unless we are working on $MFT which always
  991. * has the whole of its runlist in memory.
  992. */
  993. if (!is_mft && !is_retry &&
  994. lcn == LCN_RL_NOT_MAPPED) {
  995. is_retry = true;
  996. /*
  997. * Attempt to map runlist, dropping
  998. * lock for the duration.
  999. */
  1000. up_read(&ni->runlist.lock);
  1001. err2 = ntfs_map_runlist(ni, vcn);
  1002. if (likely(!err2))
  1003. goto lock_retry_remap;
  1004. if (err2 == -ENOMEM)
  1005. page_is_dirty = true;
  1006. lcn = err2;
  1007. } else {
  1008. err2 = -EIO;
  1009. if (!rl)
  1010. up_read(&ni->runlist.lock);
  1011. }
  1012. /* Hard error. Abort writing this record. */
  1013. if (!err || err == -ENOMEM)
  1014. err = err2;
  1015. bh->b_blocknr = -1;
  1016. ntfs_error(vol->sb, "Cannot write ntfs record "
  1017. "0x%llx (inode 0x%lx, "
  1018. "attribute type 0x%x) because "
  1019. "its location on disk could "
  1020. "not be determined (error "
  1021. "code %lli).",
  1022. (long long)block <<
  1023. bh_size_bits >>
  1024. vol->mft_record_size_bits,
  1025. ni->mft_no, ni->type,
  1026. (long long)lcn);
  1027. /*
  1028. * If this is not the first buffer, remove the
  1029. * buffers in this record from the list of
  1030. * buffers to write and clear their dirty bit
  1031. * if not error -ENOMEM.
  1032. */
  1033. if (rec_start_bh != bh) {
  1034. while (bhs[--nr_bhs] != rec_start_bh)
  1035. ;
  1036. if (err2 != -ENOMEM) {
  1037. do {
  1038. clear_buffer_dirty(
  1039. rec_start_bh);
  1040. } while ((rec_start_bh =
  1041. rec_start_bh->
  1042. b_this_page) !=
  1043. bh);
  1044. }
  1045. }
  1046. continue;
  1047. }
  1048. }
  1049. BUG_ON(!buffer_uptodate(bh));
  1050. BUG_ON(nr_bhs >= max_bhs);
  1051. bhs[nr_bhs++] = bh;
  1052. } while (block++, (bh = bh->b_this_page) != head);
  1053. if (unlikely(rl))
  1054. up_read(&ni->runlist.lock);
  1055. /* If there were no dirty buffers, we are done. */
  1056. if (!nr_bhs)
  1057. goto done;
  1058. /* Map the page so we can access its contents. */
  1059. kaddr = kmap(page);
  1060. /* Clear the page uptodate flag whilst the mst fixups are applied. */
  1061. BUG_ON(!PageUptodate(page));
  1062. ClearPageUptodate(page);
  1063. for (i = 0; i < nr_bhs; i++) {
  1064. unsigned int ofs;
  1065. /* Skip buffers which are not at the beginning of records. */
  1066. if (i % bhs_per_rec)
  1067. continue;
  1068. tbh = bhs[i];
  1069. ofs = bh_offset(tbh);
  1070. if (is_mft) {
  1071. ntfs_inode *tni;
  1072. unsigned long mft_no;
  1073. /* Get the mft record number. */
  1074. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1075. >> rec_size_bits;
  1076. /* Check whether to write this mft record. */
  1077. tni = NULL;
  1078. if (!ntfs_may_write_mft_record(vol, mft_no,
  1079. (MFT_RECORD*)(kaddr + ofs), &tni)) {
  1080. /*
  1081. * The record should not be written. This
  1082. * means we need to redirty the page before
  1083. * returning.
  1084. */
  1085. page_is_dirty = true;
  1086. /*
  1087. * Remove the buffers in this mft record from
  1088. * the list of buffers to write.
  1089. */
  1090. do {
  1091. bhs[i] = NULL;
  1092. } while (++i % bhs_per_rec);
  1093. continue;
  1094. }
  1095. /*
  1096. * The record should be written. If a locked ntfs
  1097. * inode was returned, add it to the array of locked
  1098. * ntfs inodes.
  1099. */
  1100. if (tni)
  1101. locked_nis[nr_locked_nis++] = tni;
  1102. }
  1103. /* Apply the mst protection fixups. */
  1104. err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
  1105. rec_size);
  1106. if (unlikely(err2)) {
  1107. if (!err || err == -ENOMEM)
  1108. err = -EIO;
  1109. ntfs_error(vol->sb, "Failed to apply mst fixups "
  1110. "(inode 0x%lx, attribute type 0x%x, "
  1111. "page index 0x%lx, page offset 0x%x)!"
  1112. " Unmount and run chkdsk.", vi->i_ino,
  1113. ni->type, page->index, ofs);
  1114. /*
  1115. * Mark all the buffers in this record clean as we do
  1116. * not want to write corrupt data to disk.
  1117. */
  1118. do {
  1119. clear_buffer_dirty(bhs[i]);
  1120. bhs[i] = NULL;
  1121. } while (++i % bhs_per_rec);
  1122. continue;
  1123. }
  1124. nr_recs++;
  1125. }
  1126. /* If no records are to be written out, we are done. */
  1127. if (!nr_recs)
  1128. goto unm_done;
  1129. flush_dcache_page(page);
  1130. /* Lock buffers and start synchronous write i/o on them. */
  1131. for (i = 0; i < nr_bhs; i++) {
  1132. tbh = bhs[i];
  1133. if (!tbh)
  1134. continue;
  1135. if (!trylock_buffer(tbh))
  1136. BUG();
  1137. /* The buffer dirty state is now irrelevant, just clean it. */
  1138. clear_buffer_dirty(tbh);
  1139. BUG_ON(!buffer_uptodate(tbh));
  1140. BUG_ON(!buffer_mapped(tbh));
  1141. get_bh(tbh);
  1142. tbh->b_end_io = end_buffer_write_sync;
  1143. submit_bh(WRITE, tbh);
  1144. }
  1145. /* Synchronize the mft mirror now if not @sync. */
  1146. if (is_mft && !sync)
  1147. goto do_mirror;
  1148. do_wait:
  1149. /* Wait on i/o completion of buffers. */
  1150. for (i = 0; i < nr_bhs; i++) {
  1151. tbh = bhs[i];
  1152. if (!tbh)
  1153. continue;
  1154. wait_on_buffer(tbh);
  1155. if (unlikely(!buffer_uptodate(tbh))) {
  1156. ntfs_error(vol->sb, "I/O error while writing ntfs "
  1157. "record buffer (inode 0x%lx, "
  1158. "attribute type 0x%x, page index "
  1159. "0x%lx, page offset 0x%lx)! Unmount "
  1160. "and run chkdsk.", vi->i_ino, ni->type,
  1161. page->index, bh_offset(tbh));
  1162. if (!err || err == -ENOMEM)
  1163. err = -EIO;
  1164. /*
  1165. * Set the buffer uptodate so the page and buffer
  1166. * states do not become out of sync.
  1167. */
  1168. set_buffer_uptodate(tbh);
  1169. }
  1170. }
  1171. /* If @sync, now synchronize the mft mirror. */
  1172. if (is_mft && sync) {
  1173. do_mirror:
  1174. for (i = 0; i < nr_bhs; i++) {
  1175. unsigned long mft_no;
  1176. unsigned int ofs;
  1177. /*
  1178. * Skip buffers which are not at the beginning of
  1179. * records.
  1180. */
  1181. if (i % bhs_per_rec)
  1182. continue;
  1183. tbh = bhs[i];
  1184. /* Skip removed buffers (and hence records). */
  1185. if (!tbh)
  1186. continue;
  1187. ofs = bh_offset(tbh);
  1188. /* Get the mft record number. */
  1189. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1190. >> rec_size_bits;
  1191. if (mft_no < vol->mftmirr_size)
  1192. ntfs_sync_mft_mirror(vol, mft_no,
  1193. (MFT_RECORD*)(kaddr + ofs),
  1194. sync);
  1195. }
  1196. if (!sync)
  1197. goto do_wait;
  1198. }
  1199. /* Remove the mst protection fixups again. */
  1200. for (i = 0; i < nr_bhs; i++) {
  1201. if (!(i % bhs_per_rec)) {
  1202. tbh = bhs[i];
  1203. if (!tbh)
  1204. continue;
  1205. post_write_mst_fixup((NTFS_RECORD*)(kaddr +
  1206. bh_offset(tbh)));
  1207. }
  1208. }
  1209. flush_dcache_page(page);
  1210. unm_done:
  1211. /* Unlock any locked inodes. */
  1212. while (nr_locked_nis-- > 0) {
  1213. ntfs_inode *tni, *base_tni;
  1214. tni = locked_nis[nr_locked_nis];
  1215. /* Get the base inode. */
  1216. mutex_lock(&tni->extent_lock);
  1217. if (tni->nr_extents >= 0)
  1218. base_tni = tni;
  1219. else {
  1220. base_tni = tni->ext.base_ntfs_ino;
  1221. BUG_ON(!base_tni);
  1222. }
  1223. mutex_unlock(&tni->extent_lock);
  1224. ntfs_debug("Unlocking %s inode 0x%lx.",
  1225. tni == base_tni ? "base" : "extent",
  1226. tni->mft_no);
  1227. mutex_unlock(&tni->mrec_lock);
  1228. atomic_dec(&tni->count);
  1229. iput(VFS_I(base_tni));
  1230. }
  1231. SetPageUptodate(page);
  1232. kunmap(page);
  1233. done:
  1234. if (unlikely(err && err != -ENOMEM)) {
  1235. /*
  1236. * Set page error if there is only one ntfs record in the page.
  1237. * Otherwise we would loose per-record granularity.
  1238. */
  1239. if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
  1240. SetPageError(page);
  1241. NVolSetErrors(vol);
  1242. }
  1243. if (page_is_dirty) {
  1244. ntfs_debug("Page still contains one or more dirty ntfs "
  1245. "records. Redirtying the page starting at "
  1246. "record 0x%lx.", page->index <<
  1247. (PAGE_CACHE_SHIFT - rec_size_bits));
  1248. redirty_page_for_writepage(wbc, page);
  1249. unlock_page(page);
  1250. } else {
  1251. /*
  1252. * Keep the VM happy. This must be done otherwise the
  1253. * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
  1254. * the page is clean.
  1255. */
  1256. BUG_ON(PageWriteback(page));
  1257. set_page_writeback(page);
  1258. unlock_page(page);
  1259. end_page_writeback(page);
  1260. }
  1261. if (likely(!err))
  1262. ntfs_debug("Done.");
  1263. return err;
  1264. }
  1265. /**
  1266. * ntfs_writepage - write a @page to the backing store
  1267. * @page: page cache page to write out
  1268. * @wbc: writeback control structure
  1269. *
  1270. * This is called from the VM when it wants to have a dirty ntfs page cache
  1271. * page cleaned. The VM has already locked the page and marked it clean.
  1272. *
  1273. * For non-resident attributes, ntfs_writepage() writes the @page by calling
  1274. * the ntfs version of the generic block_write_full_page() function,
  1275. * ntfs_write_block(), which in turn if necessary creates and writes the
  1276. * buffers associated with the page asynchronously.
  1277. *
  1278. * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
  1279. * the data to the mft record (which at this stage is most likely in memory).
  1280. * The mft record is then marked dirty and written out asynchronously via the
  1281. * vfs inode dirty code path for the inode the mft record belongs to or via the
  1282. * vm page dirty code path for the page the mft record is in.
  1283. *
  1284. * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
  1285. *
  1286. * Return 0 on success and -errno on error.
  1287. */
  1288. static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
  1289. {
  1290. loff_t i_size;
  1291. struct inode *vi = page->mapping->host;
  1292. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1293. char *addr;
  1294. ntfs_attr_search_ctx *ctx = NULL;
  1295. MFT_RECORD *m = NULL;
  1296. u32 attr_len;
  1297. int err;
  1298. retry_writepage:
  1299. BUG_ON(!PageLocked(page));
  1300. i_size = i_size_read(vi);
  1301. /* Is the page fully outside i_size? (truncate in progress) */
  1302. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  1303. PAGE_CACHE_SHIFT)) {
  1304. /*
  1305. * The page may have dirty, unmapped buffers. Make them
  1306. * freeable here, so the page does not leak.
  1307. */
  1308. block_invalidatepage(page, 0);
  1309. unlock_page(page);
  1310. ntfs_debug("Write outside i_size - truncated?");
  1311. return 0;
  1312. }
  1313. /*
  1314. * Only $DATA attributes can be encrypted and only unnamed $DATA
  1315. * attributes can be compressed. Index root can have the flags set but
  1316. * this means to create compressed/encrypted files, not that the
  1317. * attribute is compressed/encrypted. Note we need to check for
  1318. * AT_INDEX_ALLOCATION since this is the type of both directory and
  1319. * index inodes.
  1320. */
  1321. if (ni->type != AT_INDEX_ALLOCATION) {
  1322. /* If file is encrypted, deny access, just like NT4. */
  1323. if (NInoEncrypted(ni)) {
  1324. unlock_page(page);
  1325. BUG_ON(ni->type != AT_DATA);
  1326. ntfs_debug("Denying write access to encrypted file.");
  1327. return -EACCES;
  1328. }
  1329. /* Compressed data streams are handled in compress.c. */
  1330. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  1331. BUG_ON(ni->type != AT_DATA);
  1332. BUG_ON(ni->name_len);
  1333. // TODO: Implement and replace this with
  1334. // return ntfs_write_compressed_block(page);
  1335. unlock_page(page);
  1336. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1337. "not supported yet. Sorry.");
  1338. return -EOPNOTSUPP;
  1339. }
  1340. // TODO: Implement and remove this check.
  1341. if (NInoNonResident(ni) && NInoSparse(ni)) {
  1342. unlock_page(page);
  1343. ntfs_error(vi->i_sb, "Writing to sparse files is not "
  1344. "supported yet. Sorry.");
  1345. return -EOPNOTSUPP;
  1346. }
  1347. }
  1348. /* NInoNonResident() == NInoIndexAllocPresent() */
  1349. if (NInoNonResident(ni)) {
  1350. /* We have to zero every time due to mmap-at-end-of-file. */
  1351. if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
  1352. /* The page straddles i_size. */
  1353. unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
  1354. zero_user_segment(page, ofs, PAGE_CACHE_SIZE);
  1355. }
  1356. /* Handle mst protected attributes. */
  1357. if (NInoMstProtected(ni))
  1358. return ntfs_write_mst_block(page, wbc);
  1359. /* Normal, non-resident data stream. */
  1360. return ntfs_write_block(page, wbc);
  1361. }
  1362. /*
  1363. * Attribute is resident, implying it is not compressed, encrypted, or
  1364. * mst protected. This also means the attribute is smaller than an mft
  1365. * record and hence smaller than a page, so can simply return error on
  1366. * any pages with index above 0. Note the attribute can actually be
  1367. * marked compressed but if it is resident the actual data is not
  1368. * compressed so we are ok to ignore the compressed flag here.
  1369. */
  1370. BUG_ON(page_has_buffers(page));
  1371. BUG_ON(!PageUptodate(page));
  1372. if (unlikely(page->index > 0)) {
  1373. ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
  1374. "Aborting write.", page->index);
  1375. BUG_ON(PageWriteback(page));
  1376. set_page_writeback(page);
  1377. unlock_page(page);
  1378. end_page_writeback(page);
  1379. return -EIO;
  1380. }
  1381. if (!NInoAttr(ni))
  1382. base_ni = ni;
  1383. else
  1384. base_ni = ni->ext.base_ntfs_ino;
  1385. /* Map, pin, and lock the mft record. */
  1386. m = map_mft_record(base_ni);
  1387. if (IS_ERR(m)) {
  1388. err = PTR_ERR(m);
  1389. m = NULL;
  1390. ctx = NULL;
  1391. goto err_out;
  1392. }
  1393. /*
  1394. * If a parallel write made the attribute non-resident, drop the mft
  1395. * record and retry the writepage.
  1396. */
  1397. if (unlikely(NInoNonResident(ni))) {
  1398. unmap_mft_record(base_ni);
  1399. goto retry_writepage;
  1400. }
  1401. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1402. if (unlikely(!ctx)) {
  1403. err = -ENOMEM;
  1404. goto err_out;
  1405. }
  1406. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1407. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1408. if (unlikely(err))
  1409. goto err_out;
  1410. /*
  1411. * Keep the VM happy. This must be done otherwise the radix-tree tag
  1412. * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
  1413. */
  1414. BUG_ON(PageWriteback(page));
  1415. set_page_writeback(page);
  1416. unlock_page(page);
  1417. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  1418. i_size = i_size_read(vi);
  1419. if (unlikely(attr_len > i_size)) {
  1420. /* Race with shrinking truncate or a failed truncate. */
  1421. attr_len = i_size;
  1422. /*
  1423. * If the truncate failed, fix it up now. If a concurrent
  1424. * truncate, we do its job, so it does not have to do anything.
  1425. */
  1426. err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
  1427. attr_len);
  1428. /* Shrinking cannot fail. */
  1429. BUG_ON(err);
  1430. }
  1431. addr = kmap_atomic(page, KM_USER0);
  1432. /* Copy the data from the page to the mft record. */
  1433. memcpy((u8*)ctx->attr +
  1434. le16_to_cpu(ctx->attr->data.resident.value_offset),
  1435. addr, attr_len);
  1436. /* Zero out of bounds area in the page cache page. */
  1437. memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1438. kunmap_atomic(addr, KM_USER0);
  1439. flush_dcache_page(page);
  1440. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1441. /* We are done with the page. */
  1442. end_page_writeback(page);
  1443. /* Finally, mark the mft record dirty, so it gets written back. */
  1444. mark_mft_record_dirty(ctx->ntfs_ino);
  1445. ntfs_attr_put_search_ctx(ctx);
  1446. unmap_mft_record(base_ni);
  1447. return 0;
  1448. err_out:
  1449. if (err == -ENOMEM) {
  1450. ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
  1451. "page so we try again later.");
  1452. /*
  1453. * Put the page back on mapping->dirty_pages, but leave its
  1454. * buffers' dirty state as-is.
  1455. */
  1456. redirty_page_for_writepage(wbc, page);
  1457. err = 0;
  1458. } else {
  1459. ntfs_error(vi->i_sb, "Resident attribute write failed with "
  1460. "error %i.", err);
  1461. SetPageError(page);
  1462. NVolSetErrors(ni->vol);
  1463. }
  1464. unlock_page(page);
  1465. if (ctx)
  1466. ntfs_attr_put_search_ctx(ctx);
  1467. if (m)
  1468. unmap_mft_record(base_ni);
  1469. return err;
  1470. }
  1471. #endif /* NTFS_RW */
  1472. /**
  1473. * ntfs_aops - general address space operations for inodes and attributes
  1474. */
  1475. const struct address_space_operations ntfs_aops = {
  1476. .readpage = ntfs_readpage, /* Fill page with data. */
  1477. #ifdef NTFS_RW
  1478. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  1479. #endif /* NTFS_RW */
  1480. .migratepage = buffer_migrate_page, /* Move a page cache page from
  1481. one physical page to an
  1482. other. */
  1483. .error_remove_page = generic_error_remove_page,
  1484. };
  1485. /**
  1486. * ntfs_mst_aops - general address space operations for mst protecteed inodes
  1487. * and attributes
  1488. */
  1489. const struct address_space_operations ntfs_mst_aops = {
  1490. .readpage = ntfs_readpage, /* Fill page with data. */
  1491. #ifdef NTFS_RW
  1492. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  1493. .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
  1494. without touching the buffers
  1495. belonging to the page. */
  1496. #endif /* NTFS_RW */
  1497. .migratepage = buffer_migrate_page, /* Move a page cache page from
  1498. one physical page to an
  1499. other. */
  1500. .error_remove_page = generic_error_remove_page,
  1501. };
  1502. #ifdef NTFS_RW
  1503. /**
  1504. * mark_ntfs_record_dirty - mark an ntfs record dirty
  1505. * @page: page containing the ntfs record to mark dirty
  1506. * @ofs: byte offset within @page at which the ntfs record begins
  1507. *
  1508. * Set the buffers and the page in which the ntfs record is located dirty.
  1509. *
  1510. * The latter also marks the vfs inode the ntfs record belongs to dirty
  1511. * (I_DIRTY_PAGES only).
  1512. *
  1513. * If the page does not have buffers, we create them and set them uptodate.
  1514. * The page may not be locked which is why we need to handle the buffers under
  1515. * the mapping->private_lock. Once the buffers are marked dirty we no longer
  1516. * need the lock since try_to_free_buffers() does not free dirty buffers.
  1517. */
  1518. void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
  1519. struct address_space *mapping = page->mapping;
  1520. ntfs_inode *ni = NTFS_I(mapping->host);
  1521. struct buffer_head *bh, *head, *buffers_to_free = NULL;
  1522. unsigned int end, bh_size, bh_ofs;
  1523. BUG_ON(!PageUptodate(page));
  1524. end = ofs + ni->itype.index.block_size;
  1525. bh_size = VFS_I(ni)->i_sb->s_blocksize;
  1526. spin_lock(&mapping->private_lock);
  1527. if (unlikely(!page_has_buffers(page))) {
  1528. spin_unlock(&mapping->private_lock);
  1529. bh = head = alloc_page_buffers(page, bh_size, 1);
  1530. spin_lock(&mapping->private_lock);
  1531. if (likely(!page_has_buffers(page))) {
  1532. struct buffer_head *tail;
  1533. do {
  1534. set_buffer_uptodate(bh);
  1535. tail = bh;
  1536. bh = bh->b_this_page;
  1537. } while (bh);
  1538. tail->b_this_page = head;
  1539. attach_page_buffers(page, head);
  1540. } else
  1541. buffers_to_free = bh;
  1542. }
  1543. bh = head = page_buffers(page);
  1544. BUG_ON(!bh);
  1545. do {
  1546. bh_ofs = bh_offset(bh);
  1547. if (bh_ofs + bh_size <= ofs)
  1548. continue;
  1549. if (unlikely(bh_ofs >= end))
  1550. break;
  1551. set_buffer_dirty(bh);
  1552. } while ((bh = bh->b_this_page) != head);
  1553. spin_unlock(&mapping->private_lock);
  1554. __set_page_dirty_nobuffers(page);
  1555. if (unlikely(buffers_to_free)) {
  1556. do {
  1557. bh = buffers_to_free->b_this_page;
  1558. free_buffer_head(buffers_to_free);
  1559. buffers_to_free = bh;
  1560. } while (buffers_to_free);
  1561. }
  1562. }
  1563. #endif /* NTFS_RW */