aops.c 58 KB

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