aops.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  712. {
  713. int i;
  714. for(i = 0; i < num_pages; i++) {
  715. if (pages[i]) {
  716. unlock_page(pages[i]);
  717. mark_page_accessed(pages[i]);
  718. page_cache_release(pages[i]);
  719. }
  720. }
  721. }
  722. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  723. {
  724. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  725. brelse(wc->w_di_bh);
  726. kfree(wc);
  727. }
  728. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  729. struct ocfs2_super *osb, loff_t pos,
  730. unsigned len, struct buffer_head *di_bh)
  731. {
  732. u32 cend;
  733. struct ocfs2_write_ctxt *wc;
  734. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  735. if (!wc)
  736. return -ENOMEM;
  737. wc->w_cpos = pos >> osb->s_clustersize_bits;
  738. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  739. wc->w_clen = cend - wc->w_cpos + 1;
  740. get_bh(di_bh);
  741. wc->w_di_bh = di_bh;
  742. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  743. wc->w_large_pages = 1;
  744. else
  745. wc->w_large_pages = 0;
  746. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  747. *wcp = wc;
  748. return 0;
  749. }
  750. /*
  751. * If a page has any new buffers, zero them out here, and mark them uptodate
  752. * and dirty so they'll be written out (in order to prevent uninitialised
  753. * block data from leaking). And clear the new bit.
  754. */
  755. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  756. {
  757. unsigned int block_start, block_end;
  758. struct buffer_head *head, *bh;
  759. BUG_ON(!PageLocked(page));
  760. if (!page_has_buffers(page))
  761. return;
  762. bh = head = page_buffers(page);
  763. block_start = 0;
  764. do {
  765. block_end = block_start + bh->b_size;
  766. if (buffer_new(bh)) {
  767. if (block_end > from && block_start < to) {
  768. if (!PageUptodate(page)) {
  769. unsigned start, end;
  770. start = max(from, block_start);
  771. end = min(to, block_end);
  772. zero_user_page(page, start, end - start, KM_USER0);
  773. set_buffer_uptodate(bh);
  774. }
  775. clear_buffer_new(bh);
  776. mark_buffer_dirty(bh);
  777. }
  778. }
  779. block_start = block_end;
  780. bh = bh->b_this_page;
  781. } while (bh != head);
  782. }
  783. /*
  784. * Only called when we have a failure during allocating write to write
  785. * zero's to the newly allocated region.
  786. */
  787. static void ocfs2_write_failure(struct inode *inode,
  788. struct ocfs2_write_ctxt *wc,
  789. loff_t user_pos, unsigned user_len)
  790. {
  791. int i;
  792. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  793. to = user_pos + user_len;
  794. struct page *tmppage;
  795. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  796. for(i = 0; i < wc->w_num_pages; i++) {
  797. tmppage = wc->w_pages[i];
  798. if (ocfs2_should_order_data(inode))
  799. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  800. from, to, NULL,
  801. ocfs2_journal_dirty_data);
  802. block_commit_write(tmppage, from, to);
  803. }
  804. }
  805. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  806. struct ocfs2_write_ctxt *wc,
  807. struct page *page, u32 cpos,
  808. loff_t user_pos, unsigned user_len,
  809. int new)
  810. {
  811. int ret;
  812. unsigned int map_from = 0, map_to = 0;
  813. unsigned int cluster_start, cluster_end;
  814. unsigned int user_data_from = 0, user_data_to = 0;
  815. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  816. &cluster_start, &cluster_end);
  817. if (page == wc->w_target_page) {
  818. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  819. map_to = map_from + user_len;
  820. if (new)
  821. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  822. cluster_start, cluster_end,
  823. new);
  824. else
  825. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  826. map_from, map_to, new);
  827. if (ret) {
  828. mlog_errno(ret);
  829. goto out;
  830. }
  831. user_data_from = map_from;
  832. user_data_to = map_to;
  833. if (new) {
  834. map_from = cluster_start;
  835. map_to = cluster_end;
  836. }
  837. } else {
  838. /*
  839. * If we haven't allocated the new page yet, we
  840. * shouldn't be writing it out without copying user
  841. * data. This is likely a math error from the caller.
  842. */
  843. BUG_ON(!new);
  844. map_from = cluster_start;
  845. map_to = cluster_end;
  846. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  847. cluster_start, cluster_end, new);
  848. if (ret) {
  849. mlog_errno(ret);
  850. goto out;
  851. }
  852. }
  853. /*
  854. * Parts of newly allocated pages need to be zero'd.
  855. *
  856. * Above, we have also rewritten 'to' and 'from' - as far as
  857. * the rest of the function is concerned, the entire cluster
  858. * range inside of a page needs to be written.
  859. *
  860. * We can skip this if the page is up to date - it's already
  861. * been zero'd from being read in as a hole.
  862. */
  863. if (new && !PageUptodate(page))
  864. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  865. cpos, user_data_from, user_data_to);
  866. flush_dcache_page(page);
  867. out:
  868. return ret;
  869. }
  870. /*
  871. * This function will only grab one clusters worth of pages.
  872. */
  873. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  874. struct ocfs2_write_ctxt *wc,
  875. u32 cpos, loff_t user_pos, int new,
  876. struct page *mmap_page)
  877. {
  878. int ret = 0, i;
  879. unsigned long start, target_index, index;
  880. struct inode *inode = mapping->host;
  881. target_index = user_pos >> PAGE_CACHE_SHIFT;
  882. /*
  883. * Figure out how many pages we'll be manipulating here. For
  884. * non allocating write, we just change the one
  885. * page. Otherwise, we'll need a whole clusters worth.
  886. */
  887. if (new) {
  888. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  889. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  890. } else {
  891. wc->w_num_pages = 1;
  892. start = target_index;
  893. }
  894. for(i = 0; i < wc->w_num_pages; i++) {
  895. index = start + i;
  896. if (index == target_index && mmap_page) {
  897. /*
  898. * ocfs2_pagemkwrite() is a little different
  899. * and wants us to directly use the page
  900. * passed in.
  901. */
  902. lock_page(mmap_page);
  903. if (mmap_page->mapping != mapping) {
  904. unlock_page(mmap_page);
  905. /*
  906. * Sanity check - the locking in
  907. * ocfs2_pagemkwrite() should ensure
  908. * that this code doesn't trigger.
  909. */
  910. ret = -EINVAL;
  911. mlog_errno(ret);
  912. goto out;
  913. }
  914. page_cache_get(mmap_page);
  915. wc->w_pages[i] = mmap_page;
  916. } else {
  917. wc->w_pages[i] = find_or_create_page(mapping, index,
  918. GFP_NOFS);
  919. if (!wc->w_pages[i]) {
  920. ret = -ENOMEM;
  921. mlog_errno(ret);
  922. goto out;
  923. }
  924. }
  925. if (index == target_index)
  926. wc->w_target_page = wc->w_pages[i];
  927. }
  928. out:
  929. return ret;
  930. }
  931. /*
  932. * Prepare a single cluster for write one cluster into the file.
  933. */
  934. static int ocfs2_write_cluster(struct address_space *mapping,
  935. u32 phys, unsigned int unwritten,
  936. struct ocfs2_alloc_context *data_ac,
  937. struct ocfs2_alloc_context *meta_ac,
  938. struct ocfs2_write_ctxt *wc, u32 cpos,
  939. loff_t user_pos, unsigned user_len)
  940. {
  941. int ret, i, new, should_zero = 0;
  942. u64 v_blkno, p_blkno;
  943. struct inode *inode = mapping->host;
  944. new = phys == 0 ? 1 : 0;
  945. if (new || unwritten)
  946. should_zero = 1;
  947. if (new) {
  948. u32 tmp_pos;
  949. /*
  950. * This is safe to call with the page locks - it won't take
  951. * any additional semaphores or cluster locks.
  952. */
  953. tmp_pos = cpos;
  954. ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
  955. &tmp_pos, 1, 0, wc->w_di_bh,
  956. wc->w_handle, data_ac,
  957. meta_ac, NULL);
  958. /*
  959. * This shouldn't happen because we must have already
  960. * calculated the correct meta data allocation required. The
  961. * internal tree allocation code should know how to increase
  962. * transaction credits itself.
  963. *
  964. * If need be, we could handle -EAGAIN for a
  965. * RESTART_TRANS here.
  966. */
  967. mlog_bug_on_msg(ret == -EAGAIN,
  968. "Inode %llu: EAGAIN return during allocation.\n",
  969. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  970. if (ret < 0) {
  971. mlog_errno(ret);
  972. goto out;
  973. }
  974. } else if (unwritten) {
  975. ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
  976. wc->w_handle, cpos, 1, phys,
  977. meta_ac, &wc->w_dealloc);
  978. if (ret < 0) {
  979. mlog_errno(ret);
  980. goto out;
  981. }
  982. }
  983. if (should_zero)
  984. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  985. else
  986. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  987. /*
  988. * The only reason this should fail is due to an inability to
  989. * find the extent added.
  990. */
  991. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  992. NULL);
  993. if (ret < 0) {
  994. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  995. "at logical block %llu",
  996. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  997. (unsigned long long)v_blkno);
  998. goto out;
  999. }
  1000. BUG_ON(p_blkno == 0);
  1001. for(i = 0; i < wc->w_num_pages; i++) {
  1002. int tmpret;
  1003. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1004. wc->w_pages[i], cpos,
  1005. user_pos, user_len,
  1006. should_zero);
  1007. if (tmpret) {
  1008. mlog_errno(tmpret);
  1009. if (ret == 0)
  1010. tmpret = ret;
  1011. }
  1012. }
  1013. /*
  1014. * We only have cleanup to do in case of allocating write.
  1015. */
  1016. if (ret && new)
  1017. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1018. out:
  1019. return ret;
  1020. }
  1021. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1022. struct ocfs2_alloc_context *data_ac,
  1023. struct ocfs2_alloc_context *meta_ac,
  1024. struct ocfs2_write_ctxt *wc,
  1025. loff_t pos, unsigned len)
  1026. {
  1027. int ret, i;
  1028. loff_t cluster_off;
  1029. unsigned int local_len = len;
  1030. struct ocfs2_write_cluster_desc *desc;
  1031. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1032. for (i = 0; i < wc->w_clen; i++) {
  1033. desc = &wc->w_desc[i];
  1034. /*
  1035. * We have to make sure that the total write passed in
  1036. * doesn't extend past a single cluster.
  1037. */
  1038. local_len = len;
  1039. cluster_off = pos & (osb->s_clustersize - 1);
  1040. if ((cluster_off + local_len) > osb->s_clustersize)
  1041. local_len = osb->s_clustersize - cluster_off;
  1042. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1043. desc->c_unwritten, data_ac, meta_ac,
  1044. wc, desc->c_cpos, pos, local_len);
  1045. if (ret) {
  1046. mlog_errno(ret);
  1047. goto out;
  1048. }
  1049. len -= local_len;
  1050. pos += local_len;
  1051. }
  1052. ret = 0;
  1053. out:
  1054. return ret;
  1055. }
  1056. /*
  1057. * ocfs2_write_end() wants to know which parts of the target page it
  1058. * should complete the write on. It's easiest to compute them ahead of
  1059. * time when a more complete view of the write is available.
  1060. */
  1061. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1062. struct ocfs2_write_ctxt *wc,
  1063. loff_t pos, unsigned len, int alloc)
  1064. {
  1065. struct ocfs2_write_cluster_desc *desc;
  1066. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1067. wc->w_target_to = wc->w_target_from + len;
  1068. if (alloc == 0)
  1069. return;
  1070. /*
  1071. * Allocating write - we may have different boundaries based
  1072. * on page size and cluster size.
  1073. *
  1074. * NOTE: We can no longer compute one value from the other as
  1075. * the actual write length and user provided length may be
  1076. * different.
  1077. */
  1078. if (wc->w_large_pages) {
  1079. /*
  1080. * We only care about the 1st and last cluster within
  1081. * our range and whether they should be zero'd or not. Either
  1082. * value may be extended out to the start/end of a
  1083. * newly allocated cluster.
  1084. */
  1085. desc = &wc->w_desc[0];
  1086. if (ocfs2_should_zero_cluster(desc))
  1087. ocfs2_figure_cluster_boundaries(osb,
  1088. desc->c_cpos,
  1089. &wc->w_target_from,
  1090. NULL);
  1091. desc = &wc->w_desc[wc->w_clen - 1];
  1092. if (ocfs2_should_zero_cluster(desc))
  1093. ocfs2_figure_cluster_boundaries(osb,
  1094. desc->c_cpos,
  1095. NULL,
  1096. &wc->w_target_to);
  1097. } else {
  1098. wc->w_target_from = 0;
  1099. wc->w_target_to = PAGE_CACHE_SIZE;
  1100. }
  1101. }
  1102. /*
  1103. * Populate each single-cluster write descriptor in the write context
  1104. * with information about the i/o to be done.
  1105. *
  1106. * Returns the number of clusters that will have to be allocated, as
  1107. * well as a worst case estimate of the number of extent records that
  1108. * would have to be created during a write to an unwritten region.
  1109. */
  1110. static int ocfs2_populate_write_desc(struct inode *inode,
  1111. struct ocfs2_write_ctxt *wc,
  1112. unsigned int *clusters_to_alloc,
  1113. unsigned int *extents_to_split)
  1114. {
  1115. int ret;
  1116. struct ocfs2_write_cluster_desc *desc;
  1117. unsigned int num_clusters = 0;
  1118. unsigned int ext_flags = 0;
  1119. u32 phys = 0;
  1120. int i;
  1121. *clusters_to_alloc = 0;
  1122. *extents_to_split = 0;
  1123. for (i = 0; i < wc->w_clen; i++) {
  1124. desc = &wc->w_desc[i];
  1125. desc->c_cpos = wc->w_cpos + i;
  1126. if (num_clusters == 0) {
  1127. /*
  1128. * Need to look up the next extent record.
  1129. */
  1130. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1131. &num_clusters, &ext_flags);
  1132. if (ret) {
  1133. mlog_errno(ret);
  1134. goto out;
  1135. }
  1136. /*
  1137. * Assume worst case - that we're writing in
  1138. * the middle of the extent.
  1139. *
  1140. * We can assume that the write proceeds from
  1141. * left to right, in which case the extent
  1142. * insert code is smart enough to coalesce the
  1143. * next splits into the previous records created.
  1144. */
  1145. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1146. *extents_to_split = *extents_to_split + 2;
  1147. } else if (phys) {
  1148. /*
  1149. * Only increment phys if it doesn't describe
  1150. * a hole.
  1151. */
  1152. phys++;
  1153. }
  1154. desc->c_phys = phys;
  1155. if (phys == 0) {
  1156. desc->c_new = 1;
  1157. *clusters_to_alloc = *clusters_to_alloc + 1;
  1158. }
  1159. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1160. desc->c_unwritten = 1;
  1161. num_clusters--;
  1162. }
  1163. ret = 0;
  1164. out:
  1165. return ret;
  1166. }
  1167. /*
  1168. * This function only does anything for file systems which can't
  1169. * handle sparse files.
  1170. *
  1171. * What we want to do here is fill in any hole between the current end
  1172. * of allocation and the end of our write. That way the rest of the
  1173. * write path can treat it as an non-allocating write, which has no
  1174. * special case code for sparse/nonsparse files.
  1175. */
  1176. static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
  1177. unsigned len,
  1178. struct ocfs2_write_ctxt *wc)
  1179. {
  1180. int ret;
  1181. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1182. loff_t newsize = pos + len;
  1183. if (ocfs2_sparse_alloc(osb))
  1184. return 0;
  1185. if (newsize <= i_size_read(inode))
  1186. return 0;
  1187. ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
  1188. if (ret)
  1189. mlog_errno(ret);
  1190. return ret;
  1191. }
  1192. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1193. loff_t pos, unsigned len, unsigned flags,
  1194. struct page **pagep, void **fsdata,
  1195. struct buffer_head *di_bh, struct page *mmap_page)
  1196. {
  1197. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1198. unsigned int clusters_to_alloc, extents_to_split;
  1199. struct ocfs2_write_ctxt *wc;
  1200. struct inode *inode = mapping->host;
  1201. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1202. struct ocfs2_dinode *di;
  1203. struct ocfs2_alloc_context *data_ac = NULL;
  1204. struct ocfs2_alloc_context *meta_ac = NULL;
  1205. handle_t *handle;
  1206. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1207. if (ret) {
  1208. mlog_errno(ret);
  1209. return ret;
  1210. }
  1211. ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
  1212. if (ret) {
  1213. mlog_errno(ret);
  1214. goto out;
  1215. }
  1216. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1217. &extents_to_split);
  1218. if (ret) {
  1219. mlog_errno(ret);
  1220. goto out;
  1221. }
  1222. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1223. /*
  1224. * We set w_target_from, w_target_to here so that
  1225. * ocfs2_write_end() knows which range in the target page to
  1226. * write out. An allocation requires that we write the entire
  1227. * cluster range.
  1228. */
  1229. if (clusters_to_alloc || extents_to_split) {
  1230. /*
  1231. * XXX: We are stretching the limits of
  1232. * ocfs2_lock_allocators(). It greatly over-estimates
  1233. * the work to be done.
  1234. */
  1235. ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
  1236. extents_to_split, &data_ac, &meta_ac);
  1237. if (ret) {
  1238. mlog_errno(ret);
  1239. goto out;
  1240. }
  1241. credits = ocfs2_calc_extend_credits(inode->i_sb, di,
  1242. clusters_to_alloc);
  1243. }
  1244. ocfs2_set_target_boundaries(osb, wc, pos, len,
  1245. clusters_to_alloc + extents_to_split);
  1246. handle = ocfs2_start_trans(osb, credits);
  1247. if (IS_ERR(handle)) {
  1248. ret = PTR_ERR(handle);
  1249. mlog_errno(ret);
  1250. goto out;
  1251. }
  1252. wc->w_handle = handle;
  1253. /*
  1254. * We don't want this to fail in ocfs2_write_end(), so do it
  1255. * here.
  1256. */
  1257. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1258. OCFS2_JOURNAL_ACCESS_WRITE);
  1259. if (ret) {
  1260. mlog_errno(ret);
  1261. goto out_commit;
  1262. }
  1263. /*
  1264. * Fill our page array first. That way we've grabbed enough so
  1265. * that we can zero and flush if we error after adding the
  1266. * extent.
  1267. */
  1268. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
  1269. clusters_to_alloc + extents_to_split,
  1270. mmap_page);
  1271. if (ret) {
  1272. mlog_errno(ret);
  1273. goto out_commit;
  1274. }
  1275. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1276. len);
  1277. if (ret) {
  1278. mlog_errno(ret);
  1279. goto out_commit;
  1280. }
  1281. if (data_ac)
  1282. ocfs2_free_alloc_context(data_ac);
  1283. if (meta_ac)
  1284. ocfs2_free_alloc_context(meta_ac);
  1285. *pagep = wc->w_target_page;
  1286. *fsdata = wc;
  1287. return 0;
  1288. out_commit:
  1289. ocfs2_commit_trans(osb, handle);
  1290. out:
  1291. ocfs2_free_write_ctxt(wc);
  1292. if (data_ac)
  1293. ocfs2_free_alloc_context(data_ac);
  1294. if (meta_ac)
  1295. ocfs2_free_alloc_context(meta_ac);
  1296. return ret;
  1297. }
  1298. int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1299. loff_t pos, unsigned len, unsigned flags,
  1300. struct page **pagep, void **fsdata)
  1301. {
  1302. int ret;
  1303. struct buffer_head *di_bh = NULL;
  1304. struct inode *inode = mapping->host;
  1305. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1306. if (ret) {
  1307. mlog_errno(ret);
  1308. return ret;
  1309. }
  1310. /*
  1311. * Take alloc sem here to prevent concurrent lookups. That way
  1312. * the mapping, zeroing and tree manipulation within
  1313. * ocfs2_write() will be safe against ->readpage(). This
  1314. * should also serve to lock out allocation from a shared
  1315. * writeable region.
  1316. */
  1317. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1318. ret = ocfs2_data_lock(inode, 1);
  1319. if (ret) {
  1320. mlog_errno(ret);
  1321. goto out_fail;
  1322. }
  1323. ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
  1324. fsdata, di_bh, NULL);
  1325. if (ret) {
  1326. mlog_errno(ret);
  1327. goto out_fail_data;
  1328. }
  1329. brelse(di_bh);
  1330. return 0;
  1331. out_fail_data:
  1332. ocfs2_data_unlock(inode, 1);
  1333. out_fail:
  1334. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1335. brelse(di_bh);
  1336. ocfs2_meta_unlock(inode, 1);
  1337. return ret;
  1338. }
  1339. int ocfs2_write_end_nolock(struct address_space *mapping,
  1340. loff_t pos, unsigned len, unsigned copied,
  1341. struct page *page, void *fsdata)
  1342. {
  1343. int i;
  1344. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1345. struct inode *inode = mapping->host;
  1346. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1347. struct ocfs2_write_ctxt *wc = fsdata;
  1348. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1349. handle_t *handle = wc->w_handle;
  1350. struct page *tmppage;
  1351. if (unlikely(copied < len)) {
  1352. if (!PageUptodate(wc->w_target_page))
  1353. copied = 0;
  1354. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1355. start+len);
  1356. }
  1357. flush_dcache_page(wc->w_target_page);
  1358. for(i = 0; i < wc->w_num_pages; i++) {
  1359. tmppage = wc->w_pages[i];
  1360. if (tmppage == wc->w_target_page) {
  1361. from = wc->w_target_from;
  1362. to = wc->w_target_to;
  1363. BUG_ON(from > PAGE_CACHE_SIZE ||
  1364. to > PAGE_CACHE_SIZE ||
  1365. to < from);
  1366. } else {
  1367. /*
  1368. * Pages adjacent to the target (if any) imply
  1369. * a hole-filling write in which case we want
  1370. * to flush their entire range.
  1371. */
  1372. from = 0;
  1373. to = PAGE_CACHE_SIZE;
  1374. }
  1375. if (ocfs2_should_order_data(inode))
  1376. walk_page_buffers(wc->w_handle, page_buffers(tmppage),
  1377. from, to, NULL,
  1378. ocfs2_journal_dirty_data);
  1379. block_commit_write(tmppage, from, to);
  1380. }
  1381. pos += copied;
  1382. if (pos > inode->i_size) {
  1383. i_size_write(inode, pos);
  1384. mark_inode_dirty(inode);
  1385. }
  1386. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1387. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1388. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1389. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1390. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1391. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1392. ocfs2_commit_trans(osb, handle);
  1393. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1394. ocfs2_free_write_ctxt(wc);
  1395. return copied;
  1396. }
  1397. int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1398. loff_t pos, unsigned len, unsigned copied,
  1399. struct page *page, void *fsdata)
  1400. {
  1401. int ret;
  1402. struct inode *inode = mapping->host;
  1403. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1404. ocfs2_data_unlock(inode, 1);
  1405. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1406. ocfs2_meta_unlock(inode, 1);
  1407. return ret;
  1408. }
  1409. const struct address_space_operations ocfs2_aops = {
  1410. .readpage = ocfs2_readpage,
  1411. .writepage = ocfs2_writepage,
  1412. .bmap = ocfs2_bmap,
  1413. .sync_page = block_sync_page,
  1414. .direct_IO = ocfs2_direct_IO,
  1415. .invalidatepage = ocfs2_invalidatepage,
  1416. .releasepage = ocfs2_releasepage,
  1417. .migratepage = buffer_migrate_page,
  1418. };