aops.c 49 KB

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