aops.c 48 KB

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