aops.c 69 KB

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