aops.c 51 KB

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