aops.c 52 KB

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