aops.c 52 KB

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