aops.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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-2005 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/mm.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/swap.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/writeback.h>
  29. #include "aops.h"
  30. #include "attrib.h"
  31. #include "debug.h"
  32. #include "inode.h"
  33. #include "mft.h"
  34. #include "runlist.h"
  35. #include "types.h"
  36. #include "ntfs.h"
  37. /**
  38. * ntfs_end_buffer_async_read - async io completion for reading attributes
  39. * @bh: buffer head on which io is completed
  40. * @uptodate: whether @bh is now uptodate or not
  41. *
  42. * Asynchronous I/O completion handler for reading pages belonging to the
  43. * attribute address space of an inode. The inodes can either be files or
  44. * directories or they can be fake inodes describing some attribute.
  45. *
  46. * If NInoMstProtected(), perform the post read mst fixups when all IO on the
  47. * page has been completed and mark the page uptodate or set the error bit on
  48. * the page. To determine the size of the records that need fixing up, we
  49. * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
  50. * record size, and index_block_size_bits, to the log(base 2) of the ntfs
  51. * record size.
  52. */
  53. static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  54. {
  55. static DEFINE_SPINLOCK(page_uptodate_lock);
  56. unsigned long flags;
  57. struct buffer_head *tmp;
  58. struct page *page;
  59. ntfs_inode *ni;
  60. int page_uptodate = 1;
  61. page = bh->b_page;
  62. ni = NTFS_I(page->mapping->host);
  63. if (likely(uptodate)) {
  64. s64 file_ofs, initialized_size;
  65. set_buffer_uptodate(bh);
  66. file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
  67. bh_offset(bh);
  68. read_lock_irqsave(&ni->size_lock, flags);
  69. initialized_size = ni->initialized_size;
  70. read_unlock_irqrestore(&ni->size_lock, flags);
  71. /* Check for the current buffer head overflowing. */
  72. if (file_ofs + bh->b_size > initialized_size) {
  73. char *addr;
  74. int ofs = 0;
  75. if (file_ofs < initialized_size)
  76. ofs = initialized_size - file_ofs;
  77. addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  78. memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
  79. flush_dcache_page(page);
  80. kunmap_atomic(addr, KM_BIO_SRC_IRQ);
  81. }
  82. } else {
  83. clear_buffer_uptodate(bh);
  84. ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
  85. (unsigned long long)bh->b_blocknr);
  86. SetPageError(page);
  87. }
  88. spin_lock_irqsave(&page_uptodate_lock, flags);
  89. clear_buffer_async_read(bh);
  90. unlock_buffer(bh);
  91. tmp = bh;
  92. do {
  93. if (!buffer_uptodate(tmp))
  94. page_uptodate = 0;
  95. if (buffer_async_read(tmp)) {
  96. if (likely(buffer_locked(tmp)))
  97. goto still_busy;
  98. /* Async buffers must be locked. */
  99. BUG();
  100. }
  101. tmp = tmp->b_this_page;
  102. } while (tmp != bh);
  103. spin_unlock_irqrestore(&page_uptodate_lock, flags);
  104. /*
  105. * If none of the buffers had errors then we can set the page uptodate,
  106. * but we first have to perform the post read mst fixups, if the
  107. * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
  108. * Note we ignore fixup errors as those are detected when
  109. * map_mft_record() is called which gives us per record granularity
  110. * rather than per page granularity.
  111. */
  112. if (!NInoMstProtected(ni)) {
  113. if (likely(page_uptodate && !PageError(page)))
  114. SetPageUptodate(page);
  115. } else {
  116. char *addr;
  117. unsigned int i, recs;
  118. u32 rec_size;
  119. rec_size = ni->itype.index.block_size;
  120. recs = PAGE_CACHE_SIZE / rec_size;
  121. /* Should have been verified before we got here... */
  122. BUG_ON(!recs);
  123. addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
  124. for (i = 0; i < recs; i++)
  125. post_read_mst_fixup((NTFS_RECORD*)(addr +
  126. i * rec_size), rec_size);
  127. flush_dcache_page(page);
  128. kunmap_atomic(addr, KM_BIO_SRC_IRQ);
  129. if (likely(page_uptodate && !PageError(page)))
  130. SetPageUptodate(page);
  131. }
  132. unlock_page(page);
  133. return;
  134. still_busy:
  135. spin_unlock_irqrestore(&page_uptodate_lock, flags);
  136. return;
  137. }
  138. /**
  139. * ntfs_read_block - fill a @page of an address space with data
  140. * @page: page cache page to fill with data
  141. *
  142. * Fill the page @page of the address space belonging to the @page->host inode.
  143. * We read each buffer asynchronously and when all buffers are read in, our io
  144. * completion handler ntfs_end_buffer_read_async(), if required, automatically
  145. * applies the mst fixups to the page before finally marking it uptodate and
  146. * unlocking it.
  147. *
  148. * We only enforce allocated_size limit because i_size is checked for in
  149. * generic_file_read().
  150. *
  151. * Return 0 on success and -errno on error.
  152. *
  153. * Contains an adapted version of fs/buffer.c::block_read_full_page().
  154. */
  155. static int ntfs_read_block(struct page *page)
  156. {
  157. VCN vcn;
  158. LCN lcn;
  159. ntfs_inode *ni;
  160. ntfs_volume *vol;
  161. runlist_element *rl;
  162. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  163. sector_t iblock, lblock, zblock;
  164. unsigned long flags;
  165. unsigned int blocksize, vcn_ofs;
  166. int i, nr;
  167. unsigned char blocksize_bits;
  168. ni = NTFS_I(page->mapping->host);
  169. vol = ni->vol;
  170. /* $MFT/$DATA must have its complete runlist in memory at all times. */
  171. BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
  172. blocksize_bits = VFS_I(ni)->i_blkbits;
  173. blocksize = 1 << blocksize_bits;
  174. if (!page_has_buffers(page))
  175. create_empty_buffers(page, blocksize, 0);
  176. bh = head = page_buffers(page);
  177. if (unlikely(!bh)) {
  178. unlock_page(page);
  179. return -ENOMEM;
  180. }
  181. iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  182. read_lock_irqsave(&ni->size_lock, flags);
  183. lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
  184. zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
  185. read_unlock_irqrestore(&ni->size_lock, flags);
  186. /* Loop through all the buffers in the page. */
  187. rl = NULL;
  188. nr = i = 0;
  189. do {
  190. u8 *kaddr;
  191. if (unlikely(buffer_uptodate(bh)))
  192. continue;
  193. if (unlikely(buffer_mapped(bh))) {
  194. arr[nr++] = bh;
  195. continue;
  196. }
  197. bh->b_bdev = vol->sb->s_bdev;
  198. /* Is the block within the allowed limits? */
  199. if (iblock < lblock) {
  200. BOOL is_retry = FALSE;
  201. /* Convert iblock into corresponding vcn and offset. */
  202. vcn = (VCN)iblock << blocksize_bits >>
  203. vol->cluster_size_bits;
  204. vcn_ofs = ((VCN)iblock << blocksize_bits) &
  205. vol->cluster_size_mask;
  206. if (!rl) {
  207. lock_retry_remap:
  208. down_read(&ni->runlist.lock);
  209. rl = ni->runlist.rl;
  210. }
  211. if (likely(rl != NULL)) {
  212. /* Seek to element containing target vcn. */
  213. while (rl->length && rl[1].vcn <= vcn)
  214. rl++;
  215. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  216. } else
  217. lcn = LCN_RL_NOT_MAPPED;
  218. /* Successful remap. */
  219. if (lcn >= 0) {
  220. /* Setup buffer head to correct block. */
  221. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  222. + vcn_ofs) >> blocksize_bits;
  223. set_buffer_mapped(bh);
  224. /* Only read initialized data blocks. */
  225. if (iblock < zblock) {
  226. arr[nr++] = bh;
  227. continue;
  228. }
  229. /* Fully non-initialized data block, zero it. */
  230. goto handle_zblock;
  231. }
  232. /* It is a hole, need to zero it. */
  233. if (lcn == LCN_HOLE)
  234. goto handle_hole;
  235. /* If first try and runlist unmapped, map and retry. */
  236. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  237. int err;
  238. is_retry = TRUE;
  239. /*
  240. * Attempt to map runlist, dropping lock for
  241. * the duration.
  242. */
  243. up_read(&ni->runlist.lock);
  244. err = ntfs_map_runlist(ni, vcn);
  245. if (likely(!err))
  246. goto lock_retry_remap;
  247. rl = NULL;
  248. lcn = err;
  249. }
  250. /* Hard error, zero out region. */
  251. bh->b_blocknr = -1;
  252. SetPageError(page);
  253. ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
  254. "attribute type 0x%x, vcn 0x%llx, "
  255. "offset 0x%x because its location on "
  256. "disk could not be determined%s "
  257. "(error code %lli).", ni->mft_no,
  258. ni->type, (unsigned long long)vcn,
  259. vcn_ofs, is_retry ? " even after "
  260. "retrying" : "", (long long)lcn);
  261. }
  262. /*
  263. * Either iblock was outside lblock limits or
  264. * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
  265. * of the page and set the buffer uptodate.
  266. */
  267. handle_hole:
  268. bh->b_blocknr = -1UL;
  269. clear_buffer_mapped(bh);
  270. handle_zblock:
  271. kaddr = kmap_atomic(page, KM_USER0);
  272. memset(kaddr + i * blocksize, 0, blocksize);
  273. flush_dcache_page(page);
  274. kunmap_atomic(kaddr, KM_USER0);
  275. set_buffer_uptodate(bh);
  276. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  277. /* Release the lock if we took it. */
  278. if (rl)
  279. up_read(&ni->runlist.lock);
  280. /* Check we have at least one buffer ready for i/o. */
  281. if (nr) {
  282. struct buffer_head *tbh;
  283. /* Lock the buffers. */
  284. for (i = 0; i < nr; i++) {
  285. tbh = arr[i];
  286. lock_buffer(tbh);
  287. tbh->b_end_io = ntfs_end_buffer_async_read;
  288. set_buffer_async_read(tbh);
  289. }
  290. /* Finally, start i/o on the buffers. */
  291. for (i = 0; i < nr; i++) {
  292. tbh = arr[i];
  293. if (likely(!buffer_uptodate(tbh)))
  294. submit_bh(READ, tbh);
  295. else
  296. ntfs_end_buffer_async_read(tbh, 1);
  297. }
  298. return 0;
  299. }
  300. /* No i/o was scheduled on any of the buffers. */
  301. if (likely(!PageError(page)))
  302. SetPageUptodate(page);
  303. else /* Signal synchronous i/o error. */
  304. nr = -EIO;
  305. unlock_page(page);
  306. return nr;
  307. }
  308. /**
  309. * ntfs_readpage - fill a @page of a @file with data from the device
  310. * @file: open file to which the page @page belongs or NULL
  311. * @page: page cache page to fill with data
  312. *
  313. * For non-resident attributes, ntfs_readpage() fills the @page of the open
  314. * file @file by calling the ntfs version of the generic block_read_full_page()
  315. * function, ntfs_read_block(), which in turn creates and reads in the buffers
  316. * associated with the page asynchronously.
  317. *
  318. * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
  319. * data from the mft record (which at this stage is most likely in memory) and
  320. * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
  321. * even if the mft record is not cached at this point in time, we need to wait
  322. * for it to be read in before we can do the copy.
  323. *
  324. * Return 0 on success and -errno on error.
  325. */
  326. static int ntfs_readpage(struct file *file, struct page *page)
  327. {
  328. ntfs_inode *ni, *base_ni;
  329. u8 *kaddr;
  330. ntfs_attr_search_ctx *ctx;
  331. MFT_RECORD *mrec;
  332. unsigned long flags;
  333. u32 attr_len;
  334. int err = 0;
  335. retry_readpage:
  336. BUG_ON(!PageLocked(page));
  337. /*
  338. * This can potentially happen because we clear PageUptodate() during
  339. * ntfs_writepage() of MstProtected() attributes.
  340. */
  341. if (PageUptodate(page)) {
  342. unlock_page(page);
  343. return 0;
  344. }
  345. ni = NTFS_I(page->mapping->host);
  346. /* NInoNonResident() == NInoIndexAllocPresent() */
  347. if (NInoNonResident(ni)) {
  348. /*
  349. * Only unnamed $DATA attributes can be compressed or
  350. * encrypted.
  351. */
  352. if (ni->type == AT_DATA && !ni->name_len) {
  353. /* If file is encrypted, deny access, just like NT4. */
  354. if (NInoEncrypted(ni)) {
  355. err = -EACCES;
  356. goto err_out;
  357. }
  358. /* Compressed data streams are handled in compress.c. */
  359. if (NInoCompressed(ni))
  360. return ntfs_read_compressed_block(page);
  361. }
  362. /* Normal data stream. */
  363. return ntfs_read_block(page);
  364. }
  365. /*
  366. * Attribute is resident, implying it is not compressed or encrypted.
  367. * This also means the attribute is smaller than an mft record and
  368. * hence smaller than a page, so can simply zero out any pages with
  369. * index above 0.
  370. */
  371. if (unlikely(page->index > 0)) {
  372. kaddr = kmap_atomic(page, KM_USER0);
  373. memset(kaddr, 0, PAGE_CACHE_SIZE);
  374. flush_dcache_page(page);
  375. kunmap_atomic(kaddr, KM_USER0);
  376. goto done;
  377. }
  378. if (!NInoAttr(ni))
  379. base_ni = ni;
  380. else
  381. base_ni = ni->ext.base_ntfs_ino;
  382. /* Map, pin, and lock the mft record. */
  383. mrec = map_mft_record(base_ni);
  384. if (IS_ERR(mrec)) {
  385. err = PTR_ERR(mrec);
  386. goto err_out;
  387. }
  388. /*
  389. * If a parallel write made the attribute non-resident, drop the mft
  390. * record and retry the readpage.
  391. */
  392. if (unlikely(NInoNonResident(ni))) {
  393. unmap_mft_record(base_ni);
  394. goto retry_readpage;
  395. }
  396. ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
  397. if (unlikely(!ctx)) {
  398. err = -ENOMEM;
  399. goto unm_err_out;
  400. }
  401. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  402. CASE_SENSITIVE, 0, NULL, 0, ctx);
  403. if (unlikely(err))
  404. goto put_unm_err_out;
  405. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  406. read_lock_irqsave(&ni->size_lock, flags);
  407. if (unlikely(attr_len > ni->initialized_size))
  408. attr_len = ni->initialized_size;
  409. read_unlock_irqrestore(&ni->size_lock, flags);
  410. kaddr = kmap_atomic(page, KM_USER0);
  411. /* Copy the data to the page. */
  412. memcpy(kaddr, (u8*)ctx->attr +
  413. le16_to_cpu(ctx->attr->data.resident.value_offset),
  414. attr_len);
  415. /* Zero the remainder of the page. */
  416. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  417. flush_dcache_page(page);
  418. kunmap_atomic(kaddr, KM_USER0);
  419. put_unm_err_out:
  420. ntfs_attr_put_search_ctx(ctx);
  421. unm_err_out:
  422. unmap_mft_record(base_ni);
  423. done:
  424. SetPageUptodate(page);
  425. err_out:
  426. unlock_page(page);
  427. return err;
  428. }
  429. #ifdef NTFS_RW
  430. /**
  431. * ntfs_write_block - write a @page to the backing store
  432. * @page: page cache page to write out
  433. * @wbc: writeback control structure
  434. *
  435. * This function is for writing pages belonging to non-resident, non-mst
  436. * protected attributes to their backing store.
  437. *
  438. * For a page with buffers, map and write the dirty buffers asynchronously
  439. * under page writeback. For a page without buffers, create buffers for the
  440. * page, then proceed as above.
  441. *
  442. * If a page doesn't have buffers the page dirty state is definitive. If a page
  443. * does have buffers, the page dirty state is just a hint, and the buffer dirty
  444. * state is definitive. (A hint which has rules: dirty buffers against a clean
  445. * page is illegal. Other combinations are legal and need to be handled. In
  446. * particular a dirty page containing clean buffers for example.)
  447. *
  448. * Return 0 on success and -errno on error.
  449. *
  450. * Based on ntfs_read_block() and __block_write_full_page().
  451. */
  452. static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
  453. {
  454. VCN vcn;
  455. LCN lcn;
  456. s64 initialized_size;
  457. loff_t i_size;
  458. sector_t block, dblock, iblock;
  459. struct inode *vi;
  460. ntfs_inode *ni;
  461. ntfs_volume *vol;
  462. runlist_element *rl;
  463. struct buffer_head *bh, *head;
  464. unsigned long flags;
  465. unsigned int blocksize, vcn_ofs;
  466. int err;
  467. BOOL need_end_writeback;
  468. unsigned char blocksize_bits;
  469. vi = page->mapping->host;
  470. ni = NTFS_I(vi);
  471. vol = ni->vol;
  472. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  473. "0x%lx.", ni->mft_no, ni->type, page->index);
  474. BUG_ON(!NInoNonResident(ni));
  475. BUG_ON(NInoMstProtected(ni));
  476. blocksize_bits = vi->i_blkbits;
  477. blocksize = 1 << blocksize_bits;
  478. if (!page_has_buffers(page)) {
  479. BUG_ON(!PageUptodate(page));
  480. create_empty_buffers(page, blocksize,
  481. (1 << BH_Uptodate) | (1 << BH_Dirty));
  482. }
  483. bh = head = page_buffers(page);
  484. if (unlikely(!bh)) {
  485. ntfs_warning(vol->sb, "Error allocating page buffers. "
  486. "Redirtying page so we try again later.");
  487. /*
  488. * Put the page back on mapping->dirty_pages, but leave its
  489. * buffer's dirty state as-is.
  490. */
  491. redirty_page_for_writepage(wbc, page);
  492. unlock_page(page);
  493. return 0;
  494. }
  495. /* NOTE: Different naming scheme to ntfs_read_block()! */
  496. /* The first block in the page. */
  497. block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  498. read_lock_irqsave(&ni->size_lock, flags);
  499. i_size = i_size_read(vi);
  500. initialized_size = ni->initialized_size;
  501. read_unlock_irqrestore(&ni->size_lock, flags);
  502. /* The first out of bounds block for the data size. */
  503. dblock = (i_size + blocksize - 1) >> blocksize_bits;
  504. /* The last (fully or partially) initialized block. */
  505. iblock = initialized_size >> blocksize_bits;
  506. /*
  507. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  508. * here, and the (potentially unmapped) buffers may become dirty at
  509. * any time. If a buffer becomes dirty here after we've inspected it
  510. * then we just miss that fact, and the page stays dirty.
  511. *
  512. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  513. * handle that here by just cleaning them.
  514. */
  515. /*
  516. * Loop through all the buffers in the page, mapping all the dirty
  517. * buffers to disk addresses and handling any aliases from the
  518. * underlying block device's mapping.
  519. */
  520. rl = NULL;
  521. err = 0;
  522. do {
  523. BOOL is_retry = FALSE;
  524. if (unlikely(block >= dblock)) {
  525. /*
  526. * Mapped buffers outside i_size will occur, because
  527. * this page can be outside i_size when there is a
  528. * truncate in progress. The contents of such buffers
  529. * were zeroed by ntfs_writepage().
  530. *
  531. * FIXME: What about the small race window where
  532. * ntfs_writepage() has not done any clearing because
  533. * the page was within i_size but before we get here,
  534. * vmtruncate() modifies i_size?
  535. */
  536. clear_buffer_dirty(bh);
  537. set_buffer_uptodate(bh);
  538. continue;
  539. }
  540. /* Clean buffers are not written out, so no need to map them. */
  541. if (!buffer_dirty(bh))
  542. continue;
  543. /* Make sure we have enough initialized size. */
  544. if (unlikely((block >= iblock) &&
  545. (initialized_size < i_size))) {
  546. /*
  547. * If this page is fully outside initialized size, zero
  548. * out all pages between the current initialized size
  549. * and the current page. Just use ntfs_readpage() to do
  550. * the zeroing transparently.
  551. */
  552. if (block > iblock) {
  553. // TODO:
  554. // For each page do:
  555. // - read_cache_page()
  556. // Again for each page do:
  557. // - wait_on_page_locked()
  558. // - Check (PageUptodate(page) &&
  559. // !PageError(page))
  560. // Update initialized size in the attribute and
  561. // in the inode.
  562. // Again, for each page do:
  563. // __set_page_dirty_buffers();
  564. // page_cache_release()
  565. // We don't need to wait on the writes.
  566. // Update iblock.
  567. }
  568. /*
  569. * The current page straddles initialized size. Zero
  570. * all non-uptodate buffers and set them uptodate (and
  571. * dirty?). Note, there aren't any non-uptodate buffers
  572. * if the page is uptodate.
  573. * FIXME: For an uptodate page, the buffers may need to
  574. * be written out because they were not initialized on
  575. * disk before.
  576. */
  577. if (!PageUptodate(page)) {
  578. // TODO:
  579. // Zero any non-uptodate buffers up to i_size.
  580. // Set them uptodate and dirty.
  581. }
  582. // TODO:
  583. // Update initialized size in the attribute and in the
  584. // inode (up to i_size).
  585. // Update iblock.
  586. // FIXME: This is inefficient. Try to batch the two
  587. // size changes to happen in one go.
  588. ntfs_error(vol->sb, "Writing beyond initialized size "
  589. "is not supported yet. Sorry.");
  590. err = -EOPNOTSUPP;
  591. break;
  592. // Do NOT set_buffer_new() BUT DO clear buffer range
  593. // outside write request range.
  594. // set_buffer_uptodate() on complete buffers as well as
  595. // set_buffer_dirty().
  596. }
  597. /* No need to map buffers that are already mapped. */
  598. if (buffer_mapped(bh))
  599. continue;
  600. /* Unmapped, dirty buffer. Need to map it. */
  601. bh->b_bdev = vol->sb->s_bdev;
  602. /* Convert block into corresponding vcn and offset. */
  603. vcn = (VCN)block << blocksize_bits;
  604. vcn_ofs = vcn & vol->cluster_size_mask;
  605. vcn >>= vol->cluster_size_bits;
  606. if (!rl) {
  607. lock_retry_remap:
  608. down_read(&ni->runlist.lock);
  609. rl = ni->runlist.rl;
  610. }
  611. if (likely(rl != NULL)) {
  612. /* Seek to element containing target vcn. */
  613. while (rl->length && rl[1].vcn <= vcn)
  614. rl++;
  615. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  616. } else
  617. lcn = LCN_RL_NOT_MAPPED;
  618. /* Successful remap. */
  619. if (lcn >= 0) {
  620. /* Setup buffer head to point to correct block. */
  621. bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
  622. vcn_ofs) >> blocksize_bits;
  623. set_buffer_mapped(bh);
  624. continue;
  625. }
  626. /* It is a hole, need to instantiate it. */
  627. if (lcn == LCN_HOLE) {
  628. // TODO: Instantiate the hole.
  629. // clear_buffer_new(bh);
  630. // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  631. ntfs_error(vol->sb, "Writing into sparse regions is "
  632. "not supported yet. Sorry.");
  633. err = -EOPNOTSUPP;
  634. break;
  635. }
  636. /* If first try and runlist unmapped, map and retry. */
  637. if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
  638. is_retry = TRUE;
  639. /*
  640. * Attempt to map runlist, dropping lock for
  641. * the duration.
  642. */
  643. up_read(&ni->runlist.lock);
  644. err = ntfs_map_runlist(ni, vcn);
  645. if (likely(!err))
  646. goto lock_retry_remap;
  647. rl = NULL;
  648. lcn = err;
  649. }
  650. /* Failed to map the buffer, even after retrying. */
  651. bh->b_blocknr = -1;
  652. ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
  653. "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
  654. "because its location on disk could not be "
  655. "determined%s (error code %lli).", ni->mft_no,
  656. ni->type, (unsigned long long)vcn,
  657. vcn_ofs, is_retry ? " even after "
  658. "retrying" : "", (long long)lcn);
  659. if (!err)
  660. err = -EIO;
  661. break;
  662. } while (block++, (bh = bh->b_this_page) != head);
  663. /* Release the lock if we took it. */
  664. if (rl)
  665. up_read(&ni->runlist.lock);
  666. /* For the error case, need to reset bh to the beginning. */
  667. bh = head;
  668. /* Just an optimization, so ->readpage() isn't called later. */
  669. if (unlikely(!PageUptodate(page))) {
  670. int uptodate = 1;
  671. do {
  672. if (!buffer_uptodate(bh)) {
  673. uptodate = 0;
  674. bh = head;
  675. break;
  676. }
  677. } while ((bh = bh->b_this_page) != head);
  678. if (uptodate)
  679. SetPageUptodate(page);
  680. }
  681. /* Setup all mapped, dirty buffers for async write i/o. */
  682. do {
  683. get_bh(bh);
  684. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  685. lock_buffer(bh);
  686. if (test_clear_buffer_dirty(bh)) {
  687. BUG_ON(!buffer_uptodate(bh));
  688. mark_buffer_async_write(bh);
  689. } else
  690. unlock_buffer(bh);
  691. } else if (unlikely(err)) {
  692. /*
  693. * For the error case. The buffer may have been set
  694. * dirty during attachment to a dirty page.
  695. */
  696. if (err != -ENOMEM)
  697. clear_buffer_dirty(bh);
  698. }
  699. } while ((bh = bh->b_this_page) != head);
  700. if (unlikely(err)) {
  701. // TODO: Remove the -EOPNOTSUPP check later on...
  702. if (unlikely(err == -EOPNOTSUPP))
  703. err = 0;
  704. else if (err == -ENOMEM) {
  705. ntfs_warning(vol->sb, "Error allocating memory. "
  706. "Redirtying page so we try again "
  707. "later.");
  708. /*
  709. * Put the page back on mapping->dirty_pages, but
  710. * leave its buffer's dirty state as-is.
  711. */
  712. redirty_page_for_writepage(wbc, page);
  713. err = 0;
  714. } else
  715. SetPageError(page);
  716. }
  717. BUG_ON(PageWriteback(page));
  718. set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
  719. unlock_page(page);
  720. /*
  721. * Submit the prepared buffers for i/o. Note the page is unlocked,
  722. * and the async write i/o completion handler can end_page_writeback()
  723. * at any time after the *first* submit_bh(). So the buffers can then
  724. * disappear...
  725. */
  726. need_end_writeback = TRUE;
  727. do {
  728. struct buffer_head *next = bh->b_this_page;
  729. if (buffer_async_write(bh)) {
  730. submit_bh(WRITE, bh);
  731. need_end_writeback = FALSE;
  732. }
  733. put_bh(bh);
  734. bh = next;
  735. } while (bh != head);
  736. /* If no i/o was started, need to end_page_writeback(). */
  737. if (unlikely(need_end_writeback))
  738. end_page_writeback(page);
  739. ntfs_debug("Done.");
  740. return err;
  741. }
  742. /**
  743. * ntfs_write_mst_block - write a @page to the backing store
  744. * @page: page cache page to write out
  745. * @wbc: writeback control structure
  746. *
  747. * This function is for writing pages belonging to non-resident, mst protected
  748. * attributes to their backing store. The only supported attributes are index
  749. * allocation and $MFT/$DATA. Both directory inodes and index inodes are
  750. * supported for the index allocation case.
  751. *
  752. * The page must remain locked for the duration of the write because we apply
  753. * the mst fixups, write, and then undo the fixups, so if we were to unlock the
  754. * page before undoing the fixups, any other user of the page will see the
  755. * page contents as corrupt.
  756. *
  757. * We clear the page uptodate flag for the duration of the function to ensure
  758. * exclusion for the $MFT/$DATA case against someone mapping an mft record we
  759. * are about to apply the mst fixups to.
  760. *
  761. * Return 0 on success and -errno on error.
  762. *
  763. * Based on ntfs_write_block(), ntfs_mft_writepage(), and
  764. * write_mft_record_nolock().
  765. */
  766. static int ntfs_write_mst_block(struct page *page,
  767. struct writeback_control *wbc)
  768. {
  769. sector_t block, dblock, rec_block;
  770. struct inode *vi = page->mapping->host;
  771. ntfs_inode *ni = NTFS_I(vi);
  772. ntfs_volume *vol = ni->vol;
  773. u8 *kaddr;
  774. unsigned int rec_size = ni->itype.index.block_size;
  775. ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
  776. struct buffer_head *bh, *head, *tbh, *rec_start_bh;
  777. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  778. runlist_element *rl;
  779. int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
  780. unsigned bh_size, rec_size_bits;
  781. BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
  782. unsigned char bh_size_bits;
  783. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  784. "0x%lx.", vi->i_ino, ni->type, page->index);
  785. BUG_ON(!NInoNonResident(ni));
  786. BUG_ON(!NInoMstProtected(ni));
  787. is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
  788. /*
  789. * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
  790. * in its page cache were to be marked dirty. However this should
  791. * never happen with the current driver and considering we do not
  792. * handle this case here we do want to BUG(), at least for now.
  793. */
  794. BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
  795. (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
  796. bh_size_bits = vi->i_blkbits;
  797. bh_size = 1 << bh_size_bits;
  798. max_bhs = PAGE_CACHE_SIZE / bh_size;
  799. BUG_ON(!max_bhs);
  800. BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
  801. /* Were we called for sync purposes? */
  802. sync = (wbc->sync_mode == WB_SYNC_ALL);
  803. /* Make sure we have mapped buffers. */
  804. BUG_ON(!page_has_buffers(page));
  805. bh = head = page_buffers(page);
  806. BUG_ON(!bh);
  807. rec_size_bits = ni->itype.index.block_size_bits;
  808. BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
  809. bhs_per_rec = rec_size >> bh_size_bits;
  810. BUG_ON(!bhs_per_rec);
  811. /* The first block in the page. */
  812. rec_block = block = (sector_t)page->index <<
  813. (PAGE_CACHE_SHIFT - bh_size_bits);
  814. /* The first out of bounds block for the data size. */
  815. dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
  816. rl = NULL;
  817. err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
  818. page_is_dirty = rec_is_dirty = FALSE;
  819. rec_start_bh = NULL;
  820. do {
  821. BOOL is_retry = FALSE;
  822. if (likely(block < rec_block)) {
  823. if (unlikely(block >= dblock)) {
  824. clear_buffer_dirty(bh);
  825. set_buffer_uptodate(bh);
  826. continue;
  827. }
  828. /*
  829. * This block is not the first one in the record. We
  830. * ignore the buffer's dirty state because we could
  831. * have raced with a parallel mark_ntfs_record_dirty().
  832. */
  833. if (!rec_is_dirty)
  834. continue;
  835. if (unlikely(err2)) {
  836. if (err2 != -ENOMEM)
  837. clear_buffer_dirty(bh);
  838. continue;
  839. }
  840. } else /* if (block == rec_block) */ {
  841. BUG_ON(block > rec_block);
  842. /* This block is the first one in the record. */
  843. rec_block += bhs_per_rec;
  844. err2 = 0;
  845. if (unlikely(block >= dblock)) {
  846. clear_buffer_dirty(bh);
  847. continue;
  848. }
  849. if (!buffer_dirty(bh)) {
  850. /* Clean records are not written out. */
  851. rec_is_dirty = FALSE;
  852. continue;
  853. }
  854. rec_is_dirty = TRUE;
  855. rec_start_bh = bh;
  856. }
  857. /* Need to map the buffer if it is not mapped already. */
  858. if (unlikely(!buffer_mapped(bh))) {
  859. VCN vcn;
  860. LCN lcn;
  861. unsigned int vcn_ofs;
  862. /* Obtain the vcn and offset of the current block. */
  863. vcn = (VCN)block << bh_size_bits;
  864. vcn_ofs = vcn & vol->cluster_size_mask;
  865. vcn >>= vol->cluster_size_bits;
  866. if (!rl) {
  867. lock_retry_remap:
  868. down_read(&ni->runlist.lock);
  869. rl = ni->runlist.rl;
  870. }
  871. if (likely(rl != NULL)) {
  872. /* Seek to element containing target vcn. */
  873. while (rl->length && rl[1].vcn <= vcn)
  874. rl++;
  875. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  876. } else
  877. lcn = LCN_RL_NOT_MAPPED;
  878. /* Successful remap. */
  879. if (likely(lcn >= 0)) {
  880. /* Setup buffer head to correct block. */
  881. bh->b_blocknr = ((lcn <<
  882. vol->cluster_size_bits) +
  883. vcn_ofs) >> bh_size_bits;
  884. set_buffer_mapped(bh);
  885. } else {
  886. /*
  887. * Remap failed. Retry to map the runlist once
  888. * unless we are working on $MFT which always
  889. * has the whole of its runlist in memory.
  890. */
  891. if (!is_mft && !is_retry &&
  892. lcn == LCN_RL_NOT_MAPPED) {
  893. is_retry = TRUE;
  894. /*
  895. * Attempt to map runlist, dropping
  896. * lock for the duration.
  897. */
  898. up_read(&ni->runlist.lock);
  899. err2 = ntfs_map_runlist(ni, vcn);
  900. if (likely(!err2))
  901. goto lock_retry_remap;
  902. if (err2 == -ENOMEM)
  903. page_is_dirty = TRUE;
  904. lcn = err2;
  905. } else
  906. err2 = -EIO;
  907. /* Hard error. Abort writing this record. */
  908. if (!err || err == -ENOMEM)
  909. err = err2;
  910. bh->b_blocknr = -1;
  911. ntfs_error(vol->sb, "Cannot write ntfs record "
  912. "0x%llx (inode 0x%lx, "
  913. "attribute type 0x%x) because "
  914. "its location on disk could "
  915. "not be determined (error "
  916. "code %lli).",
  917. (long long)block <<
  918. bh_size_bits >>
  919. vol->mft_record_size_bits,
  920. ni->mft_no, ni->type,
  921. (long long)lcn);
  922. /*
  923. * If this is not the first buffer, remove the
  924. * buffers in this record from the list of
  925. * buffers to write and clear their dirty bit
  926. * if not error -ENOMEM.
  927. */
  928. if (rec_start_bh != bh) {
  929. while (bhs[--nr_bhs] != rec_start_bh)
  930. ;
  931. if (err2 != -ENOMEM) {
  932. do {
  933. clear_buffer_dirty(
  934. rec_start_bh);
  935. } while ((rec_start_bh =
  936. rec_start_bh->
  937. b_this_page) !=
  938. bh);
  939. }
  940. }
  941. continue;
  942. }
  943. }
  944. BUG_ON(!buffer_uptodate(bh));
  945. BUG_ON(nr_bhs >= max_bhs);
  946. bhs[nr_bhs++] = bh;
  947. } while (block++, (bh = bh->b_this_page) != head);
  948. if (unlikely(rl))
  949. up_read(&ni->runlist.lock);
  950. /* If there were no dirty buffers, we are done. */
  951. if (!nr_bhs)
  952. goto done;
  953. /* Map the page so we can access its contents. */
  954. kaddr = kmap(page);
  955. /* Clear the page uptodate flag whilst the mst fixups are applied. */
  956. BUG_ON(!PageUptodate(page));
  957. ClearPageUptodate(page);
  958. for (i = 0; i < nr_bhs; i++) {
  959. unsigned int ofs;
  960. /* Skip buffers which are not at the beginning of records. */
  961. if (i % bhs_per_rec)
  962. continue;
  963. tbh = bhs[i];
  964. ofs = bh_offset(tbh);
  965. if (is_mft) {
  966. ntfs_inode *tni;
  967. unsigned long mft_no;
  968. /* Get the mft record number. */
  969. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  970. >> rec_size_bits;
  971. /* Check whether to write this mft record. */
  972. tni = NULL;
  973. if (!ntfs_may_write_mft_record(vol, mft_no,
  974. (MFT_RECORD*)(kaddr + ofs), &tni)) {
  975. /*
  976. * The record should not be written. This
  977. * means we need to redirty the page before
  978. * returning.
  979. */
  980. page_is_dirty = TRUE;
  981. /*
  982. * Remove the buffers in this mft record from
  983. * the list of buffers to write.
  984. */
  985. do {
  986. bhs[i] = NULL;
  987. } while (++i % bhs_per_rec);
  988. continue;
  989. }
  990. /*
  991. * The record should be written. If a locked ntfs
  992. * inode was returned, add it to the array of locked
  993. * ntfs inodes.
  994. */
  995. if (tni)
  996. locked_nis[nr_locked_nis++] = tni;
  997. }
  998. /* Apply the mst protection fixups. */
  999. err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
  1000. rec_size);
  1001. if (unlikely(err2)) {
  1002. if (!err || err == -ENOMEM)
  1003. err = -EIO;
  1004. ntfs_error(vol->sb, "Failed to apply mst fixups "
  1005. "(inode 0x%lx, attribute type 0x%x, "
  1006. "page index 0x%lx, page offset 0x%x)!"
  1007. " Unmount and run chkdsk.", vi->i_ino,
  1008. ni->type, page->index, ofs);
  1009. /*
  1010. * Mark all the buffers in this record clean as we do
  1011. * not want to write corrupt data to disk.
  1012. */
  1013. do {
  1014. clear_buffer_dirty(bhs[i]);
  1015. bhs[i] = NULL;
  1016. } while (++i % bhs_per_rec);
  1017. continue;
  1018. }
  1019. nr_recs++;
  1020. }
  1021. /* If no records are to be written out, we are done. */
  1022. if (!nr_recs)
  1023. goto unm_done;
  1024. flush_dcache_page(page);
  1025. /* Lock buffers and start synchronous write i/o on them. */
  1026. for (i = 0; i < nr_bhs; i++) {
  1027. tbh = bhs[i];
  1028. if (!tbh)
  1029. continue;
  1030. if (unlikely(test_set_buffer_locked(tbh)))
  1031. BUG();
  1032. /* The buffer dirty state is now irrelevant, just clean it. */
  1033. clear_buffer_dirty(tbh);
  1034. BUG_ON(!buffer_uptodate(tbh));
  1035. BUG_ON(!buffer_mapped(tbh));
  1036. get_bh(tbh);
  1037. tbh->b_end_io = end_buffer_write_sync;
  1038. submit_bh(WRITE, tbh);
  1039. }
  1040. /* Synchronize the mft mirror now if not @sync. */
  1041. if (is_mft && !sync)
  1042. goto do_mirror;
  1043. do_wait:
  1044. /* Wait on i/o completion of buffers. */
  1045. for (i = 0; i < nr_bhs; i++) {
  1046. tbh = bhs[i];
  1047. if (!tbh)
  1048. continue;
  1049. wait_on_buffer(tbh);
  1050. if (unlikely(!buffer_uptodate(tbh))) {
  1051. ntfs_error(vol->sb, "I/O error while writing ntfs "
  1052. "record buffer (inode 0x%lx, "
  1053. "attribute type 0x%x, page index "
  1054. "0x%lx, page offset 0x%lx)! Unmount "
  1055. "and run chkdsk.", vi->i_ino, ni->type,
  1056. page->index, bh_offset(tbh));
  1057. if (!err || err == -ENOMEM)
  1058. err = -EIO;
  1059. /*
  1060. * Set the buffer uptodate so the page and buffer
  1061. * states do not become out of sync.
  1062. */
  1063. set_buffer_uptodate(tbh);
  1064. }
  1065. }
  1066. /* If @sync, now synchronize the mft mirror. */
  1067. if (is_mft && sync) {
  1068. do_mirror:
  1069. for (i = 0; i < nr_bhs; i++) {
  1070. unsigned long mft_no;
  1071. unsigned int ofs;
  1072. /*
  1073. * Skip buffers which are not at the beginning of
  1074. * records.
  1075. */
  1076. if (i % bhs_per_rec)
  1077. continue;
  1078. tbh = bhs[i];
  1079. /* Skip removed buffers (and hence records). */
  1080. if (!tbh)
  1081. continue;
  1082. ofs = bh_offset(tbh);
  1083. /* Get the mft record number. */
  1084. mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
  1085. >> rec_size_bits;
  1086. if (mft_no < vol->mftmirr_size)
  1087. ntfs_sync_mft_mirror(vol, mft_no,
  1088. (MFT_RECORD*)(kaddr + ofs),
  1089. sync);
  1090. }
  1091. if (!sync)
  1092. goto do_wait;
  1093. }
  1094. /* Remove the mst protection fixups again. */
  1095. for (i = 0; i < nr_bhs; i++) {
  1096. if (!(i % bhs_per_rec)) {
  1097. tbh = bhs[i];
  1098. if (!tbh)
  1099. continue;
  1100. post_write_mst_fixup((NTFS_RECORD*)(kaddr +
  1101. bh_offset(tbh)));
  1102. }
  1103. }
  1104. flush_dcache_page(page);
  1105. unm_done:
  1106. /* Unlock any locked inodes. */
  1107. while (nr_locked_nis-- > 0) {
  1108. ntfs_inode *tni, *base_tni;
  1109. tni = locked_nis[nr_locked_nis];
  1110. /* Get the base inode. */
  1111. down(&tni->extent_lock);
  1112. if (tni->nr_extents >= 0)
  1113. base_tni = tni;
  1114. else {
  1115. base_tni = tni->ext.base_ntfs_ino;
  1116. BUG_ON(!base_tni);
  1117. }
  1118. up(&tni->extent_lock);
  1119. ntfs_debug("Unlocking %s inode 0x%lx.",
  1120. tni == base_tni ? "base" : "extent",
  1121. tni->mft_no);
  1122. up(&tni->mrec_lock);
  1123. atomic_dec(&tni->count);
  1124. iput(VFS_I(base_tni));
  1125. }
  1126. SetPageUptodate(page);
  1127. kunmap(page);
  1128. done:
  1129. if (unlikely(err && err != -ENOMEM)) {
  1130. /*
  1131. * Set page error if there is only one ntfs record in the page.
  1132. * Otherwise we would loose per-record granularity.
  1133. */
  1134. if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
  1135. SetPageError(page);
  1136. NVolSetErrors(vol);
  1137. }
  1138. if (page_is_dirty) {
  1139. ntfs_debug("Page still contains one or more dirty ntfs "
  1140. "records. Redirtying the page starting at "
  1141. "record 0x%lx.", page->index <<
  1142. (PAGE_CACHE_SHIFT - rec_size_bits));
  1143. redirty_page_for_writepage(wbc, page);
  1144. unlock_page(page);
  1145. } else {
  1146. /*
  1147. * Keep the VM happy. This must be done otherwise the
  1148. * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
  1149. * the page is clean.
  1150. */
  1151. BUG_ON(PageWriteback(page));
  1152. set_page_writeback(page);
  1153. unlock_page(page);
  1154. end_page_writeback(page);
  1155. }
  1156. if (likely(!err))
  1157. ntfs_debug("Done.");
  1158. return err;
  1159. }
  1160. /**
  1161. * ntfs_writepage - write a @page to the backing store
  1162. * @page: page cache page to write out
  1163. * @wbc: writeback control structure
  1164. *
  1165. * This is called from the VM when it wants to have a dirty ntfs page cache
  1166. * page cleaned. The VM has already locked the page and marked it clean.
  1167. *
  1168. * For non-resident attributes, ntfs_writepage() writes the @page by calling
  1169. * the ntfs version of the generic block_write_full_page() function,
  1170. * ntfs_write_block(), which in turn if necessary creates and writes the
  1171. * buffers associated with the page asynchronously.
  1172. *
  1173. * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
  1174. * the data to the mft record (which at this stage is most likely in memory).
  1175. * The mft record is then marked dirty and written out asynchronously via the
  1176. * vfs inode dirty code path for the inode the mft record belongs to or via the
  1177. * vm page dirty code path for the page the mft record is in.
  1178. *
  1179. * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
  1180. *
  1181. * Return 0 on success and -errno on error.
  1182. */
  1183. static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
  1184. {
  1185. loff_t i_size;
  1186. struct inode *vi = page->mapping->host;
  1187. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1188. char *kaddr;
  1189. ntfs_attr_search_ctx *ctx = NULL;
  1190. MFT_RECORD *m = NULL;
  1191. u32 attr_len;
  1192. int err;
  1193. retry_writepage:
  1194. BUG_ON(!PageLocked(page));
  1195. i_size = i_size_read(vi);
  1196. /* Is the page fully outside i_size? (truncate in progress) */
  1197. if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
  1198. PAGE_CACHE_SHIFT)) {
  1199. /*
  1200. * The page may have dirty, unmapped buffers. Make them
  1201. * freeable here, so the page does not leak.
  1202. */
  1203. block_invalidatepage(page, 0);
  1204. unlock_page(page);
  1205. ntfs_debug("Write outside i_size - truncated?");
  1206. return 0;
  1207. }
  1208. /* NInoNonResident() == NInoIndexAllocPresent() */
  1209. if (NInoNonResident(ni)) {
  1210. /*
  1211. * Only unnamed $DATA attributes can be compressed, encrypted,
  1212. * and/or sparse.
  1213. */
  1214. if (ni->type == AT_DATA && !ni->name_len) {
  1215. /* If file is encrypted, deny access, just like NT4. */
  1216. if (NInoEncrypted(ni)) {
  1217. unlock_page(page);
  1218. ntfs_debug("Denying write access to encrypted "
  1219. "file.");
  1220. return -EACCES;
  1221. }
  1222. /* Compressed data streams are handled in compress.c. */
  1223. if (NInoCompressed(ni)) {
  1224. // TODO: Implement and replace this check with
  1225. // return ntfs_write_compressed_block(page);
  1226. unlock_page(page);
  1227. ntfs_error(vi->i_sb, "Writing to compressed "
  1228. "files is not supported yet. "
  1229. "Sorry.");
  1230. return -EOPNOTSUPP;
  1231. }
  1232. // TODO: Implement and remove this check.
  1233. if (NInoSparse(ni)) {
  1234. unlock_page(page);
  1235. ntfs_error(vi->i_sb, "Writing to sparse files "
  1236. "is not supported yet. Sorry.");
  1237. return -EOPNOTSUPP;
  1238. }
  1239. }
  1240. /* We have to zero every time due to mmap-at-end-of-file. */
  1241. if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
  1242. /* The page straddles i_size. */
  1243. unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
  1244. kaddr = kmap_atomic(page, KM_USER0);
  1245. memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
  1246. flush_dcache_page(page);
  1247. kunmap_atomic(kaddr, KM_USER0);
  1248. }
  1249. /* Handle mst protected attributes. */
  1250. if (NInoMstProtected(ni))
  1251. return ntfs_write_mst_block(page, wbc);
  1252. /* Normal data stream. */
  1253. return ntfs_write_block(page, wbc);
  1254. }
  1255. /*
  1256. * Attribute is resident, implying it is not compressed, encrypted,
  1257. * sparse, or mst protected. This also means the attribute is smaller
  1258. * than an mft record and hence smaller than a page, so can simply
  1259. * return error on any pages with index above 0.
  1260. */
  1261. BUG_ON(page_has_buffers(page));
  1262. BUG_ON(!PageUptodate(page));
  1263. if (unlikely(page->index > 0)) {
  1264. ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
  1265. "Aborting write.", page->index);
  1266. BUG_ON(PageWriteback(page));
  1267. set_page_writeback(page);
  1268. unlock_page(page);
  1269. end_page_writeback(page);
  1270. return -EIO;
  1271. }
  1272. if (!NInoAttr(ni))
  1273. base_ni = ni;
  1274. else
  1275. base_ni = ni->ext.base_ntfs_ino;
  1276. /* Map, pin, and lock the mft record. */
  1277. m = map_mft_record(base_ni);
  1278. if (IS_ERR(m)) {
  1279. err = PTR_ERR(m);
  1280. m = NULL;
  1281. ctx = NULL;
  1282. goto err_out;
  1283. }
  1284. /*
  1285. * If a parallel write made the attribute non-resident, drop the mft
  1286. * record and retry the writepage.
  1287. */
  1288. if (unlikely(NInoNonResident(ni))) {
  1289. unmap_mft_record(base_ni);
  1290. goto retry_writepage;
  1291. }
  1292. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1293. if (unlikely(!ctx)) {
  1294. err = -ENOMEM;
  1295. goto err_out;
  1296. }
  1297. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1298. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1299. if (unlikely(err))
  1300. goto err_out;
  1301. /*
  1302. * Keep the VM happy. This must be done otherwise the radix-tree tag
  1303. * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
  1304. */
  1305. BUG_ON(PageWriteback(page));
  1306. set_page_writeback(page);
  1307. unlock_page(page);
  1308. /*
  1309. * Here, we don't need to zero the out of bounds area everytime because
  1310. * the below memcpy() already takes care of the mmap-at-end-of-file
  1311. * requirements. If the file is converted to a non-resident one, then
  1312. * the code path use is switched to the non-resident one where the
  1313. * zeroing happens on each ntfs_writepage() invocation.
  1314. *
  1315. * The above also applies nicely when i_size is decreased.
  1316. *
  1317. * When i_size is increased, the memory between the old and new i_size
  1318. * _must_ be zeroed (or overwritten with new data). Otherwise we will
  1319. * expose data to userspace/disk which should never have been exposed.
  1320. *
  1321. * FIXME: Ensure that i_size increases do the zeroing/overwriting and
  1322. * if we cannot guarantee that, then enable the zeroing below. If the
  1323. * zeroing below is enabled, we MUST move the unlock_page() from above
  1324. * to after the kunmap_atomic(), i.e. just before the
  1325. * end_page_writeback().
  1326. * UPDATE: ntfs_prepare/commit_write() do the zeroing on i_size
  1327. * increases for resident attributes so those are ok.
  1328. * TODO: ntfs_truncate(), others?
  1329. */
  1330. attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
  1331. i_size = i_size_read(vi);
  1332. if (unlikely(attr_len > i_size)) {
  1333. attr_len = i_size;
  1334. ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
  1335. }
  1336. kaddr = kmap_atomic(page, KM_USER0);
  1337. /* Copy the data from the page to the mft record. */
  1338. memcpy((u8*)ctx->attr +
  1339. le16_to_cpu(ctx->attr->data.resident.value_offset),
  1340. kaddr, attr_len);
  1341. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1342. /* Zero out of bounds area in the page cache page. */
  1343. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  1344. flush_dcache_page(page);
  1345. kunmap_atomic(kaddr, KM_USER0);
  1346. end_page_writeback(page);
  1347. /* Mark the mft record dirty, so it gets written back. */
  1348. mark_mft_record_dirty(ctx->ntfs_ino);
  1349. ntfs_attr_put_search_ctx(ctx);
  1350. unmap_mft_record(base_ni);
  1351. return 0;
  1352. err_out:
  1353. if (err == -ENOMEM) {
  1354. ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
  1355. "page so we try again later.");
  1356. /*
  1357. * Put the page back on mapping->dirty_pages, but leave its
  1358. * buffers' dirty state as-is.
  1359. */
  1360. redirty_page_for_writepage(wbc, page);
  1361. err = 0;
  1362. } else {
  1363. ntfs_error(vi->i_sb, "Resident attribute write failed with "
  1364. "error %i.", err);
  1365. SetPageError(page);
  1366. NVolSetErrors(ni->vol);
  1367. make_bad_inode(vi);
  1368. }
  1369. unlock_page(page);
  1370. if (ctx)
  1371. ntfs_attr_put_search_ctx(ctx);
  1372. if (m)
  1373. unmap_mft_record(base_ni);
  1374. return err;
  1375. }
  1376. /**
  1377. * ntfs_prepare_nonresident_write -
  1378. *
  1379. */
  1380. static int ntfs_prepare_nonresident_write(struct page *page,
  1381. unsigned from, unsigned to)
  1382. {
  1383. VCN vcn;
  1384. LCN lcn;
  1385. s64 initialized_size;
  1386. loff_t i_size;
  1387. sector_t block, ablock, iblock;
  1388. struct inode *vi;
  1389. ntfs_inode *ni;
  1390. ntfs_volume *vol;
  1391. runlist_element *rl;
  1392. struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
  1393. unsigned long flags;
  1394. unsigned int vcn_ofs, block_start, block_end, blocksize;
  1395. int err;
  1396. BOOL is_retry;
  1397. unsigned char blocksize_bits;
  1398. vi = page->mapping->host;
  1399. ni = NTFS_I(vi);
  1400. vol = ni->vol;
  1401. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  1402. "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
  1403. page->index, from, to);
  1404. BUG_ON(!NInoNonResident(ni));
  1405. blocksize_bits = vi->i_blkbits;
  1406. blocksize = 1 << blocksize_bits;
  1407. /*
  1408. * create_empty_buffers() will create uptodate/dirty buffers if the
  1409. * page is uptodate/dirty.
  1410. */
  1411. if (!page_has_buffers(page))
  1412. create_empty_buffers(page, blocksize, 0);
  1413. bh = head = page_buffers(page);
  1414. if (unlikely(!bh))
  1415. return -ENOMEM;
  1416. /* The first block in the page. */
  1417. block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
  1418. read_lock_irqsave(&ni->size_lock, flags);
  1419. /*
  1420. * The first out of bounds block for the allocated size. No need to
  1421. * round up as allocated_size is in multiples of cluster size and the
  1422. * minimum cluster size is 512 bytes, which is equal to the smallest
  1423. * blocksize.
  1424. */
  1425. ablock = ni->allocated_size >> blocksize_bits;
  1426. i_size = i_size_read(vi);
  1427. initialized_size = ni->initialized_size;
  1428. read_unlock_irqrestore(&ni->size_lock, flags);
  1429. /* The last (fully or partially) initialized block. */
  1430. iblock = initialized_size >> blocksize_bits;
  1431. /* Loop through all the buffers in the page. */
  1432. block_start = 0;
  1433. rl = NULL;
  1434. err = 0;
  1435. do {
  1436. block_end = block_start + blocksize;
  1437. /*
  1438. * If buffer @bh is outside the write, just mark it uptodate
  1439. * if the page is uptodate and continue with the next buffer.
  1440. */
  1441. if (block_end <= from || block_start >= to) {
  1442. if (PageUptodate(page)) {
  1443. if (!buffer_uptodate(bh))
  1444. set_buffer_uptodate(bh);
  1445. }
  1446. continue;
  1447. }
  1448. /*
  1449. * @bh is at least partially being written to.
  1450. * Make sure it is not marked as new.
  1451. */
  1452. //if (buffer_new(bh))
  1453. // clear_buffer_new(bh);
  1454. if (block >= ablock) {
  1455. // TODO: block is above allocated_size, need to
  1456. // allocate it. Best done in one go to accommodate not
  1457. // only block but all above blocks up to and including:
  1458. // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
  1459. // - 1) >> blobksize_bits. Obviously will need to round
  1460. // up to next cluster boundary, too. This should be
  1461. // done with a helper function, so it can be reused.
  1462. ntfs_error(vol->sb, "Writing beyond allocated size "
  1463. "is not supported yet. Sorry.");
  1464. err = -EOPNOTSUPP;
  1465. goto err_out;
  1466. // Need to update ablock.
  1467. // Need to set_buffer_new() on all block bhs that are
  1468. // newly allocated.
  1469. }
  1470. /*
  1471. * Now we have enough allocated size to fulfill the whole
  1472. * request, i.e. block < ablock is true.
  1473. */
  1474. if (unlikely((block >= iblock) &&
  1475. (initialized_size < i_size))) {
  1476. /*
  1477. * If this page is fully outside initialized size, zero
  1478. * out all pages between the current initialized size
  1479. * and the current page. Just use ntfs_readpage() to do
  1480. * the zeroing transparently.
  1481. */
  1482. if (block > iblock) {
  1483. // TODO:
  1484. // For each page do:
  1485. // - read_cache_page()
  1486. // Again for each page do:
  1487. // - wait_on_page_locked()
  1488. // - Check (PageUptodate(page) &&
  1489. // !PageError(page))
  1490. // Update initialized size in the attribute and
  1491. // in the inode.
  1492. // Again, for each page do:
  1493. // __set_page_dirty_buffers();
  1494. // page_cache_release()
  1495. // We don't need to wait on the writes.
  1496. // Update iblock.
  1497. }
  1498. /*
  1499. * The current page straddles initialized size. Zero
  1500. * all non-uptodate buffers and set them uptodate (and
  1501. * dirty?). Note, there aren't any non-uptodate buffers
  1502. * if the page is uptodate.
  1503. * FIXME: For an uptodate page, the buffers may need to
  1504. * be written out because they were not initialized on
  1505. * disk before.
  1506. */
  1507. if (!PageUptodate(page)) {
  1508. // TODO:
  1509. // Zero any non-uptodate buffers up to i_size.
  1510. // Set them uptodate and dirty.
  1511. }
  1512. // TODO:
  1513. // Update initialized size in the attribute and in the
  1514. // inode (up to i_size).
  1515. // Update iblock.
  1516. // FIXME: This is inefficient. Try to batch the two
  1517. // size changes to happen in one go.
  1518. ntfs_error(vol->sb, "Writing beyond initialized size "
  1519. "is not supported yet. Sorry.");
  1520. err = -EOPNOTSUPP;
  1521. goto err_out;
  1522. // Do NOT set_buffer_new() BUT DO clear buffer range
  1523. // outside write request range.
  1524. // set_buffer_uptodate() on complete buffers as well as
  1525. // set_buffer_dirty().
  1526. }
  1527. /* Need to map unmapped buffers. */
  1528. if (!buffer_mapped(bh)) {
  1529. /* Unmapped buffer. Need to map it. */
  1530. bh->b_bdev = vol->sb->s_bdev;
  1531. /* Convert block into corresponding vcn and offset. */
  1532. vcn = (VCN)block << blocksize_bits >>
  1533. vol->cluster_size_bits;
  1534. vcn_ofs = ((VCN)block << blocksize_bits) &
  1535. vol->cluster_size_mask;
  1536. is_retry = FALSE;
  1537. if (!rl) {
  1538. lock_retry_remap:
  1539. down_read(&ni->runlist.lock);
  1540. rl = ni->runlist.rl;
  1541. }
  1542. if (likely(rl != NULL)) {
  1543. /* Seek to element containing target vcn. */
  1544. while (rl->length && rl[1].vcn <= vcn)
  1545. rl++;
  1546. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  1547. } else
  1548. lcn = LCN_RL_NOT_MAPPED;
  1549. if (unlikely(lcn < 0)) {
  1550. /*
  1551. * We extended the attribute allocation above.
  1552. * If we hit an ENOENT here it means that the
  1553. * allocation was insufficient which is a bug.
  1554. */
  1555. BUG_ON(lcn == LCN_ENOENT);
  1556. /* It is a hole, need to instantiate it. */
  1557. if (lcn == LCN_HOLE) {
  1558. // TODO: Instantiate the hole.
  1559. // clear_buffer_new(bh);
  1560. // unmap_underlying_metadata(bh->b_bdev,
  1561. // bh->b_blocknr);
  1562. // For non-uptodate buffers, need to
  1563. // zero out the region outside the
  1564. // request in this bh or all bhs,
  1565. // depending on what we implemented
  1566. // above.
  1567. // Need to flush_dcache_page().
  1568. // Or could use set_buffer_new()
  1569. // instead?
  1570. ntfs_error(vol->sb, "Writing into "
  1571. "sparse regions is "
  1572. "not supported yet. "
  1573. "Sorry.");
  1574. err = -EOPNOTSUPP;
  1575. goto err_out;
  1576. } else if (!is_retry &&
  1577. lcn == LCN_RL_NOT_MAPPED) {
  1578. is_retry = TRUE;
  1579. /*
  1580. * Attempt to map runlist, dropping
  1581. * lock for the duration.
  1582. */
  1583. up_read(&ni->runlist.lock);
  1584. err = ntfs_map_runlist(ni, vcn);
  1585. if (likely(!err))
  1586. goto lock_retry_remap;
  1587. rl = NULL;
  1588. lcn = err;
  1589. }
  1590. /*
  1591. * Failed to map the buffer, even after
  1592. * retrying.
  1593. */
  1594. bh->b_blocknr = -1;
  1595. ntfs_error(vol->sb, "Failed to write to inode "
  1596. "0x%lx, attribute type 0x%x, "
  1597. "vcn 0x%llx, offset 0x%x "
  1598. "because its location on disk "
  1599. "could not be determined%s "
  1600. "(error code %lli).",
  1601. ni->mft_no, ni->type,
  1602. (unsigned long long)vcn,
  1603. vcn_ofs, is_retry ? " even "
  1604. "after retrying" : "",
  1605. (long long)lcn);
  1606. if (!err)
  1607. err = -EIO;
  1608. goto err_out;
  1609. }
  1610. /* We now have a successful remap, i.e. lcn >= 0. */
  1611. /* Setup buffer head to correct block. */
  1612. bh->b_blocknr = ((lcn << vol->cluster_size_bits)
  1613. + vcn_ofs) >> blocksize_bits;
  1614. set_buffer_mapped(bh);
  1615. // FIXME: Something analogous to this is needed for
  1616. // each newly allocated block, i.e. BH_New.
  1617. // FIXME: Might need to take this out of the
  1618. // if (!buffer_mapped(bh)) {}, depending on how we
  1619. // implement things during the allocated_size and
  1620. // initialized_size extension code above.
  1621. if (buffer_new(bh)) {
  1622. clear_buffer_new(bh);
  1623. unmap_underlying_metadata(bh->b_bdev,
  1624. bh->b_blocknr);
  1625. if (PageUptodate(page)) {
  1626. set_buffer_uptodate(bh);
  1627. continue;
  1628. }
  1629. /*
  1630. * Page is _not_ uptodate, zero surrounding
  1631. * region. NOTE: This is how we decide if to
  1632. * zero or not!
  1633. */
  1634. if (block_end > to || block_start < from) {
  1635. void *kaddr;
  1636. kaddr = kmap_atomic(page, KM_USER0);
  1637. if (block_end > to)
  1638. memset(kaddr + to, 0,
  1639. block_end - to);
  1640. if (block_start < from)
  1641. memset(kaddr + block_start, 0,
  1642. from -
  1643. block_start);
  1644. flush_dcache_page(page);
  1645. kunmap_atomic(kaddr, KM_USER0);
  1646. }
  1647. continue;
  1648. }
  1649. }
  1650. /* @bh is mapped, set it uptodate if the page is uptodate. */
  1651. if (PageUptodate(page)) {
  1652. if (!buffer_uptodate(bh))
  1653. set_buffer_uptodate(bh);
  1654. continue;
  1655. }
  1656. /*
  1657. * The page is not uptodate. The buffer is mapped. If it is not
  1658. * uptodate, and it is only partially being written to, we need
  1659. * to read the buffer in before the write, i.e. right now.
  1660. */
  1661. if (!buffer_uptodate(bh) &&
  1662. (block_start < from || block_end > to)) {
  1663. ll_rw_block(READ, 1, &bh);
  1664. *wait_bh++ = bh;
  1665. }
  1666. } while (block++, block_start = block_end,
  1667. (bh = bh->b_this_page) != head);
  1668. /* Release the lock if we took it. */
  1669. if (rl) {
  1670. up_read(&ni->runlist.lock);
  1671. rl = NULL;
  1672. }
  1673. /* If we issued read requests, let them complete. */
  1674. while (wait_bh > wait) {
  1675. wait_on_buffer(*--wait_bh);
  1676. if (!buffer_uptodate(*wait_bh))
  1677. return -EIO;
  1678. }
  1679. ntfs_debug("Done.");
  1680. return 0;
  1681. err_out:
  1682. /*
  1683. * Zero out any newly allocated blocks to avoid exposing stale data.
  1684. * If BH_New is set, we know that the block was newly allocated in the
  1685. * above loop.
  1686. * FIXME: What about initialized_size increments? Have we done all the
  1687. * required zeroing above? If not this error handling is broken, and
  1688. * in particular the if (block_end <= from) check is completely bogus.
  1689. */
  1690. bh = head;
  1691. block_start = 0;
  1692. is_retry = FALSE;
  1693. do {
  1694. block_end = block_start + blocksize;
  1695. if (block_end <= from)
  1696. continue;
  1697. if (block_start >= to)
  1698. break;
  1699. if (buffer_new(bh)) {
  1700. void *kaddr;
  1701. clear_buffer_new(bh);
  1702. kaddr = kmap_atomic(page, KM_USER0);
  1703. memset(kaddr + block_start, 0, bh->b_size);
  1704. kunmap_atomic(kaddr, KM_USER0);
  1705. set_buffer_uptodate(bh);
  1706. mark_buffer_dirty(bh);
  1707. is_retry = TRUE;
  1708. }
  1709. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  1710. if (is_retry)
  1711. flush_dcache_page(page);
  1712. if (rl)
  1713. up_read(&ni->runlist.lock);
  1714. return err;
  1715. }
  1716. /**
  1717. * ntfs_prepare_write - prepare a page for receiving data
  1718. *
  1719. * This is called from generic_file_write() with i_sem held on the inode
  1720. * (@page->mapping->host). The @page is locked but not kmap()ped. The source
  1721. * data has not yet been copied into the @page.
  1722. *
  1723. * Need to extend the attribute/fill in holes if necessary, create blocks and
  1724. * make partially overwritten blocks uptodate,
  1725. *
  1726. * i_size is not to be modified yet.
  1727. *
  1728. * Return 0 on success or -errno on error.
  1729. *
  1730. * Should be using block_prepare_write() [support for sparse files] or
  1731. * cont_prepare_write() [no support for sparse files]. Cannot do that due to
  1732. * ntfs specifics but can look at them for implementation guidance.
  1733. *
  1734. * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
  1735. * the first byte in the page that will be written to and @to is the first byte
  1736. * after the last byte that will be written to.
  1737. */
  1738. static int ntfs_prepare_write(struct file *file, struct page *page,
  1739. unsigned from, unsigned to)
  1740. {
  1741. s64 new_size;
  1742. loff_t i_size;
  1743. struct inode *vi = page->mapping->host;
  1744. ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
  1745. ntfs_volume *vol = ni->vol;
  1746. ntfs_attr_search_ctx *ctx = NULL;
  1747. MFT_RECORD *m = NULL;
  1748. ATTR_RECORD *a;
  1749. u8 *kaddr;
  1750. u32 attr_len;
  1751. int err;
  1752. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  1753. "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
  1754. page->index, from, to);
  1755. BUG_ON(!PageLocked(page));
  1756. BUG_ON(from > PAGE_CACHE_SIZE);
  1757. BUG_ON(to > PAGE_CACHE_SIZE);
  1758. BUG_ON(from > to);
  1759. BUG_ON(NInoMstProtected(ni));
  1760. /*
  1761. * If a previous ntfs_truncate() failed, repeat it and abort if it
  1762. * fails again.
  1763. */
  1764. if (unlikely(NInoTruncateFailed(ni))) {
  1765. down_write(&vi->i_alloc_sem);
  1766. err = ntfs_truncate(vi);
  1767. up_write(&vi->i_alloc_sem);
  1768. if (err || NInoTruncateFailed(ni)) {
  1769. if (!err)
  1770. err = -EIO;
  1771. goto err_out;
  1772. }
  1773. }
  1774. /* If the attribute is not resident, deal with it elsewhere. */
  1775. if (NInoNonResident(ni)) {
  1776. /*
  1777. * Only unnamed $DATA attributes can be compressed, encrypted,
  1778. * and/or sparse.
  1779. */
  1780. if (ni->type == AT_DATA && !ni->name_len) {
  1781. /* If file is encrypted, deny access, just like NT4. */
  1782. if (NInoEncrypted(ni)) {
  1783. ntfs_debug("Denying write access to encrypted "
  1784. "file.");
  1785. return -EACCES;
  1786. }
  1787. /* Compressed data streams are handled in compress.c. */
  1788. if (NInoCompressed(ni)) {
  1789. // TODO: Implement and replace this check with
  1790. // return ntfs_write_compressed_block(page);
  1791. ntfs_error(vi->i_sb, "Writing to compressed "
  1792. "files is not supported yet. "
  1793. "Sorry.");
  1794. return -EOPNOTSUPP;
  1795. }
  1796. // TODO: Implement and remove this check.
  1797. if (NInoSparse(ni)) {
  1798. ntfs_error(vi->i_sb, "Writing to sparse files "
  1799. "is not supported yet. Sorry.");
  1800. return -EOPNOTSUPP;
  1801. }
  1802. }
  1803. /* Normal data stream. */
  1804. return ntfs_prepare_nonresident_write(page, from, to);
  1805. }
  1806. /*
  1807. * Attribute is resident, implying it is not compressed, encrypted, or
  1808. * sparse.
  1809. */
  1810. BUG_ON(page_has_buffers(page));
  1811. new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
  1812. /* If we do not need to resize the attribute allocation we are done. */
  1813. if (new_size <= i_size_read(vi))
  1814. goto done;
  1815. /* Map, pin, and lock the (base) mft record. */
  1816. if (!NInoAttr(ni))
  1817. base_ni = ni;
  1818. else
  1819. base_ni = ni->ext.base_ntfs_ino;
  1820. m = map_mft_record(base_ni);
  1821. if (IS_ERR(m)) {
  1822. err = PTR_ERR(m);
  1823. m = NULL;
  1824. ctx = NULL;
  1825. goto err_out;
  1826. }
  1827. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1828. if (unlikely(!ctx)) {
  1829. err = -ENOMEM;
  1830. goto err_out;
  1831. }
  1832. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1833. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1834. if (unlikely(err)) {
  1835. if (err == -ENOENT)
  1836. err = -EIO;
  1837. goto err_out;
  1838. }
  1839. m = ctx->mrec;
  1840. a = ctx->attr;
  1841. /* The total length of the attribute value. */
  1842. attr_len = le32_to_cpu(a->data.resident.value_length);
  1843. /* Fix an eventual previous failure of ntfs_commit_write(). */
  1844. i_size = i_size_read(vi);
  1845. if (unlikely(attr_len > i_size)) {
  1846. attr_len = i_size;
  1847. a->data.resident.value_length = cpu_to_le32(attr_len);
  1848. }
  1849. /* If we do not need to resize the attribute allocation we are done. */
  1850. if (new_size <= attr_len)
  1851. goto done_unm;
  1852. /* Check if new size is allowed in $AttrDef. */
  1853. err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
  1854. if (unlikely(err)) {
  1855. if (err == -ERANGE) {
  1856. ntfs_error(vol->sb, "Write would cause the inode "
  1857. "0x%lx to exceed the maximum size for "
  1858. "its attribute type (0x%x). Aborting "
  1859. "write.", vi->i_ino,
  1860. le32_to_cpu(ni->type));
  1861. } else {
  1862. ntfs_error(vol->sb, "Inode 0x%lx has unknown "
  1863. "attribute type 0x%x. Aborting "
  1864. "write.", vi->i_ino,
  1865. le32_to_cpu(ni->type));
  1866. err = -EIO;
  1867. }
  1868. goto err_out2;
  1869. }
  1870. /*
  1871. * Extend the attribute record to be able to store the new attribute
  1872. * size.
  1873. */
  1874. if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
  1875. le16_to_cpu(a->data.resident.value_offset) +
  1876. new_size)) {
  1877. /* Not enough space in the mft record. */
  1878. ntfs_error(vol->sb, "Not enough space in the mft record for "
  1879. "the resized attribute value. This is not "
  1880. "supported yet. Aborting write.");
  1881. err = -EOPNOTSUPP;
  1882. goto err_out2;
  1883. }
  1884. /*
  1885. * We have enough space in the mft record to fit the write. This
  1886. * implies the attribute is smaller than the mft record and hence the
  1887. * attribute must be in a single page and hence page->index must be 0.
  1888. */
  1889. BUG_ON(page->index);
  1890. /*
  1891. * If the beginning of the write is past the old size, enlarge the
  1892. * attribute value up to the beginning of the write and fill it with
  1893. * zeroes.
  1894. */
  1895. if (from > attr_len) {
  1896. memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
  1897. attr_len, 0, from - attr_len);
  1898. a->data.resident.value_length = cpu_to_le32(from);
  1899. /* Zero the corresponding area in the page as well. */
  1900. if (PageUptodate(page)) {
  1901. kaddr = kmap_atomic(page, KM_USER0);
  1902. memset(kaddr + attr_len, 0, from - attr_len);
  1903. kunmap_atomic(kaddr, KM_USER0);
  1904. flush_dcache_page(page);
  1905. }
  1906. }
  1907. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1908. mark_mft_record_dirty(ctx->ntfs_ino);
  1909. done_unm:
  1910. ntfs_attr_put_search_ctx(ctx);
  1911. unmap_mft_record(base_ni);
  1912. /*
  1913. * Because resident attributes are handled by memcpy() to/from the
  1914. * corresponding MFT record, and because this form of i/o is byte
  1915. * aligned rather than block aligned, there is no need to bring the
  1916. * page uptodate here as in the non-resident case where we need to
  1917. * bring the buffers straddled by the write uptodate before
  1918. * generic_file_write() does the copying from userspace.
  1919. *
  1920. * We thus defer the uptodate bringing of the page region outside the
  1921. * region written to to ntfs_commit_write(), which makes the code
  1922. * simpler and saves one atomic kmap which is good.
  1923. */
  1924. done:
  1925. ntfs_debug("Done.");
  1926. return 0;
  1927. err_out:
  1928. if (err == -ENOMEM)
  1929. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  1930. "prepare the write.");
  1931. else {
  1932. ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
  1933. "with error %i.", err);
  1934. NVolSetErrors(vol);
  1935. make_bad_inode(vi);
  1936. }
  1937. err_out2:
  1938. if (ctx)
  1939. ntfs_attr_put_search_ctx(ctx);
  1940. if (m)
  1941. unmap_mft_record(base_ni);
  1942. return err;
  1943. }
  1944. /**
  1945. * ntfs_commit_nonresident_write -
  1946. *
  1947. */
  1948. static int ntfs_commit_nonresident_write(struct page *page,
  1949. unsigned from, unsigned to)
  1950. {
  1951. s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
  1952. struct inode *vi = page->mapping->host;
  1953. struct buffer_head *bh, *head;
  1954. unsigned int block_start, block_end, blocksize;
  1955. BOOL partial;
  1956. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  1957. "0x%lx, from = %u, to = %u.", vi->i_ino,
  1958. NTFS_I(vi)->type, page->index, from, to);
  1959. blocksize = 1 << vi->i_blkbits;
  1960. // FIXME: We need a whole slew of special cases in here for compressed
  1961. // files for example...
  1962. // For now, we know ntfs_prepare_write() would have failed so we can't
  1963. // get here in any of the cases which we have to special case, so we
  1964. // are just a ripped off, unrolled generic_commit_write().
  1965. bh = head = page_buffers(page);
  1966. block_start = 0;
  1967. partial = FALSE;
  1968. do {
  1969. block_end = block_start + blocksize;
  1970. if (block_end <= from || block_start >= to) {
  1971. if (!buffer_uptodate(bh))
  1972. partial = TRUE;
  1973. } else {
  1974. set_buffer_uptodate(bh);
  1975. mark_buffer_dirty(bh);
  1976. }
  1977. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  1978. /*
  1979. * If this is a partial write which happened to make all buffers
  1980. * uptodate then we can optimize away a bogus ->readpage() for the next
  1981. * read(). Here we 'discover' whether the page went uptodate as a
  1982. * result of this (potentially partial) write.
  1983. */
  1984. if (!partial)
  1985. SetPageUptodate(page);
  1986. /*
  1987. * Not convinced about this at all. See disparity comment above. For
  1988. * now we know ntfs_prepare_write() would have failed in the write
  1989. * exceeds i_size case, so this will never trigger which is fine.
  1990. */
  1991. if (pos > i_size_read(vi)) {
  1992. ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
  1993. "not supported yet. Sorry.");
  1994. return -EOPNOTSUPP;
  1995. // vi->i_size = pos;
  1996. // mark_inode_dirty(vi);
  1997. }
  1998. ntfs_debug("Done.");
  1999. return 0;
  2000. }
  2001. /**
  2002. * ntfs_commit_write - commit the received data
  2003. *
  2004. * This is called from generic_file_write() with i_sem held on the inode
  2005. * (@page->mapping->host). The @page is locked but not kmap()ped. The source
  2006. * data has already been copied into the @page. ntfs_prepare_write() has been
  2007. * called before the data copied and it returned success so we can take the
  2008. * results of various BUG checks and some error handling for granted.
  2009. *
  2010. * Need to mark modified blocks dirty so they get written out later when
  2011. * ntfs_writepage() is invoked by the VM.
  2012. *
  2013. * Return 0 on success or -errno on error.
  2014. *
  2015. * Should be using generic_commit_write(). This marks buffers uptodate and
  2016. * dirty, sets the page uptodate if all buffers in the page are uptodate, and
  2017. * updates i_size if the end of io is beyond i_size. In that case, it also
  2018. * marks the inode dirty.
  2019. *
  2020. * Cannot use generic_commit_write() due to ntfs specialities but can look at
  2021. * it for implementation guidance.
  2022. *
  2023. * If things have gone as outlined in ntfs_prepare_write(), then we do not
  2024. * need to do any page content modifications here at all, except in the write
  2025. * to resident attribute case, where we need to do the uptodate bringing here
  2026. * which we combine with the copying into the mft record which means we save
  2027. * one atomic kmap.
  2028. */
  2029. static int ntfs_commit_write(struct file *file, struct page *page,
  2030. unsigned from, unsigned to)
  2031. {
  2032. struct inode *vi = page->mapping->host;
  2033. ntfs_inode *base_ni, *ni = NTFS_I(vi);
  2034. char *kaddr, *kattr;
  2035. ntfs_attr_search_ctx *ctx;
  2036. MFT_RECORD *m;
  2037. ATTR_RECORD *a;
  2038. u32 attr_len;
  2039. int err;
  2040. ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
  2041. "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
  2042. page->index, from, to);
  2043. /* If the attribute is not resident, deal with it elsewhere. */
  2044. if (NInoNonResident(ni)) {
  2045. /* Only unnamed $DATA attributes can be compressed/encrypted. */
  2046. if (ni->type == AT_DATA && !ni->name_len) {
  2047. /* Encrypted files need separate handling. */
  2048. if (NInoEncrypted(ni)) {
  2049. // We never get here at present!
  2050. BUG();
  2051. }
  2052. /* Compressed data streams are handled in compress.c. */
  2053. if (NInoCompressed(ni)) {
  2054. // TODO: Implement this!
  2055. // return ntfs_write_compressed_block(page);
  2056. // We never get here at present!
  2057. BUG();
  2058. }
  2059. }
  2060. /* Normal data stream. */
  2061. return ntfs_commit_nonresident_write(page, from, to);
  2062. }
  2063. /*
  2064. * Attribute is resident, implying it is not compressed, encrypted, or
  2065. * sparse.
  2066. */
  2067. if (!NInoAttr(ni))
  2068. base_ni = ni;
  2069. else
  2070. base_ni = ni->ext.base_ntfs_ino;
  2071. /* Map, pin, and lock the mft record. */
  2072. m = map_mft_record(base_ni);
  2073. if (IS_ERR(m)) {
  2074. err = PTR_ERR(m);
  2075. m = NULL;
  2076. ctx = NULL;
  2077. goto err_out;
  2078. }
  2079. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  2080. if (unlikely(!ctx)) {
  2081. err = -ENOMEM;
  2082. goto err_out;
  2083. }
  2084. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2085. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2086. if (unlikely(err)) {
  2087. if (err == -ENOENT)
  2088. err = -EIO;
  2089. goto err_out;
  2090. }
  2091. a = ctx->attr;
  2092. /* The total length of the attribute value. */
  2093. attr_len = le32_to_cpu(a->data.resident.value_length);
  2094. BUG_ON(from > attr_len);
  2095. kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
  2096. kaddr = kmap_atomic(page, KM_USER0);
  2097. /* Copy the received data from the page to the mft record. */
  2098. memcpy(kattr + from, kaddr + from, to - from);
  2099. /* Update the attribute length if necessary. */
  2100. if (to > attr_len) {
  2101. attr_len = to;
  2102. a->data.resident.value_length = cpu_to_le32(attr_len);
  2103. }
  2104. /*
  2105. * If the page is not uptodate, bring the out of bounds area(s)
  2106. * uptodate by copying data from the mft record to the page.
  2107. */
  2108. if (!PageUptodate(page)) {
  2109. if (from > 0)
  2110. memcpy(kaddr, kattr, from);
  2111. if (to < attr_len)
  2112. memcpy(kaddr + to, kattr + to, attr_len - to);
  2113. /* Zero the region outside the end of the attribute value. */
  2114. if (attr_len < PAGE_CACHE_SIZE)
  2115. memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
  2116. /*
  2117. * The probability of not having done any of the above is
  2118. * extremely small, so we just flush unconditionally.
  2119. */
  2120. flush_dcache_page(page);
  2121. SetPageUptodate(page);
  2122. }
  2123. kunmap_atomic(kaddr, KM_USER0);
  2124. /* Update i_size if necessary. */
  2125. if (i_size_read(vi) < attr_len) {
  2126. unsigned long flags;
  2127. write_lock_irqsave(&ni->size_lock, flags);
  2128. ni->allocated_size = ni->initialized_size = attr_len;
  2129. i_size_write(vi, attr_len);
  2130. write_unlock_irqrestore(&ni->size_lock, flags);
  2131. }
  2132. /* Mark the mft record dirty, so it gets written back. */
  2133. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2134. mark_mft_record_dirty(ctx->ntfs_ino);
  2135. ntfs_attr_put_search_ctx(ctx);
  2136. unmap_mft_record(base_ni);
  2137. ntfs_debug("Done.");
  2138. return 0;
  2139. err_out:
  2140. if (err == -ENOMEM) {
  2141. ntfs_warning(vi->i_sb, "Error allocating memory required to "
  2142. "commit the write.");
  2143. if (PageUptodate(page)) {
  2144. ntfs_warning(vi->i_sb, "Page is uptodate, setting "
  2145. "dirty so the write will be retried "
  2146. "later on by the VM.");
  2147. /*
  2148. * Put the page on mapping->dirty_pages, but leave its
  2149. * buffers' dirty state as-is.
  2150. */
  2151. __set_page_dirty_nobuffers(page);
  2152. err = 0;
  2153. } else
  2154. ntfs_error(vi->i_sb, "Page is not uptodate. Written "
  2155. "data has been lost.");
  2156. } else {
  2157. ntfs_error(vi->i_sb, "Resident attribute commit write failed "
  2158. "with error %i.", err);
  2159. NVolSetErrors(ni->vol);
  2160. make_bad_inode(vi);
  2161. }
  2162. if (ctx)
  2163. ntfs_attr_put_search_ctx(ctx);
  2164. if (m)
  2165. unmap_mft_record(base_ni);
  2166. return err;
  2167. }
  2168. #endif /* NTFS_RW */
  2169. /**
  2170. * ntfs_aops - general address space operations for inodes and attributes
  2171. */
  2172. struct address_space_operations ntfs_aops = {
  2173. .readpage = ntfs_readpage, /* Fill page with data. */
  2174. .sync_page = block_sync_page, /* Currently, just unplugs the
  2175. disk request queue. */
  2176. #ifdef NTFS_RW
  2177. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  2178. .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
  2179. ready to receive data. */
  2180. .commit_write = ntfs_commit_write, /* Commit received data. */
  2181. #endif /* NTFS_RW */
  2182. };
  2183. /**
  2184. * ntfs_mst_aops - general address space operations for mst protecteed inodes
  2185. * and attributes
  2186. */
  2187. struct address_space_operations ntfs_mst_aops = {
  2188. .readpage = ntfs_readpage, /* Fill page with data. */
  2189. .sync_page = block_sync_page, /* Currently, just unplugs the
  2190. disk request queue. */
  2191. #ifdef NTFS_RW
  2192. .writepage = ntfs_writepage, /* Write dirty page to disk. */
  2193. .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
  2194. without touching the buffers
  2195. belonging to the page. */
  2196. #endif /* NTFS_RW */
  2197. };
  2198. #ifdef NTFS_RW
  2199. /**
  2200. * mark_ntfs_record_dirty - mark an ntfs record dirty
  2201. * @page: page containing the ntfs record to mark dirty
  2202. * @ofs: byte offset within @page at which the ntfs record begins
  2203. *
  2204. * Set the buffers and the page in which the ntfs record is located dirty.
  2205. *
  2206. * The latter also marks the vfs inode the ntfs record belongs to dirty
  2207. * (I_DIRTY_PAGES only).
  2208. *
  2209. * If the page does not have buffers, we create them and set them uptodate.
  2210. * The page may not be locked which is why we need to handle the buffers under
  2211. * the mapping->private_lock. Once the buffers are marked dirty we no longer
  2212. * need the lock since try_to_free_buffers() does not free dirty buffers.
  2213. */
  2214. void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
  2215. struct address_space *mapping = page->mapping;
  2216. ntfs_inode *ni = NTFS_I(mapping->host);
  2217. struct buffer_head *bh, *head, *buffers_to_free = NULL;
  2218. unsigned int end, bh_size, bh_ofs;
  2219. BUG_ON(!PageUptodate(page));
  2220. end = ofs + ni->itype.index.block_size;
  2221. bh_size = 1 << VFS_I(ni)->i_blkbits;
  2222. spin_lock(&mapping->private_lock);
  2223. if (unlikely(!page_has_buffers(page))) {
  2224. spin_unlock(&mapping->private_lock);
  2225. bh = head = alloc_page_buffers(page, bh_size, 1);
  2226. spin_lock(&mapping->private_lock);
  2227. if (likely(!page_has_buffers(page))) {
  2228. struct buffer_head *tail;
  2229. do {
  2230. set_buffer_uptodate(bh);
  2231. tail = bh;
  2232. bh = bh->b_this_page;
  2233. } while (bh);
  2234. tail->b_this_page = head;
  2235. attach_page_buffers(page, head);
  2236. } else
  2237. buffers_to_free = bh;
  2238. }
  2239. bh = head = page_buffers(page);
  2240. do {
  2241. bh_ofs = bh_offset(bh);
  2242. if (bh_ofs + bh_size <= ofs)
  2243. continue;
  2244. if (unlikely(bh_ofs >= end))
  2245. break;
  2246. set_buffer_dirty(bh);
  2247. } while ((bh = bh->b_this_page) != head);
  2248. spin_unlock(&mapping->private_lock);
  2249. __set_page_dirty_nobuffers(page);
  2250. if (unlikely(buffers_to_free)) {
  2251. do {
  2252. bh = buffers_to_free->b_this_page;
  2253. free_buffer_head(buffers_to_free);
  2254. buffers_to_free = bh;
  2255. } while (buffers_to_free);
  2256. }
  2257. }
  2258. #endif /* NTFS_RW */