aops.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  260. ret = block_prepare_write(page, from, to, ocfs2_get_block);
  261. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  262. return ret;
  263. }
  264. /* Taken from ext3. We don't necessarily need the full blown
  265. * functionality yet, but IMHO it's better to cut and paste the whole
  266. * thing so we can avoid introducing our own bugs (and easily pick up
  267. * their fixes when they happen) --Mark */
  268. int walk_page_buffers( handle_t *handle,
  269. struct buffer_head *head,
  270. unsigned from,
  271. unsigned to,
  272. int *partial,
  273. int (*fn)( handle_t *handle,
  274. struct buffer_head *bh))
  275. {
  276. struct buffer_head *bh;
  277. unsigned block_start, block_end;
  278. unsigned blocksize = head->b_size;
  279. int err, ret = 0;
  280. struct buffer_head *next;
  281. for ( bh = head, block_start = 0;
  282. ret == 0 && (bh != head || !block_start);
  283. block_start = block_end, bh = next)
  284. {
  285. next = bh->b_this_page;
  286. block_end = block_start + blocksize;
  287. if (block_end <= from || block_start >= to) {
  288. if (partial && !buffer_uptodate(bh))
  289. *partial = 1;
  290. continue;
  291. }
  292. err = (*fn)(handle, bh);
  293. if (!ret)
  294. ret = err;
  295. }
  296. return ret;
  297. }
  298. handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
  299. struct page *page,
  300. unsigned from,
  301. unsigned to)
  302. {
  303. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  304. handle_t *handle = NULL;
  305. int ret = 0;
  306. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  307. if (!handle) {
  308. ret = -ENOMEM;
  309. mlog_errno(ret);
  310. goto out;
  311. }
  312. if (ocfs2_should_order_data(inode)) {
  313. ret = walk_page_buffers(handle,
  314. page_buffers(page),
  315. from, to, NULL,
  316. ocfs2_journal_dirty_data);
  317. if (ret < 0)
  318. mlog_errno(ret);
  319. }
  320. out:
  321. if (ret) {
  322. if (handle)
  323. ocfs2_commit_trans(osb, handle);
  324. handle = ERR_PTR(ret);
  325. }
  326. return handle;
  327. }
  328. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  329. {
  330. sector_t status;
  331. u64 p_blkno = 0;
  332. int err = 0;
  333. struct inode *inode = mapping->host;
  334. mlog_entry("(block = %llu)\n", (unsigned long long)block);
  335. /* We don't need to lock journal system files, since they aren't
  336. * accessed concurrently from multiple nodes.
  337. */
  338. if (!INODE_JOURNAL(inode)) {
  339. err = ocfs2_meta_lock(inode, NULL, 0);
  340. if (err) {
  341. if (err != -ENOENT)
  342. mlog_errno(err);
  343. goto bail;
  344. }
  345. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  346. }
  347. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
  348. if (!INODE_JOURNAL(inode)) {
  349. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  350. ocfs2_meta_unlock(inode, 0);
  351. }
  352. if (err) {
  353. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  354. (unsigned long long)block);
  355. mlog_errno(err);
  356. goto bail;
  357. }
  358. bail:
  359. status = err ? 0 : p_blkno;
  360. mlog_exit((int)status);
  361. return status;
  362. }
  363. /*
  364. * TODO: Make this into a generic get_blocks function.
  365. *
  366. * From do_direct_io in direct-io.c:
  367. * "So what we do is to permit the ->get_blocks function to populate
  368. * bh.b_size with the size of IO which is permitted at this offset and
  369. * this i_blkbits."
  370. *
  371. * This function is called directly from get_more_blocks in direct-io.c.
  372. *
  373. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  374. * fs_count, map_bh, dio->rw == WRITE);
  375. */
  376. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  377. struct buffer_head *bh_result, int create)
  378. {
  379. int ret;
  380. u64 p_blkno, inode_blocks, contig_blocks;
  381. unsigned int ext_flags;
  382. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  383. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  384. /* This function won't even be called if the request isn't all
  385. * nicely aligned and of the right size, so there's no need
  386. * for us to check any of that. */
  387. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  388. /*
  389. * Any write past EOF is not allowed because we'd be extending.
  390. */
  391. if (create && (iblock + max_blocks) > inode_blocks) {
  392. ret = -EIO;
  393. goto bail;
  394. }
  395. /* This figures out the size of the next contiguous block, and
  396. * our logical offset */
  397. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  398. &contig_blocks, &ext_flags);
  399. if (ret) {
  400. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  401. (unsigned long long)iblock);
  402. ret = -EIO;
  403. goto bail;
  404. }
  405. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
  406. ocfs2_error(inode->i_sb,
  407. "Inode %llu has a hole at block %llu\n",
  408. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  409. (unsigned long long)iblock);
  410. ret = -EROFS;
  411. goto bail;
  412. }
  413. /*
  414. * get_more_blocks() expects us to describe a hole by clearing
  415. * the mapped bit on bh_result().
  416. *
  417. * Consider an unwritten extent as a hole.
  418. */
  419. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  420. map_bh(bh_result, inode->i_sb, p_blkno);
  421. else {
  422. /*
  423. * ocfs2_prepare_inode_for_write() should have caught
  424. * the case where we'd be filling a hole and triggered
  425. * a buffered write instead.
  426. */
  427. if (create) {
  428. ret = -EIO;
  429. mlog_errno(ret);
  430. goto bail;
  431. }
  432. clear_buffer_mapped(bh_result);
  433. }
  434. /* make sure we don't map more than max_blocks blocks here as
  435. that's all the kernel will handle at this point. */
  436. if (max_blocks < contig_blocks)
  437. contig_blocks = max_blocks;
  438. bh_result->b_size = contig_blocks << blocksize_bits;
  439. bail:
  440. return ret;
  441. }
  442. /*
  443. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  444. * particularly interested in the aio/dio case. Like the core uses
  445. * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
  446. * truncation on another.
  447. */
  448. static void ocfs2_dio_end_io(struct kiocb *iocb,
  449. loff_t offset,
  450. ssize_t bytes,
  451. void *private)
  452. {
  453. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  454. int level;
  455. /* this io's submitter should not have unlocked this before we could */
  456. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  457. ocfs2_iocb_clear_rw_locked(iocb);
  458. level = ocfs2_iocb_rw_locked_level(iocb);
  459. if (!level)
  460. up_read(&inode->i_alloc_sem);
  461. ocfs2_rw_unlock(inode, level);
  462. }
  463. /*
  464. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  465. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  466. * do journalled data.
  467. */
  468. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  469. {
  470. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  471. journal_invalidatepage(journal, page, offset);
  472. }
  473. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  474. {
  475. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  476. if (!page_has_buffers(page))
  477. return 0;
  478. return journal_try_to_free_buffers(journal, page, wait);
  479. }
  480. static ssize_t ocfs2_direct_IO(int rw,
  481. struct kiocb *iocb,
  482. const struct iovec *iov,
  483. loff_t offset,
  484. unsigned long nr_segs)
  485. {
  486. struct file *file = iocb->ki_filp;
  487. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  488. int ret;
  489. mlog_entry_void();
  490. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  491. /*
  492. * We get PR data locks even for O_DIRECT. This
  493. * allows concurrent O_DIRECT I/O but doesn't let
  494. * O_DIRECT with extending and buffered zeroing writes
  495. * race. If they did race then the buffered zeroing
  496. * could be written back after the O_DIRECT I/O. It's
  497. * one thing to tell people not to mix buffered and
  498. * O_DIRECT writes, but expecting them to understand
  499. * that file extension is also an implicit buffered
  500. * write is too much. By getting the PR we force
  501. * writeback of the buffered zeroing before
  502. * proceeding.
  503. */
  504. ret = ocfs2_data_lock(inode, 0);
  505. if (ret < 0) {
  506. mlog_errno(ret);
  507. goto out;
  508. }
  509. ocfs2_data_unlock(inode, 0);
  510. }
  511. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  512. inode->i_sb->s_bdev, iov, offset,
  513. nr_segs,
  514. ocfs2_direct_IO_get_blocks,
  515. ocfs2_dio_end_io);
  516. out:
  517. mlog_exit(ret);
  518. return ret;
  519. }
  520. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  521. u32 cpos,
  522. unsigned int *start,
  523. unsigned int *end)
  524. {
  525. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  526. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  527. unsigned int cpp;
  528. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  529. cluster_start = cpos % cpp;
  530. cluster_start = cluster_start << osb->s_clustersize_bits;
  531. cluster_end = cluster_start + osb->s_clustersize;
  532. }
  533. BUG_ON(cluster_start > PAGE_SIZE);
  534. BUG_ON(cluster_end > PAGE_SIZE);
  535. if (start)
  536. *start = cluster_start;
  537. if (end)
  538. *end = cluster_end;
  539. }
  540. /*
  541. * 'from' and 'to' are the region in the page to avoid zeroing.
  542. *
  543. * If pagesize > clustersize, this function will avoid zeroing outside
  544. * of the cluster boundary.
  545. *
  546. * from == to == 0 is code for "zero the entire cluster region"
  547. */
  548. static void ocfs2_clear_page_regions(struct page *page,
  549. struct ocfs2_super *osb, u32 cpos,
  550. unsigned from, unsigned to)
  551. {
  552. void *kaddr;
  553. unsigned int cluster_start, cluster_end;
  554. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  555. kaddr = kmap_atomic(page, KM_USER0);
  556. if (from || to) {
  557. if (from > cluster_start)
  558. memset(kaddr + cluster_start, 0, from - cluster_start);
  559. if (to < cluster_end)
  560. memset(kaddr + to, 0, cluster_end - to);
  561. } else {
  562. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  563. }
  564. kunmap_atomic(kaddr, KM_USER0);
  565. }
  566. /*
  567. * Some of this taken from block_prepare_write(). We already have our
  568. * mapping by now though, and the entire write will be allocating or
  569. * it won't, so not much need to use BH_New.
  570. *
  571. * This will also skip zeroing, which is handled externally.
  572. */
  573. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  574. struct inode *inode, unsigned int from,
  575. unsigned int to, int new)
  576. {
  577. int ret = 0;
  578. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  579. unsigned int block_end, block_start;
  580. unsigned int bsize = 1 << inode->i_blkbits;
  581. if (!page_has_buffers(page))
  582. create_empty_buffers(page, bsize, 0);
  583. head = page_buffers(page);
  584. for (bh = head, block_start = 0; bh != head || !block_start;
  585. bh = bh->b_this_page, block_start += bsize) {
  586. block_end = block_start + bsize;
  587. clear_buffer_new(bh);
  588. /*
  589. * Ignore blocks outside of our i/o range -
  590. * they may belong to unallocated clusters.
  591. */
  592. if (block_start >= to || block_end <= from) {
  593. if (PageUptodate(page))
  594. set_buffer_uptodate(bh);
  595. continue;
  596. }
  597. /*
  598. * For an allocating write with cluster size >= page
  599. * size, we always write the entire page.
  600. */
  601. if (new)
  602. set_buffer_new(bh);
  603. if (!buffer_mapped(bh)) {
  604. map_bh(bh, inode->i_sb, *p_blkno);
  605. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  606. }
  607. if (PageUptodate(page)) {
  608. if (!buffer_uptodate(bh))
  609. set_buffer_uptodate(bh);
  610. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  611. !buffer_new(bh) &&
  612. (block_start < from || block_end > to)) {
  613. ll_rw_block(READ, 1, &bh);
  614. *wait_bh++=bh;
  615. }
  616. *p_blkno = *p_blkno + 1;
  617. }
  618. /*
  619. * If we issued read requests - let them complete.
  620. */
  621. while(wait_bh > wait) {
  622. wait_on_buffer(*--wait_bh);
  623. if (!buffer_uptodate(*wait_bh))
  624. ret = -EIO;
  625. }
  626. if (ret == 0 || !new)
  627. return ret;
  628. /*
  629. * If we get -EIO above, zero out any newly allocated blocks
  630. * to avoid exposing stale data.
  631. */
  632. bh = head;
  633. block_start = 0;
  634. do {
  635. block_end = block_start + bsize;
  636. if (block_end <= from)
  637. goto next_bh;
  638. if (block_start >= to)
  639. break;
  640. zero_user_page(page, block_start, bh->b_size, KM_USER0);
  641. set_buffer_uptodate(bh);
  642. mark_buffer_dirty(bh);
  643. next_bh:
  644. block_start = block_end;
  645. bh = bh->b_this_page;
  646. } while (bh != head);
  647. return ret;
  648. }
  649. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  650. #define OCFS2_MAX_CTXT_PAGES 1
  651. #else
  652. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  653. #endif
  654. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  655. /*
  656. * Describe the state of a single cluster to be written to.
  657. */
  658. struct ocfs2_write_cluster_desc {
  659. u32 c_cpos;
  660. u32 c_phys;
  661. /*
  662. * Give this a unique field because c_phys eventually gets
  663. * filled.
  664. */
  665. unsigned c_new;
  666. unsigned c_unwritten;
  667. };
  668. static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
  669. {
  670. return d->c_new || d->c_unwritten;
  671. }
  672. struct ocfs2_write_ctxt {
  673. /* Logical cluster position / len of write */
  674. u32 w_cpos;
  675. u32 w_clen;
  676. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  677. /*
  678. * This is true if page_size > cluster_size.
  679. *
  680. * It triggers a set of special cases during write which might
  681. * have to deal with allocating writes to partial pages.
  682. */
  683. unsigned int w_large_pages;
  684. /*
  685. * Pages involved in this write.
  686. *
  687. * w_target_page is the page being written to by the user.
  688. *
  689. * w_pages is an array of pages which always contains
  690. * w_target_page, and in the case of an allocating write with
  691. * page_size < cluster size, it will contain zero'd and mapped
  692. * pages adjacent to w_target_page which need to be written
  693. * out in so that future reads from that region will get
  694. * zero's.
  695. */
  696. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  697. unsigned int w_num_pages;
  698. struct page *w_target_page;
  699. /*
  700. * ocfs2_write_end() uses this to know what the real range to
  701. * write in the target should be.
  702. */
  703. unsigned int w_target_from;
  704. unsigned int w_target_to;
  705. /*
  706. * We could use journal_current_handle() but this is cleaner,
  707. * IMHO -Mark
  708. */
  709. handle_t *w_handle;
  710. struct buffer_head *w_di_bh;
  711. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  712. };
  713. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  714. {
  715. int i;
  716. for(i = 0; i < wc->w_num_pages; i++) {
  717. if (wc->w_pages[i] == NULL)
  718. continue;
  719. unlock_page(wc->w_pages[i]);
  720. mark_page_accessed(wc->w_pages[i]);
  721. page_cache_release(wc->w_pages[i]);
  722. }
  723. brelse(wc->w_di_bh);
  724. kfree(wc);
  725. }
  726. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  727. struct ocfs2_super *osb, loff_t pos,
  728. unsigned len, struct buffer_head *di_bh)
  729. {
  730. u32 cend;
  731. struct ocfs2_write_ctxt *wc;
  732. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  733. if (!wc)
  734. return -ENOMEM;
  735. wc->w_cpos = pos >> osb->s_clustersize_bits;
  736. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  737. wc->w_clen = cend - wc->w_cpos + 1;
  738. get_bh(di_bh);
  739. wc->w_di_bh = di_bh;
  740. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  741. wc->w_large_pages = 1;
  742. else
  743. wc->w_large_pages = 0;
  744. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  745. *wcp = wc;
  746. return 0;
  747. }
  748. /*
  749. * If a page has any new buffers, zero them out here, and mark them uptodate
  750. * and dirty so they'll be written out (in order to prevent uninitialised
  751. * block data from leaking). And clear the new bit.
  752. */
  753. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  754. {
  755. unsigned int block_start, block_end;
  756. struct buffer_head *head, *bh;
  757. BUG_ON(!PageLocked(page));
  758. if (!page_has_buffers(page))
  759. return;
  760. bh = head = page_buffers(page);
  761. block_start = 0;
  762. do {
  763. block_end = block_start + bh->b_size;
  764. if (buffer_new(bh)) {
  765. if (block_end > from && block_start < to) {
  766. if (!PageUptodate(page)) {
  767. unsigned start, end;
  768. start = max(from, block_start);
  769. end = min(to, block_end);
  770. zero_user_page(page, start, end - start, KM_USER0);
  771. set_buffer_uptodate(bh);
  772. }
  773. clear_buffer_new(bh);
  774. mark_buffer_dirty(bh);
  775. }
  776. }
  777. block_start = block_end;
  778. bh = bh->b_this_page;
  779. } while (bh != head);
  780. }
  781. /*
  782. * Only called when we have a failure during allocating write to write
  783. * zero's to the newly allocated region.
  784. */
  785. static void ocfs2_write_failure(struct inode *inode,
  786. struct ocfs2_write_ctxt *wc,
  787. loff_t user_pos, unsigned user_len)
  788. {
  789. int i;
  790. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  791. to = user_pos + user_len;
  792. struct page *tmppage;
  793. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  794. for(i = 0; i < wc->w_num_pages; i++) {
  795. tmppage = wc->w_pages[i];
  796. if (ocfs2_should_order_data(inode))
  797. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  798. from, to, NULL,
  799. ocfs2_journal_dirty_data);
  800. block_commit_write(tmppage, from, to);
  801. }
  802. }
  803. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  804. struct ocfs2_write_ctxt *wc,
  805. struct page *page, u32 cpos,
  806. loff_t user_pos, unsigned user_len,
  807. int new)
  808. {
  809. int ret;
  810. unsigned int map_from = 0, map_to = 0;
  811. unsigned int cluster_start, cluster_end;
  812. unsigned int user_data_from = 0, user_data_to = 0;
  813. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  814. &cluster_start, &cluster_end);
  815. if (page == wc->w_target_page) {
  816. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  817. map_to = map_from + user_len;
  818. if (new)
  819. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  820. cluster_start, cluster_end,
  821. new);
  822. else
  823. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  824. map_from, map_to, new);
  825. if (ret) {
  826. mlog_errno(ret);
  827. goto out;
  828. }
  829. user_data_from = map_from;
  830. user_data_to = map_to;
  831. if (new) {
  832. map_from = cluster_start;
  833. map_to = cluster_end;
  834. }
  835. } else {
  836. /*
  837. * If we haven't allocated the new page yet, we
  838. * shouldn't be writing it out without copying user
  839. * data. This is likely a math error from the caller.
  840. */
  841. BUG_ON(!new);
  842. map_from = cluster_start;
  843. map_to = cluster_end;
  844. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  845. cluster_start, cluster_end, new);
  846. if (ret) {
  847. mlog_errno(ret);
  848. goto out;
  849. }
  850. }
  851. /*
  852. * Parts of newly allocated pages need to be zero'd.
  853. *
  854. * Above, we have also rewritten 'to' and 'from' - as far as
  855. * the rest of the function is concerned, the entire cluster
  856. * range inside of a page needs to be written.
  857. *
  858. * We can skip this if the page is up to date - it's already
  859. * been zero'd from being read in as a hole.
  860. */
  861. if (new && !PageUptodate(page))
  862. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  863. cpos, user_data_from, user_data_to);
  864. flush_dcache_page(page);
  865. out:
  866. return ret;
  867. }
  868. /*
  869. * This function will only grab one clusters worth of pages.
  870. */
  871. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  872. struct ocfs2_write_ctxt *wc,
  873. u32 cpos, loff_t user_pos, int new,
  874. struct page *mmap_page)
  875. {
  876. int ret = 0, i;
  877. unsigned long start, target_index, index;
  878. struct inode *inode = mapping->host;
  879. target_index = user_pos >> PAGE_CACHE_SHIFT;
  880. /*
  881. * Figure out how many pages we'll be manipulating here. For
  882. * non allocating write, we just change the one
  883. * page. Otherwise, we'll need a whole clusters worth.
  884. */
  885. if (new) {
  886. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  887. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  888. } else {
  889. wc->w_num_pages = 1;
  890. start = target_index;
  891. }
  892. for(i = 0; i < wc->w_num_pages; i++) {
  893. index = start + i;
  894. if (index == target_index && mmap_page) {
  895. /*
  896. * ocfs2_pagemkwrite() is a little different
  897. * and wants us to directly use the page
  898. * passed in.
  899. */
  900. lock_page(mmap_page);
  901. if (mmap_page->mapping != mapping) {
  902. unlock_page(mmap_page);
  903. /*
  904. * Sanity check - the locking in
  905. * ocfs2_pagemkwrite() should ensure
  906. * that this code doesn't trigger.
  907. */
  908. ret = -EINVAL;
  909. mlog_errno(ret);
  910. goto out;
  911. }
  912. page_cache_get(mmap_page);
  913. wc->w_pages[i] = mmap_page;
  914. } else {
  915. wc->w_pages[i] = find_or_create_page(mapping, index,
  916. GFP_NOFS);
  917. if (!wc->w_pages[i]) {
  918. ret = -ENOMEM;
  919. mlog_errno(ret);
  920. goto out;
  921. }
  922. }
  923. if (index == target_index)
  924. wc->w_target_page = wc->w_pages[i];
  925. }
  926. out:
  927. return ret;
  928. }
  929. /*
  930. * Prepare a single cluster for write one cluster into the file.
  931. */
  932. static int ocfs2_write_cluster(struct address_space *mapping,
  933. u32 phys, unsigned int unwritten,
  934. struct ocfs2_alloc_context *data_ac,
  935. struct ocfs2_alloc_context *meta_ac,
  936. struct ocfs2_write_ctxt *wc, u32 cpos,
  937. loff_t user_pos, unsigned user_len)
  938. {
  939. int ret, i, new, should_zero = 0;
  940. u64 v_blkno, p_blkno;
  941. struct inode *inode = mapping->host;
  942. new = phys == 0 ? 1 : 0;
  943. if (new || unwritten)
  944. should_zero = 1;
  945. if (new) {
  946. u32 tmp_pos;
  947. /*
  948. * This is safe to call with the page locks - it won't take
  949. * any additional semaphores or cluster locks.
  950. */
  951. tmp_pos = cpos;
  952. ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
  953. &tmp_pos, 1, 0, wc->w_di_bh,
  954. wc->w_handle, data_ac,
  955. meta_ac, NULL);
  956. /*
  957. * This shouldn't happen because we must have already
  958. * calculated the correct meta data allocation required. The
  959. * internal tree allocation code should know how to increase
  960. * transaction credits itself.
  961. *
  962. * If need be, we could handle -EAGAIN for a
  963. * RESTART_TRANS here.
  964. */
  965. mlog_bug_on_msg(ret == -EAGAIN,
  966. "Inode %llu: EAGAIN return during allocation.\n",
  967. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  968. if (ret < 0) {
  969. mlog_errno(ret);
  970. goto out;
  971. }
  972. } else if (unwritten) {
  973. ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
  974. wc->w_handle, cpos, 1, phys,
  975. meta_ac, &wc->w_dealloc);
  976. if (ret < 0) {
  977. mlog_errno(ret);
  978. goto out;
  979. }
  980. }
  981. if (should_zero)
  982. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  983. else
  984. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  985. /*
  986. * The only reason this should fail is due to an inability to
  987. * find the extent added.
  988. */
  989. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  990. NULL);
  991. if (ret < 0) {
  992. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  993. "at logical block %llu",
  994. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  995. (unsigned long long)v_blkno);
  996. goto out;
  997. }
  998. BUG_ON(p_blkno == 0);
  999. for(i = 0; i < wc->w_num_pages; i++) {
  1000. int tmpret;
  1001. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1002. wc->w_pages[i], cpos,
  1003. user_pos, user_len,
  1004. should_zero);
  1005. if (tmpret) {
  1006. mlog_errno(tmpret);
  1007. if (ret == 0)
  1008. tmpret = ret;
  1009. }
  1010. }
  1011. /*
  1012. * We only have cleanup to do in case of allocating write.
  1013. */
  1014. if (ret && new)
  1015. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1016. out:
  1017. return ret;
  1018. }
  1019. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1020. struct ocfs2_alloc_context *data_ac,
  1021. struct ocfs2_alloc_context *meta_ac,
  1022. struct ocfs2_write_ctxt *wc,
  1023. loff_t pos, unsigned len)
  1024. {
  1025. int ret, i;
  1026. loff_t cluster_off;
  1027. unsigned int local_len = len;
  1028. struct ocfs2_write_cluster_desc *desc;
  1029. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1030. for (i = 0; i < wc->w_clen; i++) {
  1031. desc = &wc->w_desc[i];
  1032. /*
  1033. * We have to make sure that the total write passed in
  1034. * doesn't extend past a single cluster.
  1035. */
  1036. local_len = len;
  1037. cluster_off = pos & (osb->s_clustersize - 1);
  1038. if ((cluster_off + local_len) > osb->s_clustersize)
  1039. local_len = osb->s_clustersize - cluster_off;
  1040. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1041. desc->c_unwritten, data_ac, meta_ac,
  1042. wc, desc->c_cpos, pos, local_len);
  1043. if (ret) {
  1044. mlog_errno(ret);
  1045. goto out;
  1046. }
  1047. len -= local_len;
  1048. pos += local_len;
  1049. }
  1050. ret = 0;
  1051. out:
  1052. return ret;
  1053. }
  1054. /*
  1055. * ocfs2_write_end() wants to know which parts of the target page it
  1056. * should complete the write on. It's easiest to compute them ahead of
  1057. * time when a more complete view of the write is available.
  1058. */
  1059. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1060. struct ocfs2_write_ctxt *wc,
  1061. loff_t pos, unsigned len, int alloc)
  1062. {
  1063. struct ocfs2_write_cluster_desc *desc;
  1064. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1065. wc->w_target_to = wc->w_target_from + len;
  1066. if (alloc == 0)
  1067. return;
  1068. /*
  1069. * Allocating write - we may have different boundaries based
  1070. * on page size and cluster size.
  1071. *
  1072. * NOTE: We can no longer compute one value from the other as
  1073. * the actual write length and user provided length may be
  1074. * different.
  1075. */
  1076. if (wc->w_large_pages) {
  1077. /*
  1078. * We only care about the 1st and last cluster within
  1079. * our range and whether they should be zero'd or not. Either
  1080. * value may be extended out to the start/end of a
  1081. * newly allocated cluster.
  1082. */
  1083. desc = &wc->w_desc[0];
  1084. if (ocfs2_should_zero_cluster(desc))
  1085. ocfs2_figure_cluster_boundaries(osb,
  1086. desc->c_cpos,
  1087. &wc->w_target_from,
  1088. NULL);
  1089. desc = &wc->w_desc[wc->w_clen - 1];
  1090. if (ocfs2_should_zero_cluster(desc))
  1091. ocfs2_figure_cluster_boundaries(osb,
  1092. desc->c_cpos,
  1093. NULL,
  1094. &wc->w_target_to);
  1095. } else {
  1096. wc->w_target_from = 0;
  1097. wc->w_target_to = PAGE_CACHE_SIZE;
  1098. }
  1099. }
  1100. /*
  1101. * Populate each single-cluster write descriptor in the write context
  1102. * with information about the i/o to be done.
  1103. *
  1104. * Returns the number of clusters that will have to be allocated, as
  1105. * well as a worst case estimate of the number of extent records that
  1106. * would have to be created during a write to an unwritten region.
  1107. */
  1108. static int ocfs2_populate_write_desc(struct inode *inode,
  1109. struct ocfs2_write_ctxt *wc,
  1110. unsigned int *clusters_to_alloc,
  1111. unsigned int *extents_to_split)
  1112. {
  1113. int ret;
  1114. struct ocfs2_write_cluster_desc *desc;
  1115. unsigned int num_clusters = 0;
  1116. unsigned int ext_flags = 0;
  1117. u32 phys = 0;
  1118. int i;
  1119. *clusters_to_alloc = 0;
  1120. *extents_to_split = 0;
  1121. for (i = 0; i < wc->w_clen; i++) {
  1122. desc = &wc->w_desc[i];
  1123. desc->c_cpos = wc->w_cpos + i;
  1124. if (num_clusters == 0) {
  1125. /*
  1126. * Need to look up the next extent record.
  1127. */
  1128. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1129. &num_clusters, &ext_flags);
  1130. if (ret) {
  1131. mlog_errno(ret);
  1132. goto out;
  1133. }
  1134. /*
  1135. * Assume worst case - that we're writing in
  1136. * the middle of the extent.
  1137. *
  1138. * We can assume that the write proceeds from
  1139. * left to right, in which case the extent
  1140. * insert code is smart enough to coalesce the
  1141. * next splits into the previous records created.
  1142. */
  1143. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1144. *extents_to_split = *extents_to_split + 2;
  1145. } else if (phys) {
  1146. /*
  1147. * Only increment phys if it doesn't describe
  1148. * a hole.
  1149. */
  1150. phys++;
  1151. }
  1152. desc->c_phys = phys;
  1153. if (phys == 0) {
  1154. desc->c_new = 1;
  1155. *clusters_to_alloc = *clusters_to_alloc + 1;
  1156. }
  1157. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1158. desc->c_unwritten = 1;
  1159. num_clusters--;
  1160. }
  1161. ret = 0;
  1162. out:
  1163. return ret;
  1164. }
  1165. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1166. loff_t pos, unsigned len, unsigned flags,
  1167. struct page **pagep, void **fsdata,
  1168. struct buffer_head *di_bh, struct page *mmap_page)
  1169. {
  1170. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1171. unsigned int clusters_to_alloc, extents_to_split;
  1172. struct ocfs2_write_ctxt *wc;
  1173. struct inode *inode = mapping->host;
  1174. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1175. struct ocfs2_dinode *di;
  1176. struct ocfs2_alloc_context *data_ac = NULL;
  1177. struct ocfs2_alloc_context *meta_ac = NULL;
  1178. handle_t *handle;
  1179. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1180. if (ret) {
  1181. mlog_errno(ret);
  1182. return ret;
  1183. }
  1184. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1185. &extents_to_split);
  1186. if (ret) {
  1187. mlog_errno(ret);
  1188. goto out;
  1189. }
  1190. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1191. /*
  1192. * We set w_target_from, w_target_to here so that
  1193. * ocfs2_write_end() knows which range in the target page to
  1194. * write out. An allocation requires that we write the entire
  1195. * cluster range.
  1196. */
  1197. if (clusters_to_alloc || extents_to_split) {
  1198. /*
  1199. * XXX: We are stretching the limits of
  1200. * ocfs2_lock_allocators(). It greatly over-estimates
  1201. * the work to be done.
  1202. */
  1203. ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
  1204. extents_to_split, &data_ac, &meta_ac);
  1205. if (ret) {
  1206. mlog_errno(ret);
  1207. goto out;
  1208. }
  1209. credits = ocfs2_calc_extend_credits(inode->i_sb, di,
  1210. clusters_to_alloc);
  1211. }
  1212. ocfs2_set_target_boundaries(osb, wc, pos, len,
  1213. clusters_to_alloc + extents_to_split);
  1214. handle = ocfs2_start_trans(osb, credits);
  1215. if (IS_ERR(handle)) {
  1216. ret = PTR_ERR(handle);
  1217. mlog_errno(ret);
  1218. goto out;
  1219. }
  1220. wc->w_handle = handle;
  1221. /*
  1222. * We don't want this to fail in ocfs2_write_end(), so do it
  1223. * here.
  1224. */
  1225. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1226. OCFS2_JOURNAL_ACCESS_WRITE);
  1227. if (ret) {
  1228. mlog_errno(ret);
  1229. goto out_commit;
  1230. }
  1231. /*
  1232. * Fill our page array first. That way we've grabbed enough so
  1233. * that we can zero and flush if we error after adding the
  1234. * extent.
  1235. */
  1236. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
  1237. clusters_to_alloc + extents_to_split,
  1238. mmap_page);
  1239. if (ret) {
  1240. mlog_errno(ret);
  1241. goto out_commit;
  1242. }
  1243. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1244. len);
  1245. if (ret) {
  1246. mlog_errno(ret);
  1247. goto out_commit;
  1248. }
  1249. if (data_ac)
  1250. ocfs2_free_alloc_context(data_ac);
  1251. if (meta_ac)
  1252. ocfs2_free_alloc_context(meta_ac);
  1253. *pagep = wc->w_target_page;
  1254. *fsdata = wc;
  1255. return 0;
  1256. out_commit:
  1257. ocfs2_commit_trans(osb, handle);
  1258. out:
  1259. ocfs2_free_write_ctxt(wc);
  1260. if (data_ac)
  1261. ocfs2_free_alloc_context(data_ac);
  1262. if (meta_ac)
  1263. ocfs2_free_alloc_context(meta_ac);
  1264. return ret;
  1265. }
  1266. int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1267. loff_t pos, unsigned len, unsigned flags,
  1268. struct page **pagep, void **fsdata)
  1269. {
  1270. int ret;
  1271. struct buffer_head *di_bh = NULL;
  1272. struct inode *inode = mapping->host;
  1273. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1274. if (ret) {
  1275. mlog_errno(ret);
  1276. return ret;
  1277. }
  1278. /*
  1279. * Take alloc sem here to prevent concurrent lookups. That way
  1280. * the mapping, zeroing and tree manipulation within
  1281. * ocfs2_write() will be safe against ->readpage(). This
  1282. * should also serve to lock out allocation from a shared
  1283. * writeable region.
  1284. */
  1285. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1286. ret = ocfs2_data_lock(inode, 1);
  1287. if (ret) {
  1288. mlog_errno(ret);
  1289. goto out_fail;
  1290. }
  1291. ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
  1292. fsdata, di_bh, NULL);
  1293. if (ret) {
  1294. mlog_errno(ret);
  1295. goto out_fail_data;
  1296. }
  1297. brelse(di_bh);
  1298. return 0;
  1299. out_fail_data:
  1300. ocfs2_data_unlock(inode, 1);
  1301. out_fail:
  1302. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1303. brelse(di_bh);
  1304. ocfs2_meta_unlock(inode, 1);
  1305. return ret;
  1306. }
  1307. int ocfs2_write_end_nolock(struct address_space *mapping,
  1308. loff_t pos, unsigned len, unsigned copied,
  1309. struct page *page, void *fsdata)
  1310. {
  1311. int i;
  1312. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1313. struct inode *inode = mapping->host;
  1314. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1315. struct ocfs2_write_ctxt *wc = fsdata;
  1316. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1317. handle_t *handle = wc->w_handle;
  1318. struct page *tmppage;
  1319. if (unlikely(copied < len)) {
  1320. if (!PageUptodate(wc->w_target_page))
  1321. copied = 0;
  1322. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1323. start+len);
  1324. }
  1325. flush_dcache_page(wc->w_target_page);
  1326. for(i = 0; i < wc->w_num_pages; i++) {
  1327. tmppage = wc->w_pages[i];
  1328. if (tmppage == wc->w_target_page) {
  1329. from = wc->w_target_from;
  1330. to = wc->w_target_to;
  1331. BUG_ON(from > PAGE_CACHE_SIZE ||
  1332. to > PAGE_CACHE_SIZE ||
  1333. to < from);
  1334. } else {
  1335. /*
  1336. * Pages adjacent to the target (if any) imply
  1337. * a hole-filling write in which case we want
  1338. * to flush their entire range.
  1339. */
  1340. from = 0;
  1341. to = PAGE_CACHE_SIZE;
  1342. }
  1343. if (ocfs2_should_order_data(inode))
  1344. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  1345. from, to, NULL,
  1346. ocfs2_journal_dirty_data);
  1347. block_commit_write(tmppage, from, to);
  1348. }
  1349. pos += copied;
  1350. if (pos > inode->i_size) {
  1351. i_size_write(inode, pos);
  1352. mark_inode_dirty(inode);
  1353. }
  1354. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1355. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1356. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1357. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1358. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1359. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1360. ocfs2_commit_trans(osb, handle);
  1361. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1362. ocfs2_free_write_ctxt(wc);
  1363. return copied;
  1364. }
  1365. int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1366. loff_t pos, unsigned len, unsigned copied,
  1367. struct page *page, void *fsdata)
  1368. {
  1369. int ret;
  1370. struct inode *inode = mapping->host;
  1371. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1372. ocfs2_data_unlock(inode, 1);
  1373. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1374. ocfs2_meta_unlock(inode, 1);
  1375. return ret;
  1376. }
  1377. const struct address_space_operations ocfs2_aops = {
  1378. .readpage = ocfs2_readpage,
  1379. .writepage = ocfs2_writepage,
  1380. .bmap = ocfs2_bmap,
  1381. .sync_page = block_sync_page,
  1382. .direct_IO = ocfs2_direct_IO,
  1383. .invalidatepage = ocfs2_invalidatepage,
  1384. .releasepage = ocfs2_releasepage,
  1385. .migratepage = buffer_migrate_page,
  1386. };