aops.c 69 KB

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