aops.c 48 KB

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