aops.c 62 KB

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