aops.c 50 KB

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