]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/file.c
7e34e66159c690cb13beb43abaebe58b8f285333
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / file.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * file.c
5  *
6  * File open, close, extend, truncate
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  */
25
26 #include <linux/capability.h>
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/pagemap.h>
32 #include <linux/uio.h>
33 #include <linux/sched.h>
34 #include <linux/splice.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37 #include <linux/falloc.h>
38
39 #define MLOG_MASK_PREFIX ML_INODE
40 #include <cluster/masklog.h>
41
42 #include "ocfs2.h"
43
44 #include "alloc.h"
45 #include "aops.h"
46 #include "dir.h"
47 #include "dlmglue.h"
48 #include "extent_map.h"
49 #include "file.h"
50 #include "sysfile.h"
51 #include "inode.h"
52 #include "ioctl.h"
53 #include "journal.h"
54 #include "mmap.h"
55 #include "suballoc.h"
56 #include "super.h"
57
58 #include "buffer_head_io.h"
59
60 static int ocfs2_sync_inode(struct inode *inode)
61 {
62         filemap_fdatawrite(inode->i_mapping);
63         return sync_mapping_buffers(inode->i_mapping);
64 }
65
66 static int ocfs2_file_open(struct inode *inode, struct file *file)
67 {
68         int status;
69         int mode = file->f_flags;
70         struct ocfs2_inode_info *oi = OCFS2_I(inode);
71
72         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
73                    file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
74
75         spin_lock(&oi->ip_lock);
76
77         /* Check that the inode hasn't been wiped from disk by another
78          * node. If it hasn't then we're safe as long as we hold the
79          * spin lock until our increment of open count. */
80         if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
81                 spin_unlock(&oi->ip_lock);
82
83                 status = -ENOENT;
84                 goto leave;
85         }
86
87         if (mode & O_DIRECT)
88                 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
89
90         oi->ip_open_count++;
91         spin_unlock(&oi->ip_lock);
92         status = 0;
93 leave:
94         mlog_exit(status);
95         return status;
96 }
97
98 static int ocfs2_file_release(struct inode *inode, struct file *file)
99 {
100         struct ocfs2_inode_info *oi = OCFS2_I(inode);
101
102         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
103                        file->f_path.dentry->d_name.len,
104                        file->f_path.dentry->d_name.name);
105
106         spin_lock(&oi->ip_lock);
107         if (!--oi->ip_open_count)
108                 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
109         spin_unlock(&oi->ip_lock);
110
111         mlog_exit(0);
112
113         return 0;
114 }
115
116 static int ocfs2_sync_file(struct file *file,
117                            struct dentry *dentry,
118                            int datasync)
119 {
120         int err = 0;
121         journal_t *journal;
122         struct inode *inode = dentry->d_inode;
123         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
124
125         mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
126                    dentry->d_name.len, dentry->d_name.name);
127
128         err = ocfs2_sync_inode(dentry->d_inode);
129         if (err)
130                 goto bail;
131
132         journal = osb->journal->j_journal;
133         err = journal_force_commit(journal);
134
135 bail:
136         mlog_exit(err);
137
138         return (err < 0) ? -EIO : 0;
139 }
140
141 int ocfs2_should_update_atime(struct inode *inode,
142                               struct vfsmount *vfsmnt)
143 {
144         struct timespec now;
145         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
146
147         if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
148                 return 0;
149
150         if ((inode->i_flags & S_NOATIME) ||
151             ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
152                 return 0;
153
154         /*
155          * We can be called with no vfsmnt structure - NFSD will
156          * sometimes do this.
157          *
158          * Note that our action here is different than touch_atime() -
159          * if we can't tell whether this is a noatime mount, then we
160          * don't know whether to trust the value of s_atime_quantum.
161          */
162         if (vfsmnt == NULL)
163                 return 0;
164
165         if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
166             ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
167                 return 0;
168
169         if (vfsmnt->mnt_flags & MNT_RELATIME) {
170                 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
171                     (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
172                         return 1;
173
174                 return 0;
175         }
176
177         now = CURRENT_TIME;
178         if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
179                 return 0;
180         else
181                 return 1;
182 }
183
184 int ocfs2_update_inode_atime(struct inode *inode,
185                              struct buffer_head *bh)
186 {
187         int ret;
188         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
189         handle_t *handle;
190         struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
191
192         mlog_entry_void();
193
194         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
195         if (handle == NULL) {
196                 ret = -ENOMEM;
197                 mlog_errno(ret);
198                 goto out;
199         }
200
201         ret = ocfs2_journal_access(handle, inode, bh,
202                                    OCFS2_JOURNAL_ACCESS_WRITE);
203         if (ret) {
204                 mlog_errno(ret);
205                 goto out_commit;
206         }
207
208         /*
209          * Don't use ocfs2_mark_inode_dirty() here as we don't always
210          * have i_mutex to guard against concurrent changes to other
211          * inode fields.
212          */
213         inode->i_atime = CURRENT_TIME;
214         di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
215         di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
216
217         ret = ocfs2_journal_dirty(handle, bh);
218         if (ret < 0)
219                 mlog_errno(ret);
220
221 out_commit:
222         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
223 out:
224         mlog_exit(ret);
225         return ret;
226 }
227
228 static int ocfs2_set_inode_size(handle_t *handle,
229                                 struct inode *inode,
230                                 struct buffer_head *fe_bh,
231                                 u64 new_i_size)
232 {
233         int status;
234
235         mlog_entry_void();
236         i_size_write(inode, new_i_size);
237         inode->i_blocks = ocfs2_inode_sector_count(inode);
238         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
239
240         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
241         if (status < 0) {
242                 mlog_errno(status);
243                 goto bail;
244         }
245
246 bail:
247         mlog_exit(status);
248         return status;
249 }
250
251 static int ocfs2_simple_size_update(struct inode *inode,
252                                     struct buffer_head *di_bh,
253                                     u64 new_i_size)
254 {
255         int ret;
256         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
257         handle_t *handle = NULL;
258
259         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
260         if (handle == NULL) {
261                 ret = -ENOMEM;
262                 mlog_errno(ret);
263                 goto out;
264         }
265
266         ret = ocfs2_set_inode_size(handle, inode, di_bh,
267                                    new_i_size);
268         if (ret < 0)
269                 mlog_errno(ret);
270
271         ocfs2_commit_trans(osb, handle);
272 out:
273         return ret;
274 }
275
276 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
277                                      struct inode *inode,
278                                      struct buffer_head *fe_bh,
279                                      u64 new_i_size)
280 {
281         int status;
282         handle_t *handle;
283         struct ocfs2_dinode *di;
284         u64 cluster_bytes;
285
286         mlog_entry_void();
287
288         /* TODO: This needs to actually orphan the inode in this
289          * transaction. */
290
291         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
292         if (IS_ERR(handle)) {
293                 status = PTR_ERR(handle);
294                 mlog_errno(status);
295                 goto out;
296         }
297
298         status = ocfs2_journal_access(handle, inode, fe_bh,
299                                       OCFS2_JOURNAL_ACCESS_WRITE);
300         if (status < 0) {
301                 mlog_errno(status);
302                 goto out_commit;
303         }
304
305         /*
306          * Do this before setting i_size.
307          */
308         cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
309         status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
310                                                cluster_bytes);
311         if (status) {
312                 mlog_errno(status);
313                 goto out_commit;
314         }
315
316         i_size_write(inode, new_i_size);
317         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
318
319         di = (struct ocfs2_dinode *) fe_bh->b_data;
320         di->i_size = cpu_to_le64(new_i_size);
321         di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
322         di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
323
324         status = ocfs2_journal_dirty(handle, fe_bh);
325         if (status < 0)
326                 mlog_errno(status);
327
328 out_commit:
329         ocfs2_commit_trans(osb, handle);
330 out:
331
332         mlog_exit(status);
333         return status;
334 }
335
336 static int ocfs2_truncate_file(struct inode *inode,
337                                struct buffer_head *di_bh,
338                                u64 new_i_size)
339 {
340         int status = 0;
341         struct ocfs2_dinode *fe = NULL;
342         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
343         struct ocfs2_truncate_context *tc = NULL;
344
345         mlog_entry("(inode = %llu, new_i_size = %llu\n",
346                    (unsigned long long)OCFS2_I(inode)->ip_blkno,
347                    (unsigned long long)new_i_size);
348
349         fe = (struct ocfs2_dinode *) di_bh->b_data;
350         if (!OCFS2_IS_VALID_DINODE(fe)) {
351                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
352                 status = -EIO;
353                 goto bail;
354         }
355
356         mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
357                         "Inode %llu, inode i_size = %lld != di "
358                         "i_size = %llu, i_flags = 0x%x\n",
359                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
360                         i_size_read(inode),
361                         (unsigned long long)le64_to_cpu(fe->i_size),
362                         le32_to_cpu(fe->i_flags));
363
364         if (new_i_size > le64_to_cpu(fe->i_size)) {
365                 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
366                      (unsigned long long)le64_to_cpu(fe->i_size),
367                      (unsigned long long)new_i_size);
368                 status = -EINVAL;
369                 mlog_errno(status);
370                 goto bail;
371         }
372
373         mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
374              (unsigned long long)le64_to_cpu(fe->i_blkno),
375              (unsigned long long)le64_to_cpu(fe->i_size),
376              (unsigned long long)new_i_size);
377
378         /* lets handle the simple truncate cases before doing any more
379          * cluster locking. */
380         if (new_i_size == le64_to_cpu(fe->i_size))
381                 goto bail;
382
383         down_write(&OCFS2_I(inode)->ip_alloc_sem);
384
385         /* This forces other nodes to sync and drop their pages. Do
386          * this even if we have a truncate without allocation change -
387          * ocfs2 cluster sizes can be much greater than page size, so
388          * we have to truncate them anyway.  */
389         status = ocfs2_data_lock(inode, 1);
390         if (status < 0) {
391                 up_write(&OCFS2_I(inode)->ip_alloc_sem);
392
393                 mlog_errno(status);
394                 goto bail;
395         }
396
397         unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
398         truncate_inode_pages(inode->i_mapping, new_i_size);
399
400         /* alright, we're going to need to do a full blown alloc size
401          * change. Orphan the inode so that recovery can complete the
402          * truncate if necessary. This does the task of marking
403          * i_size. */
404         status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
405         if (status < 0) {
406                 mlog_errno(status);
407                 goto bail_unlock_data;
408         }
409
410         status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
411         if (status < 0) {
412                 mlog_errno(status);
413                 goto bail_unlock_data;
414         }
415
416         status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
417         if (status < 0) {
418                 mlog_errno(status);
419                 goto bail_unlock_data;
420         }
421
422         /* TODO: orphan dir cleanup here. */
423 bail_unlock_data:
424         ocfs2_data_unlock(inode, 1);
425
426         up_write(&OCFS2_I(inode)->ip_alloc_sem);
427
428 bail:
429
430         mlog_exit(status);
431         return status;
432 }
433
434 /*
435  * extend allocation only here.
436  * we'll update all the disk stuff, and oip->alloc_size
437  *
438  * expect stuff to be locked, a transaction started and enough data /
439  * metadata reservations in the contexts.
440  *
441  * Will return -EAGAIN, and a reason if a restart is needed.
442  * If passed in, *reason will always be set, even in error.
443  */
444 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
445                                struct inode *inode,
446                                u32 *logical_offset,
447                                u32 clusters_to_add,
448                                int mark_unwritten,
449                                struct buffer_head *fe_bh,
450                                handle_t *handle,
451                                struct ocfs2_alloc_context *data_ac,
452                                struct ocfs2_alloc_context *meta_ac,
453                                enum ocfs2_alloc_restarted *reason_ret)
454 {
455         int status = 0;
456         int free_extents;
457         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
458         enum ocfs2_alloc_restarted reason = RESTART_NONE;
459         u32 bit_off, num_bits;
460         u64 block;
461         u8 flags = 0;
462
463         BUG_ON(!clusters_to_add);
464
465         if (mark_unwritten)
466                 flags = OCFS2_EXT_UNWRITTEN;
467
468         free_extents = ocfs2_num_free_extents(osb, inode, fe);
469         if (free_extents < 0) {
470                 status = free_extents;
471                 mlog_errno(status);
472                 goto leave;
473         }
474
475         /* there are two cases which could cause us to EAGAIN in the
476          * we-need-more-metadata case:
477          * 1) we haven't reserved *any*
478          * 2) we are so fragmented, we've needed to add metadata too
479          *    many times. */
480         if (!free_extents && !meta_ac) {
481                 mlog(0, "we haven't reserved any metadata!\n");
482                 status = -EAGAIN;
483                 reason = RESTART_META;
484                 goto leave;
485         } else if ((!free_extents)
486                    && (ocfs2_alloc_context_bits_left(meta_ac)
487                        < ocfs2_extend_meta_needed(fe))) {
488                 mlog(0, "filesystem is really fragmented...\n");
489                 status = -EAGAIN;
490                 reason = RESTART_META;
491                 goto leave;
492         }
493
494         status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
495                                       &bit_off, &num_bits);
496         if (status < 0) {
497                 if (status != -ENOSPC)
498                         mlog_errno(status);
499                 goto leave;
500         }
501
502         BUG_ON(num_bits > clusters_to_add);
503
504         /* reserve our write early -- insert_extent may update the inode */
505         status = ocfs2_journal_access(handle, inode, fe_bh,
506                                       OCFS2_JOURNAL_ACCESS_WRITE);
507         if (status < 0) {
508                 mlog_errno(status);
509                 goto leave;
510         }
511
512         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
513         mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
514              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
515         status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
516                                      *logical_offset, block, num_bits,
517                                      flags, meta_ac);
518         if (status < 0) {
519                 mlog_errno(status);
520                 goto leave;
521         }
522
523         status = ocfs2_journal_dirty(handle, fe_bh);
524         if (status < 0) {
525                 mlog_errno(status);
526                 goto leave;
527         }
528
529         clusters_to_add -= num_bits;
530         *logical_offset += num_bits;
531
532         if (clusters_to_add) {
533                 mlog(0, "need to alloc once more, clusters = %u, wanted = "
534                      "%u\n", fe->i_clusters, clusters_to_add);
535                 status = -EAGAIN;
536                 reason = RESTART_TRANS;
537         }
538
539 leave:
540         mlog_exit(status);
541         if (reason_ret)
542                 *reason_ret = reason;
543         return status;
544 }
545
546 /*
547  * For a given allocation, determine which allocators will need to be
548  * accessed, and lock them, reserving the appropriate number of bits.
549  *
550  * Sparse file systems call this from ocfs2_write_begin_nolock()
551  * and ocfs2_allocate_unwritten_extents().
552  *
553  * File systems which don't support holes call this from
554  * ocfs2_extend_allocation().
555  */
556 int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
557                           u32 clusters_to_add, u32 extents_to_split,
558                           struct ocfs2_alloc_context **data_ac,
559                           struct ocfs2_alloc_context **meta_ac)
560 {
561         int ret = 0, num_free_extents;
562         unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
563         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
564
565         *meta_ac = NULL;
566         if (data_ac)
567                 *data_ac = NULL;
568
569         BUG_ON(clusters_to_add != 0 && data_ac == NULL);
570
571         mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
572              "clusters_to_add = %u, extents_to_split = %u\n",
573              (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
574              le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
575
576         num_free_extents = ocfs2_num_free_extents(osb, inode, di);
577         if (num_free_extents < 0) {
578                 ret = num_free_extents;
579                 mlog_errno(ret);
580                 goto out;
581         }
582
583         /*
584          * Sparse allocation file systems need to be more conservative
585          * with reserving room for expansion - the actual allocation
586          * happens while we've got a journal handle open so re-taking
587          * a cluster lock (because we ran out of room for another
588          * extent) will violate ordering rules.
589          *
590          * Most of the time we'll only be seeing this 1 cluster at a time
591          * anyway.
592          *
593          * Always lock for any unwritten extents - we might want to
594          * add blocks during a split.
595          */
596         if (!num_free_extents ||
597             (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
598                 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
599                 if (ret < 0) {
600                         if (ret != -ENOSPC)
601                                 mlog_errno(ret);
602                         goto out;
603                 }
604         }
605
606         if (clusters_to_add == 0)
607                 goto out;
608
609         ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
610         if (ret < 0) {
611                 if (ret != -ENOSPC)
612                         mlog_errno(ret);
613                 goto out;
614         }
615
616 out:
617         if (ret) {
618                 if (*meta_ac) {
619                         ocfs2_free_alloc_context(*meta_ac);
620                         *meta_ac = NULL;
621                 }
622
623                 /*
624                  * We cannot have an error and a non null *data_ac.
625                  */
626         }
627
628         return ret;
629 }
630
631 static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
632                                      u32 clusters_to_add, int mark_unwritten)
633 {
634         int status = 0;
635         int restart_func = 0;
636         int credits;
637         u32 prev_clusters;
638         struct buffer_head *bh = NULL;
639         struct ocfs2_dinode *fe = NULL;
640         handle_t *handle = NULL;
641         struct ocfs2_alloc_context *data_ac = NULL;
642         struct ocfs2_alloc_context *meta_ac = NULL;
643         enum ocfs2_alloc_restarted why;
644         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
645
646         mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
647
648         /*
649          * This function only exists for file systems which don't
650          * support holes.
651          */
652         BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
653
654         status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
655                                   OCFS2_BH_CACHED, inode);
656         if (status < 0) {
657                 mlog_errno(status);
658                 goto leave;
659         }
660
661         fe = (struct ocfs2_dinode *) bh->b_data;
662         if (!OCFS2_IS_VALID_DINODE(fe)) {
663                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
664                 status = -EIO;
665                 goto leave;
666         }
667
668 restart_all:
669         BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
670
671         status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
672                                        &meta_ac);
673         if (status) {
674                 mlog_errno(status);
675                 goto leave;
676         }
677
678         credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
679         handle = ocfs2_start_trans(osb, credits);
680         if (IS_ERR(handle)) {
681                 status = PTR_ERR(handle);
682                 handle = NULL;
683                 mlog_errno(status);
684                 goto leave;
685         }
686
687 restarted_transaction:
688         /* reserve a write to the file entry early on - that we if we
689          * run out of credits in the allocation path, we can still
690          * update i_size. */
691         status = ocfs2_journal_access(handle, inode, bh,
692                                       OCFS2_JOURNAL_ACCESS_WRITE);
693         if (status < 0) {
694                 mlog_errno(status);
695                 goto leave;
696         }
697
698         prev_clusters = OCFS2_I(inode)->ip_clusters;
699
700         status = ocfs2_do_extend_allocation(osb,
701                                             inode,
702                                             &logical_start,
703                                             clusters_to_add,
704                                             mark_unwritten,
705                                             bh,
706                                             handle,
707                                             data_ac,
708                                             meta_ac,
709                                             &why);
710         if ((status < 0) && (status != -EAGAIN)) {
711                 if (status != -ENOSPC)
712                         mlog_errno(status);
713                 goto leave;
714         }
715
716         status = ocfs2_journal_dirty(handle, bh);
717         if (status < 0) {
718                 mlog_errno(status);
719                 goto leave;
720         }
721
722         spin_lock(&OCFS2_I(inode)->ip_lock);
723         clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
724         spin_unlock(&OCFS2_I(inode)->ip_lock);
725
726         if (why != RESTART_NONE && clusters_to_add) {
727                 if (why == RESTART_META) {
728                         mlog(0, "restarting function.\n");
729                         restart_func = 1;
730                 } else {
731                         BUG_ON(why != RESTART_TRANS);
732
733                         mlog(0, "restarting transaction.\n");
734                         /* TODO: This can be more intelligent. */
735                         credits = ocfs2_calc_extend_credits(osb->sb,
736                                                             fe,
737                                                             clusters_to_add);
738                         status = ocfs2_extend_trans(handle, credits);
739                         if (status < 0) {
740                                 /* handle still has to be committed at
741                                  * this point. */
742                                 status = -ENOMEM;
743                                 mlog_errno(status);
744                                 goto leave;
745                         }
746                         goto restarted_transaction;
747                 }
748         }
749
750         mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
751              le32_to_cpu(fe->i_clusters),
752              (unsigned long long)le64_to_cpu(fe->i_size));
753         mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
754              OCFS2_I(inode)->ip_clusters, i_size_read(inode));
755
756 leave:
757         if (handle) {
758                 ocfs2_commit_trans(osb, handle);
759                 handle = NULL;
760         }
761         if (data_ac) {
762                 ocfs2_free_alloc_context(data_ac);
763                 data_ac = NULL;
764         }
765         if (meta_ac) {
766                 ocfs2_free_alloc_context(meta_ac);
767                 meta_ac = NULL;
768         }
769         if ((!status) && restart_func) {
770                 restart_func = 0;
771                 goto restart_all;
772         }
773         if (bh) {
774                 brelse(bh);
775                 bh = NULL;
776         }
777
778         mlog_exit(status);
779         return status;
780 }
781
782 static int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
783                                    u32 clusters_to_add, int mark_unwritten)
784 {
785         int ret;
786
787         /*
788          * The alloc sem blocks peope in read/write from reading our
789          * allocation until we're done changing it. We depend on
790          * i_mutex to block other extend/truncate calls while we're
791          * here.
792          */
793         down_write(&OCFS2_I(inode)->ip_alloc_sem);
794         ret = __ocfs2_extend_allocation(inode, logical_start, clusters_to_add,
795                                         mark_unwritten);
796         up_write(&OCFS2_I(inode)->ip_alloc_sem);
797
798         return ret;
799 }
800
801 /* Some parts of this taken from generic_cont_expand, which turned out
802  * to be too fragile to do exactly what we need without us having to
803  * worry about recursive locking in ->prepare_write() and
804  * ->commit_write(). */
805 static int ocfs2_write_zero_page(struct inode *inode,
806                                  u64 size)
807 {
808         struct address_space *mapping = inode->i_mapping;
809         struct page *page;
810         unsigned long index;
811         unsigned int offset;
812         handle_t *handle = NULL;
813         int ret;
814
815         offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
816         /* ugh.  in prepare/commit_write, if from==to==start of block, we 
817         ** skip the prepare.  make sure we never send an offset for the start
818         ** of a block
819         */
820         if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
821                 offset++;
822         }
823         index = size >> PAGE_CACHE_SHIFT;
824
825         page = grab_cache_page(mapping, index);
826         if (!page) {
827                 ret = -ENOMEM;
828                 mlog_errno(ret);
829                 goto out;
830         }
831
832         ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
833         if (ret < 0) {
834                 mlog_errno(ret);
835                 goto out_unlock;
836         }
837
838         if (ocfs2_should_order_data(inode)) {
839                 handle = ocfs2_start_walk_page_trans(inode, page, offset,
840                                                      offset);
841                 if (IS_ERR(handle)) {
842                         ret = PTR_ERR(handle);
843                         handle = NULL;
844                         goto out_unlock;
845                 }
846         }
847
848         /* must not update i_size! */
849         ret = block_commit_write(page, offset, offset);
850         if (ret < 0)
851                 mlog_errno(ret);
852         else
853                 ret = 0;
854
855         if (handle)
856                 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
857 out_unlock:
858         unlock_page(page);
859         page_cache_release(page);
860 out:
861         return ret;
862 }
863
864 static int ocfs2_zero_extend(struct inode *inode,
865                              u64 zero_to_size)
866 {
867         int ret = 0;
868         u64 start_off;
869         struct super_block *sb = inode->i_sb;
870
871         start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
872         while (start_off < zero_to_size) {
873                 ret = ocfs2_write_zero_page(inode, start_off);
874                 if (ret < 0) {
875                         mlog_errno(ret);
876                         goto out;
877                 }
878
879                 start_off += sb->s_blocksize;
880
881                 /*
882                  * Very large extends have the potential to lock up
883                  * the cpu for extended periods of time.
884                  */
885                 cond_resched();
886         }
887
888 out:
889         return ret;
890 }
891
892 /* 
893  * A tail_to_skip value > 0 indicates that we're being called from
894  * ocfs2_file_aio_write(). This has the following implications:
895  *
896  * - we don't want to update i_size
897  * - di_bh will be NULL, which is fine because it's only used in the
898  *   case where we want to update i_size.
899  * - ocfs2_zero_extend() will then only be filling the hole created
900  *   between i_size and the start of the write.
901  */
902 static int ocfs2_extend_file(struct inode *inode,
903                              struct buffer_head *di_bh,
904                              u64 new_i_size,
905                              size_t tail_to_skip)
906 {
907         int ret = 0;
908         u32 clusters_to_add = 0;
909
910         BUG_ON(!tail_to_skip && !di_bh);
911
912         /* setattr sometimes calls us like this. */
913         if (new_i_size == 0)
914                 goto out;
915
916         if (i_size_read(inode) == new_i_size)
917                 goto out;
918         BUG_ON(new_i_size < i_size_read(inode));
919
920         if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
921                 BUG_ON(tail_to_skip != 0);
922                 goto out_update_size;
923         }
924
925         clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - 
926                 OCFS2_I(inode)->ip_clusters;
927
928         /* 
929          * protect the pages that ocfs2_zero_extend is going to be
930          * pulling into the page cache.. we do this before the
931          * metadata extend so that we don't get into the situation
932          * where we've extended the metadata but can't get the data
933          * lock to zero.
934          */
935         ret = ocfs2_data_lock(inode, 1);
936         if (ret < 0) {
937                 mlog_errno(ret);
938                 goto out;
939         }
940
941         if (clusters_to_add) {
942                 ret = ocfs2_extend_allocation(inode,
943                                               OCFS2_I(inode)->ip_clusters,
944                                               clusters_to_add, 0);
945                 if (ret < 0) {
946                         mlog_errno(ret);
947                         goto out_unlock;
948                 }
949         }
950
951         /*
952          * Call this even if we don't add any clusters to the tree. We
953          * still need to zero the area between the old i_size and the
954          * new i_size.
955          */
956         ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
957         if (ret < 0) {
958                 mlog_errno(ret);
959                 goto out_unlock;
960         }
961
962 out_update_size:
963         if (!tail_to_skip) {
964                 /* We're being called from ocfs2_setattr() which wants
965                  * us to update i_size */
966                 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
967                 if (ret < 0)
968                         mlog_errno(ret);
969         }
970
971 out_unlock:
972         if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
973                 ocfs2_data_unlock(inode, 1);
974
975 out:
976         return ret;
977 }
978
979 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
980 {
981         int status = 0, size_change;
982         struct inode *inode = dentry->d_inode;
983         struct super_block *sb = inode->i_sb;
984         struct ocfs2_super *osb = OCFS2_SB(sb);
985         struct buffer_head *bh = NULL;
986         handle_t *handle = NULL;
987
988         mlog_entry("(0x%p, '%.*s')\n", dentry,
989                    dentry->d_name.len, dentry->d_name.name);
990
991         if (attr->ia_valid & ATTR_MODE)
992                 mlog(0, "mode change: %d\n", attr->ia_mode);
993         if (attr->ia_valid & ATTR_UID)
994                 mlog(0, "uid change: %d\n", attr->ia_uid);
995         if (attr->ia_valid & ATTR_GID)
996                 mlog(0, "gid change: %d\n", attr->ia_gid);
997         if (attr->ia_valid & ATTR_SIZE)
998                 mlog(0, "size change...\n");
999         if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
1000                 mlog(0, "time change...\n");
1001
1002 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1003                            | ATTR_GID | ATTR_UID | ATTR_MODE)
1004         if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
1005                 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
1006                 return 0;
1007         }
1008
1009         status = inode_change_ok(inode, attr);
1010         if (status)
1011                 return status;
1012
1013         size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1014         if (size_change) {
1015                 status = ocfs2_rw_lock(inode, 1);
1016                 if (status < 0) {
1017                         mlog_errno(status);
1018                         goto bail;
1019                 }
1020         }
1021
1022         status = ocfs2_meta_lock(inode, &bh, 1);
1023         if (status < 0) {
1024                 if (status != -ENOENT)
1025                         mlog_errno(status);
1026                 goto bail_unlock_rw;
1027         }
1028
1029         if (size_change && attr->ia_size != i_size_read(inode)) {
1030                 if (attr->ia_size > sb->s_maxbytes) {
1031                         status = -EFBIG;
1032                         goto bail_unlock;
1033                 }
1034
1035                 if (i_size_read(inode) > attr->ia_size)
1036                         status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1037                 else
1038                         status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
1039                 if (status < 0) {
1040                         if (status != -ENOSPC)
1041                                 mlog_errno(status);
1042                         status = -ENOSPC;
1043                         goto bail_unlock;
1044                 }
1045         }
1046
1047         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1048         if (IS_ERR(handle)) {
1049                 status = PTR_ERR(handle);
1050                 mlog_errno(status);
1051                 goto bail_unlock;
1052         }
1053
1054         /*
1055          * This will intentionally not wind up calling vmtruncate(),
1056          * since all the work for a size change has been done above.
1057          * Otherwise, we could get into problems with truncate as
1058          * ip_alloc_sem is used there to protect against i_size
1059          * changes.
1060          */
1061         status = inode_setattr(inode, attr);
1062         if (status < 0) {
1063                 mlog_errno(status);
1064                 goto bail_commit;
1065         }
1066
1067         status = ocfs2_mark_inode_dirty(handle, inode, bh);
1068         if (status < 0)
1069                 mlog_errno(status);
1070
1071 bail_commit:
1072         ocfs2_commit_trans(osb, handle);
1073 bail_unlock:
1074         ocfs2_meta_unlock(inode, 1);
1075 bail_unlock_rw:
1076         if (size_change)
1077                 ocfs2_rw_unlock(inode, 1);
1078 bail:
1079         if (bh)
1080                 brelse(bh);
1081
1082         mlog_exit(status);
1083         return status;
1084 }
1085
1086 int ocfs2_getattr(struct vfsmount *mnt,
1087                   struct dentry *dentry,
1088                   struct kstat *stat)
1089 {
1090         struct inode *inode = dentry->d_inode;
1091         struct super_block *sb = dentry->d_inode->i_sb;
1092         struct ocfs2_super *osb = sb->s_fs_info;
1093         int err;
1094
1095         mlog_entry_void();
1096
1097         err = ocfs2_inode_revalidate(dentry);
1098         if (err) {
1099                 if (err != -ENOENT)
1100                         mlog_errno(err);
1101                 goto bail;
1102         }
1103
1104         generic_fillattr(inode, stat);
1105
1106         /* We set the blksize from the cluster size for performance */
1107         stat->blksize = osb->s_clustersize;
1108
1109 bail:
1110         mlog_exit(err);
1111
1112         return err;
1113 }
1114
1115 int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1116 {
1117         int ret;
1118
1119         mlog_entry_void();
1120
1121         ret = ocfs2_meta_lock(inode, NULL, 0);
1122         if (ret) {
1123                 if (ret != -ENOENT)
1124                         mlog_errno(ret);
1125                 goto out;
1126         }
1127
1128         ret = generic_permission(inode, mask, NULL);
1129
1130         ocfs2_meta_unlock(inode, 0);
1131 out:
1132         mlog_exit(ret);
1133         return ret;
1134 }
1135
1136 static int __ocfs2_write_remove_suid(struct inode *inode,
1137                                      struct buffer_head *bh)
1138 {
1139         int ret;
1140         handle_t *handle;
1141         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1142         struct ocfs2_dinode *di;
1143
1144         mlog_entry("(Inode %llu, mode 0%o)\n",
1145                    (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
1146
1147         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1148         if (handle == NULL) {
1149                 ret = -ENOMEM;
1150                 mlog_errno(ret);
1151                 goto out;
1152         }
1153
1154         ret = ocfs2_journal_access(handle, inode, bh,
1155                                    OCFS2_JOURNAL_ACCESS_WRITE);
1156         if (ret < 0) {
1157                 mlog_errno(ret);
1158                 goto out_trans;
1159         }
1160
1161         inode->i_mode &= ~S_ISUID;
1162         if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1163                 inode->i_mode &= ~S_ISGID;
1164
1165         di = (struct ocfs2_dinode *) bh->b_data;
1166         di->i_mode = cpu_to_le16(inode->i_mode);
1167
1168         ret = ocfs2_journal_dirty(handle, bh);
1169         if (ret < 0)
1170                 mlog_errno(ret);
1171
1172 out_trans:
1173         ocfs2_commit_trans(osb, handle);
1174 out:
1175         mlog_exit(ret);
1176         return ret;
1177 }
1178
1179 /*
1180  * Will look for holes and unwritten extents in the range starting at
1181  * pos for count bytes (inclusive).
1182  */
1183 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1184                                        size_t count)
1185 {
1186         int ret = 0;
1187         unsigned int extent_flags;
1188         u32 cpos, clusters, extent_len, phys_cpos;
1189         struct super_block *sb = inode->i_sb;
1190
1191         cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1192         clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1193
1194         while (clusters) {
1195                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1196                                          &extent_flags);
1197                 if (ret < 0) {
1198                         mlog_errno(ret);
1199                         goto out;
1200                 }
1201
1202                 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1203                         ret = 1;
1204                         break;
1205                 }
1206
1207                 if (extent_len > clusters)
1208                         extent_len = clusters;
1209
1210                 clusters -= extent_len;
1211                 cpos += extent_len;
1212         }
1213 out:
1214         return ret;
1215 }
1216
1217 static int ocfs2_write_remove_suid(struct inode *inode)
1218 {
1219         int ret;
1220         struct buffer_head *bh = NULL;
1221         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1222
1223         ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1224                                oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1225         if (ret < 0) {
1226                 mlog_errno(ret);
1227                 goto out;
1228         }
1229
1230         ret =  __ocfs2_write_remove_suid(inode, bh);
1231 out:
1232         brelse(bh);
1233         return ret;
1234 }
1235
1236 /*
1237  * Allocate enough extents to cover the region starting at byte offset
1238  * start for len bytes. Existing extents are skipped, any extents
1239  * added are marked as "unwritten".
1240  */
1241 static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1242                                             u64 start, u64 len)
1243 {
1244         int ret;
1245         u32 cpos, phys_cpos, clusters, alloc_size;
1246
1247         /*
1248          * We consider both start and len to be inclusive.
1249          */
1250         cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1251         clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1252         clusters -= cpos;
1253
1254         while (clusters) {
1255                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1256                                          &alloc_size, NULL);
1257                 if (ret) {
1258                         mlog_errno(ret);
1259                         goto out;
1260                 }
1261
1262                 /*
1263                  * Hole or existing extent len can be arbitrary, so
1264                  * cap it to our own allocation request.
1265                  */
1266                 if (alloc_size > clusters)
1267                         alloc_size = clusters;
1268
1269                 if (phys_cpos) {
1270                         /*
1271                          * We already have an allocation at this
1272                          * region so we can safely skip it.
1273                          */
1274                         goto next;
1275                 }
1276
1277                 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1278                 if (ret) {
1279                         if (ret != -ENOSPC)
1280                                 mlog_errno(ret);
1281                         goto out;
1282                 }
1283
1284 next:
1285                 cpos += alloc_size;
1286                 clusters -= alloc_size;
1287         }
1288
1289         ret = 0;
1290 out:
1291         return ret;
1292 }
1293
1294 static int __ocfs2_remove_inode_range(struct inode *inode,
1295                                       struct buffer_head *di_bh,
1296                                       u32 cpos, u32 phys_cpos, u32 len,
1297                                       struct ocfs2_cached_dealloc_ctxt *dealloc)
1298 {
1299         int ret;
1300         u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
1301         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1302         struct inode *tl_inode = osb->osb_tl_inode;
1303         handle_t *handle;
1304         struct ocfs2_alloc_context *meta_ac = NULL;
1305         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1306
1307         ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
1308         if (ret) {
1309                 mlog_errno(ret);
1310                 return ret;
1311         }
1312
1313         mutex_lock(&tl_inode->i_mutex);
1314
1315         if (ocfs2_truncate_log_needs_flush(osb)) {
1316                 ret = __ocfs2_flush_truncate_log(osb);
1317                 if (ret < 0) {
1318                         mlog_errno(ret);
1319                         goto out;
1320                 }
1321         }
1322
1323         handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1324         if (handle == NULL) {
1325                 ret = -ENOMEM;
1326                 mlog_errno(ret);
1327                 goto out;
1328         }
1329
1330         ret = ocfs2_journal_access(handle, inode, di_bh,
1331                                    OCFS2_JOURNAL_ACCESS_WRITE);
1332         if (ret) {
1333                 mlog_errno(ret);
1334                 goto out;
1335         }
1336
1337         ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
1338                                   dealloc);
1339         if (ret) {
1340                 mlog_errno(ret);
1341                 goto out_commit;
1342         }
1343
1344         OCFS2_I(inode)->ip_clusters -= len;
1345         di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1346
1347         ret = ocfs2_journal_dirty(handle, di_bh);
1348         if (ret) {
1349                 mlog_errno(ret);
1350                 goto out_commit;
1351         }
1352
1353         ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
1354         if (ret)
1355                 mlog_errno(ret);
1356
1357 out_commit:
1358         ocfs2_commit_trans(osb, handle);
1359 out:
1360         mutex_unlock(&tl_inode->i_mutex);
1361
1362         if (meta_ac)
1363                 ocfs2_free_alloc_context(meta_ac);
1364
1365         return ret;
1366 }
1367
1368 /*
1369  * Truncate a byte range, avoiding pages within partial clusters. This
1370  * preserves those pages for the zeroing code to write to.
1371  */
1372 static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1373                                          u64 byte_len)
1374 {
1375         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1376         loff_t start, end;
1377         struct address_space *mapping = inode->i_mapping;
1378
1379         start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1380         end = byte_start + byte_len;
1381         end = end & ~(osb->s_clustersize - 1);
1382
1383         if (start < end) {
1384                 unmap_mapping_range(mapping, start, end - start, 0);
1385                 truncate_inode_pages_range(mapping, start, end - 1);
1386         }
1387 }
1388
1389 static int ocfs2_zero_partial_clusters(struct inode *inode,
1390                                        u64 start, u64 len)
1391 {
1392         int ret = 0;
1393         u64 tmpend, end = start + len;
1394         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1395         unsigned int csize = osb->s_clustersize;
1396         handle_t *handle;
1397
1398         /*
1399          * The "start" and "end" values are NOT necessarily part of
1400          * the range whose allocation is being deleted. Rather, this
1401          * is what the user passed in with the request. We must zero
1402          * partial clusters here. There's no need to worry about
1403          * physical allocation - the zeroing code knows to skip holes.
1404          */
1405         mlog(0, "byte start: %llu, end: %llu\n",
1406              (unsigned long long)start, (unsigned long long)end);
1407
1408         /*
1409          * If both edges are on a cluster boundary then there's no
1410          * zeroing required as the region is part of the allocation to
1411          * be truncated.
1412          */
1413         if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1414                 goto out;
1415
1416         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1417         if (handle == NULL) {
1418                 ret = -ENOMEM;
1419                 mlog_errno(ret);
1420                 goto out;
1421         }
1422
1423         /*
1424          * We want to get the byte offset of the end of the 1st cluster.
1425          */
1426         tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
1427         if (tmpend > end)
1428                 tmpend = end;
1429
1430         mlog(0, "1st range: start: %llu, tmpend: %llu\n",
1431              (unsigned long long)start, (unsigned long long)tmpend);
1432
1433         ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
1434         if (ret)
1435                 mlog_errno(ret);
1436
1437         if (tmpend < end) {
1438                 /*
1439                  * This may make start and end equal, but the zeroing
1440                  * code will skip any work in that case so there's no
1441                  * need to catch it up here.
1442                  */
1443                 start = end & ~(osb->s_clustersize - 1);
1444
1445                 mlog(0, "2nd range: start: %llu, end: %llu\n",
1446                      (unsigned long long)start, (unsigned long long)end);
1447
1448                 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1449                 if (ret)
1450                         mlog_errno(ret);
1451         }
1452
1453         ocfs2_commit_trans(osb, handle);
1454 out:
1455         return ret;
1456 }
1457
1458 static int ocfs2_remove_inode_range(struct inode *inode,
1459                                     struct buffer_head *di_bh, u64 byte_start,
1460                                     u64 byte_len)
1461 {
1462         int ret = 0;
1463         u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
1464         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1465         struct ocfs2_cached_dealloc_ctxt dealloc;
1466
1467         ocfs2_init_dealloc_ctxt(&dealloc);
1468
1469         if (byte_len == 0)
1470                 return 0;
1471
1472         trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1473         trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
1474         if (trunc_len >= trunc_start)
1475                 trunc_len -= trunc_start;
1476         else
1477                 trunc_len = 0;
1478
1479         mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
1480              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1481              (unsigned long long)byte_start,
1482              (unsigned long long)byte_len, trunc_start, trunc_len);
1483
1484         ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1485         if (ret) {
1486                 mlog_errno(ret);
1487                 goto out;
1488         }
1489
1490         cpos = trunc_start;
1491         while (trunc_len) {
1492                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1493                                          &alloc_size, NULL);
1494                 if (ret) {
1495                         mlog_errno(ret);
1496                         goto out;
1497                 }
1498
1499                 if (alloc_size > trunc_len)
1500                         alloc_size = trunc_len;
1501
1502                 /* Only do work for non-holes */
1503                 if (phys_cpos != 0) {
1504                         ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
1505                                                          phys_cpos, alloc_size,
1506                                                          &dealloc);
1507                         if (ret) {
1508                                 mlog_errno(ret);
1509                                 goto out;
1510                         }
1511                 }
1512
1513                 cpos += alloc_size;
1514                 trunc_len -= alloc_size;
1515         }
1516
1517         ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1518
1519 out:
1520         ocfs2_schedule_truncate_log_flush(osb, 1);
1521         ocfs2_run_deallocs(osb, &dealloc);
1522
1523         return ret;
1524 }
1525
1526 /*
1527  * Parts of this function taken from xfs_change_file_space()
1528  */
1529 static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1530                                      loff_t f_pos, unsigned int cmd,
1531                                      struct ocfs2_space_resv *sr,
1532                                      int change_size)
1533 {
1534         int ret;
1535         s64 llen;
1536         loff_t size;
1537         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1538         struct buffer_head *di_bh = NULL;
1539         handle_t *handle;
1540         unsigned long long max_off = inode->i_sb->s_maxbytes;
1541
1542         if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1543                 return -EROFS;
1544
1545         mutex_lock(&inode->i_mutex);
1546
1547         /*
1548          * This prevents concurrent writes on other nodes
1549          */
1550         ret = ocfs2_rw_lock(inode, 1);
1551         if (ret) {
1552                 mlog_errno(ret);
1553                 goto out;
1554         }
1555
1556         ret = ocfs2_meta_lock(inode, &di_bh, 1);
1557         if (ret) {
1558                 mlog_errno(ret);
1559                 goto out_rw_unlock;
1560         }
1561
1562         if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1563                 ret = -EPERM;
1564                 goto out_meta_unlock;
1565         }
1566
1567         switch (sr->l_whence) {
1568         case 0: /*SEEK_SET*/
1569                 break;
1570         case 1: /*SEEK_CUR*/
1571                 sr->l_start += f_pos;
1572                 break;
1573         case 2: /*SEEK_END*/
1574                 sr->l_start += i_size_read(inode);
1575                 break;
1576         default:
1577                 ret = -EINVAL;
1578                 goto out_meta_unlock;
1579         }
1580         sr->l_whence = 0;
1581
1582         llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1583
1584         if (sr->l_start < 0
1585             || sr->l_start > max_off
1586             || (sr->l_start + llen) < 0
1587             || (sr->l_start + llen) > max_off) {
1588                 ret = -EINVAL;
1589                 goto out_meta_unlock;
1590         }
1591         size = sr->l_start + sr->l_len;
1592
1593         if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1594                 if (sr->l_len <= 0) {
1595                         ret = -EINVAL;
1596                         goto out_meta_unlock;
1597                 }
1598         }
1599
1600         if (file && should_remove_suid(file->f_path.dentry)) {
1601                 ret = __ocfs2_write_remove_suid(inode, di_bh);
1602                 if (ret) {
1603                         mlog_errno(ret);
1604                         goto out_meta_unlock;
1605                 }
1606         }
1607
1608         down_write(&OCFS2_I(inode)->ip_alloc_sem);
1609         switch (cmd) {
1610         case OCFS2_IOC_RESVSP:
1611         case OCFS2_IOC_RESVSP64:
1612                 /*
1613                  * This takes unsigned offsets, but the signed ones we
1614                  * pass have been checked against overflow above.
1615                  */
1616                 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1617                                                        sr->l_len);
1618                 break;
1619         case OCFS2_IOC_UNRESVSP:
1620         case OCFS2_IOC_UNRESVSP64:
1621                 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1622                                                sr->l_len);
1623                 break;
1624         default:
1625                 ret = -EINVAL;
1626         }
1627         up_write(&OCFS2_I(inode)->ip_alloc_sem);
1628         if (ret) {
1629                 mlog_errno(ret);
1630                 goto out_meta_unlock;
1631         }
1632
1633         /*
1634          * We update c/mtime for these changes
1635          */
1636         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1637         if (IS_ERR(handle)) {
1638                 ret = PTR_ERR(handle);
1639                 mlog_errno(ret);
1640                 goto out_meta_unlock;
1641         }
1642
1643         if (change_size && i_size_read(inode) < size)
1644                 i_size_write(inode, size);
1645
1646         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1647         ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1648         if (ret < 0)
1649                 mlog_errno(ret);
1650
1651         ocfs2_commit_trans(osb, handle);
1652
1653 out_meta_unlock:
1654         brelse(di_bh);
1655         ocfs2_meta_unlock(inode, 1);
1656 out_rw_unlock:
1657         ocfs2_rw_unlock(inode, 1);
1658
1659         mutex_unlock(&inode->i_mutex);
1660 out:
1661         return ret;
1662 }
1663
1664 int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1665                             struct ocfs2_space_resv *sr)
1666 {
1667         struct inode *inode = file->f_path.dentry->d_inode;
1668         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
1669
1670         if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1671             !ocfs2_writes_unwritten_extents(osb))
1672                 return -ENOTTY;
1673         else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1674                  !ocfs2_sparse_alloc(osb))
1675                 return -ENOTTY;
1676
1677         if (!S_ISREG(inode->i_mode))
1678                 return -EINVAL;
1679
1680         if (!(file->f_mode & FMODE_WRITE))
1681                 return -EBADF;
1682
1683         return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
1684 }
1685
1686 static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1687                             loff_t len)
1688 {
1689         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1690         struct ocfs2_space_resv sr;
1691         int change_size = 1;
1692
1693         if (!ocfs2_writes_unwritten_extents(osb))
1694                 return -EOPNOTSUPP;
1695
1696         if (S_ISDIR(inode->i_mode))
1697                 return -ENODEV;
1698
1699         if (mode & FALLOC_FL_KEEP_SIZE)
1700                 change_size = 0;
1701
1702         sr.l_whence = 0;
1703         sr.l_start = (s64)offset;
1704         sr.l_len = (s64)len;
1705
1706         return __ocfs2_change_file_space(NULL, inode, offset,
1707                                          OCFS2_IOC_RESVSP64, &sr, change_size);
1708 }
1709
1710 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1711                                          loff_t *ppos,
1712                                          size_t count,
1713                                          int appending,
1714                                          int *direct_io)
1715 {
1716         int ret = 0, meta_level = appending;
1717         struct inode *inode = dentry->d_inode;
1718         u32 clusters;
1719         loff_t newsize, saved_pos;
1720
1721         /* 
1722          * We sample i_size under a read level meta lock to see if our write
1723          * is extending the file, if it is we back off and get a write level
1724          * meta lock.
1725          */
1726         for(;;) {
1727                 ret = ocfs2_meta_lock(inode, NULL, meta_level);
1728                 if (ret < 0) {
1729                         meta_level = -1;
1730                         mlog_errno(ret);
1731                         goto out;
1732                 }
1733
1734                 /* Clear suid / sgid if necessary. We do this here
1735                  * instead of later in the write path because
1736                  * remove_suid() calls ->setattr without any hint that
1737                  * we may have already done our cluster locking. Since
1738                  * ocfs2_setattr() *must* take cluster locks to
1739                  * proceeed, this will lead us to recursively lock the
1740                  * inode. There's also the dinode i_size state which
1741                  * can be lost via setattr during extending writes (we
1742                  * set inode->i_size at the end of a write. */
1743                 if (should_remove_suid(dentry)) {
1744                         if (meta_level == 0) {
1745                                 ocfs2_meta_unlock(inode, meta_level);
1746                                 meta_level = 1;
1747                                 continue;
1748                         }
1749
1750                         ret = ocfs2_write_remove_suid(inode);
1751                         if (ret < 0) {
1752                                 mlog_errno(ret);
1753                                 goto out_unlock;
1754                         }
1755                 }
1756
1757                 /* work on a copy of ppos until we're sure that we won't have
1758                  * to recalculate it due to relocking. */
1759                 if (appending) {
1760                         saved_pos = i_size_read(inode);
1761                         mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1762                 } else {
1763                         saved_pos = *ppos;
1764                 }
1765
1766                 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1767                         loff_t end = saved_pos + count;
1768
1769                         /*
1770                          * Skip the O_DIRECT checks if we don't need
1771                          * them.
1772                          */
1773                         if (!direct_io || !(*direct_io))
1774                                 break;
1775
1776                         /*
1777                          * Allowing concurrent direct writes means
1778                          * i_size changes wouldn't be synchronized, so
1779                          * one node could wind up truncating another
1780                          * nodes writes.
1781                          */
1782                         if (end > i_size_read(inode)) {
1783                                 *direct_io = 0;
1784                                 break;
1785                         }
1786
1787                         /*
1788                          * We don't fill holes during direct io, so
1789                          * check for them here. If any are found, the
1790                          * caller will have to retake some cluster
1791                          * locks and initiate the io as buffered.
1792                          */
1793                         ret = ocfs2_check_range_for_holes(inode, saved_pos,
1794                                                           count);
1795                         if (ret == 1) {
1796                                 *direct_io = 0;
1797                                 ret = 0;
1798                         } else if (ret < 0)
1799                                 mlog_errno(ret);
1800                         break;
1801                 }
1802
1803                 /*
1804                  * The rest of this loop is concerned with legacy file
1805                  * systems which don't support sparse files.
1806                  */
1807
1808                 newsize = count + saved_pos;
1809
1810                 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1811                      (long long) saved_pos, (long long) newsize,
1812                      (long long) i_size_read(inode));
1813
1814                 /* No need for a higher level metadata lock if we're
1815                  * never going past i_size. */
1816                 if (newsize <= i_size_read(inode))
1817                         break;
1818
1819                 if (meta_level == 0) {
1820                         ocfs2_meta_unlock(inode, meta_level);
1821                         meta_level = 1;
1822                         continue;
1823                 }
1824
1825                 spin_lock(&OCFS2_I(inode)->ip_lock);
1826                 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1827                         OCFS2_I(inode)->ip_clusters;
1828                 spin_unlock(&OCFS2_I(inode)->ip_lock);
1829
1830                 mlog(0, "Writing at EOF, may need more allocation: "
1831                      "i_size = %lld, newsize = %lld, need %u clusters\n",
1832                      (long long) i_size_read(inode), (long long) newsize,
1833                      clusters);
1834
1835                 /* We only want to continue the rest of this loop if
1836                  * our extend will actually require more
1837                  * allocation. */
1838                 if (!clusters)
1839                         break;
1840
1841                 ret = ocfs2_extend_file(inode, NULL, newsize, count);
1842                 if (ret < 0) {
1843                         if (ret != -ENOSPC)
1844                                 mlog_errno(ret);
1845                         goto out_unlock;
1846                 }
1847                 break;
1848         }
1849
1850         if (appending)
1851                 *ppos = saved_pos;
1852
1853 out_unlock:
1854         ocfs2_meta_unlock(inode, meta_level);
1855
1856 out:
1857         return ret;
1858 }
1859
1860 static inline void
1861 ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1862 {
1863         const struct iovec *iov = *iovp;
1864         size_t base = *basep;
1865
1866         do {
1867                 int copy = min(bytes, iov->iov_len - base);
1868
1869                 bytes -= copy;
1870                 base += copy;
1871                 if (iov->iov_len == base) {
1872                         iov++;
1873                         base = 0;
1874                 }
1875         } while (bytes);
1876         *iovp = iov;
1877         *basep = base;
1878 }
1879
1880 static struct page * ocfs2_get_write_source(char **ret_src_buf,
1881                                             const struct iovec *cur_iov,
1882                                             size_t iov_offset)
1883 {
1884         int ret;
1885         char *buf = cur_iov->iov_base + iov_offset;
1886         struct page *src_page = NULL;
1887         unsigned long off;
1888
1889         off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
1890
1891         if (!segment_eq(get_fs(), KERNEL_DS)) {
1892                 /*
1893                  * Pull in the user page. We want to do this outside
1894                  * of the meta data locks in order to preserve locking
1895                  * order in case of page fault.
1896                  */
1897                 ret = get_user_pages(current, current->mm,
1898                                      (unsigned long)buf & PAGE_CACHE_MASK, 1,
1899                                      0, 0, &src_page, NULL);
1900                 if (ret == 1)
1901                         *ret_src_buf = kmap(src_page) + off;
1902                 else
1903                         src_page = ERR_PTR(-EFAULT);
1904         } else {
1905                 *ret_src_buf = buf;
1906         }
1907
1908         return src_page;
1909 }
1910
1911 static void ocfs2_put_write_source(struct page *page)
1912 {
1913         if (page) {
1914                 kunmap(page);
1915                 page_cache_release(page);
1916         }
1917 }
1918
1919 static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1920                                          const struct iovec *iov,
1921                                          unsigned long nr_segs,
1922                                          size_t count,
1923                                          ssize_t o_direct_written)
1924 {
1925         int ret = 0;
1926         ssize_t copied, total = 0;
1927         size_t iov_offset = 0, bytes;
1928         loff_t pos;
1929         const struct iovec *cur_iov = iov;
1930         struct page *user_page, *page;
1931         char * uninitialized_var(buf);
1932         char *dst;
1933         void *fsdata;
1934
1935         /*
1936          * handle partial DIO write.  Adjust cur_iov if needed.
1937          */
1938         ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1939
1940         do {
1941                 pos = *ppos;
1942
1943                 user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
1944                 if (IS_ERR(user_page)) {
1945                         ret = PTR_ERR(user_page);
1946                         goto out;
1947                 }
1948
1949                 /* Stay within our page boundaries */
1950                 bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
1951                             (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
1952                 /* Stay within the vector boundary */
1953                 bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
1954                 /* Stay within count */
1955                 bytes = min(bytes, count);
1956
1957                 page = NULL;
1958                 ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
1959                                         &page, &fsdata);
1960                 if (ret) {
1961                         mlog_errno(ret);
1962                         goto out;
1963                 }
1964
1965                 dst = kmap_atomic(page, KM_USER0);
1966                 memcpy(dst + (pos & (loff_t)(PAGE_CACHE_SIZE - 1)), buf, bytes);
1967                 kunmap_atomic(dst, KM_USER0);
1968                 flush_dcache_page(page);
1969                 ocfs2_put_write_source(user_page);
1970
1971                 copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
1972                                          bytes, page, fsdata);
1973                 if (copied < 0) {
1974                         mlog_errno(copied);
1975                         ret = copied;
1976                         goto out;
1977                 }
1978
1979                 total += copied;
1980                 *ppos = pos + copied;
1981                 count -= copied;
1982
1983                 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1984         } while(count);
1985
1986 out:
1987         return total ? total : ret;
1988 }
1989
1990 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1991                                     const struct iovec *iov,
1992                                     unsigned long nr_segs,
1993                                     loff_t pos)
1994 {
1995         int ret, direct_io, appending, rw_level, have_alloc_sem  = 0;
1996         int can_do_direct, sync = 0;
1997         ssize_t written = 0;
1998         size_t ocount;          /* original count */
1999         size_t count;           /* after file limit checks */
2000         loff_t *ppos = &iocb->ki_pos;
2001         struct file *file = iocb->ki_filp;
2002         struct inode *inode = file->f_path.dentry->d_inode;
2003
2004         mlog_entry("(0x%p, %u, '%.*s')\n", file,
2005                    (unsigned int)nr_segs,
2006                    file->f_path.dentry->d_name.len,
2007                    file->f_path.dentry->d_name.name);
2008
2009         if (iocb->ki_left == 0)
2010                 return 0;
2011
2012         ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2013         if (ret)
2014                 return ret;
2015
2016         count = ocount;
2017
2018         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2019
2020         appending = file->f_flags & O_APPEND ? 1 : 0;
2021         direct_io = file->f_flags & O_DIRECT ? 1 : 0;
2022
2023         mutex_lock(&inode->i_mutex);
2024
2025 relock:
2026         /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
2027         if (direct_io) {
2028                 down_read(&inode->i_alloc_sem);
2029                 have_alloc_sem = 1;
2030         }
2031
2032         /* concurrent O_DIRECT writes are allowed */
2033         rw_level = !direct_io;
2034         ret = ocfs2_rw_lock(inode, rw_level);
2035         if (ret < 0) {
2036                 mlog_errno(ret);
2037                 goto out_sems;
2038         }
2039
2040         can_do_direct = direct_io;
2041         ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
2042                                             iocb->ki_left, appending,
2043                                             &can_do_direct);
2044         if (ret < 0) {
2045                 mlog_errno(ret);
2046                 goto out;
2047         }
2048
2049         /*
2050          * We can't complete the direct I/O as requested, fall back to
2051          * buffered I/O.
2052          */
2053         if (direct_io && !can_do_direct) {
2054                 ocfs2_rw_unlock(inode, rw_level);
2055                 up_read(&inode->i_alloc_sem);
2056
2057                 have_alloc_sem = 0;
2058                 rw_level = -1;
2059
2060                 direct_io = 0;
2061                 sync = 1;
2062                 goto relock;
2063         }
2064
2065         if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
2066                 sync = 1;
2067
2068         /*
2069          * XXX: Is it ok to execute these checks a second time?
2070          */
2071         ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
2072         if (ret)
2073                 goto out;
2074
2075         /*
2076          * Set pos so that sync_page_range_nolock() below understands
2077          * where to start from. We might've moved it around via the
2078          * calls above. The range we want to actually sync starts from
2079          * *ppos here.
2080          *
2081          */
2082         pos = *ppos;
2083
2084         /* communicate with ocfs2_dio_end_io */
2085         ocfs2_iocb_set_rw_locked(iocb, rw_level);
2086
2087         if (direct_io) {
2088                 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
2089                                                     ppos, count, ocount);
2090                 if (written < 0) {
2091                         ret = written;
2092                         goto out_dio;
2093                 }
2094         } else {
2095                 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
2096                                                     count, written);
2097                 if (written < 0) {
2098                         ret = written;
2099                         if (ret != -EFAULT || ret != -ENOSPC)
2100                                 mlog_errno(ret);
2101                         goto out;
2102                 }
2103         }
2104
2105 out_dio:
2106         /* buffered aio wouldn't have proper lock coverage today */
2107         BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
2108
2109         /* 
2110          * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2111          * function pointer which is called when o_direct io completes so that
2112          * it can unlock our rw lock.  (it's the clustered equivalent of
2113          * i_alloc_sem; protects truncate from racing with pending ios).
2114          * Unfortunately there are error cases which call end_io and others
2115          * that don't.  so we don't have to unlock the rw_lock if either an
2116          * async dio is going to do it in the future or an end_io after an
2117          * error has already done it.
2118          */
2119         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2120                 rw_level = -1;
2121                 have_alloc_sem = 0;
2122         }
2123
2124 out:
2125         if (rw_level != -1)
2126                 ocfs2_rw_unlock(inode, rw_level);
2127
2128 out_sems:
2129         if (have_alloc_sem)
2130                 up_read(&inode->i_alloc_sem);
2131
2132         if (written > 0 && sync) {
2133                 ssize_t err;
2134
2135                 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
2136                 if (err < 0)
2137                         written = err;
2138         }
2139
2140         mutex_unlock(&inode->i_mutex);
2141
2142         mlog_exit(ret);
2143         return written ? written : ret;
2144 }
2145
2146 static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
2147                                     struct pipe_buffer *buf,
2148                                     struct splice_desc *sd)
2149 {
2150         int ret, count;
2151         ssize_t copied = 0;
2152         struct file *file = sd->u.file;
2153         unsigned int offset;
2154         struct page *page = NULL;
2155         void *fsdata;
2156         char *src, *dst;
2157
2158         ret = buf->ops->confirm(pipe, buf);
2159         if (ret)
2160                 goto out;
2161
2162         offset = sd->pos & ~PAGE_CACHE_MASK;
2163         count = sd->len;
2164         if (count + offset > PAGE_CACHE_SIZE)
2165                 count = PAGE_CACHE_SIZE - offset;
2166
2167         ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
2168                                 &page, &fsdata);
2169         if (ret) {
2170                 mlog_errno(ret);
2171                 goto out;
2172         }
2173
2174         src = buf->ops->map(pipe, buf, 1);
2175         dst = kmap_atomic(page, KM_USER1);
2176         memcpy(dst + offset, src + buf->offset, count);
2177         kunmap_atomic(dst, KM_USER1);
2178         buf->ops->unmap(pipe, buf, src);
2179
2180         copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
2181                                  page, fsdata);
2182         if (copied < 0) {
2183                 mlog_errno(copied);
2184                 ret = copied;
2185                 goto out;
2186         }
2187 out:
2188
2189         return copied ? copied : ret;
2190 }
2191
2192 static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2193                                          struct file *out,
2194                                          loff_t *ppos,
2195                                          size_t len,
2196                                          unsigned int flags)
2197 {
2198         int ret, err;
2199         struct address_space *mapping = out->f_mapping;
2200         struct inode *inode = mapping->host;
2201         struct splice_desc sd = {
2202                 .total_len = len,
2203                 .flags = flags,
2204                 .pos = *ppos,
2205                 .u.file = out,
2206         };
2207
2208         ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
2209         if (ret > 0) {
2210                 *ppos += ret;
2211
2212                 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
2213                         err = generic_osync_inode(inode, mapping,
2214                                                   OSYNC_METADATA|OSYNC_DATA);
2215                         if (err)
2216                                 ret = err;
2217                 }
2218         }
2219
2220         return ret;
2221 }
2222
2223 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2224                                        struct file *out,
2225                                        loff_t *ppos,
2226                                        size_t len,
2227                                        unsigned int flags)
2228 {
2229         int ret;
2230         struct inode *inode = out->f_path.dentry->d_inode;
2231
2232         mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
2233                    (unsigned int)len,
2234                    out->f_path.dentry->d_name.len,
2235                    out->f_path.dentry->d_name.name);
2236
2237         inode_double_lock(inode, pipe->inode);
2238
2239         ret = ocfs2_rw_lock(inode, 1);
2240         if (ret < 0) {
2241                 mlog_errno(ret);
2242                 goto out;
2243         }
2244
2245         ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
2246                                             NULL);
2247         if (ret < 0) {
2248                 mlog_errno(ret);
2249                 goto out_unlock;
2250         }
2251
2252         /* ok, we're done with i_size and alloc work */
2253         ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
2254
2255 out_unlock:
2256         ocfs2_rw_unlock(inode, 1);
2257 out:
2258         inode_double_unlock(inode, pipe->inode);
2259
2260         mlog_exit(ret);
2261         return ret;
2262 }
2263
2264 static ssize_t ocfs2_file_splice_read(struct file *in,
2265                                       loff_t *ppos,
2266                                       struct pipe_inode_info *pipe,
2267                                       size_t len,
2268                                       unsigned int flags)
2269 {
2270         int ret = 0;
2271         struct inode *inode = in->f_path.dentry->d_inode;
2272
2273         mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
2274                    (unsigned int)len,
2275                    in->f_path.dentry->d_name.len,
2276                    in->f_path.dentry->d_name.name);
2277
2278         /*
2279          * See the comment in ocfs2_file_aio_read()
2280          */
2281         ret = ocfs2_meta_lock(inode, NULL, 0);
2282         if (ret < 0) {
2283                 mlog_errno(ret);
2284                 goto bail;
2285         }
2286         ocfs2_meta_unlock(inode, 0);
2287
2288         ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2289
2290 bail:
2291         mlog_exit(ret);
2292         return ret;
2293 }
2294
2295 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
2296                                    const struct iovec *iov,
2297                                    unsigned long nr_segs,
2298                                    loff_t pos)
2299 {
2300         int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
2301         struct file *filp = iocb->ki_filp;
2302         struct inode *inode = filp->f_path.dentry->d_inode;
2303
2304         mlog_entry("(0x%p, %u, '%.*s')\n", filp,
2305                    (unsigned int)nr_segs,
2306                    filp->f_path.dentry->d_name.len,
2307                    filp->f_path.dentry->d_name.name);
2308
2309         if (!inode) {
2310                 ret = -EINVAL;
2311                 mlog_errno(ret);
2312                 goto bail;
2313         }
2314
2315         /* 
2316          * buffered reads protect themselves in ->readpage().  O_DIRECT reads
2317          * need locks to protect pending reads from racing with truncate.
2318          */
2319         if (filp->f_flags & O_DIRECT) {
2320                 down_read(&inode->i_alloc_sem);
2321                 have_alloc_sem = 1;
2322
2323                 ret = ocfs2_rw_lock(inode, 0);
2324                 if (ret < 0) {
2325                         mlog_errno(ret);
2326                         goto bail;
2327                 }
2328                 rw_level = 0;
2329                 /* communicate with ocfs2_dio_end_io */
2330                 ocfs2_iocb_set_rw_locked(iocb, rw_level);
2331         }
2332
2333         /*
2334          * We're fine letting folks race truncates and extending
2335          * writes with read across the cluster, just like they can
2336          * locally. Hence no rw_lock during read.
2337          * 
2338          * Take and drop the meta data lock to update inode fields
2339          * like i_size. This allows the checks down below
2340          * generic_file_aio_read() a chance of actually working. 
2341          */
2342         ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
2343         if (ret < 0) {
2344                 mlog_errno(ret);
2345                 goto bail;
2346         }
2347         ocfs2_meta_unlock(inode, lock_level);
2348
2349         ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
2350         if (ret == -EINVAL)
2351                 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
2352
2353         /* buffered aio wouldn't have proper lock coverage today */
2354         BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
2355
2356         /* see ocfs2_file_aio_write */
2357         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2358                 rw_level = -1;
2359                 have_alloc_sem = 0;
2360         }
2361
2362 bail:
2363         if (have_alloc_sem)
2364                 up_read(&inode->i_alloc_sem);
2365         if (rw_level != -1) 
2366                 ocfs2_rw_unlock(inode, rw_level);
2367         mlog_exit(ret);
2368
2369         return ret;
2370 }
2371
2372 const struct inode_operations ocfs2_file_iops = {
2373         .setattr        = ocfs2_setattr,
2374         .getattr        = ocfs2_getattr,
2375         .permission     = ocfs2_permission,
2376         .fallocate      = ocfs2_fallocate,
2377 };
2378
2379 const struct inode_operations ocfs2_special_file_iops = {
2380         .setattr        = ocfs2_setattr,
2381         .getattr        = ocfs2_getattr,
2382         .permission     = ocfs2_permission,
2383 };
2384
2385 const struct file_operations ocfs2_fops = {
2386         .read           = do_sync_read,
2387         .write          = do_sync_write,
2388         .mmap           = ocfs2_mmap,
2389         .fsync          = ocfs2_sync_file,
2390         .release        = ocfs2_file_release,
2391         .open           = ocfs2_file_open,
2392         .aio_read       = ocfs2_file_aio_read,
2393         .aio_write      = ocfs2_file_aio_write,
2394         .ioctl          = ocfs2_ioctl,
2395 #ifdef CONFIG_COMPAT
2396         .compat_ioctl   = ocfs2_compat_ioctl,
2397 #endif
2398         .splice_read    = ocfs2_file_splice_read,
2399         .splice_write   = ocfs2_file_splice_write,
2400 };
2401
2402 const struct file_operations ocfs2_dops = {
2403         .read           = generic_read_dir,
2404         .readdir        = ocfs2_readdir,
2405         .fsync          = ocfs2_sync_file,
2406         .ioctl          = ocfs2_ioctl,
2407 #ifdef CONFIG_COMPAT
2408         .compat_ioctl   = ocfs2_compat_ioctl,
2409 #endif
2410 };