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