aops.c 48 KB

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