aops.c 52 KB

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