aops.c 49 KB

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