aops.c 48 KB

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