aops.c 47 KB

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