blob: a80f31776d94beacc2383b53fe04dd7b1808077b [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080026#include <linux/capability.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080027#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>
Mark Fashehe2057c52006-10-03 17:53:05 -070033#include <linux/sched.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020034#include <linux/splice.h>
Tiger Yang7f1a37e2006-11-15 15:48:42 +080035#include <linux/mount.h>
Mark Fasheh9517bac2007-02-09 20:24:12 -080036#include <linux/writeback.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080037
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"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070051#include "ioctl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#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,
Josef Sipekd28c9172006-12-08 02:37:25 -080072 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -080073
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,
Josef Sipekd28c9172006-12-08 02:37:25 -0800102 file->f_path.dentry->d_name.len,
103 file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800104
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
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800140int 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
Mark Fasheh6c2aad02006-12-19 15:25:52 -0800153 /*
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
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800164 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
165 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
166 return 0;
167
Mark Fasheh7e913c52006-12-13 00:34:35 -0800168 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
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800176 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
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700210static int ocfs2_set_inode_size(handle_t *handle,
211 struct inode *inode,
212 struct buffer_head *fe_bh,
213 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -0800214{
215 int status;
216
217 mlog_entry_void();
218 i_size_write(inode, new_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700219 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800220 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);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700239 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800240
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700241 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800242 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
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700253 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800254out:
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;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700264 handle_t *handle;
Mark Fasheh60b11392007-02-16 11:46:50 -0800265 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -0800266
267 mlog_entry_void();
268
269 /* TODO: This needs to actually orphan the inode in this
270 * transaction. */
271
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800273 if (IS_ERR(handle)) {
274 status = PTR_ERR(handle);
275 mlog_errno(status);
276 goto out;
277 }
278
Mark Fasheh60b11392007-02-16 11:46:50 -0800279 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800305 if (status < 0)
306 mlog_errno(status);
307
Mark Fasheh60b11392007-02-16 11:46:50 -0800308out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700309 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800310out:
Mark Fasheh60b11392007-02-16 11:46:50 -0800311
Mark Fashehccd979b2005-12-15 14:31:24 -0800312 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
Mark Fashehb0697052006-03-03 10:24:33 -0800325 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800328
Mark Fashehccd979b2005-12-15 14:31:24 -0800329 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),
Mark Fashehb0697052006-03-03 10:24:33 -0800337 "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,
Mark Fashehccd979b2005-12-15 14:31:24 -0800340 i_size_read(inode),
Mark Fashehb0697052006-03-03 10:24:33 -0800341 (unsigned long long)le64_to_cpu(fe->i_size),
342 le32_to_cpu(fe->i_flags));
Mark Fashehccd979b2005-12-15 14:31:24 -0800343
344 if (new_i_size > le64_to_cpu(fe->i_size)) {
Mark Fashehb0697052006-03-03 10:24:33 -0800345 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 status = -EINVAL;
349 mlog_errno(status);
350 goto bail;
351 }
352
Mark Fashehb0697052006-03-03 10:24:33 -0800353 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800357
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
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700363 down_write(&OCFS2_I(inode)->ip_alloc_sem);
364
Mark Fashehab0920c2006-03-16 15:06:37 -0800365 /* 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) {
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700371 up_write(&OCFS2_I(inode)->ip_alloc_sem);
372
Mark Fashehab0920c2006-03-16 15:06:37 -0800373 mlog_errno(status);
374 goto bail;
375 }
Mark Fashehab0920c2006-03-16 15:06:37 -0800376
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700377 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 /* 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);
Mark Fasheh60b11392007-02-16 11:46:50 -0800387 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800388 }
389
390 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
391 if (status < 0) {
392 mlog_errno(status);
Mark Fasheh60b11392007-02-16 11:46:50 -0800393 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800394 }
395
396 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
397 if (status < 0) {
398 mlog_errno(status);
Mark Fasheh60b11392007-02-16 11:46:50 -0800399 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 }
401
402 /* TODO: orphan dir cleanup here. */
Mark Fasheh60b11392007-02-16 11:46:50 -0800403bail_unlock_data:
404 ocfs2_data_unlock(inode, 1);
405
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700406 up_write(&OCFS2_I(inode)->ip_alloc_sem);
407
Mark Fashehccd979b2005-12-15 14:31:24 -0800408bail:
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,
Mark Fashehdcd05382007-01-16 11:32:23 -0800426 u32 *logical_offset,
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 u32 clusters_to_add,
428 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700429 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800430 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);
Mark Fashehb0697052006-03-03 10:24:33 -0800488 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);
Mark Fashehdcd05382007-01-16 11:32:23 -0800490 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
491 *logical_offset, block, num_bits,
492 meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800493 if (status < 0) {
494 mlog_errno(status);
495 goto leave;
496 }
497
Mark Fashehccd979b2005-12-15 14:31:24 -0800498 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;
Mark Fashehdcd05382007-01-16 11:32:23 -0800505 *logical_offset += num_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800506
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
Mark Fashehabf8b152007-01-17 13:07:24 -0800521/*
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
Mark Fasheh9517bac2007-02-09 20:24:12 -0800526 * support holes, and from ocfs2_write() for file systems which
527 * understand sparse inodes.
Mark Fashehabf8b152007-01-17 13:07:24 -0800528 */
Mark Fasheh9517bac2007-02-09 20:24:12 -0800529int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
530 u32 clusters_to_add,
531 struct ocfs2_alloc_context **data_ac,
532 struct ocfs2_alloc_context **meta_ac)
Mark Fashehabf8b152007-01-17 13:07:24 -0800533{
534 int ret, num_free_extents;
535 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
536
537 *meta_ac = NULL;
538 *data_ac = NULL;
539
540 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
541 "clusters_to_add = %u\n",
542 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
543 le32_to_cpu(di->i_clusters), clusters_to_add);
544
545 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
546 if (num_free_extents < 0) {
547 ret = num_free_extents;
548 mlog_errno(ret);
549 goto out;
550 }
551
552 /*
553 * Sparse allocation file systems need to be more conservative
554 * with reserving room for expansion - the actual allocation
555 * happens while we've got a journal handle open so re-taking
556 * a cluster lock (because we ran out of room for another
557 * extent) will violate ordering rules.
558 *
Mark Fasheh9517bac2007-02-09 20:24:12 -0800559 * Most of the time we'll only be seeing this 1 cluster at a time
Mark Fashehabf8b152007-01-17 13:07:24 -0800560 * anyway.
561 */
562 if (!num_free_extents ||
563 (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
564 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
565 if (ret < 0) {
566 if (ret != -ENOSPC)
567 mlog_errno(ret);
568 goto out;
569 }
570 }
571
572 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
573 if (ret < 0) {
574 if (ret != -ENOSPC)
575 mlog_errno(ret);
576 goto out;
577 }
578
579out:
580 if (ret) {
581 if (*meta_ac) {
582 ocfs2_free_alloc_context(*meta_ac);
583 *meta_ac = NULL;
584 }
585
586 /*
587 * We cannot have an error and a non null *data_ac.
588 */
589 }
590
591 return ret;
592}
593
Mark Fashehccd979b2005-12-15 14:31:24 -0800594static int ocfs2_extend_allocation(struct inode *inode,
595 u32 clusters_to_add)
596{
597 int status = 0;
598 int restart_func = 0;
599 int drop_alloc_sem = 0;
Mark Fashehabf8b152007-01-17 13:07:24 -0800600 int credits;
Mark Fashehdcd05382007-01-16 11:32:23 -0800601 u32 prev_clusters, logical_start;
Mark Fashehccd979b2005-12-15 14:31:24 -0800602 struct buffer_head *bh = NULL;
603 struct ocfs2_dinode *fe = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700604 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800605 struct ocfs2_alloc_context *data_ac = NULL;
606 struct ocfs2_alloc_context *meta_ac = NULL;
607 enum ocfs2_alloc_restarted why;
608 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
609
610 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
611
Mark Fashehdcd05382007-01-16 11:32:23 -0800612 /*
613 * This function only exists for file systems which don't
614 * support holes.
615 */
616 BUG_ON(ocfs2_sparse_alloc(osb));
617
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
619 OCFS2_BH_CACHED, inode);
620 if (status < 0) {
621 mlog_errno(status);
622 goto leave;
623 }
624
625 fe = (struct ocfs2_dinode *) bh->b_data;
626 if (!OCFS2_IS_VALID_DINODE(fe)) {
627 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
628 status = -EIO;
629 goto leave;
630 }
631
Mark Fashehdcd05382007-01-16 11:32:23 -0800632 logical_start = OCFS2_I(inode)->ip_clusters;
633
Mark Fashehccd979b2005-12-15 14:31:24 -0800634restart_all:
635 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
636
Mark Fashehccd979b2005-12-15 14:31:24 -0800637 /* blocks peope in read/write from reading our allocation
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800638 * until we're done changing it. We depend on i_mutex to block
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 * other extend/truncate calls while we're here. Ordering wrt
640 * start_trans is important here -- always do it before! */
641 down_write(&OCFS2_I(inode)->ip_alloc_sem);
642 drop_alloc_sem = 1;
643
Mark Fasheh9517bac2007-02-09 20:24:12 -0800644 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
645 &meta_ac);
646 if (status) {
647 mlog_errno(status);
648 goto leave;
649 }
650
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700652 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 if (IS_ERR(handle)) {
654 status = PTR_ERR(handle);
655 handle = NULL;
656 mlog_errno(status);
657 goto leave;
658 }
659
660restarted_transaction:
661 /* reserve a write to the file entry early on - that we if we
662 * run out of credits in the allocation path, we can still
663 * update i_size. */
664 status = ocfs2_journal_access(handle, inode, bh,
665 OCFS2_JOURNAL_ACCESS_WRITE);
666 if (status < 0) {
667 mlog_errno(status);
668 goto leave;
669 }
670
671 prev_clusters = OCFS2_I(inode)->ip_clusters;
672
673 status = ocfs2_do_extend_allocation(osb,
674 inode,
Mark Fashehdcd05382007-01-16 11:32:23 -0800675 &logical_start,
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 clusters_to_add,
677 bh,
678 handle,
679 data_ac,
680 meta_ac,
681 &why);
682 if ((status < 0) && (status != -EAGAIN)) {
683 if (status != -ENOSPC)
684 mlog_errno(status);
685 goto leave;
686 }
687
688 status = ocfs2_journal_dirty(handle, bh);
689 if (status < 0) {
690 mlog_errno(status);
691 goto leave;
692 }
693
694 spin_lock(&OCFS2_I(inode)->ip_lock);
695 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
696 spin_unlock(&OCFS2_I(inode)->ip_lock);
697
698 if (why != RESTART_NONE && clusters_to_add) {
699 if (why == RESTART_META) {
700 mlog(0, "restarting function.\n");
701 restart_func = 1;
702 } else {
703 BUG_ON(why != RESTART_TRANS);
704
705 mlog(0, "restarting transaction.\n");
706 /* TODO: This can be more intelligent. */
707 credits = ocfs2_calc_extend_credits(osb->sb,
708 fe,
709 clusters_to_add);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700710 status = ocfs2_extend_trans(handle, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800711 if (status < 0) {
712 /* handle still has to be committed at
713 * this point. */
714 status = -ENOMEM;
715 mlog_errno(status);
716 goto leave;
717 }
718 goto restarted_transaction;
719 }
720 }
721
Mark Fashehb0697052006-03-03 10:24:33 -0800722 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700723 le32_to_cpu(fe->i_clusters),
724 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -0800725 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
726 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
727
728leave:
729 if (drop_alloc_sem) {
730 up_write(&OCFS2_I(inode)->ip_alloc_sem);
731 drop_alloc_sem = 0;
732 }
733 if (handle) {
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700734 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 handle = NULL;
736 }
737 if (data_ac) {
738 ocfs2_free_alloc_context(data_ac);
739 data_ac = NULL;
740 }
741 if (meta_ac) {
742 ocfs2_free_alloc_context(meta_ac);
743 meta_ac = NULL;
744 }
745 if ((!status) && restart_func) {
746 restart_func = 0;
747 goto restart_all;
748 }
749 if (bh) {
750 brelse(bh);
751 bh = NULL;
752 }
753
754 mlog_exit(status);
755 return status;
756}
757
758/* Some parts of this taken from generic_cont_expand, which turned out
759 * to be too fragile to do exactly what we need without us having to
Mark Fasheh53013cb2006-05-05 19:04:03 -0700760 * worry about recursive locking in ->prepare_write() and
761 * ->commit_write(). */
Mark Fashehccd979b2005-12-15 14:31:24 -0800762static int ocfs2_write_zero_page(struct inode *inode,
763 u64 size)
764{
765 struct address_space *mapping = inode->i_mapping;
766 struct page *page;
767 unsigned long index;
768 unsigned int offset;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700769 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800770 int ret;
771
772 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
773 /* ugh. in prepare/commit_write, if from==to==start of block, we
774 ** skip the prepare. make sure we never send an offset for the start
775 ** of a block
776 */
777 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
778 offset++;
779 }
780 index = size >> PAGE_CACHE_SHIFT;
781
782 page = grab_cache_page(mapping, index);
783 if (!page) {
784 ret = -ENOMEM;
785 mlog_errno(ret);
786 goto out;
787 }
788
Mark Fasheh53013cb2006-05-05 19:04:03 -0700789 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800790 if (ret < 0) {
791 mlog_errno(ret);
792 goto out_unlock;
793 }
794
795 if (ocfs2_should_order_data(inode)) {
796 handle = ocfs2_start_walk_page_trans(inode, page, offset,
797 offset);
798 if (IS_ERR(handle)) {
799 ret = PTR_ERR(handle);
800 handle = NULL;
801 goto out_unlock;
802 }
803 }
804
805 /* must not update i_size! */
806 ret = block_commit_write(page, offset, offset);
807 if (ret < 0)
808 mlog_errno(ret);
809 else
810 ret = 0;
811
812 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700813 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800814out_unlock:
815 unlock_page(page);
816 page_cache_release(page);
817out:
818 return ret;
819}
820
821static int ocfs2_zero_extend(struct inode *inode,
822 u64 zero_to_size)
823{
824 int ret = 0;
825 u64 start_off;
826 struct super_block *sb = inode->i_sb;
827
828 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
829 while (start_off < zero_to_size) {
830 ret = ocfs2_write_zero_page(inode, start_off);
831 if (ret < 0) {
832 mlog_errno(ret);
833 goto out;
834 }
835
836 start_off += sb->s_blocksize;
Mark Fashehe2057c52006-10-03 17:53:05 -0700837
838 /*
839 * Very large extends have the potential to lock up
840 * the cpu for extended periods of time.
841 */
842 cond_resched();
Mark Fashehccd979b2005-12-15 14:31:24 -0800843 }
844
845out:
846 return ret;
847}
848
Mark Fasheh53013cb2006-05-05 19:04:03 -0700849/*
850 * A tail_to_skip value > 0 indicates that we're being called from
851 * ocfs2_file_aio_write(). This has the following implications:
852 *
853 * - we don't want to update i_size
854 * - di_bh will be NULL, which is fine because it's only used in the
855 * case where we want to update i_size.
856 * - ocfs2_zero_extend() will then only be filling the hole created
857 * between i_size and the start of the write.
858 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800859static int ocfs2_extend_file(struct inode *inode,
860 struct buffer_head *di_bh,
Mark Fasheh53013cb2006-05-05 19:04:03 -0700861 u64 new_i_size,
862 size_t tail_to_skip)
Mark Fashehccd979b2005-12-15 14:31:24 -0800863{
864 int ret = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800865 u32 clusters_to_add = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800866
Mark Fasheh53013cb2006-05-05 19:04:03 -0700867 BUG_ON(!tail_to_skip && !di_bh);
868
Mark Fashehccd979b2005-12-15 14:31:24 -0800869 /* setattr sometimes calls us like this. */
870 if (new_i_size == 0)
871 goto out;
872
873 if (i_size_read(inode) == new_i_size)
874 goto out;
875 BUG_ON(new_i_size < i_size_read(inode));
876
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800877 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
878 BUG_ON(tail_to_skip != 0);
879 goto out_update_size;
880 }
881
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
883 OCFS2_I(inode)->ip_clusters;
884
Mark Fasheh0effef72006-10-03 17:44:42 -0700885 /*
886 * protect the pages that ocfs2_zero_extend is going to be
887 * pulling into the page cache.. we do this before the
888 * metadata extend so that we don't get into the situation
889 * where we've extended the metadata but can't get the data
890 * lock to zero.
891 */
892 ret = ocfs2_data_lock(inode, 1);
893 if (ret < 0) {
894 mlog_errno(ret);
895 goto out;
896 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700897
Mark Fasheh0effef72006-10-03 17:44:42 -0700898 if (clusters_to_add) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800899 ret = ocfs2_extend_allocation(inode, clusters_to_add);
900 if (ret < 0) {
901 mlog_errno(ret);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700902 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800903 }
Mark Fasheh0effef72006-10-03 17:44:42 -0700904 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800905
Mark Fasheh0effef72006-10-03 17:44:42 -0700906 /*
907 * Call this even if we don't add any clusters to the tree. We
908 * still need to zero the area between the old i_size and the
909 * new i_size.
910 */
911 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
912 if (ret < 0) {
913 mlog_errno(ret);
914 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800915 }
916
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800917out_update_size:
Mark Fasheh53013cb2006-05-05 19:04:03 -0700918 if (!tail_to_skip) {
919 /* We're being called from ocfs2_setattr() which wants
920 * us to update i_size */
921 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
922 if (ret < 0)
923 mlog_errno(ret);
924 }
925
926out_unlock:
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800927 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
928 ocfs2_data_unlock(inode, 1);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700929
Mark Fashehccd979b2005-12-15 14:31:24 -0800930out:
931 return ret;
932}
933
934int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
935{
936 int status = 0, size_change;
937 struct inode *inode = dentry->d_inode;
938 struct super_block *sb = inode->i_sb;
939 struct ocfs2_super *osb = OCFS2_SB(sb);
940 struct buffer_head *bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700941 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800942
943 mlog_entry("(0x%p, '%.*s')\n", dentry,
944 dentry->d_name.len, dentry->d_name.name);
945
946 if (attr->ia_valid & ATTR_MODE)
947 mlog(0, "mode change: %d\n", attr->ia_mode);
948 if (attr->ia_valid & ATTR_UID)
949 mlog(0, "uid change: %d\n", attr->ia_uid);
950 if (attr->ia_valid & ATTR_GID)
951 mlog(0, "gid change: %d\n", attr->ia_gid);
952 if (attr->ia_valid & ATTR_SIZE)
953 mlog(0, "size change...\n");
954 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
955 mlog(0, "time change...\n");
956
957#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
958 | ATTR_GID | ATTR_UID | ATTR_MODE)
959 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
960 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
961 return 0;
962 }
963
964 status = inode_change_ok(inode, attr);
965 if (status)
966 return status;
967
968 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
969 if (size_change) {
970 status = ocfs2_rw_lock(inode, 1);
971 if (status < 0) {
972 mlog_errno(status);
973 goto bail;
974 }
975 }
976
Mark Fasheh4bcec182006-10-09 16:02:40 -0700977 status = ocfs2_meta_lock(inode, &bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800978 if (status < 0) {
979 if (status != -ENOENT)
980 mlog_errno(status);
981 goto bail_unlock_rw;
982 }
983
984 if (size_change && attr->ia_size != i_size_read(inode)) {
985 if (i_size_read(inode) > attr->ia_size)
986 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
987 else
Mark Fasheh53013cb2006-05-05 19:04:03 -0700988 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 if (status < 0) {
990 if (status != -ENOSPC)
991 mlog_errno(status);
992 status = -ENOSPC;
993 goto bail_unlock;
994 }
995 }
996
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700997 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800998 if (IS_ERR(handle)) {
999 status = PTR_ERR(handle);
1000 mlog_errno(status);
1001 goto bail_unlock;
1002 }
1003
Mark Fasheh7307de82007-05-09 15:16:19 -07001004 /*
1005 * This will intentionally not wind up calling vmtruncate(),
1006 * since all the work for a size change has been done above.
1007 * Otherwise, we could get into problems with truncate as
1008 * ip_alloc_sem is used there to protect against i_size
1009 * changes.
1010 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 status = inode_setattr(inode, attr);
1012 if (status < 0) {
1013 mlog_errno(status);
1014 goto bail_commit;
1015 }
1016
1017 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1018 if (status < 0)
1019 mlog_errno(status);
1020
1021bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001022 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001023bail_unlock:
1024 ocfs2_meta_unlock(inode, 1);
1025bail_unlock_rw:
1026 if (size_change)
1027 ocfs2_rw_unlock(inode, 1);
1028bail:
1029 if (bh)
1030 brelse(bh);
1031
1032 mlog_exit(status);
1033 return status;
1034}
1035
1036int ocfs2_getattr(struct vfsmount *mnt,
1037 struct dentry *dentry,
1038 struct kstat *stat)
1039{
1040 struct inode *inode = dentry->d_inode;
1041 struct super_block *sb = dentry->d_inode->i_sb;
1042 struct ocfs2_super *osb = sb->s_fs_info;
1043 int err;
1044
1045 mlog_entry_void();
1046
1047 err = ocfs2_inode_revalidate(dentry);
1048 if (err) {
1049 if (err != -ENOENT)
1050 mlog_errno(err);
1051 goto bail;
1052 }
1053
1054 generic_fillattr(inode, stat);
1055
1056 /* We set the blksize from the cluster size for performance */
1057 stat->blksize = osb->s_clustersize;
1058
1059bail:
1060 mlog_exit(err);
1061
1062 return err;
1063}
1064
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001065int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1066{
1067 int ret;
1068
1069 mlog_entry_void();
1070
1071 ret = ocfs2_meta_lock(inode, NULL, 0);
1072 if (ret) {
Mark Fasheha9f5f702007-04-26 11:43:43 -07001073 if (ret != -ENOENT)
1074 mlog_errno(ret);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001075 goto out;
1076 }
1077
1078 ret = generic_permission(inode, mask, NULL);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001079
1080 ocfs2_meta_unlock(inode, 0);
1081out:
1082 mlog_exit(ret);
1083 return ret;
1084}
1085
Mark Fashehccd979b2005-12-15 14:31:24 -08001086static int ocfs2_write_remove_suid(struct inode *inode)
1087{
1088 int ret;
1089 struct buffer_head *bh = NULL;
1090 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fasheh1fabe142006-10-09 18:11:45 -07001091 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1093 struct ocfs2_dinode *di;
1094
Mark Fashehb0697052006-03-03 10:24:33 -08001095 mlog_entry("(Inode %llu, mode 0%o)\n",
1096 (unsigned long long)oi->ip_blkno, inode->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001097
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001098 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 if (handle == NULL) {
1100 ret = -ENOMEM;
1101 mlog_errno(ret);
1102 goto out;
1103 }
1104
1105 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1106 if (ret < 0) {
1107 mlog_errno(ret);
1108 goto out_trans;
1109 }
1110
1111 ret = ocfs2_journal_access(handle, inode, bh,
1112 OCFS2_JOURNAL_ACCESS_WRITE);
1113 if (ret < 0) {
1114 mlog_errno(ret);
1115 goto out_bh;
1116 }
1117
1118 inode->i_mode &= ~S_ISUID;
1119 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1120 inode->i_mode &= ~S_ISGID;
1121
1122 di = (struct ocfs2_dinode *) bh->b_data;
1123 di->i_mode = cpu_to_le16(inode->i_mode);
1124
1125 ret = ocfs2_journal_dirty(handle, bh);
1126 if (ret < 0)
1127 mlog_errno(ret);
1128out_bh:
1129 brelse(bh);
1130out_trans:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001131 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132out:
1133 mlog_exit(ret);
1134 return ret;
1135}
1136
Mark Fasheh9517bac2007-02-09 20:24:12 -08001137/*
1138 * Will look for holes and unwritten extents in the range starting at
1139 * pos for count bytes (inclusive).
1140 */
1141static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1142 size_t count)
1143{
1144 int ret = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001145 unsigned int extent_flags;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001146 u32 cpos, clusters, extent_len, phys_cpos;
1147 struct super_block *sb = inode->i_sb;
1148
1149 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1150 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1151
1152 while (clusters) {
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001153 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1154 &extent_flags);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001155 if (ret < 0) {
1156 mlog_errno(ret);
1157 goto out;
1158 }
1159
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001160 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001161 ret = 1;
1162 break;
1163 }
1164
1165 if (extent_len > clusters)
1166 extent_len = clusters;
1167
1168 clusters -= extent_len;
1169 cpos += extent_len;
1170 }
1171out:
1172 return ret;
1173}
1174
Tiger Yang8659ac22006-10-17 18:29:52 -07001175static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1176 loff_t *ppos,
1177 size_t count,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001178 int appending,
1179 int *direct_io)
Mark Fashehccd979b2005-12-15 14:31:24 -08001180{
Tiger Yang8659ac22006-10-17 18:29:52 -07001181 int ret = 0, meta_level = appending;
1182 struct inode *inode = dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001183 u32 clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 loff_t newsize, saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001185
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 /*
1187 * We sample i_size under a read level meta lock to see if our write
1188 * is extending the file, if it is we back off and get a write level
1189 * meta lock.
1190 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 for(;;) {
Mark Fasheh4bcec182006-10-09 16:02:40 -07001192 ret = ocfs2_meta_lock(inode, NULL, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001193 if (ret < 0) {
1194 meta_level = -1;
1195 mlog_errno(ret);
1196 goto out;
1197 }
1198
1199 /* Clear suid / sgid if necessary. We do this here
1200 * instead of later in the write path because
1201 * remove_suid() calls ->setattr without any hint that
1202 * we may have already done our cluster locking. Since
1203 * ocfs2_setattr() *must* take cluster locks to
1204 * proceeed, this will lead us to recursively lock the
1205 * inode. There's also the dinode i_size state which
1206 * can be lost via setattr during extending writes (we
1207 * set inode->i_size at the end of a write. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001208 if (should_remove_suid(dentry)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 if (meta_level == 0) {
1210 ocfs2_meta_unlock(inode, meta_level);
1211 meta_level = 1;
1212 continue;
1213 }
1214
1215 ret = ocfs2_write_remove_suid(inode);
1216 if (ret < 0) {
1217 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001218 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001219 }
1220 }
1221
1222 /* work on a copy of ppos until we're sure that we won't have
1223 * to recalculate it due to relocking. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001224 if (appending) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 saved_pos = i_size_read(inode);
1226 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1227 } else {
Tiger Yang8659ac22006-10-17 18:29:52 -07001228 saved_pos = *ppos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 }
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001230
Mark Fasheh9517bac2007-02-09 20:24:12 -08001231 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1232 loff_t end = saved_pos + count;
1233
1234 /*
1235 * Skip the O_DIRECT checks if we don't need
1236 * them.
1237 */
1238 if (!direct_io || !(*direct_io))
1239 break;
1240
1241 /*
1242 * Allowing concurrent direct writes means
1243 * i_size changes wouldn't be synchronized, so
1244 * one node could wind up truncating another
1245 * nodes writes.
1246 */
1247 if (end > i_size_read(inode)) {
1248 *direct_io = 0;
1249 break;
1250 }
1251
1252 /*
1253 * We don't fill holes during direct io, so
1254 * check for them here. If any are found, the
1255 * caller will have to retake some cluster
1256 * locks and initiate the io as buffered.
1257 */
1258 ret = ocfs2_check_range_for_holes(inode, saved_pos,
1259 count);
1260 if (ret == 1) {
1261 *direct_io = 0;
1262 ret = 0;
1263 } else if (ret < 0)
1264 mlog_errno(ret);
1265 break;
1266 }
1267
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001268 /*
1269 * The rest of this loop is concerned with legacy file
1270 * systems which don't support sparse files.
1271 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001272
Tiger Yang8659ac22006-10-17 18:29:52 -07001273 newsize = count + saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274
Mark Fasheh215c7f92006-02-01 16:42:10 -08001275 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1276 (long long) saved_pos, (long long) newsize,
1277 (long long) i_size_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001278
1279 /* No need for a higher level metadata lock if we're
1280 * never going past i_size. */
1281 if (newsize <= i_size_read(inode))
1282 break;
1283
1284 if (meta_level == 0) {
1285 ocfs2_meta_unlock(inode, meta_level);
1286 meta_level = 1;
1287 continue;
1288 }
1289
1290 spin_lock(&OCFS2_I(inode)->ip_lock);
1291 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1292 OCFS2_I(inode)->ip_clusters;
1293 spin_unlock(&OCFS2_I(inode)->ip_lock);
1294
1295 mlog(0, "Writing at EOF, may need more allocation: "
Mark Fasheh215c7f92006-02-01 16:42:10 -08001296 "i_size = %lld, newsize = %lld, need %u clusters\n",
1297 (long long) i_size_read(inode), (long long) newsize,
1298 clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08001299
1300 /* We only want to continue the rest of this loop if
1301 * our extend will actually require more
1302 * allocation. */
1303 if (!clusters)
1304 break;
1305
Tiger Yang8659ac22006-10-17 18:29:52 -07001306 ret = ocfs2_extend_file(inode, NULL, newsize, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 if (ret < 0) {
1308 if (ret != -ENOSPC)
1309 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001310 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001312 break;
1313 }
1314
Tiger Yang8659ac22006-10-17 18:29:52 -07001315 if (appending)
1316 *ppos = saved_pos;
1317
1318out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001319 ocfs2_meta_unlock(inode, meta_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07001320
1321out:
1322 return ret;
1323}
1324
Mark Fasheh9517bac2007-02-09 20:24:12 -08001325static inline void
1326ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1327{
1328 const struct iovec *iov = *iovp;
1329 size_t base = *basep;
1330
1331 do {
1332 int copy = min(bytes, iov->iov_len - base);
1333
1334 bytes -= copy;
1335 base += copy;
1336 if (iov->iov_len == base) {
1337 iov++;
1338 base = 0;
1339 }
1340 } while (bytes);
1341 *iovp = iov;
1342 *basep = base;
1343}
1344
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001345static struct page * ocfs2_get_write_source(char **ret_src_buf,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001346 const struct iovec *cur_iov,
1347 size_t iov_offset)
1348{
1349 int ret;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001350 char *buf = cur_iov->iov_base + iov_offset;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001351 struct page *src_page = NULL;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001352 unsigned long off;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001353
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001354 off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001355
1356 if (!segment_eq(get_fs(), KERNEL_DS)) {
1357 /*
1358 * Pull in the user page. We want to do this outside
1359 * of the meta data locks in order to preserve locking
1360 * order in case of page fault.
1361 */
1362 ret = get_user_pages(current, current->mm,
1363 (unsigned long)buf & PAGE_CACHE_MASK, 1,
1364 0, 0, &src_page, NULL);
1365 if (ret == 1)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001366 *ret_src_buf = kmap(src_page) + off;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001367 else
1368 src_page = ERR_PTR(-EFAULT);
1369 } else {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001370 *ret_src_buf = buf;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001371 }
1372
1373 return src_page;
1374}
1375
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001376static void ocfs2_put_write_source(struct page *page)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001377{
1378 if (page) {
1379 kunmap(page);
1380 page_cache_release(page);
1381 }
1382}
1383
1384static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1385 const struct iovec *iov,
1386 unsigned long nr_segs,
1387 size_t count,
1388 ssize_t o_direct_written)
1389{
1390 int ret = 0;
1391 ssize_t copied, total = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001392 size_t iov_offset = 0, bytes;
1393 loff_t pos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001394 const struct iovec *cur_iov = iov;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001395 struct page *user_page, *page;
1396 char *buf, *dst;
1397 void *fsdata;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001398
1399 /*
1400 * handle partial DIO write. Adjust cur_iov if needed.
1401 */
1402 ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1403
1404 do {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001405 pos = *ppos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001406
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001407 user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
1408 if (IS_ERR(user_page)) {
1409 ret = PTR_ERR(user_page);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001410 goto out;
1411 }
1412
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001413 /* Stay within our page boundaries */
1414 bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
1415 (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
1416 /* Stay within the vector boundary */
1417 bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
1418 /* Stay within count */
1419 bytes = min(bytes, count);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001420
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001421 page = NULL;
1422 ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
1423 &page, &fsdata);
1424 if (ret) {
1425 mlog_errno(ret);
1426 goto out;
1427 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001428
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001429 dst = kmap_atomic(page, KM_USER0);
1430 memcpy(dst + (pos & (PAGE_CACHE_SIZE - 1)), buf, bytes);
1431 kunmap_atomic(dst, KM_USER0);
1432 flush_dcache_page(page);
1433 ocfs2_put_write_source(user_page);
1434
1435 copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
1436 bytes, page, fsdata);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001437 if (copied < 0) {
1438 mlog_errno(copied);
1439 ret = copied;
1440 goto out;
1441 }
1442
1443 total += copied;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001444 *ppos = pos + copied;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001445 count -= copied;
1446
1447 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1448 } while(count);
1449
1450out:
1451 return total ? total : ret;
1452}
1453
Tiger Yang8659ac22006-10-17 18:29:52 -07001454static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1455 const struct iovec *iov,
1456 unsigned long nr_segs,
1457 loff_t pos)
1458{
Mark Fasheh9517bac2007-02-09 20:24:12 -08001459 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1460 int can_do_direct, sync = 0;
1461 ssize_t written = 0;
1462 size_t ocount; /* original count */
1463 size_t count; /* after file limit checks */
1464 loff_t *ppos = &iocb->ki_pos;
1465 struct file *file = iocb->ki_filp;
1466 struct inode *inode = file->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001467
Mark Fasheh9517bac2007-02-09 20:24:12 -08001468 mlog_entry("(0x%p, %u, '%.*s')\n", file,
Tiger Yang8659ac22006-10-17 18:29:52 -07001469 (unsigned int)nr_segs,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001470 file->f_path.dentry->d_name.len,
1471 file->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001472
Tiger Yang8659ac22006-10-17 18:29:52 -07001473 if (iocb->ki_left == 0)
1474 return 0;
1475
Christoph Hellwigd9b08b92007-05-18 13:12:40 +02001476 ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001477 if (ret)
1478 return ret;
1479
1480 count = ocount;
1481
1482 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1483
1484 appending = file->f_flags & O_APPEND ? 1 : 0;
1485 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1486
Tiger Yang8659ac22006-10-17 18:29:52 -07001487 mutex_lock(&inode->i_mutex);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001488
1489relock:
Tiger Yang8659ac22006-10-17 18:29:52 -07001490 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001491 if (direct_io) {
Tiger Yang8659ac22006-10-17 18:29:52 -07001492 down_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001493 have_alloc_sem = 1;
Tiger Yang8659ac22006-10-17 18:29:52 -07001494 }
1495
1496 /* concurrent O_DIRECT writes are allowed */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001497 rw_level = !direct_io;
Tiger Yang8659ac22006-10-17 18:29:52 -07001498 ret = ocfs2_rw_lock(inode, rw_level);
1499 if (ret < 0) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001500 mlog_errno(ret);
1501 goto out_sems;
1502 }
1503
1504 can_do_direct = direct_io;
1505 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1506 iocb->ki_left, appending,
1507 &can_do_direct);
1508 if (ret < 0) {
Tiger Yang8659ac22006-10-17 18:29:52 -07001509 mlog_errno(ret);
1510 goto out;
1511 }
1512
Mark Fasheh9517bac2007-02-09 20:24:12 -08001513 /*
1514 * We can't complete the direct I/O as requested, fall back to
1515 * buffered I/O.
1516 */
1517 if (direct_io && !can_do_direct) {
1518 ocfs2_rw_unlock(inode, rw_level);
1519 up_read(&inode->i_alloc_sem);
1520
1521 have_alloc_sem = 0;
1522 rw_level = -1;
1523
1524 direct_io = 0;
1525 sync = 1;
1526 goto relock;
Tiger Yang8659ac22006-10-17 18:29:52 -07001527 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001528
Mark Fasheh9517bac2007-02-09 20:24:12 -08001529 if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
1530 sync = 1;
1531
1532 /*
1533 * XXX: Is it ok to execute these checks a second time?
1534 */
1535 ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
1536 if (ret)
1537 goto out;
1538
1539 /*
1540 * Set pos so that sync_page_range_nolock() below understands
1541 * where to start from. We might've moved it around via the
1542 * calls above. The range we want to actually sync starts from
1543 * *ppos here.
1544 *
1545 */
1546 pos = *ppos;
1547
Mark Fashehccd979b2005-12-15 14:31:24 -08001548 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07001549 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001550
Mark Fasheh9517bac2007-02-09 20:24:12 -08001551 if (direct_io) {
1552 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1553 ppos, count, ocount);
1554 if (written < 0) {
1555 ret = written;
1556 goto out_dio;
1557 }
1558 } else {
1559 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
1560 count, written);
1561 if (written < 0) {
1562 ret = written;
1563 if (ret != -EFAULT || ret != -ENOSPC)
1564 mlog_errno(ret);
1565 goto out;
1566 }
1567 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001568
Mark Fasheh9517bac2007-02-09 20:24:12 -08001569out_dio:
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 /* buffered aio wouldn't have proper lock coverage today */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001571 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
Mark Fashehccd979b2005-12-15 14:31:24 -08001572
1573 /*
1574 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1575 * function pointer which is called when o_direct io completes so that
1576 * it can unlock our rw lock. (it's the clustered equivalent of
1577 * i_alloc_sem; protects truncate from racing with pending ios).
1578 * Unfortunately there are error cases which call end_io and others
1579 * that don't. so we don't have to unlock the rw_lock if either an
1580 * async dio is going to do it in the future or an end_io after an
1581 * error has already done it.
1582 */
1583 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1584 rw_level = -1;
1585 have_alloc_sem = 0;
1586 }
1587
1588out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08001589 if (rw_level != -1)
1590 ocfs2_rw_unlock(inode, rw_level);
1591
1592out_sems:
Mark Fashehccd979b2005-12-15 14:31:24 -08001593 if (have_alloc_sem)
1594 up_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001595
1596 if (written > 0 && sync) {
1597 ssize_t err;
1598
1599 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
1600 if (err < 0)
1601 written = err;
1602 }
1603
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001604 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001605
1606 mlog_exit(ret);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001607 return written ? written : ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001608}
1609
Mark Fasheh6af67d82007-03-06 17:24:46 -08001610static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
1611 struct pipe_buffer *buf,
1612 struct splice_desc *sd)
1613{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001614 int ret, count;
Mark Fasheh6af67d82007-03-06 17:24:46 -08001615 ssize_t copied = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001616 struct file *file = sd->u.file;
1617 unsigned int offset;
1618 struct page *page = NULL;
1619 void *fsdata;
1620 char *src, *dst;
Mark Fasheh6af67d82007-03-06 17:24:46 -08001621
Jens Axboecac36bb2007-06-14 13:10:48 +02001622 ret = buf->ops->confirm(pipe, buf);
Mark Fasheh6af67d82007-03-06 17:24:46 -08001623 if (ret)
1624 goto out;
1625
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001626 offset = sd->pos & ~PAGE_CACHE_MASK;
Mark Fasheh6af67d82007-03-06 17:24:46 -08001627 count = sd->len;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001628 if (count + offset > PAGE_CACHE_SIZE)
1629 count = PAGE_CACHE_SIZE - offset;
Mark Fasheh6af67d82007-03-06 17:24:46 -08001630
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001631 ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
1632 &page, &fsdata);
1633 if (ret) {
1634 mlog_errno(ret);
1635 goto out;
1636 }
Mark Fasheh6af67d82007-03-06 17:24:46 -08001637
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001638 src = buf->ops->map(pipe, buf, 1);
1639 dst = kmap_atomic(page, KM_USER1);
1640 memcpy(dst + offset, src + buf->offset, count);
1641 kunmap_atomic(page, KM_USER1);
1642 buf->ops->unmap(pipe, buf, src);
Mark Fasheh6af67d82007-03-06 17:24:46 -08001643
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001644 copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
1645 page, fsdata);
1646 if (copied < 0) {
1647 mlog_errno(copied);
1648 ret = copied;
1649 goto out;
1650 }
Mark Fasheh6af67d82007-03-06 17:24:46 -08001651out:
1652
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001653 return copied ? copied : ret;
Mark Fasheh6af67d82007-03-06 17:24:46 -08001654}
1655
1656static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1657 struct file *out,
1658 loff_t *ppos,
1659 size_t len,
1660 unsigned int flags)
1661{
1662 int ret, err;
1663 struct address_space *mapping = out->f_mapping;
1664 struct inode *inode = mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001665 struct splice_desc sd = {
1666 .total_len = len,
1667 .flags = flags,
1668 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001669 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001670 };
Mark Fasheh6af67d82007-03-06 17:24:46 -08001671
Jens Axboec66ab6f2007-06-12 21:17:17 +02001672 ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
Mark Fasheh6af67d82007-03-06 17:24:46 -08001673 if (ret > 0) {
1674 *ppos += ret;
1675
1676 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
1677 err = generic_osync_inode(inode, mapping,
1678 OSYNC_METADATA|OSYNC_DATA);
1679 if (err)
1680 ret = err;
1681 }
1682 }
1683
1684 return ret;
1685}
1686
Tiger Yang8659ac22006-10-17 18:29:52 -07001687static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1688 struct file *out,
1689 loff_t *ppos,
1690 size_t len,
1691 unsigned int flags)
1692{
1693 int ret;
Josef Sipekd28c9172006-12-08 02:37:25 -08001694 struct inode *inode = out->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001695
1696 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1697 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08001698 out->f_path.dentry->d_name.len,
1699 out->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001700
1701 inode_double_lock(inode, pipe->inode);
1702
1703 ret = ocfs2_rw_lock(inode, 1);
1704 if (ret < 0) {
1705 mlog_errno(ret);
1706 goto out;
1707 }
1708
Mark Fasheh9517bac2007-02-09 20:24:12 -08001709 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
1710 NULL);
Tiger Yang8659ac22006-10-17 18:29:52 -07001711 if (ret < 0) {
1712 mlog_errno(ret);
1713 goto out_unlock;
1714 }
1715
1716 /* ok, we're done with i_size and alloc work */
Mark Fasheh6af67d82007-03-06 17:24:46 -08001717 ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
Tiger Yang8659ac22006-10-17 18:29:52 -07001718
1719out_unlock:
1720 ocfs2_rw_unlock(inode, 1);
1721out:
1722 inode_double_unlock(inode, pipe->inode);
1723
1724 mlog_exit(ret);
1725 return ret;
1726}
1727
1728static ssize_t ocfs2_file_splice_read(struct file *in,
1729 loff_t *ppos,
1730 struct pipe_inode_info *pipe,
1731 size_t len,
1732 unsigned int flags)
1733{
1734 int ret = 0;
Josef Sipekd28c9172006-12-08 02:37:25 -08001735 struct inode *inode = in->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001736
1737 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1738 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08001739 in->f_path.dentry->d_name.len,
1740 in->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001741
1742 /*
1743 * See the comment in ocfs2_file_aio_read()
1744 */
1745 ret = ocfs2_meta_lock(inode, NULL, 0);
1746 if (ret < 0) {
1747 mlog_errno(ret);
1748 goto bail;
1749 }
1750 ocfs2_meta_unlock(inode, 0);
1751
1752 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1753
1754bail:
1755 mlog_exit(ret);
1756 return ret;
1757}
1758
Mark Fashehccd979b2005-12-15 14:31:24 -08001759static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001760 const struct iovec *iov,
1761 unsigned long nr_segs,
Mark Fashehccd979b2005-12-15 14:31:24 -08001762 loff_t pos)
1763{
Tiger Yang25899de2006-11-15 15:49:02 +08001764 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 struct file *filp = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -08001766 struct inode *inode = filp->f_path.dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001767
Badari Pulavarty027445c2006-09-30 23:28:46 -07001768 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1769 (unsigned int)nr_segs,
Josef Sipekd28c9172006-12-08 02:37:25 -08001770 filp->f_path.dentry->d_name.len,
1771 filp->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001772
1773 if (!inode) {
1774 ret = -EINVAL;
1775 mlog_errno(ret);
1776 goto bail;
1777 }
1778
Mark Fashehccd979b2005-12-15 14:31:24 -08001779 /*
1780 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1781 * need locks to protect pending reads from racing with truncate.
1782 */
1783 if (filp->f_flags & O_DIRECT) {
1784 down_read(&inode->i_alloc_sem);
1785 have_alloc_sem = 1;
1786
1787 ret = ocfs2_rw_lock(inode, 0);
1788 if (ret < 0) {
1789 mlog_errno(ret);
1790 goto bail;
1791 }
1792 rw_level = 0;
1793 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07001794 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 }
1796
Mark Fashehc4374f82006-05-05 19:04:35 -07001797 /*
1798 * We're fine letting folks race truncates and extending
1799 * writes with read across the cluster, just like they can
1800 * locally. Hence no rw_lock during read.
1801 *
1802 * Take and drop the meta data lock to update inode fields
1803 * like i_size. This allows the checks down below
1804 * generic_file_aio_read() a chance of actually working.
1805 */
Tiger Yang25899de2006-11-15 15:49:02 +08001806 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07001807 if (ret < 0) {
1808 mlog_errno(ret);
1809 goto bail;
1810 }
Tiger Yang25899de2006-11-15 15:49:02 +08001811 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07001812
Badari Pulavarty027445c2006-09-30 23:28:46 -07001813 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 if (ret == -EINVAL)
1815 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1816
1817 /* buffered aio wouldn't have proper lock coverage today */
1818 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1819
1820 /* see ocfs2_file_aio_write */
1821 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1822 rw_level = -1;
1823 have_alloc_sem = 0;
1824 }
1825
1826bail:
1827 if (have_alloc_sem)
1828 up_read(&inode->i_alloc_sem);
1829 if (rw_level != -1)
1830 ocfs2_rw_unlock(inode, rw_level);
1831 mlog_exit(ret);
1832
1833 return ret;
1834}
1835
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001836const struct inode_operations ocfs2_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001837 .setattr = ocfs2_setattr,
1838 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001839 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08001840};
1841
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001842const struct inode_operations ocfs2_special_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001843 .setattr = ocfs2_setattr,
1844 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001845 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08001846};
1847
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001848const struct file_operations ocfs2_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001849 .read = do_sync_read,
1850 .write = do_sync_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08001851 .mmap = ocfs2_mmap,
1852 .fsync = ocfs2_sync_file,
1853 .release = ocfs2_file_release,
1854 .open = ocfs2_file_open,
1855 .aio_read = ocfs2_file_aio_read,
1856 .aio_write = ocfs2_file_aio_write,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001857 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08001858#ifdef CONFIG_COMPAT
1859 .compat_ioctl = ocfs2_compat_ioctl,
1860#endif
Tiger Yang8659ac22006-10-17 18:29:52 -07001861 .splice_read = ocfs2_file_splice_read,
1862 .splice_write = ocfs2_file_splice_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08001863};
1864
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001865const struct file_operations ocfs2_dops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 .read = generic_read_dir,
1867 .readdir = ocfs2_readdir,
1868 .fsync = ocfs2_sync_file,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001869 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08001870#ifdef CONFIG_COMPAT
1871 .compat_ioctl = ocfs2_compat_ioctl,
1872#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001873};