aops.c 52 KB

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