aops.c 70 KB

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