aops.c 61 KB

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