aops.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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/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. int ofs;
  83. ofs = 0;
  84. if (file_ofs < init_size)
  85. ofs = init_size - file_ofs;
  86. local_irq_save(flags);
  87. zero_user_page(page, bh_offset(bh) + ofs,
  88. bh->b_size - ofs, KM_BIO_SRC_IRQ);
  89. local_irq_restore(flags);
  90. }
  91. } else {
  92. clear_buffer_uptodate(bh);
  93. SetPageError(page);
  94. ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
  95. "0x%llx.", (unsigned long long)bh->b_blocknr);
  96. }
  97. first = page_buffers(page);
  98. local_irq_save(flags);
  99. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  100. clear_buffer_async_read(bh);
  101. unlock_buffer(bh);
  102. tmp = bh;
  103. do {
  104. if (!buffer_uptodate(tmp))
  105. page_uptodate = 0;
  106. if (buffer_async_read(tmp)) {
  107. if (likely(buffer_locked(tmp)))
  108. goto still_busy;
  109. /* Async buffers must be locked. */
  110. BUG();
  111. }
  112. tmp = tmp->b_this_page;
  113. } while (tmp != bh);
  114. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  115. local_irq_restore(flags);
  116. /*
  117. * If none of the buffers had errors then we can set the page uptodate,
  118. * but we first have to perform the post read mst fixups, if the
  119. * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
  120. * Note we ignore fixup errors as those are detected when
  121. * map_mft_record() is called which gives us per record granularity
  122. * rather than per page granularity.
  123. */
  124. if (!NInoMstProtected(ni)) {
  125. if (likely(page_uptodate && !PageError(page)))
  126. SetPageUptodate(page);
  127. } else {
  128. u8 *kaddr;
  129. unsigned int i, recs;
  130. u32 rec_size;
  131. rec_size = ni->itype.index.block_size;
  132. recs = PAGE_CACHE_SIZE / rec_size;
  133. /* Should have been verified before we got here... */
  134. BUG_ON(!recs);
  135. local_irq_save(flags);
  136. kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  137. for (i = 0; i < recs; i++)
  138. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  139. i * rec_size), rec_size);
  140. kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
  141. local_irq_restore(flags);
  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. int err = 0;
  229. if (unlikely(buffer_uptodate(bh)))
  230. continue;
  231. if (unlikely(buffer_mapped(bh))) {
  232. arr[nr++] = bh;
  233. continue;
  234. }
  235. bh->b_bdev = vol->sb->s_bdev;
  236. /* Is the block within the allowed limits? */
  237. if (iblock < lblock) {
  238. bool is_retry = false;
  239. /* Convert iblock into corresponding vcn and offset. */
  240. vcn = (VCN)iblock << blocksize_bits >>
  241. vol->cluster_size_bits;
  242. vcn_ofs = ((VCN)iblock << blocksize_bits) &
  243. vol->cluster_size_mask;
  244. if (!rl) {
  245. lock_retry_remap:
  246. down_read(&ni->runlist.lock);
  247. rl = ni->runlist.rl;
  248. }
  249. if (likely(rl != NULL)) {
  250. /* Seek to element containing target vcn. */
  251. while (rl->length && rl[1].vcn <= vcn)
  252. rl++;
  253. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  254. } else
  255. lcn = LCN_RL_NOT_MAPPED;
  256. /* Successful remap. */
  257. if (lcn >= 0) {
  258. /* Setup buffer head to correct block. */
  259. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  260. + vcn_ofs) >> blocksize_bits;
  261. set_buffer_mapped(bh);
  262. /* Only read initialized data blocks. */
  263. if (iblock < zblock) {
  264. arr[nr++] = bh;
  265. continue;
  266. }
  267. /* Fully non-initialized data block, zero it. */
  268. goto handle_zblock;
  269. }
  270. /* It is a hole, need to zero it. */
  271. if (lcn == LCN_HOLE)
  272. goto handle_hole;
  273. /* If first try and runlist unmapped, map and retry. */
  274. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  275. is_retry = true;
  276. /*
  277. * Attempt to map runlist, dropping lock for
  278. * the duration.
  279. */
  280. up_read(&ni->runlist.lock);
  281. err = ntfs_map_runlist(ni, vcn);
  282. if (likely(!err))
  283. goto lock_retry_remap;
  284. rl = NULL;
  285. } else if (!rl)
  286. up_read(&ni->runlist.lock);
  287. /*
  288. * If buffer is outside the runlist, treat it as a
  289. * hole. This can happen due to concurrent truncate
  290. * for example.
  291. */
  292. if (err == -ENOENT || lcn == LCN_ENOENT) {
  293. err = 0;
  294. goto handle_hole;
  295. }
  296. /* Hard error, zero out region. */
  297. if (!err)
  298. err = -EIO;
  299. bh->b_blocknr = -1;
  300. SetPageError(page);
  301. ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
  302. "attribute type 0x%x, vcn 0x%llx, "
  303. "offset 0x%x because its location on "
  304. "disk could not be determined%s "
  305. "(error code %i).", ni->mft_no,
  306. ni->type, (unsigned long long)vcn,
  307. vcn_ofs, is_retry ? " even after "
  308. "retrying" : "", err);
  309. }
  310. /*
  311. * Either iblock was outside lblock limits or
  312. * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
  313. * of the page and set the buffer uptodate.
  314. */
  315. handle_hole:
  316. bh->b_blocknr = -1UL;
  317. clear_buffer_mapped(bh);
  318. handle_zblock:
  319. zero_user_page(page, i * blocksize, blocksize, KM_USER0);
  320. if (likely(!err))
  321. set_buffer_uptodate(bh);
  322. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  323. /* Release the lock if we took it. */
  324. if (rl)
  325. up_read(&ni->runlist.lock);
  326. /* Check we have at least one buffer ready for i/o. */
  327. if (nr) {
  328. struct buffer_head *tbh;
  329. /* Lock the buffers. */
  330. for (i = 0; i < nr; i++) {
  331. tbh = arr[i];
  332. lock_buffer(tbh);
  333. tbh->b_end_io = ntfs_end_buffer_async_read;
  334. set_buffer_async_read(tbh);
  335. }
  336. /* Finally, start i/o on the buffers. */
  337. for (i = 0; i < nr; i++) {
  338. tbh = arr[i];
  339. if (likely(!buffer_uptodate(tbh)))
  340. submit_bh(READ, tbh);
  341. else
  342. ntfs_end_buffer_async_read(tbh, 1);
  343. }
  344. return 0;
  345. }
  346. /* No i/o was scheduled on any of the buffers. */
  347. if (likely(!PageError(page)))
  348. SetPageUptodate(page);
  349. else /* Signal synchronous i/o error. */
  350. nr = -EIO;
  351. unlock_page(page);
  352. return nr;
  353. }
  354. /**
  355. * ntfs_readpage - fill a @page of a @file with data from the device
  356. * @file: open file to which the page @page belongs or NULL
  357. * @page: page cache page to fill with data
  358. *
  359. * For non-resident attributes, ntfs_readpage() fills the @page of the open
  360. * file @file by calling the ntfs version of the generic block_read_full_page()
  361. * function, ntfs_read_block(), which in turn creates and reads in the buffers
  362. * associated with the page asynchronously.
  363. *
  364. * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
  365. * data from the mft record (which at this stage is most likely in memory) and
  366. * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
  367. * even if the mft record is not cached at this point in time, we need to wait
  368. * for it to be read in before we can do the copy.
  369. *
  370. * Return 0 on success and -errno on error.
  371. */
  372. static int ntfs_readpage(struct file *file, struct page *page)
  373. {
  374. loff_t i_size;
  375. struct inode *vi;
  376. ntfs_inode *ni, *base_ni;
  377. u8 *addr;
  378. ntfs_attr_search_ctx *ctx;
  379. MFT_RECORD *mrec;
  380. unsigned long flags;
  381. u32 attr_len;
  382. int err = 0;
  383. retry_readpage:
  384. BUG_ON(!PageLocked(page));
  385. vi = page->mapping->host;
  386. i_size = i_size_read(vi);
  387. /* Is the page fully outside i_size? (truncate in progress) */
  388. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  389. PAGE_CACHE_SHIFT)) {
  390. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  391. ntfs_debug("Read outside i_size - truncated?");
  392. goto done;
  393. }
  394. /*
  395. * This can potentially happen because we clear PageUptodate() during
  396. * ntfs_writepage() of MstProtected() attributes.
  397. */
  398. if (PageUptodate(page)) {
  399. unlock_page(page);
  400. return 0;
  401. }
  402. ni = NTFS_I(vi);
  403. /*
  404. * Only $DATA attributes can be encrypted and only unnamed $DATA
  405. * attributes can be compressed. Index root can have the flags set but
  406. * this means to create compressed/encrypted files, not that the
  407. * attribute is compressed/encrypted. Note we need to check for
  408. * AT_INDEX_ALLOCATION since this is the type of both directory and
  409. * index inodes.
  410. */
  411. if (ni->type != AT_INDEX_ALLOCATION) {
  412. /* If attribute is encrypted, deny access, just like NT4. */
  413. if (NInoEncrypted(ni)) {
  414. BUG_ON(ni->type != AT_DATA);
  415. err = -EACCES;
  416. goto err_out;
  417. }
  418. /* Compressed data streams are handled in compress.c. */
  419. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  420. BUG_ON(ni->type != AT_DATA);
  421. BUG_ON(ni->name_len);
  422. return ntfs_read_compressed_block(page);
  423. }
  424. }
  425. /* NInoNonResident() == NInoIndexAllocPresent() */
  426. if (NInoNonResident(ni)) {
  427. /* Normal, non-resident data stream. */
  428. return ntfs_read_block(page);
  429. }
  430. /*
  431. * Attribute is resident, implying it is not compressed or encrypted.
  432. * This also means the attribute is smaller than an mft record and
  433. * hence smaller than a page, so can simply zero out any pages with
  434. * index above 0. Note the attribute can actually be marked compressed
  435. * but if it is resident the actual data is not compressed so we are
  436. * ok to ignore the compressed flag here.
  437. */
  438. if (unlikely(page->index > 0)) {
  439. zero_user_page(page, 0, PAGE_CACHE_SIZE, 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. addr = kmap_atomic(page, KM_USER0);
  480. /* Copy the data to the page. */
  481. memcpy(addr, (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(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  486. flush_dcache_page(page);
  487. kunmap_atomic(addr, 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. bh->b_blocknr = -1;
  748. clear_buffer_dirty(bh);
  749. zero_user_page(page, bh_offset(bh), blocksize,
  750. KM_USER0);
  751. set_buffer_uptodate(bh);
  752. err = 0;
  753. continue;
  754. }
  755. /* Failed to map the buffer, even after retrying. */
  756. if (!err)
  757. err = -EIO;
  758. bh->b_blocknr = -1;
  759. ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
  760. "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
  761. "because its location on disk could not be "
  762. "determined%s (error code %i).", ni->mft_no,
  763. ni->type, (unsigned long long)vcn,
  764. vcn_ofs, is_retry ? " even after "
  765. "retrying" : "", err);
  766. break;
  767. } while (block++, (bh = bh->b_this_page) != head);
  768. /* Release the lock if we took it. */
  769. if (rl)
  770. up_read(&ni->runlist.lock);
  771. /* For the error case, need to reset bh to the beginning. */
  772. bh = head;
  773. /* Just an optimization, so ->readpage() is not called later. */
  774. if (unlikely(!PageUptodate(page))) {
  775. int uptodate = 1;
  776. do {
  777. if (!buffer_uptodate(bh)) {
  778. uptodate = 0;
  779. bh = head;
  780. break;
  781. }
  782. } while ((bh = bh->b_this_page) != head);
  783. if (uptodate)
  784. SetPageUptodate(page);
  785. }
  786. /* Setup all mapped, dirty buffers for async write i/o. */
  787. do {
  788. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  789. lock_buffer(bh);
  790. if (test_clear_buffer_dirty(bh)) {
  791. BUG_ON(!buffer_uptodate(bh));
  792. mark_buffer_async_write(bh);
  793. } else
  794. unlock_buffer(bh);
  795. } else if (unlikely(err)) {
  796. /*
  797. * For the error case. The buffer may have been set
  798. * dirty during attachment to a dirty page.
  799. */
  800. if (err != -ENOMEM)
  801. clear_buffer_dirty(bh);
  802. }
  803. } while ((bh = bh->b_this_page) != head);
  804. if (unlikely(err)) {
  805. // TODO: Remove the -EOPNOTSUPP check later on...
  806. if (unlikely(err == -EOPNOTSUPP))
  807. err = 0;
  808. else if (err == -ENOMEM) {
  809. ntfs_warning(vol->sb, "Error allocating memory. "
  810. "Redirtying page so we try again "
  811. "later.");
  812. /*
  813. * Put the page back on mapping->dirty_pages, but
  814. * leave its buffer's dirty state as-is.
  815. */
  816. redirty_page_for_writepage(wbc, page);
  817. err = 0;
  818. } else
  819. SetPageError(page);
  820. }
  821. BUG_ON(PageWriteback(page));
  822. set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
  823. /* Submit the prepared buffers for i/o. */
  824. need_end_writeback = true;
  825. do {
  826. struct buffer_head *next = bh->b_this_page;
  827. if (buffer_async_write(bh)) {
  828. submit_bh(WRITE, bh);
  829. need_end_writeback = false;
  830. }
  831. bh = next;
  832. } while (bh != head);
  833. unlock_page(page);
  834. /* If no i/o was started, need to end_page_writeback(). */
  835. if (unlikely(need_end_writeback))
  836. end_page_writeback(page);
  837. ntfs_debug("Done.");
  838. return err;
  839. }
  840. /**
  841. * ntfs_write_mst_block - write a @page to the backing store
  842. * @page: page cache page to write out
  843. * @wbc: writeback control structure
  844. *
  845. * This function is for writing pages belonging to non-resident, mst protected
  846. * attributes to their backing store. The only supported attributes are index
  847. * allocation and $MFT/$DATA. Both directory inodes and index inodes are
  848. * supported for the index allocation case.
  849. *
  850. * The page must remain locked for the duration of the write because we apply
  851. * the mst fixups, write, and then undo the fixups, so if we were to unlock the
  852. * page before undoing the fixups, any other user of the page will see the
  853. * page contents as corrupt.
  854. *
  855. * We clear the page uptodate flag for the duration of the function to ensure
  856. * exclusion for the $MFT/$DATA case against someone mapping an mft record we
  857. * are about to apply the mst fixups to.
  858. *
  859. * Return 0 on success and -errno on error.
  860. *
  861. * Based on ntfs_write_block(), ntfs_mft_writepage(), and
  862. * write_mft_record_nolock().
  863. */
  864. static int ntfs_write_mst_block(struct page *page,
  865. struct writeback_control *wbc)
  866. {
  867. sector_t block, dblock, rec_block;
  868. struct inode *vi = page->mapping->host;
  869. ntfs_inode *ni = NTFS_I(vi);
  870. ntfs_volume *vol = ni->vol;
  871. u8 *kaddr;
  872. unsigned int rec_size = ni->itype.index.block_size;
  873. ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
  874. struct buffer_head *bh, *head, *tbh, *rec_start_bh;
  875. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  876. runlist_element *rl;
  877. int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
  878. unsigned bh_size, rec_size_bits;
  879. bool sync, is_mft, page_is_dirty, rec_is_dirty;
  880. unsigned char bh_size_bits;
  881. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  882. "0x%lx.", vi->i_ino, ni->type, page->index);
  883. BUG_ON(!NInoNonResident(ni));
  884. BUG_ON(!NInoMstProtected(ni));
  885. is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
  886. /*
  887. * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
  888. * in its page cache were to be marked dirty. However this should
  889. * never happen with the current driver and considering we do not
  890. * handle this case here we do want to BUG(), at least for now.
  891. */
  892. BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
  893. (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
  894. bh_size = vol->sb->s_blocksize;
  895. bh_size_bits = vol->sb->s_blocksize_bits;
  896. max_bhs = PAGE_CACHE_SIZE / bh_size;
  897. BUG_ON(!max_bhs);
  898. BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
  899. /* Were we called for sync purposes? */
  900. sync = (wbc->sync_mode == WB_SYNC_ALL);
  901. /* Make sure we have mapped buffers. */
  902. bh = head = page_buffers(page);
  903. BUG_ON(!bh);
  904. rec_size_bits = ni->itype.index.block_size_bits;
  905. BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
  906. bhs_per_rec = rec_size >> bh_size_bits;
  907. BUG_ON(!bhs_per_rec);
  908. /* The first block in the page. */
  909. rec_block = block = (sector_t)page->index <<
  910. (PAGE_CACHE_SHIFT - bh_size_bits);
  911. /* The first out of bounds block for the data size. */
  912. dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
  913. rl = NULL;
  914. err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
  915. page_is_dirty = rec_is_dirty = false;
  916. rec_start_bh = NULL;
  917. do {
  918. bool is_retry = false;
  919. if (likely(block < rec_block)) {
  920. if (unlikely(block >= dblock)) {
  921. clear_buffer_dirty(bh);
  922. set_buffer_uptodate(bh);
  923. continue;
  924. }
  925. /*
  926. * This block is not the first one in the record. We
  927. * ignore the buffer's dirty state because we could
  928. * have raced with a parallel mark_ntfs_record_dirty().
  929. */
  930. if (!rec_is_dirty)
  931. continue;
  932. if (unlikely(err2)) {
  933. if (err2 != -ENOMEM)
  934. clear_buffer_dirty(bh);
  935. continue;
  936. }
  937. } else /* if (block == rec_block) */ {
  938. BUG_ON(block > rec_block);
  939. /* This block is the first one in the record. */
  940. rec_block += bhs_per_rec;
  941. err2 = 0;
  942. if (unlikely(block >= dblock)) {
  943. clear_buffer_dirty(bh);
  944. continue;
  945. }
  946. if (!buffer_dirty(bh)) {
  947. /* Clean records are not written out. */
  948. rec_is_dirty = false;
  949. continue;
  950. }
  951. rec_is_dirty = true;
  952. rec_start_bh = bh;
  953. }
  954. /* Need to map the buffer if it is not mapped already. */
  955. if (unlikely(!buffer_mapped(bh))) {
  956. VCN vcn;
  957. LCN lcn;
  958. unsigned int vcn_ofs;
  959. bh->b_bdev = vol->sb->s_bdev;
  960. /* Obtain the vcn and offset of the current block. */
  961. vcn = (VCN)block << bh_size_bits;
  962. vcn_ofs = vcn & vol->cluster_size_mask;
  963. vcn >>= vol->cluster_size_bits;
  964. if (!rl) {
  965. lock_retry_remap:
  966. down_read(&ni->runlist.lock);
  967. rl = ni->runlist.rl;
  968. }
  969. if (likely(rl != NULL)) {
  970. /* Seek to element containing target vcn. */
  971. while (rl->length && rl[1].vcn <= vcn)
  972. rl++;
  973. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  974. } else
  975. lcn = LCN_RL_NOT_MAPPED;
  976. /* Successful remap. */
  977. if (likely(lcn >= 0)) {
  978. /* Setup buffer head to correct block. */
  979. bh->b_blocknr = ((lcn <<
  980. vol->cluster_size_bits) +
  981. vcn_ofs) >> bh_size_bits;
  982. set_buffer_mapped(bh);
  983. } else {
  984. /*
  985. * Remap failed. Retry to map the runlist once
  986. * unless we are working on $MFT which always
  987. * has the whole of its runlist in memory.
  988. */
  989. if (!is_mft && !is_retry &&
  990. lcn == LCN_RL_NOT_MAPPED) {
  991. is_retry = true;
  992. /*
  993. * Attempt to map runlist, dropping
  994. * lock for the duration.
  995. */
  996. up_read(&ni->runlist.lock);
  997. err2 = ntfs_map_runlist(ni, vcn);
  998. if (likely(!err2))
  999. goto lock_retry_remap;
  1000. if (err2 == -ENOMEM)
  1001. page_is_dirty = true;
  1002. lcn = err2;
  1003. } else {
  1004. err2 = -EIO;
  1005. if (!rl)
  1006. up_read(&ni->runlist.lock);
  1007. }
  1008. /* Hard error. Abort writing this record. */
  1009. if (!err || err == -ENOMEM)
  1010. err = err2;
  1011. bh->b_blocknr = -1;
  1012. ntfs_error(vol->sb, "Cannot write ntfs record "
  1013. "0x%llx (inode 0x%lx, "
  1014. "attribute type 0x%x) because "
  1015. "its location on disk could "
  1016. "not be determined (error "
  1017. "code %lli).",
  1018. (long long)block <<
  1019. bh_size_bits >>
  1020. vol->mft_record_size_bits,
  1021. ni->mft_no, ni->type,
  1022. (long long)lcn);
  1023. /*
  1024. * If this is not the first buffer, remove the
  1025. * buffers in this record from the list of
  1026. * buffers to write and clear their dirty bit
  1027. * if not error -ENOMEM.
  1028. */
  1029. if (rec_start_bh != bh) {
  1030. while (bhs[--nr_bhs] != rec_start_bh)
  1031. ;
  1032. if (err2 != -ENOMEM) {
  1033. do {
  1034. clear_buffer_dirty(
  1035. rec_start_bh);
  1036. } while ((rec_start_bh =
  1037. rec_start_bh->
  1038. b_this_page) !=
  1039. bh);
  1040. }
  1041. }
  1042. continue;
  1043. }
  1044. }
  1045. BUG_ON(!buffer_uptodate(bh));
  1046. BUG_ON(nr_bhs >= max_bhs);
  1047. bhs[nr_bhs++] = bh;
  1048. } while (block++, (bh = bh->b_this_page) != head);
  1049. if (unlikely(rl))
  1050. up_read(&ni->runlist.lock);
  1051. /* If there were no dirty buffers, we are done. */
  1052. if (!nr_bhs)
  1053. goto done;
  1054. /* Map the page so we can access its contents. */
  1055. kaddr = kmap(page);
  1056. /* Clear the page uptodate flag whilst the mst fixups are applied. */
  1057. BUG_ON(!PageUptodate(page));
  1058. ClearPageUptodate(page);
  1059. for (i = 0; i < nr_bhs; i++) {
  1060. unsigned int ofs;
  1061. /* Skip buffers which are not at the beginning of records. */
  1062. if (i % bhs_per_rec)
  1063. continue;
  1064. tbh = bhs[i];
  1065. ofs = bh_offset(tbh);
  1066. if (is_mft) {
  1067. ntfs_inode *tni;
  1068. unsigned long mft_no;
  1069. /* Get the mft record number. */
  1070. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1071. >> rec_size_bits;
  1072. /* Check whether to write this mft record. */
  1073. tni = NULL;
  1074. if (!ntfs_may_write_mft_record(vol, mft_no,
  1075. (MFT_RECORD*)(kaddr + ofs), &tni)) {
  1076. /*
  1077. * The record should not be written. This
  1078. * means we need to redirty the page before
  1079. * returning.
  1080. */
  1081. page_is_dirty = true;
  1082. /*
  1083. * Remove the buffers in this mft record from
  1084. * the list of buffers to write.
  1085. */
  1086. do {
  1087. bhs[i] = NULL;
  1088. } while (++i % bhs_per_rec);
  1089. continue;
  1090. }
  1091. /*
  1092. * The record should be written. If a locked ntfs
  1093. * inode was returned, add it to the array of locked
  1094. * ntfs inodes.
  1095. */
  1096. if (tni)
  1097. locked_nis[nr_locked_nis++] = tni;
  1098. }
  1099. /* Apply the mst protection fixups. */
  1100. err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
  1101. rec_size);
  1102. if (unlikely(err2)) {
  1103. if (!err || err == -ENOMEM)
  1104. err = -EIO;
  1105. ntfs_error(vol->sb, "Failed to apply mst fixups "
  1106. "(inode 0x%lx, attribute type 0x%x, "
  1107. "page index 0x%lx, page offset 0x%x)!"
  1108. " Unmount and run chkdsk.", vi->i_ino,
  1109. ni->type, page->index, ofs);
  1110. /*
  1111. * Mark all the buffers in this record clean as we do
  1112. * not want to write corrupt data to disk.
  1113. */
  1114. do {
  1115. clear_buffer_dirty(bhs[i]);
  1116. bhs[i] = NULL;
  1117. } while (++i % bhs_per_rec);
  1118. continue;
  1119. }
  1120. nr_recs++;
  1121. }
  1122. /* If no records are to be written out, we are done. */
  1123. if (!nr_recs)
  1124. goto unm_done;
  1125. flush_dcache_page(page);
  1126. /* Lock buffers and start synchronous write i/o on them. */
  1127. for (i = 0; i < nr_bhs; i++) {
  1128. tbh = bhs[i];
  1129. if (!tbh)
  1130. continue;
  1131. if (unlikely(test_set_buffer_locked(tbh)))
  1132. BUG();
  1133. /* The buffer dirty state is now irrelevant, just clean it. */
  1134. clear_buffer_dirty(tbh);
  1135. BUG_ON(!buffer_uptodate(tbh));
  1136. BUG_ON(!buffer_mapped(tbh));
  1137. get_bh(tbh);
  1138. tbh->b_end_io = end_buffer_write_sync;
  1139. submit_bh(WRITE, tbh);
  1140. }
  1141. /* Synchronize the mft mirror now if not @sync. */
  1142. if (is_mft && !sync)
  1143. goto do_mirror;
  1144. do_wait:
  1145. /* Wait on i/o completion of buffers. */
  1146. for (i = 0; i < nr_bhs; i++) {
  1147. tbh = bhs[i];
  1148. if (!tbh)
  1149. continue;
  1150. wait_on_buffer(tbh);
  1151. if (unlikely(!buffer_uptodate(tbh))) {
  1152. ntfs_error(vol->sb, "I/O error while writing ntfs "
  1153. "record buffer (inode 0x%lx, "
  1154. "attribute type 0x%x, page index "
  1155. "0x%lx, page offset 0x%lx)! Unmount "
  1156. "and run chkdsk.", vi->i_ino, ni->type,
  1157. page->index, bh_offset(tbh));
  1158. if (!err || err == -ENOMEM)
  1159. err = -EIO;
  1160. /*
  1161. * Set the buffer uptodate so the page and buffer
  1162. * states do not become out of sync.
  1163. */
  1164. set_buffer_uptodate(tbh);
  1165. }
  1166. }
  1167. /* If @sync, now synchronize the mft mirror. */
  1168. if (is_mft && sync) {
  1169. do_mirror:
  1170. for (i = 0; i < nr_bhs; i++) {
  1171. unsigned long mft_no;
  1172. unsigned int ofs;
  1173. /*
  1174. * Skip buffers which are not at the beginning of
  1175. * records.
  1176. */
  1177. if (i % bhs_per_rec)
  1178. continue;
  1179. tbh = bhs[i];
  1180. /* Skip removed buffers (and hence records). */
  1181. if (!tbh)
  1182. continue;
  1183. ofs = bh_offset(tbh);
  1184. /* Get the mft record number. */
  1185. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1186. >> rec_size_bits;
  1187. if (mft_no < vol->mftmirr_size)
  1188. ntfs_sync_mft_mirror(vol, mft_no,
  1189. (MFT_RECORD*)(kaddr + ofs),
  1190. sync);
  1191. }
  1192. if (!sync)
  1193. goto do_wait;
  1194. }
  1195. /* Remove the mst protection fixups again. */
  1196. for (i = 0; i < nr_bhs; i++) {
  1197. if (!(i % bhs_per_rec)) {
  1198. tbh = bhs[i];
  1199. if (!tbh)
  1200. continue;
  1201. post_write_mst_fixup((NTFS_RECORD*)(kaddr +
  1202. bh_offset(tbh)));
  1203. }
  1204. }
  1205. flush_dcache_page(page);
  1206. unm_done:
  1207. /* Unlock any locked inodes. */
  1208. while (nr_locked_nis-- > 0) {
  1209. ntfs_inode *tni, *base_tni;
  1210. tni = locked_nis[nr_locked_nis];
  1211. /* Get the base inode. */
  1212. mutex_lock(&tni->extent_lock);
  1213. if (tni->nr_extents >= 0)
  1214. base_tni = tni;
  1215. else {
  1216. base_tni = tni->ext.base_ntfs_ino;
  1217. BUG_ON(!base_tni);
  1218. }
  1219. mutex_unlock(&tni->extent_lock);
  1220. ntfs_debug("Unlocking %s inode 0x%lx.",
  1221. tni == base_tni ? "base" : "extent",
  1222. tni->mft_no);
  1223. mutex_unlock(&tni->mrec_lock);
  1224. atomic_dec(&tni->count);
  1225. iput(VFS_I(base_tni));
  1226. }
  1227. SetPageUptodate(page);
  1228. kunmap(page);
  1229. done:
  1230. if (unlikely(err && err != -ENOMEM)) {
  1231. /*
  1232. * Set page error if there is only one ntfs record in the page.
  1233. * Otherwise we would loose per-record granularity.
  1234. */
  1235. if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
  1236. SetPageError(page);
  1237. NVolSetErrors(vol);
  1238. }
  1239. if (page_is_dirty) {
  1240. ntfs_debug("Page still contains one or more dirty ntfs "
  1241. "records. Redirtying the page starting at "
  1242. "record 0x%lx.", page->index <<
  1243. (PAGE_CACHE_SHIFT - rec_size_bits));
  1244. redirty_page_for_writepage(wbc, page);
  1245. unlock_page(page);
  1246. } else {
  1247. /*
  1248. * Keep the VM happy. This must be done otherwise the
  1249. * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
  1250. * the page is clean.
  1251. */
  1252. BUG_ON(PageWriteback(page));
  1253. set_page_writeback(page);
  1254. unlock_page(page);
  1255. end_page_writeback(page);
  1256. }
  1257. if (likely(!err))
  1258. ntfs_debug("Done.");
  1259. return err;
  1260. }
  1261. /**
  1262. * ntfs_writepage - write a @page to the backing store
  1263. * @page: page cache page to write out
  1264. * @wbc: writeback control structure
  1265. *
  1266. * This is called from the VM when it wants to have a dirty ntfs page cache
  1267. * page cleaned. The VM has already locked the page and marked it clean.
  1268. *
  1269. * For non-resident attributes, ntfs_writepage() writes the @page by calling
  1270. * the ntfs version of the generic block_write_full_page() function,
  1271. * ntfs_write_block(), which in turn if necessary creates and writes the
  1272. * buffers associated with the page asynchronously.
  1273. *
  1274. * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
  1275. * the data to the mft record (which at this stage is most likely in memory).
  1276. * The mft record is then marked dirty and written out asynchronously via the
  1277. * vfs inode dirty code path for the inode the mft record belongs to or via the
  1278. * vm page dirty code path for the page the mft record is in.
  1279. *
  1280. * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
  1281. *
  1282. * Return 0 on success and -errno on error.
  1283. */
  1284. static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
  1285. {
  1286. loff_t i_size;
  1287. struct inode *vi = page->mapping->host;
  1288. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1289. char *addr;
  1290. ntfs_attr_search_ctx *ctx = NULL;
  1291. MFT_RECORD *m = NULL;
  1292. u32 attr_len;
  1293. int err;
  1294. retry_writepage:
  1295. BUG_ON(!PageLocked(page));
  1296. i_size = i_size_read(vi);
  1297. /* Is the page fully outside i_size? (truncate in progress) */
  1298. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  1299. PAGE_CACHE_SHIFT)) {
  1300. /*
  1301. * The page may have dirty, unmapped buffers. Make them
  1302. * freeable here, so the page does not leak.
  1303. */
  1304. block_invalidatepage(page, 0);
  1305. unlock_page(page);
  1306. ntfs_debug("Write outside i_size - truncated?");
  1307. return 0;
  1308. }
  1309. /*
  1310. * Only $DATA attributes can be encrypted and only unnamed $DATA
  1311. * attributes can be compressed. Index root can have the flags set but
  1312. * this means to create compressed/encrypted files, not that the
  1313. * attribute is compressed/encrypted. Note we need to check for
  1314. * AT_INDEX_ALLOCATION since this is the type of both directory and
  1315. * index inodes.
  1316. */
  1317. if (ni->type != AT_INDEX_ALLOCATION) {
  1318. /* If file is encrypted, deny access, just like NT4. */
  1319. if (NInoEncrypted(ni)) {
  1320. unlock_page(page);
  1321. BUG_ON(ni->type != AT_DATA);
  1322. ntfs_debug("Denying write access to encrypted file.");
  1323. return -EACCES;
  1324. }
  1325. /* Compressed data streams are handled in compress.c. */
  1326. if (NInoNonResident(ni) && NInoCompressed(ni)) {
  1327. BUG_ON(ni->type != AT_DATA);
  1328. BUG_ON(ni->name_len);
  1329. // TODO: Implement and replace this with
  1330. // return ntfs_write_compressed_block(page);
  1331. unlock_page(page);
  1332. ntfs_error(vi->i_sb, "Writing to compressed files is "
  1333. "not supported yet. Sorry.");
  1334. return -EOPNOTSUPP;
  1335. }
  1336. // TODO: Implement and remove this check.
  1337. if (NInoNonResident(ni) && NInoSparse(ni)) {
  1338. unlock_page(page);
  1339. ntfs_error(vi->i_sb, "Writing to sparse files is not "
  1340. "supported yet. Sorry.");
  1341. return -EOPNOTSUPP;
  1342. }
  1343. }
  1344. /* NInoNonResident() == NInoIndexAllocPresent() */
  1345. if (NInoNonResident(ni)) {
  1346. /* We have to zero every time due to mmap-at-end-of-file. */
  1347. if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
  1348. /* The page straddles i_size. */
  1349. unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
  1350. zero_user_page(page, ofs, PAGE_CACHE_SIZE - ofs,
  1351. KM_USER0);
  1352. }
  1353. /* Handle mst protected attributes. */
  1354. if (NInoMstProtected(ni))
  1355. return ntfs_write_mst_block(page, wbc);
  1356. /* Normal, non-resident data stream. */
  1357. return ntfs_write_block(page, wbc);
  1358. }
  1359. /*
  1360. * Attribute is resident, implying it is not compressed, encrypted, or
  1361. * mst protected. This also means the attribute is smaller than an mft
  1362. * record and hence smaller than a page, so can simply return error on
  1363. * any pages with index above 0. Note the attribute can actually be
  1364. * marked compressed but if it is resident the actual data is not
  1365. * compressed so we are ok to ignore the compressed flag here.
  1366. */
  1367. BUG_ON(page_has_buffers(page));
  1368. BUG_ON(!PageUptodate(page));
  1369. if (unlikely(page->index > 0)) {
  1370. ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
  1371. "Aborting write.", page->index);
  1372. BUG_ON(PageWriteback(page));
  1373. set_page_writeback(page);
  1374. unlock_page(page);
  1375. end_page_writeback(page);
  1376. return -EIO;
  1377. }
  1378. if (!NInoAttr(ni))
  1379. base_ni = ni;
  1380. else
  1381. base_ni = ni->ext.base_ntfs_ino;
  1382. /* Map, pin, and lock the mft record. */
  1383. m = map_mft_record(base_ni);
  1384. if (IS_ERR(m)) {
  1385. err = PTR_ERR(m);
  1386. m = NULL;
  1387. ctx = NULL;
  1388. goto err_out;
  1389. }
  1390. /*
  1391. * If a parallel write made the attribute non-resident, drop the mft
  1392. * record and retry the writepage.
  1393. */
  1394. if (unlikely(NInoNonResident(ni))) {
  1395. unmap_mft_record(base_ni);
  1396. goto retry_writepage;
  1397. }
  1398. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1399. if (unlikely(!ctx)) {
  1400. err = -ENOMEM;
  1401. goto err_out;
  1402. }
  1403. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1404. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1405. if (unlikely(err))
  1406. goto err_out;
  1407. /*
  1408. * Keep the VM happy. This must be done otherwise the radix-tree tag
  1409. * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
  1410. */
  1411. BUG_ON(PageWriteback(page));
  1412. set_page_writeback(page);
  1413. unlock_page(page);
  1414. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  1415. i_size = i_size_read(vi);
  1416. if (unlikely(attr_len > i_size)) {
  1417. /* Race with shrinking truncate or a failed truncate. */
  1418. attr_len = i_size;
  1419. /*
  1420. * If the truncate failed, fix it up now. If a concurrent
  1421. * truncate, we do its job, so it does not have to do anything.
  1422. */
  1423. err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
  1424. attr_len);
  1425. /* Shrinking cannot fail. */
  1426. BUG_ON(err);
  1427. }
  1428. addr = kmap_atomic(page, KM_USER0);
  1429. /* Copy the data from the page to the mft record. */
  1430. memcpy((u8*)ctx->attr +
  1431. le16_to_cpu(ctx->attr->data.resident.value_offset),
  1432. addr, attr_len);
  1433. /* Zero out of bounds area in the page cache page. */
  1434. memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1435. kunmap_atomic(addr, KM_USER0);
  1436. flush_dcache_page(page);
  1437. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1438. /* We are done with the page. */
  1439. end_page_writeback(page);
  1440. /* Finally, mark the mft record dirty, so it gets written back. */
  1441. mark_mft_record_dirty(ctx->ntfs_ino);
  1442. ntfs_attr_put_search_ctx(ctx);
  1443. unmap_mft_record(base_ni);
  1444. return 0;
  1445. err_out:
  1446. if (err == -ENOMEM) {
  1447. ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
  1448. "page so we try again later.");
  1449. /*
  1450. * Put the page back on mapping->dirty_pages, but leave its
  1451. * buffers' dirty state as-is.
  1452. */
  1453. redirty_page_for_writepage(wbc, page);
  1454. err = 0;
  1455. } else {
  1456. ntfs_error(vi->i_sb, "Resident attribute write failed with "
  1457. "error %i.", err);
  1458. SetPageError(page);
  1459. NVolSetErrors(ni->vol);
  1460. }
  1461. unlock_page(page);
  1462. if (ctx)
  1463. ntfs_attr_put_search_ctx(ctx);
  1464. if (m)
  1465. unmap_mft_record(base_ni);
  1466. return err;
  1467. }
  1468. #endif /* NTFS_RW */
  1469. /**
  1470. * ntfs_aops - general address space operations for inodes and attributes
  1471. */
  1472. const struct address_space_operations ntfs_aops = {
  1473. .readpage = ntfs_readpage, /* Fill page with data. */
  1474. .sync_page = block_sync_page, /* Currently, just unplugs the
  1475. disk request queue. */
  1476. #ifdef NTFS_RW
  1477. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  1478. #endif /* NTFS_RW */
  1479. .migratepage = buffer_migrate_page, /* Move a page cache page from
  1480. one physical page to an
  1481. other. */
  1482. };
  1483. /**
  1484. * ntfs_mst_aops - general address space operations for mst protecteed inodes
  1485. * and attributes
  1486. */
  1487. const struct address_space_operations ntfs_mst_aops = {
  1488. .readpage = ntfs_readpage, /* Fill page with data. */
  1489. .sync_page = block_sync_page, /* Currently, just unplugs the
  1490. disk request queue. */
  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. };
  1501. #ifdef NTFS_RW
  1502. /**
  1503. * mark_ntfs_record_dirty - mark an ntfs record dirty
  1504. * @page: page containing the ntfs record to mark dirty
  1505. * @ofs: byte offset within @page at which the ntfs record begins
  1506. *
  1507. * Set the buffers and the page in which the ntfs record is located dirty.
  1508. *
  1509. * The latter also marks the vfs inode the ntfs record belongs to dirty
  1510. * (I_DIRTY_PAGES only).
  1511. *
  1512. * If the page does not have buffers, we create them and set them uptodate.
  1513. * The page may not be locked which is why we need to handle the buffers under
  1514. * the mapping->private_lock. Once the buffers are marked dirty we no longer
  1515. * need the lock since try_to_free_buffers() does not free dirty buffers.
  1516. */
  1517. void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
  1518. struct address_space *mapping = page->mapping;
  1519. ntfs_inode *ni = NTFS_I(mapping->host);
  1520. struct buffer_head *bh, *head, *buffers_to_free = NULL;
  1521. unsigned int end, bh_size, bh_ofs;
  1522. BUG_ON(!PageUptodate(page));
  1523. end = ofs + ni->itype.index.block_size;
  1524. bh_size = VFS_I(ni)->i_sb->s_blocksize;
  1525. spin_lock(&mapping->private_lock);
  1526. if (unlikely(!page_has_buffers(page))) {
  1527. spin_unlock(&mapping->private_lock);
  1528. bh = head = alloc_page_buffers(page, bh_size, 1);
  1529. spin_lock(&mapping->private_lock);
  1530. if (likely(!page_has_buffers(page))) {
  1531. struct buffer_head *tail;
  1532. do {
  1533. set_buffer_uptodate(bh);
  1534. tail = bh;
  1535. bh = bh->b_this_page;
  1536. } while (bh);
  1537. tail->b_this_page = head;
  1538. attach_page_buffers(page, head);
  1539. } else
  1540. buffers_to_free = bh;
  1541. }
  1542. bh = head = page_buffers(page);
  1543. BUG_ON(!bh);
  1544. do {
  1545. bh_ofs = bh_offset(bh);
  1546. if (bh_ofs + bh_size <= ofs)
  1547. continue;
  1548. if (unlikely(bh_ofs >= end))
  1549. break;
  1550. set_buffer_dirty(bh);
  1551. } while ((bh = bh->b_this_page) != head);
  1552. spin_unlock(&mapping->private_lock);
  1553. __set_page_dirty_nobuffers(page);
  1554. if (unlikely(buffers_to_free)) {
  1555. do {
  1556. bh = buffers_to_free->b_this_page;
  1557. free_buffer_head(buffers_to_free);
  1558. buffers_to_free = bh;
  1559. } while (buffers_to_free);
  1560. }
  1561. }
  1562. #endif /* NTFS_RW */