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