aops.c 71 KB

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