aops.c 48 KB

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