aops.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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_CACHE_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_CACHE_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_CACHE_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_CACHE_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. /*
  432. * TODO: Make this into a generic get_blocks function.
  433. *
  434. * From do_direct_io in direct-io.c:
  435. * "So what we do is to permit the ->get_blocks function to populate
  436. * bh.b_size with the size of IO which is permitted at this offset and
  437. * this i_blkbits."
  438. *
  439. * This function is called directly from get_more_blocks in direct-io.c.
  440. *
  441. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  442. * fs_count, map_bh, dio->rw == WRITE);
  443. */
  444. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  445. struct buffer_head *bh_result, int create)
  446. {
  447. int ret;
  448. u32 cpos = 0;
  449. int alloc_locked = 0;
  450. u64 p_blkno, inode_blocks, contig_blocks;
  451. unsigned int ext_flags;
  452. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  453. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  454. unsigned long len = bh_result->b_size;
  455. unsigned int clusters_to_alloc = 0, contig_clusters = 0;
  456. cpos = ocfs2_blocks_to_clusters(inode->i_sb, iblock);
  457. /* This function won't even be called if the request isn't all
  458. * nicely aligned and of the right size, so there's no need
  459. * for us to check any of that. */
  460. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  461. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  462. /* This figures out the size of the next contiguous block, and
  463. * our logical offset */
  464. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  465. &contig_blocks, &ext_flags);
  466. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  467. if (ret) {
  468. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  469. (unsigned long long)iblock);
  470. ret = -EIO;
  471. goto bail;
  472. }
  473. /* We should already CoW the refcounted extent in case of create. */
  474. BUG_ON(create && (ext_flags & OCFS2_EXT_REFCOUNTED));
  475. /* allocate blocks if no p_blkno is found, and create == 1 */
  476. if (!p_blkno && create) {
  477. ret = ocfs2_inode_lock(inode, NULL, 1);
  478. if (ret < 0) {
  479. mlog_errno(ret);
  480. goto bail;
  481. }
  482. alloc_locked = 1;
  483. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  484. /* fill hole, allocate blocks can't be larger than the size
  485. * of the hole */
  486. clusters_to_alloc = ocfs2_clusters_for_bytes(inode->i_sb, len);
  487. contig_clusters = ocfs2_clusters_for_blocks(inode->i_sb,
  488. contig_blocks);
  489. if (clusters_to_alloc > contig_clusters)
  490. clusters_to_alloc = contig_clusters;
  491. /* allocate extent and insert them into the extent tree */
  492. ret = ocfs2_extend_allocation(inode, cpos,
  493. clusters_to_alloc, 0);
  494. if (ret < 0) {
  495. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  496. mlog_errno(ret);
  497. goto bail;
  498. }
  499. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  500. &contig_blocks, &ext_flags);
  501. if (ret < 0) {
  502. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  503. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  504. (unsigned long long)iblock);
  505. ret = -EIO;
  506. goto bail;
  507. }
  508. set_buffer_new(bh_result);
  509. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  510. }
  511. /*
  512. * get_more_blocks() expects us to describe a hole by clearing
  513. * the mapped bit on bh_result().
  514. *
  515. * Consider an unwritten extent as a hole.
  516. */
  517. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  518. map_bh(bh_result, inode->i_sb, p_blkno);
  519. else
  520. clear_buffer_mapped(bh_result);
  521. /* make sure we don't map more than max_blocks blocks here as
  522. that's all the kernel will handle at this point. */
  523. if (max_blocks < contig_blocks)
  524. contig_blocks = max_blocks;
  525. bh_result->b_size = contig_blocks << blocksize_bits;
  526. bail:
  527. if (alloc_locked)
  528. ocfs2_inode_unlock(inode, 1);
  529. return ret;
  530. }
  531. /*
  532. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  533. * particularly interested in the aio/dio case. We use the rw_lock DLM lock
  534. * to protect io on one node from truncation on another.
  535. */
  536. static void ocfs2_dio_end_io(struct kiocb *iocb,
  537. loff_t offset,
  538. ssize_t bytes,
  539. void *private)
  540. {
  541. struct inode *inode = file_inode(iocb->ki_filp);
  542. int level;
  543. /* this io's submitter should not have unlocked this before we could */
  544. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  545. if (ocfs2_iocb_is_unaligned_aio(iocb)) {
  546. ocfs2_iocb_clear_unaligned_aio(iocb);
  547. mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio);
  548. }
  549. /* Let rw unlock to be done later to protect append direct io write */
  550. if (offset + bytes <= i_size_read(inode)) {
  551. ocfs2_iocb_clear_rw_locked(iocb);
  552. level = ocfs2_iocb_rw_locked_level(iocb);
  553. ocfs2_rw_unlock(inode, level);
  554. }
  555. }
  556. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  557. {
  558. if (!page_has_buffers(page))
  559. return 0;
  560. return try_to_free_buffers(page);
  561. }
  562. static int ocfs2_is_overwrite(struct ocfs2_super *osb,
  563. struct inode *inode, loff_t offset)
  564. {
  565. int ret = 0;
  566. u32 v_cpos = 0;
  567. u32 p_cpos = 0;
  568. unsigned int num_clusters = 0;
  569. unsigned int ext_flags = 0;
  570. v_cpos = ocfs2_bytes_to_clusters(osb->sb, offset);
  571. ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos,
  572. &num_clusters, &ext_flags);
  573. if (ret < 0) {
  574. mlog_errno(ret);
  575. return ret;
  576. }
  577. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  578. return 1;
  579. return 0;
  580. }
  581. static int ocfs2_direct_IO_zero_extend(struct ocfs2_super *osb,
  582. struct inode *inode, loff_t offset,
  583. u64 zero_len, int cluster_align)
  584. {
  585. u32 p_cpos = 0;
  586. u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, i_size_read(inode));
  587. unsigned int num_clusters = 0;
  588. unsigned int ext_flags = 0;
  589. int ret = 0;
  590. if (offset <= i_size_read(inode) || cluster_align)
  591. return 0;
  592. ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos, &num_clusters,
  593. &ext_flags);
  594. if (ret < 0) {
  595. mlog_errno(ret);
  596. return ret;
  597. }
  598. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  599. u64 s = i_size_read(inode);
  600. sector_t sector = ((u64)p_cpos << (osb->s_clustersize_bits - 9)) +
  601. (do_div(s, osb->s_clustersize) >> 9);
  602. ret = blkdev_issue_zeroout(osb->sb->s_bdev, sector,
  603. zero_len >> 9, GFP_NOFS, false);
  604. if (ret < 0)
  605. mlog_errno(ret);
  606. }
  607. return ret;
  608. }
  609. static int ocfs2_direct_IO_extend_no_holes(struct ocfs2_super *osb,
  610. struct inode *inode, loff_t offset)
  611. {
  612. u64 zero_start, zero_len, total_zero_len;
  613. u32 p_cpos = 0, clusters_to_add;
  614. u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, i_size_read(inode));
  615. unsigned int num_clusters = 0;
  616. unsigned int ext_flags = 0;
  617. u32 size_div, offset_div;
  618. int ret = 0;
  619. {
  620. u64 o = offset;
  621. u64 s = i_size_read(inode);
  622. offset_div = do_div(o, osb->s_clustersize);
  623. size_div = do_div(s, osb->s_clustersize);
  624. }
  625. if (offset <= i_size_read(inode))
  626. return 0;
  627. clusters_to_add = ocfs2_bytes_to_clusters(inode->i_sb, offset) -
  628. ocfs2_bytes_to_clusters(inode->i_sb, i_size_read(inode));
  629. total_zero_len = offset - i_size_read(inode);
  630. if (clusters_to_add)
  631. total_zero_len -= offset_div;
  632. /* Allocate clusters to fill out holes, and this is only needed
  633. * when we add more than one clusters. Otherwise the cluster will
  634. * be allocated during direct IO */
  635. if (clusters_to_add > 1) {
  636. ret = ocfs2_extend_allocation(inode,
  637. OCFS2_I(inode)->ip_clusters,
  638. clusters_to_add - 1, 0);
  639. if (ret) {
  640. mlog_errno(ret);
  641. goto out;
  642. }
  643. }
  644. while (total_zero_len) {
  645. ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos, &num_clusters,
  646. &ext_flags);
  647. if (ret < 0) {
  648. mlog_errno(ret);
  649. goto out;
  650. }
  651. zero_start = ocfs2_clusters_to_bytes(osb->sb, p_cpos) +
  652. size_div;
  653. zero_len = ocfs2_clusters_to_bytes(osb->sb, num_clusters) -
  654. size_div;
  655. zero_len = min(total_zero_len, zero_len);
  656. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  657. ret = blkdev_issue_zeroout(osb->sb->s_bdev,
  658. zero_start >> 9, zero_len >> 9,
  659. GFP_NOFS, false);
  660. if (ret < 0) {
  661. mlog_errno(ret);
  662. goto out;
  663. }
  664. }
  665. total_zero_len -= zero_len;
  666. v_cpos += ocfs2_bytes_to_clusters(osb->sb, zero_len + size_div);
  667. /* Only at first iteration can be cluster not aligned.
  668. * So set size_div to 0 for the rest */
  669. size_div = 0;
  670. }
  671. out:
  672. return ret;
  673. }
  674. static ssize_t ocfs2_direct_IO_write(struct kiocb *iocb,
  675. struct iov_iter *iter,
  676. loff_t offset)
  677. {
  678. ssize_t ret = 0;
  679. ssize_t written = 0;
  680. bool orphaned = false;
  681. int is_overwrite = 0;
  682. struct file *file = iocb->ki_filp;
  683. struct inode *inode = file_inode(file)->i_mapping->host;
  684. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  685. struct buffer_head *di_bh = NULL;
  686. size_t count = iter->count;
  687. journal_t *journal = osb->journal->j_journal;
  688. u64 zero_len_head, zero_len_tail;
  689. int cluster_align_head, cluster_align_tail;
  690. loff_t final_size = offset + count;
  691. int append_write = offset >= i_size_read(inode) ? 1 : 0;
  692. unsigned int num_clusters = 0;
  693. unsigned int ext_flags = 0;
  694. {
  695. u64 o = offset;
  696. u64 s = i_size_read(inode);
  697. zero_len_head = do_div(o, 1 << osb->s_clustersize_bits);
  698. cluster_align_head = !zero_len_head;
  699. zero_len_tail = osb->s_clustersize -
  700. do_div(s, osb->s_clustersize);
  701. if ((offset - i_size_read(inode)) < zero_len_tail)
  702. zero_len_tail = offset - i_size_read(inode);
  703. cluster_align_tail = !zero_len_tail;
  704. }
  705. /*
  706. * when final_size > inode->i_size, inode->i_size will be
  707. * updated after direct write, so add the inode to orphan
  708. * dir first.
  709. */
  710. if (final_size > i_size_read(inode)) {
  711. ret = ocfs2_add_inode_to_orphan(osb, inode);
  712. if (ret < 0) {
  713. mlog_errno(ret);
  714. goto out;
  715. }
  716. orphaned = true;
  717. }
  718. if (append_write) {
  719. ret = ocfs2_inode_lock(inode, NULL, 1);
  720. if (ret < 0) {
  721. mlog_errno(ret);
  722. goto clean_orphan;
  723. }
  724. /* zeroing out the previously allocated cluster tail
  725. * that but not zeroed */
  726. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  727. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  728. ret = ocfs2_direct_IO_zero_extend(osb, inode, offset,
  729. zero_len_tail, cluster_align_tail);
  730. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  731. } else {
  732. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  733. ret = ocfs2_direct_IO_extend_no_holes(osb, inode,
  734. offset);
  735. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  736. }
  737. if (ret < 0) {
  738. mlog_errno(ret);
  739. ocfs2_inode_unlock(inode, 1);
  740. goto clean_orphan;
  741. }
  742. is_overwrite = ocfs2_is_overwrite(osb, inode, offset);
  743. if (is_overwrite < 0) {
  744. mlog_errno(is_overwrite);
  745. ret = is_overwrite;
  746. ocfs2_inode_unlock(inode, 1);
  747. goto clean_orphan;
  748. }
  749. ocfs2_inode_unlock(inode, 1);
  750. }
  751. written = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
  752. offset, ocfs2_direct_IO_get_blocks,
  753. ocfs2_dio_end_io, NULL, 0);
  754. /* overwrite aio may return -EIOCBQUEUED, and it is not an error */
  755. if ((written < 0) && (written != -EIOCBQUEUED)) {
  756. loff_t i_size = i_size_read(inode);
  757. if (offset + count > i_size) {
  758. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  759. if (ret < 0) {
  760. mlog_errno(ret);
  761. goto clean_orphan;
  762. }
  763. if (i_size == i_size_read(inode)) {
  764. ret = ocfs2_truncate_file(inode, di_bh,
  765. i_size);
  766. if (ret < 0) {
  767. if (ret != -ENOSPC)
  768. mlog_errno(ret);
  769. ocfs2_inode_unlock(inode, 1);
  770. brelse(di_bh);
  771. di_bh = NULL;
  772. goto clean_orphan;
  773. }
  774. }
  775. ocfs2_inode_unlock(inode, 1);
  776. brelse(di_bh);
  777. di_bh = NULL;
  778. ret = jbd2_journal_force_commit(journal);
  779. if (ret < 0)
  780. mlog_errno(ret);
  781. }
  782. } else if (written > 0 && append_write && !is_overwrite &&
  783. !cluster_align_head) {
  784. /* zeroing out the allocated cluster head */
  785. u32 p_cpos = 0;
  786. u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, offset);
  787. ret = ocfs2_inode_lock(inode, NULL, 0);
  788. if (ret < 0) {
  789. mlog_errno(ret);
  790. goto clean_orphan;
  791. }
  792. ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos,
  793. &num_clusters, &ext_flags);
  794. if (ret < 0) {
  795. mlog_errno(ret);
  796. ocfs2_inode_unlock(inode, 0);
  797. goto clean_orphan;
  798. }
  799. BUG_ON(!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN));
  800. ret = blkdev_issue_zeroout(osb->sb->s_bdev,
  801. (u64)p_cpos << (osb->s_clustersize_bits - 9),
  802. zero_len_head >> 9, GFP_NOFS, false);
  803. if (ret < 0)
  804. mlog_errno(ret);
  805. ocfs2_inode_unlock(inode, 0);
  806. }
  807. clean_orphan:
  808. if (orphaned) {
  809. int tmp_ret;
  810. int update_isize = written > 0 ? 1 : 0;
  811. loff_t end = update_isize ? offset + written : 0;
  812. tmp_ret = ocfs2_inode_lock(inode, &di_bh, 1);
  813. if (tmp_ret < 0) {
  814. ret = tmp_ret;
  815. mlog_errno(ret);
  816. goto out;
  817. }
  818. tmp_ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh,
  819. update_isize, end);
  820. if (tmp_ret < 0) {
  821. ocfs2_inode_unlock(inode, 1);
  822. ret = tmp_ret;
  823. mlog_errno(ret);
  824. brelse(di_bh);
  825. goto out;
  826. }
  827. ocfs2_inode_unlock(inode, 1);
  828. brelse(di_bh);
  829. tmp_ret = jbd2_journal_force_commit(journal);
  830. if (tmp_ret < 0) {
  831. ret = tmp_ret;
  832. mlog_errno(tmp_ret);
  833. }
  834. }
  835. out:
  836. if (ret >= 0)
  837. ret = written;
  838. return ret;
  839. }
  840. static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  841. loff_t offset)
  842. {
  843. struct file *file = iocb->ki_filp;
  844. struct inode *inode = file_inode(file)->i_mapping->host;
  845. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  846. int full_coherency = !(osb->s_mount_opt &
  847. OCFS2_MOUNT_COHERENCY_BUFFERED);
  848. /*
  849. * Fallback to buffered I/O if we see an inode without
  850. * extents.
  851. */
  852. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  853. return 0;
  854. /* Fallback to buffered I/O if we are appending and
  855. * concurrent O_DIRECT writes are allowed.
  856. */
  857. if (i_size_read(inode) <= offset && !full_coherency)
  858. return 0;
  859. if (iov_iter_rw(iter) == READ)
  860. return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
  861. iter, offset,
  862. ocfs2_direct_IO_get_blocks,
  863. ocfs2_dio_end_io, NULL, 0);
  864. else
  865. return ocfs2_direct_IO_write(iocb, iter, offset);
  866. }
  867. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  868. u32 cpos,
  869. unsigned int *start,
  870. unsigned int *end)
  871. {
  872. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  873. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  874. unsigned int cpp;
  875. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  876. cluster_start = cpos % cpp;
  877. cluster_start = cluster_start << osb->s_clustersize_bits;
  878. cluster_end = cluster_start + osb->s_clustersize;
  879. }
  880. BUG_ON(cluster_start > PAGE_SIZE);
  881. BUG_ON(cluster_end > PAGE_SIZE);
  882. if (start)
  883. *start = cluster_start;
  884. if (end)
  885. *end = cluster_end;
  886. }
  887. /*
  888. * 'from' and 'to' are the region in the page to avoid zeroing.
  889. *
  890. * If pagesize > clustersize, this function will avoid zeroing outside
  891. * of the cluster boundary.
  892. *
  893. * from == to == 0 is code for "zero the entire cluster region"
  894. */
  895. static void ocfs2_clear_page_regions(struct page *page,
  896. struct ocfs2_super *osb, u32 cpos,
  897. unsigned from, unsigned to)
  898. {
  899. void *kaddr;
  900. unsigned int cluster_start, cluster_end;
  901. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  902. kaddr = kmap_atomic(page);
  903. if (from || to) {
  904. if (from > cluster_start)
  905. memset(kaddr + cluster_start, 0, from - cluster_start);
  906. if (to < cluster_end)
  907. memset(kaddr + to, 0, cluster_end - to);
  908. } else {
  909. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  910. }
  911. kunmap_atomic(kaddr);
  912. }
  913. /*
  914. * Nonsparse file systems fully allocate before we get to the write
  915. * code. This prevents ocfs2_write() from tagging the write as an
  916. * allocating one, which means ocfs2_map_page_blocks() might try to
  917. * read-in the blocks at the tail of our file. Avoid reading them by
  918. * testing i_size against each block offset.
  919. */
  920. static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
  921. unsigned int block_start)
  922. {
  923. u64 offset = page_offset(page) + block_start;
  924. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  925. return 1;
  926. if (i_size_read(inode) > offset)
  927. return 1;
  928. return 0;
  929. }
  930. /*
  931. * Some of this taken from __block_write_begin(). We already have our
  932. * mapping by now though, and the entire write will be allocating or
  933. * it won't, so not much need to use BH_New.
  934. *
  935. * This will also skip zeroing, which is handled externally.
  936. */
  937. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  938. struct inode *inode, unsigned int from,
  939. unsigned int to, int new)
  940. {
  941. int ret = 0;
  942. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  943. unsigned int block_end, block_start;
  944. unsigned int bsize = i_blocksize(inode);
  945. if (!page_has_buffers(page))
  946. create_empty_buffers(page, bsize, 0);
  947. head = page_buffers(page);
  948. for (bh = head, block_start = 0; bh != head || !block_start;
  949. bh = bh->b_this_page, block_start += bsize) {
  950. block_end = block_start + bsize;
  951. clear_buffer_new(bh);
  952. /*
  953. * Ignore blocks outside of our i/o range -
  954. * they may belong to unallocated clusters.
  955. */
  956. if (block_start >= to || block_end <= from) {
  957. if (PageUptodate(page))
  958. set_buffer_uptodate(bh);
  959. continue;
  960. }
  961. /*
  962. * For an allocating write with cluster size >= page
  963. * size, we always write the entire page.
  964. */
  965. if (new)
  966. set_buffer_new(bh);
  967. if (!buffer_mapped(bh)) {
  968. map_bh(bh, inode->i_sb, *p_blkno);
  969. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  970. }
  971. if (PageUptodate(page)) {
  972. if (!buffer_uptodate(bh))
  973. set_buffer_uptodate(bh);
  974. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  975. !buffer_new(bh) &&
  976. ocfs2_should_read_blk(inode, page, block_start) &&
  977. (block_start < from || block_end > to)) {
  978. ll_rw_block(READ, 1, &bh);
  979. *wait_bh++=bh;
  980. }
  981. *p_blkno = *p_blkno + 1;
  982. }
  983. /*
  984. * If we issued read requests - let them complete.
  985. */
  986. while(wait_bh > wait) {
  987. wait_on_buffer(*--wait_bh);
  988. if (!buffer_uptodate(*wait_bh))
  989. ret = -EIO;
  990. }
  991. if (ret == 0 || !new)
  992. return ret;
  993. /*
  994. * If we get -EIO above, zero out any newly allocated blocks
  995. * to avoid exposing stale data.
  996. */
  997. bh = head;
  998. block_start = 0;
  999. do {
  1000. block_end = block_start + bsize;
  1001. if (block_end <= from)
  1002. goto next_bh;
  1003. if (block_start >= to)
  1004. break;
  1005. zero_user(page, block_start, bh->b_size);
  1006. set_buffer_uptodate(bh);
  1007. mark_buffer_dirty(bh);
  1008. next_bh:
  1009. block_start = block_end;
  1010. bh = bh->b_this_page;
  1011. } while (bh != head);
  1012. return ret;
  1013. }
  1014. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  1015. #define OCFS2_MAX_CTXT_PAGES 1
  1016. #else
  1017. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  1018. #endif
  1019. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  1020. /*
  1021. * Describe the state of a single cluster to be written to.
  1022. */
  1023. struct ocfs2_write_cluster_desc {
  1024. u32 c_cpos;
  1025. u32 c_phys;
  1026. /*
  1027. * Give this a unique field because c_phys eventually gets
  1028. * filled.
  1029. */
  1030. unsigned c_new;
  1031. unsigned c_unwritten;
  1032. unsigned c_needs_zero;
  1033. };
  1034. struct ocfs2_write_ctxt {
  1035. /* Logical cluster position / len of write */
  1036. u32 w_cpos;
  1037. u32 w_clen;
  1038. /* First cluster allocated in a nonsparse extend */
  1039. u32 w_first_new_cpos;
  1040. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  1041. /*
  1042. * This is true if page_size > cluster_size.
  1043. *
  1044. * It triggers a set of special cases during write which might
  1045. * have to deal with allocating writes to partial pages.
  1046. */
  1047. unsigned int w_large_pages;
  1048. /*
  1049. * Pages involved in this write.
  1050. *
  1051. * w_target_page is the page being written to by the user.
  1052. *
  1053. * w_pages is an array of pages which always contains
  1054. * w_target_page, and in the case of an allocating write with
  1055. * page_size < cluster size, it will contain zero'd and mapped
  1056. * pages adjacent to w_target_page which need to be written
  1057. * out in so that future reads from that region will get
  1058. * zero's.
  1059. */
  1060. unsigned int w_num_pages;
  1061. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  1062. struct page *w_target_page;
  1063. /*
  1064. * w_target_locked is used for page_mkwrite path indicating no unlocking
  1065. * against w_target_page in ocfs2_write_end_nolock.
  1066. */
  1067. unsigned int w_target_locked:1;
  1068. /*
  1069. * ocfs2_write_end() uses this to know what the real range to
  1070. * write in the target should be.
  1071. */
  1072. unsigned int w_target_from;
  1073. unsigned int w_target_to;
  1074. /*
  1075. * We could use journal_current_handle() but this is cleaner,
  1076. * IMHO -Mark
  1077. */
  1078. handle_t *w_handle;
  1079. struct buffer_head *w_di_bh;
  1080. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  1081. };
  1082. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  1083. {
  1084. int i;
  1085. for(i = 0; i < num_pages; i++) {
  1086. if (pages[i]) {
  1087. unlock_page(pages[i]);
  1088. mark_page_accessed(pages[i]);
  1089. page_cache_release(pages[i]);
  1090. }
  1091. }
  1092. }
  1093. static void ocfs2_unlock_pages(struct ocfs2_write_ctxt *wc)
  1094. {
  1095. int i;
  1096. /*
  1097. * w_target_locked is only set to true in the page_mkwrite() case.
  1098. * The intent is to allow us to lock the target page from write_begin()
  1099. * to write_end(). The caller must hold a ref on w_target_page.
  1100. */
  1101. if (wc->w_target_locked) {
  1102. BUG_ON(!wc->w_target_page);
  1103. for (i = 0; i < wc->w_num_pages; i++) {
  1104. if (wc->w_target_page == wc->w_pages[i]) {
  1105. wc->w_pages[i] = NULL;
  1106. break;
  1107. }
  1108. }
  1109. mark_page_accessed(wc->w_target_page);
  1110. page_cache_release(wc->w_target_page);
  1111. }
  1112. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  1113. }
  1114. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  1115. {
  1116. ocfs2_unlock_pages(wc);
  1117. brelse(wc->w_di_bh);
  1118. kfree(wc);
  1119. }
  1120. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  1121. struct ocfs2_super *osb, loff_t pos,
  1122. unsigned len, struct buffer_head *di_bh)
  1123. {
  1124. u32 cend;
  1125. struct ocfs2_write_ctxt *wc;
  1126. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  1127. if (!wc)
  1128. return -ENOMEM;
  1129. wc->w_cpos = pos >> osb->s_clustersize_bits;
  1130. wc->w_first_new_cpos = UINT_MAX;
  1131. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  1132. wc->w_clen = cend - wc->w_cpos + 1;
  1133. get_bh(di_bh);
  1134. wc->w_di_bh = di_bh;
  1135. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  1136. wc->w_large_pages = 1;
  1137. else
  1138. wc->w_large_pages = 0;
  1139. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  1140. *wcp = wc;
  1141. return 0;
  1142. }
  1143. /*
  1144. * If a page has any new buffers, zero them out here, and mark them uptodate
  1145. * and dirty so they'll be written out (in order to prevent uninitialised
  1146. * block data from leaking). And clear the new bit.
  1147. */
  1148. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1149. {
  1150. unsigned int block_start, block_end;
  1151. struct buffer_head *head, *bh;
  1152. BUG_ON(!PageLocked(page));
  1153. if (!page_has_buffers(page))
  1154. return;
  1155. bh = head = page_buffers(page);
  1156. block_start = 0;
  1157. do {
  1158. block_end = block_start + bh->b_size;
  1159. if (buffer_new(bh)) {
  1160. if (block_end > from && block_start < to) {
  1161. if (!PageUptodate(page)) {
  1162. unsigned start, end;
  1163. start = max(from, block_start);
  1164. end = min(to, block_end);
  1165. zero_user_segment(page, start, end);
  1166. set_buffer_uptodate(bh);
  1167. }
  1168. clear_buffer_new(bh);
  1169. mark_buffer_dirty(bh);
  1170. }
  1171. }
  1172. block_start = block_end;
  1173. bh = bh->b_this_page;
  1174. } while (bh != head);
  1175. }
  1176. /*
  1177. * Only called when we have a failure during allocating write to write
  1178. * zero's to the newly allocated region.
  1179. */
  1180. static void ocfs2_write_failure(struct inode *inode,
  1181. struct ocfs2_write_ctxt *wc,
  1182. loff_t user_pos, unsigned user_len)
  1183. {
  1184. int i;
  1185. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  1186. to = user_pos + user_len;
  1187. struct page *tmppage;
  1188. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  1189. for(i = 0; i < wc->w_num_pages; i++) {
  1190. tmppage = wc->w_pages[i];
  1191. if (page_has_buffers(tmppage)) {
  1192. if (ocfs2_should_order_data(inode))
  1193. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  1194. block_commit_write(tmppage, from, to);
  1195. }
  1196. }
  1197. }
  1198. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  1199. struct ocfs2_write_ctxt *wc,
  1200. struct page *page, u32 cpos,
  1201. loff_t user_pos, unsigned user_len,
  1202. int new)
  1203. {
  1204. int ret;
  1205. unsigned int map_from = 0, map_to = 0;
  1206. unsigned int cluster_start, cluster_end;
  1207. unsigned int user_data_from = 0, user_data_to = 0;
  1208. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  1209. &cluster_start, &cluster_end);
  1210. /* treat the write as new if the a hole/lseek spanned across
  1211. * the page boundary.
  1212. */
  1213. new = new | ((i_size_read(inode) <= page_offset(page)) &&
  1214. (page_offset(page) <= user_pos));
  1215. if (page == wc->w_target_page) {
  1216. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  1217. map_to = map_from + user_len;
  1218. if (new)
  1219. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  1220. cluster_start, cluster_end,
  1221. new);
  1222. else
  1223. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  1224. map_from, map_to, new);
  1225. if (ret) {
  1226. mlog_errno(ret);
  1227. goto out;
  1228. }
  1229. user_data_from = map_from;
  1230. user_data_to = map_to;
  1231. if (new) {
  1232. map_from = cluster_start;
  1233. map_to = cluster_end;
  1234. }
  1235. } else {
  1236. /*
  1237. * If we haven't allocated the new page yet, we
  1238. * shouldn't be writing it out without copying user
  1239. * data. This is likely a math error from the caller.
  1240. */
  1241. BUG_ON(!new);
  1242. map_from = cluster_start;
  1243. map_to = cluster_end;
  1244. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  1245. cluster_start, cluster_end, new);
  1246. if (ret) {
  1247. mlog_errno(ret);
  1248. goto out;
  1249. }
  1250. }
  1251. /*
  1252. * Parts of newly allocated pages need to be zero'd.
  1253. *
  1254. * Above, we have also rewritten 'to' and 'from' - as far as
  1255. * the rest of the function is concerned, the entire cluster
  1256. * range inside of a page needs to be written.
  1257. *
  1258. * We can skip this if the page is up to date - it's already
  1259. * been zero'd from being read in as a hole.
  1260. */
  1261. if (new && !PageUptodate(page))
  1262. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  1263. cpos, user_data_from, user_data_to);
  1264. flush_dcache_page(page);
  1265. out:
  1266. return ret;
  1267. }
  1268. /*
  1269. * This function will only grab one clusters worth of pages.
  1270. */
  1271. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  1272. struct ocfs2_write_ctxt *wc,
  1273. u32 cpos, loff_t user_pos,
  1274. unsigned user_len, int new,
  1275. struct page *mmap_page)
  1276. {
  1277. int ret = 0, i;
  1278. unsigned long start, target_index, end_index, index;
  1279. struct inode *inode = mapping->host;
  1280. loff_t last_byte;
  1281. target_index = user_pos >> PAGE_CACHE_SHIFT;
  1282. /*
  1283. * Figure out how many pages we'll be manipulating here. For
  1284. * non allocating write, we just change the one
  1285. * page. Otherwise, we'll need a whole clusters worth. If we're
  1286. * writing past i_size, we only need enough pages to cover the
  1287. * last page of the write.
  1288. */
  1289. if (new) {
  1290. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  1291. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  1292. /*
  1293. * We need the index *past* the last page we could possibly
  1294. * touch. This is the page past the end of the write or
  1295. * i_size, whichever is greater.
  1296. */
  1297. last_byte = max(user_pos + user_len, i_size_read(inode));
  1298. BUG_ON(last_byte < 1);
  1299. end_index = ((last_byte - 1) >> PAGE_CACHE_SHIFT) + 1;
  1300. if ((start + wc->w_num_pages) > end_index)
  1301. wc->w_num_pages = end_index - start;
  1302. } else {
  1303. wc->w_num_pages = 1;
  1304. start = target_index;
  1305. }
  1306. for(i = 0; i < wc->w_num_pages; i++) {
  1307. index = start + i;
  1308. if (index == target_index && mmap_page) {
  1309. /*
  1310. * ocfs2_pagemkwrite() is a little different
  1311. * and wants us to directly use the page
  1312. * passed in.
  1313. */
  1314. lock_page(mmap_page);
  1315. /* Exit and let the caller retry */
  1316. if (mmap_page->mapping != mapping) {
  1317. WARN_ON(mmap_page->mapping);
  1318. unlock_page(mmap_page);
  1319. ret = -EAGAIN;
  1320. goto out;
  1321. }
  1322. page_cache_get(mmap_page);
  1323. wc->w_pages[i] = mmap_page;
  1324. wc->w_target_locked = true;
  1325. } else {
  1326. wc->w_pages[i] = find_or_create_page(mapping, index,
  1327. GFP_NOFS);
  1328. if (!wc->w_pages[i]) {
  1329. ret = -ENOMEM;
  1330. mlog_errno(ret);
  1331. goto out;
  1332. }
  1333. }
  1334. wait_for_stable_page(wc->w_pages[i]);
  1335. if (index == target_index)
  1336. wc->w_target_page = wc->w_pages[i];
  1337. }
  1338. out:
  1339. if (ret)
  1340. wc->w_target_locked = false;
  1341. return ret;
  1342. }
  1343. /*
  1344. * Prepare a single cluster for write one cluster into the file.
  1345. */
  1346. static int ocfs2_write_cluster(struct address_space *mapping,
  1347. u32 phys, unsigned int unwritten,
  1348. unsigned int should_zero,
  1349. struct ocfs2_alloc_context *data_ac,
  1350. struct ocfs2_alloc_context *meta_ac,
  1351. struct ocfs2_write_ctxt *wc, u32 cpos,
  1352. loff_t user_pos, unsigned user_len)
  1353. {
  1354. int ret, i, new;
  1355. u64 v_blkno, p_blkno;
  1356. struct inode *inode = mapping->host;
  1357. struct ocfs2_extent_tree et;
  1358. new = phys == 0 ? 1 : 0;
  1359. if (new) {
  1360. u32 tmp_pos;
  1361. /*
  1362. * This is safe to call with the page locks - it won't take
  1363. * any additional semaphores or cluster locks.
  1364. */
  1365. tmp_pos = cpos;
  1366. ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
  1367. &tmp_pos, 1, 0, wc->w_di_bh,
  1368. wc->w_handle, data_ac,
  1369. meta_ac, NULL);
  1370. /*
  1371. * This shouldn't happen because we must have already
  1372. * calculated the correct meta data allocation required. The
  1373. * internal tree allocation code should know how to increase
  1374. * transaction credits itself.
  1375. *
  1376. * If need be, we could handle -EAGAIN for a
  1377. * RESTART_TRANS here.
  1378. */
  1379. mlog_bug_on_msg(ret == -EAGAIN,
  1380. "Inode %llu: EAGAIN return during allocation.\n",
  1381. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1382. if (ret < 0) {
  1383. mlog_errno(ret);
  1384. goto out;
  1385. }
  1386. } else if (unwritten) {
  1387. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  1388. wc->w_di_bh);
  1389. ret = ocfs2_mark_extent_written(inode, &et,
  1390. wc->w_handle, cpos, 1, phys,
  1391. meta_ac, &wc->w_dealloc);
  1392. if (ret < 0) {
  1393. mlog_errno(ret);
  1394. goto out;
  1395. }
  1396. }
  1397. if (should_zero)
  1398. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  1399. else
  1400. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  1401. /*
  1402. * The only reason this should fail is due to an inability to
  1403. * find the extent added.
  1404. */
  1405. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  1406. NULL);
  1407. if (ret < 0) {
  1408. mlog(ML_ERROR, "Get physical blkno failed for inode %llu, "
  1409. "at logical block %llu",
  1410. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1411. (unsigned long long)v_blkno);
  1412. goto out;
  1413. }
  1414. BUG_ON(p_blkno == 0);
  1415. for(i = 0; i < wc->w_num_pages; i++) {
  1416. int tmpret;
  1417. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1418. wc->w_pages[i], cpos,
  1419. user_pos, user_len,
  1420. should_zero);
  1421. if (tmpret) {
  1422. mlog_errno(tmpret);
  1423. if (ret == 0)
  1424. ret = tmpret;
  1425. }
  1426. }
  1427. /*
  1428. * We only have cleanup to do in case of allocating write.
  1429. */
  1430. if (ret && new)
  1431. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1432. out:
  1433. return ret;
  1434. }
  1435. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1436. struct ocfs2_alloc_context *data_ac,
  1437. struct ocfs2_alloc_context *meta_ac,
  1438. struct ocfs2_write_ctxt *wc,
  1439. loff_t pos, unsigned len)
  1440. {
  1441. int ret, i;
  1442. loff_t cluster_off;
  1443. unsigned int local_len = len;
  1444. struct ocfs2_write_cluster_desc *desc;
  1445. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1446. for (i = 0; i < wc->w_clen; i++) {
  1447. desc = &wc->w_desc[i];
  1448. /*
  1449. * We have to make sure that the total write passed in
  1450. * doesn't extend past a single cluster.
  1451. */
  1452. local_len = len;
  1453. cluster_off = pos & (osb->s_clustersize - 1);
  1454. if ((cluster_off + local_len) > osb->s_clustersize)
  1455. local_len = osb->s_clustersize - cluster_off;
  1456. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1457. desc->c_unwritten,
  1458. desc->c_needs_zero,
  1459. data_ac, meta_ac,
  1460. wc, desc->c_cpos, pos, local_len);
  1461. if (ret) {
  1462. mlog_errno(ret);
  1463. goto out;
  1464. }
  1465. len -= local_len;
  1466. pos += local_len;
  1467. }
  1468. ret = 0;
  1469. out:
  1470. return ret;
  1471. }
  1472. /*
  1473. * ocfs2_write_end() wants to know which parts of the target page it
  1474. * should complete the write on. It's easiest to compute them ahead of
  1475. * time when a more complete view of the write is available.
  1476. */
  1477. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1478. struct ocfs2_write_ctxt *wc,
  1479. loff_t pos, unsigned len, int alloc)
  1480. {
  1481. struct ocfs2_write_cluster_desc *desc;
  1482. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1483. wc->w_target_to = wc->w_target_from + len;
  1484. if (alloc == 0)
  1485. return;
  1486. /*
  1487. * Allocating write - we may have different boundaries based
  1488. * on page size and cluster size.
  1489. *
  1490. * NOTE: We can no longer compute one value from the other as
  1491. * the actual write length and user provided length may be
  1492. * different.
  1493. */
  1494. if (wc->w_large_pages) {
  1495. /*
  1496. * We only care about the 1st and last cluster within
  1497. * our range and whether they should be zero'd or not. Either
  1498. * value may be extended out to the start/end of a
  1499. * newly allocated cluster.
  1500. */
  1501. desc = &wc->w_desc[0];
  1502. if (desc->c_needs_zero)
  1503. ocfs2_figure_cluster_boundaries(osb,
  1504. desc->c_cpos,
  1505. &wc->w_target_from,
  1506. NULL);
  1507. desc = &wc->w_desc[wc->w_clen - 1];
  1508. if (desc->c_needs_zero)
  1509. ocfs2_figure_cluster_boundaries(osb,
  1510. desc->c_cpos,
  1511. NULL,
  1512. &wc->w_target_to);
  1513. } else {
  1514. wc->w_target_from = 0;
  1515. wc->w_target_to = PAGE_CACHE_SIZE;
  1516. }
  1517. }
  1518. /*
  1519. * Populate each single-cluster write descriptor in the write context
  1520. * with information about the i/o to be done.
  1521. *
  1522. * Returns the number of clusters that will have to be allocated, as
  1523. * well as a worst case estimate of the number of extent records that
  1524. * would have to be created during a write to an unwritten region.
  1525. */
  1526. static int ocfs2_populate_write_desc(struct inode *inode,
  1527. struct ocfs2_write_ctxt *wc,
  1528. unsigned int *clusters_to_alloc,
  1529. unsigned int *extents_to_split)
  1530. {
  1531. int ret;
  1532. struct ocfs2_write_cluster_desc *desc;
  1533. unsigned int num_clusters = 0;
  1534. unsigned int ext_flags = 0;
  1535. u32 phys = 0;
  1536. int i;
  1537. *clusters_to_alloc = 0;
  1538. *extents_to_split = 0;
  1539. for (i = 0; i < wc->w_clen; i++) {
  1540. desc = &wc->w_desc[i];
  1541. desc->c_cpos = wc->w_cpos + i;
  1542. if (num_clusters == 0) {
  1543. /*
  1544. * Need to look up the next extent record.
  1545. */
  1546. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1547. &num_clusters, &ext_flags);
  1548. if (ret) {
  1549. mlog_errno(ret);
  1550. goto out;
  1551. }
  1552. /* We should already CoW the refcountd extent. */
  1553. BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
  1554. /*
  1555. * Assume worst case - that we're writing in
  1556. * the middle of the extent.
  1557. *
  1558. * We can assume that the write proceeds from
  1559. * left to right, in which case the extent
  1560. * insert code is smart enough to coalesce the
  1561. * next splits into the previous records created.
  1562. */
  1563. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1564. *extents_to_split = *extents_to_split + 2;
  1565. } else if (phys) {
  1566. /*
  1567. * Only increment phys if it doesn't describe
  1568. * a hole.
  1569. */
  1570. phys++;
  1571. }
  1572. /*
  1573. * If w_first_new_cpos is < UINT_MAX, we have a non-sparse
  1574. * file that got extended. w_first_new_cpos tells us
  1575. * where the newly allocated clusters are so we can
  1576. * zero them.
  1577. */
  1578. if (desc->c_cpos >= wc->w_first_new_cpos) {
  1579. BUG_ON(phys == 0);
  1580. desc->c_needs_zero = 1;
  1581. }
  1582. desc->c_phys = phys;
  1583. if (phys == 0) {
  1584. desc->c_new = 1;
  1585. desc->c_needs_zero = 1;
  1586. *clusters_to_alloc = *clusters_to_alloc + 1;
  1587. }
  1588. if (ext_flags & OCFS2_EXT_UNWRITTEN) {
  1589. desc->c_unwritten = 1;
  1590. desc->c_needs_zero = 1;
  1591. }
  1592. num_clusters--;
  1593. }
  1594. ret = 0;
  1595. out:
  1596. return ret;
  1597. }
  1598. static int ocfs2_write_begin_inline(struct address_space *mapping,
  1599. struct inode *inode,
  1600. struct ocfs2_write_ctxt *wc)
  1601. {
  1602. int ret;
  1603. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1604. struct page *page;
  1605. handle_t *handle;
  1606. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1607. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1608. if (IS_ERR(handle)) {
  1609. ret = PTR_ERR(handle);
  1610. mlog_errno(ret);
  1611. goto out;
  1612. }
  1613. page = find_or_create_page(mapping, 0, GFP_NOFS);
  1614. if (!page) {
  1615. ocfs2_commit_trans(osb, handle);
  1616. ret = -ENOMEM;
  1617. mlog_errno(ret);
  1618. goto out;
  1619. }
  1620. /*
  1621. * If we don't set w_num_pages then this page won't get unlocked
  1622. * and freed on cleanup of the write context.
  1623. */
  1624. wc->w_pages[0] = wc->w_target_page = page;
  1625. wc->w_num_pages = 1;
  1626. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1627. OCFS2_JOURNAL_ACCESS_WRITE);
  1628. if (ret) {
  1629. ocfs2_commit_trans(osb, handle);
  1630. mlog_errno(ret);
  1631. goto out;
  1632. }
  1633. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1634. ocfs2_set_inode_data_inline(inode, di);
  1635. if (!PageUptodate(page)) {
  1636. ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
  1637. if (ret) {
  1638. ocfs2_commit_trans(osb, handle);
  1639. goto out;
  1640. }
  1641. }
  1642. wc->w_handle = handle;
  1643. out:
  1644. return ret;
  1645. }
  1646. int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
  1647. {
  1648. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1649. if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
  1650. return 1;
  1651. return 0;
  1652. }
  1653. static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
  1654. struct inode *inode, loff_t pos,
  1655. unsigned len, struct page *mmap_page,
  1656. struct ocfs2_write_ctxt *wc)
  1657. {
  1658. int ret, written = 0;
  1659. loff_t end = pos + len;
  1660. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1661. struct ocfs2_dinode *di = NULL;
  1662. trace_ocfs2_try_to_write_inline_data((unsigned long long)oi->ip_blkno,
  1663. len, (unsigned long long)pos,
  1664. oi->ip_dyn_features);
  1665. /*
  1666. * Handle inodes which already have inline data 1st.
  1667. */
  1668. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1669. if (mmap_page == NULL &&
  1670. ocfs2_size_fits_inline_data(wc->w_di_bh, end))
  1671. goto do_inline_write;
  1672. /*
  1673. * The write won't fit - we have to give this inode an
  1674. * inline extent list now.
  1675. */
  1676. ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
  1677. if (ret)
  1678. mlog_errno(ret);
  1679. goto out;
  1680. }
  1681. /*
  1682. * Check whether the inode can accept inline data.
  1683. */
  1684. if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
  1685. return 0;
  1686. /*
  1687. * Check whether the write can fit.
  1688. */
  1689. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1690. if (mmap_page ||
  1691. end > ocfs2_max_inline_data_with_xattr(inode->i_sb, di))
  1692. return 0;
  1693. do_inline_write:
  1694. ret = ocfs2_write_begin_inline(mapping, inode, wc);
  1695. if (ret) {
  1696. mlog_errno(ret);
  1697. goto out;
  1698. }
  1699. /*
  1700. * This signals to the caller that the data can be written
  1701. * inline.
  1702. */
  1703. written = 1;
  1704. out:
  1705. return written ? written : ret;
  1706. }
  1707. /*
  1708. * This function only does anything for file systems which can't
  1709. * handle sparse files.
  1710. *
  1711. * What we want to do here is fill in any hole between the current end
  1712. * of allocation and the end of our write. That way the rest of the
  1713. * write path can treat it as an non-allocating write, which has no
  1714. * special case code for sparse/nonsparse files.
  1715. */
  1716. static int ocfs2_expand_nonsparse_inode(struct inode *inode,
  1717. struct buffer_head *di_bh,
  1718. loff_t pos, unsigned len,
  1719. struct ocfs2_write_ctxt *wc)
  1720. {
  1721. int ret;
  1722. loff_t newsize = pos + len;
  1723. BUG_ON(ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1724. if (newsize <= i_size_read(inode))
  1725. return 0;
  1726. ret = ocfs2_extend_no_holes(inode, di_bh, newsize, pos);
  1727. if (ret)
  1728. mlog_errno(ret);
  1729. wc->w_first_new_cpos =
  1730. ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
  1731. return ret;
  1732. }
  1733. static int ocfs2_zero_tail(struct inode *inode, struct buffer_head *di_bh,
  1734. loff_t pos)
  1735. {
  1736. int ret = 0;
  1737. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1738. if (pos > i_size_read(inode))
  1739. ret = ocfs2_zero_extend(inode, di_bh, pos);
  1740. return ret;
  1741. }
  1742. /*
  1743. * Try to flush truncate logs if we can free enough clusters from it.
  1744. * As for return value, "< 0" means error, "0" no space and "1" means
  1745. * we have freed enough spaces and let the caller try to allocate again.
  1746. */
  1747. static int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
  1748. unsigned int needed)
  1749. {
  1750. tid_t target;
  1751. int ret = 0;
  1752. unsigned int truncated_clusters;
  1753. mutex_lock(&osb->osb_tl_inode->i_mutex);
  1754. truncated_clusters = osb->truncated_clusters;
  1755. mutex_unlock(&osb->osb_tl_inode->i_mutex);
  1756. /*
  1757. * Check whether we can succeed in allocating if we free
  1758. * the truncate log.
  1759. */
  1760. if (truncated_clusters < needed)
  1761. goto out;
  1762. ret = ocfs2_flush_truncate_log(osb);
  1763. if (ret) {
  1764. mlog_errno(ret);
  1765. goto out;
  1766. }
  1767. if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
  1768. jbd2_log_wait_commit(osb->journal->j_journal, target);
  1769. ret = 1;
  1770. }
  1771. out:
  1772. return ret;
  1773. }
  1774. int ocfs2_write_begin_nolock(struct file *filp,
  1775. struct address_space *mapping,
  1776. loff_t pos, unsigned len, unsigned flags,
  1777. struct page **pagep, void **fsdata,
  1778. struct buffer_head *di_bh, struct page *mmap_page)
  1779. {
  1780. int ret, cluster_of_pages, credits = OCFS2_INODE_UPDATE_CREDITS;
  1781. unsigned int clusters_to_alloc, extents_to_split, clusters_need = 0;
  1782. struct ocfs2_write_ctxt *wc;
  1783. struct inode *inode = mapping->host;
  1784. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1785. struct ocfs2_dinode *di;
  1786. struct ocfs2_alloc_context *data_ac = NULL;
  1787. struct ocfs2_alloc_context *meta_ac = NULL;
  1788. handle_t *handle;
  1789. struct ocfs2_extent_tree et;
  1790. int try_free = 1, ret1;
  1791. try_again:
  1792. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1793. if (ret) {
  1794. mlog_errno(ret);
  1795. return ret;
  1796. }
  1797. if (ocfs2_supports_inline_data(osb)) {
  1798. ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
  1799. mmap_page, wc);
  1800. if (ret == 1) {
  1801. ret = 0;
  1802. goto success;
  1803. }
  1804. if (ret < 0) {
  1805. mlog_errno(ret);
  1806. goto out;
  1807. }
  1808. }
  1809. if (ocfs2_sparse_alloc(osb))
  1810. ret = ocfs2_zero_tail(inode, di_bh, pos);
  1811. else
  1812. ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos, len,
  1813. wc);
  1814. if (ret) {
  1815. mlog_errno(ret);
  1816. goto out;
  1817. }
  1818. ret = ocfs2_check_range_for_refcount(inode, pos, len);
  1819. if (ret < 0) {
  1820. mlog_errno(ret);
  1821. goto out;
  1822. } else if (ret == 1) {
  1823. clusters_need = wc->w_clen;
  1824. ret = ocfs2_refcount_cow(inode, di_bh,
  1825. wc->w_cpos, wc->w_clen, UINT_MAX);
  1826. if (ret) {
  1827. mlog_errno(ret);
  1828. goto out;
  1829. }
  1830. }
  1831. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1832. &extents_to_split);
  1833. if (ret) {
  1834. mlog_errno(ret);
  1835. goto out;
  1836. }
  1837. clusters_need += clusters_to_alloc;
  1838. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1839. trace_ocfs2_write_begin_nolock(
  1840. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1841. (long long)i_size_read(inode),
  1842. le32_to_cpu(di->i_clusters),
  1843. pos, len, flags, mmap_page,
  1844. clusters_to_alloc, extents_to_split);
  1845. /*
  1846. * We set w_target_from, w_target_to here so that
  1847. * ocfs2_write_end() knows which range in the target page to
  1848. * write out. An allocation requires that we write the entire
  1849. * cluster range.
  1850. */
  1851. if (clusters_to_alloc || extents_to_split) {
  1852. /*
  1853. * XXX: We are stretching the limits of
  1854. * ocfs2_lock_allocators(). It greatly over-estimates
  1855. * the work to be done.
  1856. */
  1857. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  1858. wc->w_di_bh);
  1859. ret = ocfs2_lock_allocators(inode, &et,
  1860. clusters_to_alloc, extents_to_split,
  1861. &data_ac, &meta_ac);
  1862. if (ret) {
  1863. mlog_errno(ret);
  1864. goto out;
  1865. }
  1866. if (data_ac)
  1867. data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  1868. credits = ocfs2_calc_extend_credits(inode->i_sb,
  1869. &di->id2.i_list);
  1870. }
  1871. /*
  1872. * We have to zero sparse allocated clusters, unwritten extent clusters,
  1873. * and non-sparse clusters we just extended. For non-sparse writes,
  1874. * we know zeros will only be needed in the first and/or last cluster.
  1875. */
  1876. if (clusters_to_alloc || extents_to_split ||
  1877. (wc->w_clen && (wc->w_desc[0].c_needs_zero ||
  1878. wc->w_desc[wc->w_clen - 1].c_needs_zero)))
  1879. cluster_of_pages = 1;
  1880. else
  1881. cluster_of_pages = 0;
  1882. ocfs2_set_target_boundaries(osb, wc, pos, len, cluster_of_pages);
  1883. handle = ocfs2_start_trans(osb, credits);
  1884. if (IS_ERR(handle)) {
  1885. ret = PTR_ERR(handle);
  1886. mlog_errno(ret);
  1887. goto out;
  1888. }
  1889. wc->w_handle = handle;
  1890. if (clusters_to_alloc) {
  1891. ret = dquot_alloc_space_nodirty(inode,
  1892. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1893. if (ret)
  1894. goto out_commit;
  1895. }
  1896. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1897. OCFS2_JOURNAL_ACCESS_WRITE);
  1898. if (ret) {
  1899. mlog_errno(ret);
  1900. goto out_quota;
  1901. }
  1902. /*
  1903. * Fill our page array first. That way we've grabbed enough so
  1904. * that we can zero and flush if we error after adding the
  1905. * extent.
  1906. */
  1907. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos, len,
  1908. cluster_of_pages, mmap_page);
  1909. if (ret && ret != -EAGAIN) {
  1910. mlog_errno(ret);
  1911. goto out_quota;
  1912. }
  1913. /*
  1914. * ocfs2_grab_pages_for_write() returns -EAGAIN if it could not lock
  1915. * the target page. In this case, we exit with no error and no target
  1916. * page. This will trigger the caller, page_mkwrite(), to re-try
  1917. * the operation.
  1918. */
  1919. if (ret == -EAGAIN) {
  1920. BUG_ON(wc->w_target_page);
  1921. ret = 0;
  1922. goto out_quota;
  1923. }
  1924. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1925. len);
  1926. if (ret) {
  1927. mlog_errno(ret);
  1928. goto out_quota;
  1929. }
  1930. if (data_ac)
  1931. ocfs2_free_alloc_context(data_ac);
  1932. if (meta_ac)
  1933. ocfs2_free_alloc_context(meta_ac);
  1934. success:
  1935. *pagep = wc->w_target_page;
  1936. *fsdata = wc;
  1937. return 0;
  1938. out_quota:
  1939. if (clusters_to_alloc)
  1940. dquot_free_space(inode,
  1941. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1942. out_commit:
  1943. ocfs2_commit_trans(osb, handle);
  1944. out:
  1945. ocfs2_free_write_ctxt(wc);
  1946. if (data_ac) {
  1947. ocfs2_free_alloc_context(data_ac);
  1948. data_ac = NULL;
  1949. }
  1950. if (meta_ac) {
  1951. ocfs2_free_alloc_context(meta_ac);
  1952. meta_ac = NULL;
  1953. }
  1954. if (ret == -ENOSPC && try_free) {
  1955. /*
  1956. * Try to free some truncate log so that we can have enough
  1957. * clusters to allocate.
  1958. */
  1959. try_free = 0;
  1960. ret1 = ocfs2_try_to_free_truncate_log(osb, clusters_need);
  1961. if (ret1 == 1)
  1962. goto try_again;
  1963. if (ret1 < 0)
  1964. mlog_errno(ret1);
  1965. }
  1966. return ret;
  1967. }
  1968. static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1969. loff_t pos, unsigned len, unsigned flags,
  1970. struct page **pagep, void **fsdata)
  1971. {
  1972. int ret;
  1973. struct buffer_head *di_bh = NULL;
  1974. struct inode *inode = mapping->host;
  1975. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1976. if (ret) {
  1977. mlog_errno(ret);
  1978. return ret;
  1979. }
  1980. /*
  1981. * Take alloc sem here to prevent concurrent lookups. That way
  1982. * the mapping, zeroing and tree manipulation within
  1983. * ocfs2_write() will be safe against ->readpage(). This
  1984. * should also serve to lock out allocation from a shared
  1985. * writeable region.
  1986. */
  1987. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1988. ret = ocfs2_write_begin_nolock(file, mapping, pos, len, flags, pagep,
  1989. fsdata, di_bh, NULL);
  1990. if (ret) {
  1991. mlog_errno(ret);
  1992. goto out_fail;
  1993. }
  1994. brelse(di_bh);
  1995. return 0;
  1996. out_fail:
  1997. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1998. brelse(di_bh);
  1999. ocfs2_inode_unlock(inode, 1);
  2000. return ret;
  2001. }
  2002. static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
  2003. unsigned len, unsigned *copied,
  2004. struct ocfs2_dinode *di,
  2005. struct ocfs2_write_ctxt *wc)
  2006. {
  2007. void *kaddr;
  2008. if (unlikely(*copied < len)) {
  2009. if (!PageUptodate(wc->w_target_page)) {
  2010. *copied = 0;
  2011. return;
  2012. }
  2013. }
  2014. kaddr = kmap_atomic(wc->w_target_page);
  2015. memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
  2016. kunmap_atomic(kaddr);
  2017. trace_ocfs2_write_end_inline(
  2018. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2019. (unsigned long long)pos, *copied,
  2020. le16_to_cpu(di->id2.i_data.id_count),
  2021. le16_to_cpu(di->i_dyn_features));
  2022. }
  2023. int ocfs2_write_end_nolock(struct address_space *mapping,
  2024. loff_t pos, unsigned len, unsigned copied,
  2025. struct page *page, void *fsdata)
  2026. {
  2027. int i, ret;
  2028. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  2029. struct inode *inode = mapping->host;
  2030. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2031. struct ocfs2_write_ctxt *wc = fsdata;
  2032. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  2033. handle_t *handle = wc->w_handle;
  2034. struct page *tmppage;
  2035. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  2036. OCFS2_JOURNAL_ACCESS_WRITE);
  2037. if (ret) {
  2038. copied = ret;
  2039. mlog_errno(ret);
  2040. goto out;
  2041. }
  2042. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2043. ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
  2044. goto out_write_size;
  2045. }
  2046. if (unlikely(copied < len)) {
  2047. if (!PageUptodate(wc->w_target_page))
  2048. copied = 0;
  2049. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  2050. start+len);
  2051. }
  2052. flush_dcache_page(wc->w_target_page);
  2053. for(i = 0; i < wc->w_num_pages; i++) {
  2054. tmppage = wc->w_pages[i];
  2055. if (tmppage == wc->w_target_page) {
  2056. from = wc->w_target_from;
  2057. to = wc->w_target_to;
  2058. BUG_ON(from > PAGE_CACHE_SIZE ||
  2059. to > PAGE_CACHE_SIZE ||
  2060. to < from);
  2061. } else {
  2062. /*
  2063. * Pages adjacent to the target (if any) imply
  2064. * a hole-filling write in which case we want
  2065. * to flush their entire range.
  2066. */
  2067. from = 0;
  2068. to = PAGE_CACHE_SIZE;
  2069. }
  2070. if (page_has_buffers(tmppage)) {
  2071. if (ocfs2_should_order_data(inode))
  2072. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  2073. block_commit_write(tmppage, from, to);
  2074. }
  2075. }
  2076. out_write_size:
  2077. pos += copied;
  2078. if (pos > i_size_read(inode)) {
  2079. i_size_write(inode, pos);
  2080. mark_inode_dirty(inode);
  2081. }
  2082. inode->i_blocks = ocfs2_inode_sector_count(inode);
  2083. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  2084. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2085. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  2086. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  2087. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  2088. ocfs2_journal_dirty(handle, wc->w_di_bh);
  2089. out:
  2090. /* unlock pages before dealloc since it needs acquiring j_trans_barrier
  2091. * lock, or it will cause a deadlock since journal commit threads holds
  2092. * this lock and will ask for the page lock when flushing the data.
  2093. * put it here to preserve the unlock order.
  2094. */
  2095. ocfs2_unlock_pages(wc);
  2096. ocfs2_commit_trans(osb, handle);
  2097. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  2098. brelse(wc->w_di_bh);
  2099. kfree(wc);
  2100. return copied;
  2101. }
  2102. static int ocfs2_write_end(struct file *file, struct address_space *mapping,
  2103. loff_t pos, unsigned len, unsigned copied,
  2104. struct page *page, void *fsdata)
  2105. {
  2106. int ret;
  2107. struct inode *inode = mapping->host;
  2108. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  2109. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  2110. ocfs2_inode_unlock(inode, 1);
  2111. return ret;
  2112. }
  2113. const struct address_space_operations ocfs2_aops = {
  2114. .readpage = ocfs2_readpage,
  2115. .readpages = ocfs2_readpages,
  2116. .writepage = ocfs2_writepage,
  2117. .write_begin = ocfs2_write_begin,
  2118. .write_end = ocfs2_write_end,
  2119. .bmap = ocfs2_bmap,
  2120. .direct_IO = ocfs2_direct_IO,
  2121. .invalidatepage = block_invalidatepage,
  2122. .releasepage = ocfs2_releasepage,
  2123. .migratepage = buffer_migrate_page,
  2124. .is_partially_uptodate = block_is_partially_uptodate,
  2125. .error_remove_page = generic_error_remove_page,
  2126. };