aops.c 50 KB

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