aops.c 48 KB

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