aops.c 70 KB

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