aops.c 51 KB

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