aops.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/highmem.h>
  24. #include <linux/pagemap.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/swap.h>
  27. #include <linux/pipe_fs_i.h>
  28. #define MLOG_MASK_PREFIX ML_FILE_IO
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "aops.h"
  33. #include "dlmglue.h"
  34. #include "extent_map.h"
  35. #include "file.h"
  36. #include "inode.h"
  37. #include "journal.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "symlink.h"
  41. #include "buffer_head_io.h"
  42. static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
  43. struct buffer_head *bh_result, int create)
  44. {
  45. int err = -EIO;
  46. int status;
  47. struct ocfs2_dinode *fe = NULL;
  48. struct buffer_head *bh = NULL;
  49. struct buffer_head *buffer_cache_bh = NULL;
  50. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  51. void *kaddr;
  52. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  53. (unsigned long long)iblock, bh_result, create);
  54. BUG_ON(ocfs2_inode_is_fast_symlink(inode));
  55. if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
  56. mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
  57. (unsigned long long)iblock);
  58. goto bail;
  59. }
  60. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  61. OCFS2_I(inode)->ip_blkno,
  62. &bh, OCFS2_BH_CACHED, inode);
  63. if (status < 0) {
  64. mlog_errno(status);
  65. goto bail;
  66. }
  67. fe = (struct ocfs2_dinode *) bh->b_data;
  68. if (!OCFS2_IS_VALID_DINODE(fe)) {
  69. mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
  70. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  71. fe->i_signature);
  72. goto bail;
  73. }
  74. if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
  75. le32_to_cpu(fe->i_clusters))) {
  76. mlog(ML_ERROR, "block offset is outside the allocated size: "
  77. "%llu\n", (unsigned long long)iblock);
  78. goto bail;
  79. }
  80. /* We don't use the page cache to create symlink data, so if
  81. * need be, copy it over from the buffer cache. */
  82. if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
  83. u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
  84. iblock;
  85. buffer_cache_bh = sb_getblk(osb->sb, blkno);
  86. if (!buffer_cache_bh) {
  87. mlog(ML_ERROR, "couldn't getblock for symlink!\n");
  88. goto bail;
  89. }
  90. /* we haven't locked out transactions, so a commit
  91. * could've happened. Since we've got a reference on
  92. * the bh, even if it commits while we're doing the
  93. * copy, the data is still good. */
  94. if (buffer_jbd(buffer_cache_bh)
  95. && ocfs2_inode_is_new(inode)) {
  96. kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
  97. if (!kaddr) {
  98. mlog(ML_ERROR, "couldn't kmap!\n");
  99. goto bail;
  100. }
  101. memcpy(kaddr + (bh_result->b_size * iblock),
  102. buffer_cache_bh->b_data,
  103. bh_result->b_size);
  104. kunmap_atomic(kaddr, KM_USER0);
  105. set_buffer_uptodate(bh_result);
  106. }
  107. brelse(buffer_cache_bh);
  108. }
  109. map_bh(bh_result, inode->i_sb,
  110. le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
  111. err = 0;
  112. bail:
  113. if (bh)
  114. brelse(bh);
  115. mlog_exit(err);
  116. return err;
  117. }
  118. static int ocfs2_get_block(struct inode *inode, sector_t iblock,
  119. struct buffer_head *bh_result, int create)
  120. {
  121. int err = 0;
  122. unsigned int ext_flags;
  123. u64 p_blkno, past_eof;
  124. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  125. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  126. (unsigned long long)iblock, bh_result, create);
  127. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  128. mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
  129. inode, inode->i_ino);
  130. if (S_ISLNK(inode->i_mode)) {
  131. /* this always does I/O for some reason. */
  132. err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
  133. goto bail;
  134. }
  135. err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
  136. &ext_flags);
  137. if (err) {
  138. mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
  139. "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
  140. (unsigned long long)p_blkno);
  141. goto bail;
  142. }
  143. /*
  144. * ocfs2 never allocates in this function - the only time we
  145. * need to use BH_New is when we're extending i_size on a file
  146. * system which doesn't support holes, in which case BH_New
  147. * allows block_prepare_write() to zero.
  148. */
  149. mlog_bug_on_msg(create && p_blkno == 0 && ocfs2_sparse_alloc(osb),
  150. "ino %lu, iblock %llu\n", inode->i_ino,
  151. (unsigned long long)iblock);
  152. /* Treat the unwritten extent as a hole for zeroing purposes. */
  153. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  154. map_bh(bh_result, inode->i_sb, p_blkno);
  155. if (!ocfs2_sparse_alloc(osb)) {
  156. if (p_blkno == 0) {
  157. err = -EIO;
  158. mlog(ML_ERROR,
  159. "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
  160. (unsigned long long)iblock,
  161. (unsigned long long)p_blkno,
  162. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  163. mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
  164. dump_stack();
  165. }
  166. past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  167. mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino,
  168. (unsigned long long)past_eof);
  169. if (create && (iblock >= past_eof))
  170. set_buffer_new(bh_result);
  171. }
  172. bail:
  173. if (err < 0)
  174. err = -EIO;
  175. mlog_exit(err);
  176. return err;
  177. }
  178. static int ocfs2_readpage(struct file *file, struct page *page)
  179. {
  180. struct inode *inode = page->mapping->host;
  181. loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
  182. int ret, unlock = 1;
  183. mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
  184. ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
  185. if (ret != 0) {
  186. if (ret == AOP_TRUNCATED_PAGE)
  187. unlock = 0;
  188. mlog_errno(ret);
  189. goto out;
  190. }
  191. if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem) == 0) {
  192. ret = AOP_TRUNCATED_PAGE;
  193. goto out_meta_unlock;
  194. }
  195. /*
  196. * i_size might have just been updated as we grabed the meta lock. We
  197. * might now be discovering a truncate that hit on another node.
  198. * block_read_full_page->get_block freaks out if it is asked to read
  199. * beyond the end of a file, so we check here. Callers
  200. * (generic_file_read, vm_ops->fault) are clever enough to check i_size
  201. * and notice that the page they just read isn't needed.
  202. *
  203. * XXX sys_readahead() seems to get that wrong?
  204. */
  205. if (start >= i_size_read(inode)) {
  206. zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
  207. SetPageUptodate(page);
  208. ret = 0;
  209. goto out_alloc;
  210. }
  211. ret = ocfs2_data_lock_with_page(inode, 0, page);
  212. if (ret != 0) {
  213. if (ret == AOP_TRUNCATED_PAGE)
  214. unlock = 0;
  215. mlog_errno(ret);
  216. goto out_alloc;
  217. }
  218. ret = block_read_full_page(page, ocfs2_get_block);
  219. unlock = 0;
  220. ocfs2_data_unlock(inode, 0);
  221. out_alloc:
  222. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  223. out_meta_unlock:
  224. ocfs2_meta_unlock(inode, 0);
  225. out:
  226. if (unlock)
  227. unlock_page(page);
  228. mlog_exit(ret);
  229. return ret;
  230. }
  231. /* Note: Because we don't support holes, our allocation has
  232. * already happened (allocation writes zeros to the file data)
  233. * so we don't have to worry about ordered writes in
  234. * ocfs2_writepage.
  235. *
  236. * ->writepage is called during the process of invalidating the page cache
  237. * during blocked lock processing. It can't block on any cluster locks
  238. * to during block mapping. It's relying on the fact that the block
  239. * mapping can't have disappeared under the dirty pages that it is
  240. * being asked to write back.
  241. */
  242. static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
  243. {
  244. int ret;
  245. mlog_entry("(0x%p)\n", page);
  246. ret = block_write_full_page(page, ocfs2_get_block, wbc);
  247. mlog_exit(ret);
  248. return ret;
  249. }
  250. /*
  251. * This is called from ocfs2_write_zero_page() which has handled it's
  252. * own cluster locking and has ensured allocation exists for those
  253. * blocks to be written.
  254. */
  255. int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
  256. unsigned from, unsigned to)
  257. {
  258. int ret;
  259. ret = block_prepare_write(page, from, to, ocfs2_get_block);
  260. return ret;
  261. }
  262. /* Taken from ext3. We don't necessarily need the full blown
  263. * functionality yet, but IMHO it's better to cut and paste the whole
  264. * thing so we can avoid introducing our own bugs (and easily pick up
  265. * their fixes when they happen) --Mark */
  266. int walk_page_buffers( handle_t *handle,
  267. struct buffer_head *head,
  268. unsigned from,
  269. unsigned to,
  270. int *partial,
  271. int (*fn)( handle_t *handle,
  272. struct buffer_head *bh))
  273. {
  274. struct buffer_head *bh;
  275. unsigned block_start, block_end;
  276. unsigned blocksize = head->b_size;
  277. int err, ret = 0;
  278. struct buffer_head *next;
  279. for ( bh = head, block_start = 0;
  280. ret == 0 && (bh != head || !block_start);
  281. block_start = block_end, bh = next)
  282. {
  283. next = bh->b_this_page;
  284. block_end = block_start + blocksize;
  285. if (block_end <= from || block_start >= to) {
  286. if (partial && !buffer_uptodate(bh))
  287. *partial = 1;
  288. continue;
  289. }
  290. err = (*fn)(handle, bh);
  291. if (!ret)
  292. ret = err;
  293. }
  294. return ret;
  295. }
  296. handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
  297. struct page *page,
  298. unsigned from,
  299. unsigned to)
  300. {
  301. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  302. handle_t *handle = NULL;
  303. int ret = 0;
  304. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  305. if (!handle) {
  306. ret = -ENOMEM;
  307. mlog_errno(ret);
  308. goto out;
  309. }
  310. if (ocfs2_should_order_data(inode)) {
  311. ret = walk_page_buffers(handle,
  312. page_buffers(page),
  313. from, to, NULL,
  314. ocfs2_journal_dirty_data);
  315. if (ret < 0)
  316. mlog_errno(ret);
  317. }
  318. out:
  319. if (ret) {
  320. if (handle)
  321. ocfs2_commit_trans(osb, handle);
  322. handle = ERR_PTR(ret);
  323. }
  324. return handle;
  325. }
  326. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  327. {
  328. sector_t status;
  329. u64 p_blkno = 0;
  330. int err = 0;
  331. struct inode *inode = mapping->host;
  332. mlog_entry("(block = %llu)\n", (unsigned long long)block);
  333. /* We don't need to lock journal system files, since they aren't
  334. * accessed concurrently from multiple nodes.
  335. */
  336. if (!INODE_JOURNAL(inode)) {
  337. err = ocfs2_meta_lock(inode, NULL, 0);
  338. if (err) {
  339. if (err != -ENOENT)
  340. mlog_errno(err);
  341. goto bail;
  342. }
  343. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  344. }
  345. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
  346. if (!INODE_JOURNAL(inode)) {
  347. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  348. ocfs2_meta_unlock(inode, 0);
  349. }
  350. if (err) {
  351. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  352. (unsigned long long)block);
  353. mlog_errno(err);
  354. goto bail;
  355. }
  356. bail:
  357. status = err ? 0 : p_blkno;
  358. mlog_exit((int)status);
  359. return status;
  360. }
  361. /*
  362. * TODO: Make this into a generic get_blocks function.
  363. *
  364. * From do_direct_io in direct-io.c:
  365. * "So what we do is to permit the ->get_blocks function to populate
  366. * bh.b_size with the size of IO which is permitted at this offset and
  367. * this i_blkbits."
  368. *
  369. * This function is called directly from get_more_blocks in direct-io.c.
  370. *
  371. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  372. * fs_count, map_bh, dio->rw == WRITE);
  373. */
  374. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  375. struct buffer_head *bh_result, int create)
  376. {
  377. int ret;
  378. u64 p_blkno, inode_blocks, contig_blocks;
  379. unsigned int ext_flags;
  380. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  381. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  382. /* This function won't even be called if the request isn't all
  383. * nicely aligned and of the right size, so there's no need
  384. * for us to check any of that. */
  385. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  386. /*
  387. * Any write past EOF is not allowed because we'd be extending.
  388. */
  389. if (create && (iblock + max_blocks) > inode_blocks) {
  390. ret = -EIO;
  391. goto bail;
  392. }
  393. /* This figures out the size of the next contiguous block, and
  394. * our logical offset */
  395. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  396. &contig_blocks, &ext_flags);
  397. if (ret) {
  398. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  399. (unsigned long long)iblock);
  400. ret = -EIO;
  401. goto bail;
  402. }
  403. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
  404. ocfs2_error(inode->i_sb,
  405. "Inode %llu has a hole at block %llu\n",
  406. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  407. (unsigned long long)iblock);
  408. ret = -EROFS;
  409. goto bail;
  410. }
  411. /*
  412. * get_more_blocks() expects us to describe a hole by clearing
  413. * the mapped bit on bh_result().
  414. *
  415. * Consider an unwritten extent as a hole.
  416. */
  417. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  418. map_bh(bh_result, inode->i_sb, p_blkno);
  419. else {
  420. /*
  421. * ocfs2_prepare_inode_for_write() should have caught
  422. * the case where we'd be filling a hole and triggered
  423. * a buffered write instead.
  424. */
  425. if (create) {
  426. ret = -EIO;
  427. mlog_errno(ret);
  428. goto bail;
  429. }
  430. clear_buffer_mapped(bh_result);
  431. }
  432. /* make sure we don't map more than max_blocks blocks here as
  433. that's all the kernel will handle at this point. */
  434. if (max_blocks < contig_blocks)
  435. contig_blocks = max_blocks;
  436. bh_result->b_size = contig_blocks << blocksize_bits;
  437. bail:
  438. return ret;
  439. }
  440. /*
  441. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  442. * particularly interested in the aio/dio case. Like the core uses
  443. * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
  444. * truncation on another.
  445. */
  446. static void ocfs2_dio_end_io(struct kiocb *iocb,
  447. loff_t offset,
  448. ssize_t bytes,
  449. void *private)
  450. {
  451. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  452. int level;
  453. /* this io's submitter should not have unlocked this before we could */
  454. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  455. ocfs2_iocb_clear_rw_locked(iocb);
  456. level = ocfs2_iocb_rw_locked_level(iocb);
  457. if (!level)
  458. up_read(&inode->i_alloc_sem);
  459. ocfs2_rw_unlock(inode, level);
  460. }
  461. /*
  462. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  463. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  464. * do journalled data.
  465. */
  466. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  467. {
  468. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  469. journal_invalidatepage(journal, page, offset);
  470. }
  471. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  472. {
  473. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  474. if (!page_has_buffers(page))
  475. return 0;
  476. return journal_try_to_free_buffers(journal, page, wait);
  477. }
  478. static ssize_t ocfs2_direct_IO(int rw,
  479. struct kiocb *iocb,
  480. const struct iovec *iov,
  481. loff_t offset,
  482. unsigned long nr_segs)
  483. {
  484. struct file *file = iocb->ki_filp;
  485. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  486. int ret;
  487. mlog_entry_void();
  488. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  489. /*
  490. * We get PR data locks even for O_DIRECT. This
  491. * allows concurrent O_DIRECT I/O but doesn't let
  492. * O_DIRECT with extending and buffered zeroing writes
  493. * race. If they did race then the buffered zeroing
  494. * could be written back after the O_DIRECT I/O. It's
  495. * one thing to tell people not to mix buffered and
  496. * O_DIRECT writes, but expecting them to understand
  497. * that file extension is also an implicit buffered
  498. * write is too much. By getting the PR we force
  499. * writeback of the buffered zeroing before
  500. * proceeding.
  501. */
  502. ret = ocfs2_data_lock(inode, 0);
  503. if (ret < 0) {
  504. mlog_errno(ret);
  505. goto out;
  506. }
  507. ocfs2_data_unlock(inode, 0);
  508. }
  509. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  510. inode->i_sb->s_bdev, iov, offset,
  511. nr_segs,
  512. ocfs2_direct_IO_get_blocks,
  513. ocfs2_dio_end_io);
  514. out:
  515. mlog_exit(ret);
  516. return ret;
  517. }
  518. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  519. u32 cpos,
  520. unsigned int *start,
  521. unsigned int *end)
  522. {
  523. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  524. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  525. unsigned int cpp;
  526. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  527. cluster_start = cpos % cpp;
  528. cluster_start = cluster_start << osb->s_clustersize_bits;
  529. cluster_end = cluster_start + osb->s_clustersize;
  530. }
  531. BUG_ON(cluster_start > PAGE_SIZE);
  532. BUG_ON(cluster_end > PAGE_SIZE);
  533. if (start)
  534. *start = cluster_start;
  535. if (end)
  536. *end = cluster_end;
  537. }
  538. /*
  539. * 'from' and 'to' are the region in the page to avoid zeroing.
  540. *
  541. * If pagesize > clustersize, this function will avoid zeroing outside
  542. * of the cluster boundary.
  543. *
  544. * from == to == 0 is code for "zero the entire cluster region"
  545. */
  546. static void ocfs2_clear_page_regions(struct page *page,
  547. struct ocfs2_super *osb, u32 cpos,
  548. unsigned from, unsigned to)
  549. {
  550. void *kaddr;
  551. unsigned int cluster_start, cluster_end;
  552. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  553. kaddr = kmap_atomic(page, KM_USER0);
  554. if (from || to) {
  555. if (from > cluster_start)
  556. memset(kaddr + cluster_start, 0, from - cluster_start);
  557. if (to < cluster_end)
  558. memset(kaddr + to, 0, cluster_end - to);
  559. } else {
  560. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  561. }
  562. kunmap_atomic(kaddr, KM_USER0);
  563. }
  564. /*
  565. * Some of this taken from block_prepare_write(). We already have our
  566. * mapping by now though, and the entire write will be allocating or
  567. * it won't, so not much need to use BH_New.
  568. *
  569. * This will also skip zeroing, which is handled externally.
  570. */
  571. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  572. struct inode *inode, unsigned int from,
  573. unsigned int to, int new)
  574. {
  575. int ret = 0;
  576. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  577. unsigned int block_end, block_start;
  578. unsigned int bsize = 1 << inode->i_blkbits;
  579. if (!page_has_buffers(page))
  580. create_empty_buffers(page, bsize, 0);
  581. head = page_buffers(page);
  582. for (bh = head, block_start = 0; bh != head || !block_start;
  583. bh = bh->b_this_page, block_start += bsize) {
  584. block_end = block_start + bsize;
  585. clear_buffer_new(bh);
  586. /*
  587. * Ignore blocks outside of our i/o range -
  588. * they may belong to unallocated clusters.
  589. */
  590. if (block_start >= to || block_end <= from) {
  591. if (PageUptodate(page))
  592. set_buffer_uptodate(bh);
  593. continue;
  594. }
  595. /*
  596. * For an allocating write with cluster size >= page
  597. * size, we always write the entire page.
  598. */
  599. if (new)
  600. set_buffer_new(bh);
  601. if (!buffer_mapped(bh)) {
  602. map_bh(bh, inode->i_sb, *p_blkno);
  603. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  604. }
  605. if (PageUptodate(page)) {
  606. if (!buffer_uptodate(bh))
  607. set_buffer_uptodate(bh);
  608. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  609. !buffer_new(bh) &&
  610. (block_start < from || block_end > to)) {
  611. ll_rw_block(READ, 1, &bh);
  612. *wait_bh++=bh;
  613. }
  614. *p_blkno = *p_blkno + 1;
  615. }
  616. /*
  617. * If we issued read requests - let them complete.
  618. */
  619. while(wait_bh > wait) {
  620. wait_on_buffer(*--wait_bh);
  621. if (!buffer_uptodate(*wait_bh))
  622. ret = -EIO;
  623. }
  624. if (ret == 0 || !new)
  625. return ret;
  626. /*
  627. * If we get -EIO above, zero out any newly allocated blocks
  628. * to avoid exposing stale data.
  629. */
  630. bh = head;
  631. block_start = 0;
  632. do {
  633. block_end = block_start + bsize;
  634. if (block_end <= from)
  635. goto next_bh;
  636. if (block_start >= to)
  637. break;
  638. zero_user_page(page, block_start, bh->b_size, KM_USER0);
  639. set_buffer_uptodate(bh);
  640. mark_buffer_dirty(bh);
  641. next_bh:
  642. block_start = block_end;
  643. bh = bh->b_this_page;
  644. } while (bh != head);
  645. return ret;
  646. }
  647. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  648. #define OCFS2_MAX_CTXT_PAGES 1
  649. #else
  650. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  651. #endif
  652. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  653. /*
  654. * Describe the state of a single cluster to be written to.
  655. */
  656. struct ocfs2_write_cluster_desc {
  657. u32 c_cpos;
  658. u32 c_phys;
  659. /*
  660. * Give this a unique field because c_phys eventually gets
  661. * filled.
  662. */
  663. unsigned c_new;
  664. unsigned c_unwritten;
  665. };
  666. static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
  667. {
  668. return d->c_new || d->c_unwritten;
  669. }
  670. struct ocfs2_write_ctxt {
  671. /* Logical cluster position / len of write */
  672. u32 w_cpos;
  673. u32 w_clen;
  674. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  675. /*
  676. * This is true if page_size > cluster_size.
  677. *
  678. * It triggers a set of special cases during write which might
  679. * have to deal with allocating writes to partial pages.
  680. */
  681. unsigned int w_large_pages;
  682. /*
  683. * Pages involved in this write.
  684. *
  685. * w_target_page is the page being written to by the user.
  686. *
  687. * w_pages is an array of pages which always contains
  688. * w_target_page, and in the case of an allocating write with
  689. * page_size < cluster size, it will contain zero'd and mapped
  690. * pages adjacent to w_target_page which need to be written
  691. * out in so that future reads from that region will get
  692. * zero's.
  693. */
  694. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  695. unsigned int w_num_pages;
  696. struct page *w_target_page;
  697. /*
  698. * ocfs2_write_end() uses this to know what the real range to
  699. * write in the target should be.
  700. */
  701. unsigned int w_target_from;
  702. unsigned int w_target_to;
  703. /*
  704. * We could use journal_current_handle() but this is cleaner,
  705. * IMHO -Mark
  706. */
  707. handle_t *w_handle;
  708. struct buffer_head *w_di_bh;
  709. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  710. };
  711. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  712. {
  713. int i;
  714. for(i = 0; i < wc->w_num_pages; i++) {
  715. if (wc->w_pages[i] == NULL)
  716. continue;
  717. unlock_page(wc->w_pages[i]);
  718. mark_page_accessed(wc->w_pages[i]);
  719. page_cache_release(wc->w_pages[i]);
  720. }
  721. brelse(wc->w_di_bh);
  722. kfree(wc);
  723. }
  724. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  725. struct ocfs2_super *osb, loff_t pos,
  726. unsigned len, struct buffer_head *di_bh)
  727. {
  728. u32 cend;
  729. struct ocfs2_write_ctxt *wc;
  730. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  731. if (!wc)
  732. return -ENOMEM;
  733. wc->w_cpos = pos >> osb->s_clustersize_bits;
  734. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  735. wc->w_clen = cend - wc->w_cpos + 1;
  736. get_bh(di_bh);
  737. wc->w_di_bh = di_bh;
  738. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  739. wc->w_large_pages = 1;
  740. else
  741. wc->w_large_pages = 0;
  742. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  743. *wcp = wc;
  744. return 0;
  745. }
  746. /*
  747. * If a page has any new buffers, zero them out here, and mark them uptodate
  748. * and dirty so they'll be written out (in order to prevent uninitialised
  749. * block data from leaking). And clear the new bit.
  750. */
  751. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  752. {
  753. unsigned int block_start, block_end;
  754. struct buffer_head *head, *bh;
  755. BUG_ON(!PageLocked(page));
  756. if (!page_has_buffers(page))
  757. return;
  758. bh = head = page_buffers(page);
  759. block_start = 0;
  760. do {
  761. block_end = block_start + bh->b_size;
  762. if (buffer_new(bh)) {
  763. if (block_end > from && block_start < to) {
  764. if (!PageUptodate(page)) {
  765. unsigned start, end;
  766. start = max(from, block_start);
  767. end = min(to, block_end);
  768. zero_user_page(page, start, end - start, KM_USER0);
  769. set_buffer_uptodate(bh);
  770. }
  771. clear_buffer_new(bh);
  772. mark_buffer_dirty(bh);
  773. }
  774. }
  775. block_start = block_end;
  776. bh = bh->b_this_page;
  777. } while (bh != head);
  778. }
  779. /*
  780. * Only called when we have a failure during allocating write to write
  781. * zero's to the newly allocated region.
  782. */
  783. static void ocfs2_write_failure(struct inode *inode,
  784. struct ocfs2_write_ctxt *wc,
  785. loff_t user_pos, unsigned user_len)
  786. {
  787. int i;
  788. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  789. to = user_pos + user_len;
  790. struct page *tmppage;
  791. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  792. for(i = 0; i < wc->w_num_pages; i++) {
  793. tmppage = wc->w_pages[i];
  794. if (ocfs2_should_order_data(inode))
  795. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  796. from, to, NULL,
  797. ocfs2_journal_dirty_data);
  798. block_commit_write(tmppage, from, to);
  799. }
  800. }
  801. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  802. struct ocfs2_write_ctxt *wc,
  803. struct page *page, u32 cpos,
  804. loff_t user_pos, unsigned user_len,
  805. int new)
  806. {
  807. int ret;
  808. unsigned int map_from = 0, map_to = 0;
  809. unsigned int cluster_start, cluster_end;
  810. unsigned int user_data_from = 0, user_data_to = 0;
  811. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  812. &cluster_start, &cluster_end);
  813. if (page == wc->w_target_page) {
  814. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  815. map_to = map_from + user_len;
  816. if (new)
  817. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  818. cluster_start, cluster_end,
  819. new);
  820. else
  821. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  822. map_from, map_to, new);
  823. if (ret) {
  824. mlog_errno(ret);
  825. goto out;
  826. }
  827. user_data_from = map_from;
  828. user_data_to = map_to;
  829. if (new) {
  830. map_from = cluster_start;
  831. map_to = cluster_end;
  832. }
  833. } else {
  834. /*
  835. * If we haven't allocated the new page yet, we
  836. * shouldn't be writing it out without copying user
  837. * data. This is likely a math error from the caller.
  838. */
  839. BUG_ON(!new);
  840. map_from = cluster_start;
  841. map_to = cluster_end;
  842. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  843. cluster_start, cluster_end, new);
  844. if (ret) {
  845. mlog_errno(ret);
  846. goto out;
  847. }
  848. }
  849. /*
  850. * Parts of newly allocated pages need to be zero'd.
  851. *
  852. * Above, we have also rewritten 'to' and 'from' - as far as
  853. * the rest of the function is concerned, the entire cluster
  854. * range inside of a page needs to be written.
  855. *
  856. * We can skip this if the page is up to date - it's already
  857. * been zero'd from being read in as a hole.
  858. */
  859. if (new && !PageUptodate(page))
  860. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  861. cpos, user_data_from, user_data_to);
  862. flush_dcache_page(page);
  863. out:
  864. return ret;
  865. }
  866. /*
  867. * This function will only grab one clusters worth of pages.
  868. */
  869. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  870. struct ocfs2_write_ctxt *wc,
  871. u32 cpos, loff_t user_pos, int new,
  872. struct page *mmap_page)
  873. {
  874. int ret = 0, i;
  875. unsigned long start, target_index, index;
  876. struct inode *inode = mapping->host;
  877. target_index = user_pos >> PAGE_CACHE_SHIFT;
  878. /*
  879. * Figure out how many pages we'll be manipulating here. For
  880. * non allocating write, we just change the one
  881. * page. Otherwise, we'll need a whole clusters worth.
  882. */
  883. if (new) {
  884. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  885. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  886. } else {
  887. wc->w_num_pages = 1;
  888. start = target_index;
  889. }
  890. for(i = 0; i < wc->w_num_pages; i++) {
  891. index = start + i;
  892. if (index == target_index && mmap_page) {
  893. /*
  894. * ocfs2_pagemkwrite() is a little different
  895. * and wants us to directly use the page
  896. * passed in.
  897. */
  898. lock_page(mmap_page);
  899. if (mmap_page->mapping != mapping) {
  900. unlock_page(mmap_page);
  901. /*
  902. * Sanity check - the locking in
  903. * ocfs2_pagemkwrite() should ensure
  904. * that this code doesn't trigger.
  905. */
  906. ret = -EINVAL;
  907. mlog_errno(ret);
  908. goto out;
  909. }
  910. page_cache_get(mmap_page);
  911. wc->w_pages[i] = mmap_page;
  912. } else {
  913. wc->w_pages[i] = find_or_create_page(mapping, index,
  914. GFP_NOFS);
  915. if (!wc->w_pages[i]) {
  916. ret = -ENOMEM;
  917. mlog_errno(ret);
  918. goto out;
  919. }
  920. }
  921. if (index == target_index)
  922. wc->w_target_page = wc->w_pages[i];
  923. }
  924. out:
  925. return ret;
  926. }
  927. /*
  928. * Prepare a single cluster for write one cluster into the file.
  929. */
  930. static int ocfs2_write_cluster(struct address_space *mapping,
  931. u32 phys, unsigned int unwritten,
  932. struct ocfs2_alloc_context *data_ac,
  933. struct ocfs2_alloc_context *meta_ac,
  934. struct ocfs2_write_ctxt *wc, u32 cpos,
  935. loff_t user_pos, unsigned user_len)
  936. {
  937. int ret, i, new, should_zero = 0;
  938. u64 v_blkno, p_blkno;
  939. struct inode *inode = mapping->host;
  940. new = phys == 0 ? 1 : 0;
  941. if (new || unwritten)
  942. should_zero = 1;
  943. if (new) {
  944. u32 tmp_pos;
  945. /*
  946. * This is safe to call with the page locks - it won't take
  947. * any additional semaphores or cluster locks.
  948. */
  949. tmp_pos = cpos;
  950. ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
  951. &tmp_pos, 1, 0, wc->w_di_bh,
  952. wc->w_handle, data_ac,
  953. meta_ac, NULL);
  954. /*
  955. * This shouldn't happen because we must have already
  956. * calculated the correct meta data allocation required. The
  957. * internal tree allocation code should know how to increase
  958. * transaction credits itself.
  959. *
  960. * If need be, we could handle -EAGAIN for a
  961. * RESTART_TRANS here.
  962. */
  963. mlog_bug_on_msg(ret == -EAGAIN,
  964. "Inode %llu: EAGAIN return during allocation.\n",
  965. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  966. if (ret < 0) {
  967. mlog_errno(ret);
  968. goto out;
  969. }
  970. } else if (unwritten) {
  971. ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
  972. wc->w_handle, cpos, 1, phys,
  973. meta_ac, &wc->w_dealloc);
  974. if (ret < 0) {
  975. mlog_errno(ret);
  976. goto out;
  977. }
  978. }
  979. if (should_zero)
  980. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  981. else
  982. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  983. /*
  984. * The only reason this should fail is due to an inability to
  985. * find the extent added.
  986. */
  987. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  988. NULL);
  989. if (ret < 0) {
  990. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  991. "at logical block %llu",
  992. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  993. (unsigned long long)v_blkno);
  994. goto out;
  995. }
  996. BUG_ON(p_blkno == 0);
  997. for(i = 0; i < wc->w_num_pages; i++) {
  998. int tmpret;
  999. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1000. wc->w_pages[i], cpos,
  1001. user_pos, user_len,
  1002. should_zero);
  1003. if (tmpret) {
  1004. mlog_errno(tmpret);
  1005. if (ret == 0)
  1006. tmpret = ret;
  1007. }
  1008. }
  1009. /*
  1010. * We only have cleanup to do in case of allocating write.
  1011. */
  1012. if (ret && new)
  1013. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1014. out:
  1015. return ret;
  1016. }
  1017. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1018. struct ocfs2_alloc_context *data_ac,
  1019. struct ocfs2_alloc_context *meta_ac,
  1020. struct ocfs2_write_ctxt *wc,
  1021. loff_t pos, unsigned len)
  1022. {
  1023. int ret, i;
  1024. loff_t cluster_off;
  1025. unsigned int local_len = len;
  1026. struct ocfs2_write_cluster_desc *desc;
  1027. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1028. for (i = 0; i < wc->w_clen; i++) {
  1029. desc = &wc->w_desc[i];
  1030. /*
  1031. * We have to make sure that the total write passed in
  1032. * doesn't extend past a single cluster.
  1033. */
  1034. local_len = len;
  1035. cluster_off = pos & (osb->s_clustersize - 1);
  1036. if ((cluster_off + local_len) > osb->s_clustersize)
  1037. local_len = osb->s_clustersize - cluster_off;
  1038. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1039. desc->c_unwritten, data_ac, meta_ac,
  1040. wc, desc->c_cpos, pos, local_len);
  1041. if (ret) {
  1042. mlog_errno(ret);
  1043. goto out;
  1044. }
  1045. len -= local_len;
  1046. pos += local_len;
  1047. }
  1048. ret = 0;
  1049. out:
  1050. return ret;
  1051. }
  1052. /*
  1053. * ocfs2_write_end() wants to know which parts of the target page it
  1054. * should complete the write on. It's easiest to compute them ahead of
  1055. * time when a more complete view of the write is available.
  1056. */
  1057. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1058. struct ocfs2_write_ctxt *wc,
  1059. loff_t pos, unsigned len, int alloc)
  1060. {
  1061. struct ocfs2_write_cluster_desc *desc;
  1062. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1063. wc->w_target_to = wc->w_target_from + len;
  1064. if (alloc == 0)
  1065. return;
  1066. /*
  1067. * Allocating write - we may have different boundaries based
  1068. * on page size and cluster size.
  1069. *
  1070. * NOTE: We can no longer compute one value from the other as
  1071. * the actual write length and user provided length may be
  1072. * different.
  1073. */
  1074. if (wc->w_large_pages) {
  1075. /*
  1076. * We only care about the 1st and last cluster within
  1077. * our range and whether they should be zero'd or not. Either
  1078. * value may be extended out to the start/end of a
  1079. * newly allocated cluster.
  1080. */
  1081. desc = &wc->w_desc[0];
  1082. if (ocfs2_should_zero_cluster(desc))
  1083. ocfs2_figure_cluster_boundaries(osb,
  1084. desc->c_cpos,
  1085. &wc->w_target_from,
  1086. NULL);
  1087. desc = &wc->w_desc[wc->w_clen - 1];
  1088. if (ocfs2_should_zero_cluster(desc))
  1089. ocfs2_figure_cluster_boundaries(osb,
  1090. desc->c_cpos,
  1091. NULL,
  1092. &wc->w_target_to);
  1093. } else {
  1094. wc->w_target_from = 0;
  1095. wc->w_target_to = PAGE_CACHE_SIZE;
  1096. }
  1097. }
  1098. /*
  1099. * Populate each single-cluster write descriptor in the write context
  1100. * with information about the i/o to be done.
  1101. *
  1102. * Returns the number of clusters that will have to be allocated, as
  1103. * well as a worst case estimate of the number of extent records that
  1104. * would have to be created during a write to an unwritten region.
  1105. */
  1106. static int ocfs2_populate_write_desc(struct inode *inode,
  1107. struct ocfs2_write_ctxt *wc,
  1108. unsigned int *clusters_to_alloc,
  1109. unsigned int *extents_to_split)
  1110. {
  1111. int ret;
  1112. struct ocfs2_write_cluster_desc *desc;
  1113. unsigned int num_clusters = 0;
  1114. unsigned int ext_flags = 0;
  1115. u32 phys = 0;
  1116. int i;
  1117. *clusters_to_alloc = 0;
  1118. *extents_to_split = 0;
  1119. for (i = 0; i < wc->w_clen; i++) {
  1120. desc = &wc->w_desc[i];
  1121. desc->c_cpos = wc->w_cpos + i;
  1122. if (num_clusters == 0) {
  1123. /*
  1124. * Need to look up the next extent record.
  1125. */
  1126. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1127. &num_clusters, &ext_flags);
  1128. if (ret) {
  1129. mlog_errno(ret);
  1130. goto out;
  1131. }
  1132. /*
  1133. * Assume worst case - that we're writing in
  1134. * the middle of the extent.
  1135. *
  1136. * We can assume that the write proceeds from
  1137. * left to right, in which case the extent
  1138. * insert code is smart enough to coalesce the
  1139. * next splits into the previous records created.
  1140. */
  1141. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1142. *extents_to_split = *extents_to_split + 2;
  1143. } else if (phys) {
  1144. /*
  1145. * Only increment phys if it doesn't describe
  1146. * a hole.
  1147. */
  1148. phys++;
  1149. }
  1150. desc->c_phys = phys;
  1151. if (phys == 0) {
  1152. desc->c_new = 1;
  1153. *clusters_to_alloc = *clusters_to_alloc + 1;
  1154. }
  1155. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1156. desc->c_unwritten = 1;
  1157. num_clusters--;
  1158. }
  1159. ret = 0;
  1160. out:
  1161. return ret;
  1162. }
  1163. /*
  1164. * This function only does anything for file systems which can't
  1165. * handle sparse files.
  1166. *
  1167. * What we want to do here is fill in any hole between the current end
  1168. * of allocation and the end of our write. That way the rest of the
  1169. * write path can treat it as an non-allocating write, which has no
  1170. * special case code for sparse/nonsparse files.
  1171. */
  1172. static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
  1173. unsigned len,
  1174. struct ocfs2_write_ctxt *wc)
  1175. {
  1176. int ret;
  1177. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1178. loff_t newsize = pos + len;
  1179. if (ocfs2_sparse_alloc(osb))
  1180. return 0;
  1181. if (newsize <= i_size_read(inode))
  1182. return 0;
  1183. ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
  1184. if (ret)
  1185. mlog_errno(ret);
  1186. return ret;
  1187. }
  1188. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1189. loff_t pos, unsigned len, unsigned flags,
  1190. struct page **pagep, void **fsdata,
  1191. struct buffer_head *di_bh, struct page *mmap_page)
  1192. {
  1193. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1194. unsigned int clusters_to_alloc, extents_to_split;
  1195. struct ocfs2_write_ctxt *wc;
  1196. struct inode *inode = mapping->host;
  1197. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1198. struct ocfs2_dinode *di;
  1199. struct ocfs2_alloc_context *data_ac = NULL;
  1200. struct ocfs2_alloc_context *meta_ac = NULL;
  1201. handle_t *handle;
  1202. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1203. if (ret) {
  1204. mlog_errno(ret);
  1205. return ret;
  1206. }
  1207. ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
  1208. if (ret) {
  1209. mlog_errno(ret);
  1210. goto out;
  1211. }
  1212. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1213. &extents_to_split);
  1214. if (ret) {
  1215. mlog_errno(ret);
  1216. goto out;
  1217. }
  1218. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1219. /*
  1220. * We set w_target_from, w_target_to here so that
  1221. * ocfs2_write_end() knows which range in the target page to
  1222. * write out. An allocation requires that we write the entire
  1223. * cluster range.
  1224. */
  1225. if (clusters_to_alloc || extents_to_split) {
  1226. /*
  1227. * XXX: We are stretching the limits of
  1228. * ocfs2_lock_allocators(). It greatly over-estimates
  1229. * the work to be done.
  1230. */
  1231. ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
  1232. extents_to_split, &data_ac, &meta_ac);
  1233. if (ret) {
  1234. mlog_errno(ret);
  1235. goto out;
  1236. }
  1237. credits = ocfs2_calc_extend_credits(inode->i_sb, di,
  1238. clusters_to_alloc);
  1239. }
  1240. ocfs2_set_target_boundaries(osb, wc, pos, len,
  1241. clusters_to_alloc + extents_to_split);
  1242. handle = ocfs2_start_trans(osb, credits);
  1243. if (IS_ERR(handle)) {
  1244. ret = PTR_ERR(handle);
  1245. mlog_errno(ret);
  1246. goto out;
  1247. }
  1248. wc->w_handle = handle;
  1249. /*
  1250. * We don't want this to fail in ocfs2_write_end(), so do it
  1251. * here.
  1252. */
  1253. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1254. OCFS2_JOURNAL_ACCESS_WRITE);
  1255. if (ret) {
  1256. mlog_errno(ret);
  1257. goto out_commit;
  1258. }
  1259. /*
  1260. * Fill our page array first. That way we've grabbed enough so
  1261. * that we can zero and flush if we error after adding the
  1262. * extent.
  1263. */
  1264. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
  1265. clusters_to_alloc + extents_to_split,
  1266. mmap_page);
  1267. if (ret) {
  1268. mlog_errno(ret);
  1269. goto out_commit;
  1270. }
  1271. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1272. len);
  1273. if (ret) {
  1274. mlog_errno(ret);
  1275. goto out_commit;
  1276. }
  1277. if (data_ac)
  1278. ocfs2_free_alloc_context(data_ac);
  1279. if (meta_ac)
  1280. ocfs2_free_alloc_context(meta_ac);
  1281. *pagep = wc->w_target_page;
  1282. *fsdata = wc;
  1283. return 0;
  1284. out_commit:
  1285. ocfs2_commit_trans(osb, handle);
  1286. out:
  1287. ocfs2_free_write_ctxt(wc);
  1288. if (data_ac)
  1289. ocfs2_free_alloc_context(data_ac);
  1290. if (meta_ac)
  1291. ocfs2_free_alloc_context(meta_ac);
  1292. return ret;
  1293. }
  1294. int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1295. loff_t pos, unsigned len, unsigned flags,
  1296. struct page **pagep, void **fsdata)
  1297. {
  1298. int ret;
  1299. struct buffer_head *di_bh = NULL;
  1300. struct inode *inode = mapping->host;
  1301. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1302. if (ret) {
  1303. mlog_errno(ret);
  1304. return ret;
  1305. }
  1306. /*
  1307. * Take alloc sem here to prevent concurrent lookups. That way
  1308. * the mapping, zeroing and tree manipulation within
  1309. * ocfs2_write() will be safe against ->readpage(). This
  1310. * should also serve to lock out allocation from a shared
  1311. * writeable region.
  1312. */
  1313. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1314. ret = ocfs2_data_lock(inode, 1);
  1315. if (ret) {
  1316. mlog_errno(ret);
  1317. goto out_fail;
  1318. }
  1319. ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
  1320. fsdata, di_bh, NULL);
  1321. if (ret) {
  1322. mlog_errno(ret);
  1323. goto out_fail_data;
  1324. }
  1325. brelse(di_bh);
  1326. return 0;
  1327. out_fail_data:
  1328. ocfs2_data_unlock(inode, 1);
  1329. out_fail:
  1330. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1331. brelse(di_bh);
  1332. ocfs2_meta_unlock(inode, 1);
  1333. return ret;
  1334. }
  1335. int ocfs2_write_end_nolock(struct address_space *mapping,
  1336. loff_t pos, unsigned len, unsigned copied,
  1337. struct page *page, void *fsdata)
  1338. {
  1339. int i;
  1340. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1341. struct inode *inode = mapping->host;
  1342. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1343. struct ocfs2_write_ctxt *wc = fsdata;
  1344. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1345. handle_t *handle = wc->w_handle;
  1346. struct page *tmppage;
  1347. if (unlikely(copied < len)) {
  1348. if (!PageUptodate(wc->w_target_page))
  1349. copied = 0;
  1350. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1351. start+len);
  1352. }
  1353. flush_dcache_page(wc->w_target_page);
  1354. for(i = 0; i < wc->w_num_pages; i++) {
  1355. tmppage = wc->w_pages[i];
  1356. if (tmppage == wc->w_target_page) {
  1357. from = wc->w_target_from;
  1358. to = wc->w_target_to;
  1359. BUG_ON(from > PAGE_CACHE_SIZE ||
  1360. to > PAGE_CACHE_SIZE ||
  1361. to < from);
  1362. } else {
  1363. /*
  1364. * Pages adjacent to the target (if any) imply
  1365. * a hole-filling write in which case we want
  1366. * to flush their entire range.
  1367. */
  1368. from = 0;
  1369. to = PAGE_CACHE_SIZE;
  1370. }
  1371. if (ocfs2_should_order_data(inode))
  1372. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  1373. from, to, NULL,
  1374. ocfs2_journal_dirty_data);
  1375. block_commit_write(tmppage, from, to);
  1376. }
  1377. pos += copied;
  1378. if (pos > inode->i_size) {
  1379. i_size_write(inode, pos);
  1380. mark_inode_dirty(inode);
  1381. }
  1382. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1383. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1384. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1385. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1386. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1387. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1388. ocfs2_commit_trans(osb, handle);
  1389. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1390. ocfs2_free_write_ctxt(wc);
  1391. return copied;
  1392. }
  1393. int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1394. loff_t pos, unsigned len, unsigned copied,
  1395. struct page *page, void *fsdata)
  1396. {
  1397. int ret;
  1398. struct inode *inode = mapping->host;
  1399. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1400. ocfs2_data_unlock(inode, 1);
  1401. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1402. ocfs2_meta_unlock(inode, 1);
  1403. return ret;
  1404. }
  1405. const struct address_space_operations ocfs2_aops = {
  1406. .readpage = ocfs2_readpage,
  1407. .writepage = ocfs2_writepage,
  1408. .bmap = ocfs2_bmap,
  1409. .sync_page = block_sync_page,
  1410. .direct_IO = ocfs2_direct_IO,
  1411. .invalidatepage = ocfs2_invalidatepage,
  1412. .releasepage = ocfs2_releasepage,
  1413. .migratepage = buffer_migrate_page,
  1414. };