aops.c 71 KB

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