aops.c 52 KB

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