ocfs2: support writing of unwritten extents
[deliverable/linux.git] / fs / ocfs2 / file.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * file.c
5 *
6 * File open, close, extend, truncate
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
16f7e0fe 26#include <linux/capability.h>
ccd979bd
MF
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31#include <linux/pagemap.h>
32#include <linux/uio.h>
e2057c5a 33#include <linux/sched.h>
d6b29d7c 34#include <linux/splice.h>
7f1a37e3 35#include <linux/mount.h>
9517bac6 36#include <linux/writeback.h>
ccd979bd
MF
37
38#define MLOG_MASK_PREFIX ML_INODE
39#include <cluster/masklog.h>
40
41#include "ocfs2.h"
42
43#include "alloc.h"
44#include "aops.h"
45#include "dir.h"
46#include "dlmglue.h"
47#include "extent_map.h"
48#include "file.h"
49#include "sysfile.h"
50#include "inode.h"
ca4d147e 51#include "ioctl.h"
ccd979bd
MF
52#include "journal.h"
53#include "mmap.h"
54#include "suballoc.h"
55#include "super.h"
56
57#include "buffer_head_io.h"
58
59static int ocfs2_sync_inode(struct inode *inode)
60{
61 filemap_fdatawrite(inode->i_mapping);
62 return sync_mapping_buffers(inode->i_mapping);
63}
64
65static int ocfs2_file_open(struct inode *inode, struct file *file)
66{
67 int status;
68 int mode = file->f_flags;
69 struct ocfs2_inode_info *oi = OCFS2_I(inode);
70
71 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
d28c9174 72 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
ccd979bd
MF
73
74 spin_lock(&oi->ip_lock);
75
76 /* Check that the inode hasn't been wiped from disk by another
77 * node. If it hasn't then we're safe as long as we hold the
78 * spin lock until our increment of open count. */
79 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
80 spin_unlock(&oi->ip_lock);
81
82 status = -ENOENT;
83 goto leave;
84 }
85
86 if (mode & O_DIRECT)
87 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
88
89 oi->ip_open_count++;
90 spin_unlock(&oi->ip_lock);
91 status = 0;
92leave:
93 mlog_exit(status);
94 return status;
95}
96
97static int ocfs2_file_release(struct inode *inode, struct file *file)
98{
99 struct ocfs2_inode_info *oi = OCFS2_I(inode);
100
101 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
d28c9174
JS
102 file->f_path.dentry->d_name.len,
103 file->f_path.dentry->d_name.name);
ccd979bd
MF
104
105 spin_lock(&oi->ip_lock);
106 if (!--oi->ip_open_count)
107 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
108 spin_unlock(&oi->ip_lock);
109
110 mlog_exit(0);
111
112 return 0;
113}
114
115static int ocfs2_sync_file(struct file *file,
116 struct dentry *dentry,
117 int datasync)
118{
119 int err = 0;
120 journal_t *journal;
121 struct inode *inode = dentry->d_inode;
122 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
123
124 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
125 dentry->d_name.len, dentry->d_name.name);
126
127 err = ocfs2_sync_inode(dentry->d_inode);
128 if (err)
129 goto bail;
130
131 journal = osb->journal->j_journal;
132 err = journal_force_commit(journal);
133
134bail:
135 mlog_exit(err);
136
137 return (err < 0) ? -EIO : 0;
138}
139
7f1a37e3
TY
140int ocfs2_should_update_atime(struct inode *inode,
141 struct vfsmount *vfsmnt)
142{
143 struct timespec now;
144 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
145
146 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
147 return 0;
148
149 if ((inode->i_flags & S_NOATIME) ||
150 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
151 return 0;
152
6c2aad05
MF
153 /*
154 * We can be called with no vfsmnt structure - NFSD will
155 * sometimes do this.
156 *
157 * Note that our action here is different than touch_atime() -
158 * if we can't tell whether this is a noatime mount, then we
159 * don't know whether to trust the value of s_atime_quantum.
160 */
161 if (vfsmnt == NULL)
162 return 0;
163
7f1a37e3
TY
164 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
165 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
166 return 0;
167
7e913c53
MF
168 if (vfsmnt->mnt_flags & MNT_RELATIME) {
169 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
170 (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
171 return 1;
172
173 return 0;
174 }
175
7f1a37e3
TY
176 now = CURRENT_TIME;
177 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
178 return 0;
179 else
180 return 1;
181}
182
183int ocfs2_update_inode_atime(struct inode *inode,
184 struct buffer_head *bh)
185{
186 int ret;
187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
188 handle_t *handle;
189
190 mlog_entry_void();
191
192 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
193 if (handle == NULL) {
194 ret = -ENOMEM;
195 mlog_errno(ret);
196 goto out;
197 }
198
199 inode->i_atime = CURRENT_TIME;
200 ret = ocfs2_mark_inode_dirty(handle, inode, bh);
201 if (ret < 0)
202 mlog_errno(ret);
203
204 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
205out:
206 mlog_exit(ret);
207 return ret;
208}
209
6cb129f5
AB
210static int ocfs2_set_inode_size(handle_t *handle,
211 struct inode *inode,
212 struct buffer_head *fe_bh,
213 u64 new_i_size)
ccd979bd
MF
214{
215 int status;
216
217 mlog_entry_void();
218 i_size_write(inode, new_i_size);
8110b073 219 inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bd
MF
220 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
221
222 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
223 if (status < 0) {
224 mlog_errno(status);
225 goto bail;
226 }
227
228bail:
229 mlog_exit(status);
230 return status;
231}
232
233static int ocfs2_simple_size_update(struct inode *inode,
234 struct buffer_head *di_bh,
235 u64 new_i_size)
236{
237 int ret;
238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1fabe148 239 handle_t *handle = NULL;
ccd979bd 240
65eff9cc 241 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
242 if (handle == NULL) {
243 ret = -ENOMEM;
244 mlog_errno(ret);
245 goto out;
246 }
247
248 ret = ocfs2_set_inode_size(handle, inode, di_bh,
249 new_i_size);
250 if (ret < 0)
251 mlog_errno(ret);
252
02dc1af4 253 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
254out:
255 return ret;
256}
257
258static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
259 struct inode *inode,
260 struct buffer_head *fe_bh,
261 u64 new_i_size)
262{
263 int status;
1fabe148 264 handle_t *handle;
60b11392 265 struct ocfs2_dinode *di;
ccd979bd
MF
266
267 mlog_entry_void();
268
269 /* TODO: This needs to actually orphan the inode in this
270 * transaction. */
271
65eff9cc 272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
273 if (IS_ERR(handle)) {
274 status = PTR_ERR(handle);
275 mlog_errno(status);
276 goto out;
277 }
278
60b11392
MF
279 status = ocfs2_journal_access(handle, inode, fe_bh,
280 OCFS2_JOURNAL_ACCESS_WRITE);
281 if (status < 0) {
282 mlog_errno(status);
283 goto out_commit;
284 }
285
286 /*
287 * Do this before setting i_size.
288 */
289 status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
290 if (status) {
291 mlog_errno(status);
292 goto out_commit;
293 }
294
295 i_size_write(inode, new_i_size);
296 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
297 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
298
299 di = (struct ocfs2_dinode *) fe_bh->b_data;
300 di->i_size = cpu_to_le64(new_i_size);
301 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
302 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
303
304 status = ocfs2_journal_dirty(handle, fe_bh);
ccd979bd
MF
305 if (status < 0)
306 mlog_errno(status);
307
60b11392 308out_commit:
02dc1af4 309 ocfs2_commit_trans(osb, handle);
ccd979bd 310out:
60b11392 311
ccd979bd
MF
312 mlog_exit(status);
313 return status;
314}
315
316static int ocfs2_truncate_file(struct inode *inode,
317 struct buffer_head *di_bh,
318 u64 new_i_size)
319{
320 int status = 0;
321 struct ocfs2_dinode *fe = NULL;
322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
323 struct ocfs2_truncate_context *tc = NULL;
324
b0697053
MF
325 mlog_entry("(inode = %llu, new_i_size = %llu\n",
326 (unsigned long long)OCFS2_I(inode)->ip_blkno,
327 (unsigned long long)new_i_size);
ccd979bd 328
ccd979bd
MF
329 fe = (struct ocfs2_dinode *) di_bh->b_data;
330 if (!OCFS2_IS_VALID_DINODE(fe)) {
331 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
332 status = -EIO;
333 goto bail;
334 }
335
336 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
b0697053
MF
337 "Inode %llu, inode i_size = %lld != di "
338 "i_size = %llu, i_flags = 0x%x\n",
339 (unsigned long long)OCFS2_I(inode)->ip_blkno,
ccd979bd 340 i_size_read(inode),
b0697053
MF
341 (unsigned long long)le64_to_cpu(fe->i_size),
342 le32_to_cpu(fe->i_flags));
ccd979bd
MF
343
344 if (new_i_size > le64_to_cpu(fe->i_size)) {
b0697053
MF
345 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
346 (unsigned long long)le64_to_cpu(fe->i_size),
347 (unsigned long long)new_i_size);
ccd979bd
MF
348 status = -EINVAL;
349 mlog_errno(status);
350 goto bail;
351 }
352
b0697053
MF
353 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
354 (unsigned long long)le64_to_cpu(fe->i_blkno),
355 (unsigned long long)le64_to_cpu(fe->i_size),
356 (unsigned long long)new_i_size);
ccd979bd
MF
357
358 /* lets handle the simple truncate cases before doing any more
359 * cluster locking. */
360 if (new_i_size == le64_to_cpu(fe->i_size))
361 goto bail;
362
2e89b2e4
MF
363 down_write(&OCFS2_I(inode)->ip_alloc_sem);
364
ab0920ce
MF
365 /* This forces other nodes to sync and drop their pages. Do
366 * this even if we have a truncate without allocation change -
367 * ocfs2 cluster sizes can be much greater than page size, so
368 * we have to truncate them anyway. */
369 status = ocfs2_data_lock(inode, 1);
370 if (status < 0) {
2e89b2e4
MF
371 up_write(&OCFS2_I(inode)->ip_alloc_sem);
372
ab0920ce
MF
373 mlog_errno(status);
374 goto bail;
375 }
ab0920ce 376
2e89b2e4
MF
377 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
378 truncate_inode_pages(inode->i_mapping, new_i_size);
379
ccd979bd
MF
380 /* alright, we're going to need to do a full blown alloc size
381 * change. Orphan the inode so that recovery can complete the
382 * truncate if necessary. This does the task of marking
383 * i_size. */
384 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
385 if (status < 0) {
386 mlog_errno(status);
60b11392 387 goto bail_unlock_data;
ccd979bd
MF
388 }
389
390 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
391 if (status < 0) {
392 mlog_errno(status);
60b11392 393 goto bail_unlock_data;
ccd979bd
MF
394 }
395
396 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
397 if (status < 0) {
398 mlog_errno(status);
60b11392 399 goto bail_unlock_data;
ccd979bd
MF
400 }
401
402 /* TODO: orphan dir cleanup here. */
60b11392
MF
403bail_unlock_data:
404 ocfs2_data_unlock(inode, 1);
405
2e89b2e4
MF
406 up_write(&OCFS2_I(inode)->ip_alloc_sem);
407
ccd979bd
MF
408bail:
409
410 mlog_exit(status);
411 return status;
412}
413
414/*
415 * extend allocation only here.
416 * we'll update all the disk stuff, and oip->alloc_size
417 *
418 * expect stuff to be locked, a transaction started and enough data /
419 * metadata reservations in the contexts.
420 *
421 * Will return -EAGAIN, and a reason if a restart is needed.
422 * If passed in, *reason will always be set, even in error.
423 */
424int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
425 struct inode *inode,
dcd0538f 426 u32 *logical_offset,
ccd979bd
MF
427 u32 clusters_to_add,
428 struct buffer_head *fe_bh,
1fabe148 429 handle_t *handle,
ccd979bd
MF
430 struct ocfs2_alloc_context *data_ac,
431 struct ocfs2_alloc_context *meta_ac,
432 enum ocfs2_alloc_restarted *reason_ret)
433{
434 int status = 0;
435 int free_extents;
436 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
437 enum ocfs2_alloc_restarted reason = RESTART_NONE;
438 u32 bit_off, num_bits;
439 u64 block;
440
441 BUG_ON(!clusters_to_add);
442
443 free_extents = ocfs2_num_free_extents(osb, inode, fe);
444 if (free_extents < 0) {
445 status = free_extents;
446 mlog_errno(status);
447 goto leave;
448 }
449
450 /* there are two cases which could cause us to EAGAIN in the
451 * we-need-more-metadata case:
452 * 1) we haven't reserved *any*
453 * 2) we are so fragmented, we've needed to add metadata too
454 * many times. */
455 if (!free_extents && !meta_ac) {
456 mlog(0, "we haven't reserved any metadata!\n");
457 status = -EAGAIN;
458 reason = RESTART_META;
459 goto leave;
460 } else if ((!free_extents)
461 && (ocfs2_alloc_context_bits_left(meta_ac)
462 < ocfs2_extend_meta_needed(fe))) {
463 mlog(0, "filesystem is really fragmented...\n");
464 status = -EAGAIN;
465 reason = RESTART_META;
466 goto leave;
467 }
468
469 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
470 &bit_off, &num_bits);
471 if (status < 0) {
472 if (status != -ENOSPC)
473 mlog_errno(status);
474 goto leave;
475 }
476
477 BUG_ON(num_bits > clusters_to_add);
478
479 /* reserve our write early -- insert_extent may update the inode */
480 status = ocfs2_journal_access(handle, inode, fe_bh,
481 OCFS2_JOURNAL_ACCESS_WRITE);
482 if (status < 0) {
483 mlog_errno(status);
484 goto leave;
485 }
486
487 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
b0697053
MF
488 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
489 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
dcd0538f
MF
490 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
491 *logical_offset, block, num_bits,
492 meta_ac);
ccd979bd
MF
493 if (status < 0) {
494 mlog_errno(status);
495 goto leave;
496 }
497
ccd979bd
MF
498 status = ocfs2_journal_dirty(handle, fe_bh);
499 if (status < 0) {
500 mlog_errno(status);
501 goto leave;
502 }
503
504 clusters_to_add -= num_bits;
dcd0538f 505 *logical_offset += num_bits;
ccd979bd
MF
506
507 if (clusters_to_add) {
508 mlog(0, "need to alloc once more, clusters = %u, wanted = "
509 "%u\n", fe->i_clusters, clusters_to_add);
510 status = -EAGAIN;
511 reason = RESTART_TRANS;
512 }
513
514leave:
515 mlog_exit(status);
516 if (reason_ret)
517 *reason_ret = reason;
518 return status;
519}
520
abf8b156
MF
521/*
522 * For a given allocation, determine which allocators will need to be
523 * accessed, and lock them, reserving the appropriate number of bits.
524 *
525 * Called from ocfs2_extend_allocation() for file systems which don't
9517bac6
MF
526 * support holes, and from ocfs2_write() for file systems which
527 * understand sparse inodes.
abf8b156 528 */
9517bac6 529int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
b27b7cbc 530 u32 clusters_to_add, u32 extents_to_split,
9517bac6
MF
531 struct ocfs2_alloc_context **data_ac,
532 struct ocfs2_alloc_context **meta_ac)
abf8b156
MF
533{
534 int ret, num_free_extents;
b27b7cbc 535 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
abf8b156
MF
536 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
537
538 *meta_ac = NULL;
539 *data_ac = NULL;
540
541 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
b27b7cbc 542 "clusters_to_add = %u, extents_to_split = %u\n",
abf8b156 543 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
b27b7cbc 544 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
abf8b156
MF
545
546 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
547 if (num_free_extents < 0) {
548 ret = num_free_extents;
549 mlog_errno(ret);
550 goto out;
551 }
552
553 /*
554 * Sparse allocation file systems need to be more conservative
555 * with reserving room for expansion - the actual allocation
556 * happens while we've got a journal handle open so re-taking
557 * a cluster lock (because we ran out of room for another
558 * extent) will violate ordering rules.
559 *
9517bac6 560 * Most of the time we'll only be seeing this 1 cluster at a time
abf8b156 561 * anyway.
b27b7cbc
MF
562 *
563 * Always lock for any unwritten extents - we might want to
564 * add blocks during a split.
abf8b156
MF
565 */
566 if (!num_free_extents ||
b27b7cbc 567 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
abf8b156
MF
568 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
569 if (ret < 0) {
570 if (ret != -ENOSPC)
571 mlog_errno(ret);
572 goto out;
573 }
574 }
575
576 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
577 if (ret < 0) {
578 if (ret != -ENOSPC)
579 mlog_errno(ret);
580 goto out;
581 }
582
583out:
584 if (ret) {
585 if (*meta_ac) {
586 ocfs2_free_alloc_context(*meta_ac);
587 *meta_ac = NULL;
588 }
589
590 /*
591 * We cannot have an error and a non null *data_ac.
592 */
593 }
594
595 return ret;
596}
597
ccd979bd
MF
598static int ocfs2_extend_allocation(struct inode *inode,
599 u32 clusters_to_add)
600{
601 int status = 0;
602 int restart_func = 0;
603 int drop_alloc_sem = 0;
abf8b156 604 int credits;
dcd0538f 605 u32 prev_clusters, logical_start;
ccd979bd
MF
606 struct buffer_head *bh = NULL;
607 struct ocfs2_dinode *fe = NULL;
1fabe148 608 handle_t *handle = NULL;
ccd979bd
MF
609 struct ocfs2_alloc_context *data_ac = NULL;
610 struct ocfs2_alloc_context *meta_ac = NULL;
611 enum ocfs2_alloc_restarted why;
612 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
613
614 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
615
dcd0538f
MF
616 /*
617 * This function only exists for file systems which don't
618 * support holes.
619 */
620 BUG_ON(ocfs2_sparse_alloc(osb));
621
ccd979bd
MF
622 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
623 OCFS2_BH_CACHED, inode);
624 if (status < 0) {
625 mlog_errno(status);
626 goto leave;
627 }
628
629 fe = (struct ocfs2_dinode *) bh->b_data;
630 if (!OCFS2_IS_VALID_DINODE(fe)) {
631 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
632 status = -EIO;
633 goto leave;
634 }
635
dcd0538f
MF
636 logical_start = OCFS2_I(inode)->ip_clusters;
637
ccd979bd
MF
638restart_all:
639 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
640
ccd979bd 641 /* blocks peope in read/write from reading our allocation
1b1dcc1b 642 * until we're done changing it. We depend on i_mutex to block
ccd979bd
MF
643 * other extend/truncate calls while we're here. Ordering wrt
644 * start_trans is important here -- always do it before! */
645 down_write(&OCFS2_I(inode)->ip_alloc_sem);
646 drop_alloc_sem = 1;
647
b27b7cbc 648 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
9517bac6
MF
649 &meta_ac);
650 if (status) {
651 mlog_errno(status);
652 goto leave;
653 }
654
ccd979bd 655 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
65eff9cc 656 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
657 if (IS_ERR(handle)) {
658 status = PTR_ERR(handle);
659 handle = NULL;
660 mlog_errno(status);
661 goto leave;
662 }
663
664restarted_transaction:
665 /* reserve a write to the file entry early on - that we if we
666 * run out of credits in the allocation path, we can still
667 * update i_size. */
668 status = ocfs2_journal_access(handle, inode, bh,
669 OCFS2_JOURNAL_ACCESS_WRITE);
670 if (status < 0) {
671 mlog_errno(status);
672 goto leave;
673 }
674
675 prev_clusters = OCFS2_I(inode)->ip_clusters;
676
677 status = ocfs2_do_extend_allocation(osb,
678 inode,
dcd0538f 679 &logical_start,
ccd979bd
MF
680 clusters_to_add,
681 bh,
682 handle,
683 data_ac,
684 meta_ac,
685 &why);
686 if ((status < 0) && (status != -EAGAIN)) {
687 if (status != -ENOSPC)
688 mlog_errno(status);
689 goto leave;
690 }
691
692 status = ocfs2_journal_dirty(handle, bh);
693 if (status < 0) {
694 mlog_errno(status);
695 goto leave;
696 }
697
698 spin_lock(&OCFS2_I(inode)->ip_lock);
699 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
700 spin_unlock(&OCFS2_I(inode)->ip_lock);
701
702 if (why != RESTART_NONE && clusters_to_add) {
703 if (why == RESTART_META) {
704 mlog(0, "restarting function.\n");
705 restart_func = 1;
706 } else {
707 BUG_ON(why != RESTART_TRANS);
708
709 mlog(0, "restarting transaction.\n");
710 /* TODO: This can be more intelligent. */
711 credits = ocfs2_calc_extend_credits(osb->sb,
712 fe,
713 clusters_to_add);
1fabe148 714 status = ocfs2_extend_trans(handle, credits);
ccd979bd
MF
715 if (status < 0) {
716 /* handle still has to be committed at
717 * this point. */
718 status = -ENOMEM;
719 mlog_errno(status);
720 goto leave;
721 }
722 goto restarted_transaction;
723 }
724 }
725
b0697053 726 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
1ca1a111
MF
727 le32_to_cpu(fe->i_clusters),
728 (unsigned long long)le64_to_cpu(fe->i_size));
ccd979bd
MF
729 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
730 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
731
732leave:
733 if (drop_alloc_sem) {
734 up_write(&OCFS2_I(inode)->ip_alloc_sem);
735 drop_alloc_sem = 0;
736 }
737 if (handle) {
02dc1af4 738 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
739 handle = NULL;
740 }
741 if (data_ac) {
742 ocfs2_free_alloc_context(data_ac);
743 data_ac = NULL;
744 }
745 if (meta_ac) {
746 ocfs2_free_alloc_context(meta_ac);
747 meta_ac = NULL;
748 }
749 if ((!status) && restart_func) {
750 restart_func = 0;
751 goto restart_all;
752 }
753 if (bh) {
754 brelse(bh);
755 bh = NULL;
756 }
757
758 mlog_exit(status);
759 return status;
760}
761
762/* Some parts of this taken from generic_cont_expand, which turned out
763 * to be too fragile to do exactly what we need without us having to
53013cba
MF
764 * worry about recursive locking in ->prepare_write() and
765 * ->commit_write(). */
ccd979bd
MF
766static int ocfs2_write_zero_page(struct inode *inode,
767 u64 size)
768{
769 struct address_space *mapping = inode->i_mapping;
770 struct page *page;
771 unsigned long index;
772 unsigned int offset;
1fabe148 773 handle_t *handle = NULL;
ccd979bd
MF
774 int ret;
775
776 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
777 /* ugh. in prepare/commit_write, if from==to==start of block, we
778 ** skip the prepare. make sure we never send an offset for the start
779 ** of a block
780 */
781 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
782 offset++;
783 }
784 index = size >> PAGE_CACHE_SHIFT;
785
786 page = grab_cache_page(mapping, index);
787 if (!page) {
788 ret = -ENOMEM;
789 mlog_errno(ret);
790 goto out;
791 }
792
53013cba 793 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
ccd979bd
MF
794 if (ret < 0) {
795 mlog_errno(ret);
796 goto out_unlock;
797 }
798
799 if (ocfs2_should_order_data(inode)) {
800 handle = ocfs2_start_walk_page_trans(inode, page, offset,
801 offset);
802 if (IS_ERR(handle)) {
803 ret = PTR_ERR(handle);
804 handle = NULL;
805 goto out_unlock;
806 }
807 }
808
809 /* must not update i_size! */
810 ret = block_commit_write(page, offset, offset);
811 if (ret < 0)
812 mlog_errno(ret);
813 else
814 ret = 0;
815
816 if (handle)
02dc1af4 817 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
ccd979bd
MF
818out_unlock:
819 unlock_page(page);
820 page_cache_release(page);
821out:
822 return ret;
823}
824
825static int ocfs2_zero_extend(struct inode *inode,
826 u64 zero_to_size)
827{
828 int ret = 0;
829 u64 start_off;
830 struct super_block *sb = inode->i_sb;
831
832 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
833 while (start_off < zero_to_size) {
834 ret = ocfs2_write_zero_page(inode, start_off);
835 if (ret < 0) {
836 mlog_errno(ret);
837 goto out;
838 }
839
840 start_off += sb->s_blocksize;
e2057c5a
MF
841
842 /*
843 * Very large extends have the potential to lock up
844 * the cpu for extended periods of time.
845 */
846 cond_resched();
ccd979bd
MF
847 }
848
849out:
850 return ret;
851}
852
53013cba
MF
853/*
854 * A tail_to_skip value > 0 indicates that we're being called from
855 * ocfs2_file_aio_write(). This has the following implications:
856 *
857 * - we don't want to update i_size
858 * - di_bh will be NULL, which is fine because it's only used in the
859 * case where we want to update i_size.
860 * - ocfs2_zero_extend() will then only be filling the hole created
861 * between i_size and the start of the write.
862 */
ccd979bd
MF
863static int ocfs2_extend_file(struct inode *inode,
864 struct buffer_head *di_bh,
53013cba
MF
865 u64 new_i_size,
866 size_t tail_to_skip)
ccd979bd
MF
867{
868 int ret = 0;
3a0782d0 869 u32 clusters_to_add = 0;
ccd979bd 870
53013cba
MF
871 BUG_ON(!tail_to_skip && !di_bh);
872
ccd979bd
MF
873 /* setattr sometimes calls us like this. */
874 if (new_i_size == 0)
875 goto out;
876
877 if (i_size_read(inode) == new_i_size)
878 goto out;
879 BUG_ON(new_i_size < i_size_read(inode));
880
3a0782d0
MF
881 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
882 BUG_ON(tail_to_skip != 0);
883 goto out_update_size;
884 }
885
ccd979bd
MF
886 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
887 OCFS2_I(inode)->ip_clusters;
888
0effef77
MF
889 /*
890 * protect the pages that ocfs2_zero_extend is going to be
891 * pulling into the page cache.. we do this before the
892 * metadata extend so that we don't get into the situation
893 * where we've extended the metadata but can't get the data
894 * lock to zero.
895 */
896 ret = ocfs2_data_lock(inode, 1);
897 if (ret < 0) {
898 mlog_errno(ret);
899 goto out;
900 }
ccd979bd 901
0effef77 902 if (clusters_to_add) {
53013cba 903 ret = ocfs2_extend_allocation(inode, clusters_to_add);
ccd979bd
MF
904 if (ret < 0) {
905 mlog_errno(ret);
53013cba 906 goto out_unlock;
ccd979bd 907 }
0effef77 908 }
ccd979bd 909
0effef77
MF
910 /*
911 * Call this even if we don't add any clusters to the tree. We
912 * still need to zero the area between the old i_size and the
913 * new i_size.
914 */
915 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
916 if (ret < 0) {
917 mlog_errno(ret);
918 goto out_unlock;
53013cba
MF
919 }
920
3a0782d0 921out_update_size:
53013cba
MF
922 if (!tail_to_skip) {
923 /* We're being called from ocfs2_setattr() which wants
924 * us to update i_size */
925 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
926 if (ret < 0)
927 mlog_errno(ret);
ccd979bd
MF
928 }
929
53013cba 930out_unlock:
3a0782d0
MF
931 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
932 ocfs2_data_unlock(inode, 1);
53013cba 933
ccd979bd
MF
934out:
935 return ret;
936}
937
938int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
939{
940 int status = 0, size_change;
941 struct inode *inode = dentry->d_inode;
942 struct super_block *sb = inode->i_sb;
943 struct ocfs2_super *osb = OCFS2_SB(sb);
944 struct buffer_head *bh = NULL;
1fabe148 945 handle_t *handle = NULL;
ccd979bd
MF
946
947 mlog_entry("(0x%p, '%.*s')\n", dentry,
948 dentry->d_name.len, dentry->d_name.name);
949
950 if (attr->ia_valid & ATTR_MODE)
951 mlog(0, "mode change: %d\n", attr->ia_mode);
952 if (attr->ia_valid & ATTR_UID)
953 mlog(0, "uid change: %d\n", attr->ia_uid);
954 if (attr->ia_valid & ATTR_GID)
955 mlog(0, "gid change: %d\n", attr->ia_gid);
956 if (attr->ia_valid & ATTR_SIZE)
957 mlog(0, "size change...\n");
958 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
959 mlog(0, "time change...\n");
960
961#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
962 | ATTR_GID | ATTR_UID | ATTR_MODE)
963 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
964 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
965 return 0;
966 }
967
968 status = inode_change_ok(inode, attr);
969 if (status)
970 return status;
971
972 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
973 if (size_change) {
974 status = ocfs2_rw_lock(inode, 1);
975 if (status < 0) {
976 mlog_errno(status);
977 goto bail;
978 }
979 }
980
4bcec184 981 status = ocfs2_meta_lock(inode, &bh, 1);
ccd979bd
MF
982 if (status < 0) {
983 if (status != -ENOENT)
984 mlog_errno(status);
985 goto bail_unlock_rw;
986 }
987
988 if (size_change && attr->ia_size != i_size_read(inode)) {
989 if (i_size_read(inode) > attr->ia_size)
990 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
991 else
53013cba 992 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
ccd979bd
MF
993 if (status < 0) {
994 if (status != -ENOSPC)
995 mlog_errno(status);
996 status = -ENOSPC;
997 goto bail_unlock;
998 }
999 }
1000
65eff9cc 1001 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
1002 if (IS_ERR(handle)) {
1003 status = PTR_ERR(handle);
1004 mlog_errno(status);
1005 goto bail_unlock;
1006 }
1007
7307de80
MF
1008 /*
1009 * This will intentionally not wind up calling vmtruncate(),
1010 * since all the work for a size change has been done above.
1011 * Otherwise, we could get into problems with truncate as
1012 * ip_alloc_sem is used there to protect against i_size
1013 * changes.
1014 */
ccd979bd
MF
1015 status = inode_setattr(inode, attr);
1016 if (status < 0) {
1017 mlog_errno(status);
1018 goto bail_commit;
1019 }
1020
1021 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1022 if (status < 0)
1023 mlog_errno(status);
1024
1025bail_commit:
02dc1af4 1026 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
1027bail_unlock:
1028 ocfs2_meta_unlock(inode, 1);
1029bail_unlock_rw:
1030 if (size_change)
1031 ocfs2_rw_unlock(inode, 1);
1032bail:
1033 if (bh)
1034 brelse(bh);
1035
1036 mlog_exit(status);
1037 return status;
1038}
1039
1040int ocfs2_getattr(struct vfsmount *mnt,
1041 struct dentry *dentry,
1042 struct kstat *stat)
1043{
1044 struct inode *inode = dentry->d_inode;
1045 struct super_block *sb = dentry->d_inode->i_sb;
1046 struct ocfs2_super *osb = sb->s_fs_info;
1047 int err;
1048
1049 mlog_entry_void();
1050
1051 err = ocfs2_inode_revalidate(dentry);
1052 if (err) {
1053 if (err != -ENOENT)
1054 mlog_errno(err);
1055 goto bail;
1056 }
1057
1058 generic_fillattr(inode, stat);
1059
1060 /* We set the blksize from the cluster size for performance */
1061 stat->blksize = osb->s_clustersize;
1062
1063bail:
1064 mlog_exit(err);
1065
1066 return err;
1067}
1068
d38eb8db
TY
1069int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1070{
1071 int ret;
1072
1073 mlog_entry_void();
1074
1075 ret = ocfs2_meta_lock(inode, NULL, 0);
1076 if (ret) {
a9f5f707
MF
1077 if (ret != -ENOENT)
1078 mlog_errno(ret);
d38eb8db
TY
1079 goto out;
1080 }
1081
1082 ret = generic_permission(inode, mask, NULL);
d38eb8db
TY
1083
1084 ocfs2_meta_unlock(inode, 0);
1085out:
1086 mlog_exit(ret);
1087 return ret;
1088}
1089
ccd979bd
MF
1090static int ocfs2_write_remove_suid(struct inode *inode)
1091{
1092 int ret;
1093 struct buffer_head *bh = NULL;
1094 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1fabe148 1095 handle_t *handle;
ccd979bd
MF
1096 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1097 struct ocfs2_dinode *di;
1098
b0697053
MF
1099 mlog_entry("(Inode %llu, mode 0%o)\n",
1100 (unsigned long long)oi->ip_blkno, inode->i_mode);
ccd979bd 1101
65eff9cc 1102 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
1103 if (handle == NULL) {
1104 ret = -ENOMEM;
1105 mlog_errno(ret);
1106 goto out;
1107 }
1108
1109 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1110 if (ret < 0) {
1111 mlog_errno(ret);
1112 goto out_trans;
1113 }
1114
1115 ret = ocfs2_journal_access(handle, inode, bh,
1116 OCFS2_JOURNAL_ACCESS_WRITE);
1117 if (ret < 0) {
1118 mlog_errno(ret);
1119 goto out_bh;
1120 }
1121
1122 inode->i_mode &= ~S_ISUID;
1123 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1124 inode->i_mode &= ~S_ISGID;
1125
1126 di = (struct ocfs2_dinode *) bh->b_data;
1127 di->i_mode = cpu_to_le16(inode->i_mode);
1128
1129 ret = ocfs2_journal_dirty(handle, bh);
1130 if (ret < 0)
1131 mlog_errno(ret);
1132out_bh:
1133 brelse(bh);
1134out_trans:
02dc1af4 1135 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
1136out:
1137 mlog_exit(ret);
1138 return ret;
1139}
1140
9517bac6
MF
1141/*
1142 * Will look for holes and unwritten extents in the range starting at
1143 * pos for count bytes (inclusive).
1144 */
1145static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1146 size_t count)
1147{
1148 int ret = 0;
49cb8d2d 1149 unsigned int extent_flags;
9517bac6
MF
1150 u32 cpos, clusters, extent_len, phys_cpos;
1151 struct super_block *sb = inode->i_sb;
1152
1153 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1154 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1155
1156 while (clusters) {
49cb8d2d
MF
1157 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1158 &extent_flags);
9517bac6
MF
1159 if (ret < 0) {
1160 mlog_errno(ret);
1161 goto out;
1162 }
1163
49cb8d2d 1164 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
9517bac6
MF
1165 ret = 1;
1166 break;
1167 }
1168
1169 if (extent_len > clusters)
1170 extent_len = clusters;
1171
1172 clusters -= extent_len;
1173 cpos += extent_len;
1174 }
1175out:
1176 return ret;
1177}
1178
8659ac25
TY
1179static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1180 loff_t *ppos,
1181 size_t count,
9517bac6
MF
1182 int appending,
1183 int *direct_io)
ccd979bd 1184{
8659ac25
TY
1185 int ret = 0, meta_level = appending;
1186 struct inode *inode = dentry->d_inode;
ccd979bd 1187 u32 clusters;
ccd979bd 1188 loff_t newsize, saved_pos;
ccd979bd 1189
ccd979bd
MF
1190 /*
1191 * We sample i_size under a read level meta lock to see if our write
1192 * is extending the file, if it is we back off and get a write level
1193 * meta lock.
1194 */
ccd979bd 1195 for(;;) {
4bcec184 1196 ret = ocfs2_meta_lock(inode, NULL, meta_level);
ccd979bd
MF
1197 if (ret < 0) {
1198 meta_level = -1;
1199 mlog_errno(ret);
1200 goto out;
1201 }
1202
1203 /* Clear suid / sgid if necessary. We do this here
1204 * instead of later in the write path because
1205 * remove_suid() calls ->setattr without any hint that
1206 * we may have already done our cluster locking. Since
1207 * ocfs2_setattr() *must* take cluster locks to
1208 * proceeed, this will lead us to recursively lock the
1209 * inode. There's also the dinode i_size state which
1210 * can be lost via setattr during extending writes (we
1211 * set inode->i_size at the end of a write. */
8659ac25 1212 if (should_remove_suid(dentry)) {
ccd979bd
MF
1213 if (meta_level == 0) {
1214 ocfs2_meta_unlock(inode, meta_level);
1215 meta_level = 1;
1216 continue;
1217 }
1218
1219 ret = ocfs2_write_remove_suid(inode);
1220 if (ret < 0) {
1221 mlog_errno(ret);
8659ac25 1222 goto out_unlock;
ccd979bd
MF
1223 }
1224 }
1225
1226 /* work on a copy of ppos until we're sure that we won't have
1227 * to recalculate it due to relocking. */
8659ac25 1228 if (appending) {
ccd979bd
MF
1229 saved_pos = i_size_read(inode);
1230 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1231 } else {
8659ac25 1232 saved_pos = *ppos;
ccd979bd 1233 }
3a0782d0 1234
9517bac6
MF
1235 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1236 loff_t end = saved_pos + count;
1237
1238 /*
1239 * Skip the O_DIRECT checks if we don't need
1240 * them.
1241 */
1242 if (!direct_io || !(*direct_io))
1243 break;
1244
1245 /*
1246 * Allowing concurrent direct writes means
1247 * i_size changes wouldn't be synchronized, so
1248 * one node could wind up truncating another
1249 * nodes writes.
1250 */
1251 if (end > i_size_read(inode)) {
1252 *direct_io = 0;
1253 break;
1254 }
1255
1256 /*
1257 * We don't fill holes during direct io, so
1258 * check for them here. If any are found, the
1259 * caller will have to retake some cluster
1260 * locks and initiate the io as buffered.
1261 */
1262 ret = ocfs2_check_range_for_holes(inode, saved_pos,
1263 count);
1264 if (ret == 1) {
1265 *direct_io = 0;
1266 ret = 0;
1267 } else if (ret < 0)
1268 mlog_errno(ret);
1269 break;
1270 }
1271
3a0782d0
MF
1272 /*
1273 * The rest of this loop is concerned with legacy file
1274 * systems which don't support sparse files.
1275 */
3a0782d0 1276
8659ac25 1277 newsize = count + saved_pos;
ccd979bd 1278
215c7f9f
MF
1279 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1280 (long long) saved_pos, (long long) newsize,
1281 (long long) i_size_read(inode));
ccd979bd
MF
1282
1283 /* No need for a higher level metadata lock if we're
1284 * never going past i_size. */
1285 if (newsize <= i_size_read(inode))
1286 break;
1287
1288 if (meta_level == 0) {
1289 ocfs2_meta_unlock(inode, meta_level);
1290 meta_level = 1;
1291 continue;
1292 }
1293
1294 spin_lock(&OCFS2_I(inode)->ip_lock);
1295 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1296 OCFS2_I(inode)->ip_clusters;
1297 spin_unlock(&OCFS2_I(inode)->ip_lock);
1298
1299 mlog(0, "Writing at EOF, may need more allocation: "
215c7f9f
MF
1300 "i_size = %lld, newsize = %lld, need %u clusters\n",
1301 (long long) i_size_read(inode), (long long) newsize,
1302 clusters);
ccd979bd
MF
1303
1304 /* We only want to continue the rest of this loop if
1305 * our extend will actually require more
1306 * allocation. */
1307 if (!clusters)
1308 break;
1309
8659ac25 1310 ret = ocfs2_extend_file(inode, NULL, newsize, count);
ccd979bd
MF
1311 if (ret < 0) {
1312 if (ret != -ENOSPC)
1313 mlog_errno(ret);
8659ac25 1314 goto out_unlock;
ccd979bd 1315 }
ccd979bd
MF
1316 break;
1317 }
1318
8659ac25
TY
1319 if (appending)
1320 *ppos = saved_pos;
1321
1322out_unlock:
ccd979bd 1323 ocfs2_meta_unlock(inode, meta_level);
8659ac25
TY
1324
1325out:
1326 return ret;
1327}
1328
9517bac6
MF
1329static inline void
1330ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1331{
1332 const struct iovec *iov = *iovp;
1333 size_t base = *basep;
1334
1335 do {
1336 int copy = min(bytes, iov->iov_len - base);
1337
1338 bytes -= copy;
1339 base += copy;
1340 if (iov->iov_len == base) {
1341 iov++;
1342 base = 0;
1343 }
1344 } while (bytes);
1345 *iovp = iov;
1346 *basep = base;
1347}
1348
3a307ffc 1349static struct page * ocfs2_get_write_source(char **ret_src_buf,
9517bac6
MF
1350 const struct iovec *cur_iov,
1351 size_t iov_offset)
1352{
1353 int ret;
3a307ffc 1354 char *buf = cur_iov->iov_base + iov_offset;
9517bac6 1355 struct page *src_page = NULL;
3a307ffc 1356 unsigned long off;
9517bac6 1357
3a307ffc 1358 off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
9517bac6
MF
1359
1360 if (!segment_eq(get_fs(), KERNEL_DS)) {
1361 /*
1362 * Pull in the user page. We want to do this outside
1363 * of the meta data locks in order to preserve locking
1364 * order in case of page fault.
1365 */
1366 ret = get_user_pages(current, current->mm,
1367 (unsigned long)buf & PAGE_CACHE_MASK, 1,
1368 0, 0, &src_page, NULL);
1369 if (ret == 1)
3a307ffc 1370 *ret_src_buf = kmap(src_page) + off;
9517bac6
MF
1371 else
1372 src_page = ERR_PTR(-EFAULT);
1373 } else {
3a307ffc 1374 *ret_src_buf = buf;
9517bac6
MF
1375 }
1376
1377 return src_page;
1378}
1379
3a307ffc 1380static void ocfs2_put_write_source(struct page *page)
9517bac6
MF
1381{
1382 if (page) {
1383 kunmap(page);
1384 page_cache_release(page);
1385 }
1386}
1387
1388static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1389 const struct iovec *iov,
1390 unsigned long nr_segs,
1391 size_t count,
1392 ssize_t o_direct_written)
1393{
1394 int ret = 0;
1395 ssize_t copied, total = 0;
3a307ffc
MF
1396 size_t iov_offset = 0, bytes;
1397 loff_t pos;
9517bac6 1398 const struct iovec *cur_iov = iov;
3a307ffc
MF
1399 struct page *user_page, *page;
1400 char *buf, *dst;
1401 void *fsdata;
9517bac6
MF
1402
1403 /*
1404 * handle partial DIO write. Adjust cur_iov if needed.
1405 */
1406 ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1407
1408 do {
3a307ffc 1409 pos = *ppos;
9517bac6 1410
3a307ffc
MF
1411 user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
1412 if (IS_ERR(user_page)) {
1413 ret = PTR_ERR(user_page);
9517bac6
MF
1414 goto out;
1415 }
1416
3a307ffc
MF
1417 /* Stay within our page boundaries */
1418 bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
1419 (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
1420 /* Stay within the vector boundary */
1421 bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
1422 /* Stay within count */
1423 bytes = min(bytes, count);
1424
1425 page = NULL;
1426 ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
1427 &page, &fsdata);
1428 if (ret) {
1429 mlog_errno(ret);
1430 goto out;
1431 }
9517bac6 1432
3a307ffc
MF
1433 dst = kmap_atomic(page, KM_USER0);
1434 memcpy(dst + (pos & (PAGE_CACHE_SIZE - 1)), buf, bytes);
1435 kunmap_atomic(dst, KM_USER0);
1436 flush_dcache_page(page);
1437 ocfs2_put_write_source(user_page);
9517bac6 1438
3a307ffc
MF
1439 copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
1440 bytes, page, fsdata);
9517bac6
MF
1441 if (copied < 0) {
1442 mlog_errno(copied);
1443 ret = copied;
1444 goto out;
1445 }
1446
1447 total += copied;
3a307ffc 1448 *ppos = pos + copied;
9517bac6
MF
1449 count -= copied;
1450
1451 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1452 } while(count);
1453
1454out:
1455 return total ? total : ret;
1456}
1457
8659ac25
TY
1458static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1459 const struct iovec *iov,
1460 unsigned long nr_segs,
1461 loff_t pos)
1462{
9517bac6
MF
1463 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1464 int can_do_direct, sync = 0;
1465 ssize_t written = 0;
1466 size_t ocount; /* original count */
1467 size_t count; /* after file limit checks */
1468 loff_t *ppos = &iocb->ki_pos;
1469 struct file *file = iocb->ki_filp;
1470 struct inode *inode = file->f_path.dentry->d_inode;
1471
1472 mlog_entry("(0x%p, %u, '%.*s')\n", file,
8659ac25 1473 (unsigned int)nr_segs,
9517bac6
MF
1474 file->f_path.dentry->d_name.len,
1475 file->f_path.dentry->d_name.name);
8659ac25 1476
8659ac25
TY
1477 if (iocb->ki_left == 0)
1478 return 0;
1479
d9b08b9e 1480 ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
9517bac6
MF
1481 if (ret)
1482 return ret;
1483
1484 count = ocount;
1485
1486 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1487
1488 appending = file->f_flags & O_APPEND ? 1 : 0;
1489 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1490
8659ac25 1491 mutex_lock(&inode->i_mutex);
9517bac6
MF
1492
1493relock:
8659ac25 1494 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
9517bac6 1495 if (direct_io) {
8659ac25 1496 down_read(&inode->i_alloc_sem);
9517bac6 1497 have_alloc_sem = 1;
8659ac25
TY
1498 }
1499
1500 /* concurrent O_DIRECT writes are allowed */
9517bac6 1501 rw_level = !direct_io;
8659ac25
TY
1502 ret = ocfs2_rw_lock(inode, rw_level);
1503 if (ret < 0) {
8659ac25 1504 mlog_errno(ret);
9517bac6 1505 goto out_sems;
8659ac25
TY
1506 }
1507
9517bac6
MF
1508 can_do_direct = direct_io;
1509 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1510 iocb->ki_left, appending,
1511 &can_do_direct);
8659ac25
TY
1512 if (ret < 0) {
1513 mlog_errno(ret);
1514 goto out;
1515 }
ccd979bd 1516
9517bac6
MF
1517 /*
1518 * We can't complete the direct I/O as requested, fall back to
1519 * buffered I/O.
1520 */
1521 if (direct_io && !can_do_direct) {
1522 ocfs2_rw_unlock(inode, rw_level);
1523 up_read(&inode->i_alloc_sem);
1524
1525 have_alloc_sem = 0;
1526 rw_level = -1;
1527
1528 direct_io = 0;
1529 sync = 1;
1530 goto relock;
1531 }
1532
1533 if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
1534 sync = 1;
1535
1536 /*
1537 * XXX: Is it ok to execute these checks a second time?
1538 */
1539 ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
1540 if (ret)
1541 goto out;
1542
1543 /*
1544 * Set pos so that sync_page_range_nolock() below understands
1545 * where to start from. We might've moved it around via the
1546 * calls above. The range we want to actually sync starts from
1547 * *ppos here.
1548 *
1549 */
1550 pos = *ppos;
1551
ccd979bd 1552 /* communicate with ocfs2_dio_end_io */
7cdfc3a1 1553 ocfs2_iocb_set_rw_locked(iocb, rw_level);
ccd979bd 1554
9517bac6
MF
1555 if (direct_io) {
1556 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1557 ppos, count, ocount);
1558 if (written < 0) {
1559 ret = written;
1560 goto out_dio;
1561 }
1562 } else {
1563 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
1564 count, written);
1565 if (written < 0) {
1566 ret = written;
1567 if (ret != -EFAULT || ret != -ENOSPC)
1568 mlog_errno(ret);
1569 goto out;
1570 }
1571 }
ccd979bd 1572
9517bac6 1573out_dio:
ccd979bd 1574 /* buffered aio wouldn't have proper lock coverage today */
9517bac6 1575 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
ccd979bd
MF
1576
1577 /*
1578 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1579 * function pointer which is called when o_direct io completes so that
1580 * it can unlock our rw lock. (it's the clustered equivalent of
1581 * i_alloc_sem; protects truncate from racing with pending ios).
1582 * Unfortunately there are error cases which call end_io and others
1583 * that don't. so we don't have to unlock the rw_lock if either an
1584 * async dio is going to do it in the future or an end_io after an
1585 * error has already done it.
1586 */
1587 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1588 rw_level = -1;
1589 have_alloc_sem = 0;
1590 }
1591
1592out:
9517bac6
MF
1593 if (rw_level != -1)
1594 ocfs2_rw_unlock(inode, rw_level);
1595
1596out_sems:
ccd979bd
MF
1597 if (have_alloc_sem)
1598 up_read(&inode->i_alloc_sem);
9517bac6
MF
1599
1600 if (written > 0 && sync) {
1601 ssize_t err;
1602
1603 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
1604 if (err < 0)
1605 written = err;
1606 }
1607
1b1dcc1b 1608 mutex_unlock(&inode->i_mutex);
ccd979bd
MF
1609
1610 mlog_exit(ret);
9517bac6 1611 return written ? written : ret;
ccd979bd
MF
1612}
1613
6af67d82
MF
1614static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
1615 struct pipe_buffer *buf,
1616 struct splice_desc *sd)
1617{
3a307ffc 1618 int ret, count;
6af67d82 1619 ssize_t copied = 0;
3a307ffc
MF
1620 struct file *file = sd->u.file;
1621 unsigned int offset;
1622 struct page *page = NULL;
1623 void *fsdata;
1624 char *src, *dst;
6af67d82 1625
cac36bb0 1626 ret = buf->ops->confirm(pipe, buf);
6af67d82
MF
1627 if (ret)
1628 goto out;
1629
3a307ffc 1630 offset = sd->pos & ~PAGE_CACHE_MASK;
6af67d82 1631 count = sd->len;
3a307ffc
MF
1632 if (count + offset > PAGE_CACHE_SIZE)
1633 count = PAGE_CACHE_SIZE - offset;
6af67d82 1634
3a307ffc
MF
1635 ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
1636 &page, &fsdata);
1637 if (ret) {
1638 mlog_errno(ret);
1639 goto out;
1640 }
6af67d82 1641
3a307ffc
MF
1642 src = buf->ops->map(pipe, buf, 1);
1643 dst = kmap_atomic(page, KM_USER1);
1644 memcpy(dst + offset, src + buf->offset, count);
1645 kunmap_atomic(page, KM_USER1);
1646 buf->ops->unmap(pipe, buf, src);
6af67d82 1647
3a307ffc
MF
1648 copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
1649 page, fsdata);
1650 if (copied < 0) {
1651 mlog_errno(copied);
1652 ret = copied;
1653 goto out;
1654 }
6af67d82
MF
1655out:
1656
3a307ffc 1657 return copied ? copied : ret;
6af67d82
MF
1658}
1659
1660static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1661 struct file *out,
1662 loff_t *ppos,
1663 size_t len,
1664 unsigned int flags)
1665{
1666 int ret, err;
1667 struct address_space *mapping = out->f_mapping;
1668 struct inode *inode = mapping->host;
c66ab6fa
JA
1669 struct splice_desc sd = {
1670 .total_len = len,
1671 .flags = flags,
1672 .pos = *ppos,
6a14b90b 1673 .u.file = out,
c66ab6fa
JA
1674 };
1675
1676 ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
6af67d82
MF
1677 if (ret > 0) {
1678 *ppos += ret;
1679
1680 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
1681 err = generic_osync_inode(inode, mapping,
1682 OSYNC_METADATA|OSYNC_DATA);
1683 if (err)
1684 ret = err;
1685 }
1686 }
1687
1688 return ret;
1689}
1690
8659ac25
TY
1691static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1692 struct file *out,
1693 loff_t *ppos,
1694 size_t len,
1695 unsigned int flags)
1696{
1697 int ret;
d28c9174 1698 struct inode *inode = out->f_path.dentry->d_inode;
8659ac25
TY
1699
1700 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1701 (unsigned int)len,
d28c9174
JS
1702 out->f_path.dentry->d_name.len,
1703 out->f_path.dentry->d_name.name);
8659ac25
TY
1704
1705 inode_double_lock(inode, pipe->inode);
1706
1707 ret = ocfs2_rw_lock(inode, 1);
1708 if (ret < 0) {
1709 mlog_errno(ret);
1710 goto out;
1711 }
1712
9517bac6
MF
1713 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
1714 NULL);
8659ac25
TY
1715 if (ret < 0) {
1716 mlog_errno(ret);
1717 goto out_unlock;
1718 }
1719
1720 /* ok, we're done with i_size and alloc work */
6af67d82 1721 ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
8659ac25
TY
1722
1723out_unlock:
1724 ocfs2_rw_unlock(inode, 1);
1725out:
1726 inode_double_unlock(inode, pipe->inode);
1727
1728 mlog_exit(ret);
1729 return ret;
1730}
1731
1732static ssize_t ocfs2_file_splice_read(struct file *in,
1733 loff_t *ppos,
1734 struct pipe_inode_info *pipe,
1735 size_t len,
1736 unsigned int flags)
1737{
1738 int ret = 0;
d28c9174 1739 struct inode *inode = in->f_path.dentry->d_inode;
8659ac25
TY
1740
1741 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1742 (unsigned int)len,
d28c9174
JS
1743 in->f_path.dentry->d_name.len,
1744 in->f_path.dentry->d_name.name);
8659ac25
TY
1745
1746 /*
1747 * See the comment in ocfs2_file_aio_read()
1748 */
1749 ret = ocfs2_meta_lock(inode, NULL, 0);
1750 if (ret < 0) {
1751 mlog_errno(ret);
1752 goto bail;
1753 }
1754 ocfs2_meta_unlock(inode, 0);
1755
1756 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1757
1758bail:
1759 mlog_exit(ret);
1760 return ret;
1761}
1762
ccd979bd 1763static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
027445c3
BP
1764 const struct iovec *iov,
1765 unsigned long nr_segs,
ccd979bd
MF
1766 loff_t pos)
1767{
25899dee 1768 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
ccd979bd 1769 struct file *filp = iocb->ki_filp;
d28c9174 1770 struct inode *inode = filp->f_path.dentry->d_inode;
ccd979bd 1771
027445c3
BP
1772 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1773 (unsigned int)nr_segs,
d28c9174
JS
1774 filp->f_path.dentry->d_name.len,
1775 filp->f_path.dentry->d_name.name);
ccd979bd
MF
1776
1777 if (!inode) {
1778 ret = -EINVAL;
1779 mlog_errno(ret);
1780 goto bail;
1781 }
1782
ccd979bd
MF
1783 /*
1784 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1785 * need locks to protect pending reads from racing with truncate.
1786 */
1787 if (filp->f_flags & O_DIRECT) {
1788 down_read(&inode->i_alloc_sem);
1789 have_alloc_sem = 1;
1790
1791 ret = ocfs2_rw_lock(inode, 0);
1792 if (ret < 0) {
1793 mlog_errno(ret);
1794 goto bail;
1795 }
1796 rw_level = 0;
1797 /* communicate with ocfs2_dio_end_io */
7cdfc3a1 1798 ocfs2_iocb_set_rw_locked(iocb, rw_level);
ccd979bd
MF
1799 }
1800
c4374f8a
MF
1801 /*
1802 * We're fine letting folks race truncates and extending
1803 * writes with read across the cluster, just like they can
1804 * locally. Hence no rw_lock during read.
1805 *
1806 * Take and drop the meta data lock to update inode fields
1807 * like i_size. This allows the checks down below
1808 * generic_file_aio_read() a chance of actually working.
1809 */
25899dee 1810 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
c4374f8a
MF
1811 if (ret < 0) {
1812 mlog_errno(ret);
1813 goto bail;
1814 }
25899dee 1815 ocfs2_meta_unlock(inode, lock_level);
c4374f8a 1816
027445c3 1817 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
ccd979bd
MF
1818 if (ret == -EINVAL)
1819 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1820
1821 /* buffered aio wouldn't have proper lock coverage today */
1822 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1823
1824 /* see ocfs2_file_aio_write */
1825 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1826 rw_level = -1;
1827 have_alloc_sem = 0;
1828 }
1829
1830bail:
1831 if (have_alloc_sem)
1832 up_read(&inode->i_alloc_sem);
1833 if (rw_level != -1)
1834 ocfs2_rw_unlock(inode, rw_level);
1835 mlog_exit(ret);
1836
1837 return ret;
1838}
1839
92e1d5be 1840const struct inode_operations ocfs2_file_iops = {
ccd979bd
MF
1841 .setattr = ocfs2_setattr,
1842 .getattr = ocfs2_getattr,
d38eb8db 1843 .permission = ocfs2_permission,
ccd979bd
MF
1844};
1845
92e1d5be 1846const struct inode_operations ocfs2_special_file_iops = {
ccd979bd
MF
1847 .setattr = ocfs2_setattr,
1848 .getattr = ocfs2_getattr,
d38eb8db 1849 .permission = ocfs2_permission,
ccd979bd
MF
1850};
1851
4b6f5d20 1852const struct file_operations ocfs2_fops = {
ccd979bd
MF
1853 .read = do_sync_read,
1854 .write = do_sync_write,
ccd979bd
MF
1855 .mmap = ocfs2_mmap,
1856 .fsync = ocfs2_sync_file,
1857 .release = ocfs2_file_release,
1858 .open = ocfs2_file_open,
1859 .aio_read = ocfs2_file_aio_read,
1860 .aio_write = ocfs2_file_aio_write,
ca4d147e 1861 .ioctl = ocfs2_ioctl,
586d232b
MF
1862#ifdef CONFIG_COMPAT
1863 .compat_ioctl = ocfs2_compat_ioctl,
1864#endif
8659ac25
TY
1865 .splice_read = ocfs2_file_splice_read,
1866 .splice_write = ocfs2_file_splice_write,
ccd979bd
MF
1867};
1868
4b6f5d20 1869const struct file_operations ocfs2_dops = {
ccd979bd
MF
1870 .read = generic_read_dir,
1871 .readdir = ocfs2_readdir,
1872 .fsync = ocfs2_sync_file,
ca4d147e 1873 .ioctl = ocfs2_ioctl,
586d232b
MF
1874#ifdef CONFIG_COMPAT
1875 .compat_ioctl = ocfs2_compat_ioctl,
1876#endif
ccd979bd 1877};
This page took 0.306385 seconds and 5 git commands to generate.