aops.c 71 KB

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