aops.c 52 KB

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