aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ocfs2/file.c')
-rw-r--r--fs/ocfs2/file.c147
1 files changed, 45 insertions, 102 deletions
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c
index 46e0d4e857c7..913fc250d85a 100644
--- a/fs/ocfs2/file.c
+++ b/fs/ocfs2/file.c
@@ -2106,7 +2106,7 @@ out:
2106} 2106}
2107 2107
2108static int ocfs2_prepare_inode_for_write(struct file *file, 2108static int ocfs2_prepare_inode_for_write(struct file *file,
2109 loff_t *ppos, 2109 loff_t pos,
2110 size_t count, 2110 size_t count,
2111 int appending, 2111 int appending,
2112 int *direct_io, 2112 int *direct_io,
@@ -2115,7 +2115,7 @@ static int ocfs2_prepare_inode_for_write(struct file *file,
2115 int ret = 0, meta_level = 0; 2115 int ret = 0, meta_level = 0;
2116 struct dentry *dentry = file->f_path.dentry; 2116 struct dentry *dentry = file->f_path.dentry;
2117 struct inode *inode = dentry->d_inode; 2117 struct inode *inode = dentry->d_inode;
2118 loff_t saved_pos = 0, end; 2118 loff_t end;
2119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2120 int full_coherency = !(osb->s_mount_opt & 2120 int full_coherency = !(osb->s_mount_opt &
2121 OCFS2_MOUNT_COHERENCY_BUFFERED); 2121 OCFS2_MOUNT_COHERENCY_BUFFERED);
@@ -2155,23 +2155,16 @@ static int ocfs2_prepare_inode_for_write(struct file *file,
2155 } 2155 }
2156 } 2156 }
2157 2157
2158 /* work on a copy of ppos until we're sure that we won't have 2158 end = pos + count;
2159 * to recalculate it due to relocking. */
2160 if (appending)
2161 saved_pos = i_size_read(inode);
2162 else
2163 saved_pos = *ppos;
2164
2165 end = saved_pos + count;
2166 2159
2167 ret = ocfs2_check_range_for_refcount(inode, saved_pos, count); 2160 ret = ocfs2_check_range_for_refcount(inode, pos, count);
2168 if (ret == 1) { 2161 if (ret == 1) {
2169 ocfs2_inode_unlock(inode, meta_level); 2162 ocfs2_inode_unlock(inode, meta_level);
2170 meta_level = -1; 2163 meta_level = -1;
2171 2164
2172 ret = ocfs2_prepare_inode_for_refcount(inode, 2165 ret = ocfs2_prepare_inode_for_refcount(inode,
2173 file, 2166 file,
2174 saved_pos, 2167 pos,
2175 count, 2168 count,
2176 &meta_level); 2169 &meta_level);
2177 if (has_refcount) 2170 if (has_refcount)
@@ -2227,7 +2220,7 @@ static int ocfs2_prepare_inode_for_write(struct file *file,
2227 * caller will have to retake some cluster 2220 * caller will have to retake some cluster
2228 * locks and initiate the io as buffered. 2221 * locks and initiate the io as buffered.
2229 */ 2222 */
2230 ret = ocfs2_check_range_for_holes(inode, saved_pos, count); 2223 ret = ocfs2_check_range_for_holes(inode, pos, count);
2231 if (ret == 1) { 2224 if (ret == 1) {
2232 /* 2225 /*
2233 * Fallback to old way if the feature bit is not set. 2226 * Fallback to old way if the feature bit is not set.
@@ -2242,12 +2235,9 @@ static int ocfs2_prepare_inode_for_write(struct file *file,
2242 break; 2235 break;
2243 } 2236 }
2244 2237
2245 if (appending)
2246 *ppos = saved_pos;
2247
2248out_unlock: 2238out_unlock:
2249 trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno, 2239 trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
2250 saved_pos, appending, count, 2240 pos, appending, count,
2251 direct_io, has_refcount); 2241 direct_io, has_refcount);
2252 2242
2253 if (meta_level >= 0) 2243 if (meta_level >= 0)
@@ -2260,19 +2250,20 @@ out:
2260static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, 2250static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
2261 struct iov_iter *from) 2251 struct iov_iter *from)
2262{ 2252{
2263 int ret, direct_io, appending, rw_level, have_alloc_sem = 0; 2253 int direct_io, appending, rw_level, have_alloc_sem = 0;
2264 int can_do_direct, has_refcount = 0; 2254 int can_do_direct, has_refcount = 0;
2265 ssize_t written = 0; 2255 ssize_t written = 0;
2266 size_t count = iov_iter_count(from); 2256 ssize_t ret;
2267 loff_t old_size, *ppos = &iocb->ki_pos; 2257 size_t count = iov_iter_count(from), orig_count;
2258 loff_t old_size;
2268 u32 old_clusters; 2259 u32 old_clusters;
2269 struct file *file = iocb->ki_filp; 2260 struct file *file = iocb->ki_filp;
2270 struct inode *inode = file_inode(file); 2261 struct inode *inode = file_inode(file);
2271 struct address_space *mapping = file->f_mapping;
2272 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2262 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2273 int full_coherency = !(osb->s_mount_opt & 2263 int full_coherency = !(osb->s_mount_opt &
2274 OCFS2_MOUNT_COHERENCY_BUFFERED); 2264 OCFS2_MOUNT_COHERENCY_BUFFERED);
2275 int unaligned_dio = 0; 2265 int unaligned_dio = 0;
2266 int dropped_dio = 0;
2276 2267
2277 trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry, 2268 trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
2278 (unsigned long long)OCFS2_I(inode)->ip_blkno, 2269 (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -2280,11 +2271,11 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
2280 file->f_path.dentry->d_name.name, 2271 file->f_path.dentry->d_name.name,
2281 (unsigned int)from->nr_segs); /* GRRRRR */ 2272 (unsigned int)from->nr_segs); /* GRRRRR */
2282 2273
2283 if (iocb->ki_nbytes == 0) 2274 if (count == 0)
2284 return 0; 2275 return 0;
2285 2276
2286 appending = file->f_flags & O_APPEND ? 1 : 0; 2277 appending = iocb->ki_flags & IOCB_APPEND ? 1 : 0;
2287 direct_io = file->f_flags & O_DIRECT ? 1 : 0; 2278 direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
2288 2279
2289 mutex_lock(&inode->i_mutex); 2280 mutex_lock(&inode->i_mutex);
2290 2281
@@ -2329,9 +2320,17 @@ relock:
2329 ocfs2_inode_unlock(inode, 1); 2320 ocfs2_inode_unlock(inode, 1);
2330 } 2321 }
2331 2322
2323 orig_count = iov_iter_count(from);
2324 ret = generic_write_checks(iocb, from);
2325 if (ret <= 0) {
2326 if (ret)
2327 mlog_errno(ret);
2328 goto out;
2329 }
2330 count = ret;
2331
2332 can_do_direct = direct_io; 2332 can_do_direct = direct_io;
2333 ret = ocfs2_prepare_inode_for_write(file, ppos, 2333 ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, appending,
2334 iocb->ki_nbytes, appending,
2335 &can_do_direct, &has_refcount); 2334 &can_do_direct, &has_refcount);
2336 if (ret < 0) { 2335 if (ret < 0) {
2337 mlog_errno(ret); 2336 mlog_errno(ret);
@@ -2339,8 +2338,7 @@ relock:
2339 } 2338 }
2340 2339
2341 if (direct_io && !is_sync_kiocb(iocb)) 2340 if (direct_io && !is_sync_kiocb(iocb))
2342 unaligned_dio = ocfs2_is_io_unaligned(inode, iocb->ki_nbytes, 2341 unaligned_dio = ocfs2_is_io_unaligned(inode, count, iocb->ki_pos);
2343 *ppos);
2344 2342
2345 /* 2343 /*
2346 * We can't complete the direct I/O as requested, fall back to 2344 * We can't complete the direct I/O as requested, fall back to
@@ -2353,6 +2351,9 @@ relock:
2353 rw_level = -1; 2351 rw_level = -1;
2354 2352
2355 direct_io = 0; 2353 direct_io = 0;
2354 iocb->ki_flags &= ~IOCB_DIRECT;
2355 iov_iter_reexpand(from, orig_count);
2356 dropped_dio = 1;
2356 goto relock; 2357 goto relock;
2357 } 2358 }
2358 2359
@@ -2376,74 +2377,18 @@ relock:
2376 /* communicate with ocfs2_dio_end_io */ 2377 /* communicate with ocfs2_dio_end_io */
2377 ocfs2_iocb_set_rw_locked(iocb, rw_level); 2378 ocfs2_iocb_set_rw_locked(iocb, rw_level);
2378 2379
2379 ret = generic_write_checks(file, ppos, &count, 2380 written = __generic_file_write_iter(iocb, from);
2380 S_ISBLK(inode->i_mode));
2381 if (ret)
2382 goto out_dio;
2383
2384 iov_iter_truncate(from, count);
2385 if (direct_io) {
2386 loff_t endbyte;
2387 ssize_t written_buffered;
2388 written = generic_file_direct_write(iocb, from, *ppos);
2389 if (written < 0 || written == count) {
2390 ret = written;
2391 goto out_dio;
2392 }
2393
2394 /*
2395 * for completing the rest of the request.
2396 */
2397 *ppos += written;
2398 count -= written;
2399 written_buffered = generic_perform_write(file, from, *ppos);
2400 /*
2401 * If generic_file_buffered_write() returned a synchronous error
2402 * then we want to return the number of bytes which were
2403 * direct-written, or the error code if that was zero. Note
2404 * that this differs from normal direct-io semantics, which
2405 * will return -EFOO even if some bytes were written.
2406 */
2407 if (written_buffered < 0) {
2408 ret = written_buffered;
2409 goto out_dio;
2410 }
2411
2412 iocb->ki_pos = *ppos + written_buffered;
2413 /* We need to ensure that the page cache pages are written to
2414 * disk and invalidated to preserve the expected O_DIRECT
2415 * semantics.
2416 */
2417 endbyte = *ppos + written_buffered - 1;
2418 ret = filemap_write_and_wait_range(file->f_mapping, *ppos,
2419 endbyte);
2420 if (ret == 0) {
2421 written += written_buffered;
2422 invalidate_mapping_pages(mapping,
2423 *ppos >> PAGE_CACHE_SHIFT,
2424 endbyte >> PAGE_CACHE_SHIFT);
2425 } else {
2426 /*
2427 * We don't know how much we wrote, so just return
2428 * the number of bytes which were direct-written
2429 */
2430 }
2431 } else {
2432 current->backing_dev_info = inode_to_bdi(inode);
2433 written = generic_perform_write(file, from, *ppos);
2434 if (likely(written >= 0))
2435 iocb->ki_pos = *ppos + written;
2436 current->backing_dev_info = NULL;
2437 }
2438
2439out_dio:
2440 /* buffered aio wouldn't have proper lock coverage today */ 2381 /* buffered aio wouldn't have proper lock coverage today */
2441 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT)); 2382 BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
2383
2384 if (unlikely(written <= 0))
2385 goto no_sync;
2442 2386
2443 if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) || 2387 if (((file->f_flags & O_DSYNC) && !direct_io) ||
2444 ((file->f_flags & O_DIRECT) && !direct_io)) { 2388 IS_SYNC(inode) || dropped_dio) {
2445 ret = filemap_fdatawrite_range(file->f_mapping, *ppos, 2389 ret = filemap_fdatawrite_range(file->f_mapping,
2446 *ppos + count - 1); 2390 iocb->ki_pos - written,
2391 iocb->ki_pos - 1);
2447 if (ret < 0) 2392 if (ret < 0)
2448 written = ret; 2393 written = ret;
2449 2394
@@ -2454,10 +2399,12 @@ out_dio:
2454 } 2399 }
2455 2400
2456 if (!ret) 2401 if (!ret)
2457 ret = filemap_fdatawait_range(file->f_mapping, *ppos, 2402 ret = filemap_fdatawait_range(file->f_mapping,
2458 *ppos + count - 1); 2403 iocb->ki_pos - written,
2404 iocb->ki_pos - 1);
2459 } 2405 }
2460 2406
2407no_sync:
2461 /* 2408 /*
2462 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io 2409 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2463 * function pointer which is called when o_direct io completes so that 2410 * function pointer which is called when o_direct io completes so that
@@ -2549,7 +2496,7 @@ static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
2549 * buffered reads protect themselves in ->readpage(). O_DIRECT reads 2496 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
2550 * need locks to protect pending reads from racing with truncate. 2497 * need locks to protect pending reads from racing with truncate.
2551 */ 2498 */
2552 if (filp->f_flags & O_DIRECT) { 2499 if (iocb->ki_flags & IOCB_DIRECT) {
2553 have_alloc_sem = 1; 2500 have_alloc_sem = 1;
2554 ocfs2_iocb_set_sem_locked(iocb); 2501 ocfs2_iocb_set_sem_locked(iocb);
2555 2502
@@ -2583,7 +2530,7 @@ static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
2583 trace_generic_file_aio_read_ret(ret); 2530 trace_generic_file_aio_read_ret(ret);
2584 2531
2585 /* buffered aio wouldn't have proper lock coverage today */ 2532 /* buffered aio wouldn't have proper lock coverage today */
2586 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); 2533 BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
2587 2534
2588 /* see ocfs2_file_write_iter */ 2535 /* see ocfs2_file_write_iter */
2589 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { 2536 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
@@ -2678,8 +2625,6 @@ const struct inode_operations ocfs2_special_file_iops = {
2678 */ 2625 */
2679const struct file_operations ocfs2_fops = { 2626const struct file_operations ocfs2_fops = {
2680 .llseek = ocfs2_file_llseek, 2627 .llseek = ocfs2_file_llseek,
2681 .read = new_sync_read,
2682 .write = new_sync_write,
2683 .mmap = ocfs2_mmap, 2628 .mmap = ocfs2_mmap,
2684 .fsync = ocfs2_sync_file, 2629 .fsync = ocfs2_sync_file,
2685 .release = ocfs2_file_release, 2630 .release = ocfs2_file_release,
@@ -2726,8 +2671,6 @@ const struct file_operations ocfs2_dops = {
2726 */ 2671 */
2727const struct file_operations ocfs2_fops_no_plocks = { 2672const struct file_operations ocfs2_fops_no_plocks = {
2728 .llseek = ocfs2_file_llseek, 2673 .llseek = ocfs2_file_llseek,
2729 .read = new_sync_read,
2730 .write = new_sync_write,
2731 .mmap = ocfs2_mmap, 2674 .mmap = ocfs2_mmap,
2732 .fsync = ocfs2_sync_file, 2675 .fsync = ocfs2_sync_file,
2733 .release = ocfs2_file_release, 2676 .release = ocfs2_file_release,