blob: 25e36fbd7bc37afd6020a091bbda6cd23d112e96 [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>
Tiger Yang8659ac22006-10-17 18:29:52 -070034#include <linux/pipe_fs_i.h>
Tiger Yang7f1a37e2006-11-15 15:48:42 +080035#include <linux/mount.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080036
37#define MLOG_MASK_PREFIX ML_INODE
38#include <cluster/masklog.h>
39
40#include "ocfs2.h"
41
42#include "alloc.h"
43#include "aops.h"
44#include "dir.h"
45#include "dlmglue.h"
46#include "extent_map.h"
47#include "file.h"
48#include "sysfile.h"
49#include "inode.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070050#include "ioctl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080051#include "journal.h"
52#include "mmap.h"
53#include "suballoc.h"
54#include "super.h"
55
56#include "buffer_head_io.h"
57
58static int ocfs2_sync_inode(struct inode *inode)
59{
60 filemap_fdatawrite(inode->i_mapping);
61 return sync_mapping_buffers(inode->i_mapping);
62}
63
64static int ocfs2_file_open(struct inode *inode, struct file *file)
65{
66 int status;
67 int mode = file->f_flags;
68 struct ocfs2_inode_info *oi = OCFS2_I(inode);
69
70 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
Josef Sipekd28c9172006-12-08 02:37:25 -080071 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -080072
73 spin_lock(&oi->ip_lock);
74
75 /* Check that the inode hasn't been wiped from disk by another
76 * node. If it hasn't then we're safe as long as we hold the
77 * spin lock until our increment of open count. */
78 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
79 spin_unlock(&oi->ip_lock);
80
81 status = -ENOENT;
82 goto leave;
83 }
84
85 if (mode & O_DIRECT)
86 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
87
88 oi->ip_open_count++;
89 spin_unlock(&oi->ip_lock);
90 status = 0;
91leave:
92 mlog_exit(status);
93 return status;
94}
95
96static int ocfs2_file_release(struct inode *inode, struct file *file)
97{
98 struct ocfs2_inode_info *oi = OCFS2_I(inode);
99
100 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
Josef Sipekd28c9172006-12-08 02:37:25 -0800101 file->f_path.dentry->d_name.len,
102 file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800103
104 spin_lock(&oi->ip_lock);
105 if (!--oi->ip_open_count)
106 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
107 spin_unlock(&oi->ip_lock);
108
109 mlog_exit(0);
110
111 return 0;
112}
113
114static int ocfs2_sync_file(struct file *file,
115 struct dentry *dentry,
116 int datasync)
117{
118 int err = 0;
119 journal_t *journal;
120 struct inode *inode = dentry->d_inode;
121 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
122
123 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
124 dentry->d_name.len, dentry->d_name.name);
125
126 err = ocfs2_sync_inode(dentry->d_inode);
127 if (err)
128 goto bail;
129
130 journal = osb->journal->j_journal;
131 err = journal_force_commit(journal);
132
133bail:
134 mlog_exit(err);
135
136 return (err < 0) ? -EIO : 0;
137}
138
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800139int ocfs2_should_update_atime(struct inode *inode,
140 struct vfsmount *vfsmnt)
141{
142 struct timespec now;
143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
144
145 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
146 return 0;
147
148 if ((inode->i_flags & S_NOATIME) ||
149 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
150 return 0;
151
Mark Fasheh6c2aad02006-12-19 15:25:52 -0800152 /*
153 * We can be called with no vfsmnt structure - NFSD will
154 * sometimes do this.
155 *
156 * Note that our action here is different than touch_atime() -
157 * if we can't tell whether this is a noatime mount, then we
158 * don't know whether to trust the value of s_atime_quantum.
159 */
160 if (vfsmnt == NULL)
161 return 0;
162
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800163 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
164 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
165 return 0;
166
Mark Fasheh7e913c52006-12-13 00:34:35 -0800167 if (vfsmnt->mnt_flags & MNT_RELATIME) {
168 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
169 (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
170 return 1;
171
172 return 0;
173 }
174
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800175 now = CURRENT_TIME;
176 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
177 return 0;
178 else
179 return 1;
180}
181
182int ocfs2_update_inode_atime(struct inode *inode,
183 struct buffer_head *bh)
184{
185 int ret;
186 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
187 handle_t *handle;
188
189 mlog_entry_void();
190
191 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
192 if (handle == NULL) {
193 ret = -ENOMEM;
194 mlog_errno(ret);
195 goto out;
196 }
197
198 inode->i_atime = CURRENT_TIME;
199 ret = ocfs2_mark_inode_dirty(handle, inode, bh);
200 if (ret < 0)
201 mlog_errno(ret);
202
203 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
204out:
205 mlog_exit(ret);
206 return ret;
207}
208
Mark Fasheh1fabe142006-10-09 18:11:45 -0700209int ocfs2_set_inode_size(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800210 struct inode *inode,
211 struct buffer_head *fe_bh,
212 u64 new_i_size)
213{
214 int status;
215
216 mlog_entry_void();
217 i_size_write(inode, new_i_size);
218 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
219 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
220
221 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
222 if (status < 0) {
223 mlog_errno(status);
224 goto bail;
225 }
226
227bail:
228 mlog_exit(status);
229 return status;
230}
231
232static int ocfs2_simple_size_update(struct inode *inode,
233 struct buffer_head *di_bh,
234 u64 new_i_size)
235{
236 int ret;
237 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700238 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800239
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700240 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800241 if (handle == NULL) {
242 ret = -ENOMEM;
243 mlog_errno(ret);
244 goto out;
245 }
246
247 ret = ocfs2_set_inode_size(handle, inode, di_bh,
248 new_i_size);
249 if (ret < 0)
250 mlog_errno(ret);
251
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700252 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800253out:
254 return ret;
255}
256
257static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
258 struct inode *inode,
259 struct buffer_head *fe_bh,
260 u64 new_i_size)
261{
262 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700263 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800264
265 mlog_entry_void();
266
267 /* TODO: This needs to actually orphan the inode in this
268 * transaction. */
269
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700270 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800271 if (IS_ERR(handle)) {
272 status = PTR_ERR(handle);
273 mlog_errno(status);
274 goto out;
275 }
276
277 status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
278 if (status < 0)
279 mlog_errno(status);
280
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700281 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800282out:
283 mlog_exit(status);
284 return status;
285}
286
287static int ocfs2_truncate_file(struct inode *inode,
288 struct buffer_head *di_bh,
289 u64 new_i_size)
290{
291 int status = 0;
292 struct ocfs2_dinode *fe = NULL;
293 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
294 struct ocfs2_truncate_context *tc = NULL;
295
Mark Fashehb0697052006-03-03 10:24:33 -0800296 mlog_entry("(inode = %llu, new_i_size = %llu\n",
297 (unsigned long long)OCFS2_I(inode)->ip_blkno,
298 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800299
300 truncate_inode_pages(inode->i_mapping, new_i_size);
301
302 fe = (struct ocfs2_dinode *) di_bh->b_data;
303 if (!OCFS2_IS_VALID_DINODE(fe)) {
304 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
305 status = -EIO;
306 goto bail;
307 }
308
309 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
Mark Fashehb0697052006-03-03 10:24:33 -0800310 "Inode %llu, inode i_size = %lld != di "
311 "i_size = %llu, i_flags = 0x%x\n",
312 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800313 i_size_read(inode),
Mark Fashehb0697052006-03-03 10:24:33 -0800314 (unsigned long long)le64_to_cpu(fe->i_size),
315 le32_to_cpu(fe->i_flags));
Mark Fashehccd979b2005-12-15 14:31:24 -0800316
317 if (new_i_size > le64_to_cpu(fe->i_size)) {
Mark Fashehb0697052006-03-03 10:24:33 -0800318 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
319 (unsigned long long)le64_to_cpu(fe->i_size),
320 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800321 status = -EINVAL;
322 mlog_errno(status);
323 goto bail;
324 }
325
Mark Fashehb0697052006-03-03 10:24:33 -0800326 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
327 (unsigned long long)le64_to_cpu(fe->i_blkno),
328 (unsigned long long)le64_to_cpu(fe->i_size),
329 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800330
331 /* lets handle the simple truncate cases before doing any more
332 * cluster locking. */
333 if (new_i_size == le64_to_cpu(fe->i_size))
334 goto bail;
335
Mark Fashehab0920c2006-03-16 15:06:37 -0800336 /* This forces other nodes to sync and drop their pages. Do
337 * this even if we have a truncate without allocation change -
338 * ocfs2 cluster sizes can be much greater than page size, so
339 * we have to truncate them anyway. */
340 status = ocfs2_data_lock(inode, 1);
341 if (status < 0) {
342 mlog_errno(status);
343 goto bail;
344 }
345 ocfs2_data_unlock(inode, 1);
346
Mark Fashehccd979b2005-12-15 14:31:24 -0800347 if (le32_to_cpu(fe->i_clusters) ==
348 ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
349 mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
350 fe->i_clusters);
351 /* No allocation change is required, so lets fast path
352 * this truncate. */
353 status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
354 if (status < 0)
355 mlog_errno(status);
356 goto bail;
357 }
358
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 /* alright, we're going to need to do a full blown alloc size
360 * change. Orphan the inode so that recovery can complete the
361 * truncate if necessary. This does the task of marking
362 * i_size. */
363 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
364 if (status < 0) {
365 mlog_errno(status);
366 goto bail;
367 }
368
369 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
370 if (status < 0) {
371 mlog_errno(status);
372 goto bail;
373 }
374
375 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
376 if (status < 0) {
377 mlog_errno(status);
378 goto bail;
379 }
380
381 /* TODO: orphan dir cleanup here. */
382bail:
383
384 mlog_exit(status);
385 return status;
386}
387
388/*
389 * extend allocation only here.
390 * we'll update all the disk stuff, and oip->alloc_size
391 *
392 * expect stuff to be locked, a transaction started and enough data /
393 * metadata reservations in the contexts.
394 *
395 * Will return -EAGAIN, and a reason if a restart is needed.
396 * If passed in, *reason will always be set, even in error.
397 */
398int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
399 struct inode *inode,
400 u32 clusters_to_add,
401 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700402 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 struct ocfs2_alloc_context *data_ac,
404 struct ocfs2_alloc_context *meta_ac,
405 enum ocfs2_alloc_restarted *reason_ret)
406{
407 int status = 0;
408 int free_extents;
409 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
410 enum ocfs2_alloc_restarted reason = RESTART_NONE;
411 u32 bit_off, num_bits;
412 u64 block;
413
414 BUG_ON(!clusters_to_add);
415
416 free_extents = ocfs2_num_free_extents(osb, inode, fe);
417 if (free_extents < 0) {
418 status = free_extents;
419 mlog_errno(status);
420 goto leave;
421 }
422
423 /* there are two cases which could cause us to EAGAIN in the
424 * we-need-more-metadata case:
425 * 1) we haven't reserved *any*
426 * 2) we are so fragmented, we've needed to add metadata too
427 * many times. */
428 if (!free_extents && !meta_ac) {
429 mlog(0, "we haven't reserved any metadata!\n");
430 status = -EAGAIN;
431 reason = RESTART_META;
432 goto leave;
433 } else if ((!free_extents)
434 && (ocfs2_alloc_context_bits_left(meta_ac)
435 < ocfs2_extend_meta_needed(fe))) {
436 mlog(0, "filesystem is really fragmented...\n");
437 status = -EAGAIN;
438 reason = RESTART_META;
439 goto leave;
440 }
441
442 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
443 &bit_off, &num_bits);
444 if (status < 0) {
445 if (status != -ENOSPC)
446 mlog_errno(status);
447 goto leave;
448 }
449
450 BUG_ON(num_bits > clusters_to_add);
451
452 /* reserve our write early -- insert_extent may update the inode */
453 status = ocfs2_journal_access(handle, inode, fe_bh,
454 OCFS2_JOURNAL_ACCESS_WRITE);
455 if (status < 0) {
456 mlog_errno(status);
457 goto leave;
458 }
459
460 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb0697052006-03-03 10:24:33 -0800461 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
462 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800463 status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
464 num_bits, meta_ac);
465 if (status < 0) {
466 mlog_errno(status);
467 goto leave;
468 }
469
470 le32_add_cpu(&fe->i_clusters, num_bits);
471 spin_lock(&OCFS2_I(inode)->ip_lock);
472 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
473 spin_unlock(&OCFS2_I(inode)->ip_lock);
474
475 status = ocfs2_journal_dirty(handle, fe_bh);
476 if (status < 0) {
477 mlog_errno(status);
478 goto leave;
479 }
480
481 clusters_to_add -= num_bits;
482
483 if (clusters_to_add) {
484 mlog(0, "need to alloc once more, clusters = %u, wanted = "
485 "%u\n", fe->i_clusters, clusters_to_add);
486 status = -EAGAIN;
487 reason = RESTART_TRANS;
488 }
489
490leave:
491 mlog_exit(status);
492 if (reason_ret)
493 *reason_ret = reason;
494 return status;
495}
496
497static int ocfs2_extend_allocation(struct inode *inode,
498 u32 clusters_to_add)
499{
500 int status = 0;
501 int restart_func = 0;
502 int drop_alloc_sem = 0;
503 int credits, num_free_extents;
504 u32 prev_clusters;
505 struct buffer_head *bh = NULL;
506 struct ocfs2_dinode *fe = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700507 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800508 struct ocfs2_alloc_context *data_ac = NULL;
509 struct ocfs2_alloc_context *meta_ac = NULL;
510 enum ocfs2_alloc_restarted why;
511 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
512
513 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
514
515 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
516 OCFS2_BH_CACHED, inode);
517 if (status < 0) {
518 mlog_errno(status);
519 goto leave;
520 }
521
522 fe = (struct ocfs2_dinode *) bh->b_data;
523 if (!OCFS2_IS_VALID_DINODE(fe)) {
524 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
525 status = -EIO;
526 goto leave;
527 }
528
529restart_all:
530 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
531
Mark Fashehb0697052006-03-03 10:24:33 -0800532 mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 "clusters_to_add = %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -0800534 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 fe->i_clusters, clusters_to_add);
536
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 num_free_extents = ocfs2_num_free_extents(osb,
538 inode,
539 fe);
540 if (num_free_extents < 0) {
541 status = num_free_extents;
542 mlog_errno(status);
543 goto leave;
544 }
545
546 if (!num_free_extents) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700547 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800548 if (status < 0) {
549 if (status != -ENOSPC)
550 mlog_errno(status);
551 goto leave;
552 }
553 }
554
Mark Fashehda5cbf22006-10-06 18:34:35 -0700555 status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 if (status < 0) {
557 if (status != -ENOSPC)
558 mlog_errno(status);
559 goto leave;
560 }
561
562 /* blocks peope in read/write from reading our allocation
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800563 * until we're done changing it. We depend on i_mutex to block
Mark Fashehccd979b2005-12-15 14:31:24 -0800564 * other extend/truncate calls while we're here. Ordering wrt
565 * start_trans is important here -- always do it before! */
566 down_write(&OCFS2_I(inode)->ip_alloc_sem);
567 drop_alloc_sem = 1;
568
569 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700570 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800571 if (IS_ERR(handle)) {
572 status = PTR_ERR(handle);
573 handle = NULL;
574 mlog_errno(status);
575 goto leave;
576 }
577
578restarted_transaction:
579 /* reserve a write to the file entry early on - that we if we
580 * run out of credits in the allocation path, we can still
581 * update i_size. */
582 status = ocfs2_journal_access(handle, inode, bh,
583 OCFS2_JOURNAL_ACCESS_WRITE);
584 if (status < 0) {
585 mlog_errno(status);
586 goto leave;
587 }
588
589 prev_clusters = OCFS2_I(inode)->ip_clusters;
590
591 status = ocfs2_do_extend_allocation(osb,
592 inode,
593 clusters_to_add,
594 bh,
595 handle,
596 data_ac,
597 meta_ac,
598 &why);
599 if ((status < 0) && (status != -EAGAIN)) {
600 if (status != -ENOSPC)
601 mlog_errno(status);
602 goto leave;
603 }
604
605 status = ocfs2_journal_dirty(handle, bh);
606 if (status < 0) {
607 mlog_errno(status);
608 goto leave;
609 }
610
611 spin_lock(&OCFS2_I(inode)->ip_lock);
612 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
613 spin_unlock(&OCFS2_I(inode)->ip_lock);
614
615 if (why != RESTART_NONE && clusters_to_add) {
616 if (why == RESTART_META) {
617 mlog(0, "restarting function.\n");
618 restart_func = 1;
619 } else {
620 BUG_ON(why != RESTART_TRANS);
621
622 mlog(0, "restarting transaction.\n");
623 /* TODO: This can be more intelligent. */
624 credits = ocfs2_calc_extend_credits(osb->sb,
625 fe,
626 clusters_to_add);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700627 status = ocfs2_extend_trans(handle, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 if (status < 0) {
629 /* handle still has to be committed at
630 * this point. */
631 status = -ENOMEM;
632 mlog_errno(status);
633 goto leave;
634 }
635 goto restarted_transaction;
636 }
637 }
638
Mark Fashehb0697052006-03-03 10:24:33 -0800639 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
640 fe->i_clusters, (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800641 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
642 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
643
644leave:
645 if (drop_alloc_sem) {
646 up_write(&OCFS2_I(inode)->ip_alloc_sem);
647 drop_alloc_sem = 0;
648 }
649 if (handle) {
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700650 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 handle = NULL;
652 }
653 if (data_ac) {
654 ocfs2_free_alloc_context(data_ac);
655 data_ac = NULL;
656 }
657 if (meta_ac) {
658 ocfs2_free_alloc_context(meta_ac);
659 meta_ac = NULL;
660 }
661 if ((!status) && restart_func) {
662 restart_func = 0;
663 goto restart_all;
664 }
665 if (bh) {
666 brelse(bh);
667 bh = NULL;
668 }
669
670 mlog_exit(status);
671 return status;
672}
673
674/* Some parts of this taken from generic_cont_expand, which turned out
675 * to be too fragile to do exactly what we need without us having to
Mark Fasheh53013cb2006-05-05 19:04:03 -0700676 * worry about recursive locking in ->prepare_write() and
677 * ->commit_write(). */
Mark Fashehccd979b2005-12-15 14:31:24 -0800678static int ocfs2_write_zero_page(struct inode *inode,
679 u64 size)
680{
681 struct address_space *mapping = inode->i_mapping;
682 struct page *page;
683 unsigned long index;
684 unsigned int offset;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700685 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800686 int ret;
687
688 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
689 /* ugh. in prepare/commit_write, if from==to==start of block, we
690 ** skip the prepare. make sure we never send an offset for the start
691 ** of a block
692 */
693 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
694 offset++;
695 }
696 index = size >> PAGE_CACHE_SHIFT;
697
698 page = grab_cache_page(mapping, index);
699 if (!page) {
700 ret = -ENOMEM;
701 mlog_errno(ret);
702 goto out;
703 }
704
Mark Fasheh53013cb2006-05-05 19:04:03 -0700705 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 if (ret < 0) {
707 mlog_errno(ret);
708 goto out_unlock;
709 }
710
711 if (ocfs2_should_order_data(inode)) {
712 handle = ocfs2_start_walk_page_trans(inode, page, offset,
713 offset);
714 if (IS_ERR(handle)) {
715 ret = PTR_ERR(handle);
716 handle = NULL;
717 goto out_unlock;
718 }
719 }
720
721 /* must not update i_size! */
722 ret = block_commit_write(page, offset, offset);
723 if (ret < 0)
724 mlog_errno(ret);
725 else
726 ret = 0;
727
728 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700729 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800730out_unlock:
731 unlock_page(page);
732 page_cache_release(page);
733out:
734 return ret;
735}
736
737static int ocfs2_zero_extend(struct inode *inode,
738 u64 zero_to_size)
739{
740 int ret = 0;
741 u64 start_off;
742 struct super_block *sb = inode->i_sb;
743
744 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
745 while (start_off < zero_to_size) {
746 ret = ocfs2_write_zero_page(inode, start_off);
747 if (ret < 0) {
748 mlog_errno(ret);
749 goto out;
750 }
751
752 start_off += sb->s_blocksize;
Mark Fashehe2057c52006-10-03 17:53:05 -0700753
754 /*
755 * Very large extends have the potential to lock up
756 * the cpu for extended periods of time.
757 */
758 cond_resched();
Mark Fashehccd979b2005-12-15 14:31:24 -0800759 }
760
761out:
762 return ret;
763}
764
Mark Fasheh53013cb2006-05-05 19:04:03 -0700765/*
766 * A tail_to_skip value > 0 indicates that we're being called from
767 * ocfs2_file_aio_write(). This has the following implications:
768 *
769 * - we don't want to update i_size
770 * - di_bh will be NULL, which is fine because it's only used in the
771 * case where we want to update i_size.
772 * - ocfs2_zero_extend() will then only be filling the hole created
773 * between i_size and the start of the write.
774 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800775static int ocfs2_extend_file(struct inode *inode,
776 struct buffer_head *di_bh,
Mark Fasheh53013cb2006-05-05 19:04:03 -0700777 u64 new_i_size,
778 size_t tail_to_skip)
Mark Fashehccd979b2005-12-15 14:31:24 -0800779{
780 int ret = 0;
781 u32 clusters_to_add;
782
Mark Fasheh53013cb2006-05-05 19:04:03 -0700783 BUG_ON(!tail_to_skip && !di_bh);
784
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 /* setattr sometimes calls us like this. */
786 if (new_i_size == 0)
787 goto out;
788
789 if (i_size_read(inode) == new_i_size)
790 goto out;
791 BUG_ON(new_i_size < i_size_read(inode));
792
793 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
794 OCFS2_I(inode)->ip_clusters;
795
Mark Fasheh0effef72006-10-03 17:44:42 -0700796 /*
797 * protect the pages that ocfs2_zero_extend is going to be
798 * pulling into the page cache.. we do this before the
799 * metadata extend so that we don't get into the situation
800 * where we've extended the metadata but can't get the data
801 * lock to zero.
802 */
803 ret = ocfs2_data_lock(inode, 1);
804 if (ret < 0) {
805 mlog_errno(ret);
806 goto out;
807 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700808
Mark Fasheh0effef72006-10-03 17:44:42 -0700809 if (clusters_to_add) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800810 ret = ocfs2_extend_allocation(inode, clusters_to_add);
811 if (ret < 0) {
812 mlog_errno(ret);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700813 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 }
Mark Fasheh0effef72006-10-03 17:44:42 -0700815 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800816
Mark Fasheh0effef72006-10-03 17:44:42 -0700817 /*
818 * Call this even if we don't add any clusters to the tree. We
819 * still need to zero the area between the old i_size and the
820 * new i_size.
821 */
822 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
823 if (ret < 0) {
824 mlog_errno(ret);
825 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 }
827
Mark Fasheh53013cb2006-05-05 19:04:03 -0700828 if (!tail_to_skip) {
829 /* We're being called from ocfs2_setattr() which wants
830 * us to update i_size */
831 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
832 if (ret < 0)
833 mlog_errno(ret);
834 }
835
836out_unlock:
Mark Fasheh0effef72006-10-03 17:44:42 -0700837 ocfs2_data_unlock(inode, 1);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700838
Mark Fashehccd979b2005-12-15 14:31:24 -0800839out:
840 return ret;
841}
842
843int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
844{
845 int status = 0, size_change;
846 struct inode *inode = dentry->d_inode;
847 struct super_block *sb = inode->i_sb;
848 struct ocfs2_super *osb = OCFS2_SB(sb);
849 struct buffer_head *bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700850 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800851
852 mlog_entry("(0x%p, '%.*s')\n", dentry,
853 dentry->d_name.len, dentry->d_name.name);
854
855 if (attr->ia_valid & ATTR_MODE)
856 mlog(0, "mode change: %d\n", attr->ia_mode);
857 if (attr->ia_valid & ATTR_UID)
858 mlog(0, "uid change: %d\n", attr->ia_uid);
859 if (attr->ia_valid & ATTR_GID)
860 mlog(0, "gid change: %d\n", attr->ia_gid);
861 if (attr->ia_valid & ATTR_SIZE)
862 mlog(0, "size change...\n");
863 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
864 mlog(0, "time change...\n");
865
866#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
867 | ATTR_GID | ATTR_UID | ATTR_MODE)
868 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
869 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
870 return 0;
871 }
872
873 status = inode_change_ok(inode, attr);
874 if (status)
875 return status;
876
877 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
878 if (size_change) {
879 status = ocfs2_rw_lock(inode, 1);
880 if (status < 0) {
881 mlog_errno(status);
882 goto bail;
883 }
884 }
885
Mark Fasheh4bcec182006-10-09 16:02:40 -0700886 status = ocfs2_meta_lock(inode, &bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800887 if (status < 0) {
888 if (status != -ENOENT)
889 mlog_errno(status);
890 goto bail_unlock_rw;
891 }
892
893 if (size_change && attr->ia_size != i_size_read(inode)) {
894 if (i_size_read(inode) > attr->ia_size)
895 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
896 else
Mark Fasheh53013cb2006-05-05 19:04:03 -0700897 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 if (status < 0) {
899 if (status != -ENOSPC)
900 mlog_errno(status);
901 status = -ENOSPC;
902 goto bail_unlock;
903 }
904 }
905
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700906 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 if (IS_ERR(handle)) {
908 status = PTR_ERR(handle);
909 mlog_errno(status);
910 goto bail_unlock;
911 }
912
913 status = inode_setattr(inode, attr);
914 if (status < 0) {
915 mlog_errno(status);
916 goto bail_commit;
917 }
918
919 status = ocfs2_mark_inode_dirty(handle, inode, bh);
920 if (status < 0)
921 mlog_errno(status);
922
923bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700924 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800925bail_unlock:
926 ocfs2_meta_unlock(inode, 1);
927bail_unlock_rw:
928 if (size_change)
929 ocfs2_rw_unlock(inode, 1);
930bail:
931 if (bh)
932 brelse(bh);
933
934 mlog_exit(status);
935 return status;
936}
937
938int ocfs2_getattr(struct vfsmount *mnt,
939 struct dentry *dentry,
940 struct kstat *stat)
941{
942 struct inode *inode = dentry->d_inode;
943 struct super_block *sb = dentry->d_inode->i_sb;
944 struct ocfs2_super *osb = sb->s_fs_info;
945 int err;
946
947 mlog_entry_void();
948
949 err = ocfs2_inode_revalidate(dentry);
950 if (err) {
951 if (err != -ENOENT)
952 mlog_errno(err);
953 goto bail;
954 }
955
956 generic_fillattr(inode, stat);
957
958 /* We set the blksize from the cluster size for performance */
959 stat->blksize = osb->s_clustersize;
960
961bail:
962 mlog_exit(err);
963
964 return err;
965}
966
Tiger Yangd38eb8d2006-11-27 09:59:21 +0800967int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
968{
969 int ret;
970
971 mlog_entry_void();
972
973 ret = ocfs2_meta_lock(inode, NULL, 0);
974 if (ret) {
Mark Fasheha9f5f702007-04-26 11:43:43 -0700975 if (ret != -ENOENT)
976 mlog_errno(ret);
Tiger Yangd38eb8d2006-11-27 09:59:21 +0800977 goto out;
978 }
979
980 ret = generic_permission(inode, mask, NULL);
Tiger Yangd38eb8d2006-11-27 09:59:21 +0800981
982 ocfs2_meta_unlock(inode, 0);
983out:
984 mlog_exit(ret);
985 return ret;
986}
987
Mark Fashehccd979b2005-12-15 14:31:24 -0800988static int ocfs2_write_remove_suid(struct inode *inode)
989{
990 int ret;
991 struct buffer_head *bh = NULL;
992 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700993 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
995 struct ocfs2_dinode *di;
996
Mark Fashehb0697052006-03-03 10:24:33 -0800997 mlog_entry("(Inode %llu, mode 0%o)\n",
998 (unsigned long long)oi->ip_blkno, inode->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800999
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001000 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001001 if (handle == NULL) {
1002 ret = -ENOMEM;
1003 mlog_errno(ret);
1004 goto out;
1005 }
1006
1007 ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1008 if (ret < 0) {
1009 mlog_errno(ret);
1010 goto out_trans;
1011 }
1012
1013 ret = ocfs2_journal_access(handle, inode, bh,
1014 OCFS2_JOURNAL_ACCESS_WRITE);
1015 if (ret < 0) {
1016 mlog_errno(ret);
1017 goto out_bh;
1018 }
1019
1020 inode->i_mode &= ~S_ISUID;
1021 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1022 inode->i_mode &= ~S_ISGID;
1023
1024 di = (struct ocfs2_dinode *) bh->b_data;
1025 di->i_mode = cpu_to_le16(inode->i_mode);
1026
1027 ret = ocfs2_journal_dirty(handle, bh);
1028 if (ret < 0)
1029 mlog_errno(ret);
1030out_bh:
1031 brelse(bh);
1032out_trans:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001033 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001034out:
1035 mlog_exit(ret);
1036 return ret;
1037}
1038
Tiger Yang8659ac22006-10-17 18:29:52 -07001039static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1040 loff_t *ppos,
1041 size_t count,
1042 int appending)
Mark Fashehccd979b2005-12-15 14:31:24 -08001043{
Tiger Yang8659ac22006-10-17 18:29:52 -07001044 int ret = 0, meta_level = appending;
1045 struct inode *inode = dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 u32 clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 loff_t newsize, saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001048
Mark Fashehccd979b2005-12-15 14:31:24 -08001049 /*
1050 * We sample i_size under a read level meta lock to see if our write
1051 * is extending the file, if it is we back off and get a write level
1052 * meta lock.
1053 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 for(;;) {
Mark Fasheh4bcec182006-10-09 16:02:40 -07001055 ret = ocfs2_meta_lock(inode, NULL, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 if (ret < 0) {
1057 meta_level = -1;
1058 mlog_errno(ret);
1059 goto out;
1060 }
1061
1062 /* Clear suid / sgid if necessary. We do this here
1063 * instead of later in the write path because
1064 * remove_suid() calls ->setattr without any hint that
1065 * we may have already done our cluster locking. Since
1066 * ocfs2_setattr() *must* take cluster locks to
1067 * proceeed, this will lead us to recursively lock the
1068 * inode. There's also the dinode i_size state which
1069 * can be lost via setattr during extending writes (we
1070 * set inode->i_size at the end of a write. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001071 if (should_remove_suid(dentry)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 if (meta_level == 0) {
1073 ocfs2_meta_unlock(inode, meta_level);
1074 meta_level = 1;
1075 continue;
1076 }
1077
1078 ret = ocfs2_write_remove_suid(inode);
1079 if (ret < 0) {
1080 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001081 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 }
1083 }
1084
1085 /* work on a copy of ppos until we're sure that we won't have
1086 * to recalculate it due to relocking. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001087 if (appending) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 saved_pos = i_size_read(inode);
1089 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1090 } else {
Tiger Yang8659ac22006-10-17 18:29:52 -07001091 saved_pos = *ppos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 }
Tiger Yang8659ac22006-10-17 18:29:52 -07001093 newsize = count + saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001094
Mark Fasheh215c7f92006-02-01 16:42:10 -08001095 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1096 (long long) saved_pos, (long long) newsize,
1097 (long long) i_size_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001098
1099 /* No need for a higher level metadata lock if we're
1100 * never going past i_size. */
1101 if (newsize <= i_size_read(inode))
1102 break;
1103
1104 if (meta_level == 0) {
1105 ocfs2_meta_unlock(inode, meta_level);
1106 meta_level = 1;
1107 continue;
1108 }
1109
1110 spin_lock(&OCFS2_I(inode)->ip_lock);
1111 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1112 OCFS2_I(inode)->ip_clusters;
1113 spin_unlock(&OCFS2_I(inode)->ip_lock);
1114
1115 mlog(0, "Writing at EOF, may need more allocation: "
Mark Fasheh215c7f92006-02-01 16:42:10 -08001116 "i_size = %lld, newsize = %lld, need %u clusters\n",
1117 (long long) i_size_read(inode), (long long) newsize,
1118 clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08001119
1120 /* We only want to continue the rest of this loop if
1121 * our extend will actually require more
1122 * allocation. */
1123 if (!clusters)
1124 break;
1125
Tiger Yang8659ac22006-10-17 18:29:52 -07001126 ret = ocfs2_extend_file(inode, NULL, newsize, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001127 if (ret < 0) {
1128 if (ret != -ENOSPC)
1129 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001130 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001131 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 break;
1133 }
1134
Tiger Yang8659ac22006-10-17 18:29:52 -07001135 if (appending)
1136 *ppos = saved_pos;
1137
1138out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001139 ocfs2_meta_unlock(inode, meta_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07001140
1141out:
1142 return ret;
1143}
1144
1145static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1146 const struct iovec *iov,
1147 unsigned long nr_segs,
1148 loff_t pos)
1149{
1150 int ret, rw_level, have_alloc_sem = 0;
1151 struct file *filp = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -08001152 struct inode *inode = filp->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001153 int appending = filp->f_flags & O_APPEND ? 1 : 0;
1154
1155 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1156 (unsigned int)nr_segs,
Josef Sipekd28c9172006-12-08 02:37:25 -08001157 filp->f_path.dentry->d_name.len,
1158 filp->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001159
1160 /* happy write of zero bytes */
1161 if (iocb->ki_left == 0)
1162 return 0;
1163
1164 mutex_lock(&inode->i_mutex);
1165 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1166 if (filp->f_flags & O_DIRECT) {
1167 have_alloc_sem = 1;
1168 down_read(&inode->i_alloc_sem);
1169 }
1170
1171 /* concurrent O_DIRECT writes are allowed */
1172 rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
1173 ret = ocfs2_rw_lock(inode, rw_level);
1174 if (ret < 0) {
1175 rw_level = -1;
1176 mlog_errno(ret);
1177 goto out;
1178 }
1179
Josef Sipekd28c9172006-12-08 02:37:25 -08001180 ret = ocfs2_prepare_inode_for_write(filp->f_path.dentry, &iocb->ki_pos,
Tiger Yang8659ac22006-10-17 18:29:52 -07001181 iocb->ki_left, appending);
1182 if (ret < 0) {
1183 mlog_errno(ret);
1184 goto out;
1185 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001186
1187 /* communicate with ocfs2_dio_end_io */
1188 ocfs2_iocb_set_rw_locked(iocb);
1189
Badari Pulavarty027445c2006-09-30 23:28:46 -07001190 ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001191
1192 /* buffered aio wouldn't have proper lock coverage today */
1193 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1194
1195 /*
1196 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1197 * function pointer which is called when o_direct io completes so that
1198 * it can unlock our rw lock. (it's the clustered equivalent of
1199 * i_alloc_sem; protects truncate from racing with pending ios).
1200 * Unfortunately there are error cases which call end_io and others
1201 * that don't. so we don't have to unlock the rw_lock if either an
1202 * async dio is going to do it in the future or an end_io after an
1203 * error has already done it.
1204 */
1205 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1206 rw_level = -1;
1207 have_alloc_sem = 0;
1208 }
1209
1210out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001211 if (have_alloc_sem)
1212 up_read(&inode->i_alloc_sem);
1213 if (rw_level != -1)
1214 ocfs2_rw_unlock(inode, rw_level);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001215 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001216
1217 mlog_exit(ret);
1218 return ret;
1219}
1220
Tiger Yang8659ac22006-10-17 18:29:52 -07001221static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1222 struct file *out,
1223 loff_t *ppos,
1224 size_t len,
1225 unsigned int flags)
1226{
1227 int ret;
Josef Sipekd28c9172006-12-08 02:37:25 -08001228 struct inode *inode = out->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001229
1230 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1231 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08001232 out->f_path.dentry->d_name.len,
1233 out->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001234
1235 inode_double_lock(inode, pipe->inode);
1236
1237 ret = ocfs2_rw_lock(inode, 1);
1238 if (ret < 0) {
1239 mlog_errno(ret);
1240 goto out;
1241 }
1242
Josef Sipekd28c9172006-12-08 02:37:25 -08001243 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0);
Tiger Yang8659ac22006-10-17 18:29:52 -07001244 if (ret < 0) {
1245 mlog_errno(ret);
1246 goto out_unlock;
1247 }
1248
1249 /* ok, we're done with i_size and alloc work */
1250 ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
1251
1252out_unlock:
1253 ocfs2_rw_unlock(inode, 1);
1254out:
1255 inode_double_unlock(inode, pipe->inode);
1256
1257 mlog_exit(ret);
1258 return ret;
1259}
1260
1261static ssize_t ocfs2_file_splice_read(struct file *in,
1262 loff_t *ppos,
1263 struct pipe_inode_info *pipe,
1264 size_t len,
1265 unsigned int flags)
1266{
1267 int ret = 0;
Josef Sipekd28c9172006-12-08 02:37:25 -08001268 struct inode *inode = in->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07001269
1270 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1271 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08001272 in->f_path.dentry->d_name.len,
1273 in->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001274
1275 /*
1276 * See the comment in ocfs2_file_aio_read()
1277 */
1278 ret = ocfs2_meta_lock(inode, NULL, 0);
1279 if (ret < 0) {
1280 mlog_errno(ret);
1281 goto bail;
1282 }
1283 ocfs2_meta_unlock(inode, 0);
1284
1285 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1286
1287bail:
1288 mlog_exit(ret);
1289 return ret;
1290}
1291
Mark Fashehccd979b2005-12-15 14:31:24 -08001292static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001293 const struct iovec *iov,
1294 unsigned long nr_segs,
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 loff_t pos)
1296{
Tiger Yang25899de2006-11-15 15:49:02 +08001297 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001298 struct file *filp = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -08001299 struct inode *inode = filp->f_path.dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001300
Badari Pulavarty027445c2006-09-30 23:28:46 -07001301 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1302 (unsigned int)nr_segs,
Josef Sipekd28c9172006-12-08 02:37:25 -08001303 filp->f_path.dentry->d_name.len,
1304 filp->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001305
1306 if (!inode) {
1307 ret = -EINVAL;
1308 mlog_errno(ret);
1309 goto bail;
1310 }
1311
Mark Fashehccd979b2005-12-15 14:31:24 -08001312 /*
1313 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
1314 * need locks to protect pending reads from racing with truncate.
1315 */
1316 if (filp->f_flags & O_DIRECT) {
1317 down_read(&inode->i_alloc_sem);
1318 have_alloc_sem = 1;
1319
1320 ret = ocfs2_rw_lock(inode, 0);
1321 if (ret < 0) {
1322 mlog_errno(ret);
1323 goto bail;
1324 }
1325 rw_level = 0;
1326 /* communicate with ocfs2_dio_end_io */
1327 ocfs2_iocb_set_rw_locked(iocb);
1328 }
1329
Mark Fashehc4374f82006-05-05 19:04:35 -07001330 /*
1331 * We're fine letting folks race truncates and extending
1332 * writes with read across the cluster, just like they can
1333 * locally. Hence no rw_lock during read.
1334 *
1335 * Take and drop the meta data lock to update inode fields
1336 * like i_size. This allows the checks down below
1337 * generic_file_aio_read() a chance of actually working.
1338 */
Tiger Yang25899de2006-11-15 15:49:02 +08001339 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07001340 if (ret < 0) {
1341 mlog_errno(ret);
1342 goto bail;
1343 }
Tiger Yang25899de2006-11-15 15:49:02 +08001344 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07001345
Badari Pulavarty027445c2006-09-30 23:28:46 -07001346 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 if (ret == -EINVAL)
1348 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1349
1350 /* buffered aio wouldn't have proper lock coverage today */
1351 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1352
1353 /* see ocfs2_file_aio_write */
1354 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1355 rw_level = -1;
1356 have_alloc_sem = 0;
1357 }
1358
1359bail:
1360 if (have_alloc_sem)
1361 up_read(&inode->i_alloc_sem);
1362 if (rw_level != -1)
1363 ocfs2_rw_unlock(inode, rw_level);
1364 mlog_exit(ret);
1365
1366 return ret;
1367}
1368
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001369const struct inode_operations ocfs2_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 .setattr = ocfs2_setattr,
1371 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001372 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08001373};
1374
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001375const struct inode_operations ocfs2_special_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001376 .setattr = ocfs2_setattr,
1377 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001378 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08001379};
1380
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001381const struct file_operations ocfs2_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001382 .read = do_sync_read,
1383 .write = do_sync_write,
1384 .sendfile = generic_file_sendfile,
1385 .mmap = ocfs2_mmap,
1386 .fsync = ocfs2_sync_file,
1387 .release = ocfs2_file_release,
1388 .open = ocfs2_file_open,
1389 .aio_read = ocfs2_file_aio_read,
1390 .aio_write = ocfs2_file_aio_write,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001391 .ioctl = ocfs2_ioctl,
Tiger Yang8659ac22006-10-17 18:29:52 -07001392 .splice_read = ocfs2_file_splice_read,
1393 .splice_write = ocfs2_file_splice_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08001394};
1395
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001396const struct file_operations ocfs2_dops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001397 .read = generic_read_dir,
1398 .readdir = ocfs2_readdir,
1399 .fsync = ocfs2_sync_file,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001400 .ioctl = ocfs2_ioctl,
Mark Fashehccd979b2005-12-15 14:31:24 -08001401};