aops.c 52 KB

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