aops.c 52 KB

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