aops.c 51 KB

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