aops.c 53 KB

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