ALSA: pcm: Use dma_bytes as size parameter in dma_mmap_coherent()
[pandora-kernel.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 #include <linux/quotaops.h>
39 #include <linux/blkdev.h>
40
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 #include "ocfs2_trace.h"
64
65 #include "buffer_head_io.h"
66
67 static int ocfs2_init_file_private(struct inode *inode, struct file *file)
68 {
69         struct ocfs2_file_private *fp;
70
71         fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
72         if (!fp)
73                 return -ENOMEM;
74
75         fp->fp_file = file;
76         mutex_init(&fp->fp_mutex);
77         ocfs2_file_lock_res_init(&fp->fp_flock, fp);
78         file->private_data = fp;
79
80         return 0;
81 }
82
83 static void ocfs2_free_file_private(struct inode *inode, struct file *file)
84 {
85         struct ocfs2_file_private *fp = file->private_data;
86         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
87
88         if (fp) {
89                 ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
90                 ocfs2_lock_res_free(&fp->fp_flock);
91                 kfree(fp);
92                 file->private_data = NULL;
93         }
94 }
95
96 static int ocfs2_file_open(struct inode *inode, struct file *file)
97 {
98         int status;
99         int mode = file->f_flags;
100         struct ocfs2_inode_info *oi = OCFS2_I(inode);
101
102         trace_ocfs2_file_open(inode, file, file->f_path.dentry,
103                               (unsigned long long)OCFS2_I(inode)->ip_blkno,
104                               file->f_path.dentry->d_name.len,
105                               file->f_path.dentry->d_name.name, mode);
106
107         if (file->f_mode & FMODE_WRITE)
108                 dquot_initialize(inode);
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         return status;
141 }
142
143 static int ocfs2_file_release(struct inode *inode, struct file *file)
144 {
145         struct ocfs2_inode_info *oi = OCFS2_I(inode);
146
147         spin_lock(&oi->ip_lock);
148         if (!--oi->ip_open_count)
149                 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
150
151         trace_ocfs2_file_release(inode, file, file->f_path.dentry,
152                                  oi->ip_blkno,
153                                  file->f_path.dentry->d_name.len,
154                                  file->f_path.dentry->d_name.name,
155                                  oi->ip_open_count);
156         spin_unlock(&oi->ip_lock);
157
158         ocfs2_free_file_private(inode, file);
159
160         return 0;
161 }
162
163 static int ocfs2_dir_open(struct inode *inode, struct file *file)
164 {
165         return ocfs2_init_file_private(inode, file);
166 }
167
168 static int ocfs2_dir_release(struct inode *inode, struct file *file)
169 {
170         ocfs2_free_file_private(inode, file);
171         return 0;
172 }
173
174 static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
175                            int datasync)
176 {
177         int err = 0;
178         journal_t *journal;
179         struct inode *inode = file->f_mapping->host;
180         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
181
182         trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
183                               OCFS2_I(inode)->ip_blkno,
184                               file->f_path.dentry->d_name.len,
185                               file->f_path.dentry->d_name.name,
186                               (unsigned long long)datasync);
187
188         err = filemap_write_and_wait_range(inode->i_mapping, start, end);
189         if (err)
190                 return err;
191
192         /*
193          * Probably don't need the i_mutex at all in here, just putting it here
194          * to be consistent with how fsync used to be called, someone more
195          * familiar with the fs could possibly remove it.
196          */
197         mutex_lock(&inode->i_mutex);
198         if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) {
199                 /*
200                  * We still have to flush drive's caches to get data to the
201                  * platter
202                  */
203                 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
204                         blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
205                 goto bail;
206         }
207
208         journal = osb->journal->j_journal;
209         err = jbd2_journal_force_commit(journal);
210
211 bail:
212         if (err)
213                 mlog_errno(err);
214         mutex_unlock(&inode->i_mutex);
215
216         return (err < 0) ? -EIO : 0;
217 }
218
219 int ocfs2_should_update_atime(struct inode *inode,
220                               struct vfsmount *vfsmnt)
221 {
222         struct timespec now;
223         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
224
225         if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
226                 return 0;
227
228         if ((inode->i_flags & S_NOATIME) ||
229             ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
230                 return 0;
231
232         /*
233          * We can be called with no vfsmnt structure - NFSD will
234          * sometimes do this.
235          *
236          * Note that our action here is different than touch_atime() -
237          * if we can't tell whether this is a noatime mount, then we
238          * don't know whether to trust the value of s_atime_quantum.
239          */
240         if (vfsmnt == NULL)
241                 return 0;
242
243         if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
244             ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
245                 return 0;
246
247         if (vfsmnt->mnt_flags & MNT_RELATIME) {
248                 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
249                     (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
250                         return 1;
251
252                 return 0;
253         }
254
255         now = CURRENT_TIME;
256         if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
257                 return 0;
258         else
259                 return 1;
260 }
261
262 int ocfs2_update_inode_atime(struct inode *inode,
263                              struct buffer_head *bh)
264 {
265         int ret;
266         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
267         handle_t *handle;
268         struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
269
270         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
271         if (IS_ERR(handle)) {
272                 ret = PTR_ERR(handle);
273                 mlog_errno(ret);
274                 goto out;
275         }
276
277         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
278                                       OCFS2_JOURNAL_ACCESS_WRITE);
279         if (ret) {
280                 mlog_errno(ret);
281                 goto out_commit;
282         }
283
284         /*
285          * Don't use ocfs2_mark_inode_dirty() here as we don't always
286          * have i_mutex to guard against concurrent changes to other
287          * inode fields.
288          */
289         inode->i_atime = CURRENT_TIME;
290         di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
291         di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
292         ocfs2_journal_dirty(handle, bh);
293
294 out_commit:
295         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
296 out:
297         return ret;
298 }
299
300 static int ocfs2_set_inode_size(handle_t *handle,
301                                 struct inode *inode,
302                                 struct buffer_head *fe_bh,
303                                 u64 new_i_size)
304 {
305         int status;
306
307         i_size_write(inode, new_i_size);
308         inode->i_blocks = ocfs2_inode_sector_count(inode);
309         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
310
311         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
312         if (status < 0) {
313                 mlog_errno(status);
314                 goto bail;
315         }
316
317 bail:
318         return status;
319 }
320
321 int ocfs2_simple_size_update(struct inode *inode,
322                              struct buffer_head *di_bh,
323                              u64 new_i_size)
324 {
325         int ret;
326         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
327         handle_t *handle = NULL;
328
329         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
330         if (IS_ERR(handle)) {
331                 ret = PTR_ERR(handle);
332                 mlog_errno(ret);
333                 goto out;
334         }
335
336         ret = ocfs2_set_inode_size(handle, inode, di_bh,
337                                    new_i_size);
338         if (ret < 0)
339                 mlog_errno(ret);
340
341         ocfs2_commit_trans(osb, handle);
342 out:
343         return ret;
344 }
345
346 static int ocfs2_cow_file_pos(struct inode *inode,
347                               struct buffer_head *fe_bh,
348                               u64 offset)
349 {
350         int status;
351         u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
352         unsigned int num_clusters = 0;
353         unsigned int ext_flags = 0;
354
355         /*
356          * If the new offset is aligned to the range of the cluster, there is
357          * no space for ocfs2_zero_range_for_truncate to fill, so no need to
358          * CoW either.
359          */
360         if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
361                 return 0;
362
363         status = ocfs2_get_clusters(inode, cpos, &phys,
364                                     &num_clusters, &ext_flags);
365         if (status) {
366                 mlog_errno(status);
367                 goto out;
368         }
369
370         if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
371                 goto out;
372
373         return ocfs2_refcount_cow(inode, NULL, fe_bh, cpos, 1, cpos+1);
374
375 out:
376         return status;
377 }
378
379 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
380                                      struct inode *inode,
381                                      struct buffer_head *fe_bh,
382                                      u64 new_i_size)
383 {
384         int status;
385         handle_t *handle;
386         struct ocfs2_dinode *di;
387         u64 cluster_bytes;
388
389         /*
390          * We need to CoW the cluster contains the offset if it is reflinked
391          * since we will call ocfs2_zero_range_for_truncate later which will
392          * write "0" from offset to the end of the cluster.
393          */
394         status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
395         if (status) {
396                 mlog_errno(status);
397                 return status;
398         }
399
400         /* TODO: This needs to actually orphan the inode in this
401          * transaction. */
402
403         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
404         if (IS_ERR(handle)) {
405                 status = PTR_ERR(handle);
406                 mlog_errno(status);
407                 goto out;
408         }
409
410         status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
411                                          OCFS2_JOURNAL_ACCESS_WRITE);
412         if (status < 0) {
413                 mlog_errno(status);
414                 goto out_commit;
415         }
416
417         /*
418          * Do this before setting i_size.
419          */
420         cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
421         status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
422                                                cluster_bytes);
423         if (status) {
424                 mlog_errno(status);
425                 goto out_commit;
426         }
427
428         i_size_write(inode, new_i_size);
429         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
430
431         di = (struct ocfs2_dinode *) fe_bh->b_data;
432         di->i_size = cpu_to_le64(new_i_size);
433         di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
434         di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
435
436         ocfs2_journal_dirty(handle, fe_bh);
437
438 out_commit:
439         ocfs2_commit_trans(osb, handle);
440 out:
441         return status;
442 }
443
444 static int ocfs2_truncate_file(struct inode *inode,
445                                struct buffer_head *di_bh,
446                                u64 new_i_size)
447 {
448         int status = 0;
449         struct ocfs2_dinode *fe = NULL;
450         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
451
452         /* We trust di_bh because it comes from ocfs2_inode_lock(), which
453          * already validated it */
454         fe = (struct ocfs2_dinode *) di_bh->b_data;
455
456         trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
457                                   (unsigned long long)le64_to_cpu(fe->i_size),
458                                   (unsigned long long)new_i_size);
459
460         mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
461                         "Inode %llu, inode i_size = %lld != di "
462                         "i_size = %llu, i_flags = 0x%x\n",
463                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
464                         i_size_read(inode),
465                         (unsigned long long)le64_to_cpu(fe->i_size),
466                         le32_to_cpu(fe->i_flags));
467
468         if (new_i_size > le64_to_cpu(fe->i_size)) {
469                 trace_ocfs2_truncate_file_error(
470                         (unsigned long long)le64_to_cpu(fe->i_size),
471                         (unsigned long long)new_i_size);
472                 status = -EINVAL;
473                 mlog_errno(status);
474                 goto bail;
475         }
476
477         down_write(&OCFS2_I(inode)->ip_alloc_sem);
478
479         ocfs2_resv_discard(&osb->osb_la_resmap,
480                            &OCFS2_I(inode)->ip_la_data_resv);
481
482         /*
483          * The inode lock forced other nodes to sync and drop their
484          * pages, which (correctly) happens even if we have a truncate
485          * without allocation change - ocfs2 cluster sizes can be much
486          * greater than page size, so we have to truncate them
487          * anyway.
488          */
489         unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
490         truncate_inode_pages(inode->i_mapping, new_i_size);
491
492         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
493                 status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
494                                                i_size_read(inode), 1);
495                 if (status)
496                         mlog_errno(status);
497
498                 goto bail_unlock_sem;
499         }
500
501         /* alright, we're going to need to do a full blown alloc size
502          * change. Orphan the inode so that recovery can complete the
503          * truncate if necessary. This does the task of marking
504          * i_size. */
505         status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
506         if (status < 0) {
507                 mlog_errno(status);
508                 goto bail_unlock_sem;
509         }
510
511         status = ocfs2_commit_truncate(osb, inode, di_bh);
512         if (status < 0) {
513                 mlog_errno(status);
514                 goto bail_unlock_sem;
515         }
516
517         /* TODO: orphan dir cleanup here. */
518 bail_unlock_sem:
519         up_write(&OCFS2_I(inode)->ip_alloc_sem);
520
521 bail:
522         if (!status && OCFS2_I(inode)->ip_clusters == 0)
523                 status = ocfs2_try_remove_refcount_tree(inode, di_bh);
524
525         return status;
526 }
527
528 /*
529  * extend file allocation only here.
530  * we'll update all the disk stuff, and oip->alloc_size
531  *
532  * expect stuff to be locked, a transaction started and enough data /
533  * metadata reservations in the contexts.
534  *
535  * Will return -EAGAIN, and a reason if a restart is needed.
536  * If passed in, *reason will always be set, even in error.
537  */
538 int ocfs2_add_inode_data(struct ocfs2_super *osb,
539                          struct inode *inode,
540                          u32 *logical_offset,
541                          u32 clusters_to_add,
542                          int mark_unwritten,
543                          struct buffer_head *fe_bh,
544                          handle_t *handle,
545                          struct ocfs2_alloc_context *data_ac,
546                          struct ocfs2_alloc_context *meta_ac,
547                          enum ocfs2_alloc_restarted *reason_ret)
548 {
549         int ret;
550         struct ocfs2_extent_tree et;
551
552         ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
553         ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
554                                           clusters_to_add, mark_unwritten,
555                                           data_ac, meta_ac, reason_ret);
556
557         return ret;
558 }
559
560 static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
561                                      u32 clusters_to_add, int mark_unwritten)
562 {
563         int status = 0;
564         int restart_func = 0;
565         int credits;
566         u32 prev_clusters;
567         struct buffer_head *bh = NULL;
568         struct ocfs2_dinode *fe = NULL;
569         handle_t *handle = NULL;
570         struct ocfs2_alloc_context *data_ac = NULL;
571         struct ocfs2_alloc_context *meta_ac = NULL;
572         enum ocfs2_alloc_restarted why;
573         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
574         struct ocfs2_extent_tree et;
575         int did_quota = 0;
576
577         /*
578          * This function only exists for file systems which don't
579          * support holes.
580          */
581         BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
582
583         status = ocfs2_read_inode_block(inode, &bh);
584         if (status < 0) {
585                 mlog_errno(status);
586                 goto leave;
587         }
588         fe = (struct ocfs2_dinode *) bh->b_data;
589
590 restart_all:
591         BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
592
593         ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
594         status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
595                                        &data_ac, &meta_ac);
596         if (status) {
597                 mlog_errno(status);
598                 goto leave;
599         }
600
601         credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
602                                             clusters_to_add);
603         handle = ocfs2_start_trans(osb, credits);
604         if (IS_ERR(handle)) {
605                 status = PTR_ERR(handle);
606                 handle = NULL;
607                 mlog_errno(status);
608                 goto leave;
609         }
610
611 restarted_transaction:
612         trace_ocfs2_extend_allocation(
613                 (unsigned long long)OCFS2_I(inode)->ip_blkno,
614                 (unsigned long long)i_size_read(inode),
615                 le32_to_cpu(fe->i_clusters), clusters_to_add,
616                 why, restart_func);
617
618         status = dquot_alloc_space_nodirty(inode,
619                         ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
620         if (status)
621                 goto leave;
622         did_quota = 1;
623
624         /* reserve a write to the file entry early on - that we if we
625          * run out of credits in the allocation path, we can still
626          * update i_size. */
627         status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
628                                          OCFS2_JOURNAL_ACCESS_WRITE);
629         if (status < 0) {
630                 mlog_errno(status);
631                 goto leave;
632         }
633
634         prev_clusters = OCFS2_I(inode)->ip_clusters;
635
636         status = ocfs2_add_inode_data(osb,
637                                       inode,
638                                       &logical_start,
639                                       clusters_to_add,
640                                       mark_unwritten,
641                                       bh,
642                                       handle,
643                                       data_ac,
644                                       meta_ac,
645                                       &why);
646         if ((status < 0) && (status != -EAGAIN)) {
647                 if (status != -ENOSPC)
648                         mlog_errno(status);
649                 goto leave;
650         }
651
652         ocfs2_journal_dirty(handle, bh);
653
654         spin_lock(&OCFS2_I(inode)->ip_lock);
655         clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
656         spin_unlock(&OCFS2_I(inode)->ip_lock);
657         /* Release unused quota reservation */
658         dquot_free_space(inode,
659                         ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
660         did_quota = 0;
661
662         if (why != RESTART_NONE && clusters_to_add) {
663                 if (why == RESTART_META) {
664                         restart_func = 1;
665                         status = 0;
666                 } else {
667                         BUG_ON(why != RESTART_TRANS);
668
669                         /* TODO: This can be more intelligent. */
670                         credits = ocfs2_calc_extend_credits(osb->sb,
671                                                             &fe->id2.i_list,
672                                                             clusters_to_add);
673                         status = ocfs2_extend_trans(handle, credits);
674                         if (status < 0) {
675                                 /* handle still has to be committed at
676                                  * this point. */
677                                 status = -ENOMEM;
678                                 mlog_errno(status);
679                                 goto leave;
680                         }
681                         goto restarted_transaction;
682                 }
683         }
684
685         trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
686              le32_to_cpu(fe->i_clusters),
687              (unsigned long long)le64_to_cpu(fe->i_size),
688              OCFS2_I(inode)->ip_clusters,
689              (unsigned long long)i_size_read(inode));
690
691 leave:
692         if (status < 0 && did_quota)
693                 dquot_free_space(inode,
694                         ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
695         if (handle) {
696                 ocfs2_commit_trans(osb, handle);
697                 handle = NULL;
698         }
699         if (data_ac) {
700                 ocfs2_free_alloc_context(data_ac);
701                 data_ac = NULL;
702         }
703         if (meta_ac) {
704                 ocfs2_free_alloc_context(meta_ac);
705                 meta_ac = NULL;
706         }
707         if ((!status) && restart_func) {
708                 restart_func = 0;
709                 goto restart_all;
710         }
711         brelse(bh);
712         bh = NULL;
713
714         return status;
715 }
716
717 /*
718  * While a write will already be ordering the data, a truncate will not.
719  * Thus, we need to explicitly order the zeroed pages.
720  */
721 static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
722 {
723         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
724         handle_t *handle = NULL;
725         int ret = 0;
726
727         if (!ocfs2_should_order_data(inode))
728                 goto out;
729
730         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
731         if (IS_ERR(handle)) {
732                 ret = -ENOMEM;
733                 mlog_errno(ret);
734                 goto out;
735         }
736
737         ret = ocfs2_jbd2_file_inode(handle, inode);
738         if (ret < 0)
739                 mlog_errno(ret);
740
741 out:
742         if (ret) {
743                 if (!IS_ERR(handle))
744                         ocfs2_commit_trans(osb, handle);
745                 handle = ERR_PTR(ret);
746         }
747         return handle;
748 }
749
750 /* Some parts of this taken from generic_cont_expand, which turned out
751  * to be too fragile to do exactly what we need without us having to
752  * worry about recursive locking in ->write_begin() and ->write_end(). */
753 static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
754                                  u64 abs_to)
755 {
756         struct address_space *mapping = inode->i_mapping;
757         struct page *page;
758         unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
759         handle_t *handle = NULL;
760         int ret = 0;
761         unsigned zero_from, zero_to, block_start, block_end;
762
763         BUG_ON(abs_from >= abs_to);
764         BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
765         BUG_ON(abs_from & (inode->i_blkbits - 1));
766
767         page = find_or_create_page(mapping, index, GFP_NOFS);
768         if (!page) {
769                 ret = -ENOMEM;
770                 mlog_errno(ret);
771                 goto out;
772         }
773
774         /* Get the offsets within the page that we want to zero */
775         zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
776         zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
777         if (!zero_to)
778                 zero_to = PAGE_CACHE_SIZE;
779
780         trace_ocfs2_write_zero_page(
781                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
782                         (unsigned long long)abs_from,
783                         (unsigned long long)abs_to,
784                         index, zero_from, zero_to);
785
786         /* We know that zero_from is block aligned */
787         for (block_start = zero_from; block_start < zero_to;
788              block_start = block_end) {
789                 block_end = block_start + (1 << inode->i_blkbits);
790
791                 /*
792                  * block_start is block-aligned.  Bump it by one to force
793                  * __block_write_begin and block_commit_write to zero the
794                  * whole block.
795                  */
796                 ret = __block_write_begin(page, block_start + 1, 0,
797                                           ocfs2_get_block);
798                 if (ret < 0) {
799                         mlog_errno(ret);
800                         goto out_unlock;
801                 }
802
803                 if (!handle) {
804                         handle = ocfs2_zero_start_ordered_transaction(inode);
805                         if (IS_ERR(handle)) {
806                                 ret = PTR_ERR(handle);
807                                 handle = NULL;
808                                 break;
809                         }
810                 }
811
812                 /* must not update i_size! */
813                 ret = block_commit_write(page, block_start + 1,
814                                          block_start + 1);
815                 if (ret < 0)
816                         mlog_errno(ret);
817                 else
818                         ret = 0;
819         }
820
821         if (handle)
822                 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
823
824 out_unlock:
825         unlock_page(page);
826         page_cache_release(page);
827 out:
828         return ret;
829 }
830
831 /*
832  * Find the next range to zero.  We do this in terms of bytes because
833  * that's what ocfs2_zero_extend() wants, and it is dealing with the
834  * pagecache.  We may return multiple extents.
835  *
836  * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
837  * needs to be zeroed.  range_start and range_end return the next zeroing
838  * range.  A subsequent call should pass the previous range_end as its
839  * zero_start.  If range_end is 0, there's nothing to do.
840  *
841  * Unwritten extents are skipped over.  Refcounted extents are CoWd.
842  */
843 static int ocfs2_zero_extend_get_range(struct inode *inode,
844                                        struct buffer_head *di_bh,
845                                        u64 zero_start, u64 zero_end,
846                                        u64 *range_start, u64 *range_end)
847 {
848         int rc = 0, needs_cow = 0;
849         u32 p_cpos, zero_clusters = 0;
850         u32 zero_cpos =
851                 zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
852         u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
853         unsigned int num_clusters = 0;
854         unsigned int ext_flags = 0;
855
856         while (zero_cpos < last_cpos) {
857                 rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
858                                         &num_clusters, &ext_flags);
859                 if (rc) {
860                         mlog_errno(rc);
861                         goto out;
862                 }
863
864                 if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
865                         zero_clusters = num_clusters;
866                         if (ext_flags & OCFS2_EXT_REFCOUNTED)
867                                 needs_cow = 1;
868                         break;
869                 }
870
871                 zero_cpos += num_clusters;
872         }
873         if (!zero_clusters) {
874                 *range_end = 0;
875                 goto out;
876         }
877
878         while ((zero_cpos + zero_clusters) < last_cpos) {
879                 rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
880                                         &p_cpos, &num_clusters,
881                                         &ext_flags);
882                 if (rc) {
883                         mlog_errno(rc);
884                         goto out;
885                 }
886
887                 if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
888                         break;
889                 if (ext_flags & OCFS2_EXT_REFCOUNTED)
890                         needs_cow = 1;
891                 zero_clusters += num_clusters;
892         }
893         if ((zero_cpos + zero_clusters) > last_cpos)
894                 zero_clusters = last_cpos - zero_cpos;
895
896         if (needs_cow) {
897                 rc = ocfs2_refcount_cow(inode, NULL, di_bh, zero_cpos,
898                                         zero_clusters, UINT_MAX);
899                 if (rc) {
900                         mlog_errno(rc);
901                         goto out;
902                 }
903         }
904
905         *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
906         *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
907                                              zero_cpos + zero_clusters);
908
909 out:
910         return rc;
911 }
912
913 /*
914  * Zero one range returned from ocfs2_zero_extend_get_range().  The caller
915  * has made sure that the entire range needs zeroing.
916  */
917 static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
918                                    u64 range_end)
919 {
920         int rc = 0;
921         u64 next_pos;
922         u64 zero_pos = range_start;
923
924         trace_ocfs2_zero_extend_range(
925                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
926                         (unsigned long long)range_start,
927                         (unsigned long long)range_end);
928         BUG_ON(range_start >= range_end);
929
930         while (zero_pos < range_end) {
931                 next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
932                 if (next_pos > range_end)
933                         next_pos = range_end;
934                 rc = ocfs2_write_zero_page(inode, zero_pos, next_pos);
935                 if (rc < 0) {
936                         mlog_errno(rc);
937                         break;
938                 }
939                 zero_pos = next_pos;
940
941                 /*
942                  * Very large extends have the potential to lock up
943                  * the cpu for extended periods of time.
944                  */
945                 cond_resched();
946         }
947
948         return rc;
949 }
950
951 int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
952                       loff_t zero_to_size)
953 {
954         int ret = 0;
955         u64 zero_start, range_start = 0, range_end = 0;
956         struct super_block *sb = inode->i_sb;
957
958         zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
959         trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
960                                 (unsigned long long)zero_start,
961                                 (unsigned long long)i_size_read(inode));
962         while (zero_start < zero_to_size) {
963                 ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
964                                                   zero_to_size,
965                                                   &range_start,
966                                                   &range_end);
967                 if (ret) {
968                         mlog_errno(ret);
969                         break;
970                 }
971                 if (!range_end)
972                         break;
973                 /* Trim the ends */
974                 if (range_start < zero_start)
975                         range_start = zero_start;
976                 if (range_end > zero_to_size)
977                         range_end = zero_to_size;
978
979                 ret = ocfs2_zero_extend_range(inode, range_start,
980                                               range_end);
981                 if (ret) {
982                         mlog_errno(ret);
983                         break;
984                 }
985                 zero_start = range_end;
986         }
987
988         return ret;
989 }
990
991 int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
992                           u64 new_i_size, u64 zero_to)
993 {
994         int ret;
995         u32 clusters_to_add;
996         struct ocfs2_inode_info *oi = OCFS2_I(inode);
997
998         /*
999          * Only quota files call this without a bh, and they can't be
1000          * refcounted.
1001          */
1002         BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
1003         BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
1004
1005         clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
1006         if (clusters_to_add < oi->ip_clusters)
1007                 clusters_to_add = 0;
1008         else
1009                 clusters_to_add -= oi->ip_clusters;
1010
1011         if (clusters_to_add) {
1012                 ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
1013                                                 clusters_to_add, 0);
1014                 if (ret) {
1015                         mlog_errno(ret);
1016                         goto out;
1017                 }
1018         }
1019
1020         /*
1021          * Call this even if we don't add any clusters to the tree. We
1022          * still need to zero the area between the old i_size and the
1023          * new i_size.
1024          */
1025         ret = ocfs2_zero_extend(inode, di_bh, zero_to);
1026         if (ret < 0)
1027                 mlog_errno(ret);
1028
1029 out:
1030         return ret;
1031 }
1032
1033 static int ocfs2_extend_file(struct inode *inode,
1034                              struct buffer_head *di_bh,
1035                              u64 new_i_size)
1036 {
1037         int ret = 0;
1038         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1039
1040         BUG_ON(!di_bh);
1041
1042         /* setattr sometimes calls us like this. */
1043         if (new_i_size == 0)
1044                 goto out;
1045
1046         if (i_size_read(inode) == new_i_size)
1047                 goto out;
1048         BUG_ON(new_i_size < i_size_read(inode));
1049
1050         /*
1051          * The alloc sem blocks people in read/write from reading our
1052          * allocation until we're done changing it. We depend on
1053          * i_mutex to block other extend/truncate calls while we're
1054          * here.  We even have to hold it for sparse files because there
1055          * might be some tail zeroing.
1056          */
1057         down_write(&oi->ip_alloc_sem);
1058
1059         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1060                 /*
1061                  * We can optimize small extends by keeping the inodes
1062                  * inline data.
1063                  */
1064                 if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
1065                         up_write(&oi->ip_alloc_sem);
1066                         goto out_update_size;
1067                 }
1068
1069                 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1070                 if (ret) {
1071                         up_write(&oi->ip_alloc_sem);
1072                         mlog_errno(ret);
1073                         goto out;
1074                 }
1075         }
1076
1077         if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
1078                 ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
1079         else
1080                 ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
1081                                             new_i_size);
1082
1083         up_write(&oi->ip_alloc_sem);
1084
1085         if (ret < 0) {
1086                 mlog_errno(ret);
1087                 goto out;
1088         }
1089
1090 out_update_size:
1091         ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
1092         if (ret < 0)
1093                 mlog_errno(ret);
1094
1095 out:
1096         return ret;
1097 }
1098
1099 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1100 {
1101         int status = 0, size_change;
1102         struct inode *inode = dentry->d_inode;
1103         struct super_block *sb = inode->i_sb;
1104         struct ocfs2_super *osb = OCFS2_SB(sb);
1105         struct buffer_head *bh = NULL;
1106         handle_t *handle = NULL;
1107         struct dquot *transfer_to[MAXQUOTAS] = { };
1108         int qtype;
1109
1110         trace_ocfs2_setattr(inode, dentry,
1111                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
1112                             dentry->d_name.len, dentry->d_name.name,
1113                             attr->ia_valid, attr->ia_mode,
1114                             attr->ia_uid, attr->ia_gid);
1115
1116         /* ensuring we don't even attempt to truncate a symlink */
1117         if (S_ISLNK(inode->i_mode))
1118                 attr->ia_valid &= ~ATTR_SIZE;
1119
1120 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1121                            | ATTR_GID | ATTR_UID | ATTR_MODE)
1122         if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
1123                 return 0;
1124
1125         status = setattr_prepare(dentry, attr);
1126         if (status)
1127                 return status;
1128
1129         if (is_quota_modification(inode, attr))
1130                 dquot_initialize(inode);
1131         size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1132         if (size_change) {
1133                 status = ocfs2_rw_lock(inode, 1);
1134                 if (status < 0) {
1135                         mlog_errno(status);
1136                         goto bail;
1137                 }
1138         }
1139
1140         status = ocfs2_inode_lock(inode, &bh, 1);
1141         if (status < 0) {
1142                 if (status != -ENOENT)
1143                         mlog_errno(status);
1144                 goto bail_unlock_rw;
1145         }
1146
1147         if (size_change) {
1148                 status = inode_newsize_ok(inode, attr->ia_size);
1149                 if (status)
1150                         goto bail_unlock;
1151
1152                 inode_dio_wait(inode);
1153
1154                 if (i_size_read(inode) >= attr->ia_size) {
1155                         if (ocfs2_should_order_data(inode)) {
1156                                 status = ocfs2_begin_ordered_truncate(inode,
1157                                                                       attr->ia_size);
1158                                 if (status)
1159                                         goto bail_unlock;
1160                         }
1161                         status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1162                 } else
1163                         status = ocfs2_extend_file(inode, bh, attr->ia_size);
1164                 if (status < 0) {
1165                         if (status != -ENOSPC)
1166                                 mlog_errno(status);
1167                         status = -ENOSPC;
1168                         goto bail_unlock;
1169                 }
1170         }
1171
1172         if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
1173             (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
1174                 /*
1175                  * Gather pointers to quota structures so that allocation /
1176                  * freeing of quota structures happens here and not inside
1177                  * dquot_transfer() where we have problems with lock ordering
1178                  */
1179                 if (attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid
1180                     && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1181                     OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1182                         transfer_to[USRQUOTA] = dqget(sb, attr->ia_uid,
1183                                                       USRQUOTA);
1184                         if (!transfer_to[USRQUOTA]) {
1185                                 status = -ESRCH;
1186                                 goto bail_unlock;
1187                         }
1188                 }
1189                 if (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid
1190                     && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1191                     OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1192                         transfer_to[GRPQUOTA] = dqget(sb, attr->ia_gid,
1193                                                       GRPQUOTA);
1194                         if (!transfer_to[GRPQUOTA]) {
1195                                 status = -ESRCH;
1196                                 goto bail_unlock;
1197                         }
1198                 }
1199                 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
1200                                            2 * ocfs2_quota_trans_credits(sb));
1201                 if (IS_ERR(handle)) {
1202                         status = PTR_ERR(handle);
1203                         mlog_errno(status);
1204                         goto bail_unlock;
1205                 }
1206                 status = __dquot_transfer(inode, transfer_to);
1207                 if (status < 0)
1208                         goto bail_commit;
1209         } else {
1210                 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1211                 if (IS_ERR(handle)) {
1212                         status = PTR_ERR(handle);
1213                         mlog_errno(status);
1214                         goto bail_unlock;
1215                 }
1216         }
1217
1218         /*
1219          * This will intentionally not wind up calling truncate_setsize(),
1220          * since all the work for a size change has been done above.
1221          * Otherwise, we could get into problems with truncate as
1222          * ip_alloc_sem is used there to protect against i_size
1223          * changes.
1224          *
1225          * XXX: this means the conditional below can probably be removed.
1226          */
1227         if ((attr->ia_valid & ATTR_SIZE) &&
1228             attr->ia_size != i_size_read(inode)) {
1229                 status = vmtruncate(inode, attr->ia_size);
1230                 if (status) {
1231                         mlog_errno(status);
1232                         goto bail_commit;
1233                 }
1234         }
1235
1236         setattr_copy(inode, attr);
1237         mark_inode_dirty(inode);
1238
1239         status = ocfs2_mark_inode_dirty(handle, inode, bh);
1240         if (status < 0)
1241                 mlog_errno(status);
1242
1243 bail_commit:
1244         ocfs2_commit_trans(osb, handle);
1245 bail_unlock:
1246         ocfs2_inode_unlock(inode, 1);
1247 bail_unlock_rw:
1248         if (size_change)
1249                 ocfs2_rw_unlock(inode, 1);
1250 bail:
1251         brelse(bh);
1252
1253         /* Release quota pointers in case we acquired them */
1254         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1255                 dqput(transfer_to[qtype]);
1256
1257         if (!status && attr->ia_valid & ATTR_MODE) {
1258                 status = ocfs2_acl_chmod(inode);
1259                 if (status < 0)
1260                         mlog_errno(status);
1261         }
1262
1263         return status;
1264 }
1265
1266 int ocfs2_getattr(struct vfsmount *mnt,
1267                   struct dentry *dentry,
1268                   struct kstat *stat)
1269 {
1270         struct inode *inode = dentry->d_inode;
1271         struct super_block *sb = dentry->d_inode->i_sb;
1272         struct ocfs2_super *osb = sb->s_fs_info;
1273         int err;
1274
1275         err = ocfs2_inode_revalidate(dentry);
1276         if (err) {
1277                 if (err != -ENOENT)
1278                         mlog_errno(err);
1279                 goto bail;
1280         }
1281
1282         generic_fillattr(inode, stat);
1283
1284         /* We set the blksize from the cluster size for performance */
1285         stat->blksize = osb->s_clustersize;
1286
1287 bail:
1288         return err;
1289 }
1290
1291 int ocfs2_permission(struct inode *inode, int mask)
1292 {
1293         int ret;
1294
1295         if (mask & MAY_NOT_BLOCK)
1296                 return -ECHILD;
1297
1298         ret = ocfs2_inode_lock(inode, NULL, 0);
1299         if (ret) {
1300                 if (ret != -ENOENT)
1301                         mlog_errno(ret);
1302                 goto out;
1303         }
1304
1305         ret = generic_permission(inode, mask);
1306
1307         ocfs2_inode_unlock(inode, 0);
1308 out:
1309         return ret;
1310 }
1311
1312 static int __ocfs2_write_remove_suid(struct inode *inode,
1313                                      struct buffer_head *bh)
1314 {
1315         int ret;
1316         handle_t *handle;
1317         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1318         struct ocfs2_dinode *di;
1319
1320         trace_ocfs2_write_remove_suid(
1321                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
1322                         inode->i_mode);
1323
1324         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1325         if (IS_ERR(handle)) {
1326                 ret = PTR_ERR(handle);
1327                 mlog_errno(ret);
1328                 goto out;
1329         }
1330
1331         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1332                                       OCFS2_JOURNAL_ACCESS_WRITE);
1333         if (ret < 0) {
1334                 mlog_errno(ret);
1335                 goto out_trans;
1336         }
1337
1338         inode->i_mode &= ~S_ISUID;
1339         if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1340                 inode->i_mode &= ~S_ISGID;
1341
1342         di = (struct ocfs2_dinode *) bh->b_data;
1343         di->i_mode = cpu_to_le16(inode->i_mode);
1344
1345         ocfs2_journal_dirty(handle, bh);
1346
1347 out_trans:
1348         ocfs2_commit_trans(osb, handle);
1349 out:
1350         return ret;
1351 }
1352
1353 /*
1354  * Will look for holes and unwritten extents in the range starting at
1355  * pos for count bytes (inclusive).
1356  */
1357 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1358                                        size_t count)
1359 {
1360         int ret = 0;
1361         unsigned int extent_flags;
1362         u32 cpos, clusters, extent_len, phys_cpos;
1363         struct super_block *sb = inode->i_sb;
1364
1365         cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1366         clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1367
1368         while (clusters) {
1369                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1370                                          &extent_flags);
1371                 if (ret < 0) {
1372                         mlog_errno(ret);
1373                         goto out;
1374                 }
1375
1376                 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1377                         ret = 1;
1378                         break;
1379                 }
1380
1381                 if (extent_len > clusters)
1382                         extent_len = clusters;
1383
1384                 clusters -= extent_len;
1385                 cpos += extent_len;
1386         }
1387 out:
1388         return ret;
1389 }
1390
1391 static int ocfs2_write_remove_suid(struct inode *inode)
1392 {
1393         int ret;
1394         struct buffer_head *bh = NULL;
1395
1396         ret = ocfs2_read_inode_block(inode, &bh);
1397         if (ret < 0) {
1398                 mlog_errno(ret);
1399                 goto out;
1400         }
1401
1402         ret =  __ocfs2_write_remove_suid(inode, bh);
1403 out:
1404         brelse(bh);
1405         return ret;
1406 }
1407
1408 /*
1409  * Allocate enough extents to cover the region starting at byte offset
1410  * start for len bytes. Existing extents are skipped, any extents
1411  * added are marked as "unwritten".
1412  */
1413 static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1414                                             u64 start, u64 len)
1415 {
1416         int ret;
1417         u32 cpos, phys_cpos, clusters, alloc_size;
1418         u64 end = start + len;
1419         struct buffer_head *di_bh = NULL;
1420
1421         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1422                 ret = ocfs2_read_inode_block(inode, &di_bh);
1423                 if (ret) {
1424                         mlog_errno(ret);
1425                         goto out;
1426                 }
1427
1428                 /*
1429                  * Nothing to do if the requested reservation range
1430                  * fits within the inode.
1431                  */
1432                 if (ocfs2_size_fits_inline_data(di_bh, end))
1433                         goto out;
1434
1435                 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1436                 if (ret) {
1437                         mlog_errno(ret);
1438                         goto out;
1439                 }
1440         }
1441
1442         /*
1443          * We consider both start and len to be inclusive.
1444          */
1445         cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1446         clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1447         clusters -= cpos;
1448
1449         while (clusters) {
1450                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1451                                          &alloc_size, NULL);
1452                 if (ret) {
1453                         mlog_errno(ret);
1454                         goto out;
1455                 }
1456
1457                 /*
1458                  * Hole or existing extent len can be arbitrary, so
1459                  * cap it to our own allocation request.
1460                  */
1461                 if (alloc_size > clusters)
1462                         alloc_size = clusters;
1463
1464                 if (phys_cpos) {
1465                         /*
1466                          * We already have an allocation at this
1467                          * region so we can safely skip it.
1468                          */
1469                         goto next;
1470                 }
1471
1472                 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1473                 if (ret) {
1474                         if (ret != -ENOSPC)
1475                                 mlog_errno(ret);
1476                         goto out;
1477                 }
1478
1479 next:
1480                 cpos += alloc_size;
1481                 clusters -= alloc_size;
1482         }
1483
1484         ret = 0;
1485 out:
1486
1487         brelse(di_bh);
1488         return ret;
1489 }
1490
1491 /*
1492  * Truncate a byte range, avoiding pages within partial clusters. This
1493  * preserves those pages for the zeroing code to write to.
1494  */
1495 static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1496                                          u64 byte_len)
1497 {
1498         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1499         loff_t start, end;
1500         struct address_space *mapping = inode->i_mapping;
1501
1502         start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1503         end = byte_start + byte_len;
1504         end = end & ~(osb->s_clustersize - 1);
1505
1506         if (start < end) {
1507                 unmap_mapping_range(mapping, start, end - start, 0);
1508                 truncate_inode_pages_range(mapping, start, end - 1);
1509         }
1510 }
1511
1512 static int ocfs2_zero_partial_clusters(struct inode *inode,
1513                                        u64 start, u64 len)
1514 {
1515         int ret = 0;
1516         u64 tmpend = 0;
1517         u64 end = start + len;
1518         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1519         unsigned int csize = osb->s_clustersize;
1520         handle_t *handle;
1521
1522         /*
1523          * The "start" and "end" values are NOT necessarily part of
1524          * the range whose allocation is being deleted. Rather, this
1525          * is what the user passed in with the request. We must zero
1526          * partial clusters here. There's no need to worry about
1527          * physical allocation - the zeroing code knows to skip holes.
1528          */
1529         trace_ocfs2_zero_partial_clusters(
1530                 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1531                 (unsigned long long)start, (unsigned long long)end);
1532
1533         /*
1534          * If both edges are on a cluster boundary then there's no
1535          * zeroing required as the region is part of the allocation to
1536          * be truncated.
1537          */
1538         if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1539                 goto out;
1540
1541         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1542         if (IS_ERR(handle)) {
1543                 ret = PTR_ERR(handle);
1544                 mlog_errno(ret);
1545                 goto out;
1546         }
1547
1548         /*
1549          * If start is on a cluster boundary and end is somewhere in another
1550          * cluster, we have not COWed the cluster starting at start, unless
1551          * end is also within the same cluster. So, in this case, we skip this
1552          * first call to ocfs2_zero_range_for_truncate() truncate and move on
1553          * to the next one.
1554          */
1555         if ((start & (csize - 1)) != 0) {
1556                 /*
1557                  * We want to get the byte offset of the end of the 1st
1558                  * cluster.
1559                  */
1560                 tmpend = (u64)osb->s_clustersize +
1561                         (start & ~(osb->s_clustersize - 1));
1562                 if (tmpend > end)
1563                         tmpend = end;
1564
1565                 trace_ocfs2_zero_partial_clusters_range1(
1566                         (unsigned long long)start,
1567                         (unsigned long long)tmpend);
1568
1569                 ret = ocfs2_zero_range_for_truncate(inode, handle, start,
1570                                                     tmpend);
1571                 if (ret)
1572                         mlog_errno(ret);
1573         }
1574
1575         if (tmpend < end) {
1576                 /*
1577                  * This may make start and end equal, but the zeroing
1578                  * code will skip any work in that case so there's no
1579                  * need to catch it up here.
1580                  */
1581                 start = end & ~(osb->s_clustersize - 1);
1582
1583                 trace_ocfs2_zero_partial_clusters_range2(
1584                         (unsigned long long)start, (unsigned long long)end);
1585
1586                 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1587                 if (ret)
1588                         mlog_errno(ret);
1589         }
1590
1591         ocfs2_commit_trans(osb, handle);
1592 out:
1593         return ret;
1594 }
1595
1596 static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
1597 {
1598         int i;
1599         struct ocfs2_extent_rec *rec = NULL;
1600
1601         for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1602
1603                 rec = &el->l_recs[i];
1604
1605                 if (le32_to_cpu(rec->e_cpos) < pos)
1606                         break;
1607         }
1608
1609         return i;
1610 }
1611
1612 /*
1613  * Helper to calculate the punching pos and length in one run, we handle the
1614  * following three cases in order:
1615  *
1616  * - remove the entire record
1617  * - remove a partial record
1618  * - no record needs to be removed (hole-punching completed)
1619 */
1620 static void ocfs2_calc_trunc_pos(struct inode *inode,
1621                                  struct ocfs2_extent_list *el,
1622                                  struct ocfs2_extent_rec *rec,
1623                                  u32 trunc_start, u32 *trunc_cpos,
1624                                  u32 *trunc_len, u32 *trunc_end,
1625                                  u64 *blkno, int *done)
1626 {
1627         int ret = 0;
1628         u32 coff, range;
1629
1630         range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1631
1632         if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
1633                 /*
1634                  * remove an entire extent record.
1635                  */
1636                 *trunc_cpos = le32_to_cpu(rec->e_cpos);
1637                 /*
1638                  * Skip holes if any.
1639                  */
1640                 if (range < *trunc_end)
1641                         *trunc_end = range;
1642                 *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
1643                 *blkno = le64_to_cpu(rec->e_blkno);
1644                 *trunc_end = le32_to_cpu(rec->e_cpos);
1645         } else if (range > trunc_start) {
1646                 /*
1647                  * remove a partial extent record, which means we're
1648                  * removing the last extent record.
1649                  */
1650                 *trunc_cpos = trunc_start;
1651                 /*
1652                  * skip hole if any.
1653                  */
1654                 if (range < *trunc_end)
1655                         *trunc_end = range;
1656                 *trunc_len = *trunc_end - trunc_start;
1657                 coff = trunc_start - le32_to_cpu(rec->e_cpos);
1658                 *blkno = le64_to_cpu(rec->e_blkno) +
1659                                 ocfs2_clusters_to_blocks(inode->i_sb, coff);
1660                 *trunc_end = trunc_start;
1661         } else {
1662                 /*
1663                  * It may have two following possibilities:
1664                  *
1665                  * - last record has been removed
1666                  * - trunc_start was within a hole
1667                  *
1668                  * both two cases mean the completion of hole punching.
1669                  */
1670                 ret = 1;
1671         }
1672
1673         *done = ret;
1674 }
1675
1676 static int ocfs2_remove_inode_range(struct inode *inode,
1677                                     struct buffer_head *di_bh, u64 byte_start,
1678                                     u64 byte_len)
1679 {
1680         int ret = 0, flags = 0, done = 0, i;
1681         u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
1682         u32 cluster_in_el;
1683         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1684         struct ocfs2_cached_dealloc_ctxt dealloc;
1685         struct address_space *mapping = inode->i_mapping;
1686         struct ocfs2_extent_tree et;
1687         struct ocfs2_path *path = NULL;
1688         struct ocfs2_extent_list *el = NULL;
1689         struct ocfs2_extent_rec *rec = NULL;
1690         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1691         u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
1692
1693         ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
1694         ocfs2_init_dealloc_ctxt(&dealloc);
1695
1696         trace_ocfs2_remove_inode_range(
1697                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
1698                         (unsigned long long)byte_start,
1699                         (unsigned long long)byte_len);
1700
1701         if (byte_len == 0)
1702                 return 0;
1703
1704         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1705                 ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
1706                                             byte_start + byte_len, 0);
1707                 if (ret) {
1708                         mlog_errno(ret);
1709                         goto out;
1710                 }
1711                 /*
1712                  * There's no need to get fancy with the page cache
1713                  * truncate of an inline-data inode. We're talking
1714                  * about less than a page here, which will be cached
1715                  * in the dinode buffer anyway.
1716                  */
1717                 unmap_mapping_range(mapping, 0, 0, 0);
1718                 truncate_inode_pages(mapping, 0);
1719                 goto out;
1720         }
1721
1722         /*
1723          * For reflinks, we may need to CoW 2 clusters which might be
1724          * partially zero'd later, if hole's start and end offset were
1725          * within one cluster(means is not exactly aligned to clustersize).
1726          */
1727
1728         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
1729
1730                 ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
1731                 if (ret) {
1732                         mlog_errno(ret);
1733                         goto out;
1734                 }
1735
1736                 ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
1737                 if (ret) {
1738                         mlog_errno(ret);
1739                         goto out;
1740                 }
1741         }
1742
1743         trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1744         trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
1745         cluster_in_el = trunc_end;
1746
1747         ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1748         if (ret) {
1749                 mlog_errno(ret);
1750                 goto out;
1751         }
1752
1753         path = ocfs2_new_path_from_et(&et);
1754         if (!path) {
1755                 ret = -ENOMEM;
1756                 mlog_errno(ret);
1757                 goto out;
1758         }
1759
1760         while (trunc_end > trunc_start) {
1761
1762                 ret = ocfs2_find_path(INODE_CACHE(inode), path,
1763                                       cluster_in_el);
1764                 if (ret) {
1765                         mlog_errno(ret);
1766                         goto out;
1767                 }
1768
1769                 el = path_leaf_el(path);
1770
1771                 i = ocfs2_find_rec(el, trunc_end);
1772                 /*
1773                  * Need to go to previous extent block.
1774                  */
1775                 if (i < 0) {
1776                         if (path->p_tree_depth == 0)
1777                                 break;
1778
1779                         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
1780                                                             path,
1781                                                             &cluster_in_el);
1782                         if (ret) {
1783                                 mlog_errno(ret);
1784                                 goto out;
1785                         }
1786
1787                         /*
1788                          * We've reached the leftmost extent block,
1789                          * it's safe to leave.
1790                          */
1791                         if (cluster_in_el == 0)
1792                                 break;
1793
1794                         /*
1795                          * The 'pos' searched for previous extent block is
1796                          * always one cluster less than actual trunc_end.
1797                          */
1798                         trunc_end = cluster_in_el + 1;
1799
1800                         ocfs2_reinit_path(path, 1);
1801
1802                         continue;
1803
1804                 } else
1805                         rec = &el->l_recs[i];
1806
1807                 ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
1808                                      &trunc_len, &trunc_end, &blkno, &done);
1809                 if (done)
1810                         break;
1811
1812                 flags = rec->e_flags;
1813                 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
1814
1815                 ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
1816                                                phys_cpos, trunc_len, flags,
1817                                                &dealloc, refcount_loc);
1818                 if (ret < 0) {
1819                         mlog_errno(ret);
1820                         goto out;
1821                 }
1822
1823                 cluster_in_el = trunc_end;
1824
1825                 ocfs2_reinit_path(path, 1);
1826         }
1827
1828         ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1829
1830 out:
1831         ocfs2_schedule_truncate_log_flush(osb, 1);
1832         ocfs2_run_deallocs(osb, &dealloc);
1833
1834         return ret;
1835 }
1836
1837 /*
1838  * Parts of this function taken from xfs_change_file_space()
1839  */
1840 static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1841                                      loff_t f_pos, unsigned int cmd,
1842                                      struct ocfs2_space_resv *sr,
1843                                      int change_size)
1844 {
1845         int ret;
1846         s64 llen;
1847         loff_t size;
1848         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1849         struct buffer_head *di_bh = NULL;
1850         handle_t *handle;
1851         unsigned long long max_off = inode->i_sb->s_maxbytes;
1852
1853         if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1854                 return -EROFS;
1855
1856         mutex_lock(&inode->i_mutex);
1857
1858         /*
1859          * This prevents concurrent writes on other nodes
1860          */
1861         ret = ocfs2_rw_lock(inode, 1);
1862         if (ret) {
1863                 mlog_errno(ret);
1864                 goto out;
1865         }
1866
1867         ret = ocfs2_inode_lock(inode, &di_bh, 1);
1868         if (ret) {
1869                 mlog_errno(ret);
1870                 goto out_rw_unlock;
1871         }
1872
1873         if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1874                 ret = -EPERM;
1875                 goto out_inode_unlock;
1876         }
1877
1878         switch (sr->l_whence) {
1879         case 0: /*SEEK_SET*/
1880                 break;
1881         case 1: /*SEEK_CUR*/
1882                 sr->l_start += f_pos;
1883                 break;
1884         case 2: /*SEEK_END*/
1885                 sr->l_start += i_size_read(inode);
1886                 break;
1887         default:
1888                 ret = -EINVAL;
1889                 goto out_inode_unlock;
1890         }
1891         sr->l_whence = 0;
1892
1893         llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1894
1895         if (sr->l_start < 0
1896             || sr->l_start > max_off
1897             || (sr->l_start + llen) < 0
1898             || (sr->l_start + llen) > max_off) {
1899                 ret = -EINVAL;
1900                 goto out_inode_unlock;
1901         }
1902         size = sr->l_start + sr->l_len;
1903
1904         if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1905                 if (sr->l_len <= 0) {
1906                         ret = -EINVAL;
1907                         goto out_inode_unlock;
1908                 }
1909         }
1910
1911         if (file && should_remove_suid(file->f_path.dentry)) {
1912                 ret = __ocfs2_write_remove_suid(inode, di_bh);
1913                 if (ret) {
1914                         mlog_errno(ret);
1915                         goto out_inode_unlock;
1916                 }
1917         }
1918
1919         down_write(&OCFS2_I(inode)->ip_alloc_sem);
1920         switch (cmd) {
1921         case OCFS2_IOC_RESVSP:
1922         case OCFS2_IOC_RESVSP64:
1923                 /*
1924                  * This takes unsigned offsets, but the signed ones we
1925                  * pass have been checked against overflow above.
1926                  */
1927                 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1928                                                        sr->l_len);
1929                 break;
1930         case OCFS2_IOC_UNRESVSP:
1931         case OCFS2_IOC_UNRESVSP64:
1932                 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1933                                                sr->l_len);
1934                 break;
1935         default:
1936                 ret = -EINVAL;
1937         }
1938         up_write(&OCFS2_I(inode)->ip_alloc_sem);
1939         if (ret) {
1940                 mlog_errno(ret);
1941                 goto out_inode_unlock;
1942         }
1943
1944         /*
1945          * We update c/mtime for these changes
1946          */
1947         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1948         if (IS_ERR(handle)) {
1949                 ret = PTR_ERR(handle);
1950                 mlog_errno(ret);
1951                 goto out_inode_unlock;
1952         }
1953
1954         if (change_size && i_size_read(inode) < size)
1955                 i_size_write(inode, size);
1956
1957         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1958         ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1959         if (ret < 0)
1960                 mlog_errno(ret);
1961
1962         if (file && (file->f_flags & O_SYNC))
1963                 handle->h_sync = 1;
1964
1965         ocfs2_commit_trans(osb, handle);
1966
1967 out_inode_unlock:
1968         brelse(di_bh);
1969         ocfs2_inode_unlock(inode, 1);
1970 out_rw_unlock:
1971         ocfs2_rw_unlock(inode, 1);
1972
1973 out:
1974         mutex_unlock(&inode->i_mutex);
1975         return ret;
1976 }
1977
1978 int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1979                             struct ocfs2_space_resv *sr)
1980 {
1981         struct inode *inode = file->f_path.dentry->d_inode;
1982         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1983
1984         if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1985             !ocfs2_writes_unwritten_extents(osb))
1986                 return -ENOTTY;
1987         else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1988                  !ocfs2_sparse_alloc(osb))
1989                 return -ENOTTY;
1990
1991         if (!S_ISREG(inode->i_mode))
1992                 return -EINVAL;
1993
1994         if (!(file->f_mode & FMODE_WRITE))
1995                 return -EBADF;
1996
1997         return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
1998 }
1999
2000 static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
2001                             loff_t len)
2002 {
2003         struct inode *inode = file->f_path.dentry->d_inode;
2004         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2005         struct ocfs2_space_resv sr;
2006         int change_size = 1;
2007         int cmd = OCFS2_IOC_RESVSP64;
2008
2009         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2010                 return -EOPNOTSUPP;
2011         if (!ocfs2_writes_unwritten_extents(osb))
2012                 return -EOPNOTSUPP;
2013
2014         if (mode & FALLOC_FL_KEEP_SIZE)
2015                 change_size = 0;
2016
2017         if (mode & FALLOC_FL_PUNCH_HOLE)
2018                 cmd = OCFS2_IOC_UNRESVSP64;
2019
2020         sr.l_whence = 0;
2021         sr.l_start = (s64)offset;
2022         sr.l_len = (s64)len;
2023
2024         return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
2025                                          change_size);
2026 }
2027
2028 int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
2029                                    size_t count)
2030 {
2031         int ret = 0;
2032         unsigned int extent_flags;
2033         u32 cpos, clusters, extent_len, phys_cpos;
2034         struct super_block *sb = inode->i_sb;
2035
2036         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
2037             !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
2038             OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2039                 return 0;
2040
2041         cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
2042         clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
2043
2044         while (clusters) {
2045                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
2046                                          &extent_flags);
2047                 if (ret < 0) {
2048                         mlog_errno(ret);
2049                         goto out;
2050                 }
2051
2052                 if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
2053                         ret = 1;
2054                         break;
2055                 }
2056
2057                 if (extent_len > clusters)
2058                         extent_len = clusters;
2059
2060                 clusters -= extent_len;
2061                 cpos += extent_len;
2062         }
2063 out:
2064         return ret;
2065 }
2066
2067 static void ocfs2_aiodio_wait(struct inode *inode)
2068 {
2069         wait_queue_head_t *wq = ocfs2_ioend_wq(inode);
2070
2071         wait_event(*wq, (atomic_read(&OCFS2_I(inode)->ip_unaligned_aio) == 0));
2072 }
2073
2074 static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
2075 {
2076         int blockmask = inode->i_sb->s_blocksize - 1;
2077         loff_t final_size = pos + count;
2078
2079         if ((pos & blockmask) || (final_size & blockmask))
2080                 return 1;
2081         return 0;
2082 }
2083
2084 static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
2085                                             struct file *file,
2086                                             loff_t pos, size_t count,
2087                                             int *meta_level)
2088 {
2089         int ret;
2090         struct buffer_head *di_bh = NULL;
2091         u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
2092         u32 clusters =
2093                 ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
2094
2095         ret = ocfs2_inode_lock(inode, &di_bh, 1);
2096         if (ret) {
2097                 mlog_errno(ret);
2098                 goto out;
2099         }
2100
2101         *meta_level = 1;
2102
2103         ret = ocfs2_refcount_cow(inode, file, di_bh, cpos, clusters, UINT_MAX);
2104         if (ret)
2105                 mlog_errno(ret);
2106 out:
2107         brelse(di_bh);
2108         return ret;
2109 }
2110
2111 static int ocfs2_prepare_inode_for_write(struct file *file,
2112                                          loff_t *ppos,
2113                                          size_t count,
2114                                          int appending,
2115                                          int *direct_io,
2116                                          int *has_refcount)
2117 {
2118         int ret = 0, meta_level = 0;
2119         struct dentry *dentry = file->f_path.dentry;
2120         struct inode *inode = dentry->d_inode;
2121         loff_t saved_pos = 0, end;
2122
2123         /*
2124          * We start with a read level meta lock and only jump to an ex
2125          * if we need to make modifications here.
2126          */
2127         for(;;) {
2128                 ret = ocfs2_inode_lock(inode, NULL, meta_level);
2129                 if (ret < 0) {
2130                         meta_level = -1;
2131                         mlog_errno(ret);
2132                         goto out;
2133                 }
2134
2135                 /* Clear suid / sgid if necessary. We do this here
2136                  * instead of later in the write path because
2137                  * remove_suid() calls ->setattr without any hint that
2138                  * we may have already done our cluster locking. Since
2139                  * ocfs2_setattr() *must* take cluster locks to
2140                  * proceeed, this will lead us to recursively lock the
2141                  * inode. There's also the dinode i_size state which
2142                  * can be lost via setattr during extending writes (we
2143                  * set inode->i_size at the end of a write. */
2144                 if (should_remove_suid(dentry)) {
2145                         if (meta_level == 0) {
2146                                 ocfs2_inode_unlock(inode, meta_level);
2147                                 meta_level = 1;
2148                                 continue;
2149                         }
2150
2151                         ret = ocfs2_write_remove_suid(inode);
2152                         if (ret < 0) {
2153                                 mlog_errno(ret);
2154                                 goto out_unlock;
2155                         }
2156                 }
2157
2158                 /* work on a copy of ppos until we're sure that we won't have
2159                  * to recalculate it due to relocking. */
2160                 if (appending)
2161                         saved_pos = i_size_read(inode);
2162                 else
2163                         saved_pos = *ppos;
2164
2165                 end = saved_pos + count;
2166
2167                 ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
2168                 if (ret == 1) {
2169                         ocfs2_inode_unlock(inode, meta_level);
2170                         meta_level = -1;
2171
2172                         ret = ocfs2_prepare_inode_for_refcount(inode,
2173                                                                file,
2174                                                                saved_pos,
2175                                                                count,
2176                                                                &meta_level);
2177                         if (has_refcount)
2178                                 *has_refcount = 1;
2179                         if (direct_io)
2180                                 *direct_io = 0;
2181                 }
2182
2183                 if (ret < 0) {
2184                         mlog_errno(ret);
2185                         goto out_unlock;
2186                 }
2187
2188                 /*
2189                  * Skip the O_DIRECT checks if we don't need
2190                  * them.
2191                  */
2192                 if (!direct_io || !(*direct_io))
2193                         break;
2194
2195                 /*
2196                  * There's no sane way to do direct writes to an inode
2197                  * with inline data.
2198                  */
2199                 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2200                         *direct_io = 0;
2201                         break;
2202                 }
2203
2204                 /*
2205                  * Allowing concurrent direct writes means
2206                  * i_size changes wouldn't be synchronized, so
2207                  * one node could wind up truncating another
2208                  * nodes writes.
2209                  */
2210                 if (end > i_size_read(inode)) {
2211                         *direct_io = 0;
2212                         break;
2213                 }
2214
2215                 /*
2216                  * We don't fill holes during direct io, so
2217                  * check for them here. If any are found, the
2218                  * caller will have to retake some cluster
2219                  * locks and initiate the io as buffered.
2220                  */
2221                 ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
2222                 if (ret == 1) {
2223                         *direct_io = 0;
2224                         ret = 0;
2225                 } else if (ret < 0)
2226                         mlog_errno(ret);
2227                 break;
2228         }
2229
2230         if (appending)
2231                 *ppos = saved_pos;
2232
2233 out_unlock:
2234         trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
2235                                             saved_pos, appending, count,
2236                                             direct_io, has_refcount);
2237
2238         if (meta_level >= 0)
2239                 ocfs2_inode_unlock(inode, meta_level);
2240
2241 out:
2242         return ret;
2243 }
2244
2245 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
2246                                     const struct iovec *iov,
2247                                     unsigned long nr_segs,
2248                                     loff_t pos)
2249 {
2250         int ret, direct_io, appending, rw_level, have_alloc_sem  = 0;
2251         int can_do_direct, has_refcount = 0;
2252         ssize_t written = 0;
2253         size_t ocount;          /* original count */
2254         size_t count;           /* after file limit checks */
2255         loff_t old_size, *ppos = &iocb->ki_pos;
2256         u32 old_clusters;
2257         struct file *file = iocb->ki_filp;
2258         struct inode *inode = file->f_path.dentry->d_inode;
2259         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2260         int full_coherency = !(osb->s_mount_opt &
2261                                OCFS2_MOUNT_COHERENCY_BUFFERED);
2262         int unaligned_dio = 0;
2263
2264         trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
2265                 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2266                 file->f_path.dentry->d_name.len,
2267                 file->f_path.dentry->d_name.name,
2268                 (unsigned int)nr_segs);
2269
2270         if (iocb->ki_left == 0)
2271                 return 0;
2272
2273         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2274
2275         appending = file->f_flags & O_APPEND ? 1 : 0;
2276         direct_io = file->f_flags & O_DIRECT ? 1 : 0;
2277
2278         mutex_lock(&inode->i_mutex);
2279
2280         ocfs2_iocb_clear_sem_locked(iocb);
2281
2282 relock:
2283         /* to match setattr's i_mutex -> rw_lock ordering */
2284         if (direct_io) {
2285                 have_alloc_sem = 1;
2286                 /* communicate with ocfs2_dio_end_io */
2287                 ocfs2_iocb_set_sem_locked(iocb);
2288         }
2289
2290         /*
2291          * Concurrent O_DIRECT writes are allowed with
2292          * mount_option "coherency=buffered".
2293          */
2294         rw_level = (!direct_io || full_coherency);
2295
2296         ret = ocfs2_rw_lock(inode, rw_level);
2297         if (ret < 0) {
2298                 mlog_errno(ret);
2299                 goto out_sems;
2300         }
2301
2302         /*
2303          * O_DIRECT writes with "coherency=full" need to take EX cluster
2304          * inode_lock to guarantee coherency.
2305          */
2306         if (direct_io && full_coherency) {
2307                 /*
2308                  * We need to take and drop the inode lock to force
2309                  * other nodes to drop their caches.  Buffered I/O
2310                  * already does this in write_begin().
2311                  */
2312                 ret = ocfs2_inode_lock(inode, NULL, 1);
2313                 if (ret < 0) {
2314                         mlog_errno(ret);
2315                         goto out_sems;
2316                 }
2317
2318                 ocfs2_inode_unlock(inode, 1);
2319         }
2320
2321         can_do_direct = direct_io;
2322         ret = ocfs2_prepare_inode_for_write(file, ppos,
2323                                             iocb->ki_left, appending,
2324                                             &can_do_direct, &has_refcount);
2325         if (ret < 0) {
2326                 mlog_errno(ret);
2327                 goto out;
2328         }
2329
2330         if (direct_io && !is_sync_kiocb(iocb))
2331                 unaligned_dio = ocfs2_is_io_unaligned(inode, iocb->ki_left,
2332                                                       *ppos);
2333
2334         /*
2335          * We can't complete the direct I/O as requested, fall back to
2336          * buffered I/O.
2337          */
2338         if (direct_io && !can_do_direct) {
2339                 ocfs2_rw_unlock(inode, rw_level);
2340
2341                 have_alloc_sem = 0;
2342                 rw_level = -1;
2343
2344                 direct_io = 0;
2345                 goto relock;
2346         }
2347
2348         if (unaligned_dio) {
2349                 /*
2350                  * Wait on previous unaligned aio to complete before
2351                  * proceeding.
2352                  */
2353                 ocfs2_aiodio_wait(inode);
2354
2355                 /* Mark the iocb as needing a decrement in ocfs2_dio_end_io */
2356                 atomic_inc(&OCFS2_I(inode)->ip_unaligned_aio);
2357                 ocfs2_iocb_set_unaligned_aio(iocb);
2358         }
2359
2360         /*
2361          * To later detect whether a journal commit for sync writes is
2362          * necessary, we sample i_size, and cluster count here.
2363          */
2364         old_size = i_size_read(inode);
2365         old_clusters = OCFS2_I(inode)->ip_clusters;
2366
2367         /* communicate with ocfs2_dio_end_io */
2368         ocfs2_iocb_set_rw_locked(iocb, rw_level);
2369
2370         ret = generic_segment_checks(iov, &nr_segs, &ocount,
2371                                      VERIFY_READ);
2372         if (ret)
2373                 goto out_dio;
2374
2375         count = ocount;
2376         ret = generic_write_checks(file, ppos, &count,
2377                                    S_ISBLK(inode->i_mode));
2378         if (ret)
2379                 goto out_dio;
2380
2381         if (direct_io) {
2382                 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
2383                                                     ppos, count, ocount);
2384                 if (written < 0) {
2385                         ret = written;
2386                         goto out_dio;
2387                 }
2388         } else {
2389                 current->backing_dev_info = file->f_mapping->backing_dev_info;
2390                 written = generic_file_buffered_write(iocb, iov, nr_segs, *ppos,
2391                                                       ppos, count, 0);
2392                 current->backing_dev_info = NULL;
2393         }
2394
2395 out_dio:
2396         /* buffered aio wouldn't have proper lock coverage today */
2397         BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
2398
2399         if (unlikely(written <= 0))
2400                 goto no_sync;
2401
2402         if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
2403             ((file->f_flags & O_DIRECT) && !direct_io)) {
2404                 ret = filemap_fdatawrite_range(file->f_mapping,
2405                                                iocb->ki_pos - written,
2406                                                iocb->ki_pos - 1);
2407                 if (ret < 0)
2408                         written = ret;
2409
2410                 if (!ret && ((old_size != i_size_read(inode)) ||
2411                              (old_clusters != OCFS2_I(inode)->ip_clusters) ||
2412                              has_refcount)) {
2413                         ret = jbd2_journal_force_commit(osb->journal->j_journal);
2414                         if (ret < 0)
2415                                 written = ret;
2416                 }
2417
2418                 if (!ret)
2419                         ret = filemap_fdatawait_range(file->f_mapping,
2420                                                       iocb->ki_pos - written,
2421                                                       iocb->ki_pos - 1);
2422         }
2423
2424 no_sync:
2425         /*
2426          * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2427          * function pointer which is called when o_direct io completes so that
2428          * it can unlock our rw lock.
2429          * Unfortunately there are error cases which call end_io and others
2430          * that don't.  so we don't have to unlock the rw_lock if either an
2431          * async dio is going to do it in the future or an end_io after an
2432          * error has already done it.
2433          */
2434         if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
2435                 rw_level = -1;
2436                 have_alloc_sem = 0;
2437                 unaligned_dio = 0;
2438         }
2439
2440         if (unaligned_dio) {
2441                 ocfs2_iocb_clear_unaligned_aio(iocb);
2442                 atomic_dec(&OCFS2_I(inode)->ip_unaligned_aio);
2443         }
2444
2445 out:
2446         if (rw_level != -1)
2447                 ocfs2_rw_unlock(inode, rw_level);
2448
2449 out_sems:
2450         if (have_alloc_sem)
2451                 ocfs2_iocb_clear_sem_locked(iocb);
2452
2453         mutex_unlock(&inode->i_mutex);
2454
2455         if (written)
2456                 ret = written;
2457         return ret;
2458 }
2459
2460 static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
2461                                 struct file *out,
2462                                 struct splice_desc *sd)
2463 {
2464         int ret;
2465
2466         ret = ocfs2_prepare_inode_for_write(out, &sd->pos,
2467                                             sd->total_len, 0, NULL, NULL);
2468         if (ret < 0) {
2469                 mlog_errno(ret);
2470                 return ret;
2471         }
2472
2473         return splice_from_pipe_feed(pipe, sd, pipe_to_file);
2474 }
2475
2476 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2477                                        struct file *out,
2478                                        loff_t *ppos,
2479                                        size_t len,
2480                                        unsigned int flags)
2481 {
2482         int ret;
2483         struct address_space *mapping = out->f_mapping;
2484         struct inode *inode = mapping->host;
2485         struct splice_desc sd = {
2486                 .flags = flags,
2487                 .u.file = out,
2488         };
2489
2490
2491         trace_ocfs2_file_splice_write(inode, out, out->f_path.dentry,
2492                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
2493                         out->f_path.dentry->d_name.len,
2494                         out->f_path.dentry->d_name.name, len);
2495
2496         ret = generic_write_checks(out, ppos, &len, 0);
2497         if (ret)
2498                 return ret;
2499         sd.total_len = len;
2500         sd.pos = *ppos;
2501
2502         if (pipe->inode)
2503                 mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_PARENT);
2504
2505         splice_from_pipe_begin(&sd);
2506         do {
2507                 ret = splice_from_pipe_next(pipe, &sd);
2508                 if (ret <= 0)
2509                         break;
2510
2511                 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2512                 ret = ocfs2_rw_lock(inode, 1);
2513                 if (ret < 0)
2514                         mlog_errno(ret);
2515                 else {
2516                         ret = ocfs2_splice_to_file(pipe, out, &sd);
2517                         ocfs2_rw_unlock(inode, 1);
2518                 }
2519                 mutex_unlock(&inode->i_mutex);
2520         } while (ret > 0);
2521         splice_from_pipe_end(pipe, &sd);
2522
2523         if (pipe->inode)
2524                 mutex_unlock(&pipe->inode->i_mutex);
2525
2526         if (sd.num_spliced)
2527                 ret = sd.num_spliced;
2528
2529         if (ret > 0) {
2530                 unsigned long nr_pages;
2531                 int err;
2532
2533                 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2534
2535                 err = generic_write_sync(out, *ppos, ret);
2536                 if (err)
2537                         ret = err;
2538                 else
2539                         *ppos += ret;
2540
2541                 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
2542         }
2543
2544         return ret;
2545 }
2546
2547 static ssize_t ocfs2_file_splice_read(struct file *in,
2548                                       loff_t *ppos,
2549                                       struct pipe_inode_info *pipe,
2550                                       size_t len,
2551                                       unsigned int flags)
2552 {
2553         int ret = 0, lock_level = 0;
2554         struct inode *inode = in->f_path.dentry->d_inode;
2555
2556         trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
2557                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
2558                         in->f_path.dentry->d_name.len,
2559                         in->f_path.dentry->d_name.name, len);
2560
2561         /*
2562          * See the comment in ocfs2_file_aio_read()
2563          */
2564         ret = ocfs2_inode_lock_atime(inode, in->f_vfsmnt, &lock_level);
2565         if (ret < 0) {
2566                 mlog_errno(ret);
2567                 goto bail;
2568         }
2569         ocfs2_inode_unlock(inode, lock_level);
2570
2571         ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2572
2573 bail:
2574         return ret;
2575 }
2576
2577 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
2578                                    const struct iovec *iov,
2579                                    unsigned long nr_segs,
2580                                    loff_t pos)
2581 {
2582         int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
2583         struct file *filp = iocb->ki_filp;
2584         struct inode *inode = filp->f_path.dentry->d_inode;
2585
2586         trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
2587                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
2588                         filp->f_path.dentry->d_name.len,
2589                         filp->f_path.dentry->d_name.name, nr_segs);
2590
2591
2592         if (!inode) {
2593                 ret = -EINVAL;
2594                 mlog_errno(ret);
2595                 goto bail;
2596         }
2597
2598         ocfs2_iocb_clear_sem_locked(iocb);
2599
2600         /*
2601          * buffered reads protect themselves in ->readpage().  O_DIRECT reads
2602          * need locks to protect pending reads from racing with truncate.
2603          */
2604         if (filp->f_flags & O_DIRECT) {
2605                 have_alloc_sem = 1;
2606                 ocfs2_iocb_set_sem_locked(iocb);
2607
2608                 ret = ocfs2_rw_lock(inode, 0);
2609                 if (ret < 0) {
2610                         mlog_errno(ret);
2611                         goto bail;
2612                 }
2613                 rw_level = 0;
2614                 /* communicate with ocfs2_dio_end_io */
2615                 ocfs2_iocb_set_rw_locked(iocb, rw_level);
2616         }
2617
2618         /*
2619          * We're fine letting folks race truncates and extending
2620          * writes with read across the cluster, just like they can
2621          * locally. Hence no rw_lock during read.
2622          *
2623          * Take and drop the meta data lock to update inode fields
2624          * like i_size. This allows the checks down below
2625          * generic_file_aio_read() a chance of actually working.
2626          */
2627         ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
2628         if (ret < 0) {
2629                 mlog_errno(ret);
2630                 goto bail;
2631         }
2632         ocfs2_inode_unlock(inode, lock_level);
2633
2634         ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
2635         trace_generic_file_aio_read_ret(ret);
2636
2637         /* buffered aio wouldn't have proper lock coverage today */
2638         BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
2639
2640         /* see ocfs2_file_aio_write */
2641         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2642                 rw_level = -1;
2643                 have_alloc_sem = 0;
2644         }
2645
2646 bail:
2647         if (have_alloc_sem)
2648                 ocfs2_iocb_clear_sem_locked(iocb);
2649
2650         if (rw_level != -1)
2651                 ocfs2_rw_unlock(inode, rw_level);
2652
2653         return ret;
2654 }
2655
2656 /* Refer generic_file_llseek_unlocked() */
2657 static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int origin)
2658 {
2659         struct inode *inode = file->f_mapping->host;
2660         int ret = 0;
2661
2662         mutex_lock(&inode->i_mutex);
2663
2664         switch (origin) {
2665         case SEEK_SET:
2666                 break;
2667         case SEEK_END:
2668                 offset += inode->i_size;
2669                 break;
2670         case SEEK_CUR:
2671                 if (offset == 0) {
2672                         offset = file->f_pos;
2673                         goto out;
2674                 }
2675                 offset += file->f_pos;
2676                 break;
2677         case SEEK_DATA:
2678         case SEEK_HOLE:
2679                 ret = ocfs2_seek_data_hole_offset(file, &offset, origin);
2680                 if (ret)
2681                         goto out;
2682                 break;
2683         default:
2684                 ret = -EINVAL;
2685                 goto out;
2686         }
2687
2688         if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
2689                 ret = -EINVAL;
2690         if (!ret && offset > inode->i_sb->s_maxbytes)
2691                 ret = -EINVAL;
2692         if (ret)
2693                 goto out;
2694
2695         if (offset != file->f_pos) {
2696                 file->f_pos = offset;
2697                 file->f_version = 0;
2698         }
2699
2700 out:
2701         mutex_unlock(&inode->i_mutex);
2702         if (ret)
2703                 return ret;
2704         return offset;
2705 }
2706
2707 const struct inode_operations ocfs2_file_iops = {
2708         .setattr        = ocfs2_setattr,
2709         .getattr        = ocfs2_getattr,
2710         .permission     = ocfs2_permission,
2711         .setxattr       = generic_setxattr,
2712         .getxattr       = generic_getxattr,
2713         .listxattr      = ocfs2_listxattr,
2714         .removexattr    = generic_removexattr,
2715         .fiemap         = ocfs2_fiemap,
2716         .get_acl        = ocfs2_iop_get_acl,
2717 };
2718
2719 const struct inode_operations ocfs2_special_file_iops = {
2720         .setattr        = ocfs2_setattr,
2721         .getattr        = ocfs2_getattr,
2722         .permission     = ocfs2_permission,
2723         .get_acl        = ocfs2_iop_get_acl,
2724 };
2725
2726 /*
2727  * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
2728  * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
2729  */
2730 const struct file_operations ocfs2_fops = {
2731         .llseek         = ocfs2_file_llseek,
2732         .read           = do_sync_read,
2733         .write          = do_sync_write,
2734         .mmap           = ocfs2_mmap,
2735         .fsync          = ocfs2_sync_file,
2736         .release        = ocfs2_file_release,
2737         .open           = ocfs2_file_open,
2738         .aio_read       = ocfs2_file_aio_read,
2739         .aio_write      = ocfs2_file_aio_write,
2740         .unlocked_ioctl = ocfs2_ioctl,
2741 #ifdef CONFIG_COMPAT
2742         .compat_ioctl   = ocfs2_compat_ioctl,
2743 #endif
2744         .lock           = ocfs2_lock,
2745         .flock          = ocfs2_flock,
2746         .splice_read    = ocfs2_file_splice_read,
2747         .splice_write   = ocfs2_file_splice_write,
2748         .fallocate      = ocfs2_fallocate,
2749 };
2750
2751 const struct file_operations ocfs2_dops = {
2752         .llseek         = generic_file_llseek,
2753         .read           = generic_read_dir,
2754         .readdir        = ocfs2_readdir,
2755         .fsync          = ocfs2_sync_file,
2756         .release        = ocfs2_dir_release,
2757         .open           = ocfs2_dir_open,
2758         .unlocked_ioctl = ocfs2_ioctl,
2759 #ifdef CONFIG_COMPAT
2760         .compat_ioctl   = ocfs2_compat_ioctl,
2761 #endif
2762         .lock           = ocfs2_lock,
2763         .flock          = ocfs2_flock,
2764 };
2765
2766 /*
2767  * POSIX-lockless variants of our file_operations.
2768  *
2769  * These will be used if the underlying cluster stack does not support
2770  * posix file locking, if the user passes the "localflocks" mount
2771  * option, or if we have a local-only fs.
2772  *
2773  * ocfs2_flock is in here because all stacks handle UNIX file locks,
2774  * so we still want it in the case of no stack support for
2775  * plocks. Internally, it will do the right thing when asked to ignore
2776  * the cluster.
2777  */
2778 const struct file_operations ocfs2_fops_no_plocks = {
2779         .llseek         = ocfs2_file_llseek,
2780         .read           = do_sync_read,
2781         .write          = do_sync_write,
2782         .mmap           = ocfs2_mmap,
2783         .fsync          = ocfs2_sync_file,
2784         .release        = ocfs2_file_release,
2785         .open           = ocfs2_file_open,
2786         .aio_read       = ocfs2_file_aio_read,
2787         .aio_write      = ocfs2_file_aio_write,
2788         .unlocked_ioctl = ocfs2_ioctl,
2789 #ifdef CONFIG_COMPAT
2790         .compat_ioctl   = ocfs2_compat_ioctl,
2791 #endif
2792         .flock          = ocfs2_flock,
2793         .splice_read    = ocfs2_file_splice_read,
2794         .splice_write   = ocfs2_file_splice_write,
2795         .fallocate      = ocfs2_fallocate,
2796 };
2797
2798 const struct file_operations ocfs2_dops_no_plocks = {
2799         .llseek         = generic_file_llseek,
2800         .read           = generic_read_dir,
2801         .readdir        = ocfs2_readdir,
2802         .fsync          = ocfs2_sync_file,
2803         .release        = ocfs2_dir_release,
2804         .open           = ocfs2_dir_open,
2805         .unlocked_ioctl = ocfs2_ioctl,
2806 #ifdef CONFIG_COMPAT
2807         .compat_ioctl   = ocfs2_compat_ioctl,
2808 #endif
2809         .flock          = ocfs2_flock,
2810 };