aops.c 71 KB

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