aops.c 70 KB

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