aops.c 69 KB

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